Skip to content

[heft] Add shard result contracts and output ownership - #6205

Open
Yashwanth Reddy Verupaka (yashwanth195) wants to merge 6 commits into
microsoft:mainfrom
yashwanth195:user/yverupaka/heft-shard-contracts
Open

Yashwanth Reddy Verupaka (yashwanth195) wants to merge 6 commits into
microsoft:mainfrom
yashwanth195:user/yverupaka/heft-shard-contracts

Conversation

@yashwanth195

Copy link
Copy Markdown
Contributor

Summary

Add reusable Heft contracts for workloads that need to publish isolated shard results safely. This standalone
foundation provides identities, versioned manifests, atomic publication and exclusive output claims; it does
not enable managed sharding or change existing task execution.

Details

  • Strict 1-based shard parsing and count-aware temp paths; schema-validated manifest reads/writes distinguish
    missing results from corrupt ones and leave workload payloads opaque.
  • Canonical path validation, alias/overlap checks, short-lived multi-output registration and attempt-fenced
    publication preserve exclusive ownership through cancellation and cleanup. Abandoned claims require verified
    manual recovery, never PID/age-based stealing.
  • Add focused regression suites, the manifest type exports/API report, a minor changefile and contract docs.

Ported only the generic foundation from 6af2a03e5461da3e2ac8bd91ce2d0f5fd91310c9 onto upstream main, with
additional standalone regressions and a trailing-newline parser correction. No unmerged PR is a prerequisite.
Collation/coherence validation, adapter registration, CLI mode wiring, Jest execution/coverage/JUnit and Rush
identity/cache integration remain separate. Callers must keep outputs disjoint from source and payload inputs;
these cooperative filesystem helpers are not a sandbox. Existing unsharded behavior is unchanged.

How it was tested

Validated this standalone port on Windows with Node 24.22.1 and the repository's Rush 5.182.0/Heft/Jest setup.
The integrated source's earlier validation is not used as evidence for this PR.

Command Result
node common\scripts\install-run-rush.js install --to @rushstack/heft Passed; locked scoped install after the initial build reported missing dependencies.
node common\scripts\install-run-rush.js build --to @rushstack/heft Prerequisites passed; Heft generated the new API report with the expected update warning, resolved by the production build below.
node common\scripts\install-run-rush.js build --only @rushstack/heft --production Passed: TypeScript, lint, schema copy and API report validation.
From apps\heft: node ..\..\common\scripts\install-run-rushx.js _phase:test --test-path-pattern sharding --disable-code-coverage Passed: four suites, 100 tests passed, two Windows 8.3-alias cases skipped because this volume exposes no short-path alias.
node common\scripts\install-run-rush.js prettier Passed; staged files formatted.
node common\scripts\install-run-rush.js change --verify --target-branch upstream/main Passed, including after commit.
git diff --cached --check Passed before commit.

Coverage includes identity/schema domains, invalid/corrupt manifests, cancellation boundaries, atomic failure
cleanup, symlinked roots versus nested junctions, filesystem name equivalence, overlapping/concurrent claims,
partial acquisition cleanup, replaced/hardlinked claims and abandoned-gate diagnostics. No POSIX run or broad
Jest adapter/coverage/collator/acceptance sweep was performed.

Impacted documentation

Added docs/heft/shard-contracts.md for implemented guarantees and caller obligations. No existing website
behavior changes. The new schema describes generated result files, so no init template applies.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active issues prevent publishing valid long output filenames and cause unbounded recursion when resolving an absent filesystem root.

2 open issues
What changed in this PR

Adds workload-neutral contracts for safely publishing isolated shard results in Heft. This is a standalone foundation; it does not enable managed sharding or change existing task execution.

Changes:

  • Adds strict shard identities, count-aware temporary paths, and schema-validated result manifests.
  • Introduces canonical output validation, exclusive ownership claims, and cancellation-aware atomic publication.
  • Adds regression tests, public manifest types, contract documentation, and a minor changefile.
File Description
docs/​heft/​shard-contracts.md Documents guarantees, lifecycle, and caller obligations.
common/​reviews/​api/​heft.api.md Records public manifest and status types.
common/​changes/​@rushstack/​heft/​heft-shard-contracts_2026-11-13.json Declares the minor feature release.
apps/​heft/​src/​sharding/​test/​ShardResultManifest.test.ts Tests manifest validation, publication, and cancellation.
apps/​heft/​src/​sharding/​test/​ShardOutputOwnership.test.ts Tests path aliases, ownership contention, and cleanup.
apps/​heft/​src/​sharding/​test/​ShardIdentity.test.ts Tests strict parsing and isolated temporary paths.
apps/​heft/​src/​sharding/​test/​ShardFileSystem.test.ts Tests atomic writes and path validation.
apps/​heft/​src/​sharding/​ShardResultManifest.ts Implements manifest validation, reads, and publication.
apps/​heft/​src/​sharding/​ShardOutputOwnership.ts Implements canonical paths and exclusive output claims.
apps/​heft/​src/​sharding/​ShardIdentity.ts Adds shard parsing, formatting, and folder helpers.
apps/​heft/​src/​sharding/​ShardFileSystem.ts Adds path validation and guarded atomic writes.
apps/​heft/​src/​sharding/​IShardResultManifest.ts Defines workload-neutral result contracts.
apps/​heft/​src/​schemas/​shard-result-manifest.schema.json Defines the versioned manifest schema.
apps/​heft/​src/​index.ts Exports manifest and status types.

Review effort: Balanced

const folder: string = path.dirname(filename);
await fs.mkdir(folder, { recursive: true });
abortSignal?.throwIfAborted();
const temporaryPath: string = path.join(folder, `.${path.basename(filename)}.${randomUUID()}.tmp`);

@dmichon-msft David Michon (dmichon-msft) Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All output files must be deterministically named. Generating random files will completely wreck build cache file tracing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed generated staging names in ab06af8. Publication now takes a fixed, caller-declared staging path independent of the destination basename; manifests use shard-manifest.partial.json. Long leaf and ancestor cases are covered.

return await fs.realpath(filename);
} catch (error) {
if (!FileSystem.isNotExistError(error as Error)) throw error;
return path.join(await resolveShardPathAsync(path.dirname(filename)), path.basename(filename));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the canonicalization and ownership layer, including this recursion, in ab06af8. Path checks are lexical; actual filesystem failures propagate from the write.

targetPath: string,
abortSignal?: AbortSignal
): Promise<void> {
const relative: string = path.relative(buildFolderPath, targetPath);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Way too expensive. Enforce literally that targetPath.startsWith(${buildFolderPath}${path.sep})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the filesystem walk with a lexical prefix check on normalized paths in ab06af8. Configuration validation performs no filesystem I/O.


"generation": {
"title": "Shard Generation",
"description": "An opaque token identifying the sharded run that produced this manifest. The collator requires it to be uniform across every manifest, which is what prevents results from two different runs being merged together.",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With build cache and incremental runs, merging results from two different runs is an explicit goal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed generation and attempt tokens in ab06af8. The manifest no longer requires results from the same run. Rush owns cache compatibility; consumer collation and mixed-cache acceptance are being updated separately.


"workload": {
"title": "Workload Name",
"description": "The kind of work this shard performed, for example \"jest\" or \"lint\". The neutral core treats this as opaque; only an adapter interprets it.",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be the heft action or phase name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced workload with the configured producer phaseName in ab06af8. The folder helper uses the existing phase/task temp suffix; separate collation work will explicitly name that producer phase/task rather than compare action aliases.

Comment thread docs/heft/shard-contracts.md Outdated

## Paths and exclusive ownership

Resolve build-root-relative configuration paths against the build root before calling the path helpers.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't resolve build-root-relative configuration paths at all. Simply ensure that they don't walk up out of the build root.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Configuration stays project-relative in ab06af8. Validation is lexical, including escape and reserved-device checks, with no realpath calls or directory creation. Callers join to the build root only at the I/O boundary.

Comment thread docs/heft/shard-contracts.md Outdated
ancestors without creating missing inputs; `resolveShardOutputPathAsync()` validates and materializes output
directories, but never creates a result file. It resolves physical aliases, including Windows short paths.

Pass canonical destinations to `acquireShardOutputLocksAsync()`. Claims exclude overlapping ancestors and

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there locks involved in this process at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the locks and claim registry in ab06af8. Single-writer scheduling and waiting for producer cleanup are orchestrator responsibilities, not a new filesystem protocol in this foundation.

Comment thread docs/heft/shard-contracts.md Outdated
Output contention fails immediately; registration contention has a two-second retry budget, not an I/O timeout.
Partial acquisition failures release acquired claims and surface cleanup failures.

Claims live in the reserved `<buildRoot>/.heft-jest-shard-locks` protocol namespace, outside shard outputs.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they don't, because they are completely unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this namespace and its recovery protocol in ab06af8, along with the ownership implementation and tests.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:heft-sharding Sharding feature for heft especially for jest

Projects

Status: Needs triage

Development

Successfully merging this pull request may close these issues.

3 participants