/** * Canonical atomic-write helper + temp-grammar registry (issue #2035). * * Single source of truth for: * - HOW production code atomically writes files under the project `.swarm/` * root: one same-directory write-temp→fsync→rename helper with exact * `finally` cleanup of its OWN temp only. * - WHAT temporary-file name grammars exist: a frozen registry derived from * the writer census so residue scanners classify candidates by exact * registered grammars instead of substring guesses. * * Why the registry lives here: every residue consumer (close clean stage, * close dry-run, config doctor, diagnose, quarantine/rollback in * `src/services/swarm-residue.ts`) must derive candidates from the SAME * registered grammars, and every atomic writer must be classifiable against * the same table (see `WRITER_CLASSIFICATION` and its ratchet test). * * Containment contract (issue #2035 req 1): the canonical target must live * under a `.swarm/` root, determined by SPELLING — the nearest `.swarm` path * segment between the filesystem root and the target. A pre-existing root * must be a real directory (no symlinks), and no path segment between it and * the target may be a symlink. Semantics are validateSwarmPath-INSPIRED but * NOT identical — see the junction limitation on * `assertSwarmContainedTarget`. */ import { randomBytes } from 'node:crypto'; import * as fs from 'node:fs'; import * as path from 'node:path'; import { invalidateCachedArtifact } from './swarm-artifact-cache'; // ───────────────────────────────────────────────────────────────────────────── // Temp-grammar registry // ───────────────────────────────────────────────────────────────────────────── /** * A registered temporary-file naming grammar. * * `match` is applied to the temp file's BASENAME only, case-sensitively — * near-misses (unicode/case variants, trailing dots, ADS suffixes) must NOT * match, so they are preserved untouched by every scanner. */ export interface SwarmTempGrammar { /** Stable id used in reports, manifests, and telemetry (never renamed). */ readonly id: string; readonly era: 'canonical' | 'current' | 'legacy'; /** Matches the temp basename. Capture group 1 = derivable target basename when `parsesTarget`. */ readonly match: RegExp; /** The grammar embeds a per-invocation uniqueness token (ts/pid/uuid/random). */ readonly token: 'instance' | 'constant'; /** Trusted for quarantine when all eligibility gates pass (issue #2035 req 5). */ readonly quarantineEligible: boolean; /** Whether a target basename can be parsed back out of the temp name. */ readonly parsesTarget: boolean; /** Producing writer sites (file:line) — every known producer is listed. */ readonly producers: readonly string[]; /** Invariant reason the producer is NOT migrated to the canonical helper. */ readonly note?: string; } const UUID_RE = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'; /** * Every temp grammar a swarm writer has ever produced. Grammars marked * `quarantineEligible: false` are constant-name temps: with no per-invocation * token they are indistinguishable from an in-flight writer of ANY process or * from an unrelated file, so scanners report them but never mutate them. * ORDER MATTERS: `matchTempGrammar`/`parseTargetBasename` take the FIRST * match, so more-specific grammars (uuid, two-token, .json-terminated) must * be declared before the generic single-token family. */ export const SWARM_TEMP_GRAMMARS: readonly SwarmTempGrammar[] = [ { id: 'canonical-v1', era: 'canonical', match: /^(.+)\.[0-9a-f]{32}\.tmp$/, token: 'instance', quarantineEligible: true, parsesTarget: true, producers: [ 'src/utils/atomic-write.ts:atomicWriteSwarmFile', // The any-root variant shares the same writeAtomicSync core and // emits the identical grammar (PR review PRR-018): 'src/utils/atomic-write.ts:atomicWriteFileAnyRoot', // The create-only variant emits the identical grammar and // publishes it with link() instead of rename(): 'src/utils/atomic-write.ts:createExclusiveSwarmFileSync', ], }, { id: 'target-suffix-tmp-uuid', era: 'current', match: new RegExp(`^(.+)\\.tmp\\.${UUID_RE}$`), token: 'instance', quarantineEligible: true, parsesTarget: true, producers: [ 'src/commands/handoff.ts (pre-#2035; site=handoff.md; migrated-to=src/commands/handoff.ts:handleHandoffCommand)', 'src/commands/handoff.ts (pre-#2035; site=handoff-prompt.md; migrated-to=src/commands/handoff.ts:handleHandoffCommand)', 'src/memory/local-jsonl-provider.ts:1288', 'src/config/bundled-skills.ts:403', ], }, { id: 'target-suffix-tmp-pid-uuid', era: 'current', match: new RegExp(`^(.+)\\.tmp\\.\\d+\\.${UUID_RE}$`), token: 'instance', quarantineEligible: true, parsesTarget: true, producers: [ 'src/hooks/pr-workflow-gate.ts (pre-#2385; migrated-to=src/pr-review/persistence.ts:writeAtomicJson)', ], note: 'pre/post-rename file-identity verification (assertOpened/ClosedSwarmFileIdentity, pr-workflow-gate.ts:17880-17980) is writer-specific and load-bearing', }, { id: 'target-suffix-tmp-num-num-json', era: 'current', match: /^(.+)\.tmp\.\d+\.\d+\.json$/, token: 'instance', quarantineEligible: true, parsesTarget: true, producers: ['src/hooks/issue-trace-state.ts:466'], note: 'the .json terminator keeps this shape distinct from the bare two-token family — declared before it so first-match wins', }, { id: 'target-suffix-tmp-num-alnum', era: 'current', match: /^(.+)\.tmp\.\d+\.[0-9a-z]+$/, token: 'instance', quarantineEligible: true, parsesTarget: true, producers: [ 'src/evidence/task-file.ts (pre-#2035; migrated-to=src/evidence/task-file.ts:atomicWriteFile)', 'src/scope/scope-persistence.ts (pre-#2035; migrated-to=src/scope/scope-persistence.ts:atomicWrite)', 'src/scope/scope-persistence.ts (pre-#2035; migrated-to=src/scope/scope-persistence.ts:atomicWriteSync)', 'src/plan/manager.ts:703', 'src/plan/manager.ts:2261', 'src/plan/manager.ts:2321', 'src/review/evidence.ts:233', 'src/turbo/lean/evidence.ts:188', 'src/summaries/manager.ts:172', 'src/evidence/manager.ts:329', 'src/evidence/manager.ts:556', 'src/tools/sast-baseline.ts:199', 'src/tools/repo-graph/storage.ts:543', 'src/memory/reflection-service.ts (pre-#2035; migrated-to=src/memory/reflection-service.ts:atomicWrite)', 'src/hooks/review-receipt.ts:727', 'src/hooks/review-receipt.ts:793', 'src/hooks/review-receipt.ts:1025', 'src/knowledge/identity.ts:234', 'src/evidence/phase-participation.ts (pre-#2035; migrated-to=src/evidence/phase-participation.ts:atomicWriteBytes)', 'src/turbo/lean/integration.ts:427', 'src/turbo/lean/reviewer.ts:401', 'src/plan/ledger.ts:1497', 'src/services/synonym-map.ts:389', 'src/services/skill-optimizer/store.ts:340', 'src/services/skill-optimizer/store.ts:503', 'src/commands/archive-sqlite.ts:318', 'src/memory/jsonl-migration.ts:161', ], note: 'fsync-discipline / lock-scoped fd writers whose temp naming is pinned by their own durability test suites (plan-durability invariant 5, #2034 crash matrix); grammars registered here so their residue stays discoverable', }, { id: 'target-suffix-tmp-token', era: 'current', match: /^(.+)\.tmp\.[0-9a-z-]{6,}$/, token: 'instance', quarantineEligible: true, parsesTarget: true, producers: [ 'src/turbo/lean/state.ts:129', 'src/turbo/lean/recovery.ts:175', 'src/hooks/delegation-gate/worktree-merge-status.ts:132', 'src/plan/ledger.ts:613', ], }, { id: 'target-suffix-tmp-dash', era: 'current', match: /^(.+)\.tmp-\d+-[0-9a-z-]+$/, token: 'instance', quarantineEligible: true, parsesTarget: true, producers: [ 'src/services/skill-evaluator.ts (pre-#2035; migrated-to=src/services/skill-evaluator.ts:atomicWrite)', 'src/services/skill-improver.ts (pre-#2035; migrated-to=src/services/skill-improver.ts:atomicWrite)', 'src/services/skill-consolidation.ts (pre-#2035; migrated-to=src/services/skill-consolidation.ts:atomicWrite)', 'src/services/skill-generator.ts (pre-#2035; migrated-to=src/services/skill-generator.ts:atomicWrite)', 'src/tools/spec-write.ts:113', 'src/sdd/effective-spec.ts:1756', 'src/background/lane-output-store.ts:353', 'src/background/pending-delegations.ts:writeDurableFileSync', 'src/background/delegation-health.ts:429', 'src/tools/submit-phase-council-verdicts.ts:477', 'src/summaries/store.ts (pre-#2035; migrated-to=src/summaries/store.ts:writeRawSidecar)', 'src/retention/jsonl-cap.ts:213', 'src/services/skill-changelog.ts:58', 'src/evidence/receipt-store.ts:414', ], note: 'background lane/delegation stores keep bounded-retry rename semantics wired into their callers (#2034/#2276 delivery contracts); migration candidates for a follow-up but grammars registered', }, { id: 'target-suffix-tmp-dash-num', era: 'current', match: /^(.+)\.tmp-\d+$/, token: 'instance', quarantineEligible: true, parsesTarget: true, producers: ['src/services/skill-improver-quota.ts:114'], }, { id: 'target-dot-pid-uuid-tmp', era: 'current', match: new RegExp(`^(.+)\\.\\d+\\.${UUID_RE}\\.tmp$`), token: 'instance', quarantineEligible: true, parsesTarget: true, producers: ['src/hooks/knowledge-receipt-ledger-storage.ts:591'], note: 'pre/post-rename file-identity verification (existingFileIdentity checks, knowledge-receipt-ledger-storage.ts:544-591) is writer-specific and load-bearing', }, { id: 'dot-numeric-instance-tmp', era: 'current', match: /^\..+(?:\.\d+){2,4}\.[0-9a-z]+\.tmp$/, token: 'instance', quarantineEligible: true, parsesTarget: false, producers: [ 'src/utils/bun-compat.ts:138', 'src/tools/repo-graph/freshness.ts:171', ], note: 'bunWrite Node-fallback internal temp and the repo-graph fingerprint temp (2-4 numeric token groups); leading-dot temps are standalone, target not parsed', }, { id: 'dot-uuid-instance-tmp', era: 'current', match: new RegExp(`^\\..+\\.${UUID_RE}\\.tmp$`), token: 'instance', quarantineEligible: true, parsesTarget: true, producers: [ 'src/tools/write-pr-review-artifact.ts:172', 'src/tools/write-pr-review-artifact.ts:192', // Symbol form: shift-proof against import-order drift in the writer // (issue #3094 re-pin; the old line-form anchor pointed at :105). 'src/tools/write-pr-review-trigger-eval.ts:createTriggerReceipt', ], note: 'bundled-skills writes outside .swarm (project skill roots) and keeps its own contained-directory logic (invariant 4 bundled-skill ownership)', }, { id: 'tmp-prefix-named', era: 'legacy', match: /^\.tmp-.+-\d+-\d+$/, token: 'instance', quarantineEligible: true, parsesTarget: false, producers: [ 'src/commands/issue.ts (pre-#2035; migrated-to=src/commands/issue.ts:atomicWriteFileSync)', ], note: 'prefix form embeds the filename; target not reliably parseable (dashes), so eligibility relies on the age/tracked/symlink/lock gates', }, { id: 'target-rebuild-close', era: 'current', match: /^(.+)\.(?:rebuild|close)\.\d+\.[0-9a-z]+$/, token: 'instance', quarantineEligible: true, parsesTarget: true, producers: ['src/plan/manager.ts:2520', 'src/plan/manager.ts:2582'], note: 'plan-durability (invariant 5) fd-write paths; grammars registered, writers unchanged in this PR', }, { id: 'target-migration-pid', era: 'current', match: /^(.+)\.migration-\d+$/, token: 'instance', quarantineEligible: true, parsesTarget: true, producers: [ 'src/scope/scope-persistence.ts (pre-#2035; migrated-to=src/scope/scope-persistence.ts:atomicWriteSync)', ], }, { id: 'dot-tmp-prefix-legacy', era: 'legacy', match: /^\.tmp\..+$/, token: 'constant', quarantineEligible: true, parsesTarget: false, producers: [ 'pre-7.x writers (pre-#2035; scanner=src/services/swarm-residue.ts:collectCandidates)', ], note: 'legacy prefix grammar: issue #2035 req 7 preserves its handling — stale instances are quarantined (formerly unlinked) under the same eligibility gates', }, { id: 'dot-constant-tmp', era: 'current', match: /^\..+\.tmp$/, token: 'constant', quarantineEligible: false, parsesTarget: false, producers: [ 'src/tools/write-drift-evidence.ts:286', 'src/tools/write-mutation-evidence.ts:173', 'src/tools/write-hallucination-evidence.ts:129', 'src/tools/write-final-council-evidence.ts:424', ], note: 'constant-name temps: no per-invocation token — reported when stale, never auto-quarantined (may be an in-flight writer of any process)', }, { id: 'target-constant-tmp', era: 'current', match: /^(.+)\.tmp$/, token: 'constant', quarantineEligible: false, parsesTarget: false, producers: [ 'src/tools/checkpoint.ts:234', 'src/full-auto/state.ts:638', 'src/context-map/persistence.ts:220', 'src/context-map/capsule-persistence.ts:111', 'src/test-impact/history-store.ts:297', 'src/test-impact/history-store.ts:302', 'src/test-impact/history-store.ts:429', 'src/test-impact/history-store.ts:435', 'src/hooks/promotion-evidence-store.ts:112', 'src/hooks/skill-usage-log.ts:1165', 'src/hooks/skill-usage-log.ts:1675', 'src/hooks/skill-usage-pending.ts:817', 'src/parallel/file-locks.ts:1107', 'src/plan/ledger.ts:1830', ], note: 'constant-name temps: reported when stale, never auto-quarantined. file-locks meta sidecars live under the scanner-skipped .swarm/locks/ subtree; the ledger reconcile temp embeds instance tokens but its .tmp-suffix shape maps here (conservatively report-only). The ledger ARCHIVE temp (src/plan/ledger.ts:613) is NOT listed here — its shape is `.tmp.`, which belongs to the target-suffix-tmp-token grammar', }, ]; /** First registered grammar whose regex matches `basename`, or undefined. */ export function matchTempGrammar( basename: string, ): SwarmTempGrammar | undefined { return SWARM_TEMP_GRAMMARS.find((g) => g.match.test(basename)); } /** * Parse the target basename a temp name was derived from (capture group 1). * Returns null for prefix/leading-dot grammars with no derivable target. */ export function parseTargetBasename(basename: string): string | null { for (const g of SWARM_TEMP_GRAMMARS) { if (!g.parsesTarget) continue; const m = g.match.exec(basename); if (m?.[1]) return m[1]; } return null; } /** * Structural classification of every file that constructs temp-file names * (ratchet source for tests/unit/utils/atomic-write.test.ts — a new temp * constructor must be classified here or the build fails): * - migrated: writes through the canonical helper (registry lists its * grammar only for historical residue discovery) * - registered-bespoke: keeps a dedicated writer with a documented invariant * reason in the grammar `note` (issue #2035 req 2) * - external: writes outside the `.swarm` containment boundary * - reader-only: references temp names without constructing them */ export const WRITER_CLASSIFICATION: Readonly< Record > = Object.freeze({ 'src/commands/close.ts': 'migrated', // These stage modules mention the registered .tmp grammar only in comments; // they do not construct temporary paths or perform atomic writes. 'src/commands/close/context.ts': 'reader-only', 'src/commands/close/clean-stage.ts': 'reader-only', 'src/commands/handoff.ts': 'migrated', 'src/commands/simulate.ts': 'migrated', 'src/commands/coupling.ts': 'migrated', 'src/commands/issue.ts': 'migrated', 'src/evidence/task-file.ts': 'migrated', 'src/evidence/phase-participation.ts': 'migrated', 'src/memory/reflection-service.ts': 'migrated', 'src/scope/scope-persistence.ts': 'migrated', 'src/background/stage-b-dispatch-binding-store.ts': 'migrated', 'src/services/skill-consolidation.ts': 'migrated', 'src/services/skill-evaluator.ts': 'migrated', // mixed .swarm/.opencode-skill destinations — delegates to the any-root // variant of the canonical helper (same dual-destination reason as task-file) 'src/services/skill-generator.ts': 'migrated', 'src/services/skill-improver.ts': 'migrated', 'src/summaries/store.ts': 'migrated', // delegates every atomic rewrite to the canonical helper (trajectory data // file + checkpoint, issue #2041); its `.tmp` mention is the cleanup // READER reaping stale atomic-write leftovers, not a constructor 'src/prm/trajectory-store.ts': 'migrated', // registered-bespoke: grammar registered, writer kept (see grammar notes) // issue #2483 bounded writers: bespoke crash-atomic tmp+rename rewrites, // grammars registered (target-suffix-tmp-dash / target-constant-tmp) // issue #2485: opt-in OTLP export spool/state atomic rewrites 'src/observability/otlp-exporter.ts': 'registered-bespoke', 'src/retention/jsonl-cap.ts': 'registered-bespoke', 'src/services/skill-changelog.ts': 'registered-bespoke', 'src/background/delegation-health.ts': 'registered-bespoke', 'src/background/lane-output-store.ts': 'registered-bespoke', 'src/background/pending-delegations.ts': 'registered-bespoke', 'src/commands/archive-sqlite.ts': 'registered-bespoke', 'src/context-map/capsule-persistence.ts': 'registered-bespoke', 'src/context-map/persistence.ts': 'registered-bespoke', // bespoke atomic single-file rewrite (write tmp + rename) for the bounded // context-map telemetry store; PID-scoped `.context-telemetry.jsonl..tmp` 'src/context-map/telemetry.ts': 'registered-bespoke', // bespoke atomic single-file rewrites (write tmp + rename) for the bounded // core event store and its authority index (issue #2039); PID-scoped // `.events.jsonl..tmp` / `.events-authority-index.json..tmp` 'src/events/core-events.ts': 'registered-bespoke', // bespoke atomic single-file rewrites (write tmp + rename) for the bounded // shell-audit security store (issue #2040); PID-scoped // `.shell-audit.jsonl..tmp` 'src/hooks/guardrails/shell-audit-store.ts': 'registered-bespoke', 'src/evidence/documents-retention.ts': 'registered-bespoke', 'src/evidence/manager.ts': 'registered-bespoke', // #2968: verification-receipt export bundle staging dir — // `.${bundleName}.tmp-${pid}-${ts}` (target-suffix-tmp-dash grammar); // receipt artifacts themselves go through writeImmutableArtifact 'src/evidence/receipt-store.ts': 'registered-bespoke', 'src/full-auto/state.ts': 'registered-bespoke', 'src/hooks/delegation-gate/worktree-merge-status.ts': 'registered-bespoke', 'src/hooks/delegation-gate/worktree-provisioning-owner.ts': 'migrated', 'src/hooks/issue-trace-state.ts': 'registered-bespoke', 'src/hooks/knowledge-receipt-ledger-storage.ts': 'registered-bespoke', 'src/hooks/pr-workflow-gate.ts': 'registered-bespoke', // bespoke atomic single-file rewrite (write tmp + rename) for the bounded // PR-review re-entry authorization store (issues #2383/#2385; moved to the // src/pr-review/ boundary); UUID-scoped `.${basename}.${uuid}.tmp` 'src/pr-review/authorization.ts': 'registered-bespoke', // bespoke atomic single-file rewrite (write tmp + rename) for the PR-review // gate-state and salvage reads (issues #2385/#2510); the logical stream is // registered in the retention data set: swarm.db coordination_state is the // authority and `.swarm/pr-workflow-gates/*.json` is the bounded projection; // UUID-scoped `.tmp..` 'src/pr-review/persistence.ts': 'registered-bespoke', 'src/hooks/promotion-evidence-store.ts': 'registered-bespoke', 'src/hooks/review-receipt.ts': 'registered-bespoke', 'src/hooks/skill-usage-log.ts': 'registered-bespoke', 'src/hooks/skill-usage-pending.ts': 'registered-bespoke', 'src/knowledge/identity.ts': 'registered-bespoke', 'src/memory/jsonl-migration.ts': 'external', 'src/memory/local-jsonl-provider.ts': 'registered-bespoke', 'src/plan/ledger.ts': 'registered-bespoke', 'src/plan/manager.ts': 'registered-bespoke', 'src/review/evidence.ts': 'registered-bespoke', 'src/sdd/effective-spec.ts': 'registered-bespoke', 'src/services/skill-improver-quota.ts': 'registered-bespoke', 'src/services/skill-optimizer/activation.ts': 'external', 'src/services/skill-optimizer/store.ts': 'registered-bespoke', 'src/services/skill-reviser.ts': 'external', 'src/services/synonym-map.ts': 'registered-bespoke', 'src/session/snapshot-writer.ts': 'registered-bespoke', 'src/summaries/manager.ts': 'registered-bespoke', 'src/test-impact/history-store.ts': 'registered-bespoke', 'src/turbo/lean/evidence.ts': 'registered-bespoke', 'src/turbo/lean/integration.ts': 'registered-bespoke', 'src/turbo/lean/recovery.ts': 'registered-bespoke', 'src/turbo/lean/reviewer.ts': 'registered-bespoke', 'src/turbo/lean/state.ts': 'registered-bespoke', 'src/tools/apply-patch.ts': 'external', 'src/tools/checkpoint.ts': 'registered-bespoke', // Issue #2788: the seven record_* receipt tools migrated from hand-rolled // temp+rename writes to atomicWriteSwarmFile — 'migrated' records that // they no longer construct temp files themselves. 'src/tools/record-implementation-review.ts': 'migrated', 'src/tools/record-issue-publication.ts': 'migrated', 'src/tools/record-issue-reproduction.ts': 'migrated', 'src/tools/record-recurrence-sweep.ts': 'migrated', 'src/tools/record-branch-freshness.ts': 'migrated', 'src/tools/record-trace-validation.ts': 'migrated', 'src/tools/record-merge-approval.ts': 'migrated', 'src/tools/repo-graph/freshness.ts': 'registered-bespoke', 'src/tools/repo-graph/storage.ts': 'registered-bespoke', 'src/tools/sast-baseline.ts': 'registered-bespoke', 'src/tools/spec-write.ts': 'registered-bespoke', 'src/tools/submit-phase-council-verdicts.ts': 'registered-bespoke', 'src/tools/write-drift-evidence.ts': 'registered-bespoke', 'src/tools/write-final-council-evidence.ts': 'registered-bespoke', 'src/tools/write-hallucination-evidence.ts': 'registered-bespoke', 'src/tools/write-mutation-evidence.ts': 'registered-bespoke', 'src/tools/write-pr-review-artifact.ts': 'registered-bespoke', 'src/tools/write-pr-review-trigger-eval.ts': 'registered-bespoke', // external: writes outside .swarm with its own containment logic 'src/config/bundled-skills.ts': 'external', 'src/services/config-doctor.ts': 'external', // registered-bespoke (final-critic round): lock-metadata sidecar temps // under the scanner-skipped .swarm/locks/ subtree; see target-constant-tmp 'src/parallel/file-locks.ts': 'registered-bespoke', // registered-bespoke (final-critic round): bunWrite's Node-fallback // internal temp (dot-numeric-instance-tmp grammar) 'src/utils/bun-compat.ts': 'registered-bespoke', // reader-only: filters/comments/example strings over temp names, never // constructs them 'src/background/plan-sync-worker.ts': 'reader-only', 'src/config/lane-permissions.ts': 'reader-only', 'src/environment/profile.ts': 'reader-only', 'src/worktree/core.ts': 'reader-only', }); // ───────────────────────────────────────────────────────────────────────────── // Containment // ───────────────────────────────────────────────────────────────────────────── /** * Max depth considered when reasoning about `.swarm` path segments (advisory * constant kept for diagnostics; containment itself is spelling-based). */ export const MAX_SWARM_ANCESTOR_DEPTH = 8; export interface SwarmContainment { /** Project root that owns the `.swarm/` directory. */ projectRoot: string; /** Canonical `.swarm/` directory containing the target. */ swarmRoot: string; } /** * Reject obviously-invalid target spellings before any filesystem access: * glob metacharacters, control chars/NUL, and unresolved environment-variable * forms (`$VAR`, `%VAR%`, leading `~`). Quarantine/inventory paths and writer * paths share this gate (issue #2035 req 7). */ export function assertWellFormedTargetPath(targetPath: string): void { if (!path.isAbsolute(targetPath)) { throw new Error(`atomic-write target must be absolute: ${targetPath}`); } // biome-ignore lint/suspicious/noControlCharactersInRegex: intentionally matching control chars to reject them (doctor.ts precedent) if (/[\0-\x1f\x7f]/.test(targetPath)) { throw new Error('atomic-write target contains control characters'); } if ( /[*?]/.test(targetPath) || /(^|[/\\])\{[^}]*\}([/\\]|$)/.test(targetPath) ) { throw new Error( `atomic-write target contains glob metacharacters: ${targetPath}`, ); } if (/(^|[/\\])~([/\\]|$)/.test(targetPath)) { throw new Error( 'atomic-write target contains unresolved home-dir marker (~)', ); } if ( /\$\{?[A-Za-z_][A-Za-z0-9_]*\}?/.test(targetPath) || /%[^/\\%]+%/.test(targetPath) ) { throw new Error( 'atomic-write target contains unresolved environment-variable form', ); } } function lstatNotSymlink(p: string, what: string): void { const st = fs.lstatSync(p); // throws ENOENT for missing — callers pre-check if (st.isSymbolicLink()) { throw new Error(`${what} is a symlink/reparse point: ${p}`); } } /** * Assert `targetPath` lives beneath a `.swarm/` root. * * The root is determined by SPELLING: the nearest `.swarm` path segment * between the filesystem root and the target. Existence-based ancestor * walking is deliberately avoided — a stray `.swarm` in an ancestor (e.g. the * OS temp dir) would hijack the walk and reject legitimate first-writes whose * own `.swarm` parent does not exist yet (invariant 4: ambiguous ancestor * `.swarm` state fails closed). If the `.swarm` root exists it must be a real * directory (no symlink), and no existing path segment between it and the * target may be a symlink. * * KNOWN LIMITATION vs `validateSwarmPath` (src/hooks/utils.ts:157, PR review * PRR-007): this check detects SYMLINKS via lstatSync().isSymbolicLink(), * which returns FALSE for Windows directory JUNCTIONS (a distinct reparse * class). validateSwarmPath resolves all reparse points via realpathSync. A * junctioned `.swarm/` therefore passes this spelling-based gate. Mitigations * in scope: junctions require same-user filesystem write access to create * (outside the adversarial-local-process threat model invariant 4 excludes), * targets are still spelling-bounded to the `.swarm` subtree, and the * spell-time `.swarm` position must already exist in the user's path. Full * junction parity would require realpath-resolution of every existing * ancestor — deliberately out of budget for the write hot path. * * Project-boundary enforcement (`.git`/`.opencode` markers, invariant 4) is * deliberately NOT re-checked here: that belongs to the tool-layer * resolvers (`resolveWorkingDirectory`/`assertProjectRoot`), which have * already validated the directory before any writer derived a target from * it — validateSwarmPath applies the same split. */ export function assertSwarmContainedTarget( targetPath: string, ): SwarmContainment { assertWellFormedTargetPath(targetPath); const resolved = path.normalize(path.resolve(targetPath)); const segments = resolved.split(path.sep); let swarmIdx = -1; for (let i = segments.length - 2; i >= 1; i--) { if (segments[i] === '.swarm') { swarmIdx = i; break; } } if (swarmIdx === -1) { throw new Error( `atomic-write target is not under a project .swarm root: ${targetPath}`, ); } const swarmRoot = segments.slice(0, swarmIdx + 1).join(path.sep); // A pre-existing `.swarm` root must be a real directory. ENOENT is fine — // the mkdir in writeAtomicSync creates it (first write into a fresh root). try { const st = fs.lstatSync(swarmRoot); if (!st.isDirectory() || st.isSymbolicLink()) { throw new Error( `.swarm root is not a real directory (symlink/reparse or non-dir): ${swarmRoot}`, ); } } catch (err) { if ((err as NodeJS.ErrnoException)?.code !== 'ENOENT') throw err; } const rel = path.relative(swarmRoot, resolved); if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) { throw new Error(`atomic-write target escapes .swarm root: ${targetPath}`); } // Deny symlinked segments between swarmRoot and the target's nearest // existing ancestor (the not-yet-existing tail cannot be a link). let probe = swarmRoot; for (const seg of rel.split(path.sep)) { probe = path.join(probe, seg); if (!fs.existsSync(probe)) break; lstatNotSymlink(probe, 'path segment under .swarm'); } return { projectRoot: path.dirname(swarmRoot), swarmRoot }; } // ───────────────────────────────────────────────────────────────────────────── // Canonical writer // ───────────────────────────────────────────────────────────────────────────── /** Hard bound on a single atomic write (bounded-write requirement). */ export const MAX_ATOMIC_WRITE_BYTES = 256 * 1024 * 1024; const RENAME_RETRY_DELAYS_MS = [10, 25, 50, 100, 200] as const; const RETRYABLE_RENAME_CODES = new Set(['EPERM', 'EBUSY', 'EACCES', 'EEXIST']); // ───────────────────────────────────────────────────────────────────────────── // Recent-write intent tracking (issue #2472 W3 / PERF-1) // ───────────────────────────────────────────────────────────────────────────── /** Bound on distinct tracked target paths (invariant 8: bounded + FIFO). */ const MAX_RECENT_WRITE_TRACKED_PATHS = 256; /** * Default window within which an ENOENT read of a target this process wrote * (or is writing) is treated as plausibly mid-rename rather than never-existed * — see `wasRecentlyWrittenByThisProcess` and the three-way ENOENT policy in * `readSwarmFileAsync` (src/hooks/utils.ts). */ export const RECENT_WRITE_WINDOW_MS = 500; /** * Resolved target path → most recent write-intent OR write-completion stamp. * The latest stamp governs: a completed write refreshes the intent stamp so * read-your-own-write stays inside the window even on slow filesystems. */ const recentWriteActivity = new Map(); function noteRecentWriteActivity(resolvedTarget: string): void { // delete-before-set refreshes insertion order, so FIFO eviction below // always drops the least-RECENTLY-written path, not the first-ever one. recentWriteActivity.delete(resolvedTarget); recentWriteActivity.set(resolvedTarget, Date.now()); while (recentWriteActivity.size > MAX_RECENT_WRITE_TRACKED_PATHS) { const oldest = recentWriteActivity.keys().next().value; if (oldest === undefined) break; recentWriteActivity.delete(oldest); } } /** * Remove the recent-write marker for `targetPath` (PRR-008). Called from the * canonical writer's failure path: a write that throws before its rename * completes must not leave a stale INTENT stamp behind, or a subsequent ENOENT * read of the target would be misclassified as a rename race and retried * against a file this process is no longer producing. Successful writes are * unaffected — they reach the WRITE COMPLETION refresh, which re-stamps the * marker for the post-rename visibility window. Returns true when a stamp was * present and removed. */ export function clearRecentWriteActivity(targetPath: string): boolean { return recentWriteActivity.delete(path.resolve(targetPath)); } /** * Whether this process recorded a write INTENT or write COMPLETION for * `targetPath` within the last `withinMs` milliseconds (default * {@link RECENT_WRITE_WINDOW_MS}). * * Consumers: the ENOENT retry ladders (`readSwarmFileAsync`, * `readFileOrEmpty` in src/services/context-budget-service.ts) use this as * the "evidenced rename-race" arm — an ENOENT on a file THIS process just * wrote (or is mid-write on: intent is recorded before the temp file write * begins) retries, while an ENOENT with no evidence the file ever existed * returns immediately (issue #2472 PERF-1: 4×10ms of sleeps per missing-file * read, ~1.9s/turn on plan-less projects). */ export function wasRecentlyWrittenByThisProcess( targetPath: string, withinMs: number = RECENT_WRITE_WINDOW_MS, ): boolean { const last = recentWriteActivity.get(path.resolve(targetPath)); if (last === undefined) return false; return Date.now() - last <= withinMs; } export interface AtomicWriteOptions { /** Skip the pre-rename fsync (tests / truly ephemeral data). */ readonly skipFsync?: boolean; /** File mode for the per-write temporary file before rename. */ readonly mode?: number; /** * Override the bounded-write cap (default MAX_ATOMIC_WRITE_BYTES). Exposed * so the bound itself is testable without allocating 256 MiB. */ readonly maxBytes?: number; /** * Same-tick destination validation (swarm-pr-feedback PR #3550 FB-001 * round 3): invoked AFTER the writer's event-loop yield and IMMEDIATELY * before the synchronous temp-write/rename sequence. A caller that * pre-validated its destination still has an in-process interleaving * window at the writer's `setImmediate`; running the check here — same * tick as the mutation syscalls — closes it. */ readonly validateTarget?: () => void; } /** * Dependency-injection seam for failure-injection tests (repo convention; * never mock.module — it leaks across bun:test files). Restore in afterEach. * * The defaults DELEGATE to the live `fs` namespace on every call (instead of * capturing the function at module load) so sibling tests that patch * `node:fs` via `mock.module` — while still calling through to the real * implementation — observe these calls, matching the recording patterns in * tests/unit/commands/atomic-writes.test.ts. */ export const _internals = { randomSuffix: (): string => randomBytes(16).toString('hex'), writeSync: ( fd: number, buffer: Uint8Array, offset: number, length: number, ): number => fs.writeSync(fd, buffer, offset, length), renameSync: (from: string, to: string): ReturnType => fs.renameSync(from, to), unlinkSync: (p: string): ReturnType => fs.unlinkSync(p), fsyncSync: (fd: number): ReturnType => fs.fsyncSync(fd), }; function toBuffer(content: string | Uint8Array): Uint8Array { if (typeof content === 'string') return Buffer.from(content, 'utf-8'); return content; } /** Portable synchronous sleep (pending-delegations.ts/transient-retry precedent). * The busy-wait fallback is theoretical on supported runtimes (Atomics.wait is * available on all Node/Bun versions this repo ships) and burns CPU only when * Atomics is unavailable — bounded by RENAME_RETRY_DELAYS_MS (385ms worst * case). Kept for precedent-consistency (PR review PRR-011: documented, not * load-bearing). */ function syncSleep(ms: number): void { try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); } catch { const start = Date.now(); while (Date.now() - start < ms) { /* bounded busy-wait fallback */ } } } /** Bounded rename with the retryable-delay loop (issue #2969: one canonical * implementation consumed by the full-auto state writer and the OTLP * exporter instead of per-file raw `renameSync` calls). Semantics are the * long-standing internal contract: up to 6 attempts, sleeping * RENAME_RETRY_DELAYS_MS between retryable failures (EPERM/EBUSY/EACCES/ * EEXIST — Windows AV scanners transiently hold freshly written files), * immediate rethrow for non-retryable errors, rethrow of the last error on * exhaustion. Routes through the `_internals.renameSync` seam like every * other write primitive here. */ export function renameSyncWithRetry( tempPath: string, targetPath: string, delays: readonly number[] = RENAME_RETRY_DELAYS_MS, ): void { let lastError: unknown; for (let attempt = 0; attempt <= delays.length; attempt++) { try { _internals.renameSync(tempPath, targetPath); return; } catch (err) { lastError = err; const code = (err as NodeJS.ErrnoException)?.code; if (!code || !RETRYABLE_RENAME_CODES.has(code)) throw err; if (attempt < delays.length) { syncSleep(delays[attempt] ?? 0); } } } throw lastError; } /** Shared core: validate, write temp, fsync, rename, cleanup own temp. */ function writeAtomicSync( targetPath: string, content: string | Uint8Array, options?: AtomicWriteOptions & { contained?: boolean }, ): void { if (options?.contained !== false) { assertSwarmContainedTarget(targetPath); } else { // Any-root callers (linked/hive knowledge stores) still get the // well-formedness guards — never globs, env forms, or relative paths. assertWellFormedTargetPath(targetPath); } const resolvedTarget = path.resolve(targetPath); // WRITE INTENT (issue #2472 W3): recorded before ANY filesystem work so a // reader that hits ENOENT while this write is in flight (temp write, fsync, // rename) knows the file is being produced and retries instead of taking // the never-existed fast path. Covers both the sync and async exported // writers — they share this core. noteRecentWriteActivity(resolvedTarget); const buffer = toBuffer(content); const maxBytes = options?.maxBytes ?? MAX_ATOMIC_WRITE_BYTES; if (buffer.byteLength > maxBytes) { throw new Error( `atomic write exceeds ${maxBytes} byte bound: ${buffer.byteLength}`, ); } // Canonical grammar: ..tmp — unique, non-predictable, and // derived from the target so residue scanners can attribute it exactly. // The temp shares the target's directory by construction (same-directory // rename requirement). const tempPath = `${resolvedTarget}.${_internals.randomSuffix()}.tmp`; fs.mkdirSync(path.dirname(tempPath), { recursive: true }); // The own-temp finally-unlink must cover the WRITE phase too: a failed // write (ENOSPC/EBADF/…) that propagates before the rename block would // otherwise leak this invocation's temp as new residue (issue #2035 // acceptance: "failed writes clean only their own temp and preserve the // previous target"). try { try { const fd = fs.openSync(tempPath, 'wx', options?.mode); try { let written = 0; while (written < buffer.byteLength) { written += _internals.writeSync( fd, buffer, written, buffer.byteLength - written, ); } if (options?.skipFsync !== true) { try { _internals.fsyncSync(fd); } catch { // fsync unsupported on this FS — durability is best-effort } } } finally { fs.closeSync(fd); } renameSyncWithRetry(tempPath, resolvedTarget); } finally { // Exact finally cleanup of THIS invocation's temp only (no-op after a // successful rename — ENOENT is swallowed). try { _internals.unlinkSync(tempPath); } catch { /* renamed away or never created */ } } } catch (error) { // PRR-008: the write failed before the rename completed. The pre-write // INTENT stamp above must not linger for RECENT_WRITE_WINDOW_MS — an // ENOENT read of this target would then be misclassified as a rename // race and retried against a file this invocation will never produce. // Clear it; the previous target (if any) stays preserved on disk. clearRecentWriteActivity(resolvedTarget); throw error; } // Best-effort parent-dir fsync so the rename itself is durable (macOS/APFS). try { const dirFd = fs.openSync(path.dirname(resolvedTarget), 'r'); try { fs.fsyncSync(dirFd); } finally { fs.closeSync(dirFd); } } catch { // directory fsync unsupported — best-effort } invalidateCachedArtifact(resolvedTarget); // WRITE COMPLETION (issue #2472 W3): refresh the stamp so a read racing the // post-rename directory-entry visibility window stays retry-worthy for the // full RECENT_WRITE_WINDOW_MS after the write finished. noteRecentWriteActivity(resolvedTarget); } /** * Canonical atomic write under the project `.swarm/` root (issue #2035 req 1): * contained target, unique non-predictable same-directory temp, bounded write, * fsync+close where supported, platform-safe replace (bounded rename retry on * Windows transient locks — never unlink-then-rename, which would break * atomicity), and exact finally cleanup of this invocation's temp only. * * The payload is bounded (≤ MAX_ATOMIC_WRITE_BYTES), so the synchronous * temp-write core is the same latency class as the previous per-caller * write-temp implementations; the async signature preserves caller shapes. */ export async function atomicWriteSwarmFile( targetPath: string, content: string | Uint8Array, options?: AtomicWriteOptions, ): Promise { await new Promise((resolve) => setImmediate(resolve)); writeAtomicSync(targetPath, content, options); } /** Synchronous variant of {@link atomicWriteSwarmFile} (same core). */ export function atomicWriteSwarmFileSync( targetPath: string, content: string | Uint8Array, options?: AtomicWriteOptions, ): void { writeAtomicSync(targetPath, content, options); } /** * CREATE-ONLY atomic write for a durable identity artifact. * * `atomicWriteSwarmFileSync` REPLACES its target, which would break the * first-writer-wins semantics a create-only artifact needs (a replay-identity * salt or claim must never be overwritten by a later writer). * * This variant publishes the content with the SAME canonical temp grammar * (`..tmp`), the same fsync-before-publish discipline and the * same own-temp cleanup — and then HARD-LINKS the temp into place instead of * renaming it. `link()` fails with EEXIST when the target already exists, so * the loser of a race observes the winner's file instead of replacing it, and * a reader never observes a partially written target (the temp is complete and * fsync'd before the link publishes it). * * Returns true when THIS caller created the target, false when the target * already existed (the caller lost the race). */ export function createExclusiveSwarmFileSync( targetPath: string, content: string | Uint8Array, options?: AtomicWriteOptions, ): boolean { assertSwarmContainedTarget(targetPath); const resolvedTarget = path.resolve(targetPath); noteRecentWriteActivity(resolvedTarget); const buffer = toBuffer(content); const maxBytes = options?.maxBytes ?? MAX_ATOMIC_WRITE_BYTES; if (buffer.byteLength > maxBytes) { throw new Error( `atomic write exceeds ${maxBytes} byte bound: ${buffer.byteLength}`, ); } const tempPath = `${resolvedTarget}.${_internals.randomSuffix()}.tmp`; fs.mkdirSync(path.dirname(tempPath), { recursive: true }); try { const fd = fs.openSync(tempPath, 'wx', options?.mode); try { let written = 0; while (written < buffer.byteLength) { written += _internals.writeSync( fd, buffer, written, buffer.byteLength - written, ); } if (options?.skipFsync !== true) { try { _internals.fsyncSync(fd); } catch { // fsync unsupported on this FS — durability is best-effort } } } finally { fs.closeSync(fd); } try { fs.linkSync(tempPath, resolvedTarget); return true; } catch (error) { if ((error as NodeJS.ErrnoException).code === 'EEXIST') return false; throw error; } } finally { try { fs.rmSync(tempPath, { force: true }); } catch { // best-effort own-temp cleanup } } } /** * Canonical atomic write WITHOUT the `.swarm` containment requirement. * * Documented invariant reason this variant exists (issue #2035 req 2): the * legacy shared writer `atomicWriteFile` (src/evidence/task-file.ts) serves * DUAL destinations — `.swarm/evidence/**` files AND the linked/hive * knowledge stores whose platform-data/link directories live OUTSIDE any * `.swarm` root (src/hooks/knowledge-store.ts, knowledge-link.ts, * memory-link.ts, family migrations). Those callers migrate to this variant * so they still gain the canonical grammar, fsync, bounded retry, exact * own-temp cleanup, and cache invalidation — everything except root * containment, which their destinations make inapplicable. New `.swarm` * callers MUST use {@link atomicWriteSwarmFile}. */ export async function atomicWriteFileAnyRoot( targetPath: string, content: string | Uint8Array, options?: AtomicWriteOptions, ): Promise { await new Promise((resolve) => setImmediate(resolve)); // FB-001 (review round 3): run the destination validator on the SAME tick // as the synchronous write, after the writer's event-loop yield — no // awaitable boundary remains between "chain verified link-free" and the // temp-write/rename syscalls, so an in-process task that plants a junction // during the yield is caught here rather than writing through it. options?.validateTarget?.(); writeAtomicSync(targetPath, content, { ...options, contained: false }); }