Skip to content

write_to_file publishes its own whole-file read as never-read, and drops the approved outside-workspace identity (split unit planned from pull 2444) #2459

Open

Description

@easonLiangWorldedtech

What the code prints

At head 8948d9a, src/core/tools/WriteToFileTool.ts still publishes with the pre-guard argument lists: saveDirectly is called with five arguments at line 135 and saveChanges with two at line 167, so the guarded kinds and the approved-target arguments stay defaulted.

  • The prevent-focus arm reads the whole file itself at line 114 and records no observation. guardedWrite therefore finds obs undefined and takes its never-read branch (src/core/tools/guardedWrite.ts lines 674-682, createIfAbsent): it refuses a target that this very arm had just read end to end. The arm holds the complete content and the guard is told nothing about it.
  • Neither arm threads the approved outside-workspace identity that saveChanges (src/integrations/editor/DiffViewProvider.ts lines 530-541) and saveDirectly (lines 1689-1704) exist to receive, while the tool computes isOutsideWorkspace at line 88 and puts it only into the approval payload at line 94.
  • The diff-view arm behaves as designed and is not part of this unit: open() records a partial preview observation only when none exists (DiffViewProvider.ts 203-206) and a verified empty placeholder as complete (239-259), so a never-read existing file correctly gets the re-read remediation and a new file publishes against the placeholder token.

Measured size of the fix

32 changed executable lines in package extension, measured with the repo gate's own function (scripts/stryker-diff.mjs buildManifest over the drafted working-tree diff; 49 raw lines added, 4 deleted). The ceiling is 500 changed executable lines per package. Drafted source is preserved in the lane capture at .tmp-n202237-cutshape/sec83/WriteToFileTool-draft.ts.

This is why the fix is a unit of its own rather than a commit on pull 2444: that pull is priced by the hosted gate itself at 1315 changed executable lines (log line of job 132565556554) against the 500 ceiling, so the gate refuses there whatever this delta is, and carrying it would buy the change no mutation proof. On its own branch the delta is 32 of 500 and the gate can run on it.

Plan for the unit

  1. Branch n20/w2f-publish-guard from main at f015dae.
  2. src/core/tools/WriteToFileTool.ts: bracket the prevent-focus arm's whole-file read with stats and record it as a complete observation only while the bracketing stats agree; name the guarded kind the operation is (update for an existing target, create for an absent one); capture canonicalizeForApproval before the ask for an outside-workspace target and thread it, plus the approved flag, into both publish arms. No knob, no suppression, no directive.
  3. Add the regression spec at the lowest layer that would have failed: src/core/tools/tests/writeToFileTool.guardedPublish.spec.ts, asserting the arguments the two publish calls receive and the observation the prevent-focus arm records - not merely that a method was called.
  4. Prove both directions: the new spec red at the pinned head blob, green with the fix; counts printed by each run with the transcripts named and the on-disk hash of the pinned source captured before the run starts.
  5. Run the local mutation preflight on the unit delta (its own base, not main) before pushing, with no Stryker disable directive added.
  6. Lint with the shared prune switch left off from a real pnpm install --frozen-lockfile, prove src/eslint-suppressions.json byte identical to its blob, add no changeset and no changelog entry per AGENTS.md.

Gaps this unit does not claim to close

  • Host scenario: no recorded extension-host scenario exercises the refusal. Both recorded write_to_file scenarios create new files (fixture ids call_write_to_file_create_001 and call_write_to_file_nested_001, expected payload fragments "operation":"created"), so a green host run on that suite shows no regression and does not clear the refusal path. A host scenario that overwrites an existing file would have to be written and is not claimed here.
  • Assertion hole, the same one apply_diff had before its own spec: src/core/tools/tests/writeToFileTool.spec.ts asserts only that saveChanges was called (expect(mockCline.diffViewProvider.saveChanges).toHaveBeenCalled()) and never reads its arguments, so it passes identically with and without the fix.

Where this came from

Named for provenance, with no verdict asked of anyone: pull 2444 (#2444) is where this class of defect was found while repairing that pull's red required leg e2e-mock; the sibling tool was named there and left unfixed. Queue rows q4352 (this unit) and q4344 (the 2444 leg) carry the lane records.

Activity

  1. easonLiangWorldedtech commented on Nov 14, 2026

    @easonLiangWorldedtech
    ContributorAuthor

    Commit 4a909332803cf3cf81a3585d59e76c44a9a4adb4 carries the fix this issue plans, and it lands inside pull #2444 rather than in a pull of its own.

    The reason is one line: the guarded kind and the approved-target parameters this unit threads arrive in pull #2444's own commits and are absent from main tip f015dae5326501ef5eb70616ac698828465b0deb, where src/core/tools/guardedWrite.ts does not resolve as a path at all and src/integrations/editor/DiffViewProvider.ts carries no line naming GuardedWriteKind, writeKind, canonicalizeForApproval or observationRegistry - measured on the blobs: at main tip that DiffViewProvider is blob 3c1d33ae283bed12d0c756503985eb0f9d5180ec with none of those names and saveChanges at line 326 and saveDirectly at line 1142 with no guarded kind or approved target in either signature, while at 8948d9a30d guardedWrite.ts is blob 9bede974293e8e01ca8a1d5996f4bb5e8d4b4132 and DiffViewProvider is blob 03b95a15651f1e7328ee582e53c0d20e0e38ee38 carrying 18 such lines, and merge-base of main tip and the 8948d9a30d head is main tip itself (main tip..8948d9a30d is 5 commits, 8948d9a30d..main tip is none). So a pull of this commit against main would fold #2444's own 33 files and +10,309/-263 into this unit's diff and the gate would price it at 1347 changed executable lines, where the unit's own commit against the base it declares is 32.

    What the commit does, in the shape this issue planned: the prevent-focus arm's whole-file read is bracketed with fs.stat({ bigint: true }) and recorded through task.observationRegistry.observe as a complete observation only while the bracketing tokens agree, which is what takes guardedWrite off its never-read branch; both publish arms now name the kind the operation is (update for an existing target, create for an absent one) instead of leaving it to the signature default; and the outside-workspace identity from canonicalizeForApproval is captured before the approval is asked and threaded, with the approved flag, into saveDirectly and saveChanges.

    Proof run on the pinned source before and after: the new spec src/core/tools/__tests__/writeToFileTool.guardedPublish.spec.ts (sha256 E81B5475CA0E237470518AD2800172562E9B170D4909A5D8C67B5AED8C731A70, 12 tests) prints Tests 11 failed | 1 passed (12) against the pinned head blob 0c5c80abb91ec21307b40d16f72819cfb7206d9b (on-disk sha256 C767216F11E56815AA0D334F772DEA1B280005FB505500D10E0D46AF2752002F) and Tests 12 passed (12) against the fixed blob dc01b4edac91cf8b559ccc2bbe3e0cf7b1c7b668 (on-disk sha256 0A7A478AAF7D6C09390886A390FC5E9D8688AC6BD4CCE97964F9CD50276AA25A). The neighbourhood suites print Tests 323 passed | 5 skipped (328). Locally, the mutation gate run on the unit delta (32 changed executable lines in the extension package against the base the commit declares) instrumented it as Instrumented 1 source file(s) with 34 mutant(s) and printed valid 34, killed 30, timeout 0, survived 4, noCoverage 0, ignored 0 with 4 blocking mutants and result Advisory findings, with no Stryker disable directive added anywhere. All four survivors sit on the bracket agreement condition at lines 134-135 and would need readToken defined while readContent is undefined, or the reverse, a state this arm cannot be in because the read is gated on the token existing; the line 136-137 half of that condition has no surviving mutant. Two earlier runs of the same delta are on the record too: the gate tool own preflight could not start Stryker on this host at all (spawnSync ...node_modules/.bin/stryker ENOENT, exit 0, empty digest - evidence in neither direction), and the first manual equivalent reached the 5-minute initial-run default and printed Initial test run timed out! before a second, with --dryRunTimeoutMinutes 20 added, printed valid 34, killed 18, survived 15, noCoverage 1 on the then-current commit, which is what the strengthened spec was written against.
    src/eslint-suppressions.json is byte for byte the blob d5b845997a775924f598d6b47793fe226b783c61 at both ends of the delta: 264 numeric leaf entries summing to 2866 at the base blob and 264 summing to 2866 at the head blob, with none moved, none risen, none gone and none added.

    Still open, named rather than banked: the two host scenarios for write_to_file both take the create path (call_write_to_file_create_001, call_write_to_file_nested_001, "operation": "created") so no host scenario covers the update path this defect sits on, and the assertion hole apply_diff had - expect(mockCline.diffViewProvider.saveChanges).toHaveBeenCalled() in apps/vscode-e2e/src/suite/commands/writeToFileTool.spec.ts - is the same one here. Both are recorded in the queue row for this unit.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions