131 Commits

Author SHA1 Message Date
BenStullsBets 6b95d24269 add sessions/0068/SESSION-0068.0-TRANSCRIPT-2026-07-03T06-53--2026-07-03T07-39.md + replace placeholder/variant SESSION-0068.0-TRANSCRIPT-2026-07-03T06-53--INPROGRESS.md 2026-07-03 07:42:11 -07:00
BenStullsBets 980cf9eefb add sessions/0067/SESSION-0067.0-TRANSCRIPT-2026-07-02T20-07--2026-07-03T07-39.md + replace placeholder/variant SESSION-0067.0-TRANSCRIPT-2026-07-02T20-07--INPROGRESS.md 2026-07-03 07:42:07 -07:00
BenStullsBets ff09c3b915 claim vscode-cowriting-plugin session 0068 (placeholder) + sessions.json entry 2026-07-03 06:54:04 -07:00
BenStullsBets 7a1b30a671 update sessions/0067/SESSION-0067.0-TRANSCRIPT-2026-07-02T20-07--INPROGRESS.md 2026-07-02 20:21:06 -07:00
benstull 3e7ad58247 Surface polish: batched Minor findings from the native-surfaces final review (#71) (#75) 2026-07-03 03:20:23 +00:00
BenStullsBets a90cc06dfa claim vscode-cowriting-plugin session 0067 (placeholder) + sessions.json entry 2026-07-02 20:08:20 -07:00
BenStullsBets 2961378ede add sessions/0066/SESSION-0066.0-TRANSCRIPT-2026-07-02T16-24--2026-07-02T18-54.md + replace placeholder/variant SESSION-0066.0-TRANSCRIPT-2026-07-02T16-24--INPROGRESS.md 2026-07-02 18:55:45 -07:00
BenStullsBets a0a94d76a9 update sessions/0066/SESSION-0066.0-TRANSCRIPT-2026-07-02T16-24--INPROGRESS.md 2026-07-02 18:21:30 -07:00
benstull 7583165354 fix(#70): INV-5 — reject after an interior tweak restores the retained original (PR #73)
Reject on an optimistically-applied proposal now restores the retained original even after interior tweaks: appliedSpans tracked-range fallback (pure shiftTracked, boundary-straddle distrust, close-clears, rebuild-only resync), honest hard failure with a Discard action, INV-16 read-only guards, rejectAll {reverted,skipped} reporting on all batch surfaces, CodeLens reachability at the tracked span. 312 unit + 94/5 host E2E.

Closes #70.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 01:19:46 +00:00
BenStullsBets 42740f7cc6 update sessions/0066/SESSION-0066.0-TRANSCRIPT-2026-07-02T16-24--INPROGRESS.md 2026-07-02 17:58:16 -07:00
BenStullsBets 6c3e331fc3 claim vscode-cowriting-plugin session 0066 (placeholder) + sessions.json entry 2026-07-02 16:25:26 -07:00
BenStullsBets d8f2d7383a add sessions/0065/SESSION-0065.0-TRANSCRIPT-2026-07-02T06-31--2026-07-02T16-10.md + replace placeholder/variant SESSION-0065.0-TRANSCRIPT-2026-07-02T06-31--INPROGRESS.md 2026-07-02 16:13:33 -07:00
benstull 935fcc35ee Native surfaces migration — evolve the extension onto VS Code's native review surfaces (9-task plan, spec v0.2.1) (#72) 2026-07-02 23:09:37 +00:00
BenStullsBets 93eeaf13b8 update sessions/0065/SESSION-0065.0-TRANSCRIPT-2026-07-02T06-31--INPROGRESS.md 2026-07-02 08:08:18 -07:00
BenStullsBets 26b2212efd claim vscode-cowriting-plugin session 0065 (placeholder) + sessions.json entry 2026-07-02 06:32:05 -07:00
BenStullsBets 0967067cb3 add sessions/0064/SESSION-0064.0-TRANSCRIPT-2026-07-01T22-19--2026-07-02T05-34.md + replace placeholder/variant SESSION-0064.0-TRANSCRIPT-2026-07-01T22-19--INPROGRESS.md 2026-07-02 05:35:06 -07:00
BenStullsBets ef61b141b6 plan(0064): native-surfaces migration — ONE plan from coauthoring-native-surfaces v0.2.1 (D17/§6.10/§7.1 rung 3+4), 9 tasks, sunsets gated on green replacements
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-01 22:40:40 -07:00
BenStullsBets 55fe663bd0 claim vscode-cowriting-plugin session 0064 (placeholder) + sessions.json entry 2026-07-01 22:19:33 -07:00
BenStullsBets 7b767a759d update sessions/0063/SESSION-0063.0-TRANSCRIPT-2026-07-01T18-49--INPROGRESS.md 2026-07-01 19:13:17 -07:00
BenStullsBets 2a1d7623f5 claim vscode-cowriting-plugin session 0063 (placeholder) + sessions.json entry 2026-07-01 18:49:59 -07:00
BenStullsBets fdab92ae6d claim vscode-cowriting-plugin session 0062 (placeholder) + sessions.json entry 2026-06-27 17:03:29 -07:00
BenStullsBets c9050c3e08 Merge origin/main (session 0061 claim) into main 2026-06-27 05:18:51 -07:00
BenStullsBets b3c476e4d9 claim vscode-cowriting-plugin session 0061 (placeholder) + sessions.json entry 2026-06-27 05:14:59 -07:00
BenStullsBets 15e6f02bdf Merge: author-colored track changes — Phase 2 (editor pane)
Brings author-colored track changes inline into the main editor (reverses
INV-32): four per-author decoration types, decorateCommitted decorating all
committed changes-since-baseline by author (human green / Claude blue + struck
hints), standalone deletions neutral, proposals recolored Claude, overlap
stacking. Subagent-driven execution of Tasks 6-9; opus whole-branch review +
fix. typecheck + 265 unit + build + E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 18:54:10 -07:00
BenStullsBets c3cecdfaa2 fix: editor renders standalone committed deletions neutral; doc/comment cleanups (final review)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-26 18:10:25 -07:00
BenStullsBets 02fbe9c77e chore: sweep retired semantic-diff markup; full suite green
- Delete dead `authorBadge` function (no callers; grep-verified)
- Merge adjacent duplicate imports from trackChangesModel in test file

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-26 17:57:16 -07:00
BenStullsBets ae19df78fb feat: overlap renders stacked author marks (insert + delete) in both panes
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-26 17:50:39 -07:00
BenStullsBets 7b59c324d5 feat: Task 7 — decorateCommitted fills committed author-colored changes in the editor (reverses INV-32)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-26 17:40:15 -07:00
BenStullsBets dcdd2ffc1c feat: per-author editor decoration types; proposals decorate in Claude blue/purple 2026-06-26 17:31:13 -07:00
BenStullsBets 1d284d2868 Merge: author-colored track changes — Phase 1 (preview pane)
Preview pane renders document changes as author-colored track changes:
underline=insert / strikethrough=remove, color=author (human green/red,
Claude blue/purple). Adjacency heuristic for deletion authorship; unchanged
text plain; pin->clean preserved. Phase 2 (editor pane) to follow.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 16:38:19 -07:00
BenStullsBets 05ca497900 chore: drop dead cw-by-*/cw-mixed preview CSS; fix added-block comment to cw-ins-*
Final-review follow-up: the live preview colors added blocks via colorByAuthor(kind="ins")
emitting cw-ins-*; the cw-by-*/cw-mixed rules (only authorBadge, which has no callers)
were styled-but-never-emitted. Cosmetic only; build + 260 unit green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 14:47:05 -07:00
BenStullsBets c579f67a43 test(e2e): assert author-colored track changes render in the preview
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-26 14:37:29 -07:00
BenStullsBets 5d1000096a feat: author-colored track-changes CSS for the preview (human green/red, Claude blue/purple) 2026-06-26 14:22:12 -07:00
BenStullsBets 2955e8d929 feat: color pending proposal blocks by author (Claude blue/purple)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-26 14:17:40 -07:00
BenStullsBets 44ef0a21db fix: render added blocks via markdown-safe sentinel path emitting cw-ins-* (heading/list structure preserved)
Defect: an ADDED block was colored via `render(wordDiffByAuthor("", raw, ...))`, which
wrapped the whole raw markdown in an inline `<ins>` BEFORE markdown-it parsed it — so
`## X` rendered as literal text, not `<h2>`. Same structural bug for list, blockquote,
and thematic-break blocks.

Fix (three-part, pure / vscode-free):
1. `blockContentStart(raw)`: new helper returning the offset past block-level markers
   (ATX headings `## `, unordered/ordered list markers, blockquotes). Open sentinels must
   not precede these markers or markdown-it cannot recognise the block construct.
2. `injectSentinels`: clamp the open-sentinel lower-bound to `blockContentStart(raw)`,
   so for `## New Section` the sentinel lands AFTER `## ` (at position 3) instead of at
   position 0, letting the heading parse correctly.
3. `sentinelsToSpans` / `colorByAuthor`: add optional `kind: "by" | "ins" = "by"`
   parameter so the sentinel path can emit `cw-ins-{author}` for added blocks while all
   existing `colorByAuthor`/`cw-by-*` call-sites are unchanged.
4. `renderReview` ADDED block path: `render(wordDiffByAuthor("", raw, ...))` →
   `colorByAuthor(raw, blk.start, landedSpans, render, "ins")`. The CHANGED non-atomic
   prose path (`wordDiffByAuthor(before, after, ...)`) is untouched — it correctly keeps
   the block markers at column 0 of the unchanged prefix.
5. `renderReviewOp`: removed the now-vestigial `colored` parameter (was always `render`
   after the added-block path moved to `changedHtml`); unchanged blocks now call
   `render(op.block.raw)` directly.

Regression test added: "renderReview: an added heading block renders as a heading AND
is author-colored as an insertion" — RED on old code, GREEN after fix. All 259 tests
pass; typecheck clean.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-26 14:09:19 -07:00
BenStullsBets 08557827da feat: author-colored changed/added blocks in review; unchanged blocks render plain 2026-06-26 13:54:24 -07:00
BenStullsBets 0ad040711f feat: authorAt + wordDiffByAuthor pure helpers (author-colored word diff w/ adjacency del) 2026-06-26 13:34:12 -07:00
BenStullsBets 94b1a9b0c2 docs: author-colored track-changes design + implementation plan
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 13:32:34 -07:00
BenStullsBets 126d71fa06 add sessions/0060/SESSION-0060.0-TRANSCRIPT-2026-06-26T12-52--2026-06-26T13-14.md + replace placeholder/variant SESSION-0060.0-TRANSCRIPT-2026-06-26T12-52--INPROGRESS.md 2026-06-26 13:15:04 -07:00
benstull e671c4bf03 fix(#59): disable claude-code IDE auto-connect to stop spurious macOS Apple-Events prompt (#68) 2026-06-26 20:12:24 +00:00
BenStullsBets 5d83f290f5 claim vscode-cowriting-plugin session 0060 (placeholder) + sessions.json entry 2026-06-26 12:52:49 -07:00
BenStullsBets e38fe1b8ca add sessions/0059/SESSION-0059.0-TRANSCRIPT-2026-06-26T06-01--2026-06-26T08-32.md + replace placeholder/variant SESSION-0059.0-TRANSCRIPT-2026-06-26T06-01--INPROGRESS.md 2026-06-26 08:33:00 -07:00
benstull 844a2789fd chore: remove stray code-repo spec copy (canonical in content repo) (#67) 2026-06-26 15:30:59 +00:00
benstull 7b98249286 F12: inline editable proposed-change diff in the Markdown editor + Accept/Reject control parity (#64) (#66) 2026-06-26 15:28:14 +00:00
BenStullsBets d2ef9457c4 add sessions/0057/SESSION-0057.0-TRANSCRIPT-2026-06-26T04-35--2026-06-26T06-55.md + replace placeholder/variant SESSION-0057.0-TRANSCRIPT-2026-06-26T04-35--INPROGRESS.md 2026-06-26 06:56:48 -07:00
benstull 7e42d115c0 feat(ux): unify Ask-Claude-to-Edit on a split-below multi-line webview (#65) 2026-06-26 13:50:31 +00:00
BenStullsBets 56cc9eb6ad claim vscode-cowriting-plugin session 0059 (placeholder) + sessions.json entry 2026-06-26 06:01:30 -07:00
BenStullsBets 2069029d73 add sessions/0058/SESSION-0058.0-TRANSCRIPT-2026-06-26T05-12--2026-06-26T05-58.md + replace placeholder/variant SESSION-0058.0-TRANSCRIPT-2026-06-26T05-12--INPROGRESS.md 2026-06-26 05:58:49 -07:00
BenStullsBets a99430c247 claim vscode-cowriting-plugin session 0058 (placeholder) + sessions.json entry 2026-06-26 05:13:09 -07:00
benstull 9432300e3c feat(ux): unify "Ask Claude to Edit" + inline prompt at selection; fix keybindings (#62) 2026-06-26 12:11:43 +00:00
BenStullsBets a42e5f145d add sessions/0056/SESSION-0056.0-TRANSCRIPT-2026-06-26T04-24--2026-06-26T04-54.md + replace placeholder/variant SESSION-0056.0-TRANSCRIPT-2026-06-26T04-24--INPROGRESS.md 2026-06-26 04:55:04 -07:00
BenStullsBets 26628c0dbe claim vscode-cowriting-plugin session 0057 (placeholder) + sessions.json entry 2026-06-26 04:53:46 -07:00
benstull 644885c6ec #60: live turn progress (activity line + token count + OutputChannel stream + cancel) (#61)
Surface Claude live output/progress during the asking-Claude status: notification activity line + token count, a Cowriting: Claude OutputChannel streaming assistant text, and a cancellable turn. Pure turnProgress reducer + runEditTurn onProgress/AbortSignal (vscode-free) + both call sites. INV-43..47.

Fixes #60
2026-06-26 11:53:10 +00:00
BenStullsBets 98b33ff53b claim vscode-cowriting-plugin session 0056 (placeholder) + sessions.json entry 2026-06-26 04:24:43 -07:00
BenStullsBets c5e464fbeb add sessions/0055/SESSION-0055.0-TRANSCRIPT-2026-06-22T23-19--2026-06-26T04-23.md + replace placeholder/variant SESSION-0055.0-TRANSCRIPT-2026-06-22T23-19--INPROGRESS.md 2026-06-26 04:24:19 -07:00
Ben Stull 16cca30d39 claim vscode-cowriting-plugin session 0055 (placeholder) + sessions.json entry 2026-06-22 23:19:57 -07:00
Ben Stull 9b6a15a43c add sessions/0054/SESSION-0054.0-TRANSCRIPT-2026-06-15T10-42--2026-06-15T10-48.md + replace placeholder/variant SESSION-0054.0-TRANSCRIPT-2026-06-15T10-42--INPROGRESS.md 2026-06-22 22:59:20 -07:00
Ben Stull edba577586 claim vscode-cowriting-plugin session 0054 (placeholder) + sessions.json entry 2026-06-15 10:43:00 -07:00
Ben Stull ceca17aa40 add sessions/0053/SESSION-0053.0-TRANSCRIPT-2026-06-15T10-23--2026-06-15T10-29.md + replace placeholder/variant SESSION-0053.0-TRANSCRIPT-2026-06-15T10-23--INPROGRESS.md 2026-06-15 10:42:23 -07:00
Ben Stull 528c76d23b claim vscode-cowriting-plugin session 0053 (placeholder) + sessions.json entry 2026-06-15 10:23:27 -07:00
Ben Stull b9517e0f68 add sessions/0052/SESSION-0052.0-TRANSCRIPT-2026-06-15T08-33--2026-06-15T08-41.md + replace placeholder/variant SESSION-0052.0-TRANSCRIPT-2026-06-15T08-33--INPROGRESS.md 2026-06-15 10:22:35 -07:00
Ben Stull 5a02e793dd claim vscode-cowriting-plugin session 0052 (placeholder) + sessions.json entry 2026-06-15 08:33:55 -07:00
Ben Stull faf0810a6c add sessions/0051/SESSION-0051.0-TRANSCRIPT-2026-06-13T17-49--2026-06-13T17-58.md + replace placeholder/variant SESSION-0051.0-TRANSCRIPT-2026-06-13T17-49--INPROGRESS.md 2026-06-15 03:28:01 -07:00
Ben Stull d597c1c362 claim vscode-cowriting-plugin session 0051 (placeholder) + sessions.json entry 2026-06-13 17:49:33 -07:00
Ben Stull 790d88c827 add sessions/0050/SESSION-0050.0-TRANSCRIPT-2026-06-13T17-43--2026-06-13T17-52.md + replace placeholder/variant SESSION-0050.0-TRANSCRIPT-2026-06-13T17-43--INPROGRESS.md 2026-06-13 17:48:52 -07:00
Ben Stull 37953cfcad claim vscode-cowriting-plugin session 0050 (placeholder) + sessions.json entry 2026-06-13 17:43:55 -07:00
Ben Stull 24e329e25d add sessions/0049/SESSION-0049.0-TRANSCRIPT-2026-06-13T09-19--2026-06-13T09-24.md + replace placeholder/variant SESSION-0049.0-TRANSCRIPT-2026-06-13T09-19--INPROGRESS.md 2026-06-13 09:22:49 -07:00
benstull 96a689aedf Merge pull request '#54 follow-up: pretest:e2e cleans out/test (stop stale cross-branch compiled tests running)' (#56) from s54-pretest-clean into main 2026-06-13 16:21:57 +00:00
Ben Stull 911ed21671 #54 follow-up: pretest:e2e cleans out/test before recompiling
`tsc -p tsconfig.e2e.json` emits to `out/` but never removes outputs for test
sources absent on the current branch, so stale compiled
`out/test/e2e/suite/*.test.js` from other branches were picked up and run by the
suite glob (`**/*.test.js`) — this caused real cross-branch test confusion in
session 0048 (a deleted probe + another branch's tests ran on an unrelated branch).

Add a `clean:e2e` script (`fs.rmSync('out/test', {recursive, force})` via node, so
no shell `rm` dependency) and run it between `build` and `tsc` in `pretest:e2e`.
Cleans ONLY `out/test` — never `out/`, which holds the just-built esbuild bundle
(`out/extension.cjs`, `out/media`).

Verified: planting a stale `out/test/.../zz.test.js` then running `pretest:e2e`
removes it, and the E2E suite stays green (73 passing + 1 pending, both passes
exit 0).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 09:21:31 -07:00
Ben Stull 2ca0fc8c51 claim vscode-cowriting-plugin session 0049 (placeholder) + sessions.json entry 2026-06-13 09:19:26 -07:00
benstull 3d9270ecc4 Merge pull request '#54: resilient undo E2E (preflight skip-guard) — fixes main E2E red' (#55) from s54-undo-e2e-resilience into main 2026-06-13 16:17:02 +00:00
Ben Stull e53f0c30ad #54: make undo-dependent E2E resilient to environments where undo is broken
`executeCommand("undo")` is non-functional in some headless `.vscode-test`
instances (it does not restore the buffer — see #54's diagnosis), which
false-fails every undo-dependent host E2E and turns `main`'s E2E red there even
though the product code is fine.

Add a RUNTIME preflight probe (`undoCapable.ts`): edit a scratch buffer, undo,
and report whether the buffer was actually restored (memoized per run). The #38
undoMarks suite gates on it via `suiteSetup` — running normally where undo works
(full coverage preserved, real regressions still caught) and skipping with a LOUD
console warning where it doesn't (no false red, no silent loss — the skip is
logged and shows as `pending`).

This fixes the "main E2E red" symptom. The underlying headless-undo limitation is
documented in #54; #40's undo-behavior coverage runs wherever undo works.

Test-infra only — no product code changed. Verified: with undo broken locally the
#38 suite skips (1 pending) and both E2E passes exit 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 09:16:40 -07:00
Ben Stull ad6cbe10c7 add sessions/0048/SESSION-0048.0-TRANSCRIPT-2026-06-13T08-45--2026-06-13T08-58.md + replace placeholder/variant SESSION-0048.0-TRANSCRIPT-2026-06-13T08-45--INPROGRESS.md 2026-06-13 08:58:11 -07:00
Ben Stull 64c7d7bad2 claim vscode-cowriting-plugin session 0048 (placeholder) + sessions.json entry 2026-06-13 08:45:48 -07:00
Ben Stull ab00cbefcc add sessions/0047/SESSION-0047.0-TRANSCRIPT-2026-06-13T08-34--2026-06-13T08-44.md + replace placeholder/variant SESSION-0047.0-TRANSCRIPT-2026-06-13T08-34--INPROGRESS.md 2026-06-13 08:43:50 -07:00
benstull c67749a53c Merge pull request '#33: harden author-coloring PUA sentinels against intra-emphasis markdown' (#53) from s33-sentinel-hardening into main 2026-06-13 15:42:04 +00:00
Ben Stull ba7623f813 #33: harden author-coloring PUA sentinels against intra-emphasis markdown
The F9/F10 author-coloring technique injects paired PUA sentinels into prose
source at span offsets, renders, then maps sentinels → cw-by-* spans. Two failure
modes when a span boundary met emphasis markup (characterized in session 0032):
- CASE1: a boundary strictly inside a delimiter run (a**b**c, between the two *)
  split `**`, breaking markdown-it's delimiter pairing (stray <em></em>, raw **).
- CASE3: a boundary inside an emphasis run (**bold** span covering **bo) rendered
  the emphasis but MISNESTED span/element (<strong>bo</span>ld</strong>).

Token-aware fix (both, per the issue #33 comment):
- injectSentinels: clamp any sentinel offset that lands strictly inside a
  delimiter run (* _ ~ `) to the run's start, so a sentinel never splits a run
  (CASE1). Delimiters are invisible once rendered, so this only shifts the colored
  boundary across markup. Drop spans that clamp to empty.
- sentinelsToSpans: replace the naive global split/join with a walker over the
  rendered HTML that emits the cw-by-* span only around TEXT runs — closing it
  before any <tag> and reopening after — so a span is always well-nested within
  inline elements (one span segment per text run, CASE3). Tags are copied
  verbatim with any stray sentinel stripped (no Private-Use-Area char leaks).

Pure, vscode-free, deterministic (INV-33). No regression: existing colorByAuthor /
renderReview cases stay green; the common no-emphasis case is byte-identical.

222 unit + 74/5 host E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 08:41:31 -07:00
Ben Stull 9e9fcb8057 claim vscode-cowriting-plugin session 0047 (placeholder) + sessions.json entry 2026-06-13 08:34:03 -07:00
Ben Stull 1381eab11e add sessions/0046/SESSION-0046.0-TRANSCRIPT-2026-06-13T08-19--2026-06-13T08-31.md + replace placeholder/variant SESSION-0046.0-TRANSCRIPT-2026-06-13T08-19--INPROGRESS.md 2026-06-13 08:32:25 -07:00
benstull 54846da1ea Merge pull request '#48: pinning the baseline leaves the review panel fully un-annotated' (#52) from s48-pin-clean-panel into main 2026-06-13 15:30:55 +00:00
Ben Stull fdd743490d #48: pinning the baseline leaves the review panel fully un-annotated
A pinned baseline with no changes since should read as a clean starting point,
but the F10 on-render still author-colored every block (colorByAuthor), painting
the whole document green/blue right after a pin. Now, when the baseline reason is
"pinned" and there are zero changes since (every diffBlocks op unchanged), the
on-render is fully clean — no change marks and no authorship coloring — while the
data-src block mapping (INV-36) and any pending proposals (review actions, not
annotations) are kept.

Scoped to the PIN specifically, not all zero-diff: a baseline advanced by a
machine-landing (accept) is also zero-diff but keeps its authorship coloring so
accepted Claude text stays blue (F10 INV-33). renderReview takes a `pinned`
option; the controller passes baseline.reason === "pinned" from both refresh and
the renderHtmlFor test seam.

- trackChangesModel.ts: renderReview gains the `pinned` RenderOption; when pinned
  + zero-diff, blocks render plain (no colorByAuthor).
- trackChangesPreview.ts: pass { pinned } through refresh + renderHtmlFor.
- unit: pinned+zero-diff → no cw-by-*; pinned+zero-diff still shows proposals;
  zero-diff WITHOUT pin (machine-landing) keeps coloring (INV-33); real changes
  after a pin re-color.
- s48PinClean host E2E: type → colored; pin → clean; edit → annotations return.

218 unit + 74/5 host E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 08:28:52 -07:00
Ben Stull 4584e06679 claim vscode-cowriting-plugin session 0046 (placeholder) + sessions.json entry 2026-06-13 08:19:43 -07:00
Ben Stull ef07acfdc1 add sessions/0045/SESSION-0045.0-TRANSCRIPT-2026-06-13T08-04--2026-06-13T08-17.md + replace placeholder/variant SESSION-0045.0-TRANSCRIPT-2026-06-13T08-04--INPROGRESS.md 2026-06-13 08:18:17 -07:00
benstull 83c4a80d8b Merge pull request '#46 (SLICE-3, accept): Accept all pending proposals in one gesture' (#51) from f12-slice3-accept-all into main 2026-06-13 15:16:18 +00:00
Ben Stull c94b9ccfe7 #46 (SLICE-3, accept): Accept all pending proposals in one gesture
Document-edit flow SLICE-3 — completes reach→review→accept
(specs/coauthoring-document-edit-flow.md §7.2, INV-42). A single "Accept all"
gesture applies every pending proposal on the current document through the
existing F4 acceptById seam (block proposals take the INV-40 word-precise path
automatically), in descending anchor order so an earlier accept never invalidates
a later one, skipping (never force-applying) proposals that can't anchor and
reporting applied-vs-skipped. Batched application of the existing accept path — no
new mechanism; the webview posts intent only (INV-35). No confirmation dialog
(undo restores).

- proposalController.ts: acceptAllProposals(document) → {applied, skipped}
  (descending order, orphan-skip); accept/acceptById gain a silent opt so the
  batch suppresses N per-proposal orphan warnings in favour of one report.
- trackChangesPreview.ts: ToolbarMsg += {type:"acceptAll"}; handleWebviewMessage
  routes it to a public acceptAll(document) that batches + reports.
- extension.ts + package.json: cowriting.acceptAllProposals command (active doc,
  markdown-gated palette entry) for the non-webview path.
- media/preview.ts + shellHtml: "✓✓ Accept all" toolbar button, posting the
  intent, shown only with ≥2 pending proposals (authorable, on-state).
- trackChangesModel.ts: diffToBlockHunks now emits one block-aligned hunk per
  CHANGED block even when changed blocks are ADJACENT (treats changed blocks as
  1:1 anchors alongside unchanged ones; gap-spans only cover add/remove runs
  between anchors) — fixes adjacent changed blocks collapsing into one proposal.
- f12Accept host E2E (apply-all reconstructs; orphan skip + report; single
  proposal; command registered/gated); MANUAL-SMOKE-F12 §3.

214 unit + 73/5 host E2E green. Completes the document-edit-flow cluster
(#42 reach + #47 review + #46 accept).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 08:15:55 -07:00
Ben Stull 6fd7555183 claim vscode-cowriting-plugin session 0045 (placeholder) + sessions.json entry 2026-06-13 08:05:04 -07:00
Ben Stull f7788c5585 add sessions/0044/SESSION-0044.0-TRANSCRIPT-2026-06-13T07-40--2026-06-13T08-04.md + replace placeholder/variant SESSION-0044.0-TRANSCRIPT-2026-06-13T07-40--INPROGRESS.md 2026-06-13 08:04:35 -07:00
Ben Stull b69d0f7d15 update sessions/0044/SESSION-0044.0-TRANSCRIPT-2026-06-13T07-40--INPROGRESS.md 2026-06-13 08:03:06 -07:00
benstull dc38b55c57 Merge pull request '#47 (SLICE-2, review): per-block document proposals + word-precise accept' (#50) from f12-slice2-block-proposals into main 2026-06-13 15:01:55 +00:00
Ben Stull 2f6008ba2b #47 (SLICE-2, review): per-block document proposals + word-precise accept
Document-edit flow SLICE-2 (specs/coauthoring-document-edit-flow.md §7.2,
INV-39/40/41 — P1 "too much to review"). A whole-document rewrite now proposes
ONE F4 proposal per CHANGED BLOCK (the unit a human reviews), but accepting a
block reconciles attribution at WORD granularity (the unit F3 records). Block =
decision unit; word = attribution unit. Supersedes INV-37's per-word cut for
document edits; selection edits unchanged.

- trackChangesModel.ts: new pure diffToBlockHunks(current, rewritten) — block-key
  alignment (reusing diffArrays/diffBlocks keying): an isolated changed block →
  one block-aligned hunk → the rewritten block raw (a code/mermaid fence is one
  atomic whole-fence hunk, INV-23); insert/delete runs → one gap-span hunk over
  the inter-anchor region (separators included) so reconstruction stays exact; a
  zero-width gap-span is anchored (INV-41). Also split diffToHunks into the raw,
  un-anchored wordEditHunks + the anchoring wrapper (the anchoring could grow an
  insertion to overlap an adjacent hunk, corrupting a batch apply — a latent bug
  that only surfaced once hunks are applied as a batch).
- trackChangesPreview.ts: runEditAndPropose document branch uses diffToBlockHunks
  and tags each proposal granularity:"block".
- model.ts / proposalModel.ts: additive optional Proposal.granularity
  ("block"|"single"; absent ⇒ single, back-compat — no migration).
- proposalController.ts: accept of a block proposal runs an intra-block word
  sub-diff (wordEditHunks, disjoint) and applies one applyAgentEdit per changed
  run, descending offset — only the words Claude changed land Claude-attributed;
  unchanged spans keep prior authorship (INV-40).
- Tests: diffToBlockHunks unit (reconstruction + fence atomic + add/remove);
  f12Review host E2E (M blocks→M proposals, unchanged→none, fence atomic, INV-40
  attribution, INV-41 insertion accept); updated the f11 document-path E2E to
  per-block (INV-39 supersedes INV-37); MANUAL-SMOKE-F12 §2.

Seam note: pendingEdits.matchEvent resolves one registration per change event, so
INV-40's per-run attribution is sequential applyAgentEdit calls (N undo steps),
not one multi-replace WorkspaceEdit — see transcript Deferred decisions.

214 unit + 69/5 host E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 08:01:16 -07:00
Ben Stull 6e944ab4cc claim vscode-cowriting-plugin session 0044 (placeholder) + sessions.json entry 2026-06-13 07:40:13 -07:00
Ben Stull 2175efb5e5 add sessions/0043/SESSION-0043.0-TRANSCRIPT-2026-06-13T07-18--2026-06-13T07-38.md + replace placeholder/variant SESSION-0043.0-TRANSCRIPT-2026-06-13T07-18--INPROGRESS.md 2026-06-13 07:39:13 -07:00
Ben Stull 5dc7d19419 update sessions/0043/SESSION-0043.0-TRANSCRIPT-2026-06-13T07-18--INPROGRESS.md 2026-06-13 07:29:04 -07:00
benstull e804c46ba0 Merge pull request '#42 (SLICE-1, reach): selection-aware Ask-Claude from editor body + tab' (#49) from f12-slice1-ask-claude-menus into main 2026-06-13 14:27:09 +00:00
Ben Stull 9c3770d26a #42 (SLICE-1, reach): selection-aware Ask-Claude from editor body + tab
Document-edit flow SLICE-1 (specs/coauthoring-document-edit-flow.md §7.2,
INV-38): make "Ask Claude to Edit" reachable from the editor body AND the
editor tab, selection-aware — a selection routes to editSelection, no
selection to editDocument — both gated to markdown/authorable docs, both
flowing through the single runEditAndPropose path (no divergent edit code).

- package.json: add editSelection + editDocument to editor/context (selection-
  aware, markdown + file/untitled) and editor/title/context (selection-aware,
  resourceLangId == markdown). Markdown-gate the existing editor/context
  editSelection entry to match (its command handler is unchanged; the palette
  still reaches any authorable doc — see transcript Deferred decisions).
- trackChangesPreview.ts: cowriting.editDocument accepts the clicked tab's
  resource Uri (editor/title/context), targeting THAT document (opening it if
  needed) and falling back to the active editor when invoked with no arg —
  mirroring showTrackChangesPreview's #41 clicked-doc resolution.
- E2E (test/e2e/suite/f12Reach.test.ts): menu entries present, selection-aware,
  markdown-gated; editDocument(uri) targets the tab doc not the active editor;
  no-arg falls back to the active editor.
- docs/MANUAL-SMOKE-F12.md: SLICE-1 reach smoke steps.

208 unit + 65/5 host E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 07:26:14 -07:00
Ben Stull 60a396c09e claim vscode-cowriting-plugin session 0043 (placeholder) + sessions.json entry 2026-06-13 07:19:03 -07:00
Ben Stull 9ec0862353 add sessions/0042/SESSION-0042.0-TRANSCRIPT-2026-06-12T21-04--2026-06-13T07-16.md + replace placeholder/variant SESSION-0042.0-TRANSCRIPT-2026-06-12T21-04--INPROGRESS.md 2026-06-13 07:17:25 -07:00
Ben Stull d5ef6dc90f add sessions/0041/SESSION-0041.0-TRANSCRIPT-2026-06-12T18-17--2026-06-13T07-16.md + replace placeholder/variant SESSION-0041.0-TRANSCRIPT-2026-06-12T18-17--INPROGRESS.md 2026-06-13 07:17:16 -07:00
Ben Stull 261f0de240 claim vscode-cowriting-plugin session 0042 (placeholder) + sessions.json entry 2026-06-12 21:05:02 -07:00
Ben Stull 46f74c8247 claim vscode-cowriting-plugin session 0041 (placeholder) + sessions.json entry 2026-06-12 18:17:58 -07:00
Ben Stull cc3d778da9 add sessions/0040/SESSION-0040.0-TRANSCRIPT-2026-06-12T17-03--2026-06-12T17-21.md + replace placeholder/variant SESSION-0040.0-TRANSCRIPT-2026-06-12T17-03--INPROGRESS.md 2026-06-12 17:22:23 -07:00
Ben Stull 981640f3d5 claim vscode-cowriting-plugin session 0040 (placeholder) + sessions.json entry 2026-06-12 17:03:31 -07:00
Ben Stull 2978663c14 add sessions/0039/SESSION-0039.0-TRANSCRIPT-2026-06-12T16-07--2026-06-12T17-02.md + replace placeholder/variant SESSION-0039.0-TRANSCRIPT-2026-06-12T16-07--INPROGRESS.md 2026-06-12 17:02:44 -07:00
Ben Stull 5eb13deca3 claim vscode-cowriting-plugin session 0039 (placeholder) + sessions.json entry 2026-06-12 16:07:27 -07:00
Ben Stull 91c55870f2 add sessions/0038/SESSION-0038.0-TRANSCRIPT-2026-06-12T15-55--2026-06-12T16-06.md + replace placeholder/variant SESSION-0038.0-TRANSCRIPT-2026-06-12T15-55--INPROGRESS.md 2026-06-12 16:06:50 -07:00
benstull eea4145904 Merge pull request 'feat(#41): Open Cowriting Review Panel from markdown file/tab right-click' (#45) from 41-review-panel-right-click into main 2026-06-12 23:04:48 +00:00
Ben Stull 695b51f903 feat(#41): Open Cowriting Review Panel from markdown file/tab right-click
The review preview (`cowriting.showTrackChangesPreview`) was only reachable from
the command palette and `ctrl+alt+r` — neither where a writer's hand naturally
goes. Add the obvious right-click entry points to the plugin's central surface:

- `explorer/context` + `editor/title/context` menu items gated on
  `resourceLangId == markdown`, both invoking `showTrackChangesPreview`.
- The command now resolves the *clicked* document: it opens the passed Uri if
  it isn't already an open document (the Explorer case), instead of falling back
  to the active editor. No-arg invocation (palette / keybinding) is unchanged.
- Retitle the command to "Open Cowriting Review Panel" so the menus read the
  operator's wording (palette shows "Cowriting: Open Cowriting Review Panel").

E2E: clicked-doc resolution (open + not-yet-open + no-arg fallback), both menu
contributions present + markdown-gated, title, and keybinding unchanged.

Closes #41.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 16:04:02 -07:00
Ben Stull de53305a08 claim vscode-cowriting-plugin session 0038 (placeholder) + sessions.json entry 2026-06-12 15:55:41 -07:00
Ben Stull 21df67022f add sessions/0037/SESSION-0037.0-TRANSCRIPT-2026-06-12T13-33--2026-06-12T14-22.md + replace placeholder/variant SESSION-0037.0-TRANSCRIPT-2026-06-12T13-33--INPROGRESS.md 2026-06-12 14:21:49 -07:00
benstull 62a2229c25 Merge pull request 'F11 — Preview toolbar as the primary interaction surface (#43)' (#44) from f11-preview-toolbar into main 2026-06-12 21:18:54 +00:00
Ben Stull 47cc733026 fix(f11): address code review — insertion anchoring, turnId, accept-all coverage (#43)
Code-review follow-up on the F11 branch (5966907..1564ef5). Three real findings:

- CRITICAL — pure-insertion hunks were born-orphaned. A document rewrite that
  INSERTS text produced a zero-width hunk (start==end) → buildFingerprint yields
  empty fp.text → anchorer.resolve orphans an empty needle → the proposal could
  never be accepted (silently). diffToHunks now anchors every zero-width
  insertion to an adjacent source token (anchorInsertion): the range absorbs the
  token and the replacement keeps it, so net text is identical but fp.text is
  non-empty and resolvable. New unit tests: applying hunks always reconstructs
  the rewrite (substitute/delete/insert/multi); insertions are never zero-width.
  New host E2E ACCEPTS a rewrite-with-insertion end to end and asserts accept-all
  reconstructs the intended document (also covers sequential multi-hunk accept).

- IMPORTANT — renderPlain cross-block fidelity. The per-block off-mode rendering
  (SLICE-2) can't resolve a reference-link definition in a separate block.
  Documented as a conscious tradeoff of the operator-locked block-level mapping
  (§6.7) — renderReview already rendered per-block, so this keeps both modes
  consistent — with a characterization test + a docstring note.

- MINOR — F11 proposals now carry a turnId (one per Ask-Claude gesture, shared
  across a document rewrite's N hunks), matching the editor-menu editSelection
  path so a rewrite groups as one agent turn.

208 unit + 9/9 F11 host E2E green. (The lone red E2E is a pre-existing,
F11-independent undoMarks timing flake — proven by isolation: it fails
identically with all F11 tests removed.)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 14:16:49 -07:00
Ben Stull 1564ef562b feat(f11): SLICE-5 — gateway, non-authorable disabling, docs (#43)
Closes the F11 feature: makes the toolbar surface reachable end to end and
guards the edit controls. Per spec §6.4/§6.5/§7.2 SLICE-5.

- package.json: cowriting.showTrackChangesPreview added to editor/title
  (when: editorLangId == markdown) — the minimal right-click → Open Review
  Preview gateway (#41/#42 expand it later).
- trackChangesPreview: the gateway command accepts the tab's resource Uri
  (palette/keybinding still fall back to the active editor); refresh() sends an
  `authorable` flag on both render messages; `editControlsEnabled` test seam.
- webview: disable Pin + Ask-Claude on a non-authorable doc (Annotations stays
  active — reading is always allowed); RenderMessage.authorable.
- host E2E: the editor/title gateway opens the preview + is markdown-guarded;
  edit controls disabled on a non-authorable (read-only-scheme) markdown doc.
- docs: docs/MANUAL-SMOKE-F11.md (live smoke, 10 steps) + README F11 section +
  intro line.

205 unit + 53 host E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 13:57:28 -07:00
Ben Stull 03b61ed43e feat(f11): SLICE-4 — single adaptive Ask-Claude button + selection mapping (#43, INV-37)
The one Ask-Claude toolbar button now adapts: its label flips on selectionchange
(Edit Selection when live text is selected in the preview, Edit Document
otherwise), and a click resolves the preview selection to a SOURCE range via the
nearest data-src ancestors (INV-36) — the webview's sole mapping duty. A
selection touching no live-source block falls back to document scope. Per spec
§6.5 PUC-2/3 / §7.2 SLICE-4.

- webview (media/preview.ts): nearestSrc() DOM walk + selectionSrcRange()
  block-union; updateAskLabel() on selectionchange + after each render; the
  adaptive click posts { askClaude, scope:"selection", start, end } or falls back
  to document scope. Sealed (INV-21): reads data-src only, posts intent.
- host: runEditAndPropose's range branch (already shared from SLICE-3) records one
  single-range F4 proposal over the resolved block-union.
- host E2E: stubbed selection turn → exactly one proposal over the resolved range
  (turn receives exactly the selected source; replaced == the range; doc
  untouched); an unchanged replacement proposes nothing. (The webview DOM
  selection→data-src lookup is sealed-sandbox → manual smoke, spec §6.8.)

205 unit + 51 host E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 13:53:14 -07:00
Ben Stull 0d1a5635cb feat(f11): SLICE-3 — Edit Document button + per-hunk proposal path (#43, INV-37)
A whole-document Ask-Claude rewrite is diffed into hunks and surfaced as N
independent F4 proposals (one per changed hunk) — reusing the F4 single-range
model N times, no new model. Per spec §6.4/§7.2 SLICE-3.

- trackChangesModel: pure `diffToHunks(currentText, rewrittenText)` →
  EditHunk[] (vscode-free, deterministic; diffWordsWithSpace, coalescing
  adjacent add/remove runs; offsets index currentText).
- trackChangesPreview: `runEditAndPropose(document, target, instruction)` — the
  shared host routine (selection → one single-range propose; document → diff →
  one propose per hunk; never mutates the doc, INV-10); `askClaude` UI wrapper
  (host showInputBox keeps LLM/secrets out of the sealed webview, INV-8/35);
  injectable `editTurn` + `setEditTurnForTest` seam (no LLM in CI); the
  `askClaude` inbound message branch; `cowriting.editDocument` command for #42
  reuse.
- package.json: register cowriting.editDocument, palette-guarded on markdown.
- webview: ✦ Ask Claude to Edit Document button → { askClaude, scope:"document" }.
- unit: diffToHunks fixtures (zero/one/multi-hunk, wholesale, determinism).
- host E2E: stubbed multi-hunk rewrite → N matching proposals, doc untouched;
  editDocument command registered + markdown-guarded.

205 unit + 49 host E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 13:51:07 -07:00
Ben Stull 1ef9451e89 feat(f11): SLICE-2 — block-level data-src emission (#43, INV-36)
The pure render layer now emits data-src-start/data-src-end (source char offsets
from BlockWithRange) on every LIVE-source rendered block, in BOTH modes — the
contract the webview's selection→source mapping (SLICE-4) walks the DOM for.
Per spec §6.1 INV-36 / §7.2 SLICE-2.

- trackChangesModel: shared `srcAttr(blk)` helper; threaded through renderOp +
  renderReviewOp + the renderReview loop (removed blocks → "" / no data-src, as
  they have no live source). renderPlain switches from a single whole-document
  markdown pass to per-block `<div data-src-start/end>` wrappers — bare divs (no
  cw- class) so the off/clean preview stays visually clean while becoming a
  selection→source surface. Pure, vscode-free, deterministic (extends INV-22).
- unit (test/trackChangesModel.test.ts): data-src offsets equal
  splitBlocksWithRanges in both modes; removed + proposal blocks carry none;
  off-mode stays cw--free; determinism.

200 unit + 47 host E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 13:45:02 -07:00
Ben Stull 8b9e61a1da feat(f11): SLICE-1 — Pin baseline toolbar button + reachability (#43)
Homes the orphaned cowriting.pinDiffBaseline command and gives the writer a
reachable Pin control in the preview toolbar. Per spec §7.2 SLICE-1
(docs/superpowers/specs/2026-06-12-f11-preview-toolbar-interaction-surface.md).

- trackChangesPreview: extract onDidReceiveMessage into handleWebviewMessage;
  add the F11 `pinBaseline` intent → DiffViewController.pin(previewedDoc) (the
  bound doc, not activeTextEditor — §6.7); ToolbarMsg union; receiveMessage test
  seam exercising the real message→seam wiring (INV-35).
- webview: ⌖ Pin baseline button in #cw-header posting { type: "pinBaseline" };
  theme-aware toolbar-button CSS (light/dark/high-contrast, disabled state).
- package.json: unhide pinDiffBaseline — commandPalette `when` false →
  editorLangId == markdown (resolves the #34 orphan from the command side).
- host E2E (test/e2e/suite/f11Toolbar.test.ts): pinBaseline message clears the
  change-marks + advances the baseline to `pinned`; palette `when` is reachable.

Also archives the F11 implementation plan to docs/superpowers/plans/.

197 unit + 47 host E2E green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 13:40:21 -07:00
Ben Stull 5966907089 claim vscode-cowriting-plugin session 0037 (placeholder) + sessions.json entry 2026-06-12 13:33:47 -07:00
Ben Stull 2e3179eaec add sessions/0036/SESSION-0036.0-TRANSCRIPT-2026-06-12T11-01--2026-06-12T12-18.md + replace placeholder/variant SESSION-0036.0-TRANSCRIPT-2026-06-12T11-01--INPROGRESS.md 2026-06-12 12:18:51 -07:00
Ben Stull b2de25ff99 design(f11): preview toolbar as the primary interaction surface
Solution Design for Feature #43 (F11): the F10 review preview's header
gains a Pin baseline button and a single adaptive Ask Claude button
(Edit Selection / Edit Document) beside the existing annotations
checkbox, plus a minimal right-click gateway that opens the preview.

Three forks locked in brainstorming session 0036:
- block-level preview-selection -> source mapping (data-src on blocks
  from BlockWithRange; union of intersected live-source blocks)
- document edit diffed into per-hunk F4 proposals (no model change)
- #43 lands a minimal editor/title -> Open Review Preview gateway
  (#41/#42 expand the menus)

Reuses F4 propose/F3 attribution/F6 baseline seams; sealed webview
posts intent only (INV-35/36/37). Resolves the orphaned pinDiffBaseline
reachability gap. Submitted to the content repo at finalize.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 12:15:44 -07:00
Ben Stull f2b6f369f5 claim vscode-cowriting-plugin session 0036 (placeholder) + sessions.json entry 2026-06-12 11:01:57 -07:00
Ben Stull ac191556a6 add sessions/0035/SESSION-0035.0-TRANSCRIPT-2026-06-12T08-53--2026-06-12T10-58.md + replace placeholder/variant SESSION-0035.0-TRANSCRIPT-2026-06-12T08-53--INPROGRESS.md 2026-06-12 10:58:54 -07:00
Ben Stull 10ad1a6992 claim vscode-cowriting-plugin session 0035 (placeholder) + sessions.json entry 2026-06-12 08:53:43 -07:00
Ben Stull 440d8ff4c9 add sessions/0034/SESSION-0034.0-TRANSCRIPT-2026-06-12T03-28--2026-06-12T03-39.md + replace placeholder/variant SESSION-0034.0-TRANSCRIPT-2026-06-12T03-28--INPROGRESS.md 2026-06-12 03:39:50 -07:00
benstull 3c8b3597dd Merge pull request 'fix #38: undo/redo no longer mis-attributes restored text in the review preview' (#39) from bug/38-undo-wrong-marks-in-preview into main 2026-06-12 10:37:48 +00:00
Ben Stull 469c8c11fc fix #38: don't mis-attribute undo/redo as fresh human authorship in the preview
Undo in the editor rendered wrong marks in the F10 review preview: restored
baseline text (and reverted Claude text) was colored as human-authored. Root
cause — attributionController.onDidChange attributed every non-seam document
change to currentAuthor() (always human) and ignored e.reason, so an undo/redo
that re-inserts text created a fresh human span over it. renderReview is pure;
the wrong marks came from these false author spans.

Fix: on e.reason === Undo|Redo, reconcile span geometry but do NOT attribute the
re-inserted chars — an undo is history navigation, not authorship, so restored
text stays neutral (unattributed) rather than falsely claimed by the human.
applyChange gains an `attributeInserted` flag (default true; false on undo/redo);
seam matching is also skipped on undo/redo (the seam only applies forward edits).

Restored text becomes neutral rather than recovering its exact prior provenance
(e.g. undoing a deletion of Claude's text shows it unattributed, not blue) —
perfect restoration would need an attribution history stack synced to the editor
undo stack (fragile due to edit coalescing); filed mentally as a follow-up. The
neutral behavior removes the misleading marks, which is the reported defect.

Tests: E2E reproduces the mid-edit-undo case (buffer stays dirty so the disk-sync
guard doesn't mask it) and asserts restored text is unattributed; +3 unit tests
for the geometric-only applyChange path. 197 unit + 50 E2E green; typecheck clean.

Closes #38

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 03:37:22 -07:00
Ben Stull 21e889b0a3 claim vscode-cowriting-plugin session 0034 (placeholder) + sessions.json entry 2026-06-12 03:28:36 -07:00
Ben Stull 7e15c887f3 add sessions/0033/SESSION-0033.0-TRANSCRIPT-2026-06-12T03-14--2026-06-12T03-27.md + replace placeholder/variant SESSION-0033.0-TRANSCRIPT-2026-06-12T03-14--INPROGRESS.md 2026-06-12 03:28:06 -07:00
Ben Stull 0ada9ed9da claim vscode-cowriting-plugin session 0033 (placeholder) + sessions.json entry 2026-06-12 03:14:58 -07:00
Ben Stull d70fa91e59 add sessions/0032/SESSION-0032.0-TRANSCRIPT-2026-06-12T01-20--2026-06-12T03-05.md + replace placeholder/variant SESSION-0032.0-TRANSCRIPT-2026-06-12T01-20--INPROGRESS.md 2026-06-12 03:05:57 -07:00
benstull 1a52ac6cb4 Merge pull request 'F6 #34: delete the dead two-pane diff-view UI, keep the baseline data layer' (#37) from task/34-delete-f6-dead-view-code into main 2026-06-12 09:10:37 +00:00
Ben Stull 3520397e41 F6 #34: delete the dead two-pane diff-view UI, keep the baseline data layer
F10 (#29) made the rendered preview the single review surface and hid the F6
two-pane vscode.diff view (command + ctrl+alt+d set when:false). This removes
that now-unreachable view code:

- DiffViewController: drop toggle/findDiffTab/epochLabel/isDiffOpen, the
  `cowriting-baseline:` TextDocumentContentProvider + BASELINE_SCHEME + baselineUri
  + the content-provider change emitter, and the toggleDiffView command. The
  baseline DATA layer is fully intact — ensureBaseline/advance/pin/capture,
  getBaseline, baselineFilePath, onDidChangeBaseline, persistence (INV-19), and
  the machine-landing auto-advance (INV-18) that F7/F10 consume.
- package.json: remove the toggleDiffView command, its commandPalette entry, and
  the ctrl+alt+d keybinding.
- E2E: diffView suite keeps the baseline-data-layer tests, drops the two-pane
  view tests; the F10 + no-workspace suites assert toggleDiffView is now absent
  (was: declared-but-hidden).

Deliberate deviation from the issue's literal acceptance: pinDiffBaseline is
KEPT. The canonical Solution Design (coauthoring-interactive-review.md §6.7)
scopes the removal to the two-pane VIEW only ("keep the controller + baseline
store"); pin() lives in the baseline lifecycle (§6.4), never touches vscode.diff,
and is exercised by live F7 baseline-reset tests. Where the P3 capture draft and
the approved spec conflict, the spec wins (documentation-leads-automation).

194 unit + 49 E2E green; typecheck + build clean. No F7/F10 behavior change.

Closes #34

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 02:10:14 -07:00
120 changed files with 16016 additions and 1421 deletions
+3
View File
@@ -8,3 +8,6 @@ test/e2e/fixtures/workspace/.threads/
# Sandbox playground churn (the EDH workspace - play freely, commit nothing)
sandbox/.threads/
# brainstorming visual-companion scratch
.superpowers/
+130 -7
View File
@@ -14,8 +14,59 @@ catalog (a pure, key-free SDK call) in a notification and the
Features shipped so far: F2 region-anchored threads (Feature #4), F3 live
human/Claude attribution (Feature #6), F4 propose/accept diff flow
(Feature #12), F5 cross-rung sidecar contract (Feature #14), F6 diff-view
toggle (Feature #17), and F10 interactive review — **write left / review
right** (Feature #29).
data layer (Feature #17), and the **native-surfaces migration** (the
"Coediting Markdown with a Machine" Solution Design) — the current UI, replacing
the bespoke F7/F10/F11 review webview with VS Code's own chrome end to end. See
**How it works** below for the current experience; the F7/F9/F10/F11 sections
further down are kept as a **historical record** of the superseded webview (each
now says so up top).
## How it works
There is **no bespoke panel**. Every surface below is native VS Code chrome —
the editor title bar, status bar, Comments gutter, source control, diff editor,
and the built-in Markdown preview:
1. **Enter coediting.** Run **"✦ Coedit this Document with Claude"** (command
palette, editor title menu, or editor/explorer context menu) on a Markdown
document — it captures a **baseline** (a clean checkpoint: git HEAD if the
file is tracked and clean, otherwise a snapshot) to measure future changes
against. Until a document is entered, none of the surfaces below attach to
it. **"Stop editing with Claude"** reverses it.
2. **Title bar + status bar are the everyday entry points.** Once coedited, the
editor title bar carries **Review Changes**, **Ask Claude**, **Toggle
Annotations**, and (in snapshot mode) **Mark Changes as Reviewed**; a
`✦ Coediting · N changes` status-bar item shows the pending-change count and
opens the review when clicked — no need to find the Source Control pane.
3. **Ask Claude is comments-first.** **"Ask Claude"** (editor title, or select
text → right-click → "Ask Claude to Edit") opens a focused comment box on
the native **Comments API** — on your selection, or a top-anchored
whole-document thread with none selected. Claude replies in-thread and
offers an edit; accepting the offer turns it into one or more pending
proposals.
4. **Pending changes live in the buffer.** A proposal is **optimistically
applied** straight into the editor (green/blue insertion tint, a struck
deletion hint) with an inline **`✓ Keep` / `✗ Reject`** CodeLens above each
changed block — and, once ≥2 proposals are pending, a top-of-file
**`✓ Keep all (N)` / `✗ Reject all`** pair. Nothing is force-applied: a
proposal whose target text changed underneath it is skipped, never guessed.
5. **Native diff answers "what changed?"** **Review Changes** (editor title, or
`Ctrl+Alt+R` / `Cmd+Alt+R`) opens VS Code's own diff editor — baseline on the
left, your live document on the right — with the platform's inline/side-by-side
toggle, navigation, and accessibility for free. A Source Control gutter/viewlet
entry (quick-diff change bars, "Open Changes") is a bonus home for git users,
never the required path.
6. **The built-in Markdown preview is the annotated read.** "Open Preview to the
Side" (or right-click a markdown file/tab → **"Open Cowriting Review
Preview"**, `cowriting.openReviewPreview`) renders the document with
authorship/change coloring **inside VS Code's own preview** — green = human,
blue = Claude, strikethrough = deleted — toggled by **Toggle Annotations**
(command or the title-bar eye icon). No separate webview, no separate
persistence.
Design: `vscode-cowriting-plugin-content/specs/coauthoring-native-surfaces.md`
(§5 UX Layout is the canonical source for this map). Migration plan:
[`docs/superpowers/plans/2026-07-01-native-surfaces-migration.md`](docs/superpowers/plans/2026-07-01-native-surfaces-migration.md).
## Architecture
@@ -54,6 +105,14 @@ Design: `vscode-cowriting-plugin-content/specs/coauthoring-inner-loop.md`. No li
## F3 — Live human/Claude attribution (Feature #6)
> **Commands superseded (native-surfaces migration).** The attribution **data
> layer** below is current, but the Commands block is historical:
> `Cowriting: Toggle Attribution` was retired — authorship/change coloring now
> toggles via **Toggle Annotations** on a coedited document (How it works §6) —
> and "Ask Claude to Edit Selection" is palette-hidden; use **Ask Claude to
> Edit** (`Ctrl+Alt+E` / `Cmd+Alt+E`) or the comments-first **Ask Claude** (How
> it works §3). Kept below as a historical record.
As you and Claude coauthor, every span in the buffer carries an author: human
edits render with a subtle left border, Claude-authored spans with a background
tint. Text that predates tracking stays plain — the honest record. Edits that
@@ -133,7 +192,16 @@ record per the contract; git push/pull is the transport, no re-homing ever.
Design: `vscode-cowriting-plugin-content/specs/coauthoring-cross-rung-format.md`.
## F6 — Diff-view toggle (Feature #17, #19)
## F6 — Diff-view toggle (Feature #17, #19) — SUPERSEDED
> **Superseded.** The two-pane `Ctrl+Alt+D` toggle this section describes was
> deleted in #34; only the baseline **data layer** survived, now serving the
> native diff editor (`Ctrl+Alt+R` / "Review Changes" — How it works §5) and the
> built-in preview annotations (§6). The native-surfaces migration's Task 2
> additionally **retired INV-18** (machine-landing auto-advance): a landed
> Claude edit stays a visible change until "Mark Changes as Reviewed" (the
> renamed "Pin Diff Baseline to Now"), it no longer auto-clears. Kept below as a
> historical record.
**`Ctrl+Alt+D`** (the same chord on macOS — not `Cmd`; or **Cowriting: Toggle
Diff View**) flips the focused document into a native `vscode.diff` against a
@@ -165,7 +233,14 @@ instead of git archaeology.
Design: `vscode-cowriting-plugin-content/specs/coauthoring-diff-view.md`.
Live smoke: [`docs/MANUAL-SMOKE-F6.md`](docs/MANUAL-SMOKE-F6.md).
## F7 — Rendered track-changes preview (Feature #21)
## F7 — Rendered track-changes preview (Feature #21) — SUPERSEDED
> **Superseded (Task 8, native-surfaces migration).** The bespoke webview this
> section describes was deleted; its authorship/change coloring lives on
> **inside VS Code's own built-in Markdown preview** — see **How it works** §6
> above. Kept below as a historical record of the pre-migration design. (The
> `Ctrl+Alt+R` chord below now opens the native **Review Changes** diff; the
> annotated preview opens via **Open Cowriting Review Preview**, no chord.)
**`Ctrl+Alt+R`** (or **Cowriting: Open Track-Changes Preview**) opens a
read-only webview **beside** a **Markdown** editor that renders the document and
@@ -197,7 +272,12 @@ the manual smoke, not the sealed-sandbox E2E.
Design: `vscode-cowriting-plugin-content/specs/coauthoring-rendered-preview.md`.
Live smoke: [`docs/MANUAL-SMOKE-F7.md`](docs/MANUAL-SMOKE-F7.md).
## F9 — Authorship view in the preview (Feature ~#27)
## F9 — Authorship view in the preview (Feature ~#27) — SUPERSEDED
> **Superseded (Task 8, native-surfaces migration).** The F7 webview this mode
> lived in was deleted; the built-in preview's annotations (How it works §6)
> always show authorship + change coloring together — there is no separate
> mode toggle. Kept below as a historical record.
The rendered preview (F7) gains a second mode, switched by a `[ Track changes |
Authorship ]` toggle in its header. **Authorship** mode re-renders the current
@@ -238,13 +318,23 @@ a plain PR revert with zero data migration.
Design: `vscode-cowriting-plugin-content/specs/coauthoring-out-of-workspace.md`.
Live smoke: [`docs/MANUAL-SMOKE-F8.md`](docs/MANUAL-SMOKE-F8.md).
## F10 — Interactive review: write left / review right (Feature #29)
## F10 — Interactive review: write left / review right (Feature #29) — SUPERSEDED
> **Superseded (Task 8, native-surfaces migration).** The editor is no longer
> "zero-annotation" — pending proposals now render optimistically applied
> in-buffer with `✓ Keep`/`✗ Reject` CodeLens (F12), and review happens across
> the native diff editor + built-in preview, not one dedicated webview. See
> **How it works** above. Kept below as a historical record. (The `Ctrl+Alt+R`
> chord below now opens the native **Review Changes** diff, not this panel.)
A clean, **zero-annotation editor** on the left; the rendered preview on the
right as the **single interactive review surface**. The editor carries no
attribution tint, no in-editor proposal threads, and no diff — all review lives
in the preview, toggled by the **Annotations** switch in its header (on by
default). `Ctrl+Alt+R` opens **"Open Review Preview"**.
default). Open it via `Ctrl+Alt+R`, the editor title-bar button, or
**right-click a markdown file in the Explorer / its editor tab →
"Open Cowriting Review Panel"** (#41) — the right-click acts on the clicked
document, opening it first if needed.
In the on-state the preview shows **green = human / blue = Claude /
strikethrough = deleted**, and surfaces each of Claude's pending F4 proposals as
@@ -262,6 +352,39 @@ they are no longer separate user surfaces.
Design: `vscode-cowriting-plugin-content/specs/coauthoring-interactive-review.md`.
Live smoke: [`docs/MANUAL-SMOKE-F10.md`](docs/MANUAL-SMOKE-F10.md).
## F11 — Preview toolbar as the primary interaction surface (Feature #43) — SUPERSEDED
> **Superseded (Task 8, native-surfaces migration).** The webview toolbar this
> section describes was deleted; its two controls moved to native chrome —
> **Pin baseline** is now **"Mark Changes as Reviewed"** in the editor title
> bar, and **Ask Claude** is the comments-first ask (How it works §3). Kept
> below as a historical record.
The review preview's **header toolbar** becomes the cockpit for the inner loop.
Beside the existing **Annotations** switch it gains two controls:
- **⌖ Pin baseline** — pins the previewed document's review baseline to now and
clears the change-marks (homes the previously-orphaned `pinDiffBaseline`
command, which is reachable from the palette again too).
- **✦ Ask Claude…** — one **adaptive** button. Its label flips on the preview's
selection: **Edit Selection** when text is selected in the rendered preview,
**Edit Document** otherwise. Clicking it opens a host input box for the
instruction (the LLM turn and prompt stay host-side — the sealed webview gains
no LLM/credential surface), then surfaces the result as F4 proposals — **one**
for a selection (mapped back to its source block-union), or **one per changed
hunk** for a whole-document rewrite (`diffToHunks`), each independently ✓/✗-able.
The pure render layer emits `data-src-start`/`data-src-end` on every block in
**both** modes (INV-36); the webview's only mapping duty is walking a selection
to its nearest `data-src` ancestor. Right-clicking a markdown tab → **Open Review
Preview** is the minimal gateway making the surface reachable end to end (#41/#42
expand it). Edit controls are disabled on a non-authorable doc (reading stays
allowed). No new model, no new persistence — pin via the F6 store, edits via the
F4 propose/accept seam with F3 attribution (INV-35..37).
Design: [`docs/superpowers/specs/2026-06-12-f11-preview-toolbar-interaction-surface.md`](docs/superpowers/specs/2026-06-12-f11-preview-toolbar-interaction-surface.md).
Live smoke: [`docs/MANUAL-SMOKE-F11.md`](docs/MANUAL-SMOKE-F11.md).
## Develop
- `npm run watch` — rebuild on change.
+67
View File
@@ -0,0 +1,67 @@
# Manual smoke — F11 preview toolbar as the primary interaction surface (#43)
F11 makes the rendered preview's **header toolbar** the primary interaction
surface: beside the existing **Annotations** switch it gains a **Pin baseline**
button and a **single adaptive Ask-Claude button** (Edit Selection when text is
selected in the preview, Edit Document otherwise). The webview's *visual*
behavior — the buttons, the live label flip, and the selection→source mapping —
is verified **here**, not in the automated host E2E (the webview is a sealed
sandbox; the host's message→seam wiring is covered by the F11 E2E suite). Run
once per change that touches F11. Live turns hit the SDK; use a trivial
instruction to keep them quick.
## Setup
1. `npm run build`
2. Launch the Extension Development Host (F5 in VS Code, or the Run panel) with
`sandbox/` open.
3. Open a markdown document with a few paragraphs (e.g. copy
`test/e2e/fixtures/workspace/docs/preview.md`).
## Steps
1. **Right-click gateway (PUC-6).** Right-click the markdown editor **tab** (or
use the editor title bar) → **"Cowriting: Open Review Preview"**. The preview
opens beside the editor. (The palette entry and `Ctrl+Alt+R` still work too.)
2. **Three header controls (PUC-1).** The header row shows, left to right: the
**☑ Annotations** checkbox (on), a **⌖ Pin baseline** button, and a **✦ Ask
Claude to Edit Document** button. They restyle with the theme.
3. **Adaptive label — selection (PUC-2).** Select a paragraph **in the preview**
(drag across rendered text). The Ask-Claude button label flips live to **"✦
Ask Claude to Edit Selection"**. Click elsewhere to clear the selection → it
flips back to **"✦ Ask Claude to Edit Document"**.
4. **Edit Selection (PUC-3).** With a paragraph selected in the preview, click
**Ask Claude to Edit Selection** → a host input box appears → type an
instruction (e.g. "tighten this") → submit. A **blue `cw-proposal` block**
appears **inline at that paragraph** with **✓ / ✗** (the selection mapped back
to its source block; the edit ran host-side). The editor does **not** change
(INV-10). Accept with **✓**: the replacement lands; the block clears.
5. **Edit Document (PUC-4).** Clear the selection (button reads "Edit Document").
Click it → instruct (e.g. "fix any typos and tighten") → submit. Claude's
whole-document rewrite surfaces as **one or more** independent blue proposal
blocks (one per changed hunk), each independently **✓ / ✗**-able. Accept some,
reject others — each behaves as an ordinary F10 proposal.
6. **Pin baseline (PUC-5).** Make (or accept) a few changes so the preview shows
change-marks and a non-zero summary. Click **⌖ Pin baseline**. Expect: the
change-marks **clear**, the summary resets to `+0 0`, and the header epoch
reads **`pinned <time>`**. "What changed" now counts from this moment.
7. **Off-state still maps (INV-36).** Toggle **Annotations** off (clean preview).
Selecting a paragraph still flips the button to **Edit Selection** and an edit
still works — the off/clean preview is also a selection→source surface.
8. **Non-authorable doc (PUC-1/7).** Open a markdown doc from a read-only/virtual
source (e.g. a Git diff view, or an Output channel rendered as markdown) and
open the preview. Expect: **Pin baseline** and **Ask-Claude** render
**disabled**; the **Annotations** toggle stays active (reading is allowed).
9. **Theme.** Toggle light / dark / high-contrast (`Ctrl+K Ctrl+T`). The two
toolbar buttons (enabled, hover, disabled states) restyle and stay legible.
10. **Cleanliness.** `git status` shows nothing written to the document, sidecar,
or repo by the toolbar gestures except the edits you explicitly accepted
(INV-20/35).
## Pass criteria
All ten steps behave as described; the adaptive label tracks the live selection;
the selection-scoped edit anchors at the right paragraph; the document edit
produces per-hunk proposals; pin clears the marks; edit controls are inert on a
non-authorable doc; no console errors in the webview devtools; nothing is
persisted by the toolbar beyond the edits you accepted.
+96
View File
@@ -0,0 +1,96 @@
# Manual smoke — F12 document-edit flow
Covers the document-edit-flow cluster (`specs/coauthoring-document-edit-flow.md`,
#42 · #47 · #46). This file is filled in slice by slice.
## SLICE-1 — #42 (reach): selection-aware Ask-Claude from body + tab (INV-38)
Run the extension (F5) on a markdown document under the sandbox workspace.
1. **Body, with selection (PUC-2).** Select a paragraph, right-click the editor
**body**. Expect **Ask Claude to Edit Selection** in the menu (and **not**
"Edit Document"). Pick it → instruct → submit; a single proposal lands over
the selection (existing F11 behavior, unchanged).
2. **Body, no selection (PUC-1).** Clear the selection (click once), right-click
the editor **body**. Expect **Ask Claude to Edit Document** (and **not** "Edit
Selection"). Pick it → instruct → submit; the whole-document rewrite surfaces
as F4 proposal(s) in the preview.
3. **Tab, with selection (PUC-3).** With a selection active, right-click the
editor **tab**. Expect **Ask Claude to Edit Selection**, acting on that tab's
document.
4. **Tab, no selection (PUC-3).** With no selection, right-click the editor
**tab**. Expect **Ask Claude to Edit Document**, acting on **that tab's**
document — even if a *different* editor is the active one. Open two markdown
tabs A and B, make A active, right-click B's tab → Edit Document → the
proposals land on **B**, not A.
5. **Markdown-gated.** Open a non-markdown file (e.g. `.txt`). Right-click body or
tab: neither **Ask Claude to Edit Selection** nor **Edit Document** appears.
6. **Single edit path.** Both entries route through the same `runEditAndPropose`
path — there is no second edit code path (INV-38). Nothing is written to the
document or sidecar by merely invoking the menu (INV-10/20/35) until you accept.
### Pass criteria
The body and tab menus show exactly one Ask-Claude edit entry, matching the live
selection state (selection → Edit Selection; none → Edit Document); the tab
gesture targets the clicked tab's document, not the active editor; both are absent
on non-markdown docs; no console errors.
## SLICE-2 — #47 (review): per-block proposals + word-precise attribution (INV-39/40/41)
On a markdown doc with several paragraphs, **Ask Claude to Edit Document** with a
light copy-edit instruction (e.g. "tighten the prose, fix typos").
1. **One proposal per changed block (INV-39).** Claude's pass surfaces as **one
✓/✗ block per changed paragraph/header/bullet**, not a flurry of word-level
blocks. A paragraph with several word edits is a **single** proposal; the
word-level `<ins>`/`<del>` still shows *inside* it. Untouched paragraphs show no
proposal.
2. **Changed fence is atomic (INV-23).** If Claude edits a code/mermaid fence, it
is **one** whole-fence proposal.
3. **Accept attributes only the changed words (INV-40).** Accept a block proposal,
then toggle the preview to **Authorship**/colors (or re-open in the on-state):
only the words Claude actually changed are Claude-colored; the unchanged words
in that block keep their prior author. The block is the decision unit; the word
is the attribution unit.
4. **Inserted block accepts cleanly (INV-41).** If Claude adds a new paragraph,
its proposal **accepts** (it is anchored to an adjacent block, never a
born-orphaned/zero-width proposal).
5. **Undo.** Accepting a block is currently **N undo steps** (one per changed run
inside the block) — `Ctrl+Z` repeatedly restores it. (Spec deferred note:
single-undo-step grouping is a possible follow-up.)
6. **Selection edits unchanged.** Edit *Selection* still produces exactly one
proposal over the selection (no block fan-out).
### Pass criteria
A document edit yields one in-context proposal per changed block (fences atomic);
accepting a block lands the whole block but Claude-attributes only the words it
changed; inserted blocks accept; selection edits are unaffected; no console errors.
## SLICE-3 — #46 (accept): Accept all (INV-42)
On a markdown doc, **Ask Claude to Edit Document** with a pass that changes
**several** blocks, so the preview shows **≥ 2** pending proposals.
1. **Button appears at ≥2 (PUC-6).** The preview toolbar shows **✓✓ Accept all**
only when there are **2 or more** pending proposals (and the doc is authorable,
annotations on). With 01 pending it is hidden.
2. **One gesture applies all.** Click **Accept all**: every pending proposal lands
(Claude-attributed per INV-40), the ✓/✗ blocks clear, and a status message
reports how many were accepted. No confirmation dialog.
3. **Undo restores.** `Ctrl+Z` walks back the applied edits (consistent with
single accept; a block accept is itself N steps — see SLICE-2).
4. **Orphan-skip + report.** If one proposal's target text was changed by hand
first (so it can't anchor), Accept all applies the rest and the report says
`… , N skipped (target text changed — undo or reject)`; the orphaned proposal
stays pending, its text untouched (never force-applied).
5. **Command path.** With no preview panel open, the command palette **Cowriting:
Accept All Claude Proposals** (markdown-gated) applies all proposals on the
active doc with the same report.
### Pass criteria
Accept all is offered only at ≥2 pending; one click applies every resolvable
proposal and reports the tally; orphans are skipped (not mangled) and remain
pending; the palette command works on the active doc; no console errors.
@@ -0,0 +1,123 @@
# Implementation Plan: F11 — Preview Toolbar as the Primary Interaction Surface (#43)
**Spec:** `docs/superpowers/specs/2026-06-12-f11-preview-toolbar-interaction-surface.md`
**Anchor:** Feature `benstull/vscode-cowriting-plugin#43` (F11, `type/feature`, `priority/P1`)
**Session:** vscode-cowriting-plugin-0037
This plan transcribes the spec's §7.2 slicing plan into concrete, file-level
tasks. Each slice is independently green (unit + host E2E) before the next. Host
E2E is this app's required tier (no browser/deploy stage — a VS Code extension);
no LLM in CI (edit turns stubbed). The webview's visual rendering, the adaptive
label, and the selection→source DOM lookup are manual-smoke only.
---
## SLICE-1 — Pin baseline button + reachability *(the immediate win)*
Homes the orphaned `cowriting.pinDiffBaseline` command and gives the writer a
reachable Pin control in the preview toolbar.
**Tasks**
1. **Host message routing** (`src/trackChangesPreview.ts`): extract the inline
`onDidReceiveMessage` body into a private `handleWebviewMessage(document, m)`
method; add a `pinBaseline` branch that calls `this.diffView.pin(document)`
(the *previewed* document — not `activeTextEditor`). The existing
`onDidChangeBaseline` subscription already re-renders with cleared marks.
2. **Test seam**: add `receiveMessage(uriString, m)` that resolves the doc and
calls `handleWebviewMessage`, so host E2E can simulate the raw webview message
and exercise the real routing.
3. **Webview** (`media/preview.ts` + `.css`): add a `⌖ Pin baseline` button in
`#cw-header`; click → `postMessage({ type: "pinBaseline" })`; theme-aware CSS.
4. **Shell HTML** (`shellHtml`): add the `<button id="cw-pin">` to the header row.
5. **Reachability** (`package.json`): flip `cowriting.pinDiffBaseline`'s
`commandPalette` `when` from `false` to `editorLangId == markdown`.
**Host E2E** (`test/e2e/suite/trackChangesPreview.test.ts` or new f11 suite):
open a markdown fixture with a divergent baseline → some block marked; simulate
`{type:"pinBaseline"}` via `receiveMessage``getLastModel` shows every block
`unchanged` (marks cleared) and `getBaseline(key).reason === "pinned"`.
**Unit:** none new (pure layer untouched this slice).
---
## SLICE-2 — Block-offset emission *(INV-36 data layer)*
Shared pure helper wrapping each rendered block with `data-src-start`/`-end`
(source char offsets from `BlockWithRange`) in **both** `renderReview` and
`renderPlain`. vscode-free, DOM-free, deterministic (extends INV-22/33). No UI.
**Tasks**
1. `src/trackChangesModel.ts`: a shared internal helper that prepends
`data-src-start="N" data-src-end="M"` to each block's wrapping element, routed
from both render paths using the existing `splitBlocksWithRanges` offsets.
2. Skip blocks with no live-source range (deletion-only / proposal blocks).
**Unit** (`test/trackChangesModel.test.ts`): `data-src-start/end` present and
correct on every block for both modes (offsets equal `BlockWithRange` ranges);
determinism (same inputs → identical HTML).
---
## SLICE-3 — Edit Document button + hunk path *(INV-37 document half)*
**Tasks**
1. `src/trackChangesModel.ts`: pure `diffToHunks(currentText, rewrittenText):
Array<{ start; end; replacement }>` — vscode-free, deterministic.
2. `src/trackChangesPreview.ts`: `runEditAndPropose(document, target, instruction)`
private routine; `askClaude`/`document` branch → host `showInputBox` →
`runEditTurn` over full text → `diffToHunks` → one F4 `propose()` per hunk.
3. `package.json`: register `cowriting.editDocument` (document-scoped), routed
through `runEditAndPropose({kind:"document"})`; for `#42` reuse.
4. Webview: `✦ Ask Claude to Edit Document` button (no-selection state) →
`postMessage({ type: "askClaude", scope: "document" })`.
**Unit:** `diffToHunks` over fixtures (single hunk → one range; multi-hunk →
disjoint ranges + correct replacements; unchanged → zero hunks; whole-doc
replacement → one full-range hunk).
**Host E2E:** simulate `{askClaude, scope:"document"}` with a stubbed multi-hunk
rewrite → **N** proposals matching the hunks.
---
## SLICE-4 — Adaptive Edit Selection *(INV-37 selection half; INV-36 consumer)*
**Tasks**
1. Webview: `selectionchange` listener flips the Ask-Claude label (Edit Selection
⇆ Edit Document); selection→nearest-`data-src` ancestor resolution →
`postMessage({ type:"askClaude", scope:"selection", start, end })`.
2. Host: `askClaude`/`selection` branch → `runEditAndPropose({kind:"range",
start, end})` → one `runEditTurn` → one F4 `propose()` over the block-union.
3. Edge: selection resolving to no live block → fall back to document scope.
**Host E2E:** simulate `{askClaude, scope:"selection", start, end}` with a stubbed
edit turn → exactly **one** proposal over the resolved range, anchored inline.
---
## SLICE-5 — Gateway, edges, tests & docs
**Tasks**
1. `package.json`: add `cowriting.showTrackChangesPreview` to `editor/title` with
`when: editorLangId == markdown` (minimal right-click gateway).
2. Non-authorable disabling: Pin + Ask-Claude controls render disabled when
`!isAuthorable(document)`; annotations toggle stays active.
3. Host E2E: gateway command opens the panel; controls inert on non-authorable.
4. `docs/MANUAL-SMOKE-F11.md` (live smoke script per spec §6.8).
5. README F11 section.
---
## Done = #43 acceptance (spec §7.3)
Preview toolbar hosts annotations checkbox + Pin baseline + single adaptive
Ask-Claude (Edit Selection ⇆ Edit Document) routing through existing F4/F3/F6;
edits surface as proposals (one for a selection, per-hunk for a document
rewrite); a right-click entry opens the preview; the pin command is no longer
orphaned; unit + host E2E green; live smoke performed once.
@@ -0,0 +1,827 @@
# Author-colored track changes Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Render every change as a track-changes diff where **style = operation** (underline = inserted, strikethrough = removed) and **color = author** (human green/red, Claude blue/purple), in both the rendered preview pane and the main editor pane.
**Architecture:** Fuse the two color systems that exist today — the *semantic* diff (green=add/red=remove) and the *authorship* tint (cw-by-claude/cw-by-human) — into one author-colored diff. Insertions are colored by their attribution span (data already exists). Deletions are colored by an **adjacency heuristic**: a struck run inherits the author of the insertion it is paired with in the same changed hunk; a standalone removed block falls back to neutral. Phase 1 does this in the pure render engine (preview). Phase 2 reverses INV-32 to bring the same author-colored track changes inline into the editor.
**Tech Stack:** TypeScript, VS Code extension API, `markdown-it`, `diff` (jsdiff), vitest (unit), Playwright/electron host harness (E2E). Render engine (`src/trackChangesModel.ts`) is pure and vscode-free.
## Global Constraints
- **Render engine stays pure / vscode-free / deterministic** — `src/trackChangesModel.ts` must not import `vscode`; same inputs → identical HTML (INV-22).
- **Author color mapping (verbatim):** human added `#3fb950` / human removed `#f85149` / Claude added `#58a6ff` / Claude removed `#bc8cff`. Insert = 2px underline in the author color + faint author-hued background; remove = line-through in the author color + faint background.
- **Convention:** underline = inserted, strikethrough = removed, color = author.
- **"Color means changed since the pinned baseline."** Unchanged-since-baseline text renders plain (no author coloring). The pin→clean behavior (#48) is preserved: a pinned, zero-diff doc renders with no annotation.
- **Deletion authorship = adjacency heuristic** (locked decision): paired deletions inherit the paired insertion's author; standalone deletions render neutral.
- **Two roles only:** `human` and `claude` (`AuthorKind`). No arbitrary palettes, no user-configurable colors (YAGNI).
- **Test command:** `npm test` (vitest, unit). E2E: `npm run test:e2e`. Typecheck: `npm run typecheck`.
- Commit after each task. Branch off `main` (do not commit to `main` directly).
---
# Phase 1 — Preview pane (independently shippable)
Delivers the full author-colored track-changes language in the rendered preview, where the attribution data already exists.
## File Structure (Phase 1)
- `src/trackChangesModel.ts` — add `authorAt()` + `wordDiffByAuthor()` pure helpers; route the review path through them; color proposal blocks by author; stop coloring unchanged blocks. Add `author?` to `ProposalView`.
- `src/trackChangesPreview.ts` — pass each proposal's author into its `ProposalView`.
- `media/preview.css` — author×operation classes; retire the semantic green/red and flat authorship tints.
- `test/trackChangesModel.test.ts` — new unit tests; update tests asserting old semantic classes.
---
### Task 1: `authorAt()` + `wordDiffByAuthor()` pure helpers
**Files:**
- Modify: `src/trackChangesModel.ts` (add after `wordMergedMarkdown`, ~line 475)
- Test: `test/trackChangesModel.test.ts`
**Interfaces:**
- Consumes: existing `AuthorKind`, `AuthorSpan` (trackChangesModel.ts:557-562), `diffWordsWithSpace` (already imported, line 12).
- Produces:
- `authorAt(offset: number, spans: AuthorSpan[]): AuthorKind | null` — the author whose span covers `offset` (half-open), else null.
- `wordDiffByAuthor(beforeRaw: string, afterRaw: string, afterStart: number, spans: AuthorSpan[]): string` — inline HTML where each inserted run is `<ins class="cw-ins-{author}">` (author looked up at the run's `afterStart`-relative landed offset) and each removed run is `<del class="cw-del-{author}">` (author = the insertion it's paired with in the same change cluster; `cw-del-none` if standalone). Unchanged parts are emitted verbatim.
- [ ] **Step 1: Write the failing test**
Add to `test/trackChangesModel.test.ts` (import `authorAt`, `wordDiffByAuthor` from `../src/trackChangesModel`):
```ts
describe("authorAt", () => {
const spans: AuthorSpan[] = [
{ start: 0, end: 5, author: "human" },
{ start: 5, end: 10, author: "claude" },
];
test("returns the covering span's author (half-open)", () => {
expect(authorAt(0, spans)).toBe("human");
expect(authorAt(4, spans)).toBe("human");
expect(authorAt(5, spans)).toBe("claude");
expect(authorAt(9, spans)).toBe("claude");
});
test("returns null outside any span", () => {
expect(authorAt(10, spans)).toBeNull();
expect(authorAt(-1, spans)).toBeNull();
});
});
describe("wordDiffByAuthor", () => {
test("an inserted run is colored by the author covering its landed offset", () => {
// before "hello " ; after "hello brave " ; "brave " inserted at landed offset 6..12
const spans: AuthorSpan[] = [{ start: 6, end: 12, author: "claude" }];
const html = wordDiffByAuthor("hello ", "hello brave ", 0, spans);
expect(html).toContain('<ins class="cw-ins-claude">brave </ins>');
expect(html).toContain("hello ");
});
test("a paired deletion inherits the paired insertion's author (adjacency)", () => {
// "light" -> "dark", both in one cluster; "dark" is claude-inserted
const spans: AuthorSpan[] = [{ start: 0, end: 4, author: "claude" }];
const html = wordDiffByAuthor("light", "dark", 0, spans);
expect(html).toContain('<del class="cw-del-claude">light</del>');
expect(html).toContain('<ins class="cw-ins-claude">dark</ins>');
});
test("a standalone deletion (no paired insertion) is neutral", () => {
const html = wordDiffByAuthor("keep this word", "keep word", 0, []);
expect(html).toContain('<del class="cw-del-none">this </del>');
expect(html).not.toContain("cw-del-human");
expect(html).not.toContain("cw-del-claude");
});
});
```
- [ ] **Step 2: Run test to verify it fails**
Run: `npm test -- trackChangesModel`
Expected: FAIL — `authorAt`/`wordDiffByAuthor` not exported.
- [ ] **Step 3: Write minimal implementation**
Add to `src/trackChangesModel.ts` after `wordMergedMarkdown` (line 475):
```ts
/** The author whose span covers `offset` (half-open [start,end)), or null. */
export function authorAt(offset: number, spans: AuthorSpan[]): AuthorKind | null {
for (const s of spans) if (offset >= s.start && offset < s.end) return s.author;
return null;
}
const insClass = (a: AuthorKind | null): string => `cw-ins-${a ?? "none"}`;
const delClass = (a: AuthorKind | null): string => `cw-del-${a ?? "none"}`;
/**
* Author-colored word diff for a changed PROSE block in the review.
* `afterStart` is the landed-text offset of `afterRaw[0]` so an inserted run's
* offset maps into `spans`. Insertions are colored by the author covering their
* offset; deletions inherit the author of the insertion they are paired with in
* the same contiguous change cluster (adjacency heuristic) — `cw-del-none` when a
* cluster has no insertion. Pure, deterministic.
*/
export function wordDiffByAuthor(
beforeRaw: string,
afterRaw: string,
afterStart: number,
spans: AuthorSpan[],
): string {
const parts = diffWordsWithSpace(beforeRaw, afterRaw);
// First pass: find each change cluster's insertion author (first added run).
// A cluster is a maximal run of added/removed parts bounded by unchanged parts.
let afterOff = afterStart;
const clusterAuthor: (AuthorKind | null)[] = []; // per part index, the cluster's del author
{
let off = afterStart;
let i = 0;
while (i < parts.length) {
const p = parts[i];
if (!p.added && !p.removed) {
clusterAuthor[i] = null;
off += p.value.length;
i++;
continue;
}
// a cluster: scan to its end, capturing the first added run's author
let j = i;
let author: AuthorKind | null = null;
let scanOff = off;
while (j < parts.length && (parts[j].added || parts[j].removed)) {
if (parts[j].added && author === null) author = authorAt(scanOff, spans);
if (parts[j].added) scanOff += parts[j].value.length;
j++;
}
for (let k = i; k < j; k++) clusterAuthor[k] = author;
off = scanOff;
i = j;
}
}
// Second pass: emit.
const out: string[] = [];
parts.forEach((p, i) => {
if (p.added) {
const a = authorAt(afterOff, spans);
out.push(`<ins class="${insClass(a)}">${p.value}</ins>`);
afterOff += p.value.length;
} else if (p.removed) {
out.push(`<del class="${delClass(clusterAuthor[i] ?? null)}">${p.value}</del>`);
} else {
out.push(p.value);
afterOff += p.value.length;
}
});
return out.join("");
}
```
- [ ] **Step 4: Run test to verify it passes**
Run: `npm test -- trackChangesModel`
Expected: PASS (new describes green).
- [ ] **Step 5: Commit**
```bash
git add src/trackChangesModel.ts test/trackChangesModel.test.ts
git commit -m "feat: authorAt + wordDiffByAuthor pure helpers (author-colored word diff w/ adjacency del)"
```
---
### Task 2: Route the review path through `wordDiffByAuthor`; stop coloring unchanged blocks
**Files:**
- Modify: `src/trackChangesModel.ts``renderReviewOp` (769-780), the `colored` closure + changed-op handling in `renderReview` (889-897)
- Test: `test/trackChangesModel.test.ts`
**Interfaces:**
- Consumes: `wordDiffByAuthor` (Task 1), `landedSpans` + `ranges` already computed in `renderReview`.
- Produces: review HTML where changed prose blocks emit `cw-ins-{author}`/`cw-del-{author}`, added blocks are author-colored as insertions, and **unchanged blocks render plain**.
- [ ] **Step 1: Write the failing test**
Add to the `renderReview` describe in `test/trackChangesModel.test.ts`:
```ts
test("renderReview: a changed prose block colors ins/del by author (claude replace)", () => {
// baseline "light" -> current "dark"; "dark" attributed to claude at 0..4
const html = renderReview("light", "dark", [{ start: 0, end: 4, author: "claude" }], []);
expect(html).toContain('cw-ins-claude');
expect(html).toContain('cw-del-claude'); // adjacency: struck "light" inherits claude
});
test("renderReview: an unchanged block renders plain (no author coloring)", () => {
const doc = "Alpha stays.";
const html = renderReview(doc, doc, [{ start: 0, end: 12, author: "human" }], []);
expect(html).not.toContain("cw-by-");
expect(html).not.toContain("cw-ins-");
});
test("renderReview: an added block is author-colored as an insertion", () => {
// baseline empty-ish -> add a new paragraph authored by human
const html = renderReview("Keep.", "Keep.\n\nFresh line.", [{ start: 6, end: 17, author: "human" }], []);
expect(html).toContain("cw-ins-human");
});
```
- [ ] **Step 2: Run test to verify it fails**
Run: `npm test -- trackChangesModel`
Expected: FAIL — changed blocks still emit plain `<ins>/<del>` via `wordMergedMarkdown`; unchanged blocks still carry `cw-by-*`.
- [ ] **Step 3: Write minimal implementation**
In `src/trackChangesModel.ts`:
**(a)** Change `renderReviewOp` (769-780) so a changed PROSE block uses author coloring. Give it the colored-insertion path for `added` and the author word-diff for non-atomic `changed`. Replace the function body:
```ts
function renderReviewOp(
op: BlockOp,
render: (src: string) => string,
colored: (raw: string) => string,
src: string,
changedHtml?: string,
): string {
// A non-atomic changed prose block is pre-rendered with author-colored ins/del
// (changedHtml). Removed blocks and atomic changed fences stay neutral via renderOp
// (standalone deletion → neutral, per the adjacency heuristic). `src` is "" for a
// removed block (no live source — INV-36).
if (op.kind === "changed" && !op.atomic && changedHtml !== undefined) {
return `<div class="cw-blk cw-changed"${src}>${changedHtml}</div>`;
}
if (op.kind === "removed" || op.kind === "changed") return renderOp(op, render, src);
return `<div class="cw-blk ${op.kind === "added" ? "cw-added" : "cw-unchanged"}"${src}>${colored(op.block.raw)}</div>`;
}
```
**(b)** In `renderReview` (the loop at 889-897), compute the author-colored changed HTML and stop coloring unchanged blocks. Replace the loop body:
```ts
let ci = 0; // pointer into ranges; advances for every op with a current-side block
const bodyParts: string[] = [];
for (const op of ops) {
const blockIndex = op.kind === "removed" ? -1 : ci;
const blk = op.kind === "removed" ? undefined : ranges[ci++];
// Only ADDED blocks are author-colored (they are insertions since baseline);
// UNCHANGED blocks render plain (color means "changed since baseline").
const colored = (raw: string): string =>
blk && !clean && op.kind === "added" ? colorByAuthor(raw, blk.start, landedSpans, render) : render(raw);
// A non-atomic changed prose block: author-colored word diff (ins by author,
// del by adjacency). `blk.start` is the landed offset of the after-side text.
const changedHtml =
op.kind === "changed" && !op.atomic && blk && !clean
? render(wordDiffByAuthor(op.before.raw, op.block.raw, blk.start, landedSpans))
: undefined;
bodyParts.push(renderReviewOp(op, render, colored, srcAttr(blk), changedHtml));
const here = blockIndex >= 0 ? byBlock.get(blockIndex) : undefined;
if (here) for (const p of here) bodyParts.push(proposalBlockHtml(p, render));
}
for (const p of trailing) bodyParts.push(proposalBlockHtml(p, render));
return bodyParts.join("\n");
```
> Note: `render(wordDiffByAuthor(...))` passes the HTML-bearing string through markdown-it (same as `wordMergedMarkdown` was rendered). `colorByAuthor` on added blocks emits `cw-by-{author}` spans — Task 4 restyles those to insert styling in CSS.
- [ ] **Step 4: Run test to verify it passes**
Run: `npm test -- trackChangesModel`
Expected: PASS for the new tests.
- [ ] **Step 5: Update tests that assert the OLD review markup**
Two existing tests assert the old semantic output (test/trackChangesModel.test.ts:226-232). Update them:
```ts
test("renderReview: human addition since baseline renders author-colored ins", () => {
const html = renderReview("hello", "hello world", [{ start: 6, end: 11, author: "human" }], []);
expect(html).toContain("cw-ins-human");
});
test("renderReview: a standalone deletion since baseline renders neutral struck del", () => {
const html = renderReview("hello world", "hello", [], []);
expect(html).toMatch(/cw-del-none|cw-removed/);
});
```
Run: `npm test -- trackChangesModel`
Expected: PASS (whole file green).
- [ ] **Step 6: Commit**
```bash
git add src/trackChangesModel.ts test/trackChangesModel.test.ts
git commit -m "feat: author-colored changed/added blocks in review; unchanged blocks render plain"
```
---
### Task 3: Color pending proposal blocks by author
**Files:**
- Modify: `src/trackChangesModel.ts``ProposalView` (731-742) + `proposalBlockHtml` (744-767)
- Modify: `src/trackChangesPreview.ts` — populate `author` on each `ProposalView`
- Test: `test/trackChangesModel.test.ts`
**Interfaces:**
- Consumes: `AuthorKind` (557).
- Produces: `ProposalView.author?: AuthorKind` (defaults to `"claude"`); `proposalBlockHtml` emits `cw-del-{author}` / `cw-ins-{author}`.
- [ ] **Step 1: Write the failing test**
```ts
test("renderReview: a proposal block colors its del/ins by author (claude default)", () => {
const proposals: ProposalView[] = [{ id: "p1", anchorStart: 0, anchorEnd: 5, replaced: "hello", replacement: "goodbye" }];
const html = renderReview("hello", "hello", [], proposals);
expect(html).toContain('cw-del-claude');
expect(html).toContain('cw-ins-claude');
});
```
- [ ] **Step 2: Run test to verify it fails**
Run: `npm test -- trackChangesModel`
Expected: FAIL — `proposalBlockHtml` still emits `cw-del` / `cw-add`.
- [ ] **Step 3: Write minimal implementation**
In `src/trackChangesModel.ts`, add `author?` to `ProposalView` (after line 741):
```ts
/** F12/#64 (INV-48): the pre-apply original, set after optimistic apply. */
original?: string;
/** Who made the proposal — colors the del/ins. Defaults to "claude". */
author?: AuthorKind;
}
```
Update `proposalBlockHtml` (753-754):
```ts
const who = p.author ?? "claude";
const before = p.replaced ? `<del class="cw-del-${who}">${safe(p.replaced)}</del>` : "";
const after = `<ins class="cw-ins-${who}">${safe(p.replacement)}</ins>`;
```
- [ ] **Step 4: Run test to verify it passes**
Run: `npm test -- trackChangesModel`
Expected: PASS.
- [ ] **Step 5: Populate `author` from the live proposal**
In `src/trackChangesPreview.ts` find where `ProposalView`s are built for `renderReview` (the `listProposals` mapping near line 373). Add `author` from the proposal's provenance. Locate the mapping and add:
```ts
author: p.author?.kind === "agent" ? "claude" : "human",
```
(where `p` is the live `Proposal` with `author: Provenance`). If the preview builds `ProposalView` via a helper, add the field there. Run `npm run typecheck` to confirm the field name matches the live `Proposal.author` shape (`model.ts:79-108`).
- [ ] **Step 6: Run typecheck + tests**
Run: `npm run typecheck && npm test -- trackChangesModel`
Expected: PASS.
- [ ] **Step 7: Commit**
```bash
git add src/trackChangesModel.ts src/trackChangesPreview.ts test/trackChangesModel.test.ts
git commit -m "feat: color pending proposal blocks by author (Claude blue/purple)"
```
---
### Task 4: CSS — author×operation color system (preview)
**Files:**
- Modify: `media/preview.css`
**Interfaces:**
- Consumes: classes emitted by Tasks 1-3: `cw-ins-human|claude|none`, `cw-del-human|claude|none`, `cw-by-human|claude` (added blocks), `cw-added`, `cw-removed`, `cw-changed`, `.cw-proposal`.
- Produces: the locked color mapping; unchanged text plain.
- [ ] **Step 1: Replace the semantic diff + authorship-tint rules**
In `media/preview.css`, replace lines 21-33 and 55-58 with author-colored rules. New block:
```css
/* Author-colored track changes — style = operation, color = author.
underline = inserted, strikethrough = removed; human green/red, Claude blue/purple. */
#cw-summary .cw-add { color: #3fb950; }
#cw-summary .cw-del { color: #f85149; }
.cw-blk { position: relative; }
/* base ins/del carry the operation; author classes carry the color */
ins { text-decoration: none; }
del { text-decoration: line-through; opacity: 0.7; }
.cw-ins-human { background: rgba(63,185,80,0.14); border-bottom: 2px solid #3fb950; text-decoration: none; }
.cw-ins-claude { background: rgba(88,166,255,0.15); border-bottom: 2px solid #58a6ff; text-decoration: none; }
.cw-ins-none { background: var(--vscode-diffEditor-insertedTextBackground, rgba(63,185,80,0.14)); text-decoration: none; }
.cw-del-human { background: rgba(248,81,73,0.11); text-decoration: line-through; text-decoration-color: #f85149; }
.cw-del-claude { background: rgba(188,140,255,0.13); text-decoration: line-through; text-decoration-color: #bc8cff; }
.cw-del-none { background: var(--vscode-diffEditor-removedTextBackground, rgba(248,81,73,0.11)); text-decoration: line-through; opacity: 0.7; }
/* whole-block ops: an added block is an insertion (author-colored inside via cw-by-*);
a standalone removed block is neutral struck (adjacency heuristic fallback) */
.cw-added { background: transparent; }
.cw-removed { background: var(--vscode-diffEditor-removedTextBackground, rgba(248,81,73,0.10)); text-decoration: line-through; opacity: 0.65; }
.cw-changed { outline: none; }
/* added-block author runs (colorByAuthor sentinels) read as insertions */
.cw-by-human { background: rgba(63,185,80,0.14); border-bottom: 2px solid #3fb950; text-decoration: none; }
.cw-by-claude { background: rgba(88,166,255,0.15); border-bottom: 2px solid #58a6ff; text-decoration: none; }
.cw-blk.cw-by-claude, .cw-blk.cw-by-human, .cw-blk.cw-mixed { outline: 2px solid currentColor; outline-offset: 2px; background: transparent; }
#cw-legend .cw-swatch { padding: 0 0.4em; border-radius: 3px; }
#cw-summary .cw-prop { opacity: 0.85; }
```
- [ ] **Step 2: Recolor the proposal block accent to neutral (author lives inside it now)**
The proposal block's left border is currently blue (`--vscode-charts-blue`). Keep it as a neutral "pending" frame — the del/ins inside now carry the author color. Change `media/preview.css:79-80`:
```css
border-left: 3px solid var(--vscode-panel-border, #555);
background: color-mix(in srgb, var(--vscode-foreground) 5%, transparent);
```
- [ ] **Step 3: Manual visual verification**
Run the extension host (or load the smoke doc) and confirm in the preview: human insertions green-underlined, Claude insertions blue-underlined, Claude deletions purple-struck, standalone deletions neutral, unchanged text plain.
Run: `npm run build`
Expected: builds clean (CSS is bundled into the webview assets per the build).
- [ ] **Step 4: Commit**
```bash
git add media/preview.css
git commit -m "feat: author-colored track-changes CSS for the preview (human green/red, Claude blue/purple)"
```
---
### Task 5: Phase-1 host E2E — author colors render in the preview
**Files:**
- Test: the existing F10/F7 preview E2E suite (find under `test/e2e/` — the preview/review specs)
**Interfaces:**
- Consumes: the rendered preview webview HTML.
- [ ] **Step 1: Write the failing E2E assertions**
In the preview E2E spec, after producing a Claude change + a human change on a doc, assert the webview body contains `cw-ins-claude` and `cw-ins-human`, and that an unchanged paragraph has no `cw-ins-`/`cw-by-` class. (Follow the existing pattern in that spec for opening the preview and reading `panel.webview` HTML or the DOM via the harness.)
- [ ] **Step 2: Run to verify it fails (or update an assertion that checked old classes)**
Run: `npm run test:e2e`
Expected: FAIL if any existing assertion checked `cw-add`/`cw-by-*`-as-authorship; update those to the new classes.
- [ ] **Step 3: Make it pass**
Adjust any E2E assertion referencing retired classes (`cw-add`, semantic `ins`/`del` colors, `cw-by-*` on unchanged blocks) to the new author classes.
Run: `npm run test:e2e`
Expected: PASS.
- [ ] **Step 4: Commit**
```bash
git add test/e2e
git commit -m "test(e2e): assert author-colored track changes render in the preview"
```
**>>> Phase 1 is independently shippable here. Open a PR for the preview pane before starting Phase 2, or continue.**
---
# Phase 2 — Editor pane (reverses INV-32)
Brings the same author-colored track changes inline into the main editor: all changes-since-baseline (committed + pending), not just pending proposals.
## File Structure (Phase 2)
- `src/editorProposalController.ts` — per-author decoration types; recolor proposals; subscribe to baseline + attribution; decorate all committed insertions (author-colored) + committed deletion hints (adjacency); overlap stacks naturally.
- `src/extension.ts` — inject `DiffViewController` + `AttributionController` into `EditorProposalController`.
- Test: editor E2E suite.
---
### Task 6: Per-author decoration types; recolor proposals to Claude
**Files:**
- Modify: `src/editorProposalController.ts` (20-27 decoration types; 80-106 renderEditor)
- Test: editor E2E
**Interfaces:**
- Consumes: `decorationPlan` (already imported), proposal list.
- Produces: four insertion decoration types (`human`/`claude`) keyed by author, deletion-hint types colored by author; proposals decorate with Claude colors.
- [ ] **Step 1: Replace the two decoration types with per-author maps**
In `src/editorProposalController.ts`, replace lines 20-27:
```ts
private readonly insDeco: Record<"human" | "claude", vscode.TextEditorDecorationType> = {
human: vscode.window.createTextEditorDecorationType({
backgroundColor: "rgba(63,185,80,0.14)", borderColor: "#3fb950",
border: "0 0 2px 0", borderStyle: "solid",
}),
claude: vscode.window.createTextEditorDecorationType({
backgroundColor: "rgba(88,166,255,0.15)", borderColor: "#58a6ff",
border: "0 0 2px 0", borderStyle: "solid",
}),
};
private readonly delDeco: Record<"human" | "claude", vscode.TextEditorDecorationType> = {
human: vscode.window.createTextEditorDecorationType({ after: { color: "#f85149" } }),
claude: vscode.window.createTextEditorDecorationType({ after: { color: "#bc8cff" } }),
};
```
Update the constructor's disposables push (39-40) to dispose all four:
```ts
this.insDeco.human, this.insDeco.claude, this.delDeco.human, this.delDeco.claude, this.lensEmitter,
```
- [ ] **Step 2: Recolor proposal decorations to Claude in `renderEditor`**
Replace the body of `renderEditor` (81-106) so it groups ranges per author and applies the right decoration type. Proposals are Claude:
```ts
private renderEditor(editor: vscode.TextEditor): void {
const doc = editor.document;
const clearAll = () => {
for (const a of ["human", "claude"] as const) {
editor.setDecorations(this.insDeco[a], []);
editor.setDecorations(this.delDeco[a], []);
}
};
if (doc.languageId !== "markdown") return clearAll();
const key = this.proposals.keyFor(doc);
const ins: Record<"human" | "claude", vscode.Range[]> = { human: [], claude: [] };
const del: Record<"human" | "claude", vscode.DecorationOptions[]> = { human: [], claude: [] };
// Pending proposals — always Claude (INV: proposals are agent-authored).
for (const v of this.proposals.listProposals(doc)) {
if (v.anchorStart === null || v.original === undefined || !this.proposals.isApplied(key, v.id)) continue;
const plan = decorationPlan(v.anchorStart, v.original, v.replacement);
for (const i of plan.insertions) ins.claude.push(new vscode.Range(doc.positionAt(i.start), doc.positionAt(i.end)));
for (const d of plan.deletions) {
del.claude.push({
range: new vscode.Range(doc.positionAt(d.at), doc.positionAt(d.at)),
renderOptions: { after: { contentText: ` ${d.text} `, textDecoration: "line-through" } },
});
}
}
// Committed changes-since-baseline are added in Task 7 (pushes into ins/del[author]).
this.decorateCommitted(editor, ins, del); // no-op stub until Task 7
for (const a of ["human", "claude"] as const) {
editor.setDecorations(this.insDeco[a], ins[a]);
editor.setDecorations(this.delDeco[a], del[a]);
}
}
/** Overridden in Task 7 to add committed (non-proposal) author-colored changes. */
private decorateCommitted(
_editor: vscode.TextEditor,
_ins: Record<"human" | "claude", vscode.Range[]>,
_del: Record<"human" | "claude", vscode.DecorationOptions[]>,
): void {}
```
- [ ] **Step 3: Typecheck + build**
Run: `npm run typecheck && npm run build`
Expected: clean.
- [ ] **Step 4: E2E — proposal shows Claude colors**
Update/extend the editor E2E to assert a pending proposal decorates (smoke: the decoration types exist and apply; the harness asserts via the proposal-applied path used today). Run: `npm run test:e2e` → PASS.
- [ ] **Step 5: Commit**
```bash
git add src/editorProposalController.ts test/e2e
git commit -m "feat: per-author editor decoration types; proposals decorate in Claude blue/purple"
```
---
### Task 7: Decorate committed changes-since-baseline by author (reverse INV-32)
**Files:**
- Modify: `src/editorProposalController.ts` — inject baseline + attribution; implement `decorateCommitted`; subscribe to baseline/attribution change events
- Modify: `src/extension.ts` (line 122) — pass the two new deps
- Test: editor E2E
**Interfaces:**
- Consumes: `DiffViewController.getBaseline(uri)` (diffViewController.ts:133) + `onDidChangeBaseline` (32); `AttributionController.spansFor(document)` (attributionController.ts:405); pure `wordEditHunks` + `authorAt` (trackChangesModel.ts).
- Produces: committed insertions colored by `authorAt(currentOffset)`; committed deletion hints colored by adjacency (the paired insertion's author).
- [ ] **Step 1: Inject the new dependencies**
Change the constructor signature (38):
```ts
constructor(
private readonly proposals: ProposalController,
private readonly diffView: import("./diffViewController").DiffViewController,
private readonly attribution: import("./attributionController").AttributionController,
) {
```
Add subscriptions in the constructor disposables list:
```ts
this.diffView.onDidChangeBaseline(({ uri }) => {
const ed = vscode.window.visibleTextEditors.find((e) => e.document.uri.toString() === uri);
if (ed) this.renderEditor(ed);
}),
```
In `src/extension.ts` (122) pass the deps (both already constructed above — `diffViewController` and `attributionController`):
```ts
const editorProposalController = new EditorProposalController(
proposalController,
diffViewController,
attributionController,
);
```
(Confirm the local variable names in `extension.ts` for the diff-view + attribution controllers via `npm run typecheck`.)
- [ ] **Step 2: Implement `decorateCommitted`**
Replace the stub with the real implementation. It diffs baseline → current, attributes insertions, and colors deletion hints by adjacency:
```ts
private decorateCommitted(
editor: vscode.TextEditor,
ins: Record<"human" | "claude", vscode.Range[]>,
del: Record<"human" | "claude", vscode.DecorationOptions[]>,
): void {
const doc = editor.document;
const baseline = this.diffView.getBaseline(doc.uri.toString());
if (!baseline || baseline.reason === "pinned") return; // pin→clean: no committed marks
const current = doc.getText();
const spans = this.attribution.spansFor(doc); // AuthorSpan[] in current coords
const hunks = wordEditHunks(baseline.text, current); // start/end index INTO current
for (const h of hunks) {
// The replacement text occupies [h.start, h.start+replacement.length) in current.
// Re-derive the intra-hunk word diff to split ins vs del and find the author.
const original = baseline.text.slice(/* mapped */ 0, 0); // see note
const plan = decorationPlan(h.start, hunkOriginal(baseline.text, current, h), current.slice(h.start, h.end));
const author = authorAt(h.start, spans) ?? "human";
for (const i of plan.insertions) ins[author].push(new vscode.Range(doc.positionAt(i.start), doc.positionAt(i.end)));
for (const d of plan.deletions) {
del[author].push({
range: new vscode.Range(doc.positionAt(d.at), doc.positionAt(d.at)),
renderOptions: { after: { contentText: ` ${d.text} `, textDecoration: "line-through" } },
});
}
}
}
```
> **Implementation note for the engineer:** `wordEditHunks(baseline, current)` returns hunks whose `start`/`end` index into **current** with a `replacement` that is the *baseline* text for that span (see trackChangesModel.ts:241-264 — `replacement` accumulates `removed` parts = baseline text, `end` advances over them). So for a committed change, the *original* (baseline) text of a hunk is `h.replacement` and the *applied* (current) text is `current.slice(h.start, h.end)`. Call `decorationPlan(h.start, h.replacement, current.slice(h.start, h.end))` directly — drop the `hunkOriginal`/`original` placeholder lines above. Verify this against a unit test before wiring (Step 3).
- [ ] **Step 3: Unit-test the hunk→plan derivation (pure)**
Because the offset semantics are subtle, add a pure unit test in `test/trackChangesModel.test.ts` proving `wordEditHunks` + `decorationPlan` reconstruct a committed change. Add an exported helper if needed:
```ts
test("committed change: wordEditHunks replacement is baseline text; current slice is applied", () => {
const baseline = "the light mode";
const current = "the dark mode";
const [h] = wordEditHunks(baseline, current);
expect(h.replacement).toContain("light"); // baseline side
expect(current.slice(h.start, h.end)).toContain("dark"); // applied side
const plan = decorationPlan(h.start, h.replacement, current.slice(h.start, h.end));
expect(plan.insertions.length).toBeGreaterThan(0);
expect(plan.deletions.some((d) => d.text.includes("light"))).toBe(true);
});
```
Run: `npm test -- trackChangesModel` → PASS. Then finalize `decorateCommitted` to use `h.replacement` + `current.slice(h.start, h.end)` (removing the placeholder lines).
- [ ] **Step 4: Add the attribution-change refresh**
Editor decorations must refresh when attribution changes (e.g. after a human edit). If `AttributionController` exposes a change event, subscribe to it and call `renderEditor`; otherwise refresh on `vscode.workspace.onDidChangeTextDocument` for the active editor (debounced like `scheduleApply`). Add to the constructor disposables:
```ts
vscode.workspace.onDidChangeTextDocument((e) => {
const ed = vscode.window.activeTextEditor;
if (ed && e.document === ed.document) this.scheduleRender(ed);
}),
```
with a small `scheduleRender(ed)` that debounces `renderEditor(ed)` (mirror `scheduleApply`, 50ms).
- [ ] **Step 5: Typecheck + build + E2E**
Run: `npm run typecheck && npm run build && npm run test:e2e`
Expected: clean. E2E: a committed human edit shows green underline; an accepted Claude replace shows blue underline + purple struck hint; a pinned baseline shows nothing.
- [ ] **Step 6: Commit**
```bash
git add src/editorProposalController.ts src/extension.ts test/trackChangesModel.test.ts test/e2e
git commit -m "feat: author-colored committed track changes inline in the editor (reverse INV-32)"
```
---
### Task 8: Overlap — stacked marks where both authors touch one span
**Files:**
- Test: `test/trackChangesModel.test.ts` (preview overlap) + editor E2E (editor overlap)
- Modify (if needed): `media/preview.css` (ensure `<ins>` nested in `<del>` and vice-versa render both marks)
**Interfaces:**
- Consumes: existing emitted classes.
- Produces: a span both inserted (author A) and being deleted (author B) shows both A's underline and B's strikethrough.
- [ ] **Step 1: Editor overlap — verify decorations stack**
In the editor, an overlap arises when a committed insertion range (author A) is also covered by a pending proposal deletion (Claude). VS Code applies multiple decoration types to overlapping ranges, so the author-A insertion underline and the Claude deletion hint already coexist. Add an editor E2E asserting both decoration types are present on the overlap region. No code change expected; if the proposal's optimistic apply *replaces* the text (so A's range no longer exists), document that committed-insert + pending-delete overlap manifests as the proposal's struck original (Claude purple) adjacent to A's surviving insertion — assert that instead.
- [ ] **Step 2: Preview overlap — CSS nesting**
Add a CSS test fixture / assertion: a `<del class="cw-del-claude"><span class="cw-ins-human">…</span></del>` renders with both strikethrough (Claude purple) and underline (human green). Add to `media/preview.css` if the nested case collapses:
```css
.cw-del-claude .cw-ins-human, .cw-del-human .cw-ins-claude,
.cw-del-claude .cw-by-human, .cw-del-human .cw-by-claude { text-decoration: inherit; }
```
- [ ] **Step 3: Run tests**
Run: `npm test && npm run test:e2e`
Expected: PASS.
- [ ] **Step 4: Commit**
```bash
git add media/preview.css test/trackChangesModel.test.ts test/e2e
git commit -m "feat: overlap renders stacked author marks (insert + delete) in both panes"
```
---
### Task 9: Sweep retired markup + full suite + manual smoke
**Files:**
- `test/**`, `media/preview.css`, any code referencing retired classes.
- [ ] **Step 1: Grep for retired classes/behaviors**
Run:
```bash
grep -rn "cw-add\b\|cw-del\b\|diffEditor.insertedTextBackground\|gitDecoration.deletedResourceForeground" src/ media/ test/
```
Reconcile every hit: `cw-add`/`cw-del` (without `-author`) only legitimately remain in `#cw-summary`. The editor's old semantic ThemeColors are gone (replaced in Task 6).
- [ ] **Step 2: Full unit + typecheck + build**
Run: `npm run typecheck && npm test && npm run build`
Expected: all green.
- [ ] **Step 3: Full E2E**
Run: `npm run test:e2e`
Expected: all green (update any remaining old-class assertions).
- [ ] **Step 4: Manual smoke (both panes)**
Open the sandbox doc, make a human edit + ask Claude to edit. Confirm: editor shows green/blue underlines + purple/red struck hints; preview shows the same; unchanged text plain in both; pin baseline → both panes clean; accept a proposal → it recolors as a committed change until re-pin.
- [ ] **Step 5: Commit + open PR**
```bash
git add -A
git commit -m "chore: sweep retired semantic-diff markup; full suite green"
```
---
## Self-Review
**1. Spec coverage:**
- Model (style=op, color=author) → Tasks 1-4 (preview), 6-7 (editor). ✓
- Human green/red + Claude blue/purple, exact hexes → Global Constraints + Task 4 CSS + Task 6 decorations. ✓
- Both panes → Phase 1 (preview) + Phase 2 (editor). ✓
- Reverses INV-32 → Task 7. ✓
- Both overlap directions, stacked → Task 8. ✓
- Color = changed-since-baseline; pin→clean preserved → Task 2 (unchanged plain), Task 7 (`reason === "pinned"` guard). ✓
- Deletion authorship = adjacency heuristic; standalone neutral → Task 1 (`wordDiffByAuthor` cluster author + `cw-del-none`), Task 7 (`authorAt(h.start)`). ✓
- Components: trackChangesModel, editorProposalController, attributionController (read via spansFor), preview.css, extension.ts wiring → covered. ✓
- Testing: unit (pure render), editor decorations (E2E), host E2E, retire old assertions → Tasks 1-3, 5, 7-9. ✓
**2. Placeholder scan:** Task 7 Step 2 intentionally shows a *wrong* placeholder (`hunkOriginal`/`original`) and immediately corrects it in the implementation note + Step 3 unit test that pins the exact offsets — this is a guided derivation, not an unresolved placeholder. No other TBD/TODO.
**3. Type consistency:** `AuthorKind` (`"claude" | "human"`) used throughout; decoration maps keyed `"human" | "claude"`; `authorAt` returns `AuthorKind | null``?? "human"`/`?? "none"` at every call site; `ProposalView.author?: AuthorKind`; `wordDiffByAuthor(beforeRaw, afterRaw, afterStart, spans)` signature matches both its test and its `renderReview` call site.
## Execution Handoff
(Filled in after you review.)
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,526 @@
# #70 — INV-5 Reject-After-Interior-Edit Gap Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** `✗ Reject` on an optimistically-applied proposal restores the retained original (INV-5) even after the writer typed inside the pending range — and when it genuinely cannot locate the applied span, it fails loudly instead of reporting success while leaving the proposed text behind.
**Architecture:** Issue #70's fix direction **(a)** with an honest **(b)** fallback. A new per-doc `appliedSpans: Map<proposalId, OffsetRange>` becomes the real F12 applied-range bookkeeping: written at optimistic-apply time, re-synced whenever the exact anchor resolves at `renderAll`, shifted through interior-safe buffer edits (`shift()`), and **deleted (distrusted)** when an edit straddles a span boundary (e.g. a whole-buffer external replace — a shifted range clamps to garbage there, and restoring at a guessed offset would corrupt the doc, INV-11's "never applied by guess"). `revertInPlace` then reverts via exact resolve → `appliedSpans` fallback → hard failure (warning, proposal kept, `false` returned). Direction (c) (fuzzy resolve) is rejected: it guesses. `rejectAll` orders by the same fallback and reports skips like `acceptAllProposals` does.
**Tech Stack:** TypeScript VS Code extension; `npm test` (vitest unit), `npm run test:e2e` (@vscode/test-electron host suite — `ProposalController` is vscode-coupled, so coverage is host E2E, matching every existing F12 test).
## Global Constraints
- INV-5: "Reject restores the retained original exactly" — the contract this fix enforces; `revertInPlace`'s doc comment already promises "reverts the whole block regardless of any in-place edits the human made".
- INV-11: anchors are immutable for a proposal's life; stale text is flagged, **never applied by guess** — the fallback must only use a span whose tracking is provably continuous (no fuzzy resolve, no boundary-straddled ranges).
- INV-12: accept/reject stay human-only gestures; no new mutation paths.
- Keep `✓ Keep` (`finalizeInPlace`) behavior byte-identical — its lookup-by-id bypass of resolution is correct by design (issue "Contrast" note).
- Existing test suites must stay green: 265+ unit, full host E2E (notably `proposals.test.ts` INV-11 stale-accept, `f12InlineDiff.test.ts`, `fullLoop.test.ts`).
- Wiggleverse hygiene: no inline trailing comments in CLI blocks; commits cite the issue and carry the `Co-Authored-By` trailer.
---
### Task 1: `appliedSpans` bookkeeping + `revertInPlace` tracked-range fallback (the INV-5 repro)
**Files:**
- Modify: `src/proposalController.ts` (DocState ~line 35, `ensureState` ~127, `optimisticApply` ~336, `finalizeInPlace` ~417, `revertInPlace` ~434, `renderAll` ~495, `onDidChange` ~529)
- Test: `test/e2e/suite/f12InlineDiff.test.ts` (new suite appended)
**Interfaces:**
- Consumes: `shift(range, edit)` / `resolve(text, fp)` from `src/anchorer.ts` (existing); `DocState.applied: Set<string>` (existing).
- Produces: `DocState.appliedSpans: Map<string, OffsetRange>` (private bookkeeping; Tasks 23 rely on its distrust + ordering semantics); `revertInPlace(docPath, proposalId, opts?: { silent?: boolean }): Promise<boolean>` — signature gains the optional `opts` used by Task 3.
- [ ] **Step 1: Write the failing E2E test — the issue's repro sketch**
Append to `test/e2e/suite/f12InlineDiff.test.ts`:
```typescript
// #70 (INV-5): rejecting a proposal AFTER typing inside its pending range must
// still restore the retained original. The exact-substring anchor is orphaned by
// the interior tweak (INV-1/INV-11) — the revert falls back to the F12
// applied-range bookkeeping (appliedSpans), which shift-tracks the span through
// interior edits.
suite("F12 inline diff — #70 reject after interior edit (INV-5)", () => {
test("revertInPlace restores the original after a tweak inside the pending range", async () => {
const ORIGINAL = "The quick brown fox jumps.";
const { doc } = await freshDoc("docs/s70-reject-tweaked.md", `# T\n\n${ORIGINAL}\n`);
const api = await getApi();
const p = api.proposalController;
const docKey = p.keyFor(doc);
const fp = { text: ORIGINAL, before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "The rapid brown fox jumps.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await p.optimisticApply(doc, id!);
await settle();
assert.ok(doc.getText().includes("The rapid brown fox jumps."), "optimistically applied");
// Human types INSIDE the pending range → orphans the exact anchor (INV-11).
const at = doc.getText().indexOf("rapid");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(at), doc.positionAt(at + "rapid".length)), "RAPID-ish");
assert.ok(await vscode.workspace.applyEdit(we), "interior tweak applied");
await settle();
assert.strictEqual(p.listProposals(doc).find((v) => v.id === id)!.anchorStart, null, "anchor orphaned by the tweak");
// ✗ Reject: must restore the retained original EXACTLY (INV-5), not skip.
assert.ok(await p.revertInPlace(docKey, id!), "reject reports success");
await settle();
assert.strictEqual(doc.getText(), `# T\n\n${ORIGINAL}\n`, "original restored exactly (INV-5)");
assert.strictEqual(p.listProposals(doc).length, 0, "proposal cleared");
});
test("rejectByIdInPlace routes the tweaked-applied case the same way", async () => {
const ORIGINAL = "A steady closing line.";
const { doc } = await freshDoc("docs/s70-reject-route.md", `# T\n\n${ORIGINAL}\n`);
const api = await getApi();
const p = api.proposalController;
const docKey = p.keyFor(doc);
const fp = { text: ORIGINAL, before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "A wobbly closing line.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await p.optimisticApply(doc, id!);
await settle();
const at = doc.getText().indexOf("wobbly");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(at), doc.positionAt(at)), "very-");
assert.ok(await vscode.workspace.applyEdit(we), "interior insertion applied");
await settle();
assert.ok(await p.rejectByIdInPlace(docKey, id!), "gesture-level reject succeeds");
await settle();
assert.strictEqual(doc.getText(), `# T\n\n${ORIGINAL}\n`, "original restored exactly (INV-5)");
});
});
```
- [ ] **Step 2: Run the new suite to verify it fails**
```bash
npm run test:e2e 2>&1 | grep -A 3 "#70"
```
Expected: both tests FAIL — first on `doc.getText()` still holding the tweaked replacement (`RAPID-ish`) after a `true` return (the silent-skip bug), i.e. the "original restored exactly" assertion.
- [ ] **Step 3: Implement `appliedSpans` + the fallback**
In `src/proposalController.ts`:
(1) `DocState` gains the map (after `applied`):
```typescript
/** ids already optimistically applied to the buffer (so the trigger doesn't re-apply). */
applied: Set<string>;
/**
* #70 (INV-5): the F12 applied-range bookkeeping — proposal id -> the applied
* span's CURRENT buffer range. Written at optimistic-apply, re-synced whenever
* the exact anchor resolves (renderAll), shifted through interior-safe edits,
* and DELETED (distrusted) when an edit straddles a span boundary — a clamped
* range is a guess, and stale text is never applied by guess (INV-11). Lets
* Reject restore the original after the human types INSIDE the pending range
* (which orphans the exact-substring anchor).
*/
appliedSpans: Map<string, OffsetRange>;
```
(2) `ensureState` initializes it: `appliedSpans: new Map(),` after `applied: new Set(),`.
(3) `optimisticApply` records the span where it already re-anchors (after `state.applied.add(proposalId);` and the `appliedStart`/`appliedEnd` computation — move the `add` down beside it or record right after the existing lines):
```typescript
state.applied.add(proposalId);
// Re-anchor to the applied text now in the buffer (its start is unchanged; its
// end shifts by the net length delta of the replacement).
const appliedStart = resolved.start;
const appliedEnd = appliedStart + proposal.replacement.length;
state.appliedSpans.set(proposalId, { start: appliedStart, end: appliedEnd });
```
(4) `renderAll`'s resolved branch re-syncs (covers the prior-session-applied reload, where the in-memory map starts empty):
```typescript
} else {
this.recordProposal(state, proposal, resolved, true);
if (state.applied.has(proposal.id)) state.appliedSpans.set(proposal.id, resolved);
}
```
(5) `onDidChange` maintains it — interior-safe edits shift, boundary-straddling edits distrust. Replace the method body:
```typescript
private onDidChange(e: vscode.TextDocumentChangeEvent): void {
const state = this.docs.get(this.keyOf(e.document));
if (!state || (state.live.size === 0 && state.appliedSpans.size === 0)) return;
for (const change of e.contentChanges) {
const edit = { start: change.rangeOffset, end: change.rangeOffset + change.rangeLength, newLength: change.text.length };
for (const [id, range] of state.live) state.live.set(id, shift(range, edit));
// #70: an edit fully inside a tracked applied span (or fully outside it)
// keeps the span meaningful; one straddling a boundary — e.g. a whole-buffer
// external replace — makes the shifted range a clamped guess, so the span
// is distrusted (deleted) rather than reverted-to by guess (INV-11).
for (const [id, range] of state.appliedSpans) {
const outside = edit.end <= range.start || edit.start >= range.end;
const inside = edit.start >= range.start && edit.end <= range.end;
if (outside || inside) state.appliedSpans.set(id, shift(range, edit));
else state.appliedSpans.delete(id);
}
}
}
```
(6) `revertInPlace` — the fix itself. Replace the method (keep its position; doc comment updated to describe the layered lookup; `opts` is consumed by Task 3):
```typescript
/**
* F12/#64 (INV-51): REJECT an optimistically-applied proposal — replace its live
* applied span with the stored `original`, then clear it. Reverts the whole block
* regardless of any in-place edits the human made to the inserted text: when an
* interior tweak has orphaned the exact anchor (INV-11), the revert falls back to
* the shift-tracked applied span (#70, INV-5). When neither locates the span
* (e.g. a boundary-straddling external rewrite distrusted it), the reject FAILS
* loudly — warning shown, proposal kept, `false` returned — instead of silently
* leaving the proposed text in the buffer. Undo (or ✓ Keep) remains the way out.
*/
async revertInPlace(docPath: string, proposalId: string, opts?: { silent?: boolean }): Promise<boolean> {
const hit = this.byId(docPath, proposalId);
if (!hit) return false;
const document = this.openDoc(hit.state);
if (!document) return false;
// Never optimistically applied → nothing of ours is in the buffer; clearing
// the record IS the reject (the legacy pending-only path).
if (hit.proposal.original === undefined) {
this.store.update(docPath, (a) => removeProposal(a, proposalId));
hit.state.applied.delete(proposalId);
hit.state.appliedSpans.delete(proposalId);
this.renderAll(document);
return true;
}
const fp = hit.state.artifact.anchors[hit.proposal.anchorId]?.fingerprint;
const resolved = fp ? resolve(document.getText(), fp) : "orphaned";
const span = resolved !== "orphaned" ? resolved : hit.state.appliedSpans.get(proposalId);
if (!span) {
if (!opts?.silent) {
void vscode.window.showWarningMessage(
"Cowriting: this proposal's applied text can't be located (it changed or moved) — undo your edits, or Keep it to leave the buffer as-is (it is never reverted by guess).",
);
}
return false;
}
const we = new vscode.WorkspaceEdit();
we.replace(
document.uri,
new vscode.Range(document.positionAt(span.start), document.positionAt(span.end)),
hit.proposal.original,
);
if (!(await vscode.workspace.applyEdit(we))) return false;
this.store.update(docPath, (a) => removeProposal(a, proposalId));
hit.state.applied.delete(proposalId);
hit.state.appliedSpans.delete(proposalId);
this.renderAll(document);
return true;
}
```
(7) `finalizeInPlace` cleans the map beside its existing `applied.delete`:
```typescript
hit.state.applied.delete(proposalId);
hit.state.appliedSpans.delete(proposalId);
```
- [ ] **Step 4: Typecheck + unit tests + the new E2E**
```bash
npm run typecheck && npm test
npm run test:e2e
```
Expected: typecheck clean; all unit tests PASS (no unit test touches `ProposalController`); host E2E PASS including the two new #70 tests and all pre-existing F12/proposals/fullLoop suites.
- [ ] **Step 5: Commit**
```bash
git add src/proposalController.ts test/e2e/suite/f12InlineDiff.test.ts
git commit -m "fix(#70): reject after an interior tweak restores the original via tracked applied span (INV-5)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>"
```
---
### Task 2: The honest hard-failure path (fix direction (b) for the residual orphan)
**Files:**
- Modify: `src/proposalController.ts` (only if Step 2 exposes a gap — the Task 1 code already implements the path)
- Test: `test/e2e/suite/f12InlineDiff.test.ts` (extend the #70 suite)
**Interfaces:**
- Consumes: Task 1's `appliedSpans` distrust semantics and `revertInPlace` failure contract.
- Produces: nothing new — locks the contract with a regression test.
- [ ] **Step 1: Write the failing/locking E2E test**
Append inside the `#70` suite from Task 1:
> **Execution note:** the first cut of this test used a whole-buffer rewrite as the
> distrusting edit and FAILED — VS Code minimizes workspace edits before change
> events fire, so a rewrite sharing a prefix/suffix with the old text decomposes
> into interior hunks the span legitimately survives (and the revert then correctly
> restores the original in place). The distrusting edit must straddle a span
> boundary even after minimization — a deletion from outside the span into its
> interior, as below.
```typescript
// Fix direction (b): when the tracked span itself is distrusted (an edit
// straddled its boundary — here a deletion running from the heading into the
// span's interior), the reject FAILS honestly: warning path, proposal kept,
// buffer untouched. Silent-skip-and-report-success (the #70 bug) must not come
// back; reverting at a clamped guessed offset (INV-11) must not either.
// (A boundary-straddling DELETION is used because VS Code minimizes workspace
// edits before change events fire — a wholesale rewrite sharing a prefix/suffix
// decomposes into interior hunks the span legitimately survives.)
test("reject fails loudly — proposal kept, buffer untouched — when the span is distrusted", async () => {
const ORIGINAL = "Sentence one stands here.";
const { doc } = await freshDoc("docs/s70-reject-distrust.md", `# T\n\n${ORIGINAL}\n`);
const api = await getApi();
const p = api.proposalController;
const docKey = p.keyFor(doc);
const fp = { text: ORIGINAL, before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "Sentence ONE stands here.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await p.optimisticApply(doc, id!);
await settle();
// Delete from inside the heading through the middle of the applied span:
// the edit straddles the span's start boundary → the tracked span is
// distrusted AND the exact anchor no longer resolves.
const mid = doc.getText().indexOf("ONE");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(2), doc.positionAt(mid + 1)), "");
assert.ok(await vscode.workspace.applyEdit(we), "boundary-straddling deletion applied");
await settle();
const before = doc.getText();
assert.strictEqual(await p.revertInPlace(docKey, id!), false, "reject reports failure (no silent success)");
await settle();
assert.strictEqual(doc.getText(), before, "buffer untouched — never reverted by guess (INV-11)");
assert.ok(p.listProposals(doc).some((v) => v.id === id), "proposal kept (not silently dropped)");
// Cleanup for later tests: the never-locatable husk is discarded via the
// plain record-only reject.
assert.strictEqual(p.rejectById(docKey, id!), true);
});
```
- [ ] **Step 2: Run it**
```bash
npm run test:e2e 2>&1 | grep -B 2 -A 5 "distrusted"
```
Expected: PASS if Task 1's implementation is complete (this is the locking regression); if it FAILS, the failure pinpoints which leg (distrust bookkeeping vs. failure return vs. proposal retention) is wrong — fix `src/proposalController.ts` accordingly, not the test.
- [ ] **Step 3: Commit**
```bash
git add test/e2e/suite/f12InlineDiff.test.ts src/proposalController.ts
git commit -m "test(#70): lock the honest hard-failure reject path (distrusted span → warn, keep, false)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>"
```
---
### Task 3: `rejectAll` parity — tweaked proposals revert too, skips are reported
**Files:**
- Modify: `src/proposalController.ts` (`rejectAll` ~line 461), `src/extension.ts` (`cowriting.rejectAllProposals` handler ~line 220)
- Test: `test/e2e/suite/f12InlineDiff.test.ts` (extend the #70 suite)
**Interfaces:**
- Consumes: Task 1's `revertInPlace(docPath, id, opts?: { silent?: boolean })` and `appliedSpans`.
- Produces: `rejectAll(document): Promise<{ reverted: number; skipped: number }>` — additive `skipped` field (existing `{ reverted }` destructurings stay valid).
- [ ] **Step 1: Write the failing E2E test**
Append inside the `#70` suite:
```typescript
// rejectAll must revert a tweaked (orphaned-anchor) proposal via the same
// tracked-span fallback, ordered descending by that span so earlier reverts
// never shift later ones — and must count what it could not revert.
test("rejectAll reverts tweaked proposals via the tracked span and reports skips", async () => {
const { doc } = await freshDoc("docs/s70-rejall-tweak.md", "# T\n\nOne aaa.\n\nTwo bbb.\n");
const api = await getApi();
const p = api.proposalController;
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({ replacement: "# T\n\nOne AAA.\n\nTwo BBB.\n", model: "m", sessionId: "s" }));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "up");
await settle();
for (const id of ids) await p.optimisticApply(doc, id);
await settle();
// Tweak INSIDE the first applied block → its exact anchor orphans.
const at = doc.getText().indexOf("AAA");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(at), doc.positionAt(at + 3)), "AAAZ");
assert.ok(await vscode.workspace.applyEdit(we), "interior tweak applied");
await settle();
const { reverted, skipped } = await p.rejectAll(doc);
await settle();
assert.strictEqual(reverted, ids.length, "ALL proposals reverted, tweaked one included");
assert.strictEqual(skipped, 0, "nothing skipped");
assert.strictEqual(doc.getText(), "# T\n\nOne aaa.\n\nTwo bbb.\n", "document fully restored (INV-5)");
assert.strictEqual(p.listProposals(doc).length, 0, "all cleared");
});
```
- [ ] **Step 2: Run it to verify the current gap**
```bash
npm run test:e2e 2>&1 | grep -B 2 -A 5 "rejectAll reverts tweaked"
```
Expected: FAIL — today the tweaked proposal sorts as an orphan (`start: -1`, reverted last), and after the first successful revert's `renderAll` the old code silently removes it without restoring (`document fully restored` assertion fails), and `skipped` does not exist on the return type (typecheck catches first — that is the same failure).
- [ ] **Step 3: Implement — order by the fallback span, count skips, report them**
Replace `rejectAll` in `src/proposalController.ts`:
```typescript
/**
* F12/#64 (INV-53): reject EVERY pending proposal on a document — revert each in
* DESCENDING span order (so an earlier revert never shifts a later one's
* offsets), symmetric with #46's accept-all. #70: a tweaked proposal whose exact
* anchor is orphaned orders (and reverts) by its shift-tracked applied span;
* one with no locatable span is SKIPPED — counted, never guessed (INV-11) —
* and the per-proposal warning is suppressed in favor of the batch report.
*/
async rejectAll(document: vscode.TextDocument): Promise<{ reverted: number; skipped: number }> {
if (!this.isTracked(document)) return { reverted: 0, skipped: 0 };
const docPath = this.keyOf(document);
const state = this.ensureState(document);
state.artifact = this.store.load(docPath) ?? emptyArtifact(docPath);
const text = document.getText();
const ordered = state.artifact.proposals
.map((p) => {
const fp = state.artifact.anchors[p.anchorId]?.fingerprint;
const r = fp ? resolve(text, fp) : "orphaned";
const start = r !== "orphaned" ? r.start : state.appliedSpans.get(p.id)?.start ?? -1;
return { id: p.id, start };
})
.sort((a, b) => b.start - a.start);
let reverted = 0;
let skipped = 0;
for (const it of ordered) {
if (await this.revertInPlace(docPath, it.id, { silent: true })) reverted++;
else skipped++;
}
return { reverted, skipped };
}
```
And in `src/extension.ts`, the `cowriting.rejectAllProposals` handler mirrors accept-all's skip note:
```typescript
const { reverted, skipped } = await proposalController.rejectAll(doc);
if (reverted === 0 && skipped === 0) return;
const skipNote = skipped > 0 ? `, ${skipped} skipped (applied text not locatable — undo your edits or Keep)` : "";
void vscode.window.showInformationMessage(
`Cowriting: rejected ${reverted} proposal${reverted === 1 ? "" : "s"}${skipNote}.`,
);
```
- [ ] **Step 4: Typecheck + full test run**
```bash
npm run typecheck && npm test
npm run test:e2e
```
Expected: all PASS — including the pre-existing `rejectAll` E2E tests (`f12InlineDiff` "rejectAll reverts every pending proposal", "control parity", `proposals.test.ts` cleanup calls), whose `{ reverted }` destructuring is unaffected by the additive field.
- [ ] **Step 5: Commit**
```bash
git add src/proposalController.ts src/extension.ts test/e2e/suite/f12InlineDiff.test.ts
git commit -m "fix(#70): rejectAll reverts tweaked proposals via the tracked span; skips are counted and reported
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>"
```
---
### Task 4: Close the loop — fullLoop E2E rejects a tweaked proposal (the deliberate avoidance falls)
**Files:**
- Modify: `test/e2e/suite/fullLoop.test.ts` (the PUC-2 reject section, ~line 144)
**Interfaces:**
- Consumes: the shipped Task 1 behavior; existing fullLoop fixtures (`rejectId`, `REJECT_REPLACEMENT`, `ORIG_REJECT`).
- Produces: nothing — upgrades the flagship E2E so the gap can't silently regress.
- [ ] **Step 1: Tweak inside the reject proposal's range before rejecting**
In `test/e2e/suite/fullLoop.test.ts`, immediately before the existing line
`assert.ok(await api.proposalController.revertInPlace(docKey, rejectId), "reject reverts in place");`,
insert:
```typescript
// #70 (INV-5): the reject leg now ALSO takes an interior tweak first — the
// original deliberately rejected only an un-tweaked proposal because the
// pre-fix revert silently skipped an orphaned anchor. The tweak orphans the
// exact anchor; the revert must restore ORIG_REJECT via the tracked span.
const rejAt = doc.getText().indexOf(REJECT_REPLACEMENT);
assert.ok(rejAt >= 0, "reject proposal's applied text present");
const rejTweak = new vscode.WorkspaceEdit();
rejTweak.replace(doc.uri, new vscode.Range(doc.positionAt(rejAt + 2), doc.positionAt(rejAt + 2)), "x");
assert.ok(await vscode.workspace.applyEdit(rejTweak), "human tweak inside the reject proposal's range");
await settle();
assert.strictEqual(
api.proposalController.listProposals(doc).find((v) => v.id === rejectId)!.anchorStart,
null,
"tweak orphans the reject proposal's exact anchor (INV-11)",
);
```
(The existing assertions right after — proposal cleared, `ORIG_REJECT` restored, `REJECT_REPLACEMENT` gone — now verify the #70 path. If `REJECT_REPLACEMENT` is a multi-word string whose slice at `+2` splits a word the later `!doc.getText().includes(REJECT_REPLACEMENT)` assertion still holds; the `includes(ORIG_REJECT)` restore assertion is the INV-5 check.)
- [ ] **Step 2: Run the fullLoop suite**
```bash
npm run test:e2e 2>&1 | grep -B 2 -A 8 "fullLoop\|full-loop\|full loop"
```
Expected: PASS end-to-end (would FAIL on unfixed code: the buffer would keep the tweaked `REJECT_REPLACEMENT`, so `includes(ORIG_REJECT)` fails).
- [ ] **Step 3: Full verification sweep**
```bash
npm run typecheck && npm test && npm run test:e2e
```
Expected: everything green.
- [ ] **Step 4: Commit**
```bash
git add test/e2e/suite/fullLoop.test.ts
git commit -m "test(#70): fullLoop reject leg now tweaks inside the pending range first (INV-5 end-to-end)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>"
```
---
## Post-execution review wave (not in the original tasks)
A high-effort multi-agent branch review after Task 4 confirmed 10 findings; all
but one were fixed in a follow-up commit on this branch ("harden the
tracked-span revert per branch review"): pure `shiftTracked` in `anchorer.ts`
(+11 unit tests; insertion-at-span-start lands before the span), tracked spans
cleared on document close, rebuild-only resync at `renderAll`, `guard.isReadOnly`
on `revertInPlace`/`finalizeInPlace`, a "Discard proposal (leave text)" action on
the hard-fail warning, CodeLens pair anchored at the tracked span (+1 E2E),
QuickPick batch menu routed through the reporting commands, and one
`clearProposal` helper (also fixes `reject()`'s stale `applied`/`appliedSpans`).
The remaining finding — `resolve()` accepts a single exact occurrence without a
context check, so a duplicated block can hijack any anchor consumer — pre-dates
this fix and is filed as its own issue.
## Self-review notes
- **Issue coverage:** direction (a) → Task 1 (`appliedSpans` fallback); direction (b) → Task 2 (hard failure, warn, keep); direction (c) explicitly rejected (guessing violates INV-11). The issue's repro sketch is Task 1's test verbatim; the "full-loop E2E deliberately rejects only an un-tweaked proposal" note is retired by Task 4; `rejectAll` (same `revertInPlace` seam) is covered by Task 3.
- **Type consistency:** `revertInPlace(docPath, proposalId, opts?: { silent?: boolean })` defined in Task 1, consumed in Task 3; `rejectAll` returns `{ reverted, skipped }` (additive); `appliedSpans: Map<string, OffsetRange>` used in Tasks 1 and 3.
- **Contrast guard:** `finalizeInPlace` untouched except symmetric `appliedSpans.delete` cleanup — the Keep bypass stays.
@@ -0,0 +1,342 @@
# #71 Surface Polish Batch Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Land the six batched Minor findings from the native-surfaces migration's final whole-branch review (issue #71) — misleading `pin()` warning, orphaned command declarations, a wrong when-clause key, README F3/banner drift, an ungated palette entry, and an uncleared debounce timer.
**Architecture:** Six independent point fixes across `src/diffViewController.ts`, `src/gitBaseline.ts`, `package.json`, and `README.md`. No new modules, no model/persistence changes, no new invariants. Verification is the existing suites (all six items are copy/declaration/cleanup changes with no unit-testable pure surface — the unit suite is vscode-free and these all live at the vscode boundary; E2E cannot observe warning-message text or palette visibility).
**Tech Stack:** VS Code extension (TypeScript, esbuild CJS bundle), vitest unit suite (`npm test`), `@vscode/test-electron` host E2E (`npm run test:e2e`).
## Global Constraints
- Work happens on branch `worktree-s71-surface-polish` in the worktree at `.claude/worktrees/s71-surface-polish` (canonical checkout is occupied by concurrent session 0062 — never touch it).
- Commits cite #71, read like surrounding history, and carry the `Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>` trailer.
- Warning copy for the not-coediting case is exactly `"Run ✦ Coedit this Document with Claude first."` — the string `src/threadController.ts:181` already uses. Do not coin a variant.
- No inline trailing comments on shell commands (wgl `no-inline-cli-comments`).
- Final gate before PR: `npm test` (unit), `npm run typecheck`, `npm run build`, `npm run test:e2e` all green.
---
### Task 1: `pin()` explicit not-coediting branch (item 1)
**Files:**
- Modify: `src/diffViewController.ts:145-155`
**Interfaces:**
- Consumes: `this.modes: Map<string, "head" | "snapshot">` (set only by `establish()`).
- Produces: nothing new — same `pin(document): void` signature; only the warning copy forks.
`pin()` currently emits "this document is git-tracked — commit to advance the baseline" for **any** non-snapshot mode, including `undefined` (a document that never established, i.e. is not being coedited) — a wrong message for that case.
- [ ] **Step 1: Fork the guard on `undefined`**
Replace lines 145155 (the whole `pin` method) with:
```ts
pin(document: vscode.TextDocument): void {
if (!this.isDiffable(document)) return;
const key = this.uriKey(document);
const mode = this.modes.get(key);
if (mode === undefined) {
// never established — the document is not being coedited (#71 item 1)
void vscode.window.showWarningMessage("Run ✦ Coedit this Document with Claude first.");
return;
}
if (mode !== "snapshot") {
void vscode.window.showWarningMessage(
"Cowriting: this document is git-tracked — commit to advance the baseline.",
);
return;
}
this.capture(document, "pinned");
}
```
Keep the existing doc comment above the method unchanged.
- [ ] **Step 2: Verify**
Run: `npm run typecheck`
Expected: clean exit.
- [ ] **Step 3: Commit**
```bash
git add src/diffViewController.ts
git commit -m "fix: markReviewed on a never-established doc says 'coedit first', not 'git-tracked' (#71 item 1)"
```
---
### Task 2: delete orphaned `acceptProposal` / `rejectProposal` declarations (item 2)
**Files:**
- Modify: `package.json` (two `contributes.commands` entries ~85-93, two `contributes.menus.commandPalette` entries ~180-187)
**Interfaces:** none — pure declaration removal. `test/e2e/suite-no-workspace/noWorkspace.test.ts:42-43` already asserts these commands are **not registered** at runtime; nothing registers them in `src/`.
- [ ] **Step 1: Delete the two command declarations**
In `contributes.commands`, delete both objects:
```json
{
"command": "cowriting.acceptProposal",
"title": "✓ Accept Proposal",
"category": "Cowriting"
},
{
"command": "cowriting.rejectProposal",
"title": "✗ Reject Proposal",
"category": "Cowriting"
},
```
- [ ] **Step 2: Delete their palette-hiding menu entries**
In `contributes.menus.commandPalette`, delete both objects (they exist only to hide the now-deleted declarations):
```json
{
"command": "cowriting.acceptProposal",
"when": "false"
},
{
"command": "cowriting.rejectProposal",
"when": "false"
},
```
- [ ] **Step 3: Verify no dangling references**
Run: `grep -rn "acceptProposal\|rejectProposal" package.json src/`
Expected: no matches (the only remaining references are the retirement assertions in `test/e2e/suite-no-workspace/noWorkspace.test.ts`).
Run: `node -e "JSON.parse(require('fs').readFileSync('package.json','utf8')); console.log('valid json')"`
Expected: `valid json`
- [ ] **Step 4: Commit**
```bash
git add package.json
git commit -m "chore: delete orphaned acceptProposal/rejectProposal declarations (#71 item 2)"
```
---
### Task 3: `openReviewPreview` editor/title when-key (item 3)
**Files:**
- Modify: `package.json:260` (the `editor/title` menu entry for `cowriting.openReviewPreview`)
**Interfaces:** none — one-token when-clause fix. Title menus key off the resource, so `resourceLangId` is correct (every sibling `editor/title` entry already uses it); `editorLangId` is the odd one out.
- [ ] **Step 1: Fix the key**
In `contributes.menus."editor/title"`, change:
```json
{
"command": "cowriting.openReviewPreview",
"when": "editorLangId == markdown",
"group": "navigation@9"
}
```
to:
```json
{
"command": "cowriting.openReviewPreview",
"when": "resourceLangId == markdown",
"group": "navigation@9"
}
```
(Only the `editor/title` entry. The `commandPalette` entries elsewhere legitimately use `editorLangId`; the `editor/title/context` and `explorer/context` entries already use `resourceLangId`.)
- [ ] **Step 2: Commit**
```bash
git add package.json
git commit -m "fix: openReviewPreview title-menu when uses resourceLangId like its siblings (#71 item 3)"
```
---
### Task 4: gate the `cowriting.createThread` palette entry (item 5)
**Files:**
- Modify: `package.json` (`contributes.menus.commandPalette`)
**Interfaces:** none — `createThreadOnSelection` (`src/threadController.ts:197-201`) already returns `undefined` silently without a selection / coediting / authorable scheme; the palette entry is currently unconditionally visible, so the command silently no-ops from the palette. Fix = gate the palette entry with the **same when-clause its `editor/context` entry uses** (`package.json:290-293`), so the palette only offers it when it can act — consistent with how `markReviewed`'s palette gate was tightened at merge.
- [ ] **Step 1: Add the palette gate**
In `contributes.menus.commandPalette`, insert after the `cowriting.proposeAgentEdit` entry:
```json
{
"command": "cowriting.createThread",
"when": "editorHasSelection && (resourceScheme == file || resourceScheme == untitled) && cowriting.isCoediting"
},
```
- [ ] **Step 2: Verify JSON**
Run: `node -e "JSON.parse(require('fs').readFileSync('package.json','utf8')); console.log('valid json')"`
Expected: `valid json`
- [ ] **Step 3: Commit**
```bash
git add package.json
git commit -m "fix: gate createThread palette entry on selection+coediting like its context-menu entry (#71 item 5)"
```
---
### Task 5: clear the `gitBaseline` reflog debounce timer in `dispose()` (item 6)
**Files:**
- Modify: `src/gitBaseline.ts:113-126` (`watchReflog`)
**Interfaces:** none — same disposable contract; the pushed disposable additionally clears the pending debounce timeout so no one-shot `repo.status()` fires post-dispose.
- [ ] **Step 1: Clear the timer in the pushed disposable**
In `watchReflog`, change:
```ts
const watcher = fs.watch(reflog, () => {
if (pending) clearTimeout(pending);
pending = setTimeout(() => void repo.status().catch(() => {}), 150);
});
this.disposables.push({ dispose: () => watcher.close() });
```
to:
```ts
const watcher = fs.watch(reflog, () => {
if (pending) clearTimeout(pending);
pending = setTimeout(() => void repo.status().catch(() => {}), 150);
});
this.disposables.push({
dispose: () => {
watcher.close();
if (pending) clearTimeout(pending);
},
});
```
- [ ] **Step 2: Verify**
Run: `npm run typecheck`
Expected: clean exit.
- [ ] **Step 3: Commit**
```bash
git add src/gitBaseline.ts
git commit -m "fix: clear reflog debounce timer on GitBaselineWatcher dispose (#71 item 6)"
```
---
### Task 6: README F3 drift + stale chord notes in superseded banners (item 4)
**Files:**
- Modify: `README.md` (F3 section ~106, F7 banner ~230-233, F10 banner ~313-317)
**Interfaces:** none — docs only. Current reality the banner must state: `Cowriting: Toggle Attribution` no longer exists (annotations toggle via **Toggle Annotations**, How it works §6); "Ask Claude to Edit Selection" is palette-hidden (`package.json:201-203``"when": "false"`), superseded by **Ask Claude to Edit** (`Ctrl+Alt+E` / `Cmd+Alt+E`) and the comments-first **Ask Claude** (§3). `Ctrl+Alt+R` today is **Review Changes** (native diff), not any preview.
- [ ] **Step 1: Add the F3 banner**
Immediately after the `## F3 — Live human/Claude attribution (Feature #6)` heading (before the "As you and Claude coauthor…" paragraph), insert:
```markdown
> **Commands superseded (native-surfaces migration).** The attribution **data
> layer** below is current, but the Commands block is historical:
> `Cowriting: Toggle Attribution` was retired — authorship/change coloring now
> toggles via **Toggle Annotations** on a coedited document (How it works §6) —
> and "Ask Claude to Edit Selection" is palette-hidden; use **Ask Claude to
> Edit** (`Ctrl+Alt+E` / `Cmd+Alt+E`) or the comments-first **Ask Claude** (How
> it works §3). Kept below as a historical record.
```
- [ ] **Step 2: Chord note in the F7 banner**
Extend the F7 superseded banner's last sentence. Change:
```markdown
> **Superseded (Task 8, native-surfaces migration).** The bespoke webview this
> section describes was deleted; its authorship/change coloring lives on
> **inside VS Code's own built-in Markdown preview** — see **How it works** §6
> above. Kept below as a historical record of the pre-migration design.
```
to:
```markdown
> **Superseded (Task 8, native-surfaces migration).** The bespoke webview this
> section describes was deleted; its authorship/change coloring lives on
> **inside VS Code's own built-in Markdown preview** — see **How it works** §6
> above. Kept below as a historical record of the pre-migration design. (The
> `Ctrl+Alt+R` chord below now opens the native **Review Changes** diff; the
> annotated preview opens via **Open Cowriting Review Preview**, no chord.)
```
- [ ] **Step 3: Chord note in the F10 banner**
Change the F10 banner's last sentence from:
```markdown
> **How it works** above. Kept below as a historical record.
```
to:
```markdown
> **How it works** above. Kept below as a historical record. (The `Ctrl+Alt+R`
> chord below now opens the native **Review Changes** diff, not this panel.)
```
- [ ] **Step 4: Commit**
```bash
git add README.md
git commit -m "docs: F3 superseded-commands banner + stale-chord notes in F7/F10 banners (#71 item 4)"
```
---
### Task 7: full verification + PR + merge
**Files:** none new.
- [ ] **Step 1: Full local gate**
Run each; all must be green:
```bash
npm test
npm run typecheck
npm run build
npm run test:e2e
```
Expected: unit suite passes (~250 tests), typecheck clean, build succeeds, host E2E passes (undo suites may self-skip via the #54 runtime probe — a loud warning, not a failure).
- [ ] **Step 2: Push branch + open PR**
```bash
git push -u origin worktree-s71-surface-polish
```
Open a PR against `main` on `benstull/vscode-cowriting-plugin` (Gitea, SSH remote) titled `Surface polish: batched Minor findings from the native-surfaces final review (#71)`; body lists the six items and cites the issue.
- [ ] **Step 3: Merge (squash) and confirm #71 auto-close/close**
Merge the PR; verify `main` carries the change and close #71 if the merge didn't.
@@ -0,0 +1,602 @@
---
status: graduated
---
# Solution Design: Preview Toolbar as the Primary Interaction Surface (F11)
| | |
| --- | --- |
| **Author(s)** | Ben Stull (with Claude) |
| **Reviewers / approvers** | Ben Stull |
| **Status** | `draft` |
| **Version** | v0.1.0 |
| **Source artifacts** | Feature `benstull/vscode-cowriting-plugin#43` (F11, `type/feature`, `priority/P1`) · Epic `#1` (closed) · Capture session `vscode-cowriting-plugin-0035` (2026-06-12) · Brainstorming session `vscode-cowriting-plugin-0036` · Builds on (all shipped): F3 `#6` (live attribution), F4 `#12` (propose/accept), F6 `#17`/`#19` (baseline + diff view), F7 `#21`/`#22` (rendered preview), F9 `#27` (authorship preview), F10 `#29` (interactive review preview), F10-followups `#31` (inline-at-anchor proposals) · Coexists with `#41` (right-click → Open Review Panel) and `#42` (right-click → Ask Claude to Edit), both blocked-by this · Parent specs (graduated): `coauthoring-inner-loop.md`, `coauthoring-attribution.md`, `coauthoring-propose-accept.md`, `coauthoring-diff-view.md`, `coauthoring-rendered-preview.md`, `coauthoring-interactive-review.md` · Lineage: `ben.stull/rfc-app#48` |
**Change log**
| Date | Version | Change | By |
| --- | --- | --- | --- |
| 2026-06-12 | v0.1.0 | Initial draft — brainstorming session 0036 (from the capture in session 0035). Three forks locked with the operator: block-level preview-selection→source mapping; document edit diffed into per-hunk F4 proposals; #43 lands a minimal right-click→open-preview gateway. | Ben Stull + Claude |
---
## 1. Business Context
### 1.1 Executive Summary
F10 made the rendered preview the **single review surface** — clean editor on the
left, annotated review on the right, with an annotations on/off toggle and
inline ✓/✗ on Claude's pending proposals. But the writer still cannot *act* from
the preview beyond accepting/rejecting proposals: to **ask Claude to edit**, they
jump back to the editor and use a selection-gated context-menu item; to **pin a
fresh review baseline** they have *no* reachable control at all (the
`cowriting.pinDiffBaseline` command is registered but `when:false`, orphaned
since `#34` removed its two-pane host); and whole-document editing doesn't exist.
F11 makes the **preview toolbar the primary interaction surface**. Beside the
existing annotations checkbox — the one control the writer already loves — the
toolbar gains a **Pin baseline** button and a **single adaptive "Ask Claude…"
button** that reads *Edit Selection* when text is selected in the preview and
*Edit Document* when nothing is. The writer reads, asks Claude to edit, and
resets the baseline all in one place, mouse-first, without leaving the rendered
document. A minimal right-click entry opens the preview, making it the surface
the `#41`/`#42` gateways will lead into.
### 1.2 Background
The inner loop shipped F2F5 (threads · attribution · propose/accept ·
cross-rung). F6 added the baseline + a native diff toggle; F7 the rendered
track-changes preview; F9 an authorship mode; F10 (`#29`) collapsed those into the
**single interactive review preview** (clean editor; annotations on/off; ✓/✗ on
F4 proposals surfaced in the rendered view); `#31` then placed proposals
**inline at their resolved anchor** in that preview.
Capture session 0035 filed `#43` (this feature) plus `#41` (right-click → Open
Review Panel) and `#42` (right-click → Ask Claude to Edit). The operator's ask:
*"Can we set it up so all interactions — Ask Claude to Edit Selection / Edit
Document, annotations off/on, Pin new baseline — are via the preview window? I
like the annotations checkbox up there; make the others buttons, with one 'Ask
Claude…' button that changes depending on whether some of the markdown preview is
selected."* That session also surfaced the stranded `pinDiffBaseline` command
(`when:false` since `#34`). This spec is the Solution Design for `#43`.
### 1.3 Business Actors / Roles
- **Coauthor (human)** — the markdown writer/engineer (PP-1); F11's sole user.
- **Coauthor (machine)** — Claude via `@cline/sdk`; not a user of F11, but the
target of the toolbar's "Ask Claude…" gesture and the author of the proposals
that result.
### 1.4 Problem Statement
The plugin's interactions are scattered across surfaces and inconsistently
reachable. "Ask Claude to Edit Selection" is only a selection-gated **editor**
context-menu item; **whole-document editing doesn't exist**; and **Pin Review
Baseline** is **unreachable from any UI**. The one control the writer loves — the
annotations on/off checkbox in the preview — proves the toolbar is the natural
home for these gestures, but it stands alone. A writer reviewing in the preview
has to leave it and hunt through editor menus / the palette to act.
### 1.5 Pain Points
- **No edit gesture in the preview** — to ask Claude to change anything, the
writer leaves the review surface for the editor's right-click menu.
- **Whole-document editing is missing** — there is no "edit the whole document"
path at all; only a selection-scoped editor command exists.
- **Pin baseline is stranded** — the command exists but no menu, keybinding, or
palette entry reaches it (`when:false` since `#34`).
- **Mouse-first review is broken mid-flow** — the preview is mouse-driven, but
acting forces a context-switch to keyboard/menus elsewhere.
### 1.6 Targeted Business Outcomes
The preview becomes a **self-contained cockpit** for the inner loop. From its
toolbar a writer can toggle annotations (today), **ask Claude to edit** (selection
or whole document, via one button that adapts to what's selected), and **pin a
fresh baseline** — no context-switching to the editor or command palette. The
interaction model consolidates around the surface the writer already prefers, and
the stranded pin command gets a real home.
### 1.7 Scope (business)
**In scope:** preview-webview toolbar controls — a **Pin baseline** button and a
**single adaptive "Ask Claude…" button** (Edit Selection ⇆ Edit Document) — beside
the existing annotations checkbox; wiring those controls to the **existing** F4
edit seam, F3 attribution, and F6 baseline command; **block-level**
preview-selection → source-range mapping (the central design risk); a **new
whole-document edit path** whose result is **diffed into per-hunk F4 proposals**;
a **minimal right-click → Open Review Preview** entry so the surface is reachable
end-to-end; resolving the pin-baseline reachability gap; unit + host-E2E coverage;
manual webview smoke.
**Out of scope (deferred, not forgotten):** **char-precise sub-block** selection
mapping (block granularity is the locked v1 — §6.7); the **richer `#41`/`#42`
menu sets** (this feature lands only the minimal gateway; `#41`/`#42` expand it);
preview→source **scroll-sync** (`#32`); multi-file / batch editing; the Explorer
tree affordance; any export / print / copy gesture.
**Non-goals (firm):** **no new edit / attribution / proposal *model*** — F11
reuses F3 `spansFor`, the F4 `propose`/`accept` single-range model, and the F6
baseline store; no change to the **sidecar**, the **cross-rung contract**, or
`SCHEMA_VERSION`; **no document mutation from the webview** (INV-20/21/34 hold —
the sealed webview posts intent only); no LLM/network/credential surface added to
the webview (INV-8 untouched — the edit turn runs host-side as today).
### 1.8 Assumptions · Constraints · Dependencies
- **Anchor:** Feature `#43` (F11). Builds directly on shipped work: the F7/F10
rendered preview + annotations toggle + host↔webview message bus, the F3
attribution + F4 propose/accept inner loop (including `#31`'s inline-at-anchor
proposal placement), and the F6 baseline store (`cowriting.pinDiffBaseline`,
currently unreachable — this feature gives it a home).
- **Central design risk (locked):** the preview is a **rendered** sealed webview
(markdown-it HTML, strict CSP — F7 INV-21), so "Edit Selection" must map a
selection in the rendered preview back to a **source markdown range**. The
rendered HTML carries **no** source positions today; only internal block
char-offsets exist (`splitBlocksWithRanges``BlockWithRange.start/end`). The
locked approach is **block-level** mapping: the pure render layer emits
`data-src-start`/`data-src-end` on each rendered block; a selection resolves to
the union of the live-source blocks it intersects (§6.7, fork 1).
- **Constraint (sealed webview):** interactive controls post messages to the
extension host, which applies edits/pins via the existing F4 / F6 paths; the
webview **never** edits the document, sidecar, or baseline directly. The edit
**turn** (LLM call) and the **instruction prompt** run host-side, keeping the
webview free of LLM/network/credential surface.
- **Coexistence:** the native editor context-menu "Ask Claude to Edit Selection"
(`cowriting.editSelection`) stays unchanged; `#41`/`#42` add right-click
gateways *into* the preview. F11 lands only a minimal gateway (§6.7, fork 3).
- No new persisted artifact; nothing in `.threads/`, the contract, or
`SCHEMA_VERSION` changes.
### 1.9 Business Use Cases
- **BUC-1 (edit from the preview)** Reviewing in the preview, the writer selects a
paragraph in the rendered document and clicks **"Ask Claude to Edit
Selection"**; Claude's proposed change appears as a blue ✓/✗ block at that
spot — without the writer ever leaving the preview.
- **BUC-2 (edit the whole document)** With nothing selected, the writer clicks
**"Ask Claude to Edit Document"**, types an instruction, and Claude's rewrite
surfaces as **several** independently-acceptable blue proposal blocks (one per
changed hunk) inline in the preview.
- **BUC-3 (pin a fresh baseline)** After accepting a batch of changes, the writer
clicks **"Pin baseline"**; the change-marks clear and "what changed" now counts
from this moment — the stranded command finally has a button.
---
## 2. Solution Proposal
F11 is a **thin increment** on F10's preview: it adds two controls to the
existing header toolbar and routes their intent through machinery that already
exists. No new model, no new persisted state.
**The pure render layer (`trackChangesModel.ts`) learns one new thing:** a shared
helper wraps each rendered block in `<div data-src-start="N" data-src-end="M">`
using the existing `splitBlocksWithRanges` offsets. It is applied to **both**
render paths — `renderReview` (annotations on) and `renderPlain` (annotations
off) — so selection→source mapping works in either mode. The helper is pure,
vscode-free, DOM-free, and deterministic (extends INV-22/33). The render layer
gains **no** selection or DOM logic.
**The webview (`media/preview.ts` + `.css`)** header becomes:
```
[ ☑ Annotations ] [ ⌖ Pin baseline ] [ ✦ Ask Claude to Edit Document ▾ ]
```
The **Ask-Claude button morphs its own label** on `selectionchange`: a non-empty
selection inside the rendered body → **"Ask Claude to Edit Selection"**; an empty
/ collapsed selection → **"Ask Claude to Edit Document"**. On click it walks the
selection's start and end nodes up to the nearest ancestor carrying
`data-src-start`/`data-src-end` and posts the resolved offsets. That
nearest-ancestor lookup is the webview's **only** mapping duty (manual-smoke
territory); everything downstream is host-side and testable. The webview stays
**sealed** (INV-21): nonce'd inline script, no network, no document mutation.
**New webview→host messages (intent only):**
```ts
type ToolbarMsg =
| { type: "pinBaseline" }
| { type: "askClaude"; scope: "selection"; start: number; end: number }
| { type: "askClaude"; scope: "document" };
```
**The host (`trackChangesPreview.ts`)** routes each intent through the existing
seams:
- **`pinBaseline`** → pins the **previewed** document (calls
`DiffViewController.pin(document)` directly — not the `activeTextEditor`-based
command, which may not point at the previewed doc) → `onDidChangeBaseline`
re-render with cleared marks.
- **`askClaude`** → host `showInputBox` for the instruction (keeps the LLM /
secret surface out of the sealed webview), then one shared host routine
`runEditAndPropose(document, target, instruction)`:
- **selection** → `target` = the block-union range `[firstBlock.start …
lastBlock.end]`; one `runEditTurn` → one F4 `propose()` over that range (the
existing Edit-Selection shape).
- **document** → `target` = whole document; one `runEditTurn` over the full
text → **diff Claude's result against the current text → one `propose()` per
changed hunk** (multiple proposals, each its own blue ✓/✗ block). Reuses the
F4 single-range model N times; no model change.
**The right-click gateway:** `cowriting.showTrackChangesPreview` is added to the
`editor/title` menu (markdown only) so right-clicking the tab opens the preview —
the minimal entry that makes the toolbar surface reachable end-to-end and lets
`#41`/`#42` expand the menu set later.
**Reachability cleanup:** `cowriting.pinDiffBaseline` gets a real palette `when`
(`editorLangId == markdown`), resolving the orphan from the command side too; a
new `cowriting.editDocument` command is registered (document-scoped edit) so
`#42`'s gateway can reuse it.
Everything downstream of *(intent) → (existing seam)* is the existing F4/F6/F3
machinery; the only genuinely new pure code is the block-offset wrapper and the
document-rewrite hunk-diff. Both are unit-testable with no vscode and no webview.
---
## 3. Product Personas
- **PP-1 Inner-loop coauthor** — the human markdown writer/engineer (as F2F10);
the only persona F11 serves.
## 4. Product Use Cases
- **PUC-1 (toolbar present)** Opening the review preview for a markdown document
shows the header with **three** controls: the annotations on/off checkbox
(existing), a **Pin baseline** button, and an adaptive **Ask Claude…** button.
Controls are inert (disabled) for non-authorable documents.
- **PUC-2 (adaptive label)** With a non-empty selection in the rendered preview
body, the Ask-Claude button reads **"Ask Claude to Edit Selection"**; with no
selection it reads **"Ask Claude to Edit Document"**. The label flips live as
the selection changes.
- **PUC-3 (edit selection)** The writer selects rendered text, clicks **Ask
Claude to Edit Selection**, and enters an instruction. The selection resolves to
the union of the source blocks it touches; Claude proposes a change over that
range; a single blue ✓/✗ proposal block appears inline at that anchor (`#31`).
- **PUC-4 (edit document)** With nothing selected, the writer clicks **Ask Claude
to Edit Document**, enters an instruction; Claude rewrites the whole document;
the rewrite is diffed into hunks and surfaces as **N** independent blue ✓/✗
proposal blocks inline. Accepting/rejecting each is the F10 path unchanged.
- **PUC-5 (pin baseline)** The writer clicks **Pin baseline**; the previewed
document's review baseline is pinned to now; the change-marks clear and the
`Since <epoch>` label updates. (No confirmation prompt — matches the existing
command's behavior; re-pinning is the recovery.)
- **PUC-6 (right-click into the preview)** Right-clicking a markdown editor tab
shows **Open Review Preview**; choosing it opens the preview (the gateway
`#41`/`#42` will build upon).
- **PUC-7 (graceful edges)** A selection confined to a deletion (struck) or
proposal block — which carries no live-source range — falls back to **document**
scope. An empty document or a selection that resolves to no live block → the
button stays in **Edit Document** mode. A non-authorable document → toolbar edit
controls are disabled. The LLM turn failing → the existing `runEditTurn`
error handling (no proposal created); the preview is unchanged.
---
## 5. UX Layout
The F10 preview is unchanged except for its **header bar**, which now hosts three
controls in a single row:
- **☑ Annotations** — the existing on/off checkbox (kept first; the operator's
preferred control).
- **⌖ Pin baseline** — a button; pins the previewed document's review baseline to
now and clears the change-marks.
- **✦ Ask Claude to Edit Document ▾** — a single button whose label and behavior
adapt to the preview's selection state (Edit **Selection** when text is
selected, Edit **Document** otherwise). Clicking it opens a host input box for
the instruction.
Buttons are styled with theme CSS variables (light / dark / high-contrast),
matching the existing toolbar chrome; they sit in the same `#cw-toggle` header
region as the annotations checkbox. When the previewed document is not authorable
(F8 `isAuthorable`), the **Pin baseline** and **Ask Claude…** controls render
**disabled** (the annotations toggle stays active — reading is always allowed).
The rendered body is unchanged from F10/`#31`: green human additions, blue
LLM-authored text, struck deletions, and pending Claude proposals as inline blue
blocks with ✓/✗ at their resolved anchors. Proposals produced via the new toolbar
edit gestures appear exactly as proposals do today.
---
## 6. Technical Design
### 6.1 Invariants
Parent invariants INV-1..INV-34 carry over unchanged. F11 adds:
- **INV-35 (toolbar gestures route through existing seams; webview never
mutates)** The Pin baseline and Ask-Claude toolbar controls post **intent**
messages to the host; **all** mutation goes through the existing machinery — pin
via the F6 baseline store (`DiffViewController.pin`), edits via the F4
`propose` → `accept`/`applyAgentEdit` (`WorkspaceEdit`) seam with F3 attribution.
No divergent edit or baseline path is introduced. The sealed webview never
edits the document, sidecar, or baseline directly (INV-20/21/34 hold); the LLM
turn and the instruction prompt run host-side (INV-8 untouched).
- **INV-36 (block-granular preview-selection → source mapping)** The pure render
layer emits `data-src-start`/`data-src-end` (source char offsets from
`BlockWithRange`) on **every** rendered block, in **both** the on (`renderReview`)
and off (`renderPlain`) modes. A preview selection resolves to the **union of
the live-source blocks it intersects** (`[min start … max end]`); blocks with no
live-source range (deletion-only / proposal blocks) are skipped, and a selection
that resolves to no live block falls back to **document** scope. The DOM
selection → nearest-`data-src` lookup is the webview's **sole** mapping duty;
the offsets and everything downstream (fingerprint, turn, propose) are host-side
and testable. The wrapping is deterministic — same inputs → identical HTML
(extends INV-22/33).
- **INV-37 (single adaptive Ask-Claude button; scope-aware)** One toolbar button
serves both scopes. A non-empty live-source selection → **Edit Selection**: one
F4 proposal over the block-union range. An empty selection → **Edit Document**:
one `runEditTurn` over the whole document, its result **diffed into hunks**, one
F4 `propose()` per changed hunk. Both scopes call the same host
`runEditAndPropose` routine and reuse the F4 **single-range** proposal model
(the document case issues multiple single-range proposals — **no new model**).
### 6.2 High-level architecture
```mermaid
flowchart LR
wv["webview header\n☑ Annotations · ⌖ Pin · ✦ Ask Claude (adaptive)"] -- "postMessage{pinBaseline | askClaude(scope,start?,end?)}" --> ctl["trackChangesPreview\n(vscode layer)"]
ctl -- "pin(document)" --> base["F6 DiffViewController\nbaseline store (INV-18)"]
ctl -- "showInputBox → runEditAndPropose" --> turn["runEditTurn\n(host-side LLM turn)"]
turn -- "selection: 1 replacement\ndocument: rewrite" --> ctl
ctl -- "selection → 1 propose()\ndocument → diff → N propose()" --> prop["F4 ProposalController\npropose() (single-range model)"]
prop -- "onDidChangeProposals" --> ctl
base -- "onDidChangeBaseline" --> ctl
ctl -- "(baseline, current, spans, proposals)" --> model["renderReview / renderPlain\n(pure)\n+ wrapBlocksWithSrc (NEW)"]
model -- "annotated HTML w/ data-src on blocks" --> ctl
ctl -- "postMessage{render}" --> wv
```
The dashed-in NEW pieces are: `wrapBlocksWithSrc` (pure), the
`runEditAndPropose` host routine with its document-rewrite hunk-diff, the three
inbound toolbar messages, and the `editor/title` gateway menu. Everything else is
the existing F6/F4/F3/F10 machinery.
### 6.3 Data model & ownership
**No new persisted artifact** (INV-20). F11 adds only transient on-the-wire
messages (the `ToolbarMsg` union in §2) and reuses F10's `RenderMsg`. Baseline is
owned by F6, proposals by F4 (sidecar), attribution by F3 — all untouched. The
block-offset `data-src` attributes are render-time only (not stored).
### 6.4 Interfaces & contracts
- **`trackChangesModel`** (vscode-free, pure): new
`wrapBlocksWithSrc(blocks: BlockWithRange[], renderedPerBlock: string[]):
string` (illustrative) — or, more precisely, both `renderReview` and
`renderPlain` route their per-block rendered HTML through a shared internal
helper that prepends `data-src-start`/`data-src-end` to each block's wrapping
element. Plus `diffToHunks(currentText: string, rewrittenText: string):
Array<{ start: number; end: number; replacement: string }>` — the pure
document-rewrite → per-hunk proposal-range list (vscode-free, deterministic).
- **`TrackChangesPreviewController`** (vscode layer): handles the three new
inbound messages; gains a `runEditAndPropose(document, target: { kind:
"range"; start; end } | { kind: "document" }, instruction)` private routine;
takes (or reaches) the `DiffViewController` to pin the previewed doc and the
edit-turn entry. New test seams as needed (`getLastModel` already exists for
asserting marks without webview DOM).
- **`DiffViewController`** (F6): `pin(document)` is reused as-is (the controller
already exposes pinning by document); `cowriting.pinDiffBaseline`'s
`package.json` `when` flips from `false` to `editorLangId == markdown`.
- **`ProposalController`** (F4): `propose(...)` reused unchanged (called once for
selection, N times for a diffed document). No signature change.
- **`AttributionController`** (F3): unchanged (`applyAgentEdit` reused on accept).
- **Commands / menus (`package.json`):**
- `cowriting.showTrackChangesPreview` — added to `editor/title` with
`when: editorLangId == markdown` (the minimal gateway). Existing palette +
`ctrl+alt+r` kept.
- `cowriting.pinDiffBaseline` — `when` flips to `editorLangId == markdown`
(no longer orphaned).
- `cowriting.editDocument` ("Ask Claude to Edit Document", document-scoped) —
new command registered, routed through `runEditAndPropose({kind:"document"})`;
available for `#42` to reuse. (The preview's selection-scoped edit is driven
by the `askClaude` message carrying webview-resolved offsets, not a command,
since the offsets originate in the webview.)
- **Webview asset** (`media/preview.ts` + `.css`): header gains the two buttons;
a `selectionchange` listener updates the Ask-Claude label; click handlers post
the `ToolbarMsg` intents; the selection→nearest-`data-src` lookup helper. Stays
sealed (nonce'd inline script, CSP unchanged).
### 6.5 PerProduct-Use-Case design
- **PUC-1 (toolbar present):** render the two buttons in the header next to the
annotations checkbox; disable Pin + Ask-Claude when `!isAuthorable(document)`.
- **PUC-2 (adaptive label):** webview `selectionchange` → if the selection is
non-empty and within the rendered body, label = "Edit Selection"; else "Edit
Document". Pure webview-local state.
- **PUC-3 (edit selection):** webview resolves selection → `{start, end}` from the
nearest `data-src` ancestors → `postMessage{askClaude, selection, start, end}` →
host `showInputBox` → `runEditAndPropose({kind:"range", start, end})` →
`runEditTurn` → one `propose()` → `onDidChangeProposals` → re-render (inline
blue block at the anchor, `#31`).
- **PUC-4 (edit document):** `postMessage{askClaude, document}` → host input box →
`runEditAndPropose({kind:"document"})` → `runEditTurn` over full text →
`diffToHunks(current, rewritten)` → one `propose()` per hunk → re-render (N blue
blocks).
- **PUC-5 (pin baseline):** `postMessage{pinBaseline}` → `DiffViewController.pin(
previewedDocument)` → `onDidChangeBaseline` → re-render (marks cleared).
- **PUC-6 (right-click gateway):** `editor/title` entry invokes
`cowriting.showTrackChangesPreview` for the tab's document.
- **PUC-7 (edges):** selection resolving to no live block → document scope;
non-authorable → controls disabled; `runEditTurn` failure → existing error path,
no proposal; empty doc → Edit Document over empty range (no-op-safe).
### 6.6 Non-functional requirements & cross-cutting concerns
The webview stays **sealed** (INV-21): local assets, strict CSP with a per-load
nonce, no network; the new inline handlers only read `data-src`/`data-proposal-id`
and post intent (no eval, no remote, no document mutation). The instruction prompt
and the LLM turn remain **host-side** — the webview gains **no** LLM, network, or
credential surface (INV-8 untouched). `diffToHunks` and the block wrapping are
O(document), run on a host gesture (not per-keystroke), fine at inner-loop scale.
No telemetry, nothing persisted.
### 6.7 Key decisions & alternatives considered
| Decision | Chosen | Alternatives rejected |
| --- | --- | --- |
| **Preview-selection → source mapping granularity** | **Block-level** — pure layer emits `data-src-start/end` from existing `BlockWithRange`; selection → union of intersected live-source blocks. Robust, reuses what exists, ships the full adaptive button now. *(Operator decision, session 0036.)* | **Char-precise** sub-block mapping — needs per-inline-token source offsets markdown-it doesn't reliably give; rendered text ≠ source (syntax stripped) → fragile, risks the whole feature on the hardest part. **Document-only first** — defers the headline adaptive button; punts the risk. |
| **Document-edit proposal granularity** | **Diff Claude's rewrite into hunks → one F4 proposal per changed hunk** — independent ✓/✗ per change; reuses the single-range model N times (no model change). *(Operator decision, session 0036.)* | **One whole-document proposal** — a single giant blue block, all-or-nothing accept/reject; poor UX for a real rewrite. |
| **`#43` vs `#41`/`#42` scope** | **`#43` lands a minimal right-click → Open Review Preview gateway** (`editor/title`), so the toolbar surface is reachable end-to-end and its E2E is real; `#41`/`#42` expand the menu set. *(Operator decision, session 0036.)* | **Toolbar only; all menus in `#41`/`#42`** — `#43`'s "a right-click entry opens the preview" acceptance/E2E couldn't be satisfied within `#43`. |
| **Instruction prompt location** | **Host `showInputBox`** — keeps LLM/secret surface out of the sealed webview; reuses the existing edit-turn flow. | **In-webview text field** — pushes prompt handling toward the sandbox; no benefit. |
| **Pin button target** | **The previewed document** (`DiffViewController.pin(document)`) — the preview knows its bound doc. | **`activeTextEditor`-based command** — may not point at the previewed doc; the source of the orphan. |
| **Pin confirmation** | **No confirm** — matches the existing command; re-pinning recovers. | **Confirm dialog** — friction for a routine, recoverable gesture. |
### 6.8 Testing strategy
- **Unit (vitest, vscode-free):** `data-src-start/end` present and correct on every
block for **both** `renderReview` and `renderPlain` (offsets equal the
`BlockWithRange` ranges; determinism — same inputs → identical HTML);
`diffToHunks` over fixtures — a single-hunk rewrite → one range; a multi-hunk
rewrite → the expected disjoint ranges with correct replacements; an unchanged
rewrite → zero hunks; whole-document replacement → one full-range hunk.
- **Host E2E (`@vscode/test-electron`, no LLM, extends the F10 suite):** open a
markdown fixture → `cowriting.showTrackChangesPreview`. Simulate
`{type:"pinBaseline"}` → `getLastModel` shows cleared change-marks + advanced
epoch. Simulate `{type:"askClaude", scope:"selection", start, end}` with a
stubbed edit turn → exactly **one** proposal over the resolved range, anchored
inline. Simulate `{type:"askClaude", scope:"document"}` with a stubbed
multi-hunk rewrite → **N** proposals matching the hunks. Invoke the
`editor/title` gateway command → panel opens. Non-authorable document → toolbar
edit controls disabled (asserted via the model/flags the host exposes). The
webview DOM, real button clicks, the `selectionchange` label flip, and the
selection→`data-src` lookup are **not** E2E-asserted (sealed sandbox) — manual
smoke.
- **Live smoke (manual — `docs/MANUAL-SMOKE-F11.md`):** open a markdown doc; open
the review preview; confirm the three header controls; select a paragraph →
button reads "Edit Selection", click → enter instruction → a blue ✓/✗ block
appears at that paragraph; clear the selection → button reads "Edit Document",
click → instruction → several blue blocks appear; click **Pin baseline** → marks
clear, `Since` label updates; right-click the tab → **Open Review Preview** opens
the panel; verify light/dark theming and that `git status` shows nothing
unexpected.
### 6.9 Failure modes, rollback & flags
A selection that resolves to no live-source block → **Edit Document** scope (never
an error). `runEditTurn` failing → existing error handling, no proposal created,
preview unchanged. `diffToHunks` producing zero hunks (rewrite == current) → no
proposals, a brief "no changes proposed" host notice. Webview disposed mid-gesture
→ the host routine completes against the document; the next open re-renders.
**No feature flag** — the toolbar controls are additive UI; nothing persists.
Rollback is reverting the PR with **zero** data migration (nothing persisted; the
F6 baseline, F4 sidecar, F3 attribution data are untouched; the unhidden pin
command and the gateway menu simply disappear).
---
## 7. Delivery Plan
### 7.1 Approach / strategy
One planning-and-executing session (F11 = `#43`), plan written just-in-time from
this spec — the F2F10 precedent. Host-E2E tier (a VS Code extension has no
browser/deploy stage); no LLM in CI (edit turns stubbed). The webview's visual
rendering, the adaptive label, and the selection→source DOM lookup are verified by
the manual smoke; the automated seams are the pure block-wrapping + `diffToHunks`
model and the host's message→seam wiring.
### 7.2 Slicing plan
- **SLICE-1 — Pin baseline button + reachability.** Webview header **Pin
baseline** button → `{type:"pinBaseline"}` → host `DiffViewController.pin(
previewedDoc)`; unhide `cowriting.pinDiffBaseline` (`when: editorLangId ==
markdown`). Host E2E: pin message clears marks. *(Immediate win — homes the
orphaned command.)*
- **SLICE-2 — Block-offset emission.** Shared pure helper wrapping each block with
`data-src-start/end` in `renderReview` **and** `renderPlain`; vitest for both
modes + determinism. No UI yet. (INV-36 data layer.)
- **SLICE-3 — Edit Document button + hunk path.** Webview **Ask Claude to Edit
Document** button (no-selection state) → `{type:"askClaude", scope:"document"}`;
host `runEditAndPropose({document})` → `runEditTurn` → `diffToHunks` → N
`propose()`; register `cowriting.editDocument`; vitest for `diffToHunks`; host
E2E for the N-proposal path. (INV-37 document half.)
- **SLICE-4 — Adaptive Edit Selection.** Webview `selectionchange` label flip +
selection→nearest-`data-src` resolution → `{type:"askClaude", scope:"selection",
start, end}`; host single-range `propose()`. Host E2E for the selection message →
one anchored proposal. (INV-37 selection half; INV-36 consumer.)
- **SLICE-5 — Gateway, edges, tests & docs.** `editor/title` → Open Review Preview
gateway; non-authorable disabling; host E2E (gateway opens panel; controls
inert on non-authorable); `docs/MANUAL-SMOKE-F11.md`; README F11 section.
E2E are first-class plan tasks (handbook §9/§4); this app's required tier is host
E2E (the F2F10 precedent).
### 7.3 Rollout / launch plan
Non-shippable (no marketplace publish). "Done" = `#43` acceptance: the preview
toolbar hosts the annotations checkbox + a Pin baseline button + a single adaptive
Ask-Claude button (Edit Selection ⇆ Edit Document) that route through the existing
F4/F3/F6 machinery; edits surface as proposals (one for a selection, per-hunk for
a document rewrite); a right-click entry opens the preview; the pin command is no
longer orphaned; unit + host E2E green; live smoke performed once.
### 7.4 Risks & mitigations
| Risk | Mitigation |
| --- | --- |
| Block-level selection feels coarse vs the editor's char-precise Edit Selection | Locked v1 decision (§6.7); a rendered surface is naturally block-grained; char-precise is a deferred follow-up if the coarseness bites |
| `data-src` attributes perturb markdown-it output or the F10 proposal/diff rendering | Wrapping is applied at the block boundary (outside inline parsing); covered by determinism + both-mode unit tests; per-block `try/catch` error chip (F7) on render failure |
| `diffToHunks` produces awkward hunk boundaries on a large rewrite | Pure + unit-tested over fixtures; hunks are line/block-aligned; worst case is more/fewer blocks, all independently ✓/✗-able — never wrong, just granular |
| Selection inside a deletion/proposal block has no live-source range | Falls back to Document scope by design (INV-36); manual-smoke verified |
| The webview selection→`data-src` lookup isn't E2E-testable (sealed) | The host half (offsets→fingerprint→propose) is E2E'd via simulated messages; the DOM lookup is the only manual-smoke-only seam, kept deliberately thin |
| Unhiding pin / adding `editDocument` widens the command surface | Both guard on `editorLangId == markdown`; both route through existing seams; no new model or persisted state |
---
## 8. Traceability matrix
| Requirement (`#43`) | Use case | Design | Slice |
| --- | --- | --- | --- |
| Pin baseline button in the preview toolbar | PUC-5 | INV-35, §6.4 (`DiffViewController.pin`) | SLICE-1 |
| Resolve the orphaned `pinDiffBaseline` reachability | PUC-5 | §6.4 (`when` flip) | SLICE-1 |
| Single adaptive Ask-Claude button (Selection ⇆ Document) | PUC-2/3/4 | INV-37, §6.2 | SLICE-3/4 |
| Preview-selection → source range mapping (block-level) | PUC-3 | INV-36, §6.7 | SLICE-2/4 |
| Edit Document path (new whole-document edit) | PUC-4 | INV-37, §6.5 (hunk diff) | SLICE-3 |
| Edits route through existing F4/F3 (no divergent path) | PUC-3/4 | INV-35, §6.4 | SLICE-3/4 |
| Right-click entry opens the preview (minimal gateway) | PUC-6 | §6.4 (`editor/title`) | SLICE-5 |
| Controls only active for supported (authorable) docs | PUC-1/7 | §6.5 | SLICE-5 |
| Sealed webview, no document mutation / LLM surface | — | INV-21/35, §6.6 | all |
| No new edit/attribution/proposal model | — | §1.7, INV-37 | all |
| Unit + host E2E + right-click-opens-preview coverage | — | §6.8 | SLICE-1..5 |
## 9. Open Questions & Decisions log
- **RESOLVED (session 0036, operator):** preview-selection → source mapping =
**block-level** (`data-src` attributes from `BlockWithRange`; union of
intersected blocks); document edit = **diffed into per-hunk F4 proposals**;
`#43` **lands a minimal right-click → Open Review Preview gateway** (`#41`/`#42`
expand the menus).
- **RESOLVED (this spec, autonomous):** instruction prompt = **host `showInputBox`**
(LLM/secrets stay out of the webview); Pin targets the **previewed document**
(`DiffViewController.pin`); **no confirmation** on pin (matches existing); a new
`cowriting.editDocument` command is registered for `#42` reuse; `pinDiffBaseline`
is unhidden (`editorLangId == markdown`).
- **OPEN → later:** **char-precise** sub-block selection mapping (deferred — block
granularity is v1); the **richer `#41`/`#42` menu sets** (this lands only the
minimal gateway); preview→source **scroll-sync** (`#32`); whether a large
document rewrite should cap/segment its hunks (only if real rewrites prove
noisy); the repo rename to `vscode-markdown-cowriting-plugin` (`#35`, deferred).
## 10. Glossary & References
- **Preview toolbar** — the review preview's header row: the annotations on/off
checkbox (existing) plus F11's Pin baseline and adaptive Ask-Claude buttons.
**Adaptive Ask-Claude button** — one button reading "Edit Selection" (non-empty
preview selection) or "Edit Document" (none). **Block-level selection mapping** —
resolving a rendered-preview selection to the union of source blocks it
intersects, via `data-src-start/end` attributes emitted by the pure render
layer. **Hunk-diffed document edit** — Claude's whole-document rewrite split
into changed hunks, each surfaced as its own F4 proposal. **Pin baseline** — F6's
`DiffViewController.pin` applied to the previewed document. **Gateway** — a
right-click entry that opens the preview (this feature lands the minimal one;
`#41`/`#42` expand them).
- Feature `#43` (F11) · Epic `#1` · builds on F3 `#6`, F4 `#12`, F6 `#17`/`#19`,
F7 `#21`/`#22`, F9 `#27`, F10 `#29`/`#31` · coexists with `#41`/`#42` · parent
specs `coauthoring-inner-loop.md`, `coauthoring-attribution.md`,
`coauthoring-propose-accept.md`, `coauthoring-diff-view.md`,
`coauthoring-rendered-preview.md`, `coauthoring-interactive-review.md` · capture
session 0035 · lineage `ben.stull/rfc-app#48`.
@@ -0,0 +1,150 @@
# Author-colored track changes (both panes) — design
**Date:** 2026-06-26
**Status:** Design approved; ready for implementation planning
**Mockups:** `.superpowers/brainstorm/15095-1782504559/content/panes-states-v2.html`
## Problem
Today the plugin uses two unrelated color systems:
- **The diff** (proposed/changed text in both panes) uses *semantic* colors —
green = added, red = removed — regardless of who made the change.
- **Authorship** (F3) uses *author* colors (blue = Claude, green = human) but
only as a flat tint in the **preview** pane, and only on committed text, not
as a diff.
So you cannot look at a change and immediately see **who** made it. The product
goal is an experience like two humans collaborating in a track-changes document
(Google Docs suggesting mode / Word track changes): every edit is a diff, and a
glance tells you both *what changed* and *who changed it*.
## The model
Every change is rendered as a diff — insertions **and** deletions — where:
- **Style encodes the operation:** underline = inserted, strikethrough = removed.
- **Color encodes the author.**
| | Added | Removed |
| ------ | ---------------- | ---------------------- |
| Human | green underline | red strikethrough |
| Claude | blue underline | purple strikethrough |
Human keeps today's exact green/red. Claude gets blue/purple (blue is already
Claude's established attribution hue; purple reads as a removal without colliding
with human red).
**Color means "changed since the pinned baseline,"** not "authored forever." Once
a change is accepted and the baseline re-pins, its marks clear and the text
returns to plain. This preserves the existing pin→clean behavior (#48): a pinned,
zero-diff document renders with no annotation.
### Concrete colors
Carry the existing human values; add Claude's pair. (Final hex values are a
polish detail for implementation; these are the design intent.)
- Human added: `#3fb950` underline, bg `rgba(63,185,80,0.14)`
- Human removed: `#f85149` strikethrough, bg `rgba(248,81,73,0.11)`
- Claude added: `#58a6ff` underline, bg `rgba(88,166,255,0.15)`
- Claude removed: `#bc8cff` strikethrough, bg `rgba(188,140,255,0.13)`
Each change run is a subtle author-hued background tint **plus** a solid 2px
underline (insert) or strikethrough (delete) in the full author color — the solid
line carries the signal even where backgrounds are faint.
## Both panes (decision A)
Author-colored track changes appear in **both** the main editor pane and the
rendered preview pane.
- **Main editor pane:** gains full inline track-changes for *all*
changes-since-baseline — human and Claude, committed and pending — not just
pending proposals. **This reverses INV-32** ("editor fully clean") from F10:
the editor was deliberately kept clean, with all who-wrote-what coloring living
only in the preview. We are intentionally undoing that.
- **Rendered preview pane:** shows the same model (it already renders a diff +
authorship; this unifies them so ins/del runs are author-colored rather than
semantically colored).
Pending Claude proposals keep their Accept/Reject affordance in both panes —
CodeLens (`✓ Accept ▾ / ✗ Reject ▾`) in the editor, buttons in the preview.
## States (must all render correctly)
Base states: human-added, human-removed, Claude-added, Claude-removed.
**Overlap** — a single span both authors touched — stacks both marks:
- Human added it, Claude is removing it → green underline **+** purple strikethrough.
- Claude added it, human is removing it → blue underline **+** red strikethrough.
Block-level: a whole added block carries the author's add treatment (left border +
tint); a whole removed block carries the author's remove treatment (struck + tint).
Resting state: unchanged-since-baseline text renders plain. After Accept + re-pin,
changed text returns to plain.
## Architecture
The core change is **fusing the currently-separate semantic-diff and authorship
systems** so that every inserted/deleted run carries its author, and rendering
colors by that author rather than by add/remove semantics.
Components affected (per the current code map):
- **`src/trackChangesModel.ts`** — `renderReview` / `renderOp` /
`wordMergedMarkdown` / `colorByAuthor`. Today `<ins>`/`<del>` are emitted with
semantic green/red classes independent of author. Change: ins/del runs become
author-aware and emit author-colored classes; overlap emits a stacked-mark
class.
- **`src/editorProposalController.ts`** — today only pending proposals decorate,
using semantic `ThemeColor`s. Change: per-author/op `TextEditorDecorationType`s
(human-add/human-del/claude-add/claude-del + overlap), and decorate **all**
changes-since-baseline, not just proposals (the INV-32 reversal).
- **`src/attributionController.ts`** — supplies the per-character/per-run author
spans the diff needs to attribute insertions (and, see below, deletions).
- **`media/preview.css`** — recolor: keep human green/red, add Claude
blue/purple, encode underline=insert / strikethrough=delete, add overlap
(stacked-mark) classes for both directions.
## Open technical question (for the plan / tech-design stage)
**Deletion authorship.** Removed text is no longer in the buffer, so the renderer
must know *who deleted it* to color the strikethrough.
- For **pending proposals**, the deleter is trivially Claude (the proposal owns
the deletion).
- For **committed deletions** (baseline → current), we must confirm the F3 seam /
attribution model carries the **deleting actor**. F3 attributes
*current-buffer* characters; deleted characters are gone. If the seam's edit
events carry the actor that performed each removal, we attribute from that; if
not, deriving deletion authorship is the main thing the implementation plan has
to solve. Until resolved, a safe fallback is to color committed deletions by the
**turn actor** that produced the diff hunk.
This is the one genuinely open architectural item; everything else is
presentation.
## Out of scope (YAGNI)
- More than two authors / arbitrary per-collaborator palettes. Two roles
(human, Claude) only.
- User-configurable colors / theme settings. Ship one good scheme.
- Changing accept/reject *mechanics* — this is purely how changes are
**visualized**; the F4 proposal lifecycle is unchanged.
- Per-word authorship inside an accepted, re-pinned (plain) document.
## Testing
- **Unit (pure render):** `trackChangesModel` renders each state to the expected
author-colored markup — the four base states, both overlap directions, block
add/remove, and resting/plain. Reuse the existing pure-render test pattern.
- **Editor decorations:** the decoration plan produces the right per-author/op
ranges, including the INV-32-reversal (committed changes now decorate) and
stacked overlap ranges.
- **Host E2E:** open a co-edited doc, assert both panes show author-colored
marks; accept a proposal + re-pin → marks clear (pin→clean preserved).
- Update/replace any test that asserts the **old** semantic green=add/red=remove
classes or the INV-32 clean-editor behavior.
+13 -10
View File
@@ -31,13 +31,14 @@ const liveTurnOptions = {
};
/** @type {import('esbuild').BuildOptions} */
const previewOptions = {
entryPoints: ["media/preview.ts"],
outfile: "out/media/preview.js",
const previewMermaidOptions = {
entryPoints: ["media/preview-mermaid.ts"],
outfile: "out/media/preview-mermaid.js",
bundle: true,
// The webview is a browser context; mermaid is bundled IN (and ONLY in) this
// asset so it never bloats the extension-host bundle (the @cline/sdk size
// discipline). No externals — everything is shipped to the sealed webview.
// Task 7 (Q4): contributed via markdown.previewScripts into the BUILT-IN
// preview's webview (a browser context) — mermaid is bundled IN (and ONLY
// in) this asset so it never bloats the extension-host bundle (the
// @cline/sdk size discipline).
platform: "browser",
format: "iife",
target: "es2020",
@@ -48,12 +49,14 @@ const previewOptions = {
if (watch) {
const ctx = await context(options);
const ctxLive = await context(liveTurnOptions);
const ctxPreview = await context(previewOptions);
await Promise.all([ctx.watch(), ctxLive.watch(), ctxPreview.watch()]);
const ctxPreviewMermaid = await context(previewMermaidOptions);
await Promise.all([ctx.watch(), ctxLive.watch(), ctxPreviewMermaid.watch()]);
console.log("esbuild: watching…");
} else {
await build(options);
await build(liveTurnOptions);
await build(previewOptions);
console.log("esbuild: build complete → out/extension.cjs + out/liveTurn.mjs + out/media/preview.js");
await build(previewMermaidOptions);
console.log(
"esbuild: build complete → out/extension.cjs + out/liveTurn.mjs + out/media/preview-mermaid.js",
);
}
+55
View File
@@ -0,0 +1,55 @@
/*
* Task 7 (D3/D21, PUC-3) — authorship + change annotations for the BUILT-IN VS
* Code Markdown preview (`markdown.previewStyles`). Same F10 vocabulary as the
* (sunsetting) custom webview's `media/preview.css`: style = operation
* (underline = inserted, strikethrough = removed), color = author (human
* green, Claude blue) — `cw-del` has no author variant (a single fixed
* struck-red mark; see `previewAnnotations.ts`/`trackChangesModel.ts`'s "del"
* sentinel tag). Light-theme defaults below; `body.vscode-dark` (also applied
* on high-contrast dark) overrides with the same palette the webview preview
* already ships, for continuity across both review surfaces.
*/
.cw-ins-claude {
background: rgba(9, 105, 218, 0.12);
border-bottom: 2px solid #0969da;
text-decoration: none;
}
.cw-ins-human {
background: rgba(26, 127, 55, 0.12);
border-bottom: 2px solid #1a7f37;
text-decoration: none;
}
.cw-del {
background: rgba(207, 34, 46, 0.1);
text-decoration: line-through;
text-decoration-color: #cf222e;
opacity: 0.75;
}
body.vscode-dark .cw-ins-claude,
body.vscode-high-contrast:not(.vscode-high-contrast-light) .cw-ins-claude {
background: rgba(88, 166, 255, 0.15);
border-bottom-color: #58a6ff;
}
body.vscode-dark .cw-ins-human,
body.vscode-high-contrast:not(.vscode-high-contrast-light) .cw-ins-human {
background: rgba(63, 185, 80, 0.14);
border-bottom-color: #3fb950;
}
body.vscode-dark .cw-del,
body.vscode-high-contrast:not(.vscode-high-contrast-light) .cw-del {
background: rgba(248, 81, 73, 0.11);
text-decoration-color: #f85149;
}
/*
* F7.1 (#22) intra-diagram mermaid diff legend, shown beneath a mermaid fence
* `previewAnnotations.ts` re-emitted through `mermaidDiff.ts` (Task 7 §2.6
* parity — same legend markup + colors as the sunset webview's
* `media/preview.css`).
*/
.cw-mermaid-legend { display: flex; gap: 0.6rem; font-size: 0.75em; opacity: 0.85; margin: 0.2rem 0 0.6rem; }
.cw-mermaid-legend .cw-leg { padding: 0 0.4em; border-radius: 3px; border: 1px solid; }
.cw-mermaid-legend .cw-leg-add { color: #2ea043; border-color: #2ea043; }
.cw-mermaid-legend .cw-leg-chg { color: #d29922; border-color: #d29922; }
.cw-mermaid-legend .cw-leg-rem { color: #808080; border-color: #808080; border-style: dashed; }
+57
View File
@@ -0,0 +1,57 @@
/**
* Task 7 (Q4 mermaid check, D3/D21): the built-in VS Code Markdown preview does
* not render mermaid fences on its own — `previewAnnotations.ts`'s
* `cowritingMarkdownItPlugin` teaches ```mermaid fences to render as
* `<pre class="mermaid">SRC</pre>` (via `options.highlight`, the same
* extension point the built-in preview's own fence rule already calls); this
* script (contributed via `markdown.previewScripts`) is what actually turns
* those into diagrams — mermaid needs a DOM, so it runs here, in the preview's
* webview, never in the extension host. Bundled by esbuild as a standalone
* IIFE → out/media/preview-mermaid.js, so mermaid never enters the host
* bundle (matching the sealed webview's `media/preview.ts` precedent).
*
* Contributed preview scripts are reloaded on every content change (VS Code
* docs), so running at module top-level is sufficient; the
* `vscode.markdown.updateContent` listener is added defensively to also cover
* in-place content updates that don't reload the script (mirrors the proven
* `bierner.markdown-mermaid` extension's own approach).
*
* RESOLVED SCOPE NOTE (Q4 finding, migration plan Task 7 Step 2.6; wired in a
* cross-task review follow-up): this script itself only turns `pre.mermaid`
* nodes into diagrams — it never sees a diff. The intra-diagram diff/legend
* augmentation (F7.1, INV-29..31) happens one step earlier, source-side,
* where it CAN be unit-tested without a DOM: `previewAnnotations.ts`'s
* `cowritingMarkdownItPlugin` re-emits a CHANGED mermaid fence's source
* through `mermaidDiff.ts` (`buildMermaidQueue`, block-level `diffBlocks`
* pairing against the F6/F7 baseline) before this script ever runs, so the
* `pre.mermaid` node this script hands to `mermaid.run` already carries the
* `classDef`/`class`/`linkStyle` styling directives — this script needn't (and
* can't, DOM-less-ly) know a diagram changed at all. Basic mermaid rendering
* (this file) is exercised by the same proven pattern as the real
* `bierner.markdown-mermaid` extension; the augmentation upstream is exercised
* by pure unit tests (`test/previewAnnotations.test.ts`).
*/
import mermaid from "mermaid";
function theme(): "dark" | "default" {
return document.body.classList.contains("vscode-dark") || document.body.classList.contains("vscode-high-contrast")
? "dark"
: "default";
}
async function run(): Promise<void> {
const nodes = Array.from(document.querySelectorAll<HTMLElement>("pre.mermaid"));
if (nodes.length === 0) return;
mermaid.initialize({ startOnLoad: false, theme: theme(), securityLevel: "strict" });
try {
await mermaid.run({ nodes });
} catch {
// mermaid.run already marks failed nodes; ensure a visible chip per failure.
for (const n of nodes) {
if (!n.querySelector("svg")) n.setAttribute("data-cw-error", "true");
}
}
}
window.addEventListener("vscode.markdown.updateContent", () => void run());
void run();
-85
View File
@@ -1,85 +0,0 @@
/* F7 track-changes preview — theme-aware via VS Code webview CSS variables. */
body {
font-family: var(--vscode-font-family);
font-size: var(--vscode-font-size);
color: var(--vscode-foreground);
background: var(--vscode-editor-background);
padding: 0 1.2rem 2rem;
line-height: 1.5;
}
#cw-header {
position: sticky;
top: 0;
background: var(--vscode-editor-background);
border-bottom: 1px solid var(--vscode-panel-border);
padding: 0.5rem 0;
font-size: 0.85em;
opacity: 0.85;
display: flex;
gap: 1rem;
}
#cw-summary .cw-add { color: var(--vscode-gitDecoration-addedResourceForeground); }
#cw-summary .cw-del { color: var(--vscode-gitDecoration-deletedResourceForeground); }
.cw-blk { position: relative; }
ins, .cw-added {
background: var(--vscode-diffEditor-insertedTextBackground, rgba(0, 255, 0, 0.15));
text-decoration: none;
}
del, .cw-removed {
background: var(--vscode-diffEditor-removedTextBackground, rgba(255, 0, 0, 0.15));
text-decoration: line-through;
opacity: 0.7;
}
.cw-changed { outline: 2px solid var(--vscode-diffEditor-insertedTextBackground, rgba(0, 255, 0, 0.25)); outline-offset: 2px; }
.cw-badge {
position: absolute;
top: 0;
right: 0;
font-size: 0.7em;
padding: 0 0.4em;
border-radius: 3px;
background: var(--vscode-badge-background);
color: var(--vscode-badge-foreground);
}
.cw-error {
border: 1px solid var(--vscode-inputValidation-errorBorder);
background: var(--vscode-inputValidation-errorBackground);
color: var(--vscode-errorForeground);
padding: 0.3rem 0.6rem;
border-radius: 3px;
font-size: 0.85em;
}
pre.mermaid { text-align: center; background: transparent; }
pre.mermaid[data-cw-error] { color: var(--vscode-errorForeground); }
/* F9 authorship mode — inline author tints + block-level fence badges + toggle. */
.cw-by-claude { background: var(--vscode-editorInfo-foreground, rgba(64, 120, 242, 0.18)); text-decoration: none; }
.cw-by-human { background: var(--vscode-gitDecoration-addedResourceForeground, rgba(46, 160, 67, 0.18)); text-decoration: none; }
.cw-blk.cw-by-claude, .cw-blk.cw-by-human, .cw-blk.cw-mixed { outline: 2px solid currentColor; outline-offset: 2px; background: transparent; }
#cw-legend .cw-swatch { padding: 0 0.4em; border-radius: 3px; }
#cw-summary .cw-prop { opacity: 0.85; }
/* F10 interactive review — annotations toggle + ✓/✗ proposal blocks. */
#cw-toggle { display: inline-flex; align-items: center; gap: 0.35em; cursor: pointer; }
.cw-proposal {
position: relative;
border-left: 3px solid var(--vscode-charts-blue, #4daafc);
background: color-mix(in srgb, var(--vscode-charts-blue, #4daafc) 12%, transparent);
padding: 0.4em 0.6em; margin: 0.4em 0; border-radius: 3px;
}
.cw-proposal-unanchored { border-left-style: dashed; opacity: 0.85; }
.cw-actions { position: absolute; top: 0.2em; right: 0.4em; display: inline-flex; gap: 0.25em; }
.cw-actions button {
cursor: pointer; border: 1px solid var(--vscode-button-border, transparent);
border-radius: 3px; font-size: 0.9em; line-height: 1; padding: 0.1em 0.35em;
background: var(--vscode-button-secondaryBackground); color: var(--vscode-button-secondaryForeground);
}
.cw-accept:hover { background: var(--vscode-testing-iconPassed, #2ea043); color: #fff; }
.cw-reject:hover { background: var(--vscode-errorForeground, #f14c4c); color: #fff; }
/* F7.1 (#22) intra-diagram mermaid diff legend. */
.cw-mermaid-legend { display: flex; gap: 0.6rem; font-size: 0.75em; opacity: 0.85; margin: 0.2rem 0 0.6rem; }
.cw-mermaid-legend .cw-leg { padding: 0 0.4em; border-radius: 3px; border: 1px solid; }
.cw-mermaid-legend .cw-leg-add { color: #2ea043; border-color: #2ea043; }
.cw-mermaid-legend .cw-leg-chg { color: #d29922; border-color: #d29922; }
.cw-mermaid-legend .cw-leg-rem { color: #808080; border-color: #808080; border-style: dashed; }
-86
View File
@@ -1,86 +0,0 @@
/**
* F7 preview webview client (sealed sandbox, INV-21). Receives annotated HTML
* from the extension host and swaps it in; runs mermaid over `.mermaid` blocks
* (mermaid needs a DOM, so it runs here, not in the host). Bundled by esbuild as
* a standalone IIFE → out/media/preview.js, so mermaid never enters the host
* bundle. No network, no LLM.
*/
import mermaid from "mermaid";
// Imported so esbuild emits the sibling out/media/preview.css (the controller
// links it into the sealed shell via asWebviewUri).
import "./preview.css";
declare function acquireVsCodeApi(): { postMessage(m: unknown): void };
interface RenderMessage {
type: "render";
mode: "on" | "off";
html: string;
epoch?: string;
summary?: { added: number; removed: number; proposals: number };
}
const vscodeApi = acquireVsCodeApi();
const body = document.getElementById("cw-body")!;
const header = document.getElementById("cw-epoch")!;
const summary = document.getElementById("cw-summary")!;
const legend = document.getElementById("cw-legend")!;
const annotationsEl = document.getElementById("cw-annotations") as HTMLInputElement | null;
// F10: the annotations on/off toggle.
annotationsEl?.addEventListener("change", () => {
vscodeApi.postMessage({ type: "setMode", mode: annotationsEl.checked ? "on" : "off" });
});
// F10: delegated ✓/✗ accept/reject of pending proposals (routed back to the F4 seam).
body.addEventListener("click", (e) => {
const btn = (e.target as HTMLElement)?.closest<HTMLElement>(".cw-actions button");
if (!btn) return;
const block = btn.closest<HTMLElement>(".cw-proposal");
const id = block?.dataset.proposalId;
const action = btn.dataset.action;
if (id && (action === "accept" || action === "reject")) {
vscodeApi.postMessage({ type: action, proposalId: id });
}
});
function themeFor(): "dark" | "default" {
return document.body.classList.contains("vscode-dark") ||
document.body.classList.contains("vscode-high-contrast")
? "dark"
: "default";
}
async function renderMermaid(): Promise<void> {
const nodes = Array.from(body.querySelectorAll<HTMLElement>("pre.mermaid"));
if (nodes.length === 0) return;
mermaid.initialize({ startOnLoad: false, theme: themeFor(), securityLevel: "strict" });
try {
await mermaid.run({ nodes });
} catch {
// mermaid.run already marks failed nodes; ensure a visible chip per failure.
for (const n of nodes) {
if (!n.querySelector("svg")) n.setAttribute("data-cw-error", "true");
}
}
}
window.addEventListener("message", (event: MessageEvent<RenderMessage>) => {
const msg = event.data;
if (msg?.type !== "render") return;
body.innerHTML = msg.html;
const on = msg.mode === "on";
if (annotationsEl) annotationsEl.checked = on;
// Off-state is a clean preview: hide the review chrome.
header.hidden = !on;
summary.hidden = !on;
legend.hidden = true;
if (on) {
header.textContent = `Review since ${msg.epoch ?? ""}`;
summary.innerHTML =
`<span class="cw-add">+${msg.summary?.added ?? 0}</span> ` +
`<span class="cw-del">${msg.summary?.removed ?? 0}</span> ` +
`<span class="cw-prop">${msg.summary?.proposals ?? 0} proposal${(msg.summary?.proposals ?? 0) === 1 ? "" : "s"}</span>`;
}
void renderMermaid();
});
+211 -33
View File
@@ -18,6 +18,28 @@
"onStartupFinished"
],
"contributes": {
"markdown.markdownItPlugins": true,
"markdown.previewStyles": [
"./media/preview-annotations.css"
],
"markdown.previewScripts": [
"./out/media/preview-mermaid.js"
],
"configuration": {
"title": "Cowriting",
"properties": {
"cowriting.liveProgress.revealOutput": {
"type": "boolean",
"default": true,
"description": "When Claude is editing, reveal the \"Cowriting: Claude\" output channel (without stealing focus) as soon as Claude starts producing text, so you can read the output as it streams."
},
"cowriting.annotations": {
"type": "boolean",
"default": true,
"description": "Show authorship + change annotations in the Markdown preview for coedited documents."
}
}
},
"commands": [
{
"command": "cowriting.showClineSdkInfo",
@@ -49,40 +71,90 @@
"title": "Apply Agent Edit (internal seam)",
"category": "Cowriting"
},
{
"command": "cowriting.edit",
"title": "Ask Claude to Edit",
"category": "Cowriting"
},
{
"command": "cowriting.editSelection",
"title": "Ask Claude to Edit Selection",
"category": "Cowriting"
},
{
"command": "cowriting.acceptProposal",
"title": "✓ Accept Proposal",
"category": "Cowriting"
},
{
"command": "cowriting.rejectProposal",
"title": "✗ Reject Proposal",
"category": "Cowriting"
},
{
"command": "cowriting.proposeAgentEdit",
"title": "Propose Agent Edit (internal seam)",
"category": "Cowriting"
},
{
"command": "cowriting.toggleDiffView",
"title": "Cowriting: Toggle Diff View",
"command": "cowriting.markReviewed",
"title": "Mark Changes as Reviewed",
"category": "Cowriting",
"icon": "$(check-all)"
},
{
"command": "cowriting.reviewChanges",
"title": "Review Changes",
"category": "Cowriting",
"icon": "$(git-compare)"
},
{
"command": "cowriting.openReviewPreview",
"title": "Open Cowriting Review Preview",
"category": "Cowriting"
},
{
"command": "cowriting.pinDiffBaseline",
"title": "Cowriting: Pin Diff Baseline to Now",
"command": "cowriting.editDocument",
"title": "Ask Claude to Edit Document",
"category": "Cowriting"
},
{
"command": "cowriting.showTrackChangesPreview",
"title": "Cowriting: Open Review Preview",
"command": "cowriting.acceptAllProposals",
"title": "Accept All Claude Proposals",
"category": "Cowriting"
},
{
"command": "cowriting.rejectAllProposals",
"title": "Reject All Claude Proposals",
"category": "Cowriting"
},
{
"command": "cowriting.proposalAcceptMenu",
"title": "Accept Claude Proposal",
"category": "Cowriting"
},
{
"command": "cowriting.proposalRejectMenu",
"title": "Reject Claude Proposal",
"category": "Cowriting"
},
{
"command": "cowriting.coeditDocument",
"title": "✦ Coedit this Document with Claude",
"category": "Cowriting"
},
{
"command": "cowriting.stopCoediting",
"title": "Stop editing with Claude",
"category": "Cowriting"
},
{
"command": "cowriting.askClaude",
"title": "Ask Claude",
"category": "Cowriting",
"icon": "$(sparkle)"
},
{
"command": "cowriting.makeThreadEdit",
"title": "✦ Make this edit",
"category": "Cowriting",
"icon": "$(sparkle)"
},
{
"command": "cowriting.toggleAnnotations",
"title": "Toggle Annotations",
"category": "Cowriting",
"icon": "$(eye)"
}
],
"menus": {
@@ -96,64 +168,168 @@
"when": "false"
},
{
"command": "cowriting.acceptProposal",
"command": "cowriting.createThread",
"when": "editorHasSelection && (resourceScheme == file || resourceScheme == untitled) && cowriting.isCoediting"
},
{
"command": "cowriting.reviewChanges",
"when": "editorLangId == markdown && cowriting.isCoediting"
},
{
"command": "cowriting.markReviewed",
"when": "editorLangId == markdown && cowriting.isCoediting && cowriting.baselineMode == snapshot"
},
{
"command": "cowriting.edit",
"when": "editorLangId == markdown"
},
{
"command": "cowriting.editSelection",
"when": "false"
},
{
"command": "cowriting.rejectProposal",
"command": "cowriting.editDocument",
"when": "false"
},
{
"command": "cowriting.toggleDiffView",
"command": "cowriting.askClaude",
"when": "editorLangId == markdown && cowriting.isCoediting"
},
{
"command": "cowriting.toggleAnnotations",
"when": "editorLangId == markdown && cowriting.isCoediting"
},
{
"command": "cowriting.makeThreadEdit",
"when": "false"
},
{
"command": "cowriting.pinDiffBaseline",
"command": "cowriting.acceptAllProposals",
"when": "editorLangId == markdown && cowriting.isCoediting"
},
{
"command": "cowriting.rejectAllProposals",
"when": "editorLangId == markdown && cowriting.isCoediting"
},
{
"command": "cowriting.proposalAcceptMenu",
"when": "false"
},
{
"command": "cowriting.proposalRejectMenu",
"when": "false"
}
],
"editor/title": [
{
"command": "cowriting.reviewChanges",
"when": "resourceLangId == markdown && cowriting.isCoediting",
"group": "navigation@1"
},
{
"command": "cowriting.askClaude",
"when": "resourceLangId == markdown && cowriting.isCoediting",
"group": "navigation@2"
},
{
"command": "cowriting.toggleAnnotations",
"when": "resourceLangId == markdown && cowriting.isCoediting",
"group": "navigation@3"
},
{
"command": "cowriting.markReviewed",
"when": "resourceLangId == markdown && cowriting.isCoediting && cowriting.baselineMode == snapshot",
"group": "navigation@4"
},
{
"command": "cowriting.openReviewPreview",
"when": "resourceLangId == markdown",
"group": "navigation@9"
}
],
"editor/title/context": [
{
"command": "cowriting.edit",
"when": "resourceLangId == markdown && cowriting.isCoediting",
"group": "1_cowriting@1"
},
{
"command": "cowriting.openReviewPreview",
"when": "resourceLangId == markdown",
"group": "1_cowriting@3"
}
],
"explorer/context": [
{
"command": "cowriting.openReviewPreview",
"when": "resourceLangId == markdown",
"group": "navigation@9"
}
],
"editor/context": [
{
"command": "cowriting.editSelection",
"when": "editorHasSelection && (resourceScheme == file || resourceScheme == untitled)",
"command": "cowriting.edit",
"when": "editorLangId == markdown && (resourceScheme == file || resourceScheme == untitled) && cowriting.isCoediting",
"group": "1_cowriting@1"
},
{
"command": "cowriting.createThread",
"when": "editorHasSelection && (resourceScheme == file || resourceScheme == untitled)",
"when": "editorHasSelection && (resourceScheme == file || resourceScheme == untitled) && cowriting.isCoediting",
"group": "1_cowriting@2"
},
{
"command": "cowriting.coeditDocument",
"when": "editorLangId == markdown && !cowriting.isCoediting",
"group": "1_cowriting@1"
},
{
"command": "cowriting.stopCoediting",
"when": "editorLangId == markdown && cowriting.isCoediting",
"group": "1_cowriting@9"
}
],
"comments/commentThread/context": [
{
"command": "cowriting.reply",
"group": "inline",
"group": "inline@1",
"when": "commentController == cowriting.threads"
},
{
"command": "cowriting.makeThreadEdit",
"group": "inline@2",
"when": "commentController == cowriting.threads && commentThread =~ /\\boffer\\b/"
}
],
"comments/commentThread/title": [
{
"command": "cowriting.makeThreadEdit",
"group": "inline@1",
"when": "commentController == cowriting.threads && commentThread =~ /\\boffer\\b/"
},
{
"command": "cowriting.resolveThread",
"group": "inline",
"when": "commentController == cowriting.threads && commentThread =~ /^open$/"
"when": "commentController == cowriting.threads && commentThread =~ /\\bopen\\b/"
},
{
"command": "cowriting.reopenThread",
"group": "inline",
"when": "commentController == cowriting.threads && commentThread =~ /^resolved$/"
"when": "commentController == cowriting.threads && commentThread =~ /\\bresolved\\b/"
}
]
},
"keybindings": [
{
"command": "cowriting.toggleDiffView",
"key": "ctrl+alt+d",
"when": "false"
"command": "cowriting.reviewChanges",
"key": "ctrl+alt+r",
"mac": "cmd+alt+r",
"when": "editorLangId == markdown && cowriting.isCoediting"
},
{
"command": "cowriting.showTrackChangesPreview",
"key": "ctrl+alt+r",
"when": "editorLangId == markdown"
"command": "cowriting.edit",
"key": "ctrl+alt+e",
"mac": "cmd+alt+e",
"when": "editorTextFocus && editorLangId == markdown && cowriting.isCoediting"
}
]
},
@@ -162,9 +338,11 @@
"watch": "node esbuild.mjs --watch",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"pretest:e2e": "npm run build && tsc -p tsconfig.e2e.json",
"clean:e2e": "node -e \"require('fs').rmSync('out/test',{recursive:true,force:true})\"",
"pretest:e2e": "npm run build && npm run clean:e2e && tsc -p tsconfig.e2e.json",
"test:e2e": "node ./out/test/e2e/runTest.js",
"smoke:live": "npm run build && node scripts/smoke-live-turn.mjs",
"smoke:native": "npm run pretest:e2e && node scripts/smoke-native-loop.mjs",
"vscode:prepublish": "node esbuild.mjs"
},
"dependencies": {
+9 -1
View File
@@ -10,7 +10,15 @@ console.log(`instruction: ${instruction}`);
console.log(`text: ${text}`);
try {
const t0 = Date.now();
const result = await runEditTurn(instruction, text);
// #60: exercise the live-progress path against the real SDK — log each reduced
// snapshot so the smoke shows streaming/activity/tokens, not just the result.
const result = await runEditTurn(instruction, text, {
onProgress: (s) => {
const bits = [s.phase === "tool" ? `tool:${s.tool}` : s.phase, `${s.chars}c`];
if (s.tokens) bits.push(`${s.tokens}tok`);
console.log(` progress: ${bits.join(" ")}`);
},
});
console.log(`replacement: ${JSON.stringify(result.replacement)}`);
console.log(`model: ${result.model}`);
console.log(`sessionId: ${result.sessionId}`);
+67
View File
@@ -0,0 +1,67 @@
#!/usr/bin/env node
// Task 9 / spec §7.1 rung 3: the manual, real-@cline/sdk smoke for the native
// comment loop -- mirrors scripts/smoke-live-turn.mjs's role for F3 (a live
// login-backed check, deliberately NOT in CI), but this one drives the whole
// EDH (comment -> reply -> offer -> proposal needs vscode's own comment/doc
// APIs, unlike smoke-live-turn.mjs's bare module call) via
// test/e2e/smoke/nativeLoop.ts, launched exactly like test/e2e/runTest.ts
// launches the regular host-E2E suites -- just against a single smoke entry
// point that is NOT swept into `npm run test:e2e` (it lives outside
// test/e2e/suite, which is the only directory suite/index.ts's mocha glob
// searches).
//
// Prereqs: `npm run pretest:e2e` (builds the extension + compiles
// test/e2e/**). `npm run smoke:native` does this for you.
//
// Opens the real sandbox/ folder (the repo's EDH workspace root), writes a
// throwaway doc, posts a real comment via ThreadController.createThreadOnSelection,
// waits for the real @cline/sdk reply + offer, makes + accepts the edit, then
// prints the created proposal ids and deletes the throwaway doc.
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { runTests } from "@vscode/test-electron";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function main() {
const projectRoot = path.resolve(__dirname, "..");
const extensionDevelopmentPath = projectRoot;
const extensionTestsPath = path.resolve(projectRoot, "out/test/e2e/smoke/nativeLoop");
const sandbox = path.resolve(projectRoot, "sandbox");
if (!fs.existsSync(`${extensionTestsPath}.js`)) {
console.error(
`smoke-native-loop: ${extensionTestsPath}.js is missing -- run "npm run pretest:e2e" first (or "npm run smoke:native", which does this for you).`,
);
process.exit(1);
}
// Same macOS UNIX-socket-length workaround as test/e2e/runTest.ts (a long
// worktree path + the default .vscode-test/user-data dir can blow past the
// ~103-char limit).
const userDataRoot = process.platform === "win32" ? os.tmpdir() : "/tmp";
const userDataDir = fs.mkdtempSync(path.join(userDataRoot, "cwud-smoke-"));
try {
await runTests({
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [sandbox, "--disable-extensions", "--user-data-dir", userDataDir],
});
console.log("smoke-native-loop: PASS");
process.exit(0);
} catch (err) {
console.error(
`smoke-native-loop: FAILED (expected when Claude Code is absent/signed out, or the sandbox/ folder is missing): ${
err instanceof Error ? err.message : String(err)
}`,
);
process.exit(1);
} finally {
fs.rmSync(userDataDir, { recursive: true, force: true });
}
}
main();
@@ -0,0 +1,91 @@
# Session 0032.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T01-20 (PST)
> End: 2026-06-12T03-05 (PST)
> Type: planning-and-executing
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
"What's next?" → operator chose: **tidy loose ends, then plan-and-execute #31**.
Then "keep going" → continued into #34, then #33.
## Pre-state
On `main`, but the baseline was not clean: local `main` behind `origin/main` by 3
(session-transcript commits — this app keeps `sessions/` in the code repo), an
untracked `specs/` dir, an abandoned worktree `pedantic-brahmagupta-472309`
(106 commits behind), and two merged-but-undeleted remote branches. Memory pointed
to plan-and-execute #31 next.
## Arc
### 1. Loose-ends tidy
- Fast-forwarded `main`; synced the 0032 claim.
- F10 spec `coauthoring-interactive-review.md` was missing from the content repo —
placed it in the content repo **working tree** (left uncommitted; that repo has
operator-owned capture drafts + a diff-view.md edit pending the operator's publish).
- Removed the stray code-repo `specs/` (one dup of content, one now-placed).
- Removed the abandoned worktree (untracked `prototype/` + `.claude/` snapshotted
to `~/.wgl/abandoned-worktree-snapshots/pedantic-brahmagupta-472309/`).
- Deleted the two merged remote branches.
- **Correction:** the "0024 unfinalized" loose end carried in prior memory is
FALSE — 0024 has a finalized transcript. Dropped from memory.
### 2. #31 — inline-at-anchor proposals (PR #36, merged; issue closed)
TDD. `renderReview` now associates each resolved proposal with the current-side
block its `anchorStart` falls in and emits the `cw-proposal` block right after that
block; same-block order by anchorStart then id; unresolved/unplaceable still trail
(INV-33/34). Implements the placement the F10 spec §2/§6.2 already specified (the
#29 trailing-block was a recorded v1 deferral). +5 unit tests, +1 E2E in-place
assertion. One pre-existing F7 E2E flake (fixed-timeout `settle()` race) passed on
re-run — unrelated to the change (different code path).
### 3. #34 — delete F6 dead two-pane view (PR #37, merged; issue closed)
Removed the dead `vscode.diff` VIEW: `DiffViewController.toggle`/`findDiffTab`/
`epochLabel`/`isDiffOpen`, the `cowriting-baseline:` content provider +
`BASELINE_SCHEME` + `baselineUri` + content-provider emitter, the `toggleDiffView`
command + `ctrl+alt+d` keybinding. Kept the baseline DATA layer fully intact
(ensureBaseline/advance/pin/capture, getBaseline, onDidChangeBaseline, persistence
INV-19, machine-landing advance INV-18). Rewrote the diffView E2E suite to the
baseline-data-layer tests; flipped F10 + no-workspace assertions to "toggleDiffView
absent."
### 4. #33 — intra-emphasis sentinel hardening (characterized, DEFERRED)
Reproduced two failure modes with the real renderer (recorded as issue #33
comment). Operator chose to defer to its own session. No code changed.
## Cut state
`main` clean and synced; 0 open PRs. #31 + #34 shipped and closed. 194 unit + 49
E2E green; typecheck + build clean. No deploy pipeline (VS Code extension, no
flotilla/PPE).
## Deferred decisions
1. **Kept `pinDiffBaseline` in #34 against the issue's literal acceptance.**
Alternative: remove it as the issue listed. Why: the canonical Solution Design
(spec §6.7) scopes the removal to the two-pane VIEW only ("keep the controller +
baseline store"); `pin()` is a §6.4 baseline-lifecycle op, never touches
`vscode.diff`, and is exercised by live F7 baseline-reset tests. Spec wins over
the P3 capture draft (documentation-leads-automation). Logged for operator
awareness.
2. **F10 spec left UNCOMMITTED in the content repo working tree** rather than
committed/pushed. Alternative: push it. Why: the content repo has operator-owned
pending work (capture issue-drafts + a diff-view.md edit); content publishing is
the Author's gesture (per wgl-capture). Surfaced to the operator.
3. **Discarded the abandoned worktree's untracked files** (prototype/index.html +
.claude config). Snapshot-and-proceed: copied to
`~/.wgl/abandoned-worktree-snapshots/` before `git worktree remove --force`.
## Next /goal
```
/goal plan-and-execute #33 (intra-emphasis sentinel hardening) via the token-aware HTML-post-process approach recommended in the issue #33 comment, per specs/coauthoring-interactive-review.md §1.7/§9
```
(Alternatives in the backlog: #32 scroll-sync needs a Solution Design first
(brainstorming); #35 repo rename is an outward admin gesture awaiting operator
timing.)
@@ -0,0 +1,59 @@
# Session 0033.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T03-14 (PST)
> End: 2026-06-12T03-27 (PST)
> Type: capture (tracked-lite)
> Posture: careful (capture default — output operator-reviewed)
> Status: **FINALIZED**
## Launch prompt
`/wgl-capture` — "Undo doesn't render correctly in the preview pane".
## Arc
A capture session turning one operator observation into a filed, typed,
source-linked Gitea issue.
- **Clarified the symptom** (AskUserQuestion): "updates but wrong marks" in the
**F10 review preview**, reproduces **always** on undo. Reflected back the read
that undo reverts the buffer (text updates) but the overlay marks (since-baseline
diff / F3 author-coloring / F4 proposal blocks) go stale/wrong.
- **Drafted** `issues/undo-renders-wrong-marks-in-preview.md` in the content repo
working tree (terse, bug-depth §5 template). Classified **type/bug**.
- **Type/bug vs the tool:** `capture-issues.sh` only accepts
`epic|feature|story|task`, so it can't file a bug — even though the tracker has a
`type/bug` label and handbook §4.3's gate vocabulary includes "leaf
story/task/bug." Declined to mis-type the defect as a task to fit the tool.
- **Filed `#38`** (type/bug, priority/P1 — operator decision) via `gitea-api.sh`
directly (labels API), since the deterministic capture script couldn't express
`bug`. → https://git.benstull.org/benstull/vscode-cowriting-plugin/issues/38
- **Filed plugin feedback `#124`** (friction/medium) on the plugin tracker for the
capture-can't-type-bugs gap, framed as a taxonomy inconsistency to reconcile
across the Capture RFC §4, `ensure-capture-labels.sh`, `capture-issues.sh`,
`wgl-capture/SKILL.md`, and handbook §4.3.
## Cut state
Code repo clean on `main`. One issue filed (#38) + one plugin-feedback issue (#124).
Draft `undo-renders-wrong-marks-in-preview.md` left uncommitted in the content repo
working tree (operator's to publish — INV-8), alongside the other pending capture
drafts + the F10 spec placed in session 0032.
## Deferred decisions
1. **Filed #38 with `type/bug` via a `gitea-api.sh` bypass** rather than
`capture-issues.sh`. Alternative: type it `task` to stay within the tool. Why:
the defect's honest type is `bug` (label exists; §4.3 recognizes it); the script
gap shouldn't drive mis-classification. The gap itself was logged as plugin
feedback #124. Priority P1 was the operator's explicit call.
## Next /goal
```
/goal plan-and-execute #38 (undo renders wrong marks in the F10 review preview, P1) — reproduce the wrong-marks-after-undo case, find which layer (F3 attribution spans / F4 proposal anchors) goes stale on undo and reconcile it, confirm across edit→undo→redo
```
(P1 — supersedes session 0032's #33 pointer. #33 intra-emphasis hardening (P3) and
#32 scroll-sync (feature, needs a Solution Design) remain open.)
@@ -0,0 +1,63 @@
# Session 0034.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T03-28 (PST)
> End: 2026-06-12T03-39 (PST)
> Type: planning-and-executing
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
`/goal next` → resolved from memory to: plan-and-execute #38 (undo renders wrong
marks in the F10 review preview, P1).
## Plan
Fix #38 (P1). Root cause (confirmed by reading + E2E repro): attribution attributed
every non-seam change to currentAuthor() (always human), ignoring e.reason — so
undo/redo re-inserting text falsely colored it human. Fix: geometry-only span
reconciliation on undo/redo (no fresh attribution). E2E repro → minimal fix → green.
## Arc (systematic debugging)
- **Phase 1 (root cause):** traced the three data sources feeding the pure
`renderReview` (baseline diff / F3 author spans / F4 proposals). Read
`attributionController.onDidChange` + `attributionTracker.applyChange`: every
non-seam change is attributed to `currentAuthor()` (always human) and `e.reason`
is ignored. Proposals only shift (not the culprit). Disk-sync guard masks
undo-to-saved-state → bug is on mid-edit undo.
- **Reproduce:** wrote `test/e2e/suite/undoMarks.test.ts` driving edit→edit→undo
(mid-edit, buffer stays dirty). Confirmed FAILING: after undo, `spansFor` =
`[{start:6,end:12,author:"human"}]` over restored "bravo " — false attribution.
- **Fix (Phase 4):** `applyChange` gains `attributeInserted` (default true);
`onDidChange` sets it false and skips seam matching when
`e.reason === Undo|Redo`. Restored text stays neutral, not falsely human.
- **Verify:** repro passes; +3 unit tests for the geometric-only path. 197 unit +
50 E2E green; typecheck clean.
- **Shipped:** PR #39 merged → main; issue #38 closed.
- **Follow-up filed #40** (type/task, P3): restore *exact* prior provenance on
undo/redo (the fix leaves undone text neutral, not its original author color);
needs an attribution history stack synced to the editor undo stack.
## Cut state
`main` clean + synced; 0 open PRs. #38 fixed and closed; #40 filed. 197 unit + 50
E2E green. No deploy pipeline (VS Code extension).
## Deferred decisions
1. **Scoped the fix to "neutral on undo," not exact provenance restoration.**
Alternative: an attribution history stack that restores each char's original
author on undo/redo. Why deferred: synchronizing such a stack with VS Code's
undo (which coalesces edits) is fragile, and the neutral fix already removes the
reported misleading marks. Tracked as #40 (P3) so it isn't lost.
## Next /goal
```
/goal plan-and-execute #33 (intra-emphasis sentinel hardening) via the token-aware HTML-post-process approach recommended in the issue #33 comment, per specs/coauthoring-interactive-review.md §1.7/§9
```
(Backlog: #40 undo provenance (P3); #32 scroll-sync (feature, needs a Solution
Design); #35 repo rename (outward, operator timing).)
@@ -0,0 +1,104 @@
# Session 0035.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T08-53 (PST)
> End: 2026-06-12T10-58 (PST)
> Type: capture (tracked-lite)
> Status: **FINALIZED**
## Launch prompt
```
Add "Open Cowriting Review Panel" to the context window when you right click on a markdown file or its tab
```
## Pre-state
- Branch `main`, clean, pushed. Prior session 0034 had shipped the #38 undo fix
(PR #39). Recorded next goal there was #33 (intra-emphasis hardening) — this
session instead opened as a **capture** session on a new line of UX asks.
- Existing command surface (from `package.json`): `cowriting.editSelection`
("Ask Claude to Edit Selection", editor-context, selection-gated);
`cowriting.showTrackChangesPreview` ("Open Review Preview", palette +
`ctrl+alt+r`); `cowriting.pinDiffBaseline` ("Pin Review Baseline to Now"),
registered but **palette-suppressed** (`when:false`) with no menu/keybinding —
orphaned since #34 deleted its F6 two-pane host.
## Arc (turn by turn)
1. **Claim + resolve.** Claimed tracked-lite transcript 0035 (`--type capture`).
Resolved app `vscode-cowriting-plugin`, content repo
`benstull/vscode-cowriting-plugin-content` (cloned), tracker
`benstull/vscode-cowriting-plugin`.
2. **Ask 1 → #41.** Right-click on a markdown file/tab → "Open Cowriting Review
Panel". Sized **type/story, P2** (pure menu wiring to the existing preview
command). Drafted `issues/review-panel-context-menu.md`. Flagged the label
wrinkle (VS Code renders the command's own title). Operator: "first one looks
good" → ensured labels (needed host token
`wgl-gitea-issues-readwrite-token-git.benstull.org`; bare default 401'd) →
filed **#41**.
3. **Ask 2 → #42.** "Ask Claude to Edit Document" when right-clicking the body with
no selection; Edit Selection/Edit Document on the tab too. Sized
**type/feature, P2** — beyond menu wiring it introduces a *new whole-document
edit capability* with open product questions (instruction gathering, one big
F4 proposal vs many, large-doc/token behavior). Drafted
`issues/ask-claude-edit-document-context-menu.md`. Operator: "file as a
feature" → filed **#42**.
4. **Question: pin baseline.** Operator asked if the baseline can be reset to the
current editor. Answer: yes — `cowriting.pinDiffBaseline` does exactly that
(`capture(doc,"pinned")` → baseline := `document.getText()`). Initially said
"palette-only"; operator's screenshot showed it absent from the palette →
re-checked and **corrected**: it's `when:false`-suppressed with no menu/
keybinding, i.e. **unreachable from any UI**.
5. **Ask 3 → #43 (lead).** Operator's direction: make the **rendered preview the
primary interaction surface** — toolbar (home of the annotations checkbox)
gains one **adaptive "Ask Claude…" button** (Edit Selection ⇆ Edit Document by
*preview* selection) and a **Pin baseline** button. Asked two shaping
questions; operator chose **coexist** (native menus #41/#42 become **gateways
that open the preview**) and **P1**. Sized **type/feature, P1**; central design
risk = mapping a rendered-preview selection back to a source markdown range
(fallback: document-level button first). Drafted
`issues/preview-toolbar-interaction-surface.md` → operator "file as-is" →
filed **#43**.
6. **Dependencies.** Operator: make #41/#42 dependent on #43. Set via Gitea
issue-dependencies API — discovered the body needs `{"owner","repo","index"}`
with the repo key **`repo`** (documented `name` 404s as
`IsErrRepoNotExist`). #41 and #42 now **blocked-by #43**; verified.
7. **Finalize** (tracked-lite): base survey (session repo clean, nothing to land);
no spec/plan artifact (INV-6); memory updated; transcript published.
## Cut state
- **Filed:** #41 (story P2), #42 (feature P2), #43 (feature P1) on
`benstull/vscode-cowriting-plugin`. #41 & #42 **blocked-by #43**.
- **Session repo:** `main`, clean, unchanged (no code touched this session).
- **Content repo:** 3 new `issues/*.md` drafts left **uncommitted** (Author's to
publish/discard — INV-8). No commits/pushes made to it.
## Operator plate (loose ends)
- The pin-baseline UI-reachability gap is captured only *inside* #43's scope
(operator chose to fold it in, not file a standalone bug).
- #41/#42 carry no comment cross-linking #43; the dependency link is recorded in
Gitea instead. (Offered to add comments; not requested.)
- Pre-existing untracked drafts from earlier capture sessions still sit in the
content repo working tree (not this session's concern).
## Deferred decisions
_None — all sizing/priority/relationship calls were made with the operator in the
moment (sizing of #42 as feature, #43 coexist-vs-supersede and P1, folding the
pin gap into #43)._
## Next-session prompt
```
/wgl-brainstorming design #43 — preview toolbar as the primary interaction surface
(adaptive "Ask Claude…" button: Edit Selection ⇆ Edit Document by preview-selection
state; Pin baseline button; annotations checkbox stays). Central design problem:
map a selection in the rendered (markdown-it) preview back to a source markdown
range; fallback is ship document-level Ask-Claude + Pin first, selection-aware
second. Coexists with #41/#42 (native right-click entries become gateways that
open the preview). Builds on F7/F10 preview + annotations toggle, F3/F4 inner
loop, F6 baseline store (gives orphaned cowriting.pinDiffBaseline a UI home).
```
@@ -0,0 +1,105 @@
# Session 0036.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T11-01 (PST)
> End: 2026-06-12T12-18 (PST)
> Type: brainstorming
> Status: **FINALIZED**
## Launch prompt
`/goal next` — resume the next recorded goal. Memory (session 0035) recorded
**Next /goal = design #43**: write the Solution Design for Feature #43 (F11) —
the preview toolbar as the primary interaction surface (adaptive Ask-Claude +
Pin-baseline buttons).
## Summary
A brainstorming session that produced and graduated the **F11 Solution Design**
for Feature `#43`: making the F10 rendered-review preview's header toolbar the
**primary interaction surface**. Beside the existing annotations on/off checkbox,
the toolbar gains a **Pin baseline** button and a **single adaptive "Ask Claude…"
button** (Edit Selection when text is selected in the preview / Edit Document
otherwise), plus a minimal right-click → Open Review Preview gateway.
The session opened by classifying `/goal next` → brainstorming, claiming session
ID **0036** (`--type brainstorming`), and verifying a clean pushed `main`. It then
oriented from the #43 issue (read via the Gitea API) and the F10 interactive-review
spec (the architecture F11 extends), and dispatched an Explore agent to produce a
precise code map of the preview webview, the markdown-it render path, the F4 edit
seam, `pinDiffBaseline`, and the #41/#42 menu state.
## Pre-state
- `main` clean and pushed; no in-flight sessions (0035 finalized).
- F10 (`#29`) shipped: clean editor + rendered preview as the single review
surface (annotations on/off; ✓/✗ on F4 proposals). `#31` shipped: proposals
rendered inline at their resolved anchor.
- `cowriting.pinDiffBaseline` registered but `when:false` (orphaned since `#34`).
- Whole-document editing did not exist; "Edit Selection" was an editor
context-menu item only.
## Turn-by-turn arc
1. **Classify + claim.** `/goal next` → resume "design #43" → brainstorming.
Claimed ID 0036; resolved app facts (content repo present; roadmap = this repo;
no BDD corpus); confirmed clean baseline.
2. **Orient.** Read issue `#43` (Gitea API, P1/feature), the F10 spec
(`coauthoring-interactive-review.md`), and prior content-repo specs/plans. Ran
an Explore agent → code map: `trackChangesPreview.ts` (webview + annotations
toggle round-trip), `trackChangesModel.ts` (markdown-it; `splitBlocksWithRanges`
`BlockWithRange` char offsets; **no** source-line mapping in rendered HTML),
`extension.ts editSelection` + `proposalController.propose` + F4 `applyAgentEdit`
seam, `diffViewController.pinDiffBaseline` (`when:false`), and the absence of
#41/#42 menu entries.
3. **Surface the forks.** Identified the central design risk — mapping a *rendered
preview* selection back to a *source markdown range* — and three load-bearing
forks. Asked the operator (AskUserQuestion) with recommendations.
4. **Forks locked** (all recommended options chosen): (1) **block-level**
selection→source mapping via `data-src` block attributes; (2) document edit
**diffed into per-hunk F4 proposals**; (3) **#43 lands a minimal right-click
gateway**.
5. **Present design + write spec.** Presented the consolidated F11 design
(INV-35/36/37, message bus, slices, testing). Wrote
`docs/superpowers/specs/2026-06-12-f11-preview-toolbar-interaction-surface.md`;
self-reviewed; verified reused seams exist (`DiffViewController.pin(document)`,
`isAuthorable(scheme)`). Committed to `main` (`b2de25f`; rebased over the
0036 placeholder push), matching the F9 direct-`design(...)`-commit precedent.
6. **Finalize.** Submitted the spec to the content repo `specs/` (graduated,
`855acec`); updated memory; published this transcript.
## Cut state (final)
- **Spec graduated:** `specs/2026-06-12-f11-preview-toolbar-interaction-surface.md`
in `vscode-cowriting-plugin-content` (`855acec`); code-repo working copy at
`docs/superpowers/specs/` (`b2de25f` on `main`).
- `main` clean and pushed. No open PRs (Gitea host — no `gh` PR flow).
- Memory: added `f11-preview-toolbar-spec-graduated.md` + MEMORY.md pointer.
## Operator plate (decisions & deferrals)
- **Three forks** locked interactively (spec §6.7) — see Summary.
- **Autonomous low-confidence calls (spec §9, all low-risk):** host `showInputBox`
for the instruction (keeps LLM/secrets out of the sealed webview); Pin targets
the previewed document via `DiffViewController.pin` (not `activeTextEditor`); new
`cowriting.editDocument` command for #42 reuse; `cowriting.pinDiffBaseline`
unhidden (`when: editorLangId == markdown`).
- **Deferred (spec §9):** char-precise sub-block selection mapping; richer #41/#42
menu sets; scroll-sync (#32); large-rewrite hunk capping; repo rename (#35).
## Deferred decisions
The autonomous calls above (spec §9) were surfaced at closeout: instruction-prompt
location, Pin target, the new `editDocument` command, and unhiding the pin command.
All are low-risk and reversible; none changed the persisted model. No other
low-confidence calls.
## Next-session prompt
```
/goal plan-and-execute #43 (F11), per specs/2026-06-12-f11-preview-toolbar-interaction-surface.md
```
Start with **SLICE-1** (Pin baseline button + unhide `cowriting.pinDiffBaseline`)
— the immediate win that homes the orphaned command. Read
`f11-preview-toolbar-spec-graduated.md` and the graduated spec's §6/§7 first.
@@ -0,0 +1,122 @@
# Session 0037.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T13-33 (PST)
> End: 2026-06-12T14-22 (PST)
> Type: planning-and-executing
> Posture: autonomous (yolo)
> Status: **FINALIZED.**
>
> Outcome: **F11 (#43) shipped to main via PR #44.** The rendered review preview's
> header toolbar is now the primary interaction surface — Pin baseline + a single
> adaptive Ask-Claude button (Edit Selection ⇆ Edit Document) routing through the
> existing F4/F6/F3 machinery, block-level selection→source mapping, document
> edits diffed into per-hunk proposals, an `editor/title` gateway, and the
> unhidden `pinDiffBaseline`. 208 unit + 9/9 F11 host E2E green. Issue #43 closed;
> branch deleted; plan archived to the content repo.
## Launch prompt
```
plan-and-execute #43 (F11), per specs/2026-06-12-f11-preview-toolbar-interaction-surface.md. Start with SLICE-1 (Pin baseline button + unhide pinDiffBaseline) — the immediate win.
```
## Plan
Plan-and-execute **F11 (#43)** — preview toolbar as the primary interaction
surface — per `docs/superpowers/specs/2026-06-12-f11-preview-toolbar-interaction-surface.md`.
Implementation plan archived at `docs/superpowers/plans/2026-06-12-f11-preview-toolbar.md`.
Posture: autonomous (yolo). Branch `f11-preview-toolbar`.
Five slices (spec §7.2), each green (unit + host E2E) before the next:
- **SLICE-1 — Pin baseline button + reachability** *(this session's lead — the immediate win)*: webview Pin button → `{pinBaseline}` → host `DiffViewController.pin(previewedDoc)`; unhide `cowriting.pinDiffBaseline` (`when: editorLangId == markdown`).
- **SLICE-2 — Block-offset emission** (INV-36 data layer): pure `data-src-start/end` on every block, both render modes.
- **SLICE-3 — Edit Document + hunk path** (INV-37 doc half): `diffToHunks`, `runEditAndPropose`, `cowriting.editDocument`.
- **SLICE-4 — Adaptive Edit Selection** (INV-37 selection half): `selectionchange` label flip + selection→`data-src` resolution → single-range propose.
- **SLICE-5 — Gateway, edges, tests & docs**: `editor/title` gateway, non-authorable disabling, MANUAL-SMOKE-F11.md, README.
## Session arc
Pre-state: clean `main` at `b2de25f` (after a fast-forward of 2; the F11 spec had
just graduated in session 0036). #43 = `type/feature` with a graduated Solution
Design → eligible planning-and-executing anchor (R2/R3).
1. **Claim + setup.** Session gate → `wgl-planning-and-executing`; claimed ID
**0037** (no concurrent sessions). Read the F11 spec (found at
`docs/superpowers/specs/`, not `specs/`). Wrote the implementation plan
(`docs/superpowers/plans/2026-06-12-f11-preview-toolbar.md`), branched
`f11-preview-toolbar`. Baseline 197 unit green.
2. **SLICE-1 (commit 8b9e61a).** TDD: failing E2E (`receiveMessage` seam absent) →
extracted `handleWebviewMessage`, added the `pinBaseline` intent →
`DiffViewController.pin(previewedDoc)`, the Pin button, theme CSS, unhid
`pinDiffBaseline`. Discovered the `undoMarks` E2E flakes (passed on re-run).
3. **SLICE-2 (1ef9451).** Pure `srcAttr` helper threaded through renderOp /
renderReviewOp / renderReview; `renderPlain` switched to per-block bare divs.
data-src on every live block in both modes; removed/proposal blocks carry none.
4. **SLICE-3 (0d1a563).** Pure `diffToHunks` (word-level, coalescing); host
`runEditAndPropose` + injectable `editTurn`/`setEditTurnForTest`; the askClaude
message + `cowriting.editDocument`; Edit Document button.
5. **SLICE-4 (03b61ed).** Webview adaptive label (`selectionchange`) +
`nearestSrc`/`selectionSrcRange` block-union mapping; host range branch
(already shared from SLICE-3). E2E for the selection path.
6. **SLICE-5 (1564ef5).** `editor/title` gateway; `authorable` render flag +
`editControlsEnabled` seam + webview disable; `MANUAL-SMOKE-F11.md`; README.
7. **Self code review (subagent) → fixes (47cc733).** Caught a **Critical**:
pure-insertion hunks were born-orphaned (verified against `anchorer.resolve`).
Fixed with `anchorInsertion`; added reconstruct + accept-all E2E coverage;
added `turnId`; documented the renderPlain cross-block tradeoff + a
characterization test. Ran the isolation experiment proving the `undoMarks`
flake is F11-independent.
8. **Ship.** Pushed; PR **#44** (Gitea API) → merged to `main` (62a2229); #43
commented + closed; branch deleted; 208 unit green on main.
Cut state: on `main`, clean, fully pushed. The only red in the host E2E suite is
the pre-existing `undoMarks` flake (see Deferred decisions); all 9 F11 E2E + 208
unit are green.
## Next /goal
```
/goal plan-and-execute #42 then #41 (now unblocked by F11) — expand the right-click menu sets into the preview (Ask Claude to Edit + Open Review Panel), per spec §6.7 fork 3; first capture the pre-existing undoMarks E2E flake as a follow-up bug.
```
## Deferred decisions
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Appended as the session runs; surfaced at
finalize. Empty if none._
- **Pre-existing E2E flake — PROVEN F11-independent, recommend a follow-up issue:**
`undoMarks.test.ts` ("F10 #38 — undo of a deletion of baseline text leaves it
unattributed") fails on `executeCommand("undo")` not reverting the
`applyEdit`-applied changes. Early in the session it failed ~1/3 (passing on
re-run); later in the session it failed consistently (machine-load sensitive).
**Isolation experiment:** removed all F11 tests, clean rebuild → the undo test
still fails identically (44 passing, same `undo restored 'bravo'` at the same
line). So it is NOT caused/aggravated by F11 — F11 touches only preview message
routing + the pure render layer. Tried a focus+single-undo+poll hardening; it
did NOT fix this run (root cause is `undo` not reverting programmatic
`WorkspaceEdit`s in test-electron, not mere timing), so I reverted it to keep
the F11 PR scoped. **Recommend a dedicated follow-up issue:** redesign the undo
test to not depend on `executeCommand("undo")` over `applyEdit` (e.g. drive the
attribution branch directly, or type real edits). All 9 F11 E2E tests are green.
- **SLICE-2 off-mode wrapping (autonomous call):** `renderPlain` now wraps each
block in a bare `<div data-src-start/end>` (no `cw-` class) rather than
rendering the whole document in one markdown pass. This makes the off/clean
preview a selection→source surface (INV-36) and matches `renderReview`'s
long-standing per-block rendering. Trade-off (raised in code review): cross-block
markdown constructs separated by blank lines (reference-link defs, footnotes)
don't resolve in off-mode — a regression for off-mode specifically, but it makes
both modes consistent (on-mode already had it). Inherent to the operator-locked
block-level mapping (§6.7). Kept per INV-36 ("both modes"); documented with a
docstring note + a characterization test. A cross-block-fidelity improvement
(markdown-it source maps) is a possible follow-up, not a change to the locked
decision.
- **Code review run (self, via subagent) before merge:** found 1 Critical (pure-
insertion hunks born-orphaned — could never be accepted), 2 Important (the
cross-block renderPlain regression above; sequential multi-hunk accept untested),
3 Minor (no turnId; test-seam recompute; webview disable not auto-tested). Fixed
the Critical (anchorInsertion + reconstruct/accept-all tests), the turnId, and
added accept-all + characterization coverage; accepted/documented the cross-block
tradeoff. Commit `47cc733`.
@@ -0,0 +1,88 @@
# Session 0038.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T15-55 (PST)
> End: 2026-06-12T16-06 (PST)
> Type: planning-and-executing
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Pre-state
`main` clean and pushed (HEAD `21df670`, behind origin by 1 — fast-forwarded to
`de53305` the session-claim commit). No in-flight sessions. Open backlog: #42
(feature, Ask Claude to Edit Document), #41 (story, right-click → Open Review
Panel); both filed in capture session 0035 as F11 follow-ups.
## Launch prompt
`/goal next` → stored goal "plan-and-execute #42 (Ask Claude to Edit Document),
then #41" (from session 0037 finalize).
## Plan
**Gate decision (§4.3 R3):** #42 is `type/feature` with no Solution Design — an
ineligible anchor for plan-and-execute. Operator chose: **plan-and-execute #41
now (eligible story); brainstorm #42's design next.** So this session does #41
only; #42's design becomes the next goal.
**#41 — Open Cowriting Review Panel from the markdown file/tab right-click menu**
(story, P2):
- `explorer/context` menu item for `.md``cowriting.showTrackChangesPreview`.
- `editor/title/context` menu item for markdown tabs → same command.
- Command accepts the right-clicked resource URI (open clicked doc, not just
active editor).
- Menu label reads "Open Cowriting Review Panel" (least-churn option).
- E2E coverage: menu contributed + opens preview.
### Outcome
**#41 SHIPPED** to main (PR #45, merged + branch deleted; issue auto-closed).
- `explorer/context` + `editor/title/context` menus (markdown-gated) →
`cowriting.showTrackChangesPreview`; command retitled "Open Cowriting Review
Panel"; command resolves the *clicked* doc (opens it if not already open),
no-arg path unchanged.
- 208 unit + 7 new E2E (`reviewPanelMenu.test.ts`); full host E2E suite green
(66 passing across both workspaces); typecheck clean.
**#42 NOT done this session** — `type/feature` with no Solution Design is an
ineligible plan-and-execute anchor (§4.3 R3). Operator chose: brainstorm #42's
design next. → **Next /goal:** brainstorm #42 (Ask Claude to Edit Document)
Solution Design, then plan-and-execute #42.
## Deferred decisions
- #42 gate (R3): asked the operator rather than auto-overriding; they chose
"#41 now, brainstorm #42 next." Not low-confidence — recorded for the
finalize report.
- Pre-existing F11-independent `undoMarks` E2E flake noted in session 0037 still
uncaptured as a follow-up issue (out of scope for #41).
## Session arc
1. Claimed session 0038 (peek → no in-flight → atomic push). Fast-forwarded
`main`. Fetched #42 + #41 from the Gitea API.
2. Ran the §4.3 issue-type gate: #42 (feature, no design) ineligible for
plan-and-execute; #41 (story) eligible. Surfaced the fork; operator chose
"#41 now, brainstorm #42 next."
3. Implemented #41: read `trackChangesPreview.ts` (the `showTrackChangesPreview`
command already took an optional `uri` but only searched *open* docs) +
`package.json` menus. Fixed the command to open the clicked doc; added
`explorer/context` + `editor/title/context`; retitled the command; updated
README.
4. Wrote `reviewPanelMenu.test.ts` (7 E2E). Typecheck + 208 unit + full host
E2E green.
5. Branch → commit → push → PR #45 → merged (branch deleted); #41 auto-closed.
6. Finalized: memory updated, transcript published.
## Next-session prompt
```
/goal brainstorm #42 (Ask Claude to Edit Document) Solution Design, then plan-and-execute #42
```
**Next /goal:** brainstorm #42 (Ask Claude to Edit Document) Solution Design,
then plan-and-execute #42. Design forks to settle: whole-doc instruction
gathering; one large proposal vs several; large-document token-limit behavior.
`cowriting.editDocument` + `runEditAndPropose(doc, {kind:"document"})` already
exist (F11 prep) — #42 is mostly menu/tab wiring + those decisions on top.
@@ -0,0 +1,74 @@
# Session 0039.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T16-07 (PST)
> End: 2026-06-12T17-02 (PST)
> Type: capture (tracked-lite)
> Posture: careful (capture default)
> Status: **FINALIZED**
## Launch prompt
`/wgl-capture We need a way to accept all of Claude's edits`
## Pre-state
Opened immediately after finalizing session 0038. `main` clean + pushed.
Backlog open: #42 (feature, Ask Claude to Edit Document). No existing
accept-all draft in the content repo.
## What was captured
**One ask → one issue filed.**
**#46** — `type/feature`, `priority/P2`
"Accept all of Claude's pending proposals on the current document in one gesture."
https://git.benstull.org/benstull/vscode-cowriting-plugin/issues/46
Confirmed gap from the code: accept is **per-proposal only** today
(`cowriting.acceptProposal``ProposalController.acceptById`; F10/F11 preview ✓
per `cw-proposal` block). No bulk accept. Felt most after F11 "Edit Document"
(#42), where one whole-document rewrite fans out into N per-hunk proposals.
## Decisions (capture forks)
- **Scope of "all" = all pending proposals on the current document** (operator
choice) — not per-turn, not cross-document. Safe apply-order; orphaned /
unresolved proposals **skipped + reported**, never force-applied.
- **Type = `type/feature`** (operator choice). The load-bearing fork is
apply-order + re-anchoring: each accept mutates the buffer and re-resolves the
remaining proposals' anchors, so bulk-accept needs a correct order /
re-resolve-between strategy. Design step also settles confirmation y/n, button
placement (preview header vs F11 toolbar), and applied-vs-skipped surfacing.
- Non-goals recorded: no reject-all, no cross-document/workspace accept-all, no
per-turn scope, no new accept seam (reuse F4 `acceptById` in a loop; webview
posts an `acceptAll` intent — sealed-webview INV-21/35).
## Session arc
1. Claimed tracked-lite session 0039 (`--type capture`); fast-forwarded `main`.
2. Resolved the content repo (`vscode-cowriting-plugin-content`); cloned + listed
existing drafts (no accept-all duplicate).
3. Inspected the code's accept surface → confirmed per-proposal-only, no bulk.
4. Reflected the ask; asked two scope/sizing questions → current-doc + feature.
5. Drafted `issues/accept-all-proposals.md` (full §5 feature template).
6. Operator approved → ensured labels → filed **#46**.
7. Finalized: memory updated, transcript published. Draft left uncommitted in the
content-repo working tree (INV-8 — Author to publish/discard).
## Deferred decisions
- Capture scripts 401 with the default token service; needed
`WGL_CAPTURE_TOKEN_SERVICE=wgl-gitea-issues-readwrite-token-git.benstull.org`
(host-specific). Worth a plugin-feedback follow-up if it recurs.
## Next-session prompt
Unchanged from 0038 — the next coding goal stands:
```
/goal brainstorm #42 (Ask Claude to Edit Document) Solution Design, then plan-and-execute #42
```
#46 (accept-all) is now in the backlog alongside #42; either could be the next
brainstorming target.
@@ -0,0 +1,81 @@
# Session 0040.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T17-03 (PST)
> End: 2026-06-12T17-21 (PST)
> Type: capture (tracked-lite)
> Posture: careful (capture default)
> Status: **FINALIZED**
## Launch prompt
`/wgl-capture When Claude suggests changes, a single change should be for a
paragraph (or header, or bulletpoint), not individual words. It's too much to
review`
## Pre-state
Third session of the run (after 0038 ship + 0039 capture). `main` clean +
pushed. Backlog: #42 (Edit Document), #46 (accept-all, filed 0039).
## What was captured
**One ask → one issue filed.**
**#47** — `type/feature`, `priority/P1`
"Block-granularity proposals: one change per paragraph/header/bullet, not per word."
https://git.benstull.org/benstull/vscode-cowriting-plugin/issues/47
Confirmed from code: F11 **Ask Claude to Edit Document** mints proposals at **word
granularity** — `diffToHunks` (src/trackChangesModel.ts) uses `diffWordsWithSpace`
and flushes a hunk on every unchanged token, so each changed word is its own ✓/✗
proposal (INV-37 "per-hunk"). A light copy-edit pass explodes into dozens of tiny
proposals → "too much to review."
## Decisions (capture forks)
- **Scope = document edits only** (operator choice) — the `diffToHunks` fan-out.
Selection edits are already a single proposal, so untouched.
- **Type = `type/feature`** (operator choice). Supersedes F11 **INV-37 per-hunk →
per-block**. Real forks recorded for the design step: block taxonomy (list items
vs whole lists, tables, blockquotes, nesting); anchoring inserted blocks (a
block-level analogue of today's word-oriented `anchorInsertion`); and the
**attribution tradeoff** — accepting a block attributes the *whole* block to
Claude, including unchanged words.
- Reuse the existing block splitter (`diffBlocks`; code/mermaid fences atomic,
INV-23). Intra-block `<ins>`/`<del>` **rendering is unchanged** — only the
decision *unit* becomes the block. `priority/P1` (directly removes the stated
pain).
## Session arc
1. Claimed tracked-lite session 0040 (`--type capture`); fast-forwarded `main`.
2. Inspected `diffToHunks` → confirmed word-level granularity is the cause.
3. Reflected the ask; asked scope + sizing → document-edits + feature.
4. Drafted `issues/block-granularity-proposals.md` (full §5 feature template).
5. Operator approved → filed **#47** (labels already ensured this run).
6. Finalized: memory updated, transcript published. Draft left uncommitted in the
content-repo working tree (INV-8).
## Deferred decisions
_None._
## Backlog observation
#42 (Ask Claude to Edit Document), #46 (accept-all), and #47 (block granularity)
form one **edit-flow cluster** around the Ask-Claude / document-edit loop. They
interact (block granularity changes how many proposals exist; accept-all changes
how they're taken; #42 is the entry point that produces them) and are best
**brainstormed / sequenced together** rather than designed in isolation.
## Next-session prompt
The standing coding goal is unchanged; consider folding the cluster in:
```
/goal brainstorm the Ask-Claude document-edit cluster (#42 Edit Document, #47 block-granularity proposals, #46 accept-all) and sequence/design it, starting with #42
```
(Or keep the narrower `/goal brainstorm #42 … then plan-and-execute #42` and take
#47/#46 after.)
@@ -0,0 +1,83 @@
# Session 0041.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T18-17 (PST) · End: 2026-06-13T07-16 (PST)
> Type: brainstorming
> Posture: autonomous (yolo)
> Goal: brainstorm the Ask-Claude document-edit cluster (#42, #47, #46) and
> sequence/design it, starting with #42
> Outcome: **GRADUATED** — one combined Solution Design
> `specs/coauthoring-document-edit-flow.md` (content repo, `status: graduated`,
> commit `9d016e8`), sequencing #42 → #47 → #46.
## Launch prompt
`/goal brainstorm the Ask-Claude document-edit cluster (#42, #47, #46) and
sequence/design it, starting with #42`
## Plan
Brainstorming session — produce a Solution Design for the **Ask-Claude
document-edit flow**, the cluster of #42 (Ask Claude to Edit Document — entry
points), #47 (block-granularity proposals), #46 (accept-all). Deliverable: one
combined Solution Design (content repo `specs/`), sequencing the three as a
delivery plan **#42 (reach) → #47 (review) → #46 (accept)**.
## Arc
1. **Init.** Claimed session 0041 (`--type brainstorming`), clean pushed `main`
baseline, seeded the Plan block. Resolved app repos (roadmap = framework repo;
content = `vscode-cowriting-plugin-content`; no BDD corpus).
2. **Read the cluster.** Read the three capture drafts (#42/#47/#46) in the
content repo `issues/`. Then ground in code: `package.json` menus/commands,
`trackChangesModel.ts` (`splitBlocks`/`diffBlocks`/`diffToHunks`/
`anchorInsertion`/`renderReview`), `trackChangesPreview.ts`
(`editDocument`/`askClaude`/`runEditAndPropose`), `proposalController.ts`
(`acceptById`/`accept``applyAgentEdit`/`listProposals`).
3. **Key reframe.** F11 (session 0037) **already shipped** the document-edit path
`cowriting.editDocument``runEditAndPropose({kind:"document"})`
`diffToHunks` (word-level, INV-37). So #42 is mostly DONE (remaining = menu
wiring); #47 swaps the document-branch diff to block-level; #46 loops the
existing accept seam.
4. **Found a spec-hygiene gap.** The F11 (#43) Solution Design was never graduated
to `specs/` — INV-35/36/37 live only in code comments + the F11 issue draft.
Logged as OQ-2 (follow-up), restated the depended-on F11 invariants in the new
spec.
5. **Locked two forks with the operator** (AskUserQuestion):
- **D-1** — per-block proposals **preserve unchanged-span attribution** (block =
decision unit, word = attribution unit; intra-block `diffToHunks` sub-diff at
accept; INV-40). *Not* whole-block→Claude.
- **D-2** — ship as **one combined Solution Design**, not three.
6. **Wrote the spec.** `specs/coauthoring-document-edit-flow.md` — business →
product → engineering, new invariants INV-38..42 (INV-39 supersedes INV-37),
delivery plan SLICE-1/2/3 = #42/#47/#46, traceability, decisions log. Self-
reviewed (no placeholders, consistent, scoped). Did **not** invoke
`writing-plans` (wgl-brainstorming override — the artifact is the spec,
submitted at finalize; implementation is a downstream session).
7. **Finalize.** Submitted the spec `--status graduated`; memory updated.
## Deferred decisions
- **D-3 (low-confidence driver call)** — sequenced **#42#47#46** per the
goal's "starting with #42," **despite #47 carrying the P1 value** ("too much to
review" is the sharpest pain). The three are code-independent so the order is
reversible (§7.1); #47 and #42 could swap with no rework if the operator wants
the P1 review fix to land first. Surfaced at closeout; logged as D-3 in the
spec §9.
- D-4 (driver) block taxonomy = exactly `splitBlocks`'s units; D-5 (driver)
accept-all needs no confirmation, button shown ≥2 pending. Both in spec §9.
## Loose ends → next session
- **OQ-2** — graduate the F11 (#43) spec to `specs/` for a clean invariant ledger
(INV-35/36/37 currently code-only).
- **OQ-1** — whether selection edits should also preserve-unchanged-span
attribution (out of #47 scope).
- Content-repo local working tree carries pre-existing untracked drafts/specs
(prior sessions / the Author's) — not landed by this session.
## Next /goal
```
/goal plan-and-execute #42 (SLICE-1 of the document-edit flow — Ask-Claude entry-point menu wiring), per specs/coauthoring-document-edit-flow.md
```
@@ -0,0 +1,62 @@
# Session 0042.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T21-04 (PST) · End: 2026-06-13T07-16 (PST)
> Type: capture (tracked-lite)
> Posture: autonomous (yolo)
> Goal: capture "When I pin the baseline, there should be no annotations in the
> review panel"
> Outcome: filed **#48** (`type/story`, `priority/P2`).
## Launch prompt
`/wgl-capture When I pin the baseline, there should be no annotations in the
review panel`
Opened mid-session, concurrent with the in-flight brainstorming session 0041
(only that — my own — session in flight; warned + proceeded per autonomous).
## Arc
1. **Claim.** Claimed session 0042 (`--type capture`, tracked-lite). App +
content repo already resolved from 0041.
2. **Ground the behavior.** Read `renderReview`/`colorByAuthor`/`renderReviewOp`
in `trackChangesModel.ts`: an **unchanged** block renders as
`cw-blk cw-unchanged` with `colorByAuthor` applied (line ~526). So after a Pin
baseline (baseline == current → every block unchanged), the change axis is
empty but **F3 authorship coloring still paints every block** — and the panel
*does* refresh on pin (`onDidChangeBaseline`), so it's not a stale render. The
author colors are the "annotations."
3. **Scope fork (AskUserQuestion).** Operator chose **pin → fully clean panel**
(zero-diff state: change marks AND authorship colors gone), **not** the broader
"authorship never colors unchanged text." → a single, design-free increment.
4. **Type.** Would be `type/bug`, but `capture-issues.sh` accepts only
epic|feature|story|task (the gap plugin feedback #124 tracks — not re-filed).
Filed as the accurate fileable type `type/story`, P2.
5. **Draft + approval + file.** Authored
`issues/pin-baseline-clears-review-annotations.md`; operator **approved**;
ensured labels (`--host git.benstull.org`,
`WGL_CAPTURE_TOKEN_SERVICE=wgl-gitea-issues-readwrite-token-git.benstull.org`);
filed **#48** —
https://git.benstull.org/benstull/vscode-cowriting-plugin/issues/48
## Deferred decisions
- **Type compromise (driver, tooling-forced)** — filed as `type/story` though the
observation is bug-flavored (surprising behavior on a primary gesture);
`capture-issues.sh` can't type `bug`. The bug-typing gap is already tracked as
plugin feedback **#124**; not re-filed.
## Loose ends
- Draft `issues/pin-baseline-clears-review-annotations.md` left **uncommitted** in
the content repo working tree (the Author's to publish or discard — capture
INV-8).
## Next /goal
(Continuation is the 0041 handoff.)
```
/goal plan-and-execute #42 (SLICE-1 of the document-edit flow — Ask-Claude entry-point menu wiring), per specs/coauthoring-document-edit-flow.md
```
@@ -0,0 +1,92 @@
# Session 0043.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-13T07-18 (PST)
> Type: planning-and-executing
> End: 2026-06-13T07-38 (PST)
> Status: **FINALIZED.**
> Posture: autonomous (yolo)
## Launch prompt
`/goal plan-and-execute #42 (SLICE-1 of the document-edit flow — Ask-Claude
entry-point menu wiring), per specs/coauthoring-document-edit-flow.md`
## Plan
**SLICE-1 — #42 (reach)** of the document-edit flow Solution Design
(`specs/coauthoring-document-edit-flow.md`, §7.2). Anchor: Feature #42
(`type/feature`); design graduated (session 0041) → §4.3 R3 satisfied.
Goal (INV-38): "Ask Claude to Edit" reachable from the editor **body** and the
editor **tab**, selection-aware (selection → `editSelection`; no selection →
`editDocument`), both gated to markdown/authorable docs, both routing through the
single `runEditAndPropose` path. Smallest increment — command already exists.
Tasks:
1. `package.json` — add `editSelection` + `editDocument` to `editor/context`
(selection-aware `when`, markdown+authorable) and `editor/title/context`
(selection-aware, `resourceLangId == markdown`); keep titles.
2. `trackChangesPreview.ts``cowriting.editDocument` accepts an optional tab
`uri` arg (mirror `showTrackChangesPreview`'s #41 resolution): open/resolve the
clicked doc, else fall back to the active editor.
3. E2E (`test/e2e/suite/`) — menu entries present + selection-aware + markdown-gated;
`editDocument(uri)` resolves the tab doc and produces a document-scoped proposal.
No model change, no new persisted artifact. No deploy pipeline (VS Code extension).
## Results
**SLICE-1 (#42, reach) shipped to `main`** — PR
[#49](https://git.benstull.org/benstull/vscode-cowriting-plugin/pulls/49)
(merged), issue #42 closed.
- `package.json``editSelection` + `editDocument` added to `editor/context`
(selection-aware, `editorLangId == markdown` + `file`/`untitled`) and
`editor/title/context` (selection-aware, `resourceLangId == markdown`);
existing `editor/context` `editSelection` entry markdown-gated to match INV-38.
- `src/trackChangesPreview.ts``cowriting.editDocument` now accepts the clicked
tab's resource `Uri` (opens it if needed), falling back to the active editor;
mirrors `showTrackChangesPreview`'s #41 resolution.
- `test/e2e/suite/f12Reach.test.ts` — 4 new host E2E (menu wiring declarative +
tab-URI targeting + no-arg fallback).
- `docs/MANUAL-SMOKE-F12.md` — SLICE-1 reach smoke steps.
Verification: `tsc --noEmit` clean; **208 unit** green; **65/5 host E2E** green
(main suite up from 61 → 65 with the 4 new tests).
**Next:** SLICE-2 — #47 (review, **P1**): document edits propose per changed
block (`diffToBlockHunks`, INV-39 supersedes INV-37; word-precise intra-block
attribution INV-40; block-insertion anchoring INV-41). Then SLICE-3 — #46
(accept-all, INV-42).
## Session arc
1. **Gate + claim.** Classified the `/goal` as planning-and-executing; claimed
session 0043 (peek showed nothing in flight). Baseline: local `main` was 4
behind `origin/main` → fast-forwarded clean.
2. **Anchor gate (§4.3 R3).** #42 is `type/feature`; its design is the graduated
combined Solution Design `specs/coauthoring-document-edit-flow.md` (session
0041) → R3 satisfied, proceed.
3. **Read the spec**, scoped SLICE-1 (reach). Explored the code: `package.json`
menus, `editDocument`/`editSelection` handlers, the F11 E2E harness (`setEditTurnForTest`).
4. **TDD.** Wrote `f12Reach.test.ts` first (red), then wired the menus + tab-URI
resolution (green).
5. **Verified** (tsc clean, 208 unit, 65/5 E2E), self-reviewed the diff, shipped
via PR #49 (merged), closed #42, updated memory, checkpoint-published the
transcript.
## Deferred decisions
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Appended as the session runs; surfaced at
finalize. Empty if none._
- **Markdown-gating `editSelection`'s `editor/context` entry** (driver call,
low-confidence). Spec §5/INV-38 say both Ask-Claude entries are markdown-gated,
but F8 made the `editSelection` *command* work on any authorable doc and its
current right-click entry has no `editorLangId == markdown` gate. Followed the
spec: gated the **menu** entries to markdown while leaving the command handlers'
behavior intact (palette still works on any authorable file). Removes the
body right-click Ask-Claude-Edit-Selection on non-markdown files — acceptable
since the proposal review surface (F10 preview) is markdown-only.
@@ -0,0 +1,127 @@
# Session 0044.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-13T07-40 (PST)
> Type: planning-and-executing
> End: 2026-06-13T08-04 (PST)
> Status: **FINALIZED.**
> Posture: autonomous (yolo)
## Launch prompt
```
/goal plan-and-execute #47 (SLICE-2 of the document-edit flow — document edits propose per changed block: diffToBlockHunks INV-39 supersedes INV-37, word-precise intra-block attribution INV-40, block-insertion anchoring INV-41), per specs/coauthoring-document-edit-flow.md
```
## Plan
**SLICE-2 — #47 (review, P1)** of the document-edit flow
(`specs/coauthoring-document-edit-flow.md` §7.2). Anchor: Feature #47
(`type/feature`); covered by the graduated combined Solution Design → §4.3 R3.
Document rewrites propose **one F4 proposal per changed block** (the unit a human
reviews), but each accept reconciles attribution at **word** granularity (the unit
F3 records). INV-39 supersedes INV-37 for document edits; INV-40 word-precise
intra-block attribution; INV-41 block-insertion anchoring. Selection edits
unchanged.
Tasks:
1. `model.ts` — add optional `granularity?: "block" | "single"` to `Proposal`
(additive, back-compat: absent ⇒ `"single"`). INV-39/§6.3.
2. `proposalModel.ts``addProposal` opts gains `granularity`, threaded onto the
proposal (omitted when absent).
3. `trackChangesModel.ts` — new pure `diffToBlockHunks(currentText,
rewrittenText): EditHunk[]`: split both sides into the existing block units
(`splitBlocksWithRanges` for source ranges + `splitBlocks` keys), diff blocks
(reuse `diffArrays` keying like `diffBlocks`), emit ONE `EditHunk` per
changed/added/removed block — changed → `[block.start,end)`→rewrite; fences
atomic (whole-fence hunk); inserted → anchored to adjacent boundary (INV-41,
reuse the `anchorInsertion` idea); unchanged → none. Same `EditHunk` shape as
`diffToHunks` (which is RETAINED as the intra-block sub-diff engine).
4. `trackChangesPreview.ts` — `runEditAndPropose` **document branch** uses
`diffToBlockHunks` and tags each minted proposal `granularity:"block"`.
5. `proposalController.ts` — `accept`: when `granularity === "block"`, re-resolve
the block text, `diffToHunks(blockText, replacement)` → per-run word sub-diff,
apply **one `applyAgentEdit` per changed run, descending offset** so only the
runs Claude changed land Claude-attributed; unchanged spans keep prior author
(INV-40). Non-block keeps the single `applyAgentEdit`.
6. Tests: unit (`diffToBlockHunks` + INV-40 controller) + host E2E (#47 cases in
§6.8); update the existing f11 document-path E2E (now per-block, supersedes
INV-37 word-level expectation); `docs/MANUAL-SMOKE-F12.md` §2.
**Design note (seam constraint):** `pendingEdits.matchEvent` resolves ONE
registration per change event, so INV-40's per-run attribution is implemented as
**sequential** `applyAgentEdit` calls (descending offset), not a single
multi-replace `WorkspaceEdit`. The spec's "one undo-grouped edit" wording is thus
approximated as N undo steps per block accept — see Deferred decisions.
No new persisted artifact; no deploy pipeline (VS Code extension).
## Results
**SLICE-2 (#47, review, P1) shipped to `main`** — PR
[#50](https://git.benstull.org/benstull/vscode-cowriting-plugin/pulls/50)
(merged), issue #47 closed.
- `trackChangesModel.ts` — new pure `diffToBlockHunks` (block-key alignment via
`diffArrays`/`diffBlocks` keying; isolated changed block → block-aligned hunk →
rewritten raw; fences atomic INV-23; insert/delete → exact gap-span hunk;
zero-width gap-span anchored INV-41). Split `diffToHunks` into raw
`wordEditHunks` + anchoring wrapper (fixes a latent overlap bug under batch apply).
- `trackChangesPreview.ts` — document branch uses `diffToBlockHunks`, tags
`granularity:"block"`.
- `model.ts`/`proposalModel.ts` — additive optional `Proposal.granularity`.
- `proposalController.ts``acceptBlock`: intra-block word sub-diff
(`wordEditHunks`) → one `applyAgentEdit` per changed run, descending offset
(INV-40 word-precise attribution; unchanged spans keep prior author).
- Tests: `diffToBlockHunks` unit (reconstruction/fence/add-remove); `f12Review`
host E2E (M→M, unchanged→none, fence atomic, INV-40, INV-41); updated f11
document-path E2E to per-block; `MANUAL-SMOKE-F12.md` §2.
Verification: `tsc --noEmit` clean; **214 unit** green; **69/5 host E2E** green
(main suite 65 → 69 net with the new F12 review tests).
**Next:** SLICE-3 — #46 (accept-all, INV-42): `acceptAllProposals` (controller,
descending-order, orphan-skip + report) + `cowriting.acceptAllProposals` command
+ preview toolbar **Accept all** button (`{type:"acceptAll"}` intent, shown ≥2
pending).
## Session arc
1. **Claim.** Resumed via `/goal next` (after session 0043 shipped #42); claimed
session 0044 for #47. Clean `main` baseline.
2. **Anchor gate.** #47 (`type/feature`) covered by the graduated combined design
→ §4.3 R3 satisfied.
3. **Deep code read.** `splitBlocks*`/`diffBlocks`/`diffToHunks`/`EditHunk`,
`runEditAndPropose`, `proposalController.accept`/`applyAgentEdit`, and crucially
`pendingEdits.matchEvent` (one registration per change event → drove the INV-40
sequential-apply decision).
4. **TDD `diffToBlockHunks`.** First attempt (coarsen word-hunks) failed a
reconstruction case — discovered `diffToHunks` can emit OVERLAPPING hunks
(anchorInsertion over-absorbs). Switched to block-key alignment with
replacement-from-rewritten-raw → green.
5. **Model + wiring + INV-40 accept.** Added `granularity`; document branch →
`diffToBlockHunks`; `acceptBlock` intra-block sub-diff. The overlap bug then
resurfaced in `acceptBlock` (batch apply) → split `diffToHunks` into raw
`wordEditHunks` (disjoint) + anchoring wrapper; `acceptBlock` uses raw → green.
6. **Verified + shipped** (214 unit, 69/5 E2E), self-reviewed, PR #50 merged,
#47 closed, memory + transcript updated.
## Deferred decisions
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Appended as the session runs; surfaced at
finalize. Empty if none._
- **INV-40 undo granularity** (driver call). Spec §6.2/§6.7 say accepting a block
proposal should be "one undo-grouped WorkspaceEdit." But the F3 seam's
`pendingEdits.matchEvent` matches exactly ONE registration per change event, so a
single `WorkspaceEdit` with N independent run-replaces can't carry per-run
attribution (the multi-hunk event fails to match any single registration → falls
through to human). Implemented INV-40 as **sequential per-run `applyAgentEdit`
calls, descending offset** — correct word-precise attribution, reuses the seam
with zero attribution-core risk, but a block accept is N undo steps rather than
one. Single-undo-step would require extending `matchEvent`/`onDidChange` to
consume multiple registrations per event (riskier F3 change) — left as a possible
follow-up.
@@ -0,0 +1,98 @@
# Session 0045.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-13T08-04 (PST)
> Type: planning-and-executing
> End: 2026-06-13T08-17 (PST)
> Status: **FINALIZED.**
> Posture: autonomous (yolo)
## Launch prompt
```
/goal plan-and-execute #46 (SLICE-3 of the document-edit flow — accept-all, INV-42), per specs/coauthoring-document-edit-flow.md
```
## Plan
**SLICE-3 — #46 (accept)** of the document-edit flow
(`specs/coauthoring-document-edit-flow.md` §7.2, INV-42). Final slice — completes
reach→review→**accept**. Anchor: Feature #46 (`type/feature`); covered by the
graduated combined design → §4.3 R3.
A single **Accept all** gesture applies every pending proposal on the current
document through the existing F4 `acceptById` seam (block proposals take the INV-40
path automatically), in a re-anchor-safe (descending) order, **skipping** orphans
and **reporting** applied-vs-skipped. Batched application of the existing path — no
new accept mechanism; the webview posts intent only (INV-35).
Tasks:
1. `proposalController.ts` — `acceptAllProposals(document): Promise<{applied,
skipped}>`: snapshot pending, sort descending by resolved anchor start,
`acceptById` each **silently** (no per-item orphan warning), tally
applied/skipped (orphans counted skipped). Add a `silent` opt to the accept
path so the batch suppresses per-proposal warnings.
2. `extension.ts` — `cowriting.acceptAllProposals` command (active doc) → reports
applied-vs-skipped via a status message.
3. `trackChangesPreview.ts` — `ToolbarMsg += {type:"acceptAll"}`;
`handleWebviewMessage` routes it → `proposals.acceptAllProposals(document)` +
report.
4. `media/preview.ts` + `shellHtml` — an **Accept all** toolbar button shown when
`summary.proposals >= 2` (and authorable), posting `{type:"acceptAll"}`.
5. `package.json` — register the `cowriting.acceptAllProposals` command (palette,
markdown-gated).
6. Tests: host E2E (N pending → all applied + cleared; orphan skipped + reported;
button hidden < 2 pending); `MANUAL-SMOKE-F12.md` §3.
No new persisted artifact; no deploy pipeline (VS Code extension).
## Results
**SLICE-3 (#46, accept) shipped to `main`** — PR
[#51](https://git.benstull.org/benstull/vscode-cowriting-plugin/pulls/51)
(merged), issue #46 closed. **Completes the document-edit-flow cluster
(#42 reach + #47 review + #46 accept).**
- `proposalController.ts` — `acceptAllProposals(document)` → `{applied, skipped}`
(descending order, orphan-skip); `accept`/`acceptById` `silent` opt for the batch.
- `trackChangesPreview.ts` — `ToolbarMsg += {type:"acceptAll"}` → public
`acceptAll(document)` (batch + report).
- `extension.ts`/`package.json` — `cowriting.acceptAllProposals` command
(active doc, markdown-gated palette).
- `media/preview.ts` + shell — "✓✓ Accept all" toolbar button (intent; shown
≥2 pending, authorable, on-state).
- `trackChangesModel.ts` — `diffToBlockHunks` fix: emit one block-aligned hunk
per changed block **even when adjacent** (changed blocks are 1:1 anchors;
gap-spans only cover add/remove runs) — caught by the accept-all E2E (3 adjacent
changed blocks were collapsing to 1 proposal).
- `f12Accept.test.ts` E2E + `MANUAL-SMOKE-F12.md` §3.
Verification: `tsc --noEmit` clean; **214 unit** green; **73/5 host E2E** green
(main suite 69 → 73 with the new accept-all tests).
**Next:** the cluster is complete; no in-flight next step. Open backlog includes
#48 (pin → fully clean review panel, story P2) and the OQ-2 F11 (#43) spec
graduation (hygiene). A natural hand-back point for operator direction.
## Session arc
1. **Claim.** Continued the rolling `next` goal after 0044 shipped #47; claimed
session 0045 for #46. Clean `main`.
2. **Plan + implement.** Controller `acceptAllProposals` (+ `silent` accept opt);
`acceptAll` intent route + public method; command + package.json; "Accept all"
toolbar button (≥2-pending gating in the sealed webview).
3. **E2E caught a real bug.** The accept-all "3 adjacent changed blocks" case
returned 1 proposal, not 3 — `diffToBlockHunks` was merging adjacent changed
blocks into one gap-span run. Fixed by treating `changed` blocks as 1:1 anchors
(each → its own block-aligned hunk; gap-spans only span add/remove runs).
4. **Verified + shipped** (214 unit, 73/5 E2E), self-reviewed, PR #51 merged,
#46 closed → **cluster complete**; memory + transcript updated.
## Deferred decisions
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Empty if none._
- _No low-confidence calls this session._ (The `diffToBlockHunks` adjacency
behavior was a bug caught by the accept-all E2E, not a judgment call.)
@@ -0,0 +1,95 @@
# Session 0046.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-13T08-19 (PST)
> Type: planning-and-executing
> End: 2026-06-13T08-31 (PST)
> Status: **FINALIZED.**
> Posture: autonomous (yolo)
## Launch prompt
```
/goal plan-and-execute #48 (pinning the baseline leaves the review panel fully un-annotated — zero-diff → no F3 authorship colors on unchanged blocks; proposals still show)
```
## Plan
**#48 (story, P2)** — pinning the baseline leaves the review panel fully
un-annotated. Anchor: leaf `story` → §4.3 R2 (no design gate). Picked
autonomously as the next backlog item after the document-edit-flow cluster shipped
(0043/0044/0045).
When `diffBlocks(baseline, current)` yields all-`unchanged` ops (zero diff — the
state right after a pin), the F10 on-render still author-colors every block
(`colorByAuthor`), so the panel looks painted instead of clean. Fix: in that
zero-diff case render clean (no author coloring, no change marks) while keeping
`data-src` mapping (INV-36) and still injecting pending proposals (proposals are
actions, not annotations). Narrow edge case of INV-33 — with-changes render
unchanged; off-state unchanged; broader "authorship never colors unchanged" NOT
in scope.
Tasks:
1. `trackChangesModel.ts` `renderReview` — detect `ops.every(unchanged)`; in that
case use a plain `render` (skip `colorByAuthor`) for blocks; proposal injection
loop unchanged.
2. Unit: baseline==current + author spans → no `cw-by-claude`/`cw-by-human` (and
no `cw-add`/`cw-del`) on the body; with a pending proposal → the `cw-proposal`
block still renders.
3. Host E2E: open preview, diverge + author-color, pin → panel clean (no
green/blue); edit again → annotations return.
4. Content repo: one-line INV-33 clarification (zero-diff → clean on-render) in
`specs/coauthoring-interactive-review.md`.
No new persisted artifact; no deploy pipeline (VS Code extension).
## Results
**#48 (story, P2) shipped to `main`** — PR
[#52](https://git.benstull.org/benstull/vscode-cowriting-plugin/pulls/52)
(merged), issue #48 closed. Picked autonomously after the document-edit-flow
cluster (0043/0044/0045).
- `trackChangesModel.ts``renderReview` gains a `pinned` `RenderOption`; when
pinned + zero-diff, blocks render plain (skip `colorByAuthor`).
- `trackChangesPreview.ts` — pass `{ pinned: baseline?.reason === "pinned" }`
from `refresh` + `renderHtmlFor`.
- Unit (4 new) + `s48PinClean` host E2E.
**Scoped to the pin specifically** (not all zero-diff): a baseline advanced by a
**machine-landing** (accept) is also zero-diff but keeps its authorship coloring
(F10 INV-33) — the F10 authorship E2E caught a pure-zero-diff rule would regress
accepted-Claude-text coloring, so the clean render is gated on `reason ===
"pinned"`.
Verification: `tsc --noEmit` clean; **218 unit** green; **74/5 host E2E** green.
## Session arc
1. **Pick + claim.** Stop hook required determining/executing the next milestone
autonomously (rolling `next`); chose #48 (leaf story, no design gate) and
claimed 0046. Clean `main`.
2. **Read the issue** (detailed, gave the solution shape) + `renderReview`.
3. **TDD.** Implemented zero-diff-clean; full suite caught the F10 authorship
regression (accept advances baseline → zero-diff → coloring was being cleared);
re-scoped to `reason === "pinned"` via a `pinned` RenderOption → all green.
4. **Shipped** (PR #52), closed #48; spec clarification written but left in the
content-repo working tree (see Deferred decisions).
## Deferred decisions
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Empty if none._
- **INV-33 clarification not pushed to the content repo** (loose end, not a
judgment call). Wrote the zero-diff-after-pin clarification into
`specs/coauthoring-interactive-review.md`, but the **content repo has
pre-existing uncommitted state that isn't this session's**: a modified
`coauthoring-diff-view.md` and ~18 untracked capture-session draft files
(`issues/*.md`, `specs/coauthoring-document-edit-flow.md`), and local `main` is
**6 behind origin**. A clean rebase would require deleting the operator's
untracked drafts (irreversible — a STOP gate), so I **soft-reset** my spec
commit into the working tree rather than force it. The clarification sits as an
uncommitted modification alongside the operator's other content-repo drafts,
for the operator to reconcile/push. The #48 **code** shipped normally.
@@ -0,0 +1,86 @@
# Session 0047.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-13T08-34 (PST)
> Type: planning-and-executing
> End: 2026-06-13T08-44 (PST)
> Status: **FINALIZED.**
> Posture: autonomous (yolo)
## Launch prompt
```
/goal plan-and-execute #33 (intra-emphasis sentinel hardening — token-aware fix for the 2 characterized failure modes)
```
## Plan
**#33 (task, P3)** — harden the F9/F10 author-coloring PUA sentinels against
intra-emphasis markdown. Anchor: leaf `task` → §4.3 R2 (no design gate). Picked
autonomously (next clean plan-and-executable backlog item; doesn't touch the
content repo). Approach = the token-aware fix recommended in the issue #33 comment.
Two characterized failure modes (issue #33 comment, session 0032):
- **CASE1** — a span boundary lands strictly inside a delimiter run (`a**b**c`,
boundary between the two `*`): the injected sentinel splits `**` → markdown parse
breaks (stray `<em></em>`, raw `**` left).
- **CASE3** — a span boundary inside an emphasis run (`**bold**`, span covers
`**bo`): emphasis renders but the author `<span>` and `<strong>` **misnest**
(`<strong>bo</span>ld</strong>`).
Fix (both needed):
1. `injectSentinels`**clamp** any sentinel offset that lands strictly inside a
markdown delimiter run (`* _ ~ \``) to the run's start, so a sentinel never
splits a delimiter (fixes CASE1). Skip now-zero-width spans.
2. `sentinelsToSpans` — replace the naive split/join with a **token-aware walker**
over the rendered HTML: emit `cw-by-*` spans only around TEXT runs, closing the
span before any `<tag>` and reopening after, so a span never crosses an element
boundary (fixes CASE3 — one span segment per text run). Strip any stray sentinel
left inside a tag (no PUA leakage).
Pure/vscode-free/deterministic (INV-33). Non-goals: link/attribute sentinel cases,
visual language, attribution model.
Tasks: unit tests reproducing CASE1 + CASE3 (+ regression on existing
colorByAuthor/renderReview cases) → implement clamp + walker → green; spec already
notes the hardening (§1.7/§9). No deploy pipeline (extension).
## Results
**#33 (task, P3) shipped to `main`** — PR
[#53](https://git.benstull.org/benstull/vscode-cowriting-plugin/pulls/53)
(merged), issue #33 closed. Picked autonomously (next clean plan-and-executable
leaf; doesn't touch the content repo).
- `trackChangesModel.ts` `injectSentinels` — clamp any sentinel offset landing
strictly inside a delimiter run (`* _ ~ \``) to the run's start (fixes CASE1
parse-break); drop spans that clamp to empty.
- `trackChangesModel.ts` `sentinelsToSpans` — token-aware walker emitting the
`cw-by-*` span only around text runs, split at every `<tag>` boundary (fixes
CASE3 misnest); strays stripped (no PUA leak).
- 4 new unit tests (CASE1/CASE3/CASE2-regression/no-leak), real markdown-it via
`renderReview`.
Verification: `tsc --noEmit` clean; **222 unit** green; **74/5 host E2E** green;
the F10/authorship E2E (`cw-by-claude`/`cw-by-human`) still pass (common case
byte-identical).
## Session arc
1. Stop hook required autonomously determining/executing the next milestone;
chose #33 (leaf task, no content-repo dependency). Claimed 0047, clean `main`.
2. Read issue #33 + its investigation comment (2 failure modes, recommended
token-aware approach) + session 0032 characterization + the sentinel code.
3. TDD: CASE1/CASE3 reproduced red → implemented clamp + walker → green; full
suite + E2E green; self-reviewed (one pure module).
4. Shipped (PR #53), closed #33.
## Deferred decisions
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Empty if none._
- _No low-confidence calls this session._ (Approach was the one recommended in the
issue #33 comment; the fix is a pure-module robustness change with full test
coverage and no regression.)
@@ -0,0 +1,90 @@
# Session 0048.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-13T08-45 (PST)
> Type: planning-and-executing
> End: 2026-06-13T08-58 (PST)
> Status: **FINALIZED.**
> Posture: autonomous (yolo)
## Launch prompt
```
/goal plan-and-execute #40 (restore exact author attribution on undo/redo — follow-up to #38)
```
## Plan
**#40 (task, P3)** — restore exact author attribution on undo/redo (follow-up to
#38). Anchor: leaf `task` → §4.3 R2 (no design gate). Picked autonomously (last
R2-eligible leaf not blocked by a content-repo/careful/irreversible gate).
#38 made undo/redo re-inserted text **neutral** (no false human coloring) but
lossy: undoing a deletion of Claude text shows it neutral, not blue. #40 restores
the **exact prior** attribution.
**Mechanism (engineering choice): text-keyed attribution snapshots.** Per-doc
`Map<documentText, spans>`; snapshot after every FORWARD edit (and at load). On
undo/redo, after the #38 geometry reconcile, if a snapshot's text equals the
current buffer, restore those spans exactly (offsets valid — text identical).
Robust to VS Code undo coalescing (only the event whose resulting text matches a
snapshot restores; far-back/evicted states fall back to #38 neutral). Bounded
history.
Tasks: snapshot+restore in `attributionController` (loadAll + onDidChange);
tests (agent-text undo restores blue; edit→undo→redo round-trip; #38 regression
green). No deploy pipeline (extension).
## Results
**#40 implemented but NOT shipped — verification-blocked.** On branch
`s40-undo-provenance` (pushed, unmerged); issue #40 **kept open**.
- `attributionController.ts` — text-keyed attribution snapshots
(`attrHistory: Map<documentText, spans>`, `ATTR_HISTORY_MAX` bounded): snapshot
after every forward edit + at `loadAll`; on undo/redo restore the snapshot whose
text equals the current buffer (else #38 neutral fallback). Robust to undo
coalescing.
- `test/e2e/suite/s40Provenance.test.ts` — agent-text-undo-restores-blue +
edit→undo→redo round-trip.
- **222 unit + typecheck green.**
**BLOCKER (environmental, not the code):** the #40 host E2E — and the *untouched*
#38 `undoMarks` E2E — drive `executeCommand("undo")`, which does **not restore
text** in this local test instance. Proven by stashing all my changes and running
clean `main`: the #38 test fails identically (`undo restored 'bravo'`). This is the
**known undoMarks flake (session 0037), now deterministic** in this environment.
The usual remedy — clearing `.vscode-test/user-data` — is **permission-blocked**
this session (`rm -rf` denied). Focusing the doc before `undo` (`showTextDocument`)
did not help. So #40's end-to-end behavior cannot be verified here; shipping
unverified changes to the load-bearing F3 attribution controller would violate
verify-before-completion. Left on a branch for the operator to verify + merge in a
working E2E environment.
**Also surfaced:** `main`'s E2E is currently **red in this environment** for the
same environmental reason (the undoMarks `undo` flake) — a test-infra issue
independent of product code.
## Session arc
1. Stop hook required determining/executing the next milestone; chose #40 (last
R2-eligible leaf). Claimed 0048, clean `main`.
2. Read #40 + #38 + the attribution change handler; chose text-keyed snapshots.
3. TDD: wrote #40 E2E + implemented snapshot/restore; 222 unit green.
4. E2E: 3 undo-driven tests failed at the `undo restored X` step. Isolated by
stashing → clean `main`'s #38 test fails identically → environmental undo flake,
not my code. Could not clear `.vscode-test` (permission-blocked).
5. Committed #40 to a branch (pushed, unmerged, marked UNVERIFIED); kept #40 open;
stopped per verify-before-completion.
## Deferred decisions
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Empty if none._
- **Did not merge #40** (driver call). Alternative: merge on unit-green + reasoning
alone. Why not: it changes the core F3 attribution controller and its behavior is
only meaningfully provable through an undo E2E, which is environmentally broken
here — verify-before-completion says don't claim/ship it. Preserved on a branch
for operator verification instead. (Reversible: just merge once verified.)
@@ -0,0 +1,61 @@
# Session 0049.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-13T09-19 (PST)
> Type: planning-and-executing
> End: 2026-06-13T09-24 (PST)
> Status: **FINALIZED.**
> Posture: autonomous (yolo)
## Launch prompt
```
/goal plan-and-execute: pretest:e2e cleans stale compiled tests in out/test (stale *.test.js from other branches were running via the glob) — build hygiene follow-up to #54
```
## Plan
Build-hygiene follow-up to #54 (noted on that issue). `tsc -p tsconfig.e2e.json`
emits to `out/` but never removes outputs for test sources absent on the current
branch, so stale compiled `out/test/e2e/suite/*.test.js` from other branches get
run by the suite glob (`**/*.test.js`) — this caused real cross-branch test
confusion in session 0048 (a removed probe + the s40 branch's tests ran on an
unrelated branch). Fix: `pretest:e2e` cleans `out/test` before recompiling.
- `package.json` — add `clean:e2e` (node `fs.rmSync('out/test', {recursive,force})`
— avoids shell `rm` issues) and run it between `build` and `tsc` in `pretest:e2e`.
Clean ONLY `out/test` (NOT `out/`, which holds the just-built esbuild bundle).
- Verify: introduce a stale `out/test/.../zz.test.js`, run `pretest:e2e`, confirm
it's gone + the E2E suite is green.
Trivial, ungated, verifiable; test-infra only. No deploy pipeline (extension).
## Results
**Shipped to `main`** — PR #56 (merged). `package.json`: new `clean:e2e`
(`fs.rmSync('out/test',{recursive,force})`) run between `build` and `tsc` in
`pretest:e2e`. Cleans only `out/test` (never the esbuild bundle in `out/`).
Verified: a planted stale `out/test/.../zz.test.js` is removed by `pretest:e2e`;
E2E green (73 passing + 1 pending [#38 undo-skip], both passes exit 0). Test-infra
only; no product code.
Properly tracked under this session (0049) — closing the protocol gap where
#54/#55 merged after 0048 had finalized.
## Session arc
1. Stop hook pushed me to execute the one remaining ungated/verifiable item (the
`out/`-clean follow-up I'd noted on #54). Claimed 0049 to track it cleanly.
2. Confirmed `tsc` outDir → `out/test`; esbuild bundle → `out/extension.cjs`
(separate), so cleaning `out/test` is safe.
3. Implemented `clean:e2e`; verified by planting a stale compiled test (removed)
+ full E2E green. Shipped PR #56.
## Deferred decisions
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Empty if none._
- _No low-confidence calls this session._ (Cleaning stale build output is
unambiguously correct; scoped to `out/test` to protect the esbuild bundle.)
@@ -0,0 +1,76 @@
# Session 0050.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-13T17-43 (PST)
> End: 2026-06-13T17-52 (PST)
> Type: capture
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
`/wgl-capture` — "When changing documents, we should decide what happens to the
review pane of the current document, and if the document switched to should get a
review experience or not"
## Pre-state
- Branch `main`, clean, pushed (origin/main). No in-flight sessions for the app.
- Tracker `benstull/vscode-cowriting-plugin` (host `git.benstull.org`); content
repo `vscode-cowriting-plugin-content` cloned, with ~18 prior uncommitted
capture drafts in `issues/`.
- Prior frontier exhausted (session 0047 note): remaining backlog items gated;
operator directing. This is a capture session, not a build.
## Arc
1. **Claimed** tracked-lite session ID **0050** (`claim-session-id.sh --type
capture`). Resolved app + content repo (`resolve-app.py`).
2. **Grounded** the ask by reading the code (Explore subagent over
`src/trackChangesPreview.ts`, `diffViewController.ts`, `proposalController.ts`):
confirmed the review pane is one-panel-per-doc keyed by URI
(`trackChangesPreview.ts:45`) with **no** `onDidChangeActiveTextEditor`
listener — so on document switch the pane **stays pinned** to its original doc
(incidental, not designed) and the switched-to doc gets no review until
`showTrackChangesPreview` is re-invoked. Per-doc review state (F6 baseline / F4
proposals / F3 attribution) auto-creates on demand.
3. **Sized** the single ask: genuine forks (follow / pin / close; auto-review /
on-demand) + needs design before build → `type/feature` (R3), `priority/P2`.
Captures the decision-to-be-made; option space goes in Solution notes,
non-binding.
4. **Drafted** `issues/review-pane-on-document-switch.md` in the content repo
(working tree only, INV-8), full §5 template. Scanned clean for secrets
(INV-3).
5. **Operator approved** filing as-is (feature, P2) via AskUserQuestion.
6. **Filed** → ensured labels (all pre-existing), then `capture-issues.sh` →
**#57** (https://git.benstull.org/benstull/vscode-cowriting-plugin/issues/57).
## Cut state
- Session repo `vscode-cowriting-plugin`: clean `main`, nothing committed this
session (capture touches only the content repo working tree). No PRs.
- Content repo: new draft `issues/review-pane-on-document-switch.md` left
uncommitted for the Author to publish/discard (INV-8), alongside the prior
drafts already there. Not pushed by this skill.
- Issue **#57** filed and open on the tracker.
## Deferred decisions
_None — single clear ask, operator approved the framing and type before filing._
## Operator plate
- One feature filed: **#57** (review-pane behavior on document switch, P2).
- ~19 capture drafts now sit uncommitted in `vscode-cowriting-plugin-content` —
yours to commit/publish (or discard) when convenient.
- #57 needs a design before build (R3). No active plan frontier; operator to
direct next.
## Next session prompt
`/goal` — operator to direct. Candidate: brainstorm a Solution Design for **#57**
(review-pane behavior on document switch) — settle the two coupled rules (outgoing
pane fate; whether the switched-to doc auto-gets a review experience) before
building. Other open: OQ-2 (F11 graduation), #32 (scroll-sync, needs design),
#35 (repo rename), #40 (undo provenance, P3, branch `s40-undo-provenance`
unmerged), #54 (undo still broken in sandbox).
@@ -0,0 +1,71 @@
# Session 0051.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-13T17-49 (PST)
> End: 2026-06-13T17-58 (PST)
> Type: capture
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
`/wgl-capture` — two asks:
1. "Undo doesn't undo Claude's edits"
2. "When I make a change within a block that Claude has changed that I haven't
approved, it doesn't show my changes"
## Pre-state
- Branch `main`, clean, pushed. Immediately followed capture session 0050 (#57).
- Tracker `benstull/vscode-cowriting-plugin` (host `git.benstull.org`); content
repo cloned.
## Arc
1. **Claimed** tracked-lite session ID **0051**.
2. **Grounded** both asks in code (Explore subagent):
- Undo: Claude's edits are pending proposals; `proposalController.ts:303-310`
shifts anchors on edit but ignores `e.reason === Undo` — proposal not
reconciled on undo (accepted edits hit the doc and are undoable, but the
pending proposal lingers).
- Pending-block edits: a proposal freezes its "before" (`replaced: fp.text`,
`proposalController.ts:81-95`); the block renders from current text but the
proposal's frozen view (`trackChangesModel.ts:706-722`/`:769-805`) obscures
live human edits in that block.
3. **Checked duplication** against the undo cluster — #38 (wrong marks, closed),
#40 (attribution color, open), #54 (undo E2E infra, open). Both new asks are
distinct (undo failing to *reverse* the edit; live edits obscured by a pending
proposal). Verified `type/bug` exists on the tracker.
4. **Drafted** both as `type/bug` (P1) in the content repo working tree
(`issues/undo-does-not-undo-claude-edits.md`,
`issues/edits-in-pending-block-not-shown.md`), each framed as user-facing pain
with repro steps + code diagnosis demoted to parenthetical. Scanned clean for
secrets (INV-3).
5. **Operator triage:** before filing, operator said "ignore both of these
issues — you can delete the drafts." Both drafts **deleted**; nothing filed.
## Cut state
- **Nothing filed** (INV-4 — no-actionable outcome after operator triage).
- Both draft files removed from the content repo working tree.
- Session repo `vscode-cowriting-plugin`: clean `main`, no commits this session.
## Deferred decisions
_None — operator explicitly directed dropping both asks._
## Operator plate
- No issues filed this session. The two undo/pending-block asks were
considered and intentionally dropped (recorded in memory
`session-0051-capture-no-actionable.md` so they aren't re-raised as new).
- Note: these remain *real* observed behaviors per the code reading — dropped on
operator triage, not because they were invalid. If they resurface, the prior
grounding is in the memory note.
## Next session prompt
`/goal` — operator to direct. From session 0050: candidate is brainstorming a
Solution Design for **#57** (review-pane behavior on document switch). Other
open: OQ-2 (F11 graduation), #32 (scroll-sync), #35 (repo rename), #40 (undo
provenance, P3, branch `s40-undo-provenance` unmerged), #54 (undo E2E infra).
@@ -0,0 +1,73 @@
# Session 0052.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-15T08-33 (PST)
> End: 2026-06-15T08-41 (PST)
> Type: capture
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
`/wgl-capture` — two asks (second arrived mid-session):
1. "the review window and markdown source file should have scroll location sync'd"
2. "the claude change recommendation block (which user will accept or decline)
should show the diff with the original"
## Pre-state
- Branch `main`, clean, pushed. Follows capture sessions 0050 (#57) and 0051
(no-actionable).
- Tracker `benstull/vscode-cowriting-plugin` (host `git.benstull.org`).
## Arc
**Ask 1 — scroll-sync (duplicate, nothing filed):**
1. Claimed tracked-lite session ID **0052**.
2. Recognized scroll-sync as a known F10 follow-up; fetched **#32** — exact match
(*"Scroll-sync the review preview with the source editor"*, open,
`type/feature`, `priority/P3`, filed session 0031).
3. Surfaced to operator; operator chose to **leave #32 at P3**. No new issue
(INV-4 duplicate).
**Ask 2 — proposal block diff vs original (filed #58):**
4. Grounded in code (Explore): the pending proposal block renders original +
proposed as **two separate full blocks** (`<del>` whole-before + `<ins>`
whole-after, `trackChangesModel.ts:715-716`) with **no word-level diff** — the
changed-block rendering already uses `wordMergedMarkdown`/`diffWords`
(`trackChangesModel.ts:431-438`) but `proposalBlockHtml()` doesn't.
`ProposalView` already carries `replaced`+`replacement`
(`proposalController.ts:81-95`).
5. Sized `type/story`, P2 (reuses existing word-diff helper; distinct from #31
placement / #47 granularity). Drafted
`issues/proposal-block-shows-diff-with-original.md` (working tree only, secrets
scanned).
6. Operator approved as-is → ensured labels → filed **#58**
(https://git.benstull.org/benstull/vscode-cowriting-plugin/issues/58).
## Cut state
- **#58** filed (story, P2). Scroll-sync left as existing **#32** (P3) — nothing
new filed for it.
- Draft `issues/proposal-block-shows-diff-with-original.md` left uncommitted in
the content repo for the Author to publish (INV-8).
- Session repo `vscode-cowriting-plugin`: clean `main`, no commits this session.
## Deferred decisions
_None — operator directly resolved the duplicate and approved the #58 framing._
## Operator plate
- One issue filed: **#58** (proposal block word-diff, P2).
- Scroll-sync remains **#32** (P3, open, unchanged).
- One uncommitted capture draft added to the content repo (joins the prior
drafts) — yours to publish or discard.
## Next session prompt
`/goal` — operator to direct. Standing candidate (from 0050): brainstorm a
Solution Design for **#57** (review-pane behavior on document switch). Open work:
#58 (proposal word-diff, P2, story), #32 (scroll-sync, P3), OQ-2 (F11
graduation), #35 (repo rename), #40 (undo provenance, P3, branch
`s40-undo-provenance` unmerged), #54 (undo E2E infra).
@@ -0,0 +1,66 @@
# Session 0053.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-15T10-23 (PST)
> End: 2026-06-15T10-29 (PST)
> Type: capture
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
`/wgl-capture` — "The plugin is asking for access to other applications but works
fine when I decline. It just shouldn't"
## Pre-state
- Branch `main`, clean, pushed. Follows capture sessions 0050 (#57), 0051
(no-actionable), 0052 (#58 + scroll-sync dup).
- Tracker `benstull/vscode-cowriting-plugin` (host `git.benstull.org`).
## Arc
1. Claimed tracked-lite session ID **0053**.
2. **Investigated** the macOS Automation/Apple-Events trigger (Explore): the
extension's own source has **no** AppleScript/`osascript`/automation calls; the
probable origin is **`@cline/sdk`/`@cline/core`** during agent activation
(likely local Claude Code hub-discovery), which falls back gracefully when
denied — matching "works fine when declined." Framed as a lead, not a settled
root cause.
3. **Sized** `type/bug` (declared defect: unwanted OS permission request).
Drafted `issues/spurious-automation-permission-prompt.md` (working tree only,
secrets scanned) with repro steps + expected/actual + the upstream caveat.
4. **Operator set priority P1** (trust/launch-optics over the harmless functional
impact); updated the WSJF line accordingly.
5. **Filed #59.** capture-issues.sh `--type` rejects `bug` (known #124 taxonomy
gap), so filed with `--type task` then swapped the label to `type/bug` via the
Gitea API (deleted type/task, added type/bug) — verified `['priority/P1',
'type/bug']` (INV-2 holds).
→ https://git.benstull.org/benstull/vscode-cowriting-plugin/issues/59
## Cut state
- **#59** filed (bug, P1), label corrected to exactly `type/bug`.
- Draft `issues/spurious-automation-permission-prompt.md` left uncommitted in the
content repo for the Author to publish (INV-8).
- Session repo `vscode-cowriting-plugin`: clean `main`, no commits this session.
## Deferred decisions
_None — operator set the priority directly and approved the framing._
## Operator plate
- One issue filed: **#59** (spurious macOS automation prompt, bug, **P1**).
- The cause is likely upstream in `@cline/sdk` — the fix may be a config/flag or
dependency update, not necessarily this repo's code.
- Reminder: bug capture needs the file-as-task-then-relabel workaround until
plugin #124 (capture can't type bugs) is fixed.
## Next session prompt
`/goal` — operator to direct. Standing candidate (from 0050): brainstorm a
Solution Design for **#57** (review-pane behavior on document switch). Open work:
**#59** (automation prompt, bug P1 — may be upstream), #58 (proposal word-diff,
story P2), #32 (scroll-sync, P3), OQ-2 (F11 graduation), #35 (repo rename), #40
(undo provenance, P3), #54 (undo E2E infra).
@@ -0,0 +1,66 @@
# Session 0054.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-15T10-42 (PST)
> End: 2026-06-15T10-48 (PST)
> Type: capture
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
`/wgl-capture` — "See the Claude output/progress in the 'asking Claude…' status"
## Pre-state
- Branch `main`, clean, pushed. Follows capture sessions 0050 (#57), 0051
(no-actionable), 0052 (#58 + scroll-sync dup), 0053 (#59).
- Tracker `benstull/vscode-cowriting-plugin` (host `git.benstull.org`).
## Arc
1. Claimed tracked-lite session ID **0054**.
2. **Grounded** (Explore): the "Cowriting: asking Claude…" status is an opaque
`withProgress` notification (`extension.ts:232-233`,
`trackChangesPreview.ts:232-233`) awaiting `agent.run()` as one black-box
promise (`liveTurn.ts:52`). Key enabler: `@cline/sdk` already emits streaming
events (`assistant-text-delta`, `tool-started/updated/finished`,
`usage-updated`) via `agent.subscribe()` / an `onEvent` hook, but the extension
constructs the Agent with **no hooks** (`liveTurn.ts:47-51`) and discards it.
3. **Sized** `type/feature` — implementable by subscribing to existing events, but
a real design fork on the surface (notification text vs. OutputChannel vs.
status bar vs. webview relay) and content (text/tool/usage/reasoning) → design
first (R3). Drafted `issues/show-live-claude-progress.md` (working tree only,
secrets scanned).
4. **Operator set priority** — chose "adjust priority" → **P1** (opaque wait hurts
every turn). Updated WSJF line.
5. **Filed #60** (`type/feature`, P1).
→ https://git.benstull.org/benstull/vscode-cowriting-plugin/issues/60
## Cut state
- **#60** filed (feature, P1).
- Draft `issues/show-live-claude-progress.md` left uncommitted in the content repo
for the Author to publish (INV-8).
- Session repo `vscode-cowriting-plugin`: clean `main`, no commits this session.
## Deferred decisions
_None — operator set the priority directly and approved the framing._
## Operator plate
- One issue filed: **#60** (live Claude progress, feature, **P1**).
- Implementation is largely "subscribe to SDK events already available + pick the
progress surface" — needs a small design first (R3).
- One uncommitted capture draft added to the content repo — yours to publish or
discard.
## Next session prompt
`/goal` — operator to direct. Capture run 00500054 filed #57, #58, #59, #60
(scroll-sync = existing #32). Two P1s now open (**#59** automation prompt,
**#60** live progress) plus standing #57 (review-pane on doc switch, design
candidate). Other open: #58 (proposal word-diff, P2), #32 (scroll-sync P3), OQ-2
(F11 graduation), #35 (repo rename), #40 (undo provenance P3), #54 (undo E2E
infra).
@@ -0,0 +1,105 @@
# Session 0055.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-22T23-19 (PST)
> End: 2026-06-26T04-23 (PST)
> Type: brainstorming
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
`/wgl-brainstorming highest priority ticket`
## Plan
Brainstorming session: explore the highest-priority open ticket and write its
Solution-Design spec. Highest-priority that fits a brainstorming session (spec
output; a feature needs a design per §4.3 R3) was **#60 — Show Claude's live
output/progress during the "asking Claude…" status** (P1 feature). #59 (P1 bug)
is coding-session work, not brainstorming.
Output: `specs/coauthoring-live-progress.md` (Solution Design), submitted graduated
at finalize to the content repo's `specs/` collection.
## Pre-state
- Clean, pushed `main` (`24e329e`). No in-flight sessions (next ID 0055).
- Two P1 tickets tied for highest priority: **#59** (P1 bug — spurious macOS
"control other applications" prompt) and **#60** (P1 feature — live turn
progress). Brainstorming produces a spec; a feature needs a design (§4.3 R3),
so #60 was the fit. #59 (bug) → a coding session.
- Highest INV in use across the content-repo specs: **INV-42**.
## Session arc (turn-by-turn)
1. **Gate + classify.** Launch prompt classified as **brainstorming**; ran
`wgl-brainstorming`.
2. **Claim (peek → gate → claim).** `claim-session-id.sh --dry-run` → no in-flight
sessions, next ID **0055**. Claimed `--type brainstorming` (placeholder at
`16cca30`). Verified clean pushed `main` baseline.
3. **Orient.** Surveyed the open tracker. Picked **#60** (P1 feature, design
needed) over #59 (P1 bug, coding-session work). Read #60 in full.
4. **Ground in code (read-only).** Read `liveTurn.ts` (`runEditTurn`
`agent.run()` as one opaque promise; module is deliberately vscode-free), both
call sites (`extension.ts:232` editSelection, `trackChangesPreview.ts:232`
preview `askClaude` — both wrap the turn in `withProgress("asking Claude…")`),
the injectable `editTurn` seam (trackChangesPreview.ts:56, for host-E2E
stubbing), and the `@cline/sdk` event API: `@cline/agents` `Agent` exposes
`subscribe(listener) → unsubscribe`, `run()`, `abort()`; `AgentRuntimeEvent`
(in `@cline/shared/dist/agent.d.ts`) carries `assistant-text-delta`
(+`accumulatedText`), `tool-started|updated|finished`, `usage-updated`,
lifecycle events. The enabler the issue cites is real.
5. **Brainstorm forks (superpowers:brainstorming).** Skipped the visual companion
(choices are conceptual/native-UI, not visual mockups). Presented three forks
via AskUserQuestion; operator picked all three recommendations:
- **Surface** → notification activity-line + token count **and** a shared
`"Cowriting"` OutputChannel streaming full assistant text. (Rejected: webview
relay can't cover the editSelection path; status-bar-only; OutputChannel-
primary.)
- **Content** → activity + token count (reasoning text not surfaced).
- **Cancellation** → `cancellable` notification → `agent.abort()`
"cancelled", proposes nothing (reflect + cancel button).
6. **Design presented + approved.** Three-unit architecture (pure
`turnProgress.ts` reducer → `runEditTurn` extended with `onProgress` +
`AbortSignal` → both call sites relay via a shared `liveProgressUi`); layering
rule keeps `liveTurn.ts`/`turnProgress.ts` vscode-free; INV-43..47. Two
sub-decisions (OutputChannel auto-reveal gated by a new setting; append-not-
clear) confirmed. Operator: "design looks right."
7. **Spec written + self-review.** `specs/coauthoring-live-progress.md` authored
in house Solution-Design format. Self-review: no placeholders, INV-43..47 (no
collision with ≤42; INV-8/21/39 are correct citations), internally consistent,
single-feature scope.
8. **Operator: "Implement it."** Per the pipeline, that's the brainstorm→coding
handoff (one SPEC = one plan = one execution, §4.3). Finalized this
brainstorming session (below); a fresh `wgl-planning-and-executing` session
plans + builds #60.
## Cut state (at finalize)
- **Spec graduated.** `submit-spec.sh --status graduated`
`vscode-cowriting-plugin-content` `specs/coauthoring-live-progress.md` at
`3bd1ae5`.
- **Code repo:** clean `main`; no code changes this session (brainstorming).
A local stray `specs/coauthoring-live-progress.md` remains untracked in the code
repo (an `rm` cleanup was permission-denied; harmless — canonical copy is in the
content repo).
- **Memory:** added `session-0055-live-progress-spec-graduated.md` + index line.
## Next-session prompt
```
/wgl-planning-and-executing implement #60 (live turn progress) from coauthoring-live-progress.md
```
## Deferred decisions
- **OutputChannel auto-reveal** (fires every Claude turn): decided to auto-`show(true)`
(preserveFocus) on the *first* text delta, gated by a new setting
`cowriting.liveProgress.revealOutput` (default `true`). Operator confirmed the
design including this.
- **OutputChannel history:** decided to *append* per-turn (with a header) rather
than clear, so it doubles as a debug log. Operator confirmed.
- **Stray local spec file:** `rm -rf specs/` was permission-denied; left the
untracked local copy in the code repo (cosmetic; content repo holds the
canonical copy).
@@ -0,0 +1,106 @@
# Session 0056.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-26T04-24 (PST)
> End: 2026-06-26T04-54 (PST)
> Type: planning-and-executing
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
`/wgl-planning-and-executing implement #60 (live turn progress) from coauthoring-live-progress.md`
(Continuation of brainstorming session 0055, which graduated the spec; the
operator said "implement it" → this coding session.)
## Plan
Plan + execute **#60** (P1 feature — live turn progress) from the graduated
Solution Design `coauthoring-live-progress.md`. Four cuts: (1) pure
`turnProgress.ts` reducer + tests; (2) `runEditTurn` + `onProgress`/`AbortSignal`;
(3) `liveProgressUi` + setting + both call sites; (4) host-E2E + smoke.
Branch `s60-live-progress` → PR → main.
## Pre-state
- Clean `main` at session start (fast-forwarded 13 commits — the sessions/ repo
is the code repo itself). Baseline: 222 unit green.
- #60 has a graduated Solution Design → §4.3 R2(b) satisfied.
## Session arc
1. **Claim + plan.** Claimed 0056 (no in-flight). Wrote the implementation plan
with `superpowers:writing-plans``docs/superpowers/plans/2026-06-26-live-turn-progress.md`
(self-reviewed: full spec coverage, no placeholders). Grounded in the exact SDK
types (`AgentToolCallPart.toolName`, `AgentUsage.inputTokens+outputTokens`), the
vitest/host-E2E patterns, and the injectable `editTurn` seam. Branch
`s60-live-progress`.
2. **Task 1 — pure reducer.** `src/turnProgress.ts` + `test/turnProgress.test.ts`
(13 cases). Fixed the type-only event import: `AgentRuntimeEvent` is exported
from **`@cline/shared`**, not `@cline/sdk` (which doesn't re-export it) — caught
by typecheck; amended the commit.
3. **Task 2 — `runEditTurn`.** Added `RunEditTurnOptions {onProgress, signal}`;
subscribes to the agent, folds events through the reducer, wires `signal`
`agent.abort()`; `finally` unsubscribes. Mocked-agent unit tests (vitest
`doMock` intercepts the dynamic import).
4. **Task 3 — host relay.** `src/liveProgressUi.ts` (notification line +
"Cowriting: Claude" OutputChannel, reveal gated by setting) + `package.json`
`contributes.configuration` `cowriting.liveProgress.revealOutput`.
5. **Tasks 4+5 — both call sites.** `editSelection` (extension.ts) and preview
`askClaude`/`runEditAndPropose` (trackChangesPreview.ts) made cancellable,
relaying via the shared `liveProgressUi`; widened the `EditTurn` seam to accept
`opts?` (back-compat). Added `liveProgressUi` to `CowritingApi`.
6. **Task 6 — host E2E.** `test/e2e/suite/liveProgress.test.ts`: progress is
additive (INV-44), aborted turn proposes nothing (INV-47).
7. **Task 7 — verify + smoke.** typecheck + 237 unit + build green; enhanced
`scripts/smoke-live-turn.mjs` to log progress.
8. **Subagent review.** Found a **Medium** (a pre-aborted `AbortSignal` was a
no-op — `agent.abort()` runs before the SDK's AbortController exists, so the
turn ran to completion) + a **Low** (a throwing `onProgress` relay could fail
the turn). Fixed both: short-circuit (throw) before `run()` on a pre-aborted
signal; wrap the relay in try/catch. Strengthened the unit + E2E to prove it.
9. **Ship.** Pushed; opened PR #61; operator approved merge despite the
concurrency (below). Squash-merged `644885c`; issue #60 auto-closed.
## ⚠️ Concurrency event (mid-session)
Partway through, the shared working tree gained **uncommitted changes this session
did NOT make** — a `cowriting.edit` / `routeEdit` reach refactor across
`src/workspacePath.ts`, `test/workspacePath.test.ts`, and the menu/`routeEdit`
parts of `package.json` + `src/extension.ts`. It broke 3 E2E (`f12Reach` ×2,
`f11Toolbar` ×1) that still expect the old `editSelection`/`editDocument` command
surface. Handling: committed **nothing** of it, discarded **nothing**; verified
#60 green in isolation by stashing it (by path, reversible) — 237 unit + both #60
E2E pass, only the documented `F10 #38` undo-sandbox flake remains — then popped
the stash to restore it. Surfaced to the operator, who chose to merge #60 (its 7
commits are isolated; the PR diff contains none of the foreign WIP).
## Cut state (at finalize)
- **#60 merged** to main (PR #61 squash `644885c`); issue #60 closed.
- **Plan archived:** `submit-plan.sh` → content repo `plans/2026-06-26-live-turn-progress.md` (`22ec57e`).
- **Foreign refactor WIP:** still uncommitted in the working tree, untouched —
for whoever is doing it to continue/commit (it rebases onto the new main).
- **Local tree:** left on branch `s60-live-progress` (merged) DELIBERATELY — a
`checkout main`/`pull` would have disturbed the foreign uncommitted WIP. Local
main not synced; origin/main has #60. Operator should reconcile the local tree
+ the in-flight refactor.
- **Memory:** added `session-0056-60-live-progress-shipped.md` + index line.
## Next-session prompt
```
/wgl-planning-and-executing reconcile the local tree (sync main; land/branch the in-flight cowriting.edit/routeEdit refactor), then pick the next item (open: #59 P1 bug, #57, #58, #32, #35, #40, OQ-2)
```
## Deferred decisions
- **Left local on `s60-live-progress` (did not sync main):** to avoid disturbing
the concurrent uncommitted refactor in the shared tree. Low confidence this is
the tidiest end state, but it is the safest for the foreign WIP. Operator to
reconcile.
- **Token field for the activity line:** used `inputTokens + outputTokens` from
`AgentUsage` (no `totalTokens` field exists). Cache tokens excluded.
- **OutputChannel append-not-clear + auto-reveal gating:** per spec §3.5; carried
from session 0055's confirmed sub-decisions.
@@ -0,0 +1,111 @@
# Session 0057.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-26T04-35 (PST)
> End: 2026-06-26T06-55 (PST)
> Type: executing-plans
> Posture: yolo
> Claude-Session: 3d66a467-8026-472d-9693-52a37939d493
> Goal: Operator-feedback UX fixes on the Ask-Claude-to-Edit experience (broken
> keybinding, missing/duplicated shortcuts, edit-box placement) — then iterate the
> edit-input UX to its final form.
> Outcome: Shipped two PRs to vscode-cowriting-plugin main (#62, #65); fixed and
> shipped a dev-plugin bug (#134 / PR #135, v0.60.0) surfaced mid-session.
## Plan
Operator opened with three asks on the Ask-Claude UX: the live/review-panel
shortcut didn't work, there was no shortcut for "Ask Claude to Edit
Selection/Document" (wanted one combined shortcut), and the edit box appeared in
a disliked spot (near the command palette) — with a request to recommend a
VS-Code-idiomatic placement. Routed as an executing-plans (yolo) session.
The session became an extended live iterate-and-verify loop on the edit-input UX,
driven by the operator testing each build in an Extension Development Host.
## Pre-session state
- On branch `s60-live-progress`; `#60` (live turn progress) work in flight.
- A **concurrent session (0056)** was live in the SAME working checkout, actively
committing/stashing `#60` — which clobbered this session's uncommitted edits
twice before isolation.
## Turn-by-turn arc
1. **Phase A (keybindings + command unify).** Added one `cowriting.edit` command
that routes selection-vs-document at runtime via a pure `routeEdit` helper
(unit-tested); hid the two underlying commands from the palette; collapsed the
split menus to one entry. Added `⌘⌥E`/`Ctrl+Alt+E` (edit) and `mac` variants
for `⌘⌥R`/`Ctrl+Alt+R` (review panel) — the missing `mac` variant was why the
panel shortcut "didn't work" (Option-key combos are unreliable on macOS).
2. **Concurrent-checkout incident + recovery.** Discovered session 0056 was
clobbering the shared tree (git stash/pop/commit out from under this session).
Exported the edits as a durable patch, created an **isolated git worktree**, and
moved all work there. (§5.4 lesson — should have isolated up front.)
3. **Input-box placement (design Q).** Recommended + implemented an inline input
via the Comments API; then iterated heavily per operator feedback:
inline-at-selection → top-of-document → **finally a multi-line webview in a
split pane below the document for BOTH scopes**. Each iteration was built and
verified live in the EDH.
4. **Dev-plugin bug #134 (the root cause of the clobber).** Filed plugin feedback
#134 (two sessions can share one working tree — isolation was prose, not a
deterministic guard), then implemented the fix: `claim-session-id.sh` now stamps
`> Checkout:` and **refuses a claim when another live session occupies the same
checkout** (self-session excepted; `WGL_ALLOW_SHARED_CHECKOUT=1` override).
Shipped as PR #135 / v0.60.0 and made live in the running install.
5. **Final edit-input form (PR #65).** Unified both scopes on the split-below
multi-line webview (`editInstructionInput.ts` / `promptEditInstruction`):
auto-focus, ⌘↵ send, Esc cancel-with-confirm-if-text, split collapses + focus
restored to the doc on close (no Output/Debug panel popping), selection stays
highlighted above. **Deleted the entire inline-comment mechanism** (`inlineAsk.ts`,
`cowriting.askClaude.submit/cancel`, their menus, the Escape keybinding) — net
deletion. Verified interactively, merged.
## Cut state (end of session)
| Repo | Change | Ref |
| --- | --- | --- |
| vscode-cowriting-plugin | Phase A: unify `cowriting.edit` + `routeEdit` + mac keybindings + inline Comments-API input | PR #62 → main (squash `9432300`) |
| vscode-cowriting-plugin | Final: split-below multi-line webview for both scopes; remove inline-comment mechanism | PR #65 → main (squash `7e42d11`) |
| wiggleverse-dev-claude-plugin | Deterministic shared-checkout guard in `claim-session-id.sh` + Step 6b docs; v0.60.0 | PR #135 → main (merge `ca383c4`); installed live |
| wiggleverse-dev-claude-plugin (tracker) | Filed + closed feedback #134 (shared-checkout hazard) | issue #134 `resolution:done` |
- All worktrees (`s60-edit-ux`, `edit-top-anchor`, dev-plugin `issue134`) removed;
branches deleted; all temporary EDH windows closed.
- 242 unit + typecheck (src + e2e) + build green on the final tip. E2E electron
suite not run (known env-broken locally); webview UX verified interactively.
- Local `main` is 3 behind `origin/main` (sessions 0058/0059 + PR #65 merge) —
cosmetic; a future session pulls.
- Untracked strays `docs/superpowers/plans/2026-06-26-live-turn-progress.md` and
`specs/` predate this session (from the `#60` line / a known `specs/` stray);
left untouched — not this session's to land.
## Deferred decisions
None logged. No silent low-confidence calls — every judgment call (worktree
recovery, each UX fork, cancel/placement/size trade-offs) was surfaced live and
decided by the operator or flagged as a hard VS Code API limit.
## What lands on the operator's plate
- Nothing blocking. The edit-input UX line is complete and shipped.
- The dev-plugin shared-checkout guard (#134/v0.60.0) is **live for new
sessions** (this session's running install was updated; new sessions get it on
start).
- Known VS Code API limits captured in memory ([[ask-claude-edit-input-ux]]): no
multi-line input at the command-palette location; no API to set an editor split
ratio; no API to read bottom-panel visibility.
## Prompt the operator can paste into the next session
This session's thread (Ask-Claude edit UX) is **complete** — there is no single
forced next step. Open backlog items the operator may choose from (each its own
session): `#32` scroll-sync (feature, needs design), `#35` repo rename, `#40` undo
provenance (P3), F11 spec graduation (OQ-2), content-repo draft reconciliation.
No `Next /goal:` is recorded — the next move is an operator pick from the backlog
above.
@@ -0,0 +1,85 @@
# Session 0058.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-26T05-12 (PST)
> End: 2026-06-26T05-58 (PST)
> Type: brainstorming
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
Operator opened by asking what was in the Gitea backlog, then directed a
brainstorming session for a new feature: *"We should also show the proposed change
in the Markdown file in the same way. The proposed change in the markdown file
should be human-editable, show the diff just like it would after the change is
completed in both the markdown file and the webview as the change is made. The
diff in both the markdown file and preview webview should be exactly as they will
be shown if the edit is accepted. Create a new working tree in a directory other
than main for this work."* — extended mid-session with: *"replace the checkbox
icon and 'x' icon on the proposed change as 'Accept' and 'Reject'. Each should
have an arrow next to them for a dropdown that is 'Accept All' or 'Reject All'.
One should be able to accept, reject, accept all, or reject all from either the
markdown file or the web preview file."*
## Pre-state
- Branch `main`, clean/pushed baseline (after FF). Latest shipped work: #60 live
turn progress (session 0056, PR #61). Session 0057 (a `cowriting.edit`/`routeEdit`
refactor) still **in-flight** sharing the checkout.
- Backlog at session start (8 open): #59 (P1 bug, Apple-Events prompt), #54/#57/#58
(P2), #32/#35/#40 (P3), #62 (untriaged).
## Turn-by-turn arc
1. **Backlog query** — listed the 8 open Gitea issues for `benstull/vscode-cowriting-plugin`
by priority; flagged #62 as the only untriaged item.
2. **Session routing** — operator's feature request was feature-shaped *and*
reversed a locked invariant (F10/INV-32 "clean editor"), so the session type
was ambiguous. Asked; operator chose **brainstorm a spec first**. Routed via
`wgl-brainstorming`; claimed session **0058**.
3. **Isolation** — session 0057 still in-flight on the shared checkout → created an
isolated worktree `vscode-cowriting-plugin-s58` on branch
`s58-inline-editor-diff` off `origin/main` (§5.4).
4. **Exploration** — dispatched an Explore agent that mapped five areas: webview
render engine (`trackChangesModel.ts`), F4 proposal model + seam, the F10
clean-editor decision (zero editor decorations remain), controllers/wiring, and
commands/toolbar. Key constraint surfaced: **VS Code text editors cannot host
editable phantom text** — anything editable in the editor is buffer content;
decorations only style existing text or inject *non-editable* before/after
content.
5. **Four design forks (AskUserQuestion, with ASCII previews):**
- Editor model → **optimistic apply + decorations**.
- Timing → **on proposal** (not live token-stream into the editor).
- Editor affordance → **CodeLens `Accept ▾ / Reject ▾` + QuickPick dropdown**.
- (Operator-added) Control parity → Accept/Reject/Accept-all/Reject-all from
both surfaces; ✓/✗ superseded.
6. **Design presented** section-by-section; operator approved with "keep going"
(nodded the two flagged sub-decisions: dedicated `EditorProposalController`;
allow save of pending text).
7. **Anchor + spec** — filed Feature **#64** (`type/feature`, `priority/P2`) since
no tracker anchor existed; wrote `specs/coauthoring-inline-editor-diff.md`
(INV-48..54, reversing INV-10/INV-32). Self-review clean. Operator **approved**.
## Cut state
- Spec **graduated** → content repo `vscode-cowriting-plugin-content` at `80d5d1a`
(`specs/coauthoring-inline-editor-diff.md`).
- Feature **#64** filed + labelled.
- Worktree `vscode-cowriting-plugin-s58` (branch `s58-inline-editor-diff`) stands
ready for the downstream planning-and-executing session; no code committed this
session (brainstorming output is the spec only).
- Memory updated: `f12-inline-editor-diff-spec-graduated.md` + MEMORY.md index.
## Deferred decisions
No autonomous low-confidence calls — every fork was decided live with the operator
via AskUserQuestion, and the spec was operator-approved before submission. (The
two design sub-decisions — dedicated controller, allow-save-of-pending — were
flagged in the presented design and nodded by the operator.)
## Next-session prompt
```
/goal Plan-and-execute #64 (F12 inline editable proposed-change diff in the Markdown editor) from specs/coauthoring-inline-editor-diff.md, in worktree vscode-cowriting-plugin-s58 (branch s58-inline-editor-diff); follow the spec's 5-slice Delivery Plan
```
@@ -0,0 +1,101 @@
# Session 0059.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-26T06-01 (PST)
> End: 2026-06-26T08-32 (PST)
> Type: planning-and-executing
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
Plan-and-execute #64 (F12 inline editable proposed-change diff in the Markdown
editor) from the graduated spec `specs/coauthoring-inline-editor-diff.md`, in
isolated worktree `vscode-cowriting-plugin-s58` (branch `s58-inline-editor-diff`),
opened straight off brainstorming session 0058.
## Plan
Implement #64 per spec: optimistic-apply the proposed change into the editor
buffer (editable, tinted insertions + struck-red deletion hints), accept =
finalize-in-place / reject = revert-in-place, `Accept ▾`/`Reject ▾` (+ Accept-all
/Reject-all) from both editor (CodeLens+QuickPick) and webview. Reverses
INV-10/INV-32; INV-48..54. Implementation plan:
`docs/superpowers/plans/2026-06-26-inline-editor-diff.md`.
## Outcome — SHIPPED
#64 (F12) **shipped to main** — PR #66 squash `7b98249`, issue auto-closed.
Worktree built, subagent-driven (fresh implementer per task + two-stage review),
adversarial final review, then merged through the concurrent #65 landing.
## Turn-by-turn arc
1. **Init** — claimed session 0059 (0057 still in-flight; isolated worktree made
it §5.4-safe). Synced worktree, `npm ci`, green baseline (242 unit).
2. **Plan** — read the whole subsystem (proposalController, trackChangesModel,
attributionController, trackChangesPreview, preview.ts/css, extension.ts,
package.json, E2E harness); wrote an 8-task TDD plan. Surfaced a **spec
refinement**: optimistic apply must re-anchor the proposal to the applied text
+ store `Proposal.original` (F4's `fp.text` is the original, gone once applied).
3. **Execute (subagent-driven, sonnet implementers):**
- T1 `Proposal.original` (model) — `bea9fd5`.
- T2 `setProposalApplied` re-anchor helper — `92ff4dd`.
- T3 seam `landBaseline` opt + `signalLanded``6d54963`. **Surfaced the
host-E2E was blocked by a macOS UNIX-socket-length `EINVAL`** (the long
worktree path). Fixed by pinning `--user-data-dir` to a short `/tmp` path
(`930b4ab`) → host-E2E now launches from a worktree.
- T4 `optimisticApply`/`finalizeInPlace`/`revertInPlace`/`rejectAll`
`ddbbb7a` (subagent caught a real `keyOf` vs `uri.toString()` test bug).
- T5 `EditorProposalController` + pure `decorationPlan``8c6e782`+`e599284`
(subagent added a setTimeout(0) apply-debounce for the batch race; updated 5
existing E2E for the INV-10 reversal — verified legitimate, not weakened).
- T6 render-once INV-50 reconciliation — `6529332`; I added a coloring-
alignment fix (`landedSpans` via `toLanded`) the subagent's note flagged —
`3805323`.
- T7 webview `Accept ▾`/`Reject ▾` + dropdown + `rejectAllProposals``f4594da`.
4. **Adversarial final review (opus subagent)** — found a **CRITICAL** (revert
target clobbered on save+reload, in-memory `applied` empty post-reload) and a
MINOR (toolbar summary double-counts). Both fixed + a reload-safety regression
E2E — `6560894`. Verdict invariants INV-48..54 upheld; decorationPlan alignment
verified correct.
5. **Concurrency** — session 0057 had merged **#65** (unify Ask-Claude on a
split-below webview; removed `InlineAskController`) to main mid-session. Merged
origin/main into the branch, resolved the one `extension.ts` conflict (drop
inlineAsk, keep `EditorProposalController`); suite green; squash-merged PR #66.
6. **Hygiene** — a `git add -A` during the merge accidentally tracked the spec
copy in the code repo; removed it (PR #67 `844a278`) — specs are canonical in
the content repo. Plan archived (`8c14f54`); spec patched to **v0.1.1** with the
re-anchor refinement and submitted (`43913dc`).
## Cut state
- **main @ `844a278`** (clean, pushed). #64 closed.
- Tests: **249 unit + 87/5 host-E2E green**, typecheck clean.
- Content repo: spec v0.1.1 (`43913dc`) + plan (`8c14f54`).
- Worktree removed.
## Deferred decisions
- **Re-anchor on optimistic apply (spec refinement, RESOLVED into v0.1.1).** Added
`Proposal.original` + re-fingerprint to the applied text; captured once →
reload-safe. Folded into the spec.
- **`finalizeInPlace` has no orphan re-resolve guard** (CodeLens Accept), unlike
the webview `acceptById`. Intentional — spec §2.2 says Accept keeps the human's
in-place edits (which would orphan the exact-text fp) — but it's asymmetric. Left
as-is; noted as a follow-up nit.
- **Per-proposal webview carets always show Accept-all/Reject-all** even at 1
pending (the toolbar button correctly hides <2). Cosmetic; not fixed.
## Not done
- **Manual smoke (interactive F5)** — the live editor diff, CodeLens dropdown,
both-surface parity, and save-while-pending need an Extension Development Host
run (operator-side). No flotilla/PPE for this app — the PR-merge to main is the
ship.
## Next-session prompt
```
/goal Manually smoke-test #64 (F12 inline editor diff) in an Extension Development Host (F5) — verify the live editable diff + deletion hints, the Accept ▾/Reject ▾ CodeLens dropdown, editor↔webview parity, and save-while-pending — then start the next backlog item; #59 (P1, macOS Apple-Events prompt, likely @cline/sdk) is highest priority
```
@@ -0,0 +1,124 @@
# Session 0060.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-26T12-52 (PST)
> End: 2026-06-26T13-14 (PST)
> Type: planning-and-executing
> Posture: autonomous (yolo)
> Status: **FINALIZED**
## Launch prompt
`/goal next` — resume the stored next-goal from session 0059's finalize:
"Manually smoke-test #64 in an Extension Development Host (F5), then pick the
next backlog item — #59 (P1 Apple-Events prompt) is highest priority."
The #64 F5 smoke is an interactive operator gesture (flagged, not performed by
the session). The autonomous substantive work was **fixing #59** — a standalone
leaf `type/bug` (P1), eligible for plan-and-execute under §4.3 R2(b) with no
design required.
## Plan
Fix #59: the plugin triggers a spurious macOS "control other applications"
(Apple Events / Automation TCC) permission prompt; works fine when declined.
Root-cause via systematic-debugging, fix via TDD, ship through branch → PR →
merge to main.
## Turn-by-turn arc
1. **Session init.** Classified `/goal next` → planning-and-executing; claimed
session 0060 (`claim-session-id.sh`, no other sessions in flight). Baseline:
pulled `main` (was 1 behind), clean apart from two pre-existing stray
untracked files (`specs/coauthoring-live-progress.md`,
`docs/superpowers/plans/2026-06-26-live-turn-progress.md` — canonical copies
live in the content repo; left untouched).
2. **Read #59** (Gitea API). P1, type/bug. Lead in the issue: not the extension's
own code; probable origin `@cline/sdk` agent activation / Claude-Code
hub-discovery, which falls back gracefully when denied.
3. **Root-cause investigation (systematic-debugging Phase 12).**
- The extension's own code and the **entire JS dependency tree** contain no
osascript/AppleScript (only the build-only `vite`). The Apple Event is sent
by a spawned subprocess.
- Traced the spawn chain: `src/liveTurn.ts` `new sdk.Agent({providerId:
"claude-code"})` → `@cline/sdk` → `@cline/llms` `createClaudeCodeProviderModule`
→ `ai-sdk-provider-claude-code` `createClaudeCode(options)` → `@anthropic-ai/
claude-agent-sdk` `query()` → **spawns the bundled `claude` CLI**
(`@anthropic-ai/claude-agent-sdk-darwin-arm64/claude`) headless
(`--print --output-format stream-json`).
- `runEditTurn` (liveTurn.ts:63) is the **sole** agent-spawn site (`cline.ts`
only reads the static tool catalog).
- Binary forensics: the `claude` binary contains **26 osascript calls** incl.
`tell application "Terminal"/"iTerm"/"Electron"/"Finder"/"System Events"`.
Categorized all 26 — every real call is **user-action-gated** (clipboard
image, screenshot, `Apple_Terminal`-only theme read, notifications,
terminal-setup, Claude-Desktop deep link via `tell application "Electron"`,
browser-default-handler detection). The only path that fires **without user
action** when `claude` is launched inside the VS Code extension host is
**IDE auto-connect** (osascripts to identify/activate the editor). We never
use the IDE connection → declining is harmless = exact symptom.
- Gate confirmed: `if(!((autoConnectIde||…||CLAUDE_CODE_SSE_PORT||…||
CH(CLAUDE_CODE_AUTO_CONNECT_IDE)) && !mK(CLAUDE_CODE_AUTO_CONNECT_IDE))) return;`
with `mK("0")===true` → `CLAUDE_CODE_AUTO_CONNECT_IDE=0` hard-disables it.
4. **Fix design (verified end-to-end, statically).** Pass `options.env` to the
Agent. The provider merges `{...getBaseProcessEnv(), ...settings.env}` and the
agent-SDK spawns the child with `env = L4 ? {...L4} : {...process.env}`. Seeding
`options.env` with the full `process.env` keeps default inheritance intact
(login under HOME, proxy/CA vars) and adds only `CLAUDE_CODE_AUTO_CONNECT_IDE=0`
(+ `CLAUDE_CODE_IDE_SKIP_AUTO_INSTALL=1`, belt-and-suspenders for the
extension-install deep link). `options` type is `Record<string,unknown>` — no
cast.
5. **TDD.** RED: unit test asserts `runEditTurn` constructs the Agent with both
switches set and the rest of `process.env` preserved (fake Agent captures
`cfg`). Watched it fail (`env` undefined). GREEN: added `options.env` to the
Agent constructor in `liveTurn.ts`. 250 unit + typecheck + build green.
6. **Empirical live-turn smoke (`scripts/smoke-live-turn.mjs` + osascript shim)
was DECLINED** by the operator (it makes a live network/auth'd agent call).
Env-preservation rests on the static chain proof + the unit test instead.
7. **Ship.** Branch `s60-59-apple-events-prompt` → commit → push → **PR #68** →
squash-merge to main (`e671c4b`), branch deleted, **#59 auto-closed**.
Post-merge: 250 unit green on fresh main.
## Cut state
- **#59 SHIPPED** to main (squash `e671c4b`, PR #68; issue closed).
- `src/liveTurn.ts` (+19) and `test/liveTurn.test.ts` (+38) only.
- 250 unit + typecheck + build green; E2E unaffected (it stubs the turn, never
reaching the real spawn path).
- No implementation-plan artifact (small leaf bug) → nothing to archive in
`plans/`. No deploy pipeline stage (VS Code extension, no flotilla/PPE) →
change ships at merge.
## Deferred decisions
- **Did not run the real-turn empirical smoke** (operator declined the live
agent call). Alternative: run it with an osascript shim to observe the trigger
directly. Why proceeded: the env→spawn chain and gate semantics are proven by
static analysis, the plumbing is unit-tested, and the definitive confirmation
(fresh-TCC GUI check) is inherently an operator gesture per the issue's
acceptance — so the smoke was not load-bearing for shipping.
- **Set both `AUTO_CONNECT_IDE=0` and `IDE_SKIP_AUTO_INSTALL=1`** rather than the
single minimal switch. Alternative: auto-connect=0 alone (likely sufficient,
since auto-install rides the auto-connect flow). Why both: cheap, explicit,
both are no-ops outside an IDE; defense-in-depth against a separately-gated
install path.
## Operator plate (next session)
1. **Operator F5 smoke of #64** (F12 inline editor diff) — still pending from 0059.
2. **Fresh-TCC GUI smoke of #59** — reset macOS Automation TCC for the host app,
exercise a Claude edit, confirm the "control other applications" prompt is
gone and the edit still works.
3. Then a backlog item — **#58** (P2 story, self-contained) is the most actionable.
## Next-session prompt
```
/goal Operator-smoke #64 (F5) + fresh-TCC GUI smoke of #59, then plan-and-execute #58 (proposal accept/decline word-level diff — route proposalBlockHtml through wordMergedMarkdown)
```
@@ -1,13 +1,13 @@
# Session 0032.0 — Transcript
# Session 0061.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-12T01-20 (PST)
> Start: 2026-06-27T05-14 (PST)
> Type: planning-and-executing
> Status: **PLACEHOLDER — claimed at session start; finalized at session end.**
>
> This file reserves session ID 0032 for vscode-cowriting-plugin. The driver replaces this
> This file reserves session ID 0061 for vscode-cowriting-plugin. The driver replaces this
> body with the full transcript and renames the file to its final
> SESSION-0032.0-TRANSCRIPT-2026-06-12T01-20--<end>.md form at session end.
> SESSION-0061.0-TRANSCRIPT-2026-06-27T05-14--<end>.md form at session end.
## Launch prompt
@@ -0,0 +1,23 @@
# Session 0062.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-06-27T17-02 (PST)
> Type: brainstorming
> Posture: careful
> Claude-Session: 2c832f9a-a70c-4d71-ab13-c4f39d6eb595
> Checkout: /Users/benstull/git/benstull.org/benstull/vscode-cowriting-plugin
> Status: **PLACEHOLDER — claimed at session start; finalized at session end.**
>
> This file reserves session ID 0062 for vscode-cowriting-plugin. The driver replaces this
> body with the full transcript and renames the file to its final
> SESSION-0062.0-TRANSCRIPT-2026-06-27T17-02--<end>.md form at session end.
## Launch prompt
_(launch prompt not captured at claim time)_
## Deferred decisions
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Appended as the session runs; surfaced at
finalize. Empty if none._
@@ -0,0 +1,65 @@
# Session 0063.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-07-01T18-49 (PST)
> Type: brainstorming
> Posture: careful
> Claude-Session: 1a8fc96b-6f36-4102-be7b-decb53251b2b
> Checkout: /Users/benstull/git/benstull.org/benstull/vscode-cowriting-plugin-s0063
> Status: **PLACEHOLDER — claimed at session start; finalized at session end.**
>
> This file reserves session ID 0063 for vscode-cowriting-plugin. The driver replaces this
> body with the full transcript and renames the file to its final
> SESSION-0063.0-TRANSCRIPT-2026-07-01T18-49--<end>.md form at session end.
## Launch prompt
```
Examine the existing vs plugin, the prototype, and the half-baked spec that I used Opus to create and make a recommendation for a VS Code experience that allows humans and machines to collaborate on a document, with the machine making suggestions based on human input and the human able to approve, reject, or modify those suggestions. This should use as many VS Code UX paradigms as possible, use as many existing VS Code components or extensions, but create custom components/UX when needed. You can assume this is just Claude as the 'machine' to start
```
## Plan
> Anchor: design-only north-star recommendation (no tracker anchor; lineage =
> Epic #1 / F1F12 + session 0062's `coauthoring-native-surfaces.md` draft —
> same anchor treatment 0062 recorded)
1. Examine the three inputs: the shipped plugin (F1F12 code on `main`), the
prototype, and the half-baked Opus-authored spec (untracked `specs/` in the
primary checkout).
2. Identify/confirm the anchoring issue (§4.3 R1 gate) — capture one if none fits.
3. Walk the consequential UX decisions with the operator (AskUserQuestion
checkpoints), grounded in native VS Code paradigms vs custom components.
4. Draft the recommendation as a Solution-Design spec in the content repo's
`specs/`; whole-document review at the Gitea render URL.
## Session record (running)
- Examined the three inputs: shipped plugin (F1F12 inventory), prototype
(`vscode-cowriting-prototype` = rung-2 spike: E1/E3/E5 PASS — key finding:
CodeLens does not render in a diff editor; E2/E4 unbuilt), and the 0062 draft
`coauthoring-native-surfaces.md` v0.1.0 + companion spike plan.
- Recommendation delivered + four forks decided with the operator
(AskUserQuestion checkpoints, all recommended options chosen):
1. Review model = **in-buffer pending changes + per-hunk CodeLens** (D18;
supersedes multi-diff Keep/Undo; amends INV-5/INV-12; resolves Q2/Q7).
2. Packaging (Q1) = **evolve the shipped extension in place** (D17; §6.10
rewritten from "greenfield").
3. Ask surface = **comments-first** (D19; split-pane input webview sunsets).
4. Artifact = **amend the 0062 spec to v0.2.0** on content-repo branch
`session-0063` (0062's working tree untouched). Also D20 (tweaks × per-hunk
decisions), D21 (D11 → shipped reality).
- Spec v0.2.0 + spike-spec status committed (`431ebeb`) and pushed to
`session-0063`; whole-document review handed to the operator at the Gitea
render URL (terminal gate).
## Deferred decisions
- Proceeded past the concurrent-session warning (0061/0062 both INPROGRESS,
placeholder-only claims from 2026-06-27) because the operator explicitly
launched this session; isolated in worktree `session-0063` per #134/§5.4.
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Appended as the session runs; surfaced at
finalize. Empty if none._
@@ -0,0 +1,58 @@
# Session 0064.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-07-01T22-19 (PST) · End: 2026-07-02T05-34 (PST)
> Type: writing-plans
> Posture: careful
> Claude-Session: 7c03a44e-1f92-4445-afd9-928efd55afba
> Checkout: /Users/benstull/git/benstull.org/benstull/vscode-cowriting-plugin-wp (isolated worktree; torn down at finalize)
> Anchor: design specs/coauthoring-native-surfaces.md (v0.2.1, graduated)
## Launch prompt
> Got it. Let's implement the full plugin
> (operator, after driving the rung-2 feasibility spike; context: evolve the shipped
> extension in place onto specs/coauthoring-native-surfaces.md, D17/§6.10/§7.1)
## Pre-state
- Native-surfaces Solution Design at **v0.2.0, draft**, on content-repo branch `session-0063` (session 0063 INPROGRESS, awaiting operator review). Content `main` had never received the 0062/0063 spec lineage.
- Rung-2 feasibility spike: E1/E3/E5 PASS (recorded in v0.2.0); **E2/E4 unvalidated**. Earlier in this same conversation (pre-session), the throwaway prototype `vscode-cowriting-prototype` was rebuilt to drive the FULL v0.2.0 loop (enter → ask-via-comment → pending-in-buffer → tweak → baseline/diff → preview → stop) and the operator drove it end to end.
- Three concurrent INPROGRESS sessions (0061/0062/0063); 0062 occupied the main plugin checkout → this session claimed from an isolated worktree (#134 gate).
## Arc (turn by turn, condensed)
1. **Spike driving (pre-claim, same conversation):** operator drove the walkthrough; two E4 defects surfaced and were fixed live in the prototype — (a) "Make this edit" gated on a nonexistent `commentThreadContextValue` key (real key: **`commentThread`**); (b) Ask-Claude didn't focus the comment input (fixed via **`workbench.action.addComment`**); (c) "cursor must be within a commenting range" → commenting ranges are **cached per document**; re-assigning `commentingRangeProvider` is the only refresh signal. All recorded in the prototype's FINDINGS.md.
2. **Gate + claim:** operator asked to "implement the full plugin" → `wgl-writing-plans` init; claim refused the shared checkout (live 0062) → isolated worktree `vscode-cowriting-plugin-wp`, claimed **0064** (`--type writing-plans`, careful).
3. **Eligibility:** `check-issue-eligibility.sh --for implementation-plan --design …coauthoring-native-surfaces.md`**ELIGIBLE (R2a)**. Design-readiness: N/A (spec §5.1 = Code directly, no UI design).
4. **Sequencing decision (operator):** fold spike findings first, then plan. Operator confirmed the drive reached step 5+ → **E2 PASS**; E4 **PASS-with-fallback**.
5. **Spec v0.2.1 delta** (content worktree branch `session-0064` off `session-0063`): §6.4 spike-verified API seams (commenting-range refresh, addComment focus, `commentThread` when-key, offer affordances; baseline nested-repo + `state.HEAD.commit` notes), §7.1 rung 2 closed (E2 PASS / E4 PASS-with-fallback; D15 preview-selection half explicitly un-spiked), §7.3 risks updated. Pushed; operator **approved → graduated**.
6. **Plan written** (`superpowers:writing-plans`; codebase mapped via an Explore subagent + direct reads): `docs/superpowers/plans/2026-07-01-native-surfaces-migration.md` — ONE plan, 9 tasks in §7.1 rung-4 order: (1) CoeditingRegistry (INV-10) → (2) baseline router git-HEAD+snapshot (INV-7; **retires INV-18** machine-landing advance) → (3) QuickDiff + `cowriting-baseline:` + Review Changes + status bar (INV-13) → (4) gate every surface → (5) extract EditFlow from the review webview → (6) comments-first ask + comment→reply→offer→proposal loop (D19/D10/D8; sunsets the input webview) → (7) built-in-preview annotations via markdown-it sentinels (D3/D21; Q4 mermaid stop-and-record check) → (8) sunset the review webview + ✓ Keep/✗ Reject copy + README → (9) full-loop E2E + real-SDK smoke (rung 3 folded in). D15 explicitly deferred. Committed to plugin branch `session-0064`, pushed.
7. **Review gate:** operator approved — explicitly accepting the **D15 deferral** and the **INV-18 retirement** — and directed execution to a new session.
8. **Finalize (single approved plan):** content `session-0064` merged → content `main` (landed the whole 0062/0063/0064 spec lineage; flagged), plugin `session-0064` fast-forwarded → plugin `main`; plan archived via `submit-plan.sh` → content `plans/2026-07-01-native-surfaces-migration.md` (ddaf3b0); **issue #69** filed (D15, feature P2, resume context in body); both session worktrees removed; memory updated.
## Deferred decisions
_None — the two judgment calls (D15 deferral, INV-18 retirement) were put to the operator explicitly and approved at the plan gate._
## Cut state
- **Spec:** `coauthoring-native-surfaces.md` **v0.2.1 `graduated`**, on content `main`.
- **Plan:** approved + archived at content `plans/2026-07-01-native-surfaces-migration.md`; also on plugin `main` under `docs/superpowers/plans/`. **Not executed** (by design — writing-plans is terminal).
- **Spike:** complete (5/5); prototype **retained deliberately** (operator: iterate UX there before the real build) — supersedes the spike-plan's delete-when-done for now.
- **Tracker:** #69 (D15 preview-initiated comments, feature P2) parked with full resume context.
- Leftover: merged-but-undeleted local branch refs (`session-wp-native-surfaces` in the plugin clone, `session-0064` in the content clone) — operator declined force-deletion; plain `-d` from a main checkout clears them. Sessions 0061/0062/0063 remain INPROGRESS (0062/0063 will find their content branches already merged).
## Operator plate
- Open the executing session (below) when ready.
- Sessions 0061/0062/0063 are still unfinalized — worth closing out.
- Optional tidy: delete the two stale local branch refs.
## Next session
```
/goal Execute the native-surfaces migration plan (content repo plans/2026-07-01-native-surfaces-migration.md, 9 tasks, from graduated spec coauthoring-native-surfaces.md v0.2.1) end to end
```
(Open with `wgl-executing-plans`; the plan's tasks run 1→9 strictly, each ending typecheck+unit+host-E2E green; Task 9's live smoke is the operator-run rung-3 gate.)
@@ -0,0 +1,68 @@
# Session 0065.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-07-02T06-31 (PST)
> End: 2026-07-02T16-10 (PST)
> Type: executing-plans
> Posture: yolo
> Claude-Session: 32dcda47-7361-4fa7-8145-9d118b5de3cc
> Checkout: /Users/benstull/git/benstull.org/benstull/vscode-cowriting-plugin/.claude/worktrees/session-native-surfaces-exec (isolated worktree, branch `session-0065`)
> Status: FINALIZED
## Launch prompt
> The last session should have set a goal. If not, find the spec that we should be implementing
## Plan
- **Anchor:** graduated Solution Design `coauthoring-native-surfaces.md` v0.2.1 (content repo, on main) — R2(a), ELIGIBLE per `check-issue-eligibility.sh`; lineage: sessions 0062 (spec) → 0063 (v0.2.0) → 0064 (spike + plan approved).
- **Goal:** execute the reviewed implementation plan `plans/2026-07-01-native-surfaces-migration.md` — 9 tasks in §7.1 rung-4 order — and land it (no §9 web pipeline: VS Code extension, spec §7.2; done = merged + suites green + rung-3 smoke).
- **Isolation:** sessions 0061/0062/0063 live-concurrent; 0062 occupied the primary checkout → claim refused shared tree (plugin #134) → EnterWorktree `session-native-surfaces-exec`, branch renamed `session-0065`.
- Approved deliberate changes carried by the plan: INV-18 retirement (no machine-landing baseline advance); D15 deferred as issue #69.
## Pre-state
- Plugin repo `main` at `ef61b14` (0064's plan-archive commit); clean; 265 unit + full host E2E green at baseline (verified before any work).
- Content repo `main` at `ddaf3b0` (0064 lineage merged); local content clone behind origin + carrying other sessions' uncommitted drafts — never touched; spec read via `git show origin/main:`.
## Arc (turn-by-turn, compressed)
1. **Init:** goal recovered from memory (0064 note). Claim dry-run → shared-checkout refusal → worktree isolation → claimed **0065** (`26b2212`). Anchor gate: ELIGIBLE (R2a). Baseline verified green.
2. **Execution model:** superpowers:subagent-driven-development — fresh implementer subagent per task, task-scoped reviewer (spec + quality verdicts), fix subagents on findings, re-review loops; durable ledger `.superpowers/sdd/progress.md`; final whole-branch review at the end. Models tiered (sonnet implementers; opus for risky-diff reviews; fable for the final review).
3. **Task 1 — CoeditingRegistry (INV-10 gate):** `2597cba`; review found untested `syncContext` context-key derivation (plan-mandated gap) → fix `de83757` (11 unit tests). Approved.
4. **Task 2 — baseline router (INV-7):** `447a117`. Deviations approved by review: `.git/logs/HEAD` reflog watcher nudging `repo.status()` (vscode.git's watcher missed external commits in the host; change detection still keyed off `state.HEAD.commit`), E2E suites gaining enter-calls early. Machine-landing advance deleted; `markReviewed` replaces `pinDiffBaseline`. Approved.
5. **Task 3 — native diff surface:** `cf65528` (QuickDiff, `cowriting-baseline:` provider, Review Changes, status bar, pure `countLineHunks`). Approved.
6. **Task 4 — gate every surface:** `a323b82`; review confirmed a PUC-7 restore-on-enter gap (proposals/attribution) → fix `a36353d` (registry subscribers + regression E2E verified fail-without-fix). Approved.
7. **Plan-file repair:** fused `---### Task 5` heading broke the task extractor → `f23fc4a`.
8. **Task 5 — EditFlow extraction:** `ad5d34b`; review caught a real regression (palette accept/reject-all no longer repainted an open review panel) → fix `8fbbe45`. Approved.
9. **Task 6 — comments-first ask loop:** `75f4a3c`; review found a **Critical**: `cowriting.edit`'s no-selection route (⌘⌥E) dead-ended in the rejecting input stub → fix `c9975ba` (all ask entry points converge on `ThreadController.askClaude`; machine-author guard; offer-WeakMap cleanup). Approved. `editInstructionInput.ts` deleted.
10. **Task 7 — built-in preview annotations:** `17fc01e` (pure markdown-it plugin; sentinel machinery generalized 3-way, backward-compatible — 84/84 old tests unedited; render-wrapper approach correctly replacing the brief's per-token rule; mermaid rendering via `markdown.previewScripts` — Q4 confirmed). Approved; intra-diagram diff scoped out by the implementer.
11. **Task 8 — webview sunset:** `2170a0d` (1472 lines; coverage moved in-commit, arithmetic reconciled exactly). Review flagged the Task-7 scope-out as an unsanctioned INV-29 retirement + `renderReview` going production-dead. **Adjudication:** plan T7 §2.6 mandated the re-emit and its stop condition never triggered → fix `705de31` (diffMermaid wired into the fence path, 6 ported unit tests) + hardening `ff42b5a` (body-matched queue; misalignment degrades to verbatim; parse-less re-render reset). `renderReview` kept deliberately (see Deferred decisions). Approved.
12. **Task 9 — full-loop E2E + smoke:** `22c9da0` (PUC-7→8→2→1 walk; `scripts/smoke-native-loop.mjs` + `npm run smoke:native`, operator-run). Review verified the implementer's adjudication (interior tweak → anchor orphans by contract; Keep-by-id survives) and surfaced a **pre-existing INV-5 gap** (reject-after-interior-edit) → plan text corrected (`17366a4`) + **issue #70 filed** (bug P2).
13. **Final whole-branch review (fable):** verdict "with fixes" — Critical (preview-annotation host leaked past INV-10: wrong-doc annotation + stale-after-exit), Important (thread contextValue conflation made Resolve/Reopen permanently unmatchable once Claude replied; coedited docs opened post-activation never got a baseline) → ONE fix wave `0d69a29` (all three at root + regression E2Es + fold-in minors + polish). Re-review: full when-clause state matrix verified, no double-establish race, **Ready to merge: Yes**. **Issue #71 filed** (surface-polish batch).
14. **Ship:** final verification (typecheck, 301 unit, 90+5 E2E, build — all green) → pushed → **PR #72****squash-merged to main `935fcc3`** → remote branch deleted → plan re-archived to content repo (`b0687b6`).
## Deferred decisions
- **`renderReview` left in place though production-dead after the Task 8 webview sunset.** The old F10 pure render engine (`renderReview`/`renderOp` in `trackChangesModel.ts`) lost its last production consumer when `trackChangesPreview.ts` was deleted; three E2E suites (authorship, s48PinClean, undoMarks) now probe it directly as a pure module via a test helper. I kept it (tested pure module, INV-33 documentation value, low carrying cost) rather than deleting + reworking those suites mid-migration. Operator may prefer deletion as a follow-up cleanup.
- **INV-29 mermaid-diff parity adjudicated as plan-mandated, not retired.** Task 7 scoped out the F7.1 re-emit for testability; the plan's stop condition (previewScripts unworkable) never triggered, so I dispatched a fix to wire `mermaidDiff` into the preview fence path rather than accepting the silent retirement. Judged from plan T7 §2.6 + the plan's spec-coverage self-check.
## Cut state
- Plugin `main` = `935fcc3` (PR #72 squash). Working tree clean. Remote `session-0065` deleted; **local branch ref `session-0065` remains** (squash → plain `-d` refuses; operator has preferred manual cleanup of such refs).
- Content repo `main` = `b0687b6` (plan re-archived with in-execution edits: heading fix + Task 9 wording correction).
- Suites at HEAD: typecheck · 301 unit · 90 + 5 host E2E (1 env-conditional #54 undo-probe pending on some runs) · esbuild — all green.
- Issues: **#70 filed** (INV-5 reject-after-interior-edit, bug P2), **#71 filed** (surface polish, task P3); #69 (D15) pre-existing.
- Sessions 0061/0062/0063 still INPROGRESS (other Claude sessions); their checkouts/branches untouched.
## Operator plate
1. **`npm run smoke:native`** — rung-3 real-SDK smoke through the comment loop (one green run completes the plan's definition of done; report it to this transcript's home if you want the record complete).
2. **Mermaid-preview GUI smoke** — open a coedited doc with a changed ```mermaid fence in the built-in preview; confirm diagram renders with cwAdded/ghost styling + legend (previewScripts path isn't E2E-queryable).
3. Optional cleanup: `git branch -D session-0065` in the plugin clone; delete the now-merged worktree leftovers if any; #71 when convenient.
## Next-session prompt
```
/goal Plan and execute #70 — fix the INV-5 reject-after-interior-edit gap (proposalController revertInPlace orphan path); fix directions in the issue body
```
@@ -0,0 +1,113 @@
# Session 0066.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-07-02T16-24 (PST)
> End: 2026-07-02T18-54 (PST)
> Type: planning-and-executing
> Posture: yolo
> Claude-Session: 14c9f9cc-92a1-47ef-a12d-9b0ac2469e13
> Checkout: /Users/benstull/git/benstull.org/benstull/vscode-cowriting-plugin/.claude/worktrees/fix-70-inv5-reject-gap
> Status: FINALIZED
## Launch prompt
```
/goal Plan and execute #70 — fix the INV-5 reject-after-interior-edit gap (proposalController revertInPlace orphan path); fix directions in the issue body
```
## Plan
> Anchor: #70 (type/bug, P2 — ELIGIBLE R2b)
Fix the INV-5 reject-after-interior-edit gap: `revertInPlace` in
`src/proposalController.ts` skipped the restore when an interior edit inside
the pending proposal's range orphaned the exact-substring anchor (INV-11),
yet removed the proposal and reported success — ✗ Reject silently left the
(edited) proposed text in the buffer. Evaluate the issue's fix directions
(a/b/c), TDD the fix, ship branch → PR → merge per §5.4.
## Pre-state
- `origin/main` at `d8f2d73` (session 0065's native-surfaces migration
shipped; #70/#71 filed from its final review).
- Three concurrent sessions in flight (0061, 0062, 0063 placeholders);
session 0062 occupied the canonical checkout, so the #134 claim guard
refused a shared-tree claim — this session created worktree
`.claude/worktrees/fix-70-inv5-reject-gap` off `origin/main` FIRST and
claimed 0066 from inside it (the inverted order the init documents).
- Canonical checkout's untracked files (`specs/`, a plan doc) belong to the
concurrent session and were left untouched.
## Arc
1. **Init.** Claimed 0066 from the worktree; #70 checked ELIGIBLE (type/bug,
R2b; needed `--host git.benstull.org` + the issues token service). Fetched
the issue body carrying the three fix directions.
2. **Design.** Explored `proposalController.ts` / `anchorer.ts` /
`editorProposalController.ts`. Chose direction **(a)** — tracked applied
range — with **(b)** honest hard failure as fallback; rejected **(c)**
fuzzy resolve (INV-11: never guess). Key insight: `state.live` is
shift-maintained but clobbered to a collapsed placeholder by any
intervening `renderAll`, so a dedicated `appliedSpans` map with explicit
trust semantics was designed instead.
3. **Plan.** `docs/superpowers/plans/2026-07-02-70-inv5-reject-after-interior-edit.md`
(4 TDD tasks). Fused single-session path (leaf bug), executed inline.
4. **Tasks 14** (commits `a9f8dd0`, `82cea74`, `ddd634b`, `f47dfbc`):
appliedSpans + revertInPlace fallback (E2E repro failed-then-passed);
hard-failure lock test (first cut used a whole-buffer rewrite and FAILED —
VS Code minimizes workspace edits, so it decomposed into interior hunks
the span legitimately survives; switched to a boundary-straddling
deletion); rejectAll `{reverted, skipped}` parity; fullLoop reject leg now
tweaks inside the pending range first (retiring its deliberate avoidance).
5. **Review wave** (high-effort multi-agent workflow; 10 CONFIRMED findings,
9 fixed in `e389406`): pure `shiftTracked` in anchorer (+11 unit tests;
insertion-at-span-start lands BEFORE the span), tracked spans cleared on
doc close (disk can change with no events), rebuild-only resync (duplicate
-text resolve can't clobber continuous tracking), `guard.isReadOnly` on
finalize/revert (INV-16), "Discard proposal (leave text)" action on the
hard-fail warning (reload leaves no other dismissal path), CodeLens pair
anchored at the tracked span (the fixed path stays reachable from the
primary gesture), QuickPick batch menu routed through the reporting
commands, one `clearProposal` helper (fixes `reject()`'s stale bookkeeping).
The 10th finding is pre-existing → filed **#74**.
6. **Ship.** PR #73 → squash-merged to `main` as `7583165`; #70 auto-closed.
312 unit + 94/5 host E2E green (1 occasionally pending = the known #54
undo-capability probe). Plan archived to the content repo
(`plans/2026-07-02-70-inv5-reject-after-interior-edit.md` at `0019d58`).
Remote branch `s70-inv5-reject-gap` deleted after merge.
## Deployment pipeline (§9)
This app is a VS Code extension with no deploy infra (no flotilla/PPE stage
exists — per prior sessions, "no deploy pipeline (extension, no flotilla)").
The localhost stage ran in full: typecheck + 312 unit + 94/5 host E2E green.
Nothing to deploy; no release tag applies.
## Cut state
- `origin/main` = `7583165` (the #70 fix). Worktree branch merged; remote
branch deleted; worktree torn down at finalize.
- Issue #70 CLOSED (auto, via PR). Issue #74 (bug P3) OPEN — filed this
session: `resolve()` accepts a single exact occurrence without a context
check (pre-existing; duplicated block can hijack any anchor consumer).
- Concurrent sessions 0061/0062/0063 still INPROGRESS — not mine to adopt
(live, per their stamps).
## Operator plate
- **Manual smoke owed:** the "Discard proposal (leave text)" warning button
(native notification — not E2E-testable) + the smokes already owed from
sessions 0062/0065.
- Prod promotion: N/A (no deploy pipeline for the extension).
## Deferred decisions
No low-confidence calls this session. (Design forks — (a)+(b) over (c), the
distrust semantics, squash-merge — all followed the issue's own directions,
existing invariants, or repo convention.)
## Next-session prompt
```
/goal Plan and execute #71 — surface-polish batch from the native-surfaces final review (6 small items listed in the issue body)
```
@@ -0,0 +1,109 @@
# Session 0067.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-07-02T20-07 (PST)
> Type: planning-and-executing
> Posture: yolo
> Claude-Session: d29a9976-cb2d-427b-9ef1-4488b3c2a262
> Checkout: /Users/benstull/git/benstull.org/benstull/vscode-cowriting-plugin/.claude/worktrees/s71-surface-polish
> End: 2026-07-03T07-39 (PST)
> Status: **FINALIZED**
## Launch prompt
```
Plan and execute #71 — surface-polish batch from the native-surfaces final review (6 small items listed in the issue body)
```
## Plan
> Anchor: #71 (type/task, P3 — ELIGIBLE R2b)
Plan and execute #71 — the surface-polish batch from the native-surfaces
migration final review (session 0065). Six small items from the issue body:
1. `markReviewed`/`pin()` misleading warning on never-established docs (`src/diffViewController.ts`)
2. Delete orphaned `cowriting.acceptProposal`/`rejectProposal` declarations (`package.json`)
3. `openReviewPreview` editor/title when-key: `editorLangId``resourceLangId`
4. README F3 section drift: SUPERSEDED banner + stale chord references
5. Gate/warn `cowriting.createThread` palette entry when not coediting
6. Clear `gitBaseline` reflog debounce timer in `dispose()` (`src/gitBaseline.ts`)
Fused session (trivial leaf batch): just-in-time plan → execute → tests green
→ PR → merge. Isolated worktree `s71-surface-polish` (checkout contended by
live session 0062).
## Execution record
- Plan written (`docs/superpowers/plans/2026-07-02-71-surface-polish.md`, committed
on the branch) and executed inline — all six items landed as one commit each:
1. `pin()` not-coediting branch → "Run ✦ Coedit this Document with Claude first."
2. orphaned `acceptProposal`/`rejectProposal` declarations + `when:false` palette
entries deleted
3. `openReviewPreview` editor/title when-key `editorLangId``resourceLangId`
(+ the E2E assertion at `reviewPanelMenu.test.ts:129` that codified the old
key — the one mid-run E2E failure, fixed forward)
4. README: F3 superseded-commands banner; F7/F10 banner chord notes
5. `createThread` palette entry gated (same when as its editor/context entry)
6. `gitBaseline` reflog debounce timer cleared on dispose
- Verification: 312 unit + typecheck + build + full host E2E green.
- Shipped: PR #75 squash-merged to `main` as `3e7ad58`; issue #71 auto-closed;
remote branch deleted. No flotilla/PPE pipeline — this app is a VS Code
extension with no deployed service (§9 ends at merge + green suites here).
## Post-ship arc (operator review + Q&A)
- Launched the EDH from the worktree for operator review
(`code --extensionDevelopmentPath=<worktree> <worktree>/sandbox`; VS Code
found at `~/Downloads/Visual Studio Code.app`).
- Operator Q&A, all assessment-only: (1) comment box is the native Comments
API widget — Cmd+Enter submits, Enter newlines; operator chose to keep the
native mapping. (2) macOS "access data from other apps" TCC prompt — App
Data Protection category, distinct from #59; VS Code found QUARANTINED +
App-Translocated (runs from ~/Downloads) — recommended move to
/Applications; #59 env guard confirmed present (`liveTurn.ts:82-83`).
(3) External (outside-editor) Claude edits: detected via the disk-sync
branch (`attributionController.ts:175`), rendered neutral, never attributed
— cross-rung sidecar is the designed path to attributed external edits.
(4) `onDidChangeTextDocument` carries no originator identity (why the seam
pre-registers edits, INV-9). (5) Sketched per-author diff surfaces
(synthetic per-author baselines + second QuickDiffProvider). (6) Explained
tweak-before-accept tracking (mixed authorship; #70 appliedSpans; reject
reverts whole span or refuses).
- The Q&A converged on a product pivot the operator dictated: simplify review
to baseline-diff Keep/Reject (three-verb toolbar, no annotations, no
preview). Handed off to capture session 0068 (draft written, NOT filed) and
an implementation prompt.
## Deferred decisions
- design-readiness soft gate returned MISSING for #71 — proceeded: all six
items are copy/menu-gating/cleanup within existing surfaces, no new UI
beyond the design system (R4 not implicated).
## Closeout
Shipped: PR #75 (squash `3e7ad58`), issue #71 closed, remote branch deleted.
Suites: 312 unit + typecheck + build + full host E2E green (one mid-run E2E
fix: `reviewPanelMenu.test.ts:129` had codified the wrong when-key). Plan
archived to content repo `plans/2026-07-02-71-surface-polish.md`. No
flotilla/PPE stage exists for this app (VS Code extension, no deployed
service) — §9 ends at merge + green suites. Worktree `s71-surface-polish` is
merged-clean but KEPT at finalize: the operator's EDH review window still runs
from it (`--extensionDevelopmentPath`); remove after closing that window
(`git worktree remove .claude/worktrees/s71-surface-polish`). Operator plate:
close the EDH then remove the worktree; move VS Code.app out of ~/Downloads to
/Applications (translocation/TCC); decide on the 0068 unfiled draft.
## Next-session prompt
/goal Resume the capture: file the unfiled draft
issues/simplify-review-baseline-diff-keep-reject.md as type/feature P1 on
benstull/vscode-cowriting-plugin, then brainstorm its Solution Design
(specs/coauthoring-baseline-diff-review.md) on the new issue, locking the five
forks in the draft's Solution notes.
_Autonomous-mode low-confidence calls the driver made and would have
liked operator input on. Appended as the session runs; surfaced at
finalize. Empty if none._
@@ -0,0 +1,50 @@
# Session 0068.0 — Transcript
> App: vscode-cowriting-plugin
> Start: 2026-07-03T06-53 (PST)
> Type: capture
> Posture: careful
> Claude-Session: d29a9976-cb2d-427b-9ef1-4488b3c2a262
> Checkout: /Users/benstull/git/benstull.org/benstull/vscode-cowriting-plugin/.claude/worktrees/s71-surface-polish
> End: 2026-07-03T07-39 (PST)
> Status: **FINALIZED — tracked-lite (capture); NOTHING FILED**
## Launch prompt
```
Capture one type/feature issue: simplify the review model to baseline-diff Keep/Reject — comments to ask Claude, native diff to review (per-hunk Keep/Reject/Modify, Keep advances the baseline), no annotations, no preview surface; toolbar reduces to Ask Claude / Review Changes / Mark All Changes as Reviewed. Context developed in conversation following session 0067; brainstorming session to follow on the filed issue.
```
## Arc
- Claimed 0068 (capture, careful) from the s71 worktree; resolved content repo
`benstull/vscode-cowriting-plugin-content`. Detector listed ~24 pre-#80
unfiled-marker drafts for long-filed issues (operator-owned backlog hygiene
— untouched).
- Drafted `issues/simplify-review-baseline-diff-keep-reject.md` per the
feature batching profile: business block → operator approved ("Looks
good") → structural block + solution notes (approach pre-validated in the
originating 0067 conversation; five design forks recorded).
- **Filing withheld (INV-1):** at the file-on-approval gate the operator
pivoted — "Just give me the prompt for implementing" — so NO issue was
filed. Delivered the implementation prompt (unattended design→build chain
with named yolo override, plus the two-step interactive variant), with `#N`
pending the eventual filing.
- Finalize: draft left in the content-repo working tree as the resumable
surface (never auto-discarded); type-fit judge not run (nothing filed);
type-fit: judged 0, mismatch 0, overridden 0.
## Deferred decisions
_None — careful posture; every gate was operator-answered. The one open
operator decision (file the draft or not) is deliberately left open and
carried in the next-session prompt._
## Next-session prompt
/goal Resume the capture: file the unfiled draft
issues/simplify-review-baseline-diff-keep-reject.md as type/feature P1 on
benstull/vscode-cowriting-plugin, then brainstorm its Solution Design
(specs/coauthoring-baseline-diff-review.md) on the new issue, locking the five
forks in the draft's Solution notes.
+108
View File
@@ -94,5 +94,113 @@
},
"0032": {
"title": ""
},
"0033": {
"title": ""
},
"0034": {
"title": ""
},
"0035": {
"title": ""
},
"0036": {
"title": ""
},
"0037": {
"title": ""
},
"0038": {
"title": ""
},
"0039": {
"title": ""
},
"0040": {
"title": ""
},
"0041": {
"title": ""
},
"0042": {
"title": ""
},
"0043": {
"title": ""
},
"0044": {
"title": ""
},
"0045": {
"title": ""
},
"0046": {
"title": ""
},
"0047": {
"title": ""
},
"0048": {
"title": ""
},
"0049": {
"title": ""
},
"0050": {
"title": ""
},
"0051": {
"title": ""
},
"0052": {
"title": ""
},
"0053": {
"title": ""
},
"0054": {
"title": ""
},
"0055": {
"title": ""
},
"0056": {
"title": ""
},
"0057": {
"title": ""
},
"0058": {
"title": ""
},
"0059": {
"title": ""
},
"0060": {
"title": ""
},
"0061": {
"title": ""
},
"0062": {
"title": ""
},
"0063": {
"title": ""
},
"0064": {
"title": ""
},
"0065": {
"title": ""
},
"0066": {
"title": ""
},
"0067": {
"title": ""
},
"0068": {
"title": ""
}
}
+24
View File
@@ -101,6 +101,30 @@ export function resolve(docText: string, fp: Fingerprint): OffsetRange | "orphan
return "orphaned";
}
/**
* #70 (INV-5/INV-11): maintain a TRACKED applied span across an in-session edit.
* Unlike `shift`, which clamps every point into a best-effort range, a tracked
* span must stay *provably* meaningful — it is a revert target. So:
* - an edit fully OUTSIDE or fully INSIDE the span shifts it (the span is
* still "the applied block, as tweaked");
* - an insertion exactly AT the span start lands BEFORE the span (both ends
* shift right) — matching exact-resolve semantics, where text typed just
* before the applied block is never part of it;
* - any edit STRADDLING a span boundary returns "distrusted": the shifted
* result would be a clamped guess, and stale text is never reverted by
* guess (INV-11).
*/
export function shiftTracked(range: OffsetRange, edit: TextEdit): OffsetRange | "distrusted" {
const delta = edit.newLength - (edit.end - edit.start);
if (edit.start === edit.end && edit.start === range.start) {
return { start: range.start + delta, end: range.end + delta };
}
const outside = edit.end <= range.start || edit.start >= range.end;
const inside = edit.start >= range.start && edit.end <= range.end;
if (!outside && !inside) return "distrusted";
return shift(range, edit);
}
/** Maintain a live range across an in-session edit (spec §6.4 `shift`). */
export function shift(range: OffsetRange, edit: TextEdit): OffsetRange {
const delta = edit.newLength - (edit.end - edit.start);
+77 -19
View File
@@ -21,6 +21,7 @@ import { minimizeReplace, PendingEditRegistry } from "./pendingEdits";
import type { VersionGuard } from "./versionGuard";
import { isAuthorable } from "./workspacePath";
import type { AuthorSpan } from "./trackChangesModel";
import type { CoeditingRegistry } from "./coeditingRegistry";
/** Test-facing snapshot of live attribution state for a document. */
export interface RenderedSpan {
@@ -56,8 +57,11 @@ export class AttributionController implements vscode.Disposable {
/**
* F6 (§6.2/§6.4): the single machine-landing signal. Fired after a real
* (non-no-op) seam apply succeeds. INV-9 makes the seam the sole machine-edit
* ingress, so this is the sole signal — DiffViewController subscribes to
* advance the baseline; no call-site wiring, no future driver can forget it.
* ingress, so this is the sole signal. Native-surfaces migration (INV-7/
* INV-18 retirement, spec §6.4): DiffViewController no longer subscribes
* here to advance the baseline — a landed edit stays a visible
* change-since-baseline until commit (head mode) or "Mark Changes as
* Reviewed" (snapshot mode). Other consumers may still subscribe.
*/
private readonly applyEmitter = new vscode.EventEmitter<{ document: vscode.TextDocument }>();
readonly onDidApplyAgentEdit: vscode.Event<{ document: vscode.TextDocument }> = this.applyEmitter.event;
@@ -66,12 +70,35 @@ export class AttributionController implements vscode.Disposable {
private readonly store: SidecarRouter,
private readonly rootDir: string | undefined,
private readonly guard: VersionGuard,
private readonly registry: CoeditingRegistry,
) {
this.disposables.push(this.statusItem, this.output, this.applyEmitter);
this.disposables.push(
vscode.workspace.onDidChangeTextDocument((e) => this.onDidChange(e)),
vscode.workspace.onDidSaveTextDocument((d) => this.onDidSave(d)),
vscode.window.onDidChangeActiveTextEditor(() => this.renderActive()),
// PUC-7 restore-on-enter: `loadAll` is the only path that (re)populates
// s.spans/s.orphans from the sidecar — without this, a document's FIRST
// entry into coediting (registry.enter(), extension.ts) never runs it,
// so pre-existing committed authorship never surfaces until the next
// edit. Mirrors ThreadController's own registry subscription; `loadAll`
// already gates on isCoediting, and exit intentionally does nothing here
// (no data to wipe — EditorProposalController's own gate clears the
// decorations that read spansFor). Registered before ProposalController/
// EditorProposalController (construction order in extension.ts) so their
// renders see fresh spans on the same enter transition.
this.registry.onDidChange(({ uri, coediting }) => {
if (!coediting) {
// Finding 5 (final whole-branch review): exit hides the status-bar
// item too — otherwise a stale "N orphaned attributions" warning
// for a doc that's no longer even coediting stays pinned in the bar
// until some unrelated render happens to fire for it.
if (vscode.window.activeTextEditor?.document.uri.toString() === uri) this.statusItem.hide();
return;
}
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri);
if (doc) this.loadAll(doc);
}),
);
}
@@ -101,6 +128,7 @@ export class AttributionController implements vscode.Disposable {
/** Load the sidecar and re-resolve every attribution (live span | orphan). */
loadAll(document: vscode.TextDocument): void {
if (!this.isTracked(document)) return;
if (!this.registry.isCoediting(document.uri)) return;
const docPath = this.keyOf(document);
const s = this.state(docPath);
const artifact = this.store.load(docPath);
@@ -142,6 +170,7 @@ export class AttributionController implements vscode.Disposable {
private onDidChange(e: vscode.TextDocumentChangeEvent): void {
if (!this.isTracked(e.document) || e.contentChanges.length === 0) return;
if (!this.registry.isCoediting(e.document.uri)) return;
const docPath = this.keyOf(e.document);
if (!e.document.isDirty && this.matchesDisk(e.document)) {
// Disk sync (revert / external reload): buffer now equals the file on
@@ -152,18 +181,28 @@ export class AttributionController implements vscode.Disposable {
return;
}
const s = this.state(docPath);
// An undo/redo is history navigation, NOT authorship (#38): reconcile span
// geometry but never freshly attribute the re-inserted text to the current
// author — otherwise restored baseline text (or reverted Claude text) is
// falsely colored human in the preview. A seam edit is always a forward
// apply, so undo/redo also bypasses seam matching.
const isUndoRedo =
e.reason === vscode.TextDocumentChangeReason.Undo ||
e.reason === vscode.TextDocumentChangeReason.Redo;
// One applyEdit = one change event, but the host may deliver a seam edit
// as SEVERAL minimal hunks (word-level diffing). Match the EVENT's net
// effect against the registry; on a hit the agent owns its FULL intended
// replacement (INV-9) — apply it as ONE algebra edit, not per hunk.
const hit = this.pending.matchEvent(
docPath,
e.contentChanges.map((c) => ({
start: c.rangeOffset,
end: c.rangeOffset + c.rangeLength,
newLength: c.text.length,
})),
);
const hit = isUndoRedo
? null
: this.pending.matchEvent(
docPath,
e.contentChanges.map((c) => ({
start: c.rangeOffset,
end: c.rangeOffset + c.rangeLength,
newLength: c.text.length,
})),
);
if (hit) {
const full = hit.full ?? { start: hit.start, end: hit.end, newLength: hit.newText.length };
s.spans = applyChange(s.spans, full, hit.provenance, {
@@ -180,10 +219,16 @@ export class AttributionController implements vscode.Disposable {
end: change.rangeOffset + change.rangeLength,
newLength: change.text.length,
};
s.spans = applyChange(s.spans, edit, this.currentAuthor(), {
newId: () => newId("at"),
now: () => new Date().toISOString(),
});
s.spans = applyChange(
s.spans,
edit,
this.currentAuthor(),
{
newId: () => newId("at"),
now: () => new Date().toISOString(),
},
!isUndoRedo,
);
}
}
if (s.spans.length > 0) s.hadAttributions = true;
@@ -233,7 +278,7 @@ export class AttributionController implements vscode.Disposable {
range: vscode.Range,
newText: string,
provenance: Provenance,
opts?: { expectedVersion?: number; turnId?: string },
opts?: { expectedVersion?: number; turnId?: string; landBaseline?: boolean },
): Promise<boolean> {
if (!this.isTracked(document)) return false;
if (opts?.expectedVersion !== undefined && document.version !== opts.expectedVersion) return false;
@@ -274,15 +319,28 @@ export class AttributionController implements vscode.Disposable {
"(host minimized differently?) — the edit may be mis-attributed (INV-9).",
);
}
if (ok) {
// F6 (INV-18): a real machine landing — signal the baseline to advance so
// this text never shows as a change in the diff view. Fire regardless of
// attribution-match bookkeeping above; the landing happened either way.
if (ok && opts?.landBaseline !== false) {
// F6 (INV-18, retired as a baseline advance — spec §6.4/INV-7): fire the
// machine-landing signal. F12 (INV-48) suppresses this for optimistic
// apply: the proposed text is in the buffer but the change stays PENDING
// until accept.
this.applyEmitter.fire({ document });
}
return ok;
}
/**
* F12/#64 (INV-51): fire the machine-landing signal WITHOUT applying text — used
* by finalize-in-place, where the proposed text already landed in the buffer via
* optimistic apply (`landBaseline:false`) and accept marks the landing so the
* now-accepted change is recorded (INV-7: the baseline itself no longer
* advances on landing — it advances only on commit / "Mark Changes as
* Reviewed").
*/
signalLanded(document: vscode.TextDocument): void {
if (this.isTracked(document)) this.applyEmitter.fire({ document });
}
// ---- PUC-4: persistence on save ----------------------------------------------------
private onDidSave(document: vscode.TextDocument): void {
+8 -1
View File
@@ -49,12 +49,19 @@ export function coalesce(spans: LiveSpan[]): LiveSpan[] {
* Apply one document edit (the half-open range [start,end) replaced by
* newLength chars) authored by `author`. Existing spans shift/split/clip;
* inserted chars become a new span of `author` (INV-7).
*
* `attributeInserted` (default true) controls whether inserted chars get a new
* span. Pass `false` for an UNDO/REDO change (#38): the geometry of existing
* spans is still reconciled, but re-inserted text is NOT freshly attributed —
* an undo is history navigation, not authorship, so restored text stays neutral
* rather than being falsely claimed by the current author.
*/
export function applyChange(
spans: LiveSpan[],
edit: TextEdit,
author: Provenance,
ctx: TrackerCtx,
attributeInserted = true,
): LiveSpan[] {
const delta = edit.newLength - (edit.end - edit.start);
const out: LiveSpan[] = [];
@@ -71,7 +78,7 @@ export function applyChange(
if (right) out.push(left ? { ...right, id: ctx.newId() } : right);
}
}
if (edit.newLength > 0) {
if (attributeInserted && edit.newLength > 0) {
out.push({
id: ctx.newId(),
start: edit.start,
+17 -2
View File
@@ -7,11 +7,19 @@
* key is a hash of the document URI (the controller derives it), not a
* workspace-relative path; an untitled buffer has no durable identity and is
* never persisted here (in-memory only — see DiffViewController).
*
* INV-7 (native-surfaces migration, spec §6.4): the baseline is now either the
* git-HEAD blob (never persisted — see GitBaselineAdapter/DiffViewController)
* or a snapshot captured on coediting-entry / re-pinned by "Mark Changes as
* Reviewed". `"opened"`/`"machine-landing"` are legacy reasons from the
* shipped #17/#48 baseline; `normalizeReason` migrates them on load so old
* on-disk baselines keep round-tripping under the new vocabulary.
*/
import * as fs from "node:fs";
import * as path from "node:path";
export type BaselineReason = "opened" | "machine-landing" | "pinned";
export type BaselineReason = "entered" | "pinned" | "head";
type LegacyBaselineReason = BaselineReason | "opened" | "machine-landing";
export interface Baseline {
/** the document URI (the identity the key is derived from) — for round-trip/debug. */
@@ -22,6 +30,12 @@ export interface Baseline {
reason: BaselineReason;
}
/** Migrate legacy on-disk reasons to the current vocabulary (§6.4 INV-7). */
export function normalizeReason(reason: LegacyBaselineReason): BaselineReason {
if (reason === "opened" || reason === "machine-landing") return "entered";
return reason;
}
export class BaselineStore {
/** @param storageDir absolute VS Code GLOBAL-storage dir (context.globalStorageUri.fsPath). */
constructor(private readonly storageDir: string) {}
@@ -34,7 +48,8 @@ export class BaselineStore {
load(key: string): Baseline | null {
const p = this.baselinePath(key);
if (!fs.existsSync(p)) return null;
return JSON.parse(fs.readFileSync(p, "utf8")) as Baseline;
const raw = JSON.parse(fs.readFileSync(p, "utf8")) as Baseline;
return { ...raw, reason: normalizeReason(raw.reason) };
}
/** Newest epoch wins — overwrite in place, no history (state-not-history, the F3/F4 precedent). */
+57
View File
@@ -0,0 +1,57 @@
/**
* CoeditingRegistry — the INV-10 activation gate (spec §6.4). Holds the set of
* documents the writer has explicitly entered into coediting; every surface
* (SCM/QuickDiff, comments, preview annotations, edit commands, decorations)
* checks membership before attaching. Persisted in workspaceState so a reload
* restores the set (the durable sidecar carries the artifacts; this carries
* only the mode).
*/
import * as vscode from "vscode";
const STATE_KEY = "cowriting.coeditingSet";
export class CoeditingRegistry implements vscode.Disposable {
private readonly set: Set<string>;
private readonly emitter = new vscode.EventEmitter<{ uri: string; coediting: boolean }>();
readonly onDidChange = this.emitter.event;
/** Set by the SCM surface (Task 3) so syncContext can expose the baseline mode. */
baselineModeOf: ((uri: vscode.Uri) => "head" | "snapshot" | undefined) | undefined;
constructor(private readonly state: vscode.Memento) {
this.set = new Set(state.get<string[]>(STATE_KEY, []));
}
enter(uri: vscode.Uri): void {
const key = uri.toString();
if (this.set.has(key)) return;
this.set.add(key);
void this.state.update(STATE_KEY, [...this.set]);
this.emitter.fire({ uri: key, coediting: true });
}
exit(uri: vscode.Uri): void {
const key = uri.toString();
if (!this.set.delete(key)) return;
void this.state.update(STATE_KEY, [...this.set]);
this.emitter.fire({ uri: key, coediting: false });
}
isCoediting(uri: vscode.Uri): boolean {
return this.set.has(uri.toString());
}
list(): string[] {
return [...this.set];
}
syncContext(editor: vscode.TextEditor | undefined): void {
const coediting = !!editor && this.isCoediting(editor.document.uri);
void vscode.commands.executeCommand("setContext", "cowriting.isCoediting", coediting);
const mode = coediting && editor ? this.baselineModeOf?.(editor.document.uri) : undefined;
void vscode.commands.executeCommand("setContext", "cowriting.baselineMode", mode ?? "");
}
dispose(): void {
this.emitter.dispose();
}
}
+141 -141
View File
@@ -1,70 +1,84 @@
/**
* DiffViewController — F6 diff-view toggle (spec §6.2/§6.4). Owns the baseline
* lifecycle (initialize at first sight / advance at every machine landing / pin
* on demand), serves the baseline as a readonly `cowriting-baseline:` virtual
* document, and toggles a native vscode.diff (baseline left, the LIVE document
* right). A pure view: never mutates the document, sidecar, or attribution
* state (INV-19).
* DiffViewController — the baseline router (native-surfaces migration, spec
* §6.4, INV-7). Resolves each coedited document's review baseline to one of two
* modes: **head** (git-tracked — the baseline is the `HEAD` blob, re-read on
* every commit, never persisted) or **snapshot** (untracked/no-repo — captured
* on coediting-entry, re-pinned on demand by "Mark Changes as Reviewed").
* Baselines are established ONLY for documents the CoeditingRegistry has
* entered (INV-10) — opening a file no longer captures one (the retired
* `ensureBaseline`/#17 behavior). The shipped machine-landing advance (#48,
* INV-18) is retired too: a landed Claude edit stays a visible change-since-
* baseline until the file is committed (head mode) or reviewed (snapshot
* mode) — spec INV-7's note, D21. A pure data layer: never mutates the
* document, sidecar, or attribution state (INV-19).
*
* F6 works on ANY text document, not just workspace files — it needs no
* The F6 two-pane `vscode.diff` *view* (toggle UI + the `cowriting-baseline:`
* virtual document) was removed in #34 once F10 made the rendered preview the
* single review surface; only the baseline store survives here (spec §6.7).
*
* The baseline works on ANY text document, not just workspace files — it needs no
* `.threads/` sidecar, only a stable doc identity + a storage home. So it has
* its OWN diffable predicate (any `file:` or `untitled:` doc), decoupled from
* F2's workspace `isTracked`. Persistable docs (`file:`) keep their baseline in
* VS Code's per-extension GLOBAL storage keyed by a hash of the document URI;
* untitled buffers have no durable identity, so their baseline is in-memory
* only (lost on reload) — the same degrade the storage-unavailable path uses.
* F2's workspace `isTracked`. Persistable docs (`file:`) keep their SNAPSHOT
* baseline in VS Code's per-extension GLOBAL storage keyed by a hash of the
* document URI (INV-7: head-mode baselines are storage-free — never
* persisted); untitled buffers have no durable identity, so their baseline is
* in-memory only (lost on reload) — the same degrade the storage-unavailable
* path uses.
*/
import * as path from "node:path";
import { createHash } from "node:crypto";
import * as vscode from "vscode";
import { BaselineStore, type Baseline, type BaselineReason } from "./baselineStore";
export const BASELINE_SCHEME = "cowriting-baseline";
import type { GitBaselineAdapter } from "./gitBaseline";
import type { CoeditingRegistry } from "./coeditingRegistry";
export class DiffViewController implements vscode.Disposable {
private readonly disposables: vscode.Disposable[] = [];
/** Source of truth for the content provider, keyed by `document.uri.toString()`. */
/** Source of truth for the baseline, keyed by `document.uri.toString()`. */
private readonly baselines = new Map<string, Baseline>();
private readonly onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
/** F7 (additive): fires on every baseline capture (open / advance / pin) so
* the track-changes preview refreshes without polling. Mirrors the internal
* content-provider change signal but carries the real document URI. */
/** Which mode each established document resolved to (INV-7). */
private readonly modes = new Map<string, "head" | "snapshot">();
/** F7 (additive): fires on every baseline capture (establish / head-refresh /
* pin) so the track-changes preview refreshes without polling. Carries the
* real document URI. */
private readonly onDidChangeBaselineEmitter = new vscode.EventEmitter<{ uri: string }>();
readonly onDidChangeBaseline = this.onDidChangeBaselineEmitter.event;
private storageWarned = false;
constructor(private readonly store: BaselineStore | null) {
const provider: vscode.TextDocumentContentProvider = {
onDidChange: this.onDidChangeEmitter.event,
provideTextDocumentContent: (uri) => {
// The baseline URI carries the real document URI in its query (see baselineUri).
return this.baselines.get(uri.query)?.text ?? "";
},
};
constructor(
private readonly store: BaselineStore | null,
private readonly git: GitBaselineAdapter,
registry: CoeditingRegistry,
) {
this.disposables.push(
this.onDidChangeEmitter,
this.onDidChangeBaselineEmitter,
vscode.workspace.registerTextDocumentContentProvider(BASELINE_SCHEME, provider),
vscode.commands.registerCommand("cowriting.toggleDiffView", () =>
this.toggle(vscode.window.activeTextEditor),
),
vscode.commands.registerCommand("cowriting.pinDiffBaseline", () =>
vscode.commands.registerCommand("cowriting.markReviewed", () =>
this.pinCommand(vscode.window.activeTextEditor),
),
// F6 captures a baseline for any diffable doc the moment it is first seen,
// independent of the workspace gate (so "opened" is the open-time text).
vscode.workspace.onDidOpenTextDocument((d) => this.ensureBaseline(d)),
// INV-10: a baseline is established only once its document ENTERS
// coediting; exiting keeps the stored baseline as-is (PUC-7).
registry.onDidChange(({ uri, coediting }) => {
if (!coediting) return;
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri);
if (doc) void this.establish(doc);
}),
// A commit on a watched repo may advance a head-mode baseline (D13).
git.onDidChangeHead(() => void this.refreshHeadBaselines()),
);
for (const d of vscode.workspace.textDocuments) this.ensureBaseline(d);
// Restore already-coediting documents (e.g. after an extension host reload).
for (const key of registry.list()) {
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === key);
if (doc) void this.establish(doc);
}
}
// ---- diffability / identity --------------------------------------------------------
/** Any text document F6 can diff: a saved file OR an unsaved buffer. */
/** Any text document F6 tracks a baseline for: a saved file OR an unsaved buffer. */
private isDiffable(document: vscode.TextDocument): boolean {
return document.uri.scheme === "file" || document.uri.scheme === "untitled";
}
/** Only `file:` docs have a durable identity to persist a baseline against. */
/** Only `file:` docs have a durable identity to persist a SNAPSHOT baseline against. */
private isPersistable(document: vscode.TextDocument): boolean {
return document.uri.scheme === "file";
}
@@ -76,59 +90,114 @@ export class DiffViewController implements vscode.Disposable {
private storageKey(uriKey: string): string {
return createHash("sha256").update(uriKey).digest("hex");
}
/** The readonly virtual-doc URI; its query carries the real document URI. */
private baselineUri(document: vscode.TextDocument): vscode.Uri {
const name = path.basename(document.uri.path) || "untitled";
return vscode.Uri.from({ scheme: BASELINE_SCHEME, path: "/" + name, query: this.uriKey(document) });
}
// ---- baseline lifecycle (§6.4) -----------------------------------------------------
// ---- baseline lifecycle (§6.4, INV-7) -----------------------------------------------
/** First sight of a diffable doc: load the stored baseline, else capture `opened`. */
ensureBaseline(document: vscode.TextDocument): void {
/**
* Resolve this document's baseline mode and (re)establish its baseline.
* git-tracked (a HEAD blob resolves) → **head** mode, baseline = HEAD, never
* persisted. Otherwise → **snapshot** mode: load a previously-stored
* snapshot, else capture the buffer now (`"entered"`). Called once when a
* document enters coediting (registry.enter) and at startup for documents
* already coediting.
*/
async establish(document: vscode.TextDocument): Promise<void> {
if (!this.isDiffable(document)) return;
const key = this.uriKey(document);
if (this.baselines.has(key)) return;
const head = await this.git.headFor(document.uri);
if (head) {
this.modes.set(key, "head");
this.setBaseline(key, head.text, "head");
return;
}
this.modes.set(key, "snapshot");
if (this.store && this.isPersistable(document)) {
try {
const stored = this.store.load(this.storageKey(key));
if (stored) {
this.baselines.set(key, stored);
return;
}
} catch {
this.warnStorageOnce();
const stored = this.tryLoad(key); // existing load path, legacy reasons normalized
if (stored) {
this.baselines.set(key, stored);
this.fire(key);
return;
}
}
this.capture(document, "opened");
this.capture(document, "entered");
}
/** Machine landing (INV-18): re-capture so landed text never shows as a change. */
advance(document: vscode.TextDocument): void {
if (!this.isDiffable(document)) return;
this.capture(document, "machine-landing");
/** The mode a document's baseline resolved to, or undefined before `establish`. */
modeOf(uriString: string): "head" | "snapshot" | undefined {
return this.modes.get(uriString);
}
/** Human pin: baseline := now; the open diff visibly empties (left = right). */
/** Re-read HEAD for every head-mode document; a commit advances the baseline (D13). */
private async refreshHeadBaselines(): Promise<void> {
for (const [key, mode] of this.modes) {
if (mode !== "head") continue;
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === key);
if (!doc) continue;
const head = await this.git.headFor(doc.uri);
if (head && head.text !== this.baselines.get(key)?.text) {
this.setBaseline(key, head.text, "head"); // commit advanced the baseline (D13)
}
}
}
/** Human pin ("Mark Changes as Reviewed", D14): snapshot mode only — a
* git-tracked (head-mode) baseline advances only by commit. */
pin(document: vscode.TextDocument): void {
if (!this.isDiffable(document)) return;
const key = this.uriKey(document);
const mode = this.modes.get(key);
if (mode === undefined) {
// never established — the document is not being coedited (#71 item 1)
void vscode.window.showWarningMessage("Run ✦ Coedit this Document with Claude first.");
return;
}
if (mode !== "snapshot") {
void vscode.window.showWarningMessage(
"Cowriting: this document is git-tracked — commit to advance the baseline.",
);
return;
}
this.capture(document, "pinned");
}
/** Capture buffer text at this epoch, persist (if persistable), refresh any open diff. */
/** Capture buffer text at this epoch, persist (if persistable), notify F7/F10. */
private capture(document: vscode.TextDocument, reason: BaselineReason): void {
const key = this.uriKey(document);
const baseline: Baseline = { uri: key, text: document.getText(), capturedAt: new Date().toISOString(), reason };
this.baselines.set(key, baseline);
if (this.store && this.isPersistable(document)) {
try {
this.store.save(this.storageKey(key), baseline);
} catch {
this.warnStorageOnce();
}
this.trySave(key, baseline);
}
// An open diff re-requests the left side when its baseline URI changes.
this.onDidChangeEmitter.fire(this.baselineUri(document));
this.fire(key);
}
/** Set a baseline from resolved text directly (head mode — no document buffer read). */
private setBaseline(key: string, text: string, reason: BaselineReason): void {
const baseline: Baseline = { uri: key, text, capturedAt: new Date().toISOString(), reason };
this.baselines.set(key, baseline);
// head-mode baselines are storage-free (INV-7); persist only snapshots.
if (reason !== "head" && this.store) this.trySave(key, baseline);
this.fire(key);
}
private tryLoad(key: string): Baseline | null {
try {
return this.store!.load(this.storageKey(key));
} catch {
this.warnStorageOnce();
return null;
}
}
private trySave(key: string, baseline: Baseline): void {
try {
this.store!.save(this.storageKey(key), baseline);
} catch {
this.warnStorageOnce();
}
}
private fire(key: string): void {
this.onDidChangeBaselineEmitter.fire({ uri: key });
}
@@ -136,78 +205,13 @@ export class DiffViewController implements vscode.Disposable {
if (this.storageWarned) return;
this.storageWarned = true;
void vscode.window.showWarningMessage(
"Cowriting: diff-view storage is unavailable — baselines are kept in memory only and won't survive a reload.",
"Cowriting: baseline storage is unavailable — baselines are kept in memory only and won't survive a reload.",
);
}
// ---- toggle UX (§6.5 PUC-1) --------------------------------------------------------
/**
* If this doc's baseline diff is open → close it and reveal the normal editor;
* else open vscode.diff (baseline left, the live document right). Warns only
* when there is no active text editor (or a non-diffable scheme).
*/
private async toggle(editor: vscode.TextEditor | undefined): Promise<void> {
if (!editor || !this.isDiffable(editor.document)) {
void vscode.window.showWarningMessage(
"Cowriting: focus a text editor to toggle its diff view.",
);
return;
}
const document = editor.document;
const openTab = this.findDiffTab(document.uri);
if (openTab) {
await vscode.window.tabGroups.close(openTab);
await vscode.window.showTextDocument(document, { preview: false });
return;
}
this.ensureBaseline(document);
const baseline = this.baselines.get(this.uriKey(document))!;
const name = path.basename(document.uri.path) || "untitled";
const unsaved = this.isPersistable(document) ? "" : " (unsaved)";
const title = `${name} — my changes since ${this.epochLabel(baseline)}${unsaved}`;
await vscode.commands.executeCommand(
"vscode.diff",
this.baselineUri(document),
document.uri,
title,
{ preview: false },
);
}
/** The open baseline-diff tab for this document, if any. */
private findDiffTab(modified: vscode.Uri): vscode.Tab | undefined {
for (const group of vscode.window.tabGroups.all) {
for (const tab of group.tabs) {
const input = tab.input;
if (
input instanceof vscode.TabInputTextDiff &&
input.original.scheme === BASELINE_SCHEME &&
input.modified.toString() === modified.toString()
) {
return tab;
}
}
}
return undefined;
}
/** Human-readable epoch for the diff tab title (§5 / §6.5). */
private epochLabel(baseline: Baseline): string {
const time = new Date(baseline.capturedAt).toLocaleTimeString();
switch (baseline.reason) {
case "opened":
return `opened ${time}`;
case "machine-landing":
return `Claude landed ${time}`;
case "pinned":
return `pinned ${time}`;
}
}
private pinCommand(editor: vscode.TextEditor | undefined): void {
if (!editor || !this.isDiffable(editor.document)) {
void vscode.window.showWarningMessage("Cowriting: focus a text editor to pin its diff baseline.");
void vscode.window.showWarningMessage("Cowriting: focus a text editor to mark its changes as reviewed.");
return;
}
this.pin(editor.document);
@@ -219,10 +223,6 @@ export class DiffViewController implements vscode.Disposable {
const b = this.baselines.get(uriString);
return b ? { text: b.text, reason: b.reason, capturedAt: b.capturedAt } : undefined;
}
/** Is this doc's baseline diff currently open in any tab group? */
isDiffOpen(uriString: string): boolean {
return this.findDiffTab(vscode.Uri.parse(uriString)) !== undefined;
}
/** Absolute on-disk path of this doc's persisted baseline, or undefined (untitled/in-memory). */
baselineFilePath(uriString: string): string | undefined {
if (!this.store || vscode.Uri.parse(uriString).scheme !== "file") return undefined;
+102
View File
@@ -0,0 +1,102 @@
/**
* EditFlow — the host edit-turn flow (F11/F12), extracted from the review
* webview controller ahead of its sunset (native-surfaces migration, spec
* §6.10). Owns the low-level turn→proposal(s) cut (`runEditAndPropose`,
* INV-39/40): never mutates the document directly — every turn lands as one or
* more F4 proposals via `ProposalController.propose` (INV-10). Reachable, from
* Task 6 on, from the thread controller (`ThreadController.runMakeEdit`) —
* vscode-API-only, no webview state. The `cowriting.editDocument` command lives
* in extension.ts (Finding-1 fix) so it can hand off to
* `ThreadController.askClaude()`. Task 8 deleted this class's own
* `askClaude`/`askEditInstruction` (the review webview's toolbar ask) once
* their only caller — the webview — was sunset alongside them.
*/
import * as vscode from "vscode";
import type { ProposalController } from "./proposalController";
import { diffToBlockHunks } from "./trackChangesModel";
import { buildFingerprint } from "./anchorer";
import type { EditTurnResult, RunEditTurnOptions } from "./liveTurn";
/**
* F11: a host edit turn (selection/document text + instruction → rewrite).
* Injectable for tests. #60: accepts optional turn options (onProgress/signal);
* the arg is optional so existing test stubs that ignore it stay valid.
*/
type EditTurn = (instruction: string, text: string, opts?: RunEditTurnOptions) => Promise<EditTurnResult>;
/** F11: what an Ask-Claude gesture edits — a resolved selection range, or the whole document. */
export type EditTarget = { kind: "range"; start: number; end: number } | { kind: "document" };
export class EditFlow implements vscode.Disposable {
private readonly disposables: vscode.Disposable[] = [];
/**
* F11: the host edit turn (INV-8 — runs host-side, @cline/sdk loaded lazily and
* never bundled). Injectable so host E2E can stub it (no LLM in CI).
*/
private editTurn: EditTurn = async (instruction, text, opts) => {
const { runEditTurn } = await import("./liveTurn");
return runEditTurn(instruction, text, opts);
};
/** Monotonic per-session counter minting a stable turnId for each Ask-Claude gesture. */
private turnSeq = 0;
private nextTurnSeq(): number {
return ++this.turnSeq;
}
constructor(private readonly proposals: ProposalController) {}
/**
* F11/F12 (INV-35/39): run one host edit turn and record the result as F4
* proposal(s) — a SELECTION yields one single-range proposal over the resolved
* block-union; a DOCUMENT rewrite is `diffToBlockHunks`'d into one proposal per
* changed BLOCK (#47, INV-39 supersedes INV-37's per-word cut), each tagged
* `granularity:"block"` so accept reconciles attribution per word (INV-40).
* Never mutates the document (INV-10). Returns the created proposal ids.
*/
async runEditAndPropose(
document: vscode.TextDocument,
target: EditTarget,
instruction: string,
opts?: RunEditTurnOptions,
): Promise<string[]> {
const full = document.getText();
// One turnId per gesture — the document case's N hunk-proposals all share it,
// so a single rewrite groups as one agent turn (parity with editSelection).
const turnId = `turn-${this.nextTurnSeq()}`;
const provenance = (turn: EditTurnResult) =>
({ kind: "agent" as const, id: "claude", agent: { sdk: "@cline/sdk", model: turn.model, sessionId: turn.sessionId } });
if (target.kind === "range") {
const selected = full.slice(target.start, target.end);
const turn = await this.editTurn(instruction, selected, opts);
if (turn.replacement === "" || turn.replacement === selected) return [];
const fp = buildFingerprint(full, { start: target.start, end: target.end });
const id = await this.proposals.propose(document, fp, turn.replacement, provenance(turn), { turnId, instruction });
return id ? [id] : [];
}
const turn = await this.editTurn(instruction, full, opts);
const ids: string[] = [];
// #47 (INV-39, supersedes INV-37): a document rewrite is cut at BLOCK
// granularity — one proposal per changed block (the unit a human reviews) —
// not per word. Each is tagged `granularity:"block"` so accept reconciles
// attribution per word inside the block (INV-40).
for (const h of diffToBlockHunks(full, turn.replacement)) {
const fp = buildFingerprint(full, { start: h.start, end: h.end });
const id = await this.proposals.propose(document, fp, h.replacement, provenance(turn), {
turnId,
instruction,
granularity: "block",
});
if (id) ids.push(id);
}
return ids;
}
// ---- test seam (§6.4) ----
/** F11 test seam: stub the host edit turn so the document/selection paths run without an LLM. */
setEditTurnForTest(fn: EditTurn): void {
this.editTurn = fn;
}
dispose(): void {
for (const d of this.disposables) d.dispose();
}
}
+288
View File
@@ -0,0 +1,288 @@
/**
* EditorProposalController — F12/#64 editor surface (spec coauthoring-inline-editor-diff
* §3.5). Reverses INV-32 for pending proposals: on `onDidChangeProposals` it
* (1) optimistically applies any not-yet-applied proposal into the active editor's
* buffer (ProposalController.optimisticApply, INV-48), (2) decorates each applied
* proposal — insertion tint over the proposed text + a non-editable struck-red hint
* for deletions (INV-52, decorationPlan/INV-49), and (3) provides a CodeLens
* `Accept ▾ / Reject ▾` above each block whose ▾ opens a QuickPick (this / all).
* Owns no proposal STATE — it is a view over ProposalController (which stays the
* pure F4 owner). Phase 2 decorates ALL committed changes-since-baseline by author
* (human green / Claude blue + struck hints for deletions), plus pending proposals;
* a pinned baseline suppresses committed marks entirely (pin→clean, INV-48).
*/
import * as vscode from "vscode";
import type { ProposalController } from "./proposalController";
import type { DiffViewController } from "./diffViewController";
import type { AttributionController } from "./attributionController";
import { decorationPlan, wordEditHunks, authorAt } from "./trackChangesModel";
import { isAuthorable } from "./workspacePath";
import type { CoeditingRegistry } from "./coeditingRegistry";
export class EditorProposalController implements vscode.Disposable, vscode.CodeLensProvider {
private readonly disposables: vscode.Disposable[] = [];
private readonly insDeco: Record<"human" | "claude", vscode.TextEditorDecorationType> = {
human: vscode.window.createTextEditorDecorationType({
backgroundColor: "rgba(63,185,80,0.14)", borderColor: "#3fb950",
border: "0 0 2px 0", borderStyle: "solid",
}),
claude: vscode.window.createTextEditorDecorationType({
backgroundColor: "rgba(88,166,255,0.15)", borderColor: "#58a6ff",
border: "0 0 2px 0", borderStyle: "solid",
}),
};
private readonly delDeco: Record<"human" | "claude" | "none", vscode.TextEditorDecorationType> = {
human: vscode.window.createTextEditorDecorationType({ after: { color: "#f85149" } }),
claude: vscode.window.createTextEditorDecorationType({ after: { color: "#bc8cff" } }),
none: vscode.window.createTextEditorDecorationType({ after: { color: new vscode.ThemeColor("descriptionForeground") } }),
};
private readonly lensEmitter = new vscode.EventEmitter<void>();
readonly onDidChangeCodeLenses = this.lensEmitter.event;
/** Pending debounce timers — one per URI — coalesce rapid-fire propose events
* (e.g. runEditAndPropose's N sequential propose() calls) into a single
* optimistic-apply pass that runs after ALL proposals are created. Without this,
* each propose() fires onDidChangeProposals synchronously and the controller's
* optimisticApply runs concurrently with the still-in-progress propose loop,
* causing "file changed in the meantime" workspace-edit conflicts. */
private readonly pendingApply = new Map<string, ReturnType<typeof setTimeout>>();
/** Debounce timers for committed-change re-render (one per URI, 50 ms). */
private readonly pendingRender = new Map<string, ReturnType<typeof setTimeout>>();
constructor(
private readonly proposals: ProposalController,
private readonly diffView: DiffViewController,
private readonly attribution: AttributionController,
private readonly registry: CoeditingRegistry,
) {
this.disposables.push(
this.insDeco.human, this.insDeco.claude, this.delDeco.human, this.delDeco.claude, this.delDeco.none, this.lensEmitter,
vscode.languages.registerCodeLensProvider({ language: "markdown" }, this),
this.proposals.onDidChangeProposals(({ uri }) => this.scheduleApply(uri)),
vscode.window.onDidChangeActiveTextEditor((ed) => ed && this.renderEditor(ed)),
// Refresh committed decorations when the baseline changes (establish / pin / head-refresh).
this.diffView.onDidChangeBaseline(({ uri }) => {
const ed = vscode.window.visibleTextEditors.find((e) => e.document.uri.toString() === uri);
if (ed) this.renderEditor(ed);
}),
// Refresh committed decorations (debounced) when the active doc changes — attribution spans shift.
vscode.workspace.onDidChangeTextDocument((e) => {
const ed = vscode.window.activeTextEditor;
if (ed && e.document === ed.document) this.scheduleRender(ed);
}),
// INV-10: re-render (and refresh CodeLens) on every gate change — exit clears
// decorations/CodeLens via renderEditor's own gate check, re-entry redraws.
this.registry.onDidChange(({ uri }) => {
const ed = vscode.window.visibleTextEditors.find((e) => e.document.uri.toString() === uri);
if (ed) this.renderEditor(ed);
this.lensEmitter.fire();
}),
// the four QuickPick-backed menu commands
vscode.commands.registerCommand("cowriting.proposalAcceptMenu", (id?: string) => this.menu("accept", id)),
vscode.commands.registerCommand("cowriting.proposalRejectMenu", (id?: string) => this.menu("reject", id)),
);
}
/** Debounce-schedule an optimistic-apply pass for the given URI. Multiple rapid
* onDidChangeProposals events (from a single runEditAndPropose batch) collapse
* into one pass that runs after the batch completes. */
private scheduleApply(uri: string): void {
const prev = this.pendingApply.get(uri);
if (prev !== undefined) clearTimeout(prev);
this.pendingApply.set(
uri,
setTimeout(() => {
this.pendingApply.delete(uri);
void this.onProposalsChanged(uri);
}, 0),
);
}
/** Debounce a committed-change re-render for `editor` (50 ms — lets rapid typing settle). */
private scheduleRender(editor: vscode.TextEditor): void {
const uri = editor.document.uri.toString();
const prev = this.pendingRender.get(uri);
if (prev !== undefined) clearTimeout(prev);
this.pendingRender.set(
uri,
setTimeout(() => {
this.pendingRender.delete(uri);
this.renderEditor(editor);
}, 50),
);
}
/** Apply any not-yet-applied proposals on the doc, then re-decorate + refresh lenses. */
private async onProposalsChanged(uri: string): Promise<void> {
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri);
if (!doc || doc.languageId !== "markdown" || !isAuthorable(doc.uri.scheme)) return;
// INV-10: a doc that exited coediting between the schedule and this tick gets
// no optimistic-apply/decorations/CodeLens.
if (!this.registry.isCoediting(doc.uri)) return;
const key = this.proposals.keyFor(doc);
for (const v of this.proposals.listProposals(doc)) {
if (v.anchorStart !== null && !this.proposals.isApplied(key, v.id)) {
await this.proposals.optimisticApply(doc, v.id); // fires onDidChangeProposals again; guarded by isApplied
}
}
const ed = vscode.window.visibleTextEditors.find((e) => e.document.uri.toString() === uri);
if (ed) this.renderEditor(ed);
this.lensEmitter.fire();
}
/** Decorate the editor for every applied proposal on its document (INV-52). */
private renderEditor(editor: vscode.TextEditor): void {
const doc = editor.document;
const clearAll = () => {
for (const a of ["human", "claude"] as const) editor.setDecorations(this.insDeco[a], []);
for (const a of ["human", "claude", "none"] as const) editor.setDecorations(this.delDeco[a], []);
};
if (doc.languageId !== "markdown") return clearAll();
// INV-10: a non-entered doc gets no decorations at all.
if (!this.registry.isCoediting(doc.uri)) return clearAll();
const key = this.proposals.keyFor(doc);
const ins: Record<"human" | "claude", vscode.Range[]> = { human: [], claude: [] };
const del: Record<"human" | "claude" | "none", vscode.DecorationOptions[]> = { human: [], claude: [], none: [] };
// Pending proposals — always Claude (INV: proposals are agent-authored).
for (const v of this.proposals.listProposals(doc)) {
if (v.anchorStart === null || v.original === undefined || !this.proposals.isApplied(key, v.id)) continue;
const plan = decorationPlan(v.anchorStart, v.original, v.replacement);
for (const i of plan.insertions) ins.claude.push(new vscode.Range(doc.positionAt(i.start), doc.positionAt(i.end)));
for (const d of plan.deletions) {
del.claude.push({
range: new vscode.Range(doc.positionAt(d.at), doc.positionAt(d.at)),
renderOptions: { after: { contentText: ` ${d.text} `, textDecoration: "line-through" } },
});
}
}
// Committed changes-since-baseline are added by decorateCommitted (pushes into ins/del[author]).
this.decorateCommitted(editor, ins, del);
for (const a of ["human", "claude"] as const) editor.setDecorations(this.insDeco[a], ins[a]);
for (const a of ["human", "claude", "none"] as const) editor.setDecorations(this.delDeco[a], del[a]);
}
/**
* Decorates committed (non-proposal) changes-since-baseline by author (Task 7 / reverse INV-32).
* Diffs current buffer against the F6 baseline; attributes each changed run to its author;
* skips any hunk overlapping an applied pending proposal (the proposal loop owns those).
* pin→clean: returns without marking when baseline.reason === "pinned".
* Adjacency heuristic: a deletion in a hunk that has insertions inherits the hunk author;
* a standalone deletion (hunk with no insertions) renders neutral ("none") — matching the
* preview's cw-del-none treatment.
*/
private decorateCommitted(
editor: vscode.TextEditor,
ins: Record<"human" | "claude", vscode.Range[]>,
del: Record<"human" | "claude" | "none", vscode.DecorationOptions[]>,
): void {
const doc = editor.document;
const baseline = this.diffView.getBaseline(doc.uri.toString());
// No baseline yet, or baseline was pinned (pin→clean: no committed marks, INV-48).
if (!baseline || baseline.reason === "pinned") return;
const current = doc.getText();
// Nothing changed — short-circuit before the diff.
if (current === baseline.text) return;
const spans = this.attribution.spansFor(doc);
const key = this.proposals.keyFor(doc);
// Build the set of current-buffer ranges owned by applied pending proposals so we
// can skip hunks that fall inside them — the proposal loop already decorates those.
const appliedRanges: { start: number; end: number }[] = [];
for (const v of this.proposals.listProposals(doc)) {
if (v.anchorStart !== null && v.original !== undefined && this.proposals.isApplied(key, v.id)) {
appliedRanges.push({ start: v.anchorStart, end: v.anchorStart + v.replacement.length });
}
}
// wordEditHunks(current, baseline.text): start/end are in current coords; replacement is baseline text.
const hunks = wordEditHunks(current, baseline.text);
for (const h of hunks) {
// Skip hunks whose current-buffer range overlaps an applied pending proposal.
// The skip is whole-hunk and therefore conservative: a committed edit word-merged
// into a proposal's hunk is skipped entirely — rare at word granularity.
if (appliedRanges.some((r) => h.start < r.end && h.end > r.start)) continue;
const author = authorAt(h.start, spans) ?? "human";
// h.replacement = baseline text for this span; current.slice(h.start, h.end) = applied text.
const plan = decorationPlan(h.start, h.replacement, current.slice(h.start, h.end));
for (const i of plan.insertions) {
ins[author].push(new vscode.Range(doc.positionAt(i.start), doc.positionAt(i.end)));
}
// Adjacency heuristic: a deletion paired with an insertion in THIS hunk inherits the
// insertion author; a standalone deletion (no insertion in the hunk) is neutral.
const delAuthor = plan.insertions.length > 0 ? author : "none";
for (const d of plan.deletions) {
del[delAuthor].push({
range: new vscode.Range(doc.positionAt(d.at), doc.positionAt(d.at)),
renderOptions: { after: { contentText: ` ${d.text} `, textDecoration: "line-through" } },
});
}
}
}
/**
* CodeLensProvider (Task 8, PUC-2 wording): a `✓ Keep` / `✗ Reject` pair above
* each applied block, plus — when ≥2 proposals are pending on the doc — a
* top-of-file `✓ Keep all (N)` / `✗ Reject all` pair routed directly at the
* batch commands (no QuickPick detour for the common "review is done" case).
*/
provideCodeLenses(document: vscode.TextDocument): vscode.CodeLens[] {
if (document.languageId !== "markdown") return [];
if (!this.registry.isCoediting(document.uri)) return [];
const key = this.proposals.keyFor(document);
// #70: an interior tweak orphans the exact anchor (anchorStart null) but the
// proposal stays fully decidable — Keep by id, Reject via the tracked span —
// so the lens pair anchors at the tracked span instead of vanishing (which
// made the fixed reject path unreachable from the primary gesture surface).
const applied: { id: string; at: number }[] = [];
for (const v of this.proposals.listProposals(document)) {
if (!this.proposals.isApplied(key, v.id)) continue;
const at = v.anchorStart ?? this.proposals.trackedSpan(key, v.id)?.start;
if (at !== undefined) applied.push({ id: v.id, at });
}
const lenses: vscode.CodeLens[] = [];
if (applied.length >= 2) {
const top = new vscode.Range(0, 0, 0, 0);
lenses.push(
new vscode.CodeLens(top, { title: `✓ Keep all (${applied.length})`, command: "cowriting.acceptAllProposals" }),
new vscode.CodeLens(top, { title: "✗ Reject all", command: "cowriting.rejectAllProposals" }),
);
}
for (const v of applied) {
const pos = document.positionAt(v.at);
const line = new vscode.Range(pos.line, 0, pos.line, 0);
lenses.push(
new vscode.CodeLens(line, { title: "✓ Keep", command: "cowriting.proposalAcceptMenu", arguments: [v.id] }),
new vscode.CodeLens(line, { title: "✗ Reject", command: "cowriting.proposalRejectMenu", arguments: [v.id] }),
);
}
return lenses;
}
/** The dropdown: this-proposal vs all-proposals, then dispatch. */
private async menu(kind: "accept" | "reject", id?: string): Promise<void> {
const doc = vscode.window.activeTextEditor?.document;
if (!doc || !id) return;
const key = this.proposals.keyFor(doc);
const verb = kind === "accept" ? "Accept" : "Reject";
const pick = await vscode.window.showQuickPick(
[`${verb} this proposal`, `${verb} ALL proposals`],
{ placeHolder: `${verb} Claude's proposal` },
);
if (!pick) return;
const all = pick.includes("ALL");
if (kind === "accept") {
// The batch commands own the applied/skipped reporting (#70: a silent:true
// batch with a discarded tally re-creates the silent-skip failure mode).
if (all) await vscode.commands.executeCommand("cowriting.acceptAllProposals");
else await this.proposals.finalizeInPlace(key, id);
} else {
if (all) await vscode.commands.executeCommand("cowriting.rejectAllProposals");
else await this.proposals.revertInPlace(key, id);
}
}
dispose(): void {
for (const t of this.pendingApply.values()) clearTimeout(t);
this.pendingApply.clear();
for (const t of this.pendingRender.values()) clearTimeout(t);
this.pendingRender.clear();
for (const d of this.disposables) d.dispose();
}
}
+323 -110
View File
@@ -10,8 +10,15 @@ import { BaselineStore } from "./baselineStore";
import { GlobalSidecarStore } from "./globalSidecarStore";
import { SidecarRouter } from "./sidecarRouter";
import { DiffViewController } from "./diffViewController";
import { TrackChangesPreviewController } from "./trackChangesPreview";
import { isAuthorable, selectionRejection } from "./workspacePath";
import { GitBaselineAdapter } from "./gitBaseline";
import { EditFlow } from "./editFlow";
import { LiveProgressUi } from "./liveProgressUi";
import { EditorProposalController } from "./editorProposalController";
import { isAuthorable, routeEdit } from "./workspacePath";
import { CoeditingRegistry } from "./coeditingRegistry";
import { ScmSurfaceController } from "./scmSurface";
import type MarkdownIt from "markdown-it";
import { cowritingMarkdownItPlugin, type AnnotationInputs } from "./previewAnnotations";
const CHANNEL_NAME = "Cowriting (Cline SDK)";
@@ -21,14 +28,30 @@ export interface CowritingApi {
proposalController: ProposalController;
versionGuard: VersionGuard;
diffViewController: DiffViewController;
trackChangesPreviewController: TrackChangesPreviewController;
editFlow: EditFlow;
sidecarRouter: SidecarRouter;
liveProgressUi: LiveProgressUi;
editorProposalController: EditorProposalController;
coeditingRegistry: CoeditingRegistry;
scmSurfaceController: ScmSurfaceController;
/** Task 7 test seam: the REAL production `inputsFor`, so E2E exercises the
* actual registry/diffView/attribution/proposals/config wiring (not a
* hand-rolled reimplementation of it). */
previewAnnotationHost: { inputsFor(env: unknown): AnnotationInputs | undefined };
/** Task 7 (D3/D21): the markdown-it plugin factory VS Code's built-in preview
* loads via `markdown.markdownItPlugins` (package.json). */
extendMarkdownIt: (md: unknown) => unknown;
}
export function activate(context: vscode.ExtensionContext): CowritingApi | undefined {
// --- POC command (Feature #2), unchanged ---
const output = vscode.window.createOutputChannel(CHANNEL_NAME);
context.subscriptions.push(output);
// #60: shared live-progress UI (notification activity line + "Cowriting: Claude"
// OutputChannel) for both Ask-Claude entry points.
const liveProgressUi = new LiveProgressUi();
context.subscriptions.push(liveProgressUi);
context.subscriptions.push(
vscode.commands.registerCommand("cowriting.showClineSdkInfo", async () => {
try {
@@ -50,18 +73,66 @@ export function activate(context: vscode.ExtensionContext): CowritingApi | undef
}),
);
// --- F6: diff-view toggle (Features #17 + #19) — workspace-INDEPENDENT ---
// F6 works on ANY diffable doc (file or untitled), so it is constructed
// regardless of whether a folder is open, and its commands are always live
// (never stubbed). Baseline lives in VS Code's per-extension GLOBAL storage
// (always present), never the repo (INV-19). The machine-landing advance is
// wired in the with-root branch below (landings only occur on workspace files
// through the seam). The controller self-wires baseline capture on open.
// --- PUC-7 (native-surfaces migration): the INV-10 opt-in gate ---
// Every later native surface (SCM/QuickDiff, comments, preview annotations,
// edit commands, decorations) checks isCoediting before attaching. Persisted
// in workspaceState so a reload restores the set.
const coeditingRegistry = new CoeditingRegistry(context.workspaceState);
context.subscriptions.push(coeditingRegistry);
context.subscriptions.push(
vscode.commands.registerCommand("cowriting.coeditDocument", () => {
const ed = vscode.window.activeTextEditor;
if (!ed || ed.document.languageId !== "markdown" || !isAuthorable(ed.document.uri.scheme)) {
void vscode.window.showWarningMessage("Cowriting: open a Markdown document to coedit it.");
return;
}
coeditingRegistry.enter(ed.document.uri);
coeditingRegistry.syncContext(ed);
}),
vscode.commands.registerCommand("cowriting.stopCoediting", () => {
const ed = vscode.window.activeTextEditor;
if (!ed) return;
coeditingRegistry.exit(ed.document.uri);
coeditingRegistry.syncContext(ed);
}),
vscode.window.onDidChangeActiveTextEditor((ed) => coeditingRegistry.syncContext(ed)),
);
coeditingRegistry.syncContext(vscode.window.activeTextEditor);
// --- Baseline router (native-surfaces migration, spec §6.4, INV-7) — workspace-
// INDEPENDENT --- The two-pane vscode.diff VIEW was removed in #34 (F10's
// rendered preview is the single review surface); only the baseline data
// layer survives, consumed by F7/F10. It tracks a baseline for ANY diffable
// doc (file or untitled), so it is constructed regardless of whether a folder
// is open. A SNAPSHOT baseline lives in VS Code's per-extension GLOBAL
// storage (always present), never the repo (INV-19); a git-tracked doc's HEAD
// baseline is never persisted (INV-7). Baselines are established only for
// documents the CoeditingRegistry has entered — see the registry wiring
// inside DiffViewController's constructor.
const gitBaseline = new GitBaselineAdapter();
context.subscriptions.push(gitBaseline);
const baselineStorageDir = context.globalStorageUri?.fsPath;
const baselineStore = baselineStorageDir ? new BaselineStore(baselineStorageDir) : null;
const diffViewController = new DiffViewController(baselineStore);
const diffViewController = new DiffViewController(baselineStore, gitBaseline, coeditingRegistry);
context.subscriptions.push(diffViewController);
// Task 1 hook: expose the resolved baseline mode ("head"/"snapshot") to the
// registry's context-key sync, and re-sync `cowriting.baselineMode` whenever
// a baseline is (re)captured (entry, head-refresh, pin) — the "Mark Changes
// as Reviewed" menu entry (snapshot-only, Task 3) is gated on that key.
coeditingRegistry.baselineModeOf = (uri) => diffViewController.modeOf(uri.toString());
context.subscriptions.push(
diffViewController.onDidChangeBaseline(() => coeditingRegistry.syncContext(vscode.window.activeTextEditor)),
);
// --- Task 3: the native diff surface (spec §6.4/§5, INV-13) — QuickDiff
// gutter bars, the cowriting-baseline: content provider, "Review Changes",
// and the status-bar change count. Constructed after diffViewController
// (consumes getBaseline/modeOf/onDidChangeBaseline) and the registry
// (consumes isCoediting/onDidChange), gated on INV-10. ---
const scmSurfaceController = new ScmSurfaceController(coeditingRegistry, diffViewController);
context.subscriptions.push(scmSurfaceController);
// F8: the out-of-workspace/untitled authoring sidecar — same GLOBAL storage
// home as the F6 baseline, keyed by sha256(uri) (INV-19/24). Constructed
// workspace-independently; the router falls back to it for any non-in-folder
@@ -82,36 +153,91 @@ export function activate(context: vscode.ExtensionContext): CowritingApi | undef
// F5 (INV-16): one shared guard — newer-major sidecars are read-only, one
// warning per doc across the three co-owning controllers.
const versionGuard = new VersionGuard(sidecarRouter);
const threadController = new ThreadController(sidecarRouter, root, versionGuard);
context.subscriptions.push(threadController);
// --- F3: live attribution (Feature #6) ---
const attributionController = new AttributionController(sidecarRouter, root, versionGuard);
const attributionController = new AttributionController(sidecarRouter, root, versionGuard, coeditingRegistry);
context.subscriptions.push(attributionController);
// --- F4: propose/accept (Feature #12) — constructed before the preview so F10
// can route ✓/✗ through it ---
const proposalController = new ProposalController(sidecarRouter, attributionController, root, versionGuard);
// --- F4: propose/accept (Feature #12) ---
const proposalController = new ProposalController(
sidecarRouter,
attributionController,
root,
versionGuard,
coeditingRegistry,
);
context.subscriptions.push(proposalController);
// --- F7/F10: the review preview is the single interactive review surface ---
// Workspace-INDEPENDENT (works on any markdown doc, reuses the F6 baseline,
// INV-20). Constructed AFTER attribution (reads F3 spans) and proposals (routes
// F4 accept/reject from the webview ✓/✗).
const trackChangesPreviewController = new TrackChangesPreviewController(
diffViewController,
context.extensionUri,
attributionController,
proposalController,
);
context.subscriptions.push(trackChangesPreviewController);
// --- F11/F12 (native-surfaces migration, spec §6.10): the edit flow — the
// `cowriting.editDocument` command, the instruction-prompt/progress-wrapped
// "ask Claude" gesture (INV-10 gate), and the turn→proposal(s) cut
// (`runEditAndPropose`, INV-39/40). Constructed BEFORE the thread controller,
// which consumes it too (Task 6: the comment-loop's "make this edit" offer
// calls runEditAndPropose). ---
const editFlow = new EditFlow(proposalController);
context.subscriptions.push(editFlow);
// --- F6 machine-landing wiring — now for ANY authorable doc ---
// The seam's single machine-landing signal advances the F6 baseline (INV-18);
// the seam can now fire on out-of-folder files too, so wire it unconditionally.
context.subscriptions.push(
attributionController.onDidApplyAgentEdit((e) => diffViewController.advance(e.document)),
// --- F2/F10 (Task 6, D19/D10/D8): comments-first ask + the comment→reply→
// offer→proposal loop. Consumes editFlow (offer → pending proposals) and
// liveProgressUi (the shared "asking Claude…" notification + output channel). ---
const threadController = new ThreadController(sidecarRouter, root, versionGuard, coeditingRegistry, editFlow, liveProgressUi);
context.subscriptions.push(threadController);
// --- F12 (#64): the editor surface — optimistic-apply proposals into the buffer,
// decorate the diff, and provide Accept ▾/Reject ▾ CodeLens (INV-48/49/52/53). ---
const editorProposalController = new EditorProposalController(
proposalController,
diffViewController,
attributionController,
coeditingRegistry,
);
context.subscriptions.push(editorProposalController);
// #46 (INV-42): accept every pending proposal on the active doc in one gesture
// (also reachable from the top-of-file "Keep all (N)" CodeLens, Task 8 PUC-2).
// Reuses the batched F4 seam + reports applied-vs-skipped, routing directly to
// ProposalController (native-surfaces migration: no preview-controller
// indirection).
context.subscriptions.push(
vscode.commands.registerCommand("cowriting.acceptAllProposals", async () => {
const doc = vscode.window.activeTextEditor?.document;
if (!doc || doc.languageId !== "markdown") {
void vscode.window.showWarningMessage("Cowriting: open a Markdown document to accept its proposals.");
return;
}
const { applied, skipped } = await proposalController.acceptAllProposals(doc);
if (applied === 0 && skipped === 0) return;
const skipNote = skipped > 0 ? `, ${skipped} skipped (target text changed — undo or reject)` : "";
void vscode.window.showInformationMessage(
`Cowriting: accepted ${applied} proposal${applied === 1 ? "" : "s"}${skipNote}.`,
);
}),
);
// #64 (INV-53): reject every pending proposal on the active doc in one gesture
// (native-surfaces migration: routes directly to ProposalController).
context.subscriptions.push(
vscode.commands.registerCommand("cowriting.rejectAllProposals", async () => {
const doc = vscode.window.activeTextEditor?.document;
if (!doc || doc.languageId !== "markdown") {
void vscode.window.showWarningMessage("Cowriting: open a Markdown document to reject its proposals.");
return;
}
const { reverted, skipped } = await proposalController.rejectAll(doc);
if (reverted === 0 && skipped === 0) return;
const skipNote = skipped > 0 ? `, ${skipped} skipped (applied text not locatable — undo your edits or Keep)` : "";
void vscode.window.showInformationMessage(
`Cowriting: rejected ${reverted} proposal${reverted === 1 ? "" : "s"}${skipNote}.`,
);
}),
);
// NOTE (native-surfaces migration, INV-7/INV-18 retirement): the shipped
// machine-landing baseline advance (#48) — wiring
// attributionController.onDidApplyAgentEdit → diffViewController.advance —
// was removed here. A landed Claude edit now stays a visible change-since-
// baseline until the file is committed (head mode) or reviewed via
// "Mark Changes as Reviewed" (snapshot mode).
// One SHARED sidecar watcher for both controllers; self-writes are suppressed
// centrally in the repo store (only repo `.threads/` sidecars are watched —
@@ -174,93 +300,116 @@ export function activate(context: vscode.ExtensionContext): CowritingApi | undef
),
);
// F3 SLICE-5 → F4 SLICE-4: the live turn — selection + instruction → one
// claude-code SDK turn (liveTurn.ts, INV-8) → a PENDING PROPOSAL (F4,
// INV-10); the applyAgentEdit seam (INV-9) now fires only on accept.
// Task 6 (D19): the comments-first ask replaces the old inline turn
// plumbing here — the ask now IS a comment. threadController.askClaude()
// opens a focused comment box on the current selection (or, with no
// selection, a top-anchored whole-document thread); the comment→reply→
// offer→proposal loop (ThreadController.respondInThread/makeThreadEdit)
// takes it from there. Kept as its own command (rather than folded into
// cowriting.edit) for the host E2E harness and the internal seams.
context.subscriptions.push(
vscode.commands.registerCommand("cowriting.editSelection", async () => {
await threadController.askClaude();
}),
);
// Code-review fix (Finding 1, session native-surfaces-exec): `cowriting.editDocument`
// used to delegate to `EditFlow.askClaude(doc, {kind:"document"})`, which prompted via
// `askEditInstruction` — a rejecting stub after Task 6 sunset the input webview
// (spec §6.10; both `askClaude`/`askEditInstruction` were deleted from EditFlow in
// Task 8 once their only caller, the review webview, died too). Reached with no
// selection (the common case for `cowriting.edit`), that was a SILENT dead end: the
// reject fired before EditFlow.askClaude's own try/catch and was `void`'d, so no
// toast, no thread, nothing. This command now resolves
// its target document exactly as before (a tab's clicked URI, falling back to the
// active editor), focuses it, and hands off to the SAME comments-first ask as
// `cowriting.editSelection` — `ThreadController.askClaude()` top-anchors a
// whole-document ask when the (now-focused) editor's selection is empty (D19). Kept
// registered + hidden from the palette for the host E2E harness and #42's
// tab-targeting behavior; `EditFlow.runEditAndPropose` (untouched) stays the E2E seam
// for driving a turn without the native comment UI — see f12Reach.test.ts /
// f11Toolbar.test.ts / f12Accept.test.ts / f12Review.test.ts.
context.subscriptions.push(
vscode.commands.registerCommand("cowriting.editDocument", async (uri?: vscode.Uri) => {
const doc = uri
? vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri.toString()) ??
(await vscode.workspace.openTextDocument(uri))
: vscode.window.activeTextEditor?.document;
if (!doc || doc.languageId !== "markdown") {
void vscode.window.showWarningMessage("Cowriting: open a Markdown document to ask Claude to edit it.");
return;
}
if (vscode.window.activeTextEditor?.document.uri.toString() !== doc.uri.toString()) {
await vscode.window.showTextDocument(doc);
}
await threadController.askClaude();
}),
);
// The single user-facing "Ask Claude to Edit" gesture (one command, one
// keybinding, one menu entry). It routes to the selection flow (editSelection)
// or the whole-document flow (editDocument) by selection/context — see
// routeEdit. Both underlying commands now end at ThreadController.askClaude()
// (comments-first ask, D19); they stay registered for the host E2E harness and
// the internal seams, but are hidden from the palette.
context.subscriptions.push(
vscode.commands.registerCommand("cowriting.edit", async (uri?: vscode.Uri) => {
const editor = vscode.window.activeTextEditor;
// F8: authoring works on any file: or untitled: doc (the router decides
// where its artifact is stored). Each failure still names its real reason
// (no editor / no selection / a non-{file,untitled} read-only view) — not
// always "select some text" (#24's per-condition messaging).
const reason = selectionRejection({
hasEditor: !!editor,
const route = routeEdit({
hasUri: !!uri,
uriMatchesActiveEditor: !!uri && editor?.document.uri.toString() === uri.toString(),
hasActiveEditor: !!editor,
selectionEmpty: editor?.selection.isEmpty ?? true,
scheme: editor?.document.uri.scheme ?? "",
});
if (reason) {
void vscode.window.showWarningMessage(reason);
return;
}
if (!editor) return; // unreachable once reason is null, but narrows the type
const instruction = await vscode.window.showInputBox({
prompt: "What should Claude do with the selection?",
placeHolder: "e.g. tighten this paragraph",
});
if (!instruction) return;
if (editor.selection.isEmpty) {
void vscode.window.showWarningMessage("Cowriting: select some text to send to Claude first.");
return;
}
const document = editor.document;
const selection = editor.selection;
const selectedText = document.getText(selection);
// Capture the anchor BEFORE the turn (spec §6.5 PUC-1): mid-turn edits
// can't skew it — the proposal renders wherever the target re-resolves.
const fp = buildFingerprint(document.getText(), {
start: document.offsetAt(selection.start),
end: document.offsetAt(selection.end),
});
const turnId = `turn-${Date.now().toString(36)}`;
try {
await vscode.window.withProgress(
{ location: vscode.ProgressLocation.Notification, title: "Cowriting: asking Claude…" },
async () => {
const { runEditTurn } = await import("./liveTurn");
const turn = await runEditTurn(instruction, selectedText);
if (turn.replacement === "") {
void vscode.window.showWarningMessage(
"Cowriting: Claude returned an empty replacement — nothing was proposed.",
);
return;
}
if (turn.replacement === selectedText) {
void vscode.window.showInformationMessage(
"Cowriting: Claude proposed no change to the selection.",
);
return;
}
// F4 (INV-10): the turn ends in a PROPOSAL, not a buffer mutation.
// The seam now fires only on accept (ProposalController, INV-9).
const id = await proposalController.propose(
document,
fp,
turn.replacement,
{
kind: "agent",
id: "claude",
agent: { sdk: "@cline/sdk", model: turn.model, sessionId: turn.sessionId },
},
{ turnId, instruction },
);
if (id) {
void vscode.window.showInformationMessage(
"Cowriting: Claude proposed an edit — review the diff at the highlighted range (✓ accept / ✗ reject).",
);
}
},
);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
void vscode.window.showErrorMessage(`Cowriting: Claude edit failed — ${message}`);
if (route === "selection") {
await vscode.commands.executeCommand("cowriting.editSelection");
} else {
await vscode.commands.executeCommand("cowriting.editDocument", uri);
}
}),
);
// Task 8 (native-surfaces migration, spec §6.10): the minimal gateway replacing
// the sunset review-webview's #41 right-click entries. Resolves the clicked
// doc (or falls back to the active editor, mirroring editDocument/#41), enters
// coediting if not already (so a fresh doc gets a baseline to review against —
// no-op if already entered or not authorable), then hands off to VS Code's
// OWN "Open Preview to the Side" — the built-in preview IS the review surface
// now (Task 7's annotations render inside it).
context.subscriptions.push(
vscode.commands.registerCommand("cowriting.openReviewPreview", async (uri?: vscode.Uri) => {
const doc = uri
? vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri.toString()) ??
(await vscode.workspace.openTextDocument(uri))
: vscode.window.activeTextEditor?.document;
if (!doc || doc.languageId !== "markdown") {
void vscode.window.showWarningMessage("Cowriting: open a Markdown document to review it.");
return;
}
if (vscode.window.activeTextEditor?.document.uri.toString() !== doc.uri.toString()) {
await vscode.window.showTextDocument(doc);
}
if (!coeditingRegistry.isCoediting(doc.uri) && isAuthorable(doc.uri.scheme)) {
await vscode.commands.executeCommand("cowriting.coeditDocument");
}
await vscode.commands.executeCommand("markdown.showPreviewToSide", doc.uri);
}),
);
// Render threads + attributions for already-open editors, and on future opens.
// INV-10: gated on the registry too — belt-and-suspenders with the per-controller
// gates (renderAll/loadAll already early-return), but keeps the intent explicit
// at the call site and avoids the no-op calls for a non-entered doc.
const renderIfOpen = (doc: vscode.TextDocument) => {
if (isAuthorable(doc.uri.scheme)) {
if (isAuthorable(doc.uri.scheme) && coeditingRegistry.isCoediting(doc.uri)) {
// Finding 3 (final whole-branch review): DiffViewController only
// establishes a baseline on the registry's enter TRANSITION and, at
// startup, for documents already open. A doc that is a PERSISTED
// coediting member but wasn't open yet at either of those moments (e.g.
// lazily restored after a window reload) reaches this handler with no
// baseline/mode — establish it now so QuickDiff/Review-Changes/Mark-
// Reviewed never see a silent empty-baseline gap.
if (diffViewController.modeOf(doc.uri.toString()) === undefined) void diffViewController.establish(doc);
threadController.renderAll(doc);
attributionController.loadAll(doc);
proposalController.renderAll(doc);
@@ -269,14 +418,78 @@ export function activate(context: vscode.ExtensionContext): CowritingApi | undef
vscode.workspace.textDocuments.forEach(renderIfOpen);
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(renderIfOpen));
// --- Task 7 (D3/D21, PUC-3): annotations in the BUILT-IN Markdown preview ---
// `env.currentDocument` (VS Code's markdown-it render env) is an OBSERVED
// preview-engine behavior, not a documented contract, so `lastCoeditedUri`
// is the fallback that keeps single-doc correctness when it's absent — the
// last editor that WAS coediting when it lost focus (e.g. focus moved to the
// preview pane itself) stays the annotation target until a different
// coedited doc becomes active.
let lastCoeditedUri: string | undefined;
const trackLastCoedited = (ed: vscode.TextEditor | undefined) => {
if (ed && coeditingRegistry.isCoediting(ed.document.uri)) lastCoeditedUri = ed.document.uri.toString();
};
trackLastCoedited(vscode.window.activeTextEditor);
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(trackLastCoedited),
// The active editor doesn't itself change when `cowriting.coeditDocument`
// enters IT into coediting — re-check on every gate change too, so the
// very first render after entering already has an annotation target.
coeditingRegistry.onDidChange(() => trackLastCoedited(vscode.window.activeTextEditor)),
);
const previewAnnotationHost = {
inputsFor(env: unknown): AnnotationInputs | undefined {
const envUri = (env as { currentDocument?: { toString(): string } } | undefined)?.currentDocument?.toString();
// Finding 1 (final whole-branch review): the `lastCoeditedUri` fallback
// is for an ABSENT env only (env.currentDocument didn't resolve) — a
// present envUri is authoritative for the doc actually being previewed
// and must never fall back to a different (or since-exited) document's
// inputs. Either way, the winning key must currently be a coediting
// member (INV-10) — an exited doc's stale `lastCoeditedUri` fails this
// check too, closing the "preview stays annotated after stopCoediting"
// gap.
const key = envUri === undefined ? lastCoeditedUri : envUri;
if (!key || !coeditingRegistry.list().includes(key)) return undefined;
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === key);
if (!doc) return undefined;
const baseline = diffViewController.getBaseline(key);
return {
baselineText: baseline?.text,
baselineReason: baseline?.reason,
spans: attributionController.spansFor(doc),
proposals: proposalController.listProposals(doc),
enabled: vscode.workspace.getConfiguration("cowriting").get<boolean>("annotations", true),
};
},
};
// Toggle: flips `cowriting.annotations` + refreshes the built-in preview
// (VS Code re-invokes extendMarkdownIt's plugin on the next render — no
// extension-side re-render call exists beyond the standard refresh command).
context.subscriptions.push(
vscode.commands.registerCommand("cowriting.toggleAnnotations", async () => {
const config = vscode.workspace.getConfiguration("cowriting");
const current = config.get<boolean>("annotations", true);
await config.update("annotations", !current, vscode.ConfigurationTarget.Global);
await vscode.commands.executeCommand("markdown.preview.refresh");
}),
);
return {
threadController,
attributionController,
proposalController,
versionGuard,
diffViewController,
trackChangesPreviewController,
editFlow,
sidecarRouter,
liveProgressUi,
editorProposalController,
coeditingRegistry,
scmSurfaceController,
previewAnnotationHost,
extendMarkdownIt: (md: unknown) => cowritingMarkdownItPlugin(md as MarkdownIt, previewAnnotationHost),
};
}
+136
View File
@@ -0,0 +1,136 @@
/**
* GitBaselineAdapter resolves a document's git-HEAD baseline via the built-in
* Git extension API (spec §6.4, INV-7). Spike-verified: Repository.show('HEAD',
* path) + state.onDidChange work as assumed; the change event is coarse, so
* re-reads key off state.HEAD.commit; a nested repo may need openRepository().
*
* Hardening (Task 2 E2E, host-verified): for a repo that is NOT a workspace
* folder (the common case here F8/#19 precedent, coediting works on any
* file), the git extension's own filesystem watcher does not reliably notice
* an out-of-band commit (e.g. a `git commit` run from a terminal, or in the
* host E2E) `repo.state.onDidChange` can simply never fire. We additionally
* watch `.git/logs/HEAD` (the reflog VS Code itself also watches for
* in-workspace repos) with Node's own `fs.watch` and nudge `repo.status()`,
* which reliably re-syncs `state.HEAD` and lets the existing onDidChange path
* do the rest. Best-effort: any failure here just falls back to the passive
* listener.
*/
import * as fs from "node:fs";
import * as path from "node:path";
import * as vscode from "vscode";
interface GitRepository {
rootUri: vscode.Uri;
state: { HEAD?: { commit?: string }; onDidChange: vscode.Event<void> };
show(ref: string, path: string): Promise<string>;
status(): Promise<void>;
}
interface GitAPI {
repositories: GitRepository[];
getRepository(uri: vscode.Uri): GitRepository | null;
openRepository(root: vscode.Uri): Promise<GitRepository | null>;
onDidOpenRepository: vscode.Event<GitRepository>;
}
export interface HeadBaseline { text: string; commit: string }
export class GitBaselineAdapter implements vscode.Disposable {
private api: GitAPI | null | undefined; // undefined = not resolved yet; null = unavailable
private readonly hooked = new WeakSet<GitRepository>();
private readonly lastCommit = new Map<string, string>(); // repo root → HEAD commit
private readonly emitter = new vscode.EventEmitter<void>();
readonly onDidChangeHead = this.emitter.event;
private readonly disposables: vscode.Disposable[] = [this.emitter];
private async gitApi(): Promise<GitAPI | null> {
if (this.api !== undefined) return this.api;
try {
const ext = vscode.extensions.getExtension("vscode.git");
if (!ext) return (this.api = null);
const exports = ext.isActive ? ext.exports : await ext.activate();
this.api = exports.getAPI(1) as GitAPI;
this.disposables.push(this.api.onDidOpenRepository((r) => this.hook(r)));
for (const r of this.api.repositories) this.hook(r);
} catch {
this.api = null;
}
return this.api;
}
/** HEAD blob text + commit for a tracked file; null when untracked / no repo / no HEAD. */
async headFor(uri: vscode.Uri): Promise<HeadBaseline | null> {
if (uri.scheme !== "file") return null;
const git = await this.gitApi();
if (!git) return null;
let repo = git.getRepository(uri);
if (!repo) repo = await this.discoverNested(git, uri);
if (!repo) return null;
this.hook(repo);
try {
const text = await repo.show("HEAD", uri.fsPath);
return { text, commit: repo.state.HEAD?.commit ?? "" };
} catch {
return null; // untracked, ignored, or no commit yet → snapshot mode
}
}
/** Spike finding: a nested repo may not be auto-opened — walk up for .git, then openRepository. */
private async discoverNested(git: GitAPI, uri: vscode.Uri): Promise<GitRepository | null> {
let dir = path.dirname(uri.fsPath);
for (let i = 0; i < 32; i++) {
if (fs.existsSync(path.join(dir, ".git"))) {
await git.openRepository(vscode.Uri.file(dir));
return git.getRepository(uri);
}
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
return null;
}
/** Watch a repo; fire onDidChangeHead only when HEAD's commit actually moves. */
private hook(repo: GitRepository): void {
if (this.hooked.has(repo)) return;
this.hooked.add(repo);
this.lastCommit.set(repo.rootUri.toString(), repo.state.HEAD?.commit ?? "");
this.disposables.push(
repo.state.onDidChange(() => {
const key = repo.rootUri.toString();
const commit = repo.state.HEAD?.commit ?? "";
if (this.lastCommit.get(key) !== commit) {
this.lastCommit.set(key, commit);
this.emitter.fire();
}
}),
);
this.watchReflog(repo);
}
/** Best-effort nudge: watch `.git/logs/HEAD` directly and force `repo.status()`
* on any write, so an out-of-band commit is picked up even when the git
* extension's own watcher doesn't fire (see the class doc). */
private watchReflog(repo: GitRepository): void {
try {
const reflog = path.join(repo.rootUri.fsPath, ".git", "logs", "HEAD");
if (!fs.existsSync(reflog)) return;
let pending: ReturnType<typeof setTimeout> | undefined;
const watcher = fs.watch(reflog, () => {
if (pending) clearTimeout(pending);
pending = setTimeout(() => void repo.status().catch(() => {}), 150);
});
this.disposables.push({
dispose: () => {
watcher.close();
if (pending) clearTimeout(pending);
},
});
} catch {
// best-effort only — the passive repo.state.onDidChange listener still applies.
}
}
dispose(): void {
for (const d of this.disposables) d.dispose();
}
}
+65
View File
@@ -0,0 +1,65 @@
/**
* liveProgressUi.ts host-side relay of TurnProgress snapshots to VS Code UI
* (#60, spec coauthoring-live-progress.md §3.4). The ONLY surfaces are the
* existing withProgress notification (an activity line) and a dedicated
* OutputChannel streaming the full assistant text no new network/webview
* surface (INV-45). vscode-only; all pure logic lives in turnProgress.ts.
*/
import * as vscode from "vscode";
import { formatProgressLine, type TurnProgressSnapshot } from "./turnProgress";
const CHANNEL_NAME = "Cowriting: Claude";
export interface TurnUi {
/** Pass to runEditTurn's opts.onProgress. */
onProgress: (snapshot: TurnProgressSnapshot) => void;
/** Pass to runEditTurn's opts.signal — fired when the user cancels the notification. */
signal: AbortSignal;
}
export class LiveProgressUi {
readonly channel: vscode.OutputChannel;
constructor() {
this.channel = vscode.window.createOutputChannel(CHANNEL_NAME);
}
/**
* Begin one turn's UI. Writes the per-turn header to the channel and returns
* the onProgress relay + an AbortSignal linked to the notification's cancel
* token. The channel APPENDS (it doubles as a debug log of recent turns, spec
* §3.5); it auto-reveals (without stealing focus) on the first streamed text,
* gated by `cowriting.liveProgress.revealOutput`.
*/
begin(
instruction: string,
progress: vscode.Progress<{ message?: string }>,
token: vscode.CancellationToken,
): TurnUi {
const controller = new AbortController();
token.onCancellationRequested(() => controller.abort());
this.channel.appendLine(`── asking: ${instruction} ──`);
let revealed = false;
const reveal = (): void => {
if (revealed) return;
revealed = true;
const cfg = vscode.workspace.getConfiguration("cowriting");
if (cfg.get<boolean>("liveProgress.revealOutput", true)) this.channel.show(true);
};
const onProgress = (s: TurnProgressSnapshot): void => {
progress.report({ message: formatProgressLine(s) });
if (s.textDelta) {
this.channel.append(s.textDelta);
reveal();
}
};
return { onProgress, signal: controller.signal };
}
dispose(): void {
this.channel.dispose();
}
}
+80 -11
View File
@@ -9,6 +9,9 @@
* never bundled (esbuild keeps it external).
*/
import type { TurnProgressSnapshot } from "./turnProgress";
import { createTurnProgressState, reduceTurnProgress } from "./turnProgress";
export interface EditTurnResult {
replacement: string;
model: string;
@@ -16,6 +19,19 @@ export interface EditTurnResult {
sessionId: string;
}
/**
* Options for runEditTurn. Both new fields are purely additive observability /
* control over the existing turn (INV-44): `onProgress` streams reduced progress
* snapshots out; `signal` cancels the turn in. Neither touches the result path,
* and neither pulls `vscode` into this module (INV-43 `AbortSignal` is a web
* standard, the progress snapshot is a domain type).
*/
export interface RunEditTurnOptions {
modelId?: string;
onProgress?: (snapshot: TurnProgressSnapshot) => void;
signal?: AbortSignal;
}
const SYSTEM_PROMPT = [
"You are a precise text editor embedded in VS Code.",
"You will be given a piece of text and an instruction.",
@@ -40,7 +56,7 @@ export function extractReplacement(outputText: string, selectedText: string): st
export async function runEditTurn(
instruction: string,
selectedText: string,
opts?: { modelId?: string },
opts?: RunEditTurnOptions,
): Promise<EditTurnResult> {
const sdk = await import("@cline/sdk");
const modelId = opts?.modelId ?? "sonnet";
@@ -48,17 +64,70 @@ export async function runEditTurn(
providerId: "claude-code",
modelId,
systemPrompt: SYSTEM_PROMPT,
// The claude-code provider spawns the bundled `claude` CLI. On macOS, when
// that process is launched from inside VS Code it performs IDE auto-connect,
// which shells out to `osascript` (`tell application …`) to identify/activate
// the editor — raising the macOS "control other applications" (Apple Events)
// permission prompt (#59). We only consume the turn's text result and never
// use the IDE connection, so we disable it: `CLAUDE_CODE_AUTO_CONNECT_IDE=0`
// hard-returns out of the auto-connect path in the binary, and
// `CLAUDE_CODE_IDE_SKIP_AUTO_INSTALL=1` skips the extension-install deep link.
// Seeding with the full `process.env` keeps the default inheritance intact
// (login under HOME, proxy/CA vars) — we only add the two switches. The
// provider forwards `options.env` into the spawned child (verified end to end
// through @cline/sdk → ai-sdk-provider-claude-code → @anthropic-ai/claude-agent-sdk).
options: {
env: {
...process.env,
CLAUDE_CODE_AUTO_CONNECT_IDE: "0",
CLAUDE_CODE_IDE_SKIP_AUTO_INSTALL: "1",
},
},
});
const result = await agent.run(
`<instruction>\n${instruction}\n</instruction>\n<text>\n${selectedText}\n</text>`,
);
// The SDK's AgentRunResult.status union is "completed" | "aborted" | "failed"
// (@cline/shared agent.d.ts) — "completed" is the success status.
if (result.status !== "completed") {
throw new Error(
`claude-code turn ${result.status}: ${result.error?.message ?? "unknown error"} ` +
"(is Claude Code installed and signed in?)",
// Stream reduced progress snapshots out (INV-44 additive) and wire cancellation
// in via the AbortSignal (INV-47). agent.subscribe returns its unsubscribe fn.
let state = createTurnProgressState();
const unsubscribe = opts?.onProgress
? agent.subscribe((event) => {
const next = reduceTurnProgress(state, event);
state = next.state;
// Observability must never affect the result (INV-44): a throwing relay
// is swallowed, not allowed to propagate into the SDK and fail the turn.
if (next.snapshot) {
try {
opts.onProgress!(next.snapshot);
} catch {
/* progress is best-effort */
}
}
})
: undefined;
const onAbort = () => agent.abort();
opts?.signal?.addEventListener("abort", onAbort);
try {
// A signal already aborted before the turn starts can't be honored by
// agent.abort() (the SDK's AbortController isn't created until run()), so
// short-circuit to the same aborted outcome the call site reflects (INV-47).
if (opts?.signal?.aborted) {
throw new Error("claude-code turn aborted: cancelled before start");
}
const result = await agent.run(
`<instruction>\n${instruction}\n</instruction>\n<text>\n${selectedText}\n</text>`,
);
// The SDK's AgentRunResult.status union is "completed" | "aborted" | "failed"
// (@cline/shared agent.d.ts) — "completed" is the success status. An aborted
// turn (user cancel) falls into this throw; the call site reflects "cancelled".
if (result.status !== "completed") {
throw new Error(
`claude-code turn ${result.status}: ${result.error?.message ?? "unknown error"} ` +
"(is Claude Code installed and signed in?)",
);
}
return { replacement: extractReplacement(result.outputText, selectedText), model: modelId, sessionId: result.runId };
} finally {
unsubscribe?.();
opts?.signal?.removeEventListener("abort", onAbort);
}
return { replacement: extractReplacement(result.outputText, selectedText), model: modelId, sessionId: result.runId };
}
+17
View File
@@ -90,6 +90,21 @@ export interface Proposal {
turnId?: string;
/** what the human asked for (review context). */
instruction?: string;
/**
* F12/#47 (INV-39/40): the review-decision unit this proposal represents.
* `"block"` the anchor spans a whole document block and accept reconciles
* attribution per WORD inside it (INV-40); `"single"` (or absent, for
* back-compat with older sidecars) a single-range proposal accepted whole.
*/
granularity?: "block" | "single";
/**
* F12/#64 (INV-48): the pre-apply text, captured when a proposal is
* optimistically applied to the buffer. `replacement` is now in the buffer and
* `fp.text` re-anchors to it, so `original` is the only record of what to revert
* to (revert-in-place) and what to show struck in the `<del>` half. Absent on a
* proposal created but not yet optimistically applied (or older sidecars).
*/
original?: string;
}
export interface Artifact {
@@ -245,6 +260,8 @@ export function serializeArtifact(a: Artifact): string {
createdAt: p.createdAt,
...(p.turnId !== undefined ? { turnId: p.turnId } : {}),
...(p.instruction !== undefined ? { instruction: p.instruction } : {}),
...(p.original !== undefined ? { original: p.original } : {}),
...(p.granularity !== undefined ? { granularity: p.granularity } : {}),
},
p,
),
+300
View File
@@ -0,0 +1,300 @@
/**
* previewAnnotations Task 7 (D3/D21, PUC-3): a pure, vscode-free markdown-it
* plugin that annotates the BUILT-IN VS Code Markdown preview with the F10
* authorship/change-mark vocabulary (`.cw-ins-claude` / `.cw-ins-human` /
* `.cw-del`) the native-surfaces migration's replacement for the sunset
* webview's review body (spec §6.4/§6.10). Reuses the shipped sentinel
* discipline (`injectSentinels`/`sentinelsToSpans`, #33/#47-hardened) instead
* of inventing a new one: `annotateSource` computes PUA-sentinel-wrapped
* markdown SOURCE (never HTML) against the F6/F7 baseline + F3 spans + F4
* pending proposals; `cowritingMarkdownItPlugin` wires that transform into the
* shared `markdown-it` instance (the SAME instance VS Code's built-in preview
* uses via `markdown.markdownItPlugins`) as (a) a core rule swap `state.src`
* before `normalize` and (b) a full-render wrapper that turns surviving
* sentinel pairs into `<span class="cw-…">`.
*
* Implementation note (resolved drift from the migration plan's Task 7 sketch,
* which described (b) as "a text renderer rule"): `sentinelsToSpans` is a
* stateful, single-pass walk over one HTML string installing it as a
* PER-TOKEN `renderer.rules.text` hook would reset that state at every text
* token boundary and silently break the "survives emphasis/tag boundaries"
* invariant (#33 CASE3) for any span whose sentinel-open and sentinel-close
* land in DIFFERENT text tokens (e.g. either side of a `**bold**` run the
* exact scenario CASE3 exists to cover). Wrapping `md.renderer.render` instead
* runs the walk ONCE over the fully-assembled HTML, exactly like the F9/F10
* `colorByAuthor` precedent (`render(injected)` a whole-block HTML string,
* not a token fragment) this is what actually delivers the stated invariant,
* robust to callers that invoke `md.render()` directly or `md.parse()` +
* `md.renderer.render()` separately (VS Code's own scroll-sync `data-line`
* injection uses the latter).
*
* `host.inputsFor(env)` is the ONE thin, vscode-touching seam (owned by
* `extension.ts`): `env.currentDocument` is an OBSERVED VS Code preview-engine
* behavior, not a documented contract, so the host falls back to the last
* actively-coedited document when it is absent (see extension.ts's
* `lastCoeditedUri`).
*
* Deliberate choice: the mermaid-fence rendering (the `options.highlight`
* override installed below) applies to EVERY markdown preview, coedited or
* not it is not gated on coediting state. Gating it would make a diagram's
* rendering flip (mermaid <-> plain fence) the instant a doc enters or exits
* coediting, which is a worse surprise than always rendering mermaid diagrams
* as diagrams; only the change-tracking OVERLAY on top (`buildMermaidQueue`'s
* diff augmentation) is gated on having a baseline to diff against.
*/
import type MarkdownIt from "markdown-it";
import { diffMermaid } from "./mermaidDiff";
import {
diffBlocks,
injectSentinels,
landedTextOf,
MERMAID_LEGEND,
mermaidFenceBody,
sentinelsToSpans,
splitBlocksWithRanges,
wordEditHunks,
type AuthorSpan,
type ProposalView,
type TaggedSpan,
} from "./trackChangesModel";
export interface AnnotationInputs {
baselineText: string | undefined;
baselineReason: "entered" | "pinned" | "head" | undefined;
spans: AuthorSpan[];
proposals: ProposalView[];
enabled: boolean;
}
/**
* Inject the sentinel vocabulary into `text` block-by-block (INV-23: code/
* mermaid fences stay atomic never sentinel-injected, never word-refined) so
* the delimiter-run + block-marker safety `injectSentinels` carries applies
* PER BLOCK, not just at the very top of the whole document (its clamps are
* anchored to a block's own start, matching every other caller `colorByAuthor`
* is always invoked per block too).
*/
function injectAllSentinels(text: string, marks: TaggedSpan[]): string {
if (marks.length === 0) return text;
const blocks = splitBlocksWithRanges(text);
if (blocks.length === 0) return text;
let out = "";
let cursor = 0;
for (const b of blocks) {
out += text.slice(cursor, b.start); // preserve inter-block gaps (blank lines) verbatim
if (b.type === "prose") {
const blockMarks = marks.filter((m) => m.end > b.start && m.start < b.end);
out += blockMarks.length ? injectSentinels(b.raw, b.start, blockMarks) : b.raw;
} else {
out += b.raw; // INV-23: fences are never annotated
}
cursor = b.end;
}
out += text.slice(cursor); // trailing content after the last block
return out;
}
/**
* Pure: markdown source -> sentinel-annotated source (authorship + change +
* proposal marks vs the F6/F7 baseline). Skips everything when annotations are
* disabled, or (the #48 rule) once the baseline was just PINNED with zero
* LANDED diff pending F4 proposals are unaffected by that gate (matching the
* shipped preview's "pin→clean, proposals still show", #48/INV-33).
*/
export function annotateSource(src: string, inputs: AnnotationInputs): string {
if (!inputs.enabled) return src;
const proposals = inputs.proposals ?? [];
const baselineText = inputs.baselineText ?? src; // no baseline → no change-marks
const landedText = landedTextOf(src, proposals);
const pinnedZeroDiff = inputs.baselineReason === "pinned" && landedText === baselineText;
const insSpans: TaggedSpan[] = [];
const deletions: { at: number; text: string }[] = [];
const appliedRanges: { start: number; end: number }[] = [];
// (c) pending F4 proposals already sit applied in `src` (F12 optimistic
// apply, INV-48) — mark the applied range ins-<author> (`p.author` — a
// proposal defaults to "claude" per `ProposalView.author`, but a human can
// also be the proposer, e.g. a human-authored suggestion routed through the
// same F4 pending-proposal path) and resurface the pre-apply original as a
// deletion at the same point.
for (const p of proposals) {
if (p.anchorStart === null || p.anchorEnd === null) continue;
appliedRanges.push({ start: p.anchorStart, end: p.anchorEnd });
if (p.anchorEnd > p.anchorStart) insSpans.push({ start: p.anchorStart, end: p.anchorEnd, tag: p.author ?? "claude" });
const original = p.original ?? p.replaced;
if (original) deletions.push({ at: p.anchorStart, text: original });
}
// (a)/(b) committed changes-since-baseline. Each hunk (wordEditHunks(src,
// baseline) — start/end in SRC coords, `replacement` = the baseline-side text
// for that span) yields (a) insertion marks from the F3 `spans` CLIPPED to the
// hunk's changed range — a hunk may straddle more than one author (e.g. two
// adjacent words typed by different authors with nothing committed between
// them), so spans are clipped individually, never the whole hunk tagged with
// one author — and (b) the baseline's dropped text reinserted at the hunk
// start. A hunk owned by an applied pending proposal is already marked above.
if (!pinnedZeroDiff && landedText !== baselineText) {
for (const h of wordEditHunks(src, baselineText)) {
if (appliedRanges.some((r) => h.start < r.end && h.end > r.start)) continue;
for (const s of inputs.spans) {
const start = Math.max(s.start, h.start);
const end = Math.min(s.end, h.end);
if (end > start) insSpans.push({ start, end, tag: s.author });
}
if (h.replacement) deletions.push({ at: h.start, text: h.replacement });
}
}
if (insSpans.length === 0 && deletions.length === 0) return src;
// Splice deletion reinsertions into the source high→low (so earlier offsets
// stay valid), shifting every mark recorded so far and appending a "del" mark
// over the freshly-inserted text.
const marks: TaggedSpan[] = insSpans.map((s) => ({ ...s }));
let text = src;
for (const d of [...deletions].sort((a, b) => b.at - a.at)) {
text = text.slice(0, d.at) + d.text + text.slice(d.at);
for (const m of marks) {
if (m.start >= d.at) m.start += d.text.length;
if (m.end >= d.at) m.end += d.text.length;
}
marks.push({ start: d.at, end: d.at + d.text.length, tag: "del" });
}
return injectAllSentinels(text, marks);
}
/** One `buildMermaidQueue` entry: a changed mermaid fence's own body (for
* matching, `mermaidFenceBody` normalization no trailing newline) paired
* with its `diffMermaid`-augmented replacement source. `consumed` is reset by
* the `render` wrapper (see `cowritingMarkdownItPlugin`) so the SAME entries
* can back a re-render without a fresh parse. */
interface MermaidQueueEntry {
readonly curBody: string;
readonly source: string;
consumed: boolean;
}
/**
* Per-render queue of mermaid-fence overrides: one entry per `changed` atomic
* mermaid op (Task 7 §2.6 parity: reuses `diffBlocks`' baseline/current block
* pairing, the SAME pairing `renderReview`/`renderOp` used in the sunset
* webview, so a changed mermaid fence is re-emitted through `diffMermaid`
* exactly as it was there INV-23 fences stay atomic, INV-29..31 styling
* directives) that has a diffable before/current pair AND a successful
* `diffMermaid` augmentation. `[]` when there is no baseline to diff against,
* annotations are disabled, or the landed text equals the baseline (covers
* the #48 pinned-zero-diff case too, since `diffBlocks` on identical text
* yields no `changed` ops).
*
* Diffed against `landedText` (proposals reverted), matching the "committed
* change since baseline" boundary `annotateSource`'s own `wordEditHunks(src,
* baselineText)` gate already draws (a pending, not-yet-accepted proposal
* isn't a landed change) — a fence a pending proposal's anchor overlaps is
* left unaugmented, same as the rest of that gate.
*
* NOT positional: `src` (what markdown-it actually parses fences from) and
* `landedText` (what this function diffs) can disagree on mermaid-fence COUNT
* or ORDER e.g. a pending proposal optimistically applied in `src` (F12,
* INV-48) inserts a whole new mermaid fence that `landedText` (proposals
* reverted) doesn't have, or `annotateSource`'s committed-deletion reinsert
* splices an extra fence into the rendered source. `options.highlight`
* (`cowritingMarkdownItPlugin`) matches each queue entry to a fence by BODY,
* not by position a fence with no matching entry renders verbatim (the
* queue is a bag of `changed` fences to find, not a strict per-fence
* override list), so misalignment degrades to verbatim exactly as intended,
* never substitutes the wrong diagram's augmentation into the wrong fence.
*
* Pure, vscode-free (INV-6).
*/
function buildMermaidQueue(src: string, inputs: AnnotationInputs): MermaidQueueEntry[] {
if (!inputs.enabled || inputs.baselineText === undefined) return [];
const baselineText = inputs.baselineText;
const landedText = landedTextOf(src, inputs.proposals ?? []);
if (landedText === baselineText) return [];
const entries: MermaidQueueEntry[] = [];
for (const op of diffBlocks(baselineText, landedText)) {
if (op.kind !== "changed" || op.block.type !== "mermaid") continue; // unchanged/added/removed: no override needed
const curBody = mermaidFenceBody(op.block.raw);
const beforeBody = op.before.type === "mermaid" ? mermaidFenceBody(op.before.raw) : null;
if (curBody === null || beforeBody === null) continue;
const result = diffMermaid(beforeBody, curBody);
if (result.kind === "augmented") entries.push({ curBody, source: result.source, consumed: false }); // INV-30 fallback: verbatim
}
return entries;
}
/**
* markdown-it plugin factory: the host supplies `AnnotationInputs` per render
* (`host.inputsFor(env)`; `undefined` = no coediting context for this render
* source passed through untouched). Installs (a) a core rule BEFORE `normalize`
* that swaps `state.src` for `annotateSource(...)`, and (b) a full-render
* wrapper (see module docstring) that turns surviving sentinel pairs into
* `cw-…` spans. Also teaches ```mermaid fences to render as `<pre class="mermaid">`
* (Q4/Task 7 Step 2.6) via `options.highlight` the same extension point the
* built-in preview's own fence rule already calls for syntax highlighting so
* the bundled `media/preview-mermaid.js` previewScript has something to run
* mermaid over (the built-in preview does not render mermaid on its own). A
* CHANGED mermaid fence's source is re-emitted through `diffMermaid` first
* (`buildMermaidQueue`, Task 7 §2.6 parity) so mermaid renders the intra-
* diagram diff (INV-29..31), not just the current diagram. `options.highlight`
* has no `env`/`state` parameter of its own, so the per-render queue is
* threaded through a closure variable set by the core rule (which DOES see
* `state.env`) and consumed BY BODY MATCH, not fence position (see
* `buildMermaidQueue`), by `highlight`: each fence's `code` is matched against
* unconsumed queue entries in order, the first match is consumed (so a later
* fence with the same body still finds its own entry), and a fence with no
* match renders verbatim WITHOUT consuming anything (so a later fence can
* still match). markdown-it's `parse` (core rules) always completes before
* `renderer.render` invokes `highlight`, and a single md instance never
* renders concurrently, so the closure handoff itself is safe but VS Code
* may call `md.parse()` and `md.renderer.render()` separately and re-render
* cached tokens without a fresh parse (the same caller pattern the module
* docstring notes for scroll-sync), which would otherwise replay a queue whose
* entries were already marked consumed by the prior render; the `render`
* wrapper resets every entry's `consumed` flag so each render pass starts with
* the full queue available again.
*/
export function cowritingMarkdownItPlugin(
md: MarkdownIt,
host: { inputsFor(env: unknown): AnnotationInputs | undefined },
): MarkdownIt {
let mermaidQueue: MermaidQueueEntry[] = [];
md.core.ruler.before("normalize", "cowriting-annotate", (state) => {
const inputs = host.inputsFor(state.env);
mermaidQueue = inputs ? buildMermaidQueue(state.src, inputs) : [];
if (!inputs) return;
state.src = annotateSource(state.src, inputs);
});
const baseRender = md.renderer.render.bind(md.renderer);
md.renderer.render = (tokens, options, env) => {
for (const entry of mermaidQueue) entry.consumed = false; // re-render-without-reparse safety net
return sentinelsToSpans(baseRender(tokens, options, env), "ins");
};
const baseHighlight = md.options.highlight;
md.options.highlight = (code, lang, attrs) => {
if (lang?.trim().toLowerCase() === "mermaid") {
const normalized = code.replace(/\n+$/, ""); // token.content keeps a trailing LF; mermaidFenceBody doesn't
const entry = mermaidQueue.find((e) => !e.consumed && e.curBody === normalized);
if (entry) entry.consumed = true;
const legend = entry ? MERMAID_LEGEND : "";
return `<pre class="mermaid" style="all: unset;">${escapeMermaidSource(entry?.source ?? code)}</pre>${legend}`;
}
return baseHighlight?.(code, lang, attrs) ?? md.utils.escapeHtml(code);
};
return md;
}
/** Minimal HTML-escape for a mermaid fence body (no markdown parsing — verbatim diagram source). */
function escapeMermaidSource(source: string): string {
return source
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\n+$/, "");
}
+411 -26
View File
@@ -13,12 +13,13 @@
import * as vscode from "vscode";
import { SidecarRouter, docIdentity } from "./sidecarRouter";
import { emptyArtifact, type Artifact, type Fingerprint, type Proposal, type Provenance } from "./model";
import { resolve, shift, type OffsetRange } from "./anchorer";
import { addProposal, removeProposal } from "./proposalModel";
import { resolve, shift, shiftTracked, buildFingerprint, type OffsetRange } from "./anchorer";
import { addProposal, removeProposal, setProposalApplied } from "./proposalModel";
import type { AttributionController } from "./attributionController";
import type { VersionGuard } from "./versionGuard";
import { isAuthorable } from "./workspacePath";
import type { ProposalView } from "./trackChangesModel";
import { wordEditHunks, type ProposalView } from "./trackChangesModel";
import type { CoeditingRegistry } from "./coeditingRegistry";
/** Test-facing snapshot of what is currently rendered for a document. */
export interface RenderedProposal {
@@ -39,6 +40,18 @@ interface DocState {
live: Map<string, OffsetRange>;
/** proposal ids whose anchor did not resolve at last render (stale/orphaned). */
unresolved: Set<string>;
/** ids already optimistically applied to the buffer (so the trigger doesn't re-apply). */
applied: Set<string>;
/**
* #70 (INV-5): the F12 applied-range bookkeeping proposal id -> the applied
* span's CURRENT buffer range. Written at optimistic-apply, re-synced whenever
* the exact anchor resolves (renderAll), shifted through interior-safe edits,
* and DELETED (distrusted) when an edit straddles a span boundary a clamped
* range is a guess, and stale text is never applied by guess (INV-11). Lets
* Reject restore the original after the human types INSIDE the pending range
* (which orphans the exact-substring anchor).
*/
appliedSpans: Map<string, OffsetRange>;
}
export class ProposalController implements vscode.Disposable {
@@ -54,11 +67,43 @@ export class ProposalController implements vscode.Disposable {
private readonly attribution: AttributionController,
private readonly rootDir: string | undefined,
private readonly guard: VersionGuard,
private readonly registry: CoeditingRegistry,
) {
this.disposables.push(this.statusItem);
this.disposables.push(this.onDidChangeProposalsEmitter);
this.disposables.push(
vscode.workspace.onDidChangeTextDocument((e) => this.onDidChange(e)),
// #70: a tracked applied span is only meaningful while its buffer is live —
// a closed doc can change on disk with no change events, so a span kept
// across close/reopen could revert over unrelated text (INV-11: never by
// guess). Cleared here; a resolving anchor rebuilds it at the next renderAll.
vscode.workspace.onDidCloseTextDocument((doc) => {
this.docs.get(this.keyOf(doc))?.appliedSpans.clear();
}),
// PUC-7 restore-on-enter (mirrors ThreadController/AttributionController):
// `renderAll` is the only path that recomputes state.live/unresolved AND
// fires onDidChangeProposals — the event EditorProposalController listens
// to for optimistic-apply (decorations/CodeLens). Without this, a FIRST
// entry into coediting (registry.enter()) never fires it, so pre-existing
// pending proposals stay un-applied/undecorated until the next edit.
// `renderAll` already gates on isCoediting; exit intentionally does
// nothing here (it clears only the live/unresolved recompute, not the
// persisted artifact — EditorProposalController's own gate hides the
// decorations). Registered after AttributionController's own subscription
// (construction order in extension.ts) so decorateCommitted's spansFor
// read sees fresh attribution on the same enter transition.
this.registry.onDidChange(({ uri, coediting }) => {
if (!coediting) {
// Finding 5 (final whole-branch review): exit hides the status-bar
// item too — otherwise a stale "N stale proposals" warning for a
// doc that's no longer even coediting stays pinned in the bar until
// some unrelated render happens to fire for it.
if (vscode.window.activeTextEditor?.document.uri.toString() === uri) this.statusItem.hide();
return;
}
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri);
if (doc) this.renderAll(doc);
}),
);
}
@@ -89,8 +134,10 @@ export class ProposalController implements vscode.Disposable {
id: p.id,
anchorStart: resolved === "orphaned" ? null : resolved.start,
anchorEnd: resolved === "orphaned" ? null : resolved.end,
replaced: fp?.text ?? "",
replaced: p.original ?? fp?.text ?? "",
replacement: p.replacement,
original: p.original,
author: p.author?.kind === "agent" ? "claude" : "human",
};
});
}
@@ -104,6 +151,8 @@ export class ProposalController implements vscode.Disposable {
artifact: this.store.load(docPath) ?? emptyArtifact(docPath),
live: new Map(),
unresolved: new Set(),
applied: new Set(),
appliedSpans: new Map(),
};
this.docs.set(docPath, state);
}
@@ -122,7 +171,7 @@ export class ProposalController implements vscode.Disposable {
fp: Fingerprint,
replacement: string,
author: Provenance,
opts?: { turnId?: string; instruction?: string },
opts?: { turnId?: string; instruction?: string; granularity?: "block" | "single" },
): Promise<string | undefined> {
if (!this.isTracked(document)) return undefined;
if (this.guard.isReadOnly(this.keyOf(document))) return undefined;
@@ -137,10 +186,66 @@ export class ProposalController implements vscode.Disposable {
// ---- PUC-2/PUC-3: accept / reject (INV-11/INV-12) ----------------------------------
/** Accept by proposal id (test-facing twin of the thread-menu gesture). */
async acceptById(docPath: string, proposalId: string): Promise<boolean> {
/** Accept by id — F12: finalize the already-applied text in place (INV-51). */
async acceptById(docPath: string, proposalId: string, opts?: { silent?: boolean }): Promise<boolean> {
if (this.isApplied(docPath, proposalId)) {
// INV-11 (applied path): if an external write mangled the optimistically-applied
// text so the fingerprint no longer resolves, refuse finalize — same guard as the
// legacy accept path. Direct finalizeInPlace calls (CodeLens Accept gesture where
// the user may have edited inside the applied span) bypass this check intentionally.
const hit = this.byId(docPath, proposalId);
if (hit) {
const document = this.openDoc(hit.state);
if (document) {
const fp = hit.state.artifact.anchors[hit.proposal.anchorId]?.fingerprint;
const resolved = fp ? resolve(document.getText(), fp) : "orphaned";
if (resolved === "orphaned") {
if (!opts?.silent) {
void vscode.window.showWarningMessage(
"Cowriting: this proposal's target text changed or is missing — undo to restore it, or reject to discard (it is never applied by guess).",
);
}
return false;
}
}
}
return this.finalizeInPlace(docPath, proposalId);
}
// Fallback: a proposal that was never optimistically applied (e.g. orphaned at
// apply time) keeps the legacy seam-apply accept.
const hit = this.byId(docPath, proposalId);
return hit ? this.accept(hit.state, hit.proposal) : false;
return hit ? this.accept(hit.state, hit.proposal, opts) : false;
}
/**
* #46 (INV-42): accept EVERY pending proposal on a document in one gesture a
* batched application of the existing `acceptById` seam, not a new mechanism.
* Block proposals take the INV-40 word-precise path automatically. Applied in
* DESCENDING anchor order so an earlier accept never invalidates a later one's
* offsets; proposals whose anchor can't resolve are SKIPPED (never force-applied)
* and counted. Returns the applied-vs-skipped tally for the caller to report.
*/
async acceptAllProposals(document: vscode.TextDocument): Promise<{ applied: number; skipped: number }> {
if (!this.isTracked(document)) return { applied: 0, skipped: 0 };
const state = this.ensureState(document);
state.artifact = this.store.load(state.docPath) ?? emptyArtifact(state.docPath);
const text = document.getText();
const items = state.artifact.proposals.map((p) => {
const fp = state.artifact.anchors[p.anchorId]?.fingerprint;
const resolved = fp ? resolve(text, fp) : "orphaned";
return { id: p.id, start: resolved === "orphaned" ? null : resolved.start };
});
const resolvable = items
.filter((i): i is { id: string; start: number } => i.start !== null)
.sort((a, b) => b.start - a.start);
let applied = 0;
let skipped = items.length - resolvable.length; // orphans, skipped up front
for (const it of resolvable) {
// silent: one batch report stands in for N per-proposal warnings.
if (await this.acceptById(state.docPath, it.id, { silent: true })) applied++;
else skipped++;
}
return { applied, skipped };
}
/** Reject by proposal id (test-facing twin of the thread-menu gesture). */
rejectById(docPath: string, proposalId: string): boolean {
@@ -150,7 +255,13 @@ export class ProposalController implements vscode.Disposable {
return true;
}
private async accept(state: DocState, proposal: Proposal): Promise<boolean> {
/** Reject by id — F12: revert the applied text in place (INV-51). */
async rejectByIdInPlace(docPath: string, proposalId: string): Promise<boolean> {
if (this.isApplied(docPath, proposalId)) return this.revertInPlace(docPath, proposalId);
return this.rejectById(docPath, proposalId);
}
private async accept(state: DocState, proposal: Proposal, opts?: { silent?: boolean }): Promise<boolean> {
if (this.guard.isReadOnly(state.docPath)) return false;
const document = this.openDoc(state);
if (!document) return false;
@@ -158,22 +269,32 @@ export class ProposalController implements vscode.Disposable {
const fp = state.artifact.anchors[proposal.anchorId]?.fingerprint;
const resolved = fp ? resolve(document.getText(), fp) : "orphaned";
if (resolved === "orphaned") {
void vscode.window.showWarningMessage(
"Cowriting: this proposal's target text changed or is missing — undo to restore it, or reject to discard (it is never applied by guess).",
);
// #46: accept-all suppresses per-proposal warnings (one batch report instead).
if (!opts?.silent)
void vscode.window.showWarningMessage(
"Cowriting: this proposal's target text changed or is missing — undo to restore it, or reject to discard (it is never applied by guess).",
);
this.renderAll(document);
return false;
}
const range = new vscode.Range(document.positionAt(resolved.start), document.positionAt(resolved.end));
// No awaits between resolve and the seam call: document.version is current.
const ok = await this.attribution.applyAgentEdit(document, range, proposal.replacement, proposal.author, {
expectedVersion: document.version,
turnId: proposal.turnId,
});
// #47 (INV-40): a BLOCK proposal applies the whole block but attributes only
// the words Claude actually changed; a single proposal applies its whole range.
const ok =
proposal.granularity === "block"
? await this.acceptBlock(document, resolved, proposal)
: await this.attribution.applyAgentEdit(
document,
new vscode.Range(document.positionAt(resolved.start), document.positionAt(resolved.end)),
proposal.replacement,
proposal.author,
// No awaits between resolve and the seam call: document.version is current.
{ expectedVersion: document.version, turnId: proposal.turnId },
);
if (!ok) {
void vscode.window.showWarningMessage(
"Cowriting: the editor rejected the accept — the proposal is still pending.",
);
if (!opts?.silent)
void vscode.window.showWarningMessage(
"Cowriting: the editor rejected the accept — the proposal is still pending.",
);
return false;
}
this.store.update(state.docPath, (a) => removeProposal(a, proposal.id));
@@ -181,16 +302,262 @@ export class ProposalController implements vscode.Disposable {
return true;
}
/**
* #47 (INV-40): accept a BLOCK proposal apply Claude's whole block, but
* attribute ONLY the runs Claude actually changed. The block is the DECISION
* unit; the word is the ATTRIBUTION unit. An intra-block word sub-diff
* (`wordEditHunks`, the raw engine INV-37 used, repurposed un-anchored so the
* runs stay disjoint for a batch apply) yields the changed runs; each lands
* through the F4 seam (Claude-attributed), applied last-position-first so an
* earlier run's offsets stay valid under the later ones. Unchanged spans within
* the block are never touched, so their prior authorship stands. (Each run is
* one seam edit / one undo step see the spec's deferred note on undo grouping.)
*/
private async acceptBlock(
document: vscode.TextDocument,
resolved: OffsetRange,
proposal: Proposal,
): Promise<boolean> {
const blockText = document.getText(
new vscode.Range(document.positionAt(resolved.start), document.positionAt(resolved.end)),
);
const subHunks = wordEditHunks(blockText, proposal.replacement);
if (subHunks.length === 0) return true; // block already equals the proposal — nothing to attribute
for (const h of [...subHunks].sort((a, b) => b.start - a.start)) {
const range = new vscode.Range(
document.positionAt(resolved.start + h.start),
document.positionAt(resolved.start + h.end),
);
const ok = await this.attribution.applyAgentEdit(document, range, h.replacement, proposal.author, {
expectedVersion: document.version,
turnId: proposal.turnId,
});
if (!ok) return false;
}
return true;
}
/** True once this proposal's text is in the buffer (optimistic apply ran). */
isApplied(docPath: string, proposalId: string): boolean {
return this.docs.get(docPath)?.applied.has(proposalId) ?? false;
}
/** #70: the shift-tracked applied span (the revert-fallback bookkeeping)
* exposed so the editor surface can keep the decision gestures reachable
* for an applied proposal whose exact anchor an interior tweak orphaned. */
trackedSpan(docPath: string, proposalId: string): OffsetRange | undefined {
return this.docs.get(docPath)?.appliedSpans.get(proposalId);
}
/** The one proposal-clear sequence (record + in-memory bookkeeping + re-render)
* shared by finalize/revert/reject so no path leaves a stale entry behind. */
private clearProposal(state: DocState, proposalId: string, document?: vscode.TextDocument): void {
this.store.update(state.docPath, (a) => removeProposal(a, proposalId));
state.applied.delete(proposalId);
state.appliedSpans.delete(proposalId);
if (document) this.renderAll(document);
}
/**
* F12/#64 (INV-48): optimistically apply a pending proposal INTO the buffer so
* the editor shows the would-be-accepted result (editable). Reuses the F4
* word-precise seam (block per-word hunks, INV-40; single whole range) but
* with `landBaseline:false` (the change stays pending). Then re-anchors the
* proposal to the applied text and stores the original (`setProposalApplied`), so
* `resolve()` finds it in the mutated buffer and revert/decorate key off it.
* Idempotent: a no-op if already applied.
*/
async optimisticApply(document: vscode.TextDocument, proposalId: string): Promise<boolean> {
if (!this.isTracked(document) || this.guard.isReadOnly(this.keyOf(document))) return false;
const docPath = this.keyOf(document);
const state = this.ensureState(document);
if (state.applied.has(proposalId)) return true;
state.artifact = this.store.load(docPath) ?? emptyArtifact(docPath);
const proposal = state.artifact.proposals.find((p) => p.id === proposalId);
const fp = proposal ? state.artifact.anchors[proposal.anchorId]?.fingerprint : undefined;
if (!proposal || !fp) return false;
// Reload-safety (INV-51/54): a proposal that already carries `original` was
// optimistically applied in a PRIOR session — the buffer holds the applied text
// and `fp` points at it, but this (fresh) controller's in-memory `applied` set is
// empty. Re-applying would recapture `original` from the already-applied buffer
// (= the replacement) and CLOBBER the true revert target, breaking Reject. Mark it
// applied in memory and stop — `original` is captured exactly once, on first apply.
if (proposal.original !== undefined) {
state.applied.add(proposalId);
this.renderAll(document);
return true;
}
const resolved = resolve(document.getText(), fp);
if (resolved === "orphaned") return false;
const original = document.getText(
new vscode.Range(document.positionAt(resolved.start), document.positionAt(resolved.end)),
);
const ok =
proposal.granularity === "block"
? await this.applyBlockOptimistic(document, resolved, proposal)
: await this.attribution.applyAgentEdit(
document,
new vscode.Range(document.positionAt(resolved.start), document.positionAt(resolved.end)),
proposal.replacement,
proposal.author,
{ expectedVersion: document.version, turnId: proposal.turnId, landBaseline: false },
);
if (!ok) return false;
state.applied.add(proposalId);
// Re-anchor to the applied text now in the buffer (its start is unchanged; its
// end shifts by the net length delta of the replacement).
const appliedStart = resolved.start;
const appliedEnd = appliedStart + proposal.replacement.length;
state.appliedSpans.set(proposalId, { start: appliedStart, end: appliedEnd });
const appliedFp = buildFingerprint(document.getText(), { start: appliedStart, end: appliedEnd });
this.store.update(docPath, (a) => setProposalApplied(a, proposalId, appliedFp, original));
this.renderAll(document);
return true;
}
/** Block optimistic apply: the INV-40 per-word hunks, but landBaseline:false. */
private async applyBlockOptimistic(
document: vscode.TextDocument,
resolved: OffsetRange,
proposal: Proposal,
): Promise<boolean> {
const blockText = document.getText(
new vscode.Range(document.positionAt(resolved.start), document.positionAt(resolved.end)),
);
const subHunks = wordEditHunks(blockText, proposal.replacement);
if (subHunks.length === 0) return true;
for (const h of [...subHunks].sort((a, b) => b.start - a.start)) {
const range = new vscode.Range(
document.positionAt(resolved.start + h.start),
document.positionAt(resolved.start + h.end),
);
const ok = await this.attribution.applyAgentEdit(document, range, h.replacement, proposal.author, {
expectedVersion: document.version, turnId: proposal.turnId, landBaseline: false,
});
if (!ok) return false;
}
return true;
}
/**
* F12/#64 (INV-51): ACCEPT an optimistically-applied proposal the text is
* already in the buffer, so this only marks the attribution landing (via
* `attribution.signalLanded`) and clears the proposal. No re-application.
* Native-surfaces migration (INV-7/INV-18 retirement, spec §6.4): this no
* longer advances the baseline a landed proposal stays a visible change-
* since-baseline until commit (head mode) or "Mark Changes as Reviewed"
* (snapshot mode); nothing here subscribes to `signalLanded` for baseline
* purposes anymore (the `onDidApplyAgentEdit → advance` wire was removed).
*/
async finalizeInPlace(docPath: string, proposalId: string): Promise<boolean> {
if (this.guard.isReadOnly(docPath)) return false;
const hit = this.byId(docPath, proposalId);
if (!hit) return false;
const document = this.openDoc(hit.state);
if (!document) return false;
this.attribution.signalLanded(document);
this.clearProposal(hit.state, proposalId, document);
return true;
}
/**
* F12/#64 (INV-51): REJECT an optimistically-applied proposal replace its live
* applied span with the stored `original`, then clear it. Reverts the whole block
* regardless of any in-place edits the human made to the inserted text: when an
* interior tweak has orphaned the exact anchor (INV-11), the revert falls back to
* the shift-tracked applied span (#70, INV-5). When neither locates the span
* (e.g. a boundary-straddling external rewrite distrusted it), the reject FAILS
* loudly warning shown, proposal kept, `false` returned instead of silently
* leaving the proposed text in the buffer. Undo (or Keep) remains the way out.
*/
async revertInPlace(docPath: string, proposalId: string, opts?: { silent?: boolean }): Promise<boolean> {
if (this.guard.isReadOnly(docPath)) return false;
const hit = this.byId(docPath, proposalId);
if (!hit) return false;
const document = this.openDoc(hit.state);
if (!document) return false;
// Never optimistically applied → nothing of ours is in the buffer; clearing
// the record IS the reject (the legacy pending-only path).
if (hit.proposal.original === undefined) {
this.clearProposal(hit.state, proposalId, document);
return true;
}
const fp = hit.state.artifact.anchors[hit.proposal.anchorId]?.fingerprint;
const resolved = fp ? resolve(document.getText(), fp) : "orphaned";
const span = resolved !== "orphaned" ? resolved : hit.state.appliedSpans.get(proposalId);
if (!span) {
if (!opts?.silent) {
// The revert is refused, but the record must stay dismissable (a window
// reload empties both the tracked spans and the undo stack): offer a
// record-only discard that leaves the buffer exactly as it stands.
const DISCARD = "Discard proposal (leave text)";
void vscode.window
.showWarningMessage(
"Cowriting: this proposal's applied text can't be located (it changed or moved) — it is never reverted by guess. Undo your edits, Keep it, or discard the record as-is.",
DISCARD,
)
.then((choice) => {
if (choice === DISCARD) this.clearProposal(hit.state, proposalId, document);
});
}
return false;
}
const we = new vscode.WorkspaceEdit();
we.replace(
document.uri,
new vscode.Range(document.positionAt(span.start), document.positionAt(span.end)),
hit.proposal.original,
);
if (!(await vscode.workspace.applyEdit(we))) return false;
this.clearProposal(hit.state, proposalId, document);
return true;
}
/**
* F12/#64 (INV-53): reject EVERY pending proposal on a document revert each in
* DESCENDING span order (so an earlier revert never shifts a later one's
* offsets), symmetric with #46's accept-all. #70: a tweaked proposal whose exact
* anchor is orphaned orders (and reverts) by its shift-tracked applied span;
* one with no locatable span is SKIPPED counted, never guessed (INV-11)
* and the per-proposal warning is suppressed in favor of the batch report.
*/
async rejectAll(document: vscode.TextDocument): Promise<{ reverted: number; skipped: number }> {
if (!this.isTracked(document)) return { reverted: 0, skipped: 0 };
const docPath = this.keyOf(document);
const state = this.ensureState(document);
state.artifact = this.store.load(docPath) ?? emptyArtifact(docPath);
const text = document.getText();
const ordered = state.artifact.proposals
.map((p) => {
const fp = state.artifact.anchors[p.anchorId]?.fingerprint;
const r = fp ? resolve(text, fp) : "orphaned";
const start = r !== "orphaned" ? r.start : state.appliedSpans.get(p.id)?.start ?? -1;
return { id: p.id, start };
})
.sort((a, b) => b.start - a.start);
let reverted = 0;
let skipped = 0;
for (const it of ordered) {
if (await this.revertInPlace(docPath, it.id, { silent: true })) reverted++;
else skipped++;
}
return { reverted, skipped };
}
private reject(state: DocState, proposal: Proposal): void {
if (this.guard.isReadOnly(state.docPath)) return;
this.store.update(state.docPath, (a) => removeProposal(a, proposal.id));
const document = this.openDoc(state);
if (document) this.renderAll(document);
this.clearProposal(state, proposal.id, this.openDoc(state));
}
// ---- PUC-4: load / external change / resolve-or-flag --------------------------------
/** Load + (re)render every pending proposal at its resolved anchor (or flagged). */
/** Load + (re)render every pending proposal at its resolved anchor (or flagged).
* INV-10: the RENDER portion (live/unresolved recompute, the status bar, and
* the onDidChangeProposals fire that drives F12's optimistic-apply/CodeLens/
* decorations) is gated on coediting `propose()` itself stays ungated (it
* always persists), and so does the state LOAD below: `acceptById`/`rejectById`
* key off `this.docs` (populated here) regardless of coediting, since deciding
* an already-persisted proposal is not a "rendering" surface. */
renderAll(document: vscode.TextDocument): void {
if (!this.isTracked(document)) return;
const docPath = this.keyOf(document);
@@ -198,6 +565,7 @@ export class ProposalController implements vscode.Disposable {
state.artifact = this.store.load(docPath) ?? emptyArtifact(docPath);
state.live.clear();
state.unresolved.clear();
if (!this.registry.isCoediting(document.uri)) return;
const text = document.getText();
for (const proposal of state.artifact.proposals) {
const fp = state.artifact.anchors[proposal.anchorId]?.fingerprint;
@@ -208,6 +576,14 @@ export class ProposalController implements vscode.Disposable {
this.recordProposal(state, proposal, { start: off, end: off }, false);
} else {
this.recordProposal(state, proposal, resolved, true);
// #70: REBUILD the applied-range bookkeeping from an exact resolve when
// the map has no entry (a prior-session apply, or after a close/reopen
// clear). An entry already present has been shift-maintained continuously
// since apply and is ground truth — never clobbered by a resolve, which
// can land on an exact duplicate of the applied text elsewhere.
if (state.applied.has(proposal.id) && !state.appliedSpans.has(proposal.id)) {
state.appliedSpans.set(proposal.id, resolved);
}
}
}
this.renderStatus(state);
@@ -226,10 +602,19 @@ export class ProposalController implements vscode.Disposable {
private onDidChange(e: vscode.TextDocumentChangeEvent): void {
const state = this.docs.get(this.keyOf(e.document));
if (!state || state.live.size === 0) return;
if (!state || (state.live.size === 0 && state.appliedSpans.size === 0)) return;
for (const change of e.contentChanges) {
const edit = { start: change.rangeOffset, end: change.rangeOffset + change.rangeLength, newLength: change.text.length };
for (const [id, range] of state.live) state.live.set(id, shift(range, edit));
// #70: an edit fully inside a tracked applied span (or fully outside it)
// keeps the span meaningful; one straddling a boundary — e.g. a whole-buffer
// external replace — makes the shifted range a clamped guess, so the span
// is distrusted (deleted) rather than reverted-to by guess (INV-11).
for (const [id, range] of state.appliedSpans) {
const shifted = shiftTracked(range, edit);
if (shifted === "distrusted") state.appliedSpans.delete(id);
else state.appliedSpans.set(id, shifted);
}
}
}
+21 -1
View File
@@ -12,7 +12,7 @@ export function addProposal(
fp: Fingerprint,
replacement: string,
author: Provenance,
opts?: { turnId?: string; instruction?: string },
opts?: { turnId?: string; instruction?: string; granularity?: "block" | "single" },
): { proposalId: string; anchorId: string } {
const anchorId = newId("a");
const proposalId = newId("pr");
@@ -25,6 +25,7 @@ export function addProposal(
createdAt: new Date().toISOString(),
...(opts?.turnId !== undefined ? { turnId: opts.turnId } : {}),
...(opts?.instruction !== undefined ? { instruction: opts.instruction } : {}),
...(opts?.granularity !== undefined ? { granularity: opts.granularity } : {}),
});
return { proposalId, anchorId };
}
@@ -36,6 +37,25 @@ export function removeProposal(artifact: Artifact, proposalId: string): boolean
return artifact.proposals.length < before;
}
/**
* F12/#64 (INV-48): record a proposal as optimistically applied store the
* pre-apply `original` (for revert + the struck `<del>`) and re-point its anchor
* fingerprint to the now-in-buffer applied text so `resolve()` finds it. Idempotent
* shape: a second call simply overwrites with the same values.
*/
export function setProposalApplied(
artifact: Artifact,
proposalId: string,
appliedFp: Fingerprint,
original: string,
): boolean {
const p = artifact.proposals.find((x) => x.id === proposalId);
if (!p) return false;
p.original = original;
artifact.anchors[p.anchorId] = { fingerprint: appliedFp };
return true;
}
/** Markdown comment body: instruction header + fenced whole-range diff. */
export function proposalBody(targetText: string, p: Proposal): string {
const header = p.instruction ? `**Claude proposes** — _${p.instruction}_` : "**Claude proposes**";
+120
View File
@@ -0,0 +1,120 @@
/**
* ScmSurfaceController the native diff surface (spec §6.4/§5, INV-13).
* Owns: the "Cowriting" SourceControl + QuickDiffProvider (gutter bars), the
* cowriting-baseline: TextDocumentContentProvider, the "Review Changes" native
* diff command, and the "✦ Coediting · N changes" status-bar item. Gated on
* the CoeditingRegistry (INV-10): non-entered docs resolve no original.
*/
import * as path from "node:path";
import * as vscode from "vscode";
import type { CoeditingRegistry } from "./coeditingRegistry";
import type { DiffViewController } from "./diffViewController";
import { countLineHunks } from "./trackChangesModel";
export const BASELINE_SCHEME = "cowriting-baseline";
export function baselineUriFor(docUri: vscode.Uri): vscode.Uri {
const name = path.basename(docUri.path) || "untitled.md";
return vscode.Uri.from({ scheme: BASELINE_SCHEME, path: `/${name}`, query: encodeURIComponent(docUri.toString()) });
}
export class ScmSurfaceController implements vscode.Disposable {
private readonly disposables: vscode.Disposable[] = [];
private readonly statusItem: vscode.StatusBarItem;
private readonly counts = new Map<string, number>();
private readonly baselineEmitter = new vscode.EventEmitter<vscode.Uri>();
private readonly recountTimers = new Map<string, ReturnType<typeof setTimeout>>();
constructor(
private readonly registry: CoeditingRegistry,
private readonly diffView: DiffViewController,
) {
this.statusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
this.statusItem.command = "cowriting.reviewChanges";
this.statusItem.tooltip = "Cowriting — click to review changes (baseline ⟷ live)";
this.disposables.push(this.statusItem, this.baselineEmitter);
this.disposables.push(
vscode.workspace.registerTextDocumentContentProvider(BASELINE_SCHEME, {
onDidChange: this.baselineEmitter.event,
provideTextDocumentContent: (uri) =>
this.diffView.getBaseline(decodeURIComponent(uri.query))?.text ?? "",
}),
);
const sc = vscode.scm.createSourceControl("cowriting", "✦ Cowriting");
sc.quickDiffProvider = {
provideOriginalResource: (uri: vscode.Uri) =>
this.registry.isCoediting(uri) && this.diffView.getBaseline(uri.toString())
? baselineUriFor(uri)
: null,
};
this.disposables.push(sc);
this.disposables.push(
vscode.commands.registerCommand("cowriting.reviewChanges", () => this.openReview()),
this.diffView.onDidChangeBaseline(({ uri }) => {
this.baselineEmitter.fire(baselineUriFor(vscode.Uri.parse(uri)));
this.scheduleRecount(uri);
}),
this.registry.onDidChange(({ uri, coediting }) => {
if (coediting) this.scheduleRecount(uri);
this.refreshStatus();
}),
vscode.workspace.onDidChangeTextDocument((e) => {
if (this.registry.isCoediting(e.document.uri)) this.scheduleRecount(e.document.uri.toString());
}),
vscode.window.onDidChangeActiveTextEditor(() => this.refreshStatus()),
);
this.refreshStatus();
}
changeCount(uriString: string): number {
return this.counts.get(uriString) ?? 0;
}
private async openReview(): Promise<void> {
const ed = vscode.window.activeTextEditor;
if (!ed || !this.registry.isCoediting(ed.document.uri)) {
void vscode.window.showWarningMessage("Cowriting: not coediting this document.");
return;
}
this.baselineEmitter.fire(baselineUriFor(ed.document.uri));
await vscode.commands.executeCommand(
"vscode.diff",
baselineUriFor(ed.document.uri),
ed.document.uri,
`${path.basename(ed.document.uri.path)} — Coediting (baseline ⟷ live)`,
{ preview: true },
);
}
private scheduleRecount(uriString: string): void {
const prev = this.recountTimers.get(uriString);
if (prev !== undefined) clearTimeout(prev);
this.recountTimers.set(uriString, setTimeout(() => {
this.recountTimers.delete(uriString);
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uriString);
const baseline = this.diffView.getBaseline(uriString);
if (!doc || !baseline) return;
this.counts.set(uriString, countLineHunks(baseline.text, doc.getText()));
this.refreshStatus();
}, 150));
}
private refreshStatus(): void {
const ed = vscode.window.activeTextEditor;
if (!ed || !this.registry.isCoediting(ed.document.uri)) {
this.statusItem.hide();
return;
}
const n = this.counts.get(ed.document.uri.toString()) ?? 0;
this.statusItem.text = `$(sparkle) Coediting · ${n} change${n === 1 ? "" : "s"}`;
this.statusItem.show();
}
dispose(): void {
for (const t of this.recountTimers.values()) clearTimeout(t);
for (const d of this.disposables) d.dispose();
}
}
+293 -16
View File
@@ -15,6 +15,10 @@ import { gitUserEmail } from "./identity";
import { addThread, appendMessage, setStatus } from "./threadModel";
import type { VersionGuard } from "./versionGuard";
import { isAuthorable } from "./workspacePath";
import type { CoeditingRegistry } from "./coeditingRegistry";
import type { EditFlow, EditTarget } from "./editFlow";
import type { LiveProgressUi } from "./liveProgressUi";
import type { EditTurnResult, RunEditTurnOptions } from "./liveTurn";
/** Test-facing snapshot of what is currently rendered for a document. */
export interface RenderedThread {
@@ -22,8 +26,22 @@ export interface RenderedThread {
status: "open" | "resolved";
orphaned: boolean;
range: { startLine: number; endLine: number };
/** Finding 2 (final whole-branch review): the raw `contextValue` VS Code's
* `comments/commentThread/*` `when`-clauses key on exposed directly so
* E2E can assert the status token survives an offer transition without a
* DOM/UI query. */
contextValue: string;
}
/** D10/PUC-8: every human comment on a coedited doc runs this reply turn. */
const REPLY_PROMPT = (ask: string): string =>
"Reply conversationally (2-4 sentences) to this remark about the text, then, if the remark implies " +
"an edit, state the edit you would make. Remark: " +
ask;
/** F6.x/Task 6: the injectable low-level turn runner behind respondInThread. */
type TurnRunner = (instruction: string, context: string, opts?: RunEditTurnOptions) => Promise<EditTurnResult>;
interface DocState {
docPath: string;
uri: vscode.Uri;
@@ -34,25 +52,45 @@ interface DocState {
live: Map<string, OffsetRange>;
/** thread id -> orphaned flag for the current render. */
orphaned: Map<string, boolean>;
/** Finding 2 (final whole-branch review): thread id -> in-flight offer-flow
* token ("offer" once a machine reply lands, "offerdone" once spent),
* tracked SEPARATELY from the open/resolved/orphaned status so setting one
* never clobbers the other both are folded into one space-separated
* `contextValue` string by `applyContextValue` (package.json when-clauses
* match either token with an unanchored regex). Ephemeral: reset on every
* render (a fresh vsThread never carries a stale offer). */
offerToken: Map<string, "offer" | "offerdone">;
}
export class ThreadController implements vscode.Disposable {
private readonly controller: vscode.CommentController;
private readonly disposables: vscode.Disposable[] = [];
private readonly docs = new Map<string, DocState>(); // keyed by docPath
/** thread -> the pending machine "offer" (D8): the ask it answered, ready to become an edit. */
private readonly offers = new WeakMap<vscode.CommentThread, { ask: string; threadId: string }>();
/** Test seam (Task 6): stub the reply-loop turn so E2E never spawns a real @cline/sdk agent. */
private turnRunner: TurnRunner | undefined;
/** Kept as ONE object; re-assigned on every registry change so VS Code re-queries
* (spec §6.4 v0.2.1 reassignment is the API's only "ranges changed" signal). */
private readonly rangeProvider: vscode.CommentingRangeProvider = {
provideCommentingRanges: (document) => {
if (!isAuthorable(document.uri.scheme)) return [];
if (!this.registry.isCoediting(document.uri)) return [];
return [new vscode.Range(0, 0, Math.max(0, document.lineCount - 1), 0)];
},
};
constructor(
private readonly store: SidecarRouter,
private readonly rootDir: string | undefined,
private readonly guard: VersionGuard,
private readonly registry: CoeditingRegistry,
private readonly editFlow: EditFlow,
private readonly liveProgressUi: LiveProgressUi,
) {
this.controller = vscode.comments.createCommentController("cowriting.threads", "Coauthoring Threads");
this.controller.commentingRangeProvider = {
provideCommentingRanges: (document) => {
if (!isAuthorable(document.uri.scheme)) return [];
return [new vscode.Range(0, 0, Math.max(0, document.lineCount - 1), 0)];
},
};
this.controller.commentingRangeProvider = this.rangeProvider;
this.disposables.push(this.controller);
this.disposables.push(
@@ -64,8 +102,30 @@ export class ThreadController implements vscode.Disposable {
vscode.commands.registerCommand("cowriting.reopenThread", (t: vscode.CommentThread) =>
this.setThreadStatus(t, "open"),
),
// D19 (spec §6.4 v0.2.1): the comments-first "Ask Claude" gesture — a
// focused comment box, not a webview.
vscode.commands.registerCommand("cowriting.askClaude", () => this.askClaude()),
// D8: turn a machine "offer" thread into pending F4 proposal(s) (INV-5).
vscode.commands.registerCommand(
"cowriting.makeThreadEdit",
(arg: { thread: vscode.CommentThread } | vscode.CommentThread) => this.makeEditFromThreadArg(arg),
),
vscode.workspace.onDidChangeTextDocument((e) => this.onDidChange(e)),
vscode.workspace.onDidSaveTextDocument((d) => this.onDidSave(d)),
// INV-10 (PUC-7): force VS Code to re-query commenting ranges on every gate
// change, and hide/restore rendered threads — exit hides only (dispose the
// rendered vsThreads), re-enter restores (renderAll reloads from the
// sidecar, which is the durable source of truth regardless of gate state).
this.registry.onDidChange(({ uri, coediting }) => {
this.controller.commentingRangeProvider = this.rangeProvider;
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri);
if (!doc) return;
if (coediting) {
this.renderAll(doc);
} else {
this.disposeRendered(this.keyOf(doc));
}
}),
);
}
@@ -100,17 +160,44 @@ export class ThreadController implements vscode.Disposable {
vsThreads: new Map(),
live: new Map(),
orphaned: new Map(),
offerToken: new Map(),
};
this.docs.set(docPath, state);
}
return state;
}
// ---- D19: comments-first ask -----------------------------------------------------
/**
* D19 (spec §6.4 v0.2.1): "Ask Claude" opens a focused comment box on the
* active editor's selection, or, when there is no selection, a top-anchored
* whole-document thread. The ask itself IS a comment respondInThread (D10)
* takes it from there once the human submits it via cowriting.reply.
*/
async askClaude(): Promise<void> {
const ed = vscode.window.activeTextEditor;
if (!ed || !this.registry.isCoediting(ed.document.uri)) {
void vscode.window.showWarningMessage("Run ✦ Coedit this Document with Claude first.");
return;
}
if (ed.selection.isEmpty) {
// whole-document ask → top-anchored thread (D19)
ed.selection = new vscode.Selection(0, 0, 0, 0);
ed.revealRange(new vscode.Range(0, 0, 0, 0));
}
// Spike finding: workbench.action.addComment is the only path that opens the
// comment widget WITH its input focused (no thread.focus() API exists).
this.controller.commentingRangeProvider = this.rangeProvider; // defensive refresh
await vscode.commands.executeCommand("workbench.action.addComment");
}
// ---- PUC-1: create on selection -------------------------------------------------
async createThreadOnSelection(firstBody = "New thread"): Promise<string | undefined> {
const editor = vscode.window.activeTextEditor;
if (!editor || editor.selection.isEmpty || !isAuthorable(editor.document.uri.scheme)) return undefined;
if (!this.registry.isCoediting(editor.document.uri)) return undefined;
if (this.guard.isReadOnly(this.keyOf(editor.document))) return undefined;
const document = editor.document;
const state = this.ensureState(document);
@@ -119,22 +206,175 @@ export class ThreadController implements vscode.Disposable {
end: document.offsetAt(editor.selection.end),
};
const fp = buildFingerprint(document.getText(), offsets);
const { threadId } = addThread(state.artifact, fp, { author: this.currentAuthor(), body: firstBody });
const author = this.currentAuthor();
const { threadId } = addThread(state.artifact, fp, { author, body: firstBody });
this.persist(state);
this.renderThread(document, state, threadId, offsets, false);
const vsThread = this.renderThread(document, state, threadId, offsets, false);
// D10: the first message of a new thread is also "a comment on a coedited
// doc" — run the same respond loop as a reply. Finding 2 guard: only for a
// human-authored message (never re-trigger the loop off the machine's own
// words — see reply() below for the matching guard).
if (author.kind !== "agent") {
void this.respondInThread(vsThread, state, threadId, firstBody);
}
return threadId;
}
// ---- PUC-2: reply / resolve -----------------------------------------------------
reply(r: vscode.CommentReply): void {
const threadId = this.threadIdOf(r.thread);
const state = this.stateOfThread(r.thread);
if (!threadId || !state) return;
const vsThread = r.thread;
if (!isAuthorable(vsThread.uri.scheme) || !this.registry.isCoediting(vsThread.uri)) return;
const document = vscode.workspace.textDocuments.find((d) => d.uri.toString() === vsThread.uri.toString());
if (!document) return;
const state = this.ensureState(document);
if (this.guard.isReadOnly(state.docPath)) return;
appendMessage(state.artifact, threadId, { author: this.currentAuthor(), body: r.text });
const author = this.currentAuthor();
let threadId = this.threadIdOf(vsThread);
if (threadId) {
appendMessage(state.artifact, threadId, { author, body: r.text });
} else {
// D10/D19: a brand-new thread created via the native "+"/comment-widget
// gesture (not via createThreadOnSelection) — VS Code hands us a real,
// still-unregistered CommentThread; register it now.
const range = vsThread.range ?? new vscode.Range(0, 0, 0, 0);
const offsets: OffsetRange = {
start: document.offsetAt(range.start),
end: document.offsetAt(range.end),
};
const fp = buildFingerprint(document.getText(), offsets);
const created = addThread(state.artifact, fp, { author, body: r.text });
threadId = created.threadId;
state.vsThreads.set(threadId, vsThread);
state.live.set(threadId, offsets);
state.orphaned.set(threadId, false);
// Finding 2 (final whole-branch review): a thread born via the native
// "+" gesture skipped renderThread entirely, so it never got a status
// contextValue at all — Resolve/Reopen never appeared for it. Stamp one
// now, same as every other thread.
this.applyContextValue(state, threadId, vsThread);
}
this.persist(state);
this.refreshComments(r.thread, state, threadId);
this.refreshComments(vsThread, state, threadId);
// D10: every HUMAN comment on a coedited doc runs a turn. Finding 2 guard:
// `cowriting.reply` is the ingress for both a genuine human reply and (in
// principle) a re-dispatched machine message — never loop the machine's own
// reply back through respondInThread (it would double-turn on Claude's words
// and re-flip an already-"offer" thread).
if (author.kind !== "agent") {
void this.respondInThread(vsThread, state, threadId, r.text);
}
}
/**
* D10/PUC-8: run one reply turn for `ask`, then append the machine's reply
* (INV-8 onBehalfOf provenance) and flip the thread into an "offer" ready
* for makeThreadEdit to cut it into pending proposal(s) (D8, INV-5).
*/
private async respondInThread(
vsThread: vscode.CommentThread,
state: DocState,
threadId: string,
ask: string,
): Promise<void> {
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === vsThread.uri.toString());
if (!doc) return;
const contextText = this.threadContextText(doc, vsThread);
try {
await vscode.window.withProgress(
{ location: vscode.ProgressLocation.Notification, title: "Cowriting: asking Claude…", cancellable: true },
async (progress, token) => {
const run = this.turnRunner ?? (await import("./liveTurn")).runEditTurn;
const ui = this.liveProgressUi.begin(ask, progress, token);
let turn: EditTurnResult;
try {
turn = await run(REPLY_PROMPT(ask), contextText, { onProgress: ui.onProgress, signal: ui.signal });
} catch (err) {
if (token.isCancellationRequested) return;
throw err;
}
// machine reply, onBehalfOf provenance (INV-8)
appendMessage(state.artifact, threadId, {
author: {
kind: "agent",
id: "claude",
agent: { sdk: "@cline/sdk", model: turn.model, sessionId: turn.sessionId, onBehalfOf: this.currentAuthor() },
},
body: turn.replacement,
});
this.persist(state);
this.refreshComments(vsThread, state, threadId);
// Finding 2: fold "offer" in alongside the status token — see
// `applyContextValue` — instead of clobbering it (when-key:
// `commentThread =~ /\boffer\b/`).
state.offerToken.set(threadId, "offer");
this.applyContextValue(state, threadId, vsThread);
this.offers.set(vsThread, { ask, threadId });
},
);
} catch (err) {
void vscode.window.showErrorMessage(`Cowriting: Claude reply failed — ${err instanceof Error ? err.message : String(err)}`);
}
}
/** The passage a ranged thread discusses, or the whole document for a top-anchored (D19) thread. */
private threadContextText(doc: vscode.TextDocument, vsThread: vscode.CommentThread): string {
const range = vsThread.range;
if (range && !range.isEmpty) return doc.getText(range);
return doc.getText();
}
/** D19: a ranged thread targets its range; a top-anchored (whole-doc-ask) thread targets the document. */
private targetOf(doc: vscode.TextDocument, vsThread: vscode.CommentThread): EditTarget {
const range = vsThread.range;
if (!range || range.isEmpty) return { kind: "document" };
return { kind: "range", start: doc.offsetAt(range.start), end: doc.offsetAt(range.end) };
}
// ---- D8: offer -> pending proposal(s) (INV-5) -------------------------------------
private async makeEditFromThreadArg(arg: { thread: vscode.CommentThread } | vscode.CommentThread): Promise<void> {
const vsThread = "thread" in arg ? arg.thread : arg;
await this.runMakeEdit(vsThread);
}
private async runMakeEdit(vsThread: vscode.CommentThread): Promise<string[]> {
const offer = this.offers.get(vsThread);
if (!offer || !this.registry.isCoediting(vsThread.uri)) return [];
const doc = await vscode.workspace.openTextDocument(vsThread.uri);
const target = this.targetOf(doc, vsThread);
const ids = await this.editFlow.runEditAndPropose(doc, target, offer.ask);
// Finding 2 (final whole-branch review): fold "offerdone" in alongside the
// status token rather than clobbering it — see `applyContextValue`.
const state = this.stateOfThread(vsThread);
if (state) {
state.offerToken.set(offer.threadId, "offerdone");
this.applyContextValue(state, offer.threadId, vsThread);
}
// Finding 3 (seam hygiene): drop the offer once it's spent — otherwise the
// `makeThreadEdit(threadId, docPath)` test seam (or a stray second UI click,
// now dead per contextValue but still a valid direct call) could re-run the
// same edit a second time. A future offer on this thread is a fresh
// respondInThread() call, which re-populates the WeakMap entry.
this.offers.delete(vsThread);
if (ids.length) {
void vscode.window.showInformationMessage(
`Cowriting: ${ids.length} pending change${ids.length === 1 ? "" : "s"} landed in the buffer — ✓ Keep / ✗ Reject there.`,
);
}
return ids;
}
/** Test seam (Task 6): run the offer for a thread without the UI button. */
async makeThreadEdit(threadId: string, docPath: string): Promise<string[]> {
const vsThread = this.docs.get(docPath)?.vsThreads.get(threadId);
if (!vsThread) return [];
return this.runMakeEdit(vsThread);
}
/** Test seam (Task 6): stub the reply-loop turn so E2E never spawns a real @cline/sdk agent. */
setTurnRunnerForTest(fn: TurnRunner): void {
this.turnRunner = fn;
}
private setThreadStatus(vsThread: vscode.CommentThread, status: "open" | "resolved"): void {
@@ -145,13 +385,14 @@ export class ThreadController implements vscode.Disposable {
setStatus(state.artifact, threadId, status);
this.persist(state);
vsThread.state = status === "resolved" ? vscode.CommentThreadState.Resolved : vscode.CommentThreadState.Unresolved;
vsThread.contextValue = status;
this.applyContextValue(state, threadId, vsThread); // preserves any in-flight offer token
}
// ---- PUC-3/4: render, reload, re-anchor -----------------------------------------
/** Load + (re)render every thread for a document at its resolved anchor (or orphaned). */
renderAll(document: vscode.TextDocument): void {
if (!this.registry.isCoediting(document.uri)) return;
const docPath = this.keyOf(document);
const state = this.ensureState(document);
// fresh artifact from disk (reload / external change)
@@ -160,6 +401,7 @@ export class ThreadController implements vscode.Disposable {
state.vsThreads.clear();
state.live.clear();
state.orphaned.clear();
state.offerToken.clear();
const text = document.getText();
for (const thread of state.artifact.threads) {
const fp = state.artifact.anchors[thread.anchorId]?.fingerprint;
@@ -225,7 +467,7 @@ export class ThreadController implements vscode.Disposable {
threadId: string,
offsets: OffsetRange,
orphaned: boolean,
): void {
): vscode.CommentThread {
const thread = state.artifact.threads.find((t) => t.id === threadId)!;
const range = new vscode.Range(document.positionAt(offsets.start), document.positionAt(offsets.end));
const vsThread = this.controller.createCommentThread(
@@ -234,12 +476,34 @@ export class ThreadController implements vscode.Disposable {
thread.messages.map((m) => this.toComment(m.body, m.author.id)),
);
vsThread.label = orphaned ? "⚠ Orphaned thread (anchor not found)" : undefined;
vsThread.contextValue = orphaned ? "orphaned" : thread.status;
vsThread.state = thread.status === "resolved" ? vscode.CommentThreadState.Resolved : vscode.CommentThreadState.Unresolved;
vsThread.collapsibleState = vscode.CommentThreadCollapsibleState.Collapsed;
state.vsThreads.set(threadId, vsThread);
state.live.set(threadId, offsets);
state.orphaned.set(threadId, orphaned);
state.offerToken.delete(threadId); // a freshly-created vsThread never carries a stale offer
this.applyContextValue(state, threadId, vsThread);
return vsThread;
}
/**
* Finding 2 (final whole-branch review): `contextValue` is the ONE string
* VS Code's `comments/commentThread/*` `when`-clauses can key on, but two
* INDEPENDENT things need to show through it the open/resolved/orphaned
* status (Resolve/Reopen) and the offer-flow state (makeThreadEdit). Folding
* both into one space-separated string (`"open offer"`, `"resolved"`, )
* with package.json `when`s matched by an unanchored `=~ /\btoken\b/` regex
* lets either flip without erasing the other setting the offer flag alone
* used to stomp the status token entirely (every human comment now triggers
* a reply, so every thread on a coedited doc lost its Resolve/Reopen menu
* the moment the machine answered).
*/
private applyContextValue(state: DocState, threadId: string, vsThread: vscode.CommentThread): void {
const orphaned = !!state.orphaned.get(threadId);
const thread = state.artifact.threads.find((t) => t.id === threadId);
const statusToken = orphaned ? "orphaned" : (thread?.status ?? "open");
const offerToken = state.offerToken.get(threadId);
vsThread.contextValue = offerToken ? `${statusToken} ${offerToken}` : statusToken;
}
private refreshComments(vsThread: vscode.CommentThread, state: DocState, threadId: string): void {
@@ -269,6 +533,18 @@ export class ThreadController implements vscode.Disposable {
return undefined;
}
/** PUC-7 (INV-10): exit hides only dispose the rendered vsThreads for a doc
* without touching its sidecar-backed artifact (re-enter restores via renderAll). */
private disposeRendered(docPath: string): void {
const state = this.docs.get(docPath);
if (!state) return;
for (const vsThread of state.vsThreads.values()) vsThread.dispose();
state.vsThreads.clear();
state.live.clear();
state.orphaned.clear();
state.offerToken.clear();
}
// ---- test-facing surface --------------------------------------------------------
getRendered(docPath: string): RenderedThread[] {
@@ -283,6 +559,7 @@ export class ThreadController implements vscode.Disposable {
status: t.status,
orphaned: !!state.orphaned.get(id),
range: { startLine: r ? r.start.line : 0, endLine: r ? r.end.line : 0 },
contextValue: typeof vsThread.contextValue === "string" ? vsThread.contextValue : "",
});
}
return out;
+647 -59
View File
@@ -9,7 +9,7 @@
* DOM `markdown-it` and `diff` are libraries; mermaid runs later in the webview.
*/
import MarkdownIt from "markdown-it";
import { diffArrays, diffWords } from "diff";
import { diffArrays, diffWords, diffWordsWithSpace } from "diff";
import { diffMermaid } from "./mermaidDiff";
export type BlockType = "prose" | "code" | "mermaid";
@@ -193,6 +193,270 @@ export function diffBlocks(baselineText: string, currentText: string): BlockOp[]
return ops;
}
/**
* #64/Task 3 (INV-6): the number of line-grain hunks between `oldText` and
* `newText` a run of one or more contiguous non-equal lines counts as ONE
* hunk (the status-bar "N changes" count). Same LCS walk `diffBlocks` uses
* (jsdiff `diffArrays`), at line grain instead of block grain. Pure,
* vscode-free, deterministic.
*/
export function countLineHunks(oldText: string, newText: string): number {
const before = oldText.split(/\r?\n/);
const after = newText.split(/\r?\n/);
const changes = diffArrays(before, after);
let hunks = 0;
let inHunk = false;
for (const ch of changes) {
if (ch.added || ch.removed) {
if (!inHunk) hunks++;
inHunk = true;
} else {
inHunk = false;
}
}
return hunks;
}
/** A contiguous changed region of `currentText` and its replacement (F11). */
export interface EditHunk {
/** char offset of the hunk's first changed char in currentText. */
start: number;
/** char offset one past the hunk's last changed char (start==end → a pure insertion). */
end: number;
/** the text that replaces currentText.slice(start, end). */
replacement: string;
}
/**
* F11 (INV-37, §6.4): diff a whole-document rewrite into per-hunk replacement
* ranges each becomes one independent F4 single-range proposal (so a document
* edit surfaces as N independently /-able blue blocks, no new model). Pure,
* vscode-free, deterministic. Offsets index into `currentText`: removed +
* unchanged parts reconstruct it exactly, so advancing on those yields true
* source offsets. Adjacent added/removed runs coalesce into one hunk; an
* unchanged part flushes the open hunk.
*
* A PURE insertion (added text between two unchanged regions) would otherwise be
* a zero-width hunk (start==end) and F4's fingerprint of empty text can never
* resolve (anchorer.resolve orphans an empty needle), so the proposal could
* never be accepted. Every zero-width hunk is therefore anchored to an adjacent
* source token (`anchorInsertion`): its range absorbs that token and its
* replacement keeps it, so the net text is identical but `fp.text` is non-empty
* and resolvable. A zero-width hunk is always bordered by unchanged text (or a
* doc edge), so the absorbed token is genuinely present in `currentText` and is
* never part of another hunk.
*/
export function diffToHunks(currentText: string, rewrittenText: string): EditHunk[] {
return wordEditHunks(currentText, rewrittenText).map((h) =>
h.start === h.end ? anchorInsertion(h, currentText) : h,
);
}
/**
* The raw word-level diff underlying `diffToHunks`: disjoint, ordered EditHunks
* that exactly partition the change (a pure insertion stays ZERO-WIDTH at its
* offset not anchored). `diffToHunks` adds anchoring on top so each hunk's F4
* fingerprint resolves; callers that apply hunks directly through the seam
* (INV-40's intra-block accept) want THESE raw, non-overlapping hunks instead
* anchoring can grow an insertion to absorb a token an adjacent hunk also edits,
* which corrupts a batch apply. Pure, vscode-free, deterministic. Applying these
* rightleft reconstructs `rewrittenText` exactly.
*/
export function wordEditHunks(currentText: string, rewrittenText: string): EditHunk[] {
const hunks: EditHunk[] = [];
let offset = 0;
let open: EditHunk | null = null;
const flush = () => {
if (open) hunks.push(open);
open = null;
};
for (const part of diffWordsWithSpace(currentText, rewrittenText)) {
if (part.added) {
open ??= { start: offset, end: offset, replacement: "" };
open.replacement += part.value;
} else if (part.removed) {
open ??= { start: offset, end: offset, replacement: "" };
offset += part.value.length;
open.end = offset;
} else {
flush();
offset += part.value.length;
}
}
flush();
return hunks;
}
/** F12/#64 (INV-49/52): the editor render of one optimistically-applied proposal. */
export interface DecorationPlan {
/** buffer ranges of inserted (proposed) text → tinted (INV-52). */
insertions: { start: number; end: number }[];
/** struck original text shown as a non-editable hint at a buffer offset (INV-52). */
deletions: { at: number; text: string }[];
}
/**
* F12/#64 (INV-49): compute the editor decoration plan for a proposal whose applied
* text occupies `[anchorStart, anchorStart+replacement.length)` in the buffer. The
* SAME word diff the webview uses (`wordEditHunks`) drives it, so both surfaces show
* the identical diff. A changed run maps to (a) an insertion range over the run's
* applied text and (b) a deletion hint carrying the run's removed original text at
* the run start; a pure insertion has no deletion hint; a pure deletion has only a
* hint. Pure, vscode-free, deterministic.
*/
export function decorationPlan(anchorStart: number, original: string, replacement: string): DecorationPlan {
const insertions: { start: number; end: number }[] = [];
const deletions: { at: number; text: string }[] = [];
// Walk the word diff once, tracking the applied-side offset as we consume parts.
let appliedOffset = anchorStart;
for (const part of diffWordsWithSpace(original, replacement)) {
if (part.added) {
insertions.push({ start: appliedOffset, end: appliedOffset + part.value.length });
appliedOffset += part.value.length;
} else if (part.removed) {
deletions.push({ at: appliedOffset, text: part.value });
// removed text is NOT in the applied buffer → appliedOffset does not advance
} else {
appliedOffset += part.value.length;
}
}
return { insertions, deletions };
}
const isWs = (c: string): boolean => /\s/.test(c);
/**
* Grow a zero-width insertion hunk to absorb an adjacent source token so its F4
* fingerprint has real text to resolve. Prefer the FOLLOWING token (optional
* leading whitespace + a run of non-whitespace); at end-of-document, fall back to
* the PRECEDING token. The absorbed text is kept in the replacement, so the
* applied result is unchanged.
*/
function anchorInsertion(hunk: EditHunk, text: string): EditHunk {
const p = hunk.start;
if (p < text.length) {
let e = p;
while (e < text.length && isWs(text[e])) e++;
while (e < text.length && !isWs(text[e])) e++;
if (e === p) e = Math.min(text.length, p + 1);
return { start: p, end: e, replacement: hunk.replacement + text.slice(p, e) };
}
let s = p;
while (s > 0 && isWs(text[s - 1])) s--;
while (s > 0 && !isWs(text[s - 1])) s--;
if (s === p) s = Math.max(0, p - 1);
return { start: s, end: p, replacement: text.slice(s, p) + hunk.replacement };
}
type BlockAlignKind = "unchanged" | "changed" | "removed" | "added";
interface BlockAlignOp {
kind: BlockAlignKind;
ci?: number; // index into the current blocks
ni?: number; // index into the rewritten blocks
}
/**
* Align two block sequences by normalized key (jsdiff `diffArrays`), pairing an
* adjacent removed-then-added run element-wise into `changed` ops (the surplus
* staying `removed` / `added`) the same pairing `diffBlocks` uses, but yielding
* index ops so the caller can read each side's source range. Pure.
*/
function alignBlocks(cur: BlockWithRange[], next: BlockWithRange[]): BlockAlignOp[] {
const changes = diffArrays(
cur.map((b) => b.key),
next.map((b) => b.key),
);
const ops: BlockAlignOp[] = [];
let bi = 0; // current index
let ci = 0; // rewritten index
for (let n = 0; n < changes.length; n++) {
const ch = changes[n];
const count = ch.count ?? ch.value.length;
if (!ch.added && !ch.removed) {
for (let k = 0; k < count; k++) ops.push({ kind: "unchanged", ci: bi++, ni: ci++ });
continue;
}
if (ch.removed) {
const nx = changes[n + 1];
const addCount = nx?.added ? (nx.count ?? nx.value.length) : 0;
const paired = Math.min(count, addCount);
for (let k = 0; k < paired; k++) ops.push({ kind: "changed", ci: bi++, ni: ci++ });
for (let k = paired; k < count; k++) ops.push({ kind: "removed", ci: bi++ });
for (let k = paired; k < addCount; k++) ops.push({ kind: "added", ni: ci++ });
if (nx?.added) n++;
continue;
}
for (let k = 0; k < count; k++) ops.push({ kind: "added", ni: ci++ });
}
return ops;
}
/**
* #47 (INV-39, §6.4): diff a whole-document rewrite into ONE EditHunk per CHANGED
* BLOCK the unit a human reviews rather than per word (which INV-37/`diffToHunks`
* did and this supersedes for document edits). Built by aligning both sides into
* the existing block units (`splitBlocksWithRanges`) and keying with the same diff
* as `diffBlocks`:
* - a CHANGED block (1:1 aligned, even when adjacent to other changed blocks) a
* block-aligned hunk `[block.start, block.end)` the rewritten block's raw
* text. A code/mermaid fence is one such whole-block hunk (atomic, INV-23). So
* a copy-edit pass touching N consecutive paragraphs yields N proposals.
* - a run of block INSERTIONS / DELETIONS one gap-span hunk covering the source
* between the bounding aligned blocks (unchanged OR changed both are 1:1
* anchors) the matching rewritten span (separators included), so
* reconstruction stays exact. A zero-width gap-span (insert at a seamless
* boundary) is anchored to an adjacent token (INV-41) so its F4 fingerprint
* resolves and it accepts.
* - unchanged blocks (same key AND same raw) no hunk.
* Pure, vscode-free, deterministic; same `EditHunk` shape as `diffToHunks` (which
* is RETAINED as the intra-block sub-diff engine for word-precise accept
* attribution INV-40). Applying all hunks rightleft reconstructs `rewrittenText`
* exactly.
*/
export function diffToBlockHunks(currentText: string, rewrittenText: string): EditHunk[] {
const cur = splitBlocksWithRanges(currentText);
const next = splitBlocksWithRanges(rewrittenText);
const ops = alignBlocks(cur, next);
// Same key but different raw (whitespace/case) is still a real content change.
for (const op of ops) {
if (op.kind === "unchanged" && cur[op.ci!].raw !== next[op.ni!].raw) op.kind = "changed";
}
// `changed` and `unchanged` are 1:1 anchors (both sides' offsets are known);
// `added`/`removed` have no counterpart and must be spanned together.
const isAnchor = (op: BlockAlignOp) => op.kind === "unchanged" || op.kind === "changed";
const hunks: EditHunk[] = [];
let i = 0;
while (i < ops.length) {
const op = ops[i];
if (op.kind === "unchanged") {
i++;
continue;
}
if (op.kind === "changed") {
const c = cur[op.ci!];
hunks.push({ start: c.start, end: c.end, replacement: next[op.ni!].raw });
i++;
continue;
}
// A maximal run of added/removed blocks → one gap-span hunk over the source
// between the bounding anchors (or the document edges), replaced with the
// matching rewritten span, so the separators reconstruct exactly.
let j = i;
while (j < ops.length && !isAnchor(ops[j])) j++;
const prev = i > 0 ? ops[i - 1] : null; // an anchor by construction
const after = j < ops.length ? ops[j] : null; // an anchor (or null at EOF)
const curStart = prev ? cur[prev.ci!].end : 0;
const curEnd = after ? cur[after.ci!].start : currentText.length;
const newStart = prev ? next[prev.ni!].end : 0;
const newEnd = after ? next[after.ni!].start : rewrittenText.length;
const hunk: EditHunk = { start: curStart, end: curEnd, replacement: rewrittenText.slice(newStart, newEnd) };
hunks.push(hunk.start === hunk.end ? anchorInsertion(hunk, currentText) : hunk);
i = j;
}
hunks.sort((a, b) => a.start - b.start);
return hunks;
}
const md = new MarkdownIt({ html: true, linkify: false, breaks: false });
// mermaid fences → <pre class="mermaid">SRC</pre> for client-side rendering; all
// other fences fall through to markdown-it's default (escaped <pre><code>).
@@ -205,8 +469,13 @@ md.renderer.rules.fence = (tokens, idx, options, env, self) => {
return defaultFence(tokens, idx, options, env, self);
};
/** Inner source of a ```mermaid fence (drops the fence lines), or null. */
function mermaidFenceBody(raw: string): string | null {
/**
* Inner source of a ```mermaid fence (drops the fence lines), or null. Exported
* so `previewAnnotations.ts` can pair a `diffBlocks` mermaid `changed` op's
* before/current fence bodies for `diffMermaid` without reinventing this
* extraction (Task 7 §2.6 parity).
*/
export function mermaidFenceBody(raw: string): string | null {
const lines = raw.split(/\r?\n/);
const open = lines[0]?.match(/^(\s*)(`{3,}|~{3,})\s*(\w+)?/);
if (!open || open[3]?.toLowerCase() !== "mermaid") return null;
@@ -215,8 +484,12 @@ function mermaidFenceBody(raw: string): string | null {
return body.join("\n");
}
/** F7.1 (#22): legend shown beneath an intra-diagram-diffed mermaid block. */
const MERMAID_LEGEND =
/**
* F7.1 (#22): legend shown beneath an intra-diagram-diffed mermaid block.
* Exported for reuse by `previewAnnotations.ts`'s built-in-preview mermaid
* diff wiring (Task 7 §2.6 parity same legend markup, both surfaces).
*/
export const MERMAID_LEGEND =
'<div class="cw-mermaid-legend">' +
'<span class="cw-leg cw-leg-add">added</span>' +
'<span class="cw-leg cw-leg-chg">changed</span>' +
@@ -234,9 +507,90 @@ function wordMergedMarkdown(beforeRaw: string, afterRaw: string): string {
.join("");
}
/** The author whose span covers `offset` (half-open [start,end)), or null. */
export function authorAt(offset: number, spans: AuthorSpan[]): AuthorKind | null {
for (const s of spans) if (offset >= s.start && offset < s.end) return s.author;
return null;
}
const insClass = (a: AuthorKind | null): string => `cw-ins-${a ?? "none"}`;
const delClass = (a: AuthorKind | null): string => `cw-del-${a ?? "none"}`;
/**
* Author-colored word diff for a changed PROSE block in the review.
* `afterStart` is the landed-text offset of `afterRaw[0]` so an inserted run's
* offset maps into `spans`. Insertions are colored by the author covering their
* offset; deletions inherit the author of the insertion they are paired with in
* the same contiguous change cluster (adjacency heuristic) `cw-del-none` when a
* cluster has no insertion. Pure, deterministic.
*/
export function wordDiffByAuthor(
beforeRaw: string,
afterRaw: string,
afterStart: number,
spans: AuthorSpan[],
): string {
const parts = diffWordsWithSpace(beforeRaw, afterRaw);
// First pass: find each change cluster's insertion author (first added run).
// A cluster is a maximal run of added/removed parts bounded by unchanged parts.
let afterOff = afterStart;
const clusterAuthor: (AuthorKind | null)[] = []; // per part index, the cluster's del author
{
let off = afterStart;
let i = 0;
while (i < parts.length) {
const p = parts[i];
if (!p.added && !p.removed) {
clusterAuthor[i] = null;
off += p.value.length;
i++;
continue;
}
// a cluster: scan to its end, capturing the first added run's author
let j = i;
let author: AuthorKind | null = null;
let scanOff = off;
while (j < parts.length && (parts[j].added || parts[j].removed)) {
if (parts[j].added && author === null) author = authorAt(scanOff, spans);
if (parts[j].added) scanOff += parts[j].value.length;
j++;
}
for (let k = i; k < j; k++) clusterAuthor[k] = author;
off = scanOff;
i = j;
}
}
// Second pass: emit.
const out: string[] = [];
parts.forEach((p, i) => {
if (p.added) {
const a = authorAt(afterOff, spans);
out.push(`<ins class="${insClass(a)}">${p.value}</ins>`);
afterOff += p.value.length;
} else if (p.removed) {
out.push(`<del class="${delClass(clusterAuthor[i] ?? null)}">${p.value}</del>`);
} else {
out.push(p.value);
afterOff += p.value.length;
}
});
return out.join("");
}
export interface RenderOptions {
/** Per-block markdown→HTML renderer (test seam). Defaults to the bundled markdown-it. */
render?: (src: string) => string;
/**
* #48: the baseline was just PINNED (`reason === "pinned"` "Mark Changes as
* Reviewed" in snapshot mode). With zero changes since that pin, the
* on-render is fully clean no authorship coloring so a pin reads as
* "this is my clean starting point". Distinct from an ordinary accepted
* proposal, which since the native-surfaces migration retired the
* machine-landing baseline advance (INV-18/INV-7) stays a visible change-
* since-baseline and keeps its authorship coloring (F10 INV-33). Only
* consulted by `renderReview`.
*/
pinned?: boolean;
}
function defaultRender(src: string): string {
@@ -247,7 +601,17 @@ function chip(message: string): string {
return `<div class="cw-error">Could not render this block: ${md.utils.escapeHtml(message)}</div>`;
}
function renderOp(op: BlockOp, render: (src: string) => string): string {
/**
* F11 (INV-36): the source-range attributes a live block's wrapping element
* carries, so a preview selection can map back to a source markdown range. `""`
* when the block has no live source (a baseline-only deletion / a proposal
* block) those are skipped by the selection mapper. Pure + deterministic.
*/
function srcAttr(blk: { start: number; end: number } | undefined): string {
return blk ? ` data-src-start="${blk.start}" data-src-end="${blk.end}"` : "";
}
function renderOp(op: BlockOp, render: (src: string) => string, src = ""): string {
const safe = (src: string): string => {
try {
return render(src);
@@ -293,7 +657,7 @@ function renderOp(op: BlockOp, render: (src: string) => string): string {
}
break;
}
return `<div class="cw-blk ${cls}">${badge}${inner}</div>`;
return `<div class="cw-blk ${cls}"${src}>${badge}${inner}</div>`;
}
export type AuthorKind = "claude" | "human";
@@ -303,35 +667,91 @@ export interface AuthorSpan {
author: AuthorKind;
}
/**
* Task 7 (preview annotations, native-surfaces migration): the sentinel
* vocabulary generalizes beyond per-author spans to a plain "del" tag a
* re-surfaced baseline deletion has no author variant (CSS carries one fixed
* `.cw-del`, the F10 vocabulary). `injectSentinels`/`sentinelsToSpans` are
* exported so `previewAnnotations.ts` can drive the SAME token-aware
* discipline (#33/#47) for its 3-way ins-claude/ins-human/del marks;
* `colorByAuthor` (F9/F10) keeps its narrower per-author call, unaffected.
*/
export type SentinelTag = AuthorKind | "del";
export interface TaggedSpan {
start: number;
end: number;
tag: SentinelTag;
}
// Private-Use-Area sentinels (never appear in real content; markdown-it passes
// them through as plain text). Paired open/close per author.
const SENT = {
// them through as plain text). Paired open/close per tag.
const SENT: Record<SentinelTag, { open: string; close: string }> = {
claude: { open: "", close: "" },
human: { open: "", close: "" },
} as const;
del: { open: "", close: "" },
};
const SENT_TAGS = Object.keys(SENT) as SentinelTag[];
function isCloseSentinel(m: string): boolean {
return m === SENT.claude.close || m === SENT.human.close;
return SENT_TAGS.some((t) => SENT[t].close === m);
}
function authorBadge(authors: Set<AuthorKind>): { cls: string; label: string } | null {
if (authors.size === 0) return null;
if (authors.size > 1) return { cls: "cw-mixed", label: "mixed" };
const only = [...authors][0];
return only === "claude"
? { cls: "cw-by-claude", label: "Claude" }
: { cls: "cw-by-human", label: "You" };
// Markdown emphasis / code delimiters whose RUNS must never be split by an
// injected sentinel — a sentinel between two run chars (e.g. `*|*`) breaks
// markdown-it's delimiter pairing (#33 CASE1).
const isDelimChar = (c: string): boolean => c === "*" || c === "_" || c === "~" || c === "`";
/**
* #33 (CASE1): a sentinel must not land STRICTLY INSIDE a delimiter run. If `at`
* sits between two identical delimiter chars, snap it to the run's start (a
* position outside the run) so the run stays intact. Delimiters are invisible once
* rendered, so snapping only shifts the colored boundary across markup, never over
* visible text.
*/
function clampOffDelimiterRun(raw: string, at: number): number {
if (at <= 0 || at >= raw.length) return at;
if (!(isDelimChar(raw[at]) && raw[at - 1] === raw[at])) return at;
let p = at;
while (p > 0 && raw[p - 1] === raw[at]) p--;
return p;
}
/** Inject paired sentinels into a prose block's raw text for the spans clipped to it. */
function injectSentinels(raw: string, blockStart: number, spans: AuthorSpan[]): string {
/**
* Returns the offset within `raw` after block-level markdown markers (ATX
* headings, list markers, blockquote markers). Open sentinels must not be
* injected before these markers a PUA char at position 0 would prevent
* markdown-it from recognising the block construct (e.g. `##` is only a
* heading marker when it is the very first char on the line).
*/
function blockContentStart(raw: string): number {
// ATX headings: "#{1,6} " at the start of the line.
const heading = raw.match(/^#{1,6} /);
if (heading) return heading[0].length;
// Unordered list markers: "- ", "* ", "+ ".
const ul = raw.match(/^[-*+] /);
if (ul) return ul[0].length;
// Ordered list markers: "N. " or "N) ".
const ol = raw.match(/^\d+[.)]\s+/);
if (ol) return ol[0].length;
// Blockquotes: "> " or ">".
const bq = raw.match(/^> ?/);
if (bq) return bq[0].length;
return 0;
}
/** Inject paired sentinels into a block's raw text for the (tagged) spans clipped to it. */
export function injectSentinels(raw: string, blockStart: number, spans: TaggedSpan[]): string {
const inserts: { at: number; marker: string }[] = [];
// Open sentinels must not precede block-level markdown markers (heading/list/
// blockquote syntax at position 0) — a PUA char before "## " prevents markdown-it
// from recognising the heading. Clamp to the content-start offset.
const minOpen = blockContentStart(raw);
for (const s of spans) {
const lo = Math.max(0, s.start - blockStart);
const hi = Math.min(raw.length, s.end - blockStart);
if (hi <= lo) continue;
inserts.push({ at: lo, marker: SENT[s.author].open });
inserts.push({ at: hi, marker: SENT[s.author].close });
const lo = Math.max(minOpen, clampOffDelimiterRun(raw, Math.max(0, s.start - blockStart)));
const hi = clampOffDelimiterRun(raw, Math.min(raw.length, s.end - blockStart));
if (hi <= lo) continue; // empty (or clamped to empty) span contributes nothing
inserts.push({ at: lo, marker: SENT[s.tag].open });
inserts.push({ at: hi, marker: SENT[s.tag].close });
}
// Apply high offset → low so earlier offsets stay valid. At an equal offset
// (one span's close == the next's open), apply opens BEFORE closes so the
@@ -342,13 +762,69 @@ function injectSentinels(raw: string, blockStart: number, spans: AuthorSpan[]):
return out;
}
/** Replace the rendered sentinels with author <span> tags. */
function sentinelsToSpans(html: string): string {
return html
.split(SENT.claude.open).join('<span class="cw-by-claude">')
.split(SENT.claude.close).join("</span>")
.split(SENT.human.open).join('<span class="cw-by-human">')
.split(SENT.human.close).join("</span>");
const SENTINEL_OF: Record<string, { tag: SentinelTag; open: boolean } | undefined> = {};
for (const tag of SENT_TAGS) {
SENTINEL_OF[SENT[tag].open] = { tag, open: true };
SENTINEL_OF[SENT[tag].close] = { tag, open: false };
}
const ALL_SENTINELS = new RegExp(`[${SENT_TAGS.map((t) => SENT[t].open + SENT[t].close).join("")}]`, "g");
/**
* #33: token-aware replacement of the rendered sentinels with `cw-*` spans. A
* naive global string-replace (the old approach) could emit a span that
* CROSSES an element boundary `<span><strong>bo</span>ld</strong>` when a
* boundary fell inside an emphasis run (CASE3). This walks the rendered HTML and
* emits the span only around TEXT runs, CLOSING it before any `<tag>` and
* REOPENING it after, so a span is always well-nested within the inline elements
* (one `<span>` segment per text run). Tags are copied verbatim (with any stray
* sentinel stripped, so no Private-Use-Area char ever leaks). `kind` picks the
* author-class prefix (`cw-by-*`/`cw-ins-*`); the tag-less "del" mark (Task 7,
* no author variant) always renders the fixed `cw-del` regardless of `kind`.
* Pure, deterministic.
*/
export function sentinelsToSpans(html: string, kind: "by" | "ins" = "by"): string {
const out: string[] = [];
let current: SentinelTag | null = null; // which region we're inside
let spanOpen = false; // whether a <span> is currently open in `out`
const classFor = (tag: SentinelTag): string => (tag === "del" ? "cw-del" : `cw-${kind}-${tag}`);
const openSpan = () => {
if (current && !spanOpen) {
out.push(`<span class="${classFor(current)}">`);
spanOpen = true;
}
};
const closeSpan = () => {
if (spanOpen) {
out.push("</span>");
spanOpen = false;
}
};
for (let i = 0; i < html.length; i++) {
const ch = html[i];
const sentinel = SENTINEL_OF[ch];
if (sentinel) {
if (sentinel.open) current = sentinel.tag; // span opens lazily before the next text char
else {
closeSpan();
current = null;
}
continue;
}
if (ch === "<") {
// An HTML tag: never let an author span straddle it (text `<` is escaped to
// &lt;, so a raw `<` is always a real tag). Copy the tag verbatim, sentinel-free.
closeSpan();
const gt = html.indexOf(">", i);
const end = gt === -1 ? html.length - 1 : gt;
out.push(html.slice(i, end + 1).replace(ALL_SENTINELS, ""));
i = end;
continue;
}
openSpan();
out.push(ch);
}
closeSpan();
return out.join("");
}
/**
@@ -361,20 +837,42 @@ export function colorByAuthor(
blockStart: number,
spans: AuthorSpan[],
render: (src: string) => string,
kind: "by" | "ins" = "by",
): string {
const overlapping = spans.filter((s) => s.end > blockStart && s.start < blockStart + raw.length);
const injected = injectSentinels(raw, blockStart, overlapping);
return sentinelsToSpans(render(injected));
const injected = injectSentinels(
raw,
blockStart,
overlapping.map((s) => ({ start: s.start, end: s.end, tag: s.author })),
);
return sentinelsToSpans(render(injected), kind);
}
/** Off-state body: the current buffer as plain markdown, no annotations (INV-33). */
/**
* Off-state body: the current buffer as plain markdown, no annotations (INV-33).
* Each block is wrapped in a bare `<div data-src-start/end>` so the off-mode
* preview is still a selectionsource mapping surface (INV-36) while staying
* visually clean no `cw-` annotation classes. A doc with no blocks (empty /
* whitespace-only) renders whole. Pure + deterministic.
*
* Tradeoff of the locked block-level mapping (§6.7): rendering PER BLOCK (so each
* carries its offsets) means a markdown construct split across blank-line-
* separated blocks a reference-link use and its definition doesn't resolve
* across blocks. `renderReview` already rendered per-block; this keeps the two
* modes consistent rather than faithful-but-different. Characterized in tests.
*/
export function renderPlain(currentText: string, opts: RenderOptions = {}): string {
const render = opts.render ?? defaultRender;
try {
return render(currentText);
} catch (err) {
return chip(err instanceof Error ? err.message : String(err));
}
const safe = (src: string): string => {
try {
return render(src);
} catch (err) {
return chip(err instanceof Error ? err.message : String(err));
}
};
const blocks = splitBlocksWithRanges(currentText);
if (blocks.length === 0) return safe(currentText);
return blocks.map((b) => `<div${srcAttr(b)}>${safe(b.raw)}</div>`).join("\n");
}
export interface ProposalView {
@@ -386,6 +884,10 @@ export interface ProposalView {
replaced: string;
/** the proposed replacement text. */
replacement: string;
/** F12/#64 (INV-48): the pre-apply original, set after optimistic apply. */
original?: string;
/** Who made the proposal — colors the del/ins. Defaults to "claude". */
author?: AuthorKind;
}
function proposalBlockHtml(p: ProposalView, render: (src: string) => string): string {
@@ -397,12 +899,19 @@ function proposalBlockHtml(p: ProposalView, render: (src: string) => string): st
}
};
const unanchored = p.anchorStart === null ? " cw-proposal-unanchored" : "";
const before = p.replaced ? `<del class="cw-del">${safe(p.replaced)}</del>` : "";
const after = `<ins class="cw-add">${safe(p.replacement)}</ins>`;
const who = p.author ?? "claude";
const before = p.replaced ? `<del class="cw-del-${who}">${safe(p.replaced)}</del>` : "";
const after = `<ins class="cw-ins-${who}">${safe(p.replacement)}</ins>`;
const actions =
`<span class="cw-actions">` +
`<button class="cw-accept" data-action="accept">✓</button>` +
`<button class="cw-reject" data-action="reject">✗</button>` +
`<span class="cw-btngroup">` +
`<button class="cw-accept" data-action="accept">Accept</button>` +
`<button class="cw-caret" data-action="acceptAll" title="Accept all pending proposals">▾</button>` +
`</span>` +
`<span class="cw-btngroup">` +
`<button class="cw-reject" data-action="reject">Reject</button>` +
`<button class="cw-caret" data-action="rejectAll" title="Reject all pending proposals">▾</button>` +
`</span>` +
`</span>`;
return `<div class="cw-proposal${unanchored}" data-proposal-id="${md.utils.escapeHtml(p.id)}">${actions}${before}${after}</div>`;
}
@@ -410,18 +919,27 @@ function proposalBlockHtml(p: ProposalView, render: (src: string) => string): st
function renderReviewOp(
op: BlockOp,
render: (src: string) => string,
colored: (raw: string) => string,
src: string,
changedHtml?: string,
): string {
// removed blocks and any changed block (atomic fences diffed whole; non-atomic prose
// word-merged) render via renderOp — no author sentinels (deletions neutral, spec §6.7).
if (op.kind === "removed" || op.kind === "changed") return renderOp(op, render);
return `<div class="cw-blk ${op.kind === "added" ? "cw-added" : "cw-unchanged"}">${colored(op.block.raw)}</div>`;
// A non-atomic changed prose block is pre-rendered with author-colored ins/del
// (changedHtml). Removed blocks and atomic changed fences stay neutral via renderOp
// (standalone deletion → neutral, per the adjacency heuristic). `src` is "" for a
// removed block (no live source — INV-36).
if (op.kind === "changed" && !op.atomic && changedHtml !== undefined) {
return `<div class="cw-blk cw-changed"${src}>${changedHtml}</div>`;
}
if (op.kind === "removed" || op.kind === "changed") return renderOp(op, render, src);
// "added": changedHtml is colorByAuthor sentinel output (markdown-safe, cw-ins-*).
// "unchanged": render(raw) — plain, no author coloring (color = "changed since baseline").
return `<div class="cw-blk ${op.kind === "added" ? "cw-added" : "cw-unchanged"}"${src}>${changedHtml ?? render(op.block.raw)}</div>`;
}
/**
* On-state body (INV-33): the F7 baseline diff added/changed PROSE author-colored
* via colorByAuthor (F9 sentinels), deletions struck overlaid with F4 pending
* proposals as blue cw-proposal blocks (/). One pass, pure, vscode-free.
* On-state body: the F7 baseline diff added/changed PROSE author-colored via
* wordDiffByAuthor (cw-ins-{author}/cw-del-{author} per token), unchanged blocks render plain,
* deletions struck neutral overlaid with F4 pending proposals as blue cw-proposal
* blocks (/). One pass, pure, vscode-free.
*
* A resolved proposal renders INLINE, right after the current-side block its anchor
* falls in (#31) so "what is Claude proposing, and where?" is answered by
@@ -430,6 +948,26 @@ function renderReviewOp(
* INV-34). Deterministic: proposals in the same block are ordered by anchorStart
* then id; trailing proposals keep input order.
*/
/**
* F12/#64 (INV-50): `currentText` with every resolved pending proposal's applied
* span reverted to its original (`replaced`) the "landed" text the baseline diff
* should run against, so a pending proposal renders ONCE (as a proposal), never also
* as a landed change. Reverts highlow so earlier offsets stay valid. Pure. The
* preview's summary tally diffs against this too, so the toolbar count matches the
* body (a pending change is not double-counted as both a landed add/remove and a
* proposal).
*/
export function landedTextOf(currentText: string, proposals: ProposalView[]): string {
const pendingApplied = proposals
.filter((p) => p.anchorStart !== null)
.sort((a, b) => b.anchorStart! - a.anchorStart!);
let landedText = currentText;
for (const p of pendingApplied) {
landedText = landedText.slice(0, p.anchorStart!) + p.replaced + landedText.slice(p.anchorEnd!);
}
return landedText;
}
export function renderReview(
baselineText: string,
currentText: string,
@@ -438,10 +976,47 @@ export function renderReview(
opts: RenderOptions = {},
): string {
const render = opts.render ?? defaultRender;
const ranges = splitBlocksWithRanges(currentText);
const ops = diffBlocks(baselineText, currentText);
// Associate each resolved proposal with the current-side block index whose range
// F12/#64 (INV-50): with optimistic apply the proposed text is already in
// `currentText`, so a naive baseline→current diff would render each proposed
// change BOTH as a landed diff and as its proposal block. Diff against the
// "landed" text (current minus pending proposals) so they render once.
const pendingApplied = proposals
.filter((p) => p.anchorStart !== null)
.sort((a, b) => b.anchorStart! - a.anchorStart!);
const landedText = landedTextOf(currentText, proposals);
const ranges = splitBlocksWithRanges(landedText);
const ops = diffBlocks(baselineText, landedText);
// #48: right after a PIN (baseline reason "pinned") with no changes since, the
// panel is fully clean: no change marks (already absent) AND no authorship
// coloring, so the pin reads as "this is my clean starting point". Suppress
// wordDiffByAuthor for every block in this case; data-src mapping and any pending
// proposals (review actions, not annotations) are kept below.
const clean = opts.pinned === true && ops.every((o) => o.kind === "unchanged");
// Map a currentText offset to its landedText offset (account for reverted spans
// that precede it; reverts were applied high→low so the cumulative delta is stable).
const toLanded = (curOff: number): number => {
let delta = 0;
for (const p of [...pendingApplied].sort((a, b) => a.anchorStart! - b.anchorStart!)) {
if (p.anchorStart! < curOff) delta += p.replaced.length - (p.anchorEnd! - p.anchorStart!);
}
return curOff + delta;
};
// F12/#64 (INV-49/50): blocks are split from `landedText`, so `blk.start` is a
// landedText offset — but `authorSpans` arrive in `currentText` coordinates. A
// colored block sitting AFTER a length-changing pending proposal would otherwise
// be mis-colored (the offsets diverge by the revert delta). Map the spans into
// landedText coordinates once so authorship coloring stays aligned.
const landedSpans: AuthorSpan[] = authorSpans.map((s) => ({
...s,
start: toLanded(s.start),
end: toLanded(s.end),
}));
// Associate each resolved proposal with the landedText block index whose range
// it anchors into: the largest block with start <= anchorStart (the containing
// block, or the nearest preceding block when the anchor sits in a gap). A
// resolved anchor before all blocks, and every unresolved proposal, trails.
@@ -453,7 +1028,7 @@ export function renderReview(
const byBlock = new Map<number, ProposalView[]>();
const trailing: ProposalView[] = [];
for (const p of proposals) {
const j = p.anchorStart === null ? -1 : blockOf(p.anchorStart);
const j = p.anchorStart === null ? -1 : blockOf(toLanded(p.anchorStart));
if (j < 0) {
trailing.push(p);
continue;
@@ -469,9 +1044,22 @@ export function renderReview(
for (const op of ops) {
const blockIndex = op.kind === "removed" ? -1 : ci;
const blk = op.kind === "removed" ? undefined : ranges[ci++];
const colored = (raw: string): string =>
blk ? colorByAuthor(raw, blk.start, authorSpans, render) : render(raw);
bodyParts.push(renderReviewOp(op, render, colored));
// Only ADDED and CHANGED prose blocks are author-colored (they differ since baseline);
// UNCHANGED blocks render plain (color means "changed since baseline").
// A non-atomic changed prose block: author-colored word diff (ins by author,
// del by adjacency). An added block: sentinel-safe colorByAuthor with kind="ins"
// (markdown-it parses the block first, so ## → <h2> before sentinels become spans;
// the old wordDiffByAuthor path wrapped raw in <ins> BEFORE parsing — structural bug).
// `blk.start` is the landed offset of the after-side text.
const changedHtml =
blk && !clean
? op.kind === "changed" && !op.atomic
? render(wordDiffByAuthor(op.before.raw, op.block.raw, blk.start, landedSpans))
: op.kind === "added"
? colorByAuthor(op.block.raw, blk.start, landedSpans, render, "ins")
: undefined
: undefined;
bodyParts.push(renderReviewOp(op, render, srcAttr(blk), changedHtml));
const here = blockIndex >= 0 ? byBlock.get(blockIndex) : undefined;
if (here) for (const p of here) bodyParts.push(proposalBlockHtml(p, render));
}
-285
View File
@@ -1,285 +0,0 @@
/**
* TrackChangesPreviewController F7 vscode layer (spec §6.2/§6.4). Owns one
* sealed webview panel per markdown document, beside the source editor. On open /
* debounced edit / F6 baseline-epoch change it reads the baseline (from the
* reused DiffViewController) + the live buffer, runs the pure render engine, and
* posts the HTML. Pure read-only: never mutates the document, sidecar, or
* baseline (INV-20). The webview is sealed: local assets only, strict CSP,
* per-load nonce, no network (INV-21).
*/
import * as path from "node:path";
import { randomBytes } from "node:crypto";
import * as vscode from "vscode";
import type { DiffViewController } from "./diffViewController";
import type { AttributionController } from "./attributionController";
import type { ProposalController } from "./proposalController";
import { renderReview, renderPlain, diffBlocks, type BlockOp } from "./trackChangesModel";
const VIEW_TYPE = "cowriting.trackChangesPreview";
const DEBOUNCE_MS = 150;
export class TrackChangesPreviewController implements vscode.Disposable {
private readonly disposables: vscode.Disposable[] = [];
private readonly panels = new Map<string, vscode.WebviewPanel>();
private readonly lastModel = new Map<string, BlockOp[]>();
private readonly debounces = new Map<string, NodeJS.Timeout>();
/** F10: per-panel annotations mode — on (default) shows review marks, off is clean. */
private readonly mode = new Map<string, "on" | "off">();
/** F10 (PUC-6): off-panel indicator of pending proposals on the active doc. */
private readonly statusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 88);
constructor(
private readonly diffView: DiffViewController,
private readonly extensionUri: vscode.Uri,
private readonly attribution: AttributionController,
private readonly proposals: ProposalController,
) {
this.disposables.push(
vscode.commands.registerCommand("cowriting.showTrackChangesPreview", () =>
this.show(vscode.window.activeTextEditor?.document),
),
vscode.workspace.onDidChangeTextDocument((e) => this.onEdit(e.document)),
this.diffView.onDidChangeBaseline(({ uri }) => this.refreshByUri(uri)),
this.proposals.onDidChangeProposals(({ uri }) => {
this.refreshByUri(uri);
this.updateStatus(uri);
}),
this.statusItem,
);
this.statusItem.command = "cowriting.showTrackChangesPreview";
}
private isMarkdown(document: vscode.TextDocument): boolean {
return document.languageId === "markdown";
}
/** Open or reveal the preview for a markdown document (PUC-1). */
show(document: vscode.TextDocument | undefined): void {
if (!document || !this.isMarkdown(document)) {
void vscode.window.showWarningMessage(
"Cowriting: open a Markdown document to use the track-changes preview (F6 covers other files).",
);
return;
}
const key = document.uri.toString();
const existing = this.panels.get(key);
if (existing) {
existing.reveal(vscode.ViewColumn.Beside);
this.refresh(document);
return;
}
const name = path.basename(document.uri.path) || "untitled";
const panel = vscode.window.createWebviewPanel(
VIEW_TYPE,
`Review: ${name}`,
{ viewColumn: vscode.ViewColumn.Beside, preserveFocus: true },
{
enableScripts: true,
retainContextWhenHidden: false,
localResourceRoots: [vscode.Uri.joinPath(this.extensionUri, "out", "media")],
},
);
panel.webview.html = this.shellHtml(panel.webview);
panel.onDidDispose(
() => {
this.panels.delete(key);
this.lastModel.delete(key);
this.mode.delete(key);
// A panel is gone: re-show the off-panel indicator if proposals remain.
this.updateStatus(key);
},
null,
this.disposables,
);
// F10: the webview posts the annotations toggle + ✓/✗ proposal decisions back.
panel.webview.onDidReceiveMessage(
(m: { type?: string; mode?: "on" | "off"; proposalId?: string }) => {
if (m?.type === "setMode" && (m.mode === "on" || m.mode === "off")) {
this.mode.set(key, m.mode);
this.refresh(document);
} else if (m?.type === "accept" && m.proposalId) {
void this.proposals
.acceptById(this.proposals.keyFor(document), m.proposalId)
.then(() => this.refresh(document));
} else if (m?.type === "reject" && m.proposalId) {
this.proposals.rejectById(this.proposals.keyFor(document), m.proposalId);
this.refresh(document);
}
},
null,
this.disposables,
);
this.panels.set(key, panel);
// A panel is now open for this doc — the off-panel indicator is redundant.
this.hideStatus();
this.refresh(document);
}
private onEdit(document: vscode.TextDocument): void {
const key = document.uri.toString();
if (!this.panels.has(key)) return;
const pending = this.debounces.get(key);
if (pending) clearTimeout(pending);
this.debounces.set(
key,
setTimeout(() => {
this.debounces.delete(key);
this.refresh(document);
}, DEBOUNCE_MS),
);
}
private refreshByUri(uri: string): void {
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri);
if (doc) this.refresh(doc);
}
/** Recompute the model + post HTML to the panel for its current mode (no-op if no panel). */
refresh(document: vscode.TextDocument): void {
const key = document.uri.toString();
const panel = this.panels.get(key);
if (!panel) return;
const mode = this.mode.get(key) ?? "on";
const current = document.getText();
const baseline = this.diffView.getBaseline(key);
const baselineText = baseline?.text ?? current; // no baseline → no change-marks
const ops = diffBlocks(baselineText, current);
this.lastModel.set(key, ops);
if (mode === "off") {
void panel.webview.postMessage({ type: "render", mode, html: renderPlain(current) });
return;
}
const spans = this.attribution.spansFor(document);
const proposals = this.proposals.listProposals(document);
const summary = {
added: ops.filter((o) => o.kind === "added").length,
removed: ops.filter((o) => o.kind === "removed").length,
proposals: proposals.length,
};
void panel.webview.postMessage({
type: "render",
mode,
html: renderReview(baselineText, current, spans, proposals),
epoch: this.epochLabel(baseline),
summary,
});
}
/** F10 (PUC-6): off-panel proposal indicator on the active doc. Hidden when a panel is open. */
private updateStatus(uri: string): void {
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri);
if (!doc) {
this.hideStatus();
return;
}
const n = this.proposals.listProposals(doc).length;
if (n === 0 || this.panels.has(uri)) {
this.hideStatus();
return;
}
this.statusItem.text = `$(comment-discussion) ${n} Claude proposal${n === 1 ? "" : "s"}`;
this.statusItem.tooltip = "Cowriting: open the review preview to accept/reject Claude's proposals";
this.statusItem.show();
}
/**
* Hide the off-panel indicator AND clear its text, so the `statusText()` seam
* is honest: a hidden indicator reports `undefined` (not its stale last value).
*/
private hideStatus(): void {
this.statusItem.text = "";
this.statusItem.hide();
}
private epochLabel(baseline: { reason: string; capturedAt: string } | undefined): string {
if (!baseline) return "opened (no baseline yet)";
const time = new Date(baseline.capturedAt).toLocaleTimeString();
switch (baseline.reason) {
case "machine-landing":
return `Claude landed ${time}`;
case "pinned":
return `pinned ${time}`;
default:
return `opened ${time}`;
}
}
private shellHtml(webview: vscode.Webview): string {
const nonce = randomBytes(16).toString("base64");
const scriptUri = webview.asWebviewUri(
vscode.Uri.joinPath(this.extensionUri, "out", "media", "preview.js"),
);
const styleUri = webview.asWebviewUri(
vscode.Uri.joinPath(this.extensionUri, "out", "media", "preview.css"),
);
// Sealed CSP (INV-21): no network. 'unsafe-inline' style is required only for
// mermaid's dynamically injected <style> tags; scripts are nonce-gated and
// strictly local (no remote/CDN script source).
const csp =
`default-src 'none'; ` +
`img-src ${webview.cspSource} data:; ` +
`font-src ${webview.cspSource}; ` +
`style-src ${webview.cspSource} 'unsafe-inline'; ` +
`script-src 'nonce-${nonce}';`;
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Security-Policy" content="${csp}" />
<link href="${styleUri}" rel="stylesheet" />
<title>Track changes</title>
</head>
<body>
<div id="cw-header">
<label id="cw-toggle"><input type="checkbox" id="cw-annotations" checked /> Annotations</label>
<span id="cw-epoch">Review</span>
<span id="cw-summary"></span>
<span id="cw-legend"></span>
</div>
<div id="cw-body"></div>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
</html>`;
}
// ---- test seam (§6.4) ----
isOpen(uriString: string): boolean {
return this.panels.has(uriString);
}
getLastModel(uriString: string): BlockOp[] | undefined {
return this.lastModel.get(uriString);
}
/** F10 test seam: the review HTML the panel would post for a doc (on-state). */
renderHtmlFor(uriString: string): string {
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uriString);
if (!doc) return "";
const current = doc.getText();
const baseline = this.diffView.getBaseline(uriString);
return renderReview(
baseline?.text ?? current,
current,
this.attribution.spansFor(doc),
this.proposals.listProposals(doc),
);
}
/** F10: current annotations mode for a panel (default on). */
getMode(uriString: string): "on" | "off" {
return this.mode.get(uriString) ?? "on";
}
/** F10: set the annotations mode and re-render (the programmatic twin of the header toggle). */
setMode(uriString: string, mode: "on" | "off"): void {
this.mode.set(uriString, mode);
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uriString);
if (doc) this.refresh(doc);
}
/** F10 test seam (SLICE-4 E2E): the off-panel status-bar indicator text, if shown. */
statusText(): string | undefined {
return this.statusItem.text || undefined;
}
dispose(): void {
for (const t of this.debounces.values()) clearTimeout(t);
for (const p of this.panels.values()) p.dispose();
for (const d of this.disposables) d.dispose();
}
}
+122
View File
@@ -0,0 +1,122 @@
/**
* turnProgress.ts pure reduction of @cline/sdk Agent runtime events into a
* small UI-facing progress snapshot (#60, spec coauthoring-live-progress.md §3.2).
*
* INV-43: vscode-free. INV-46: a pure function no vscode, no SDK runtime
* dependency (`AgentRuntimeEvent` is imported TYPE-only, so it is erased at
* compile and never pulls the ESM SDK into the bundle). All eventstate logic
* lives here so it is unit-tested in isolation; the UI call sites only format and
* relay snapshots.
*/
import type { AgentRuntimeEvent } from "@cline/shared";
export type TurnPhase = "thinking" | "writing" | "tool";
export interface TurnProgressSnapshot {
phase: TurnPhase;
/** present iff phase === "tool" — the running tool's name. */
tool?: string;
/** accumulated assistant-text length so far. */
chars: number;
/** running total tokens (input+output); undefined until the first usage event. */
tokens?: number;
/** the new assistant-text chunk since the last snapshot (for the OutputChannel). */
textDelta?: string;
}
export interface TurnProgressState {
phase: TurnPhase;
chars: number;
tokens?: number;
/** stack of tool names currently running (depth-tracked for overlap). */
activeTools: string[];
/** true once any assistant text has streamed (tool-finish then reverts to writing). */
sawText: boolean;
}
export function createTurnProgressState(): TurnProgressState {
return { phase: "thinking", chars: 0, tokens: undefined, activeTools: [], sawText: false };
}
function restingPhase(state: TurnProgressState): TurnPhase {
if (state.activeTools.length) return "tool";
return state.sawText ? "writing" : "thinking";
}
function toSnapshot(state: TurnProgressState, textDelta?: string): TurnProgressSnapshot {
return {
phase: state.phase,
tool: state.phase === "tool" ? state.activeTools[state.activeTools.length - 1] : undefined,
chars: state.chars,
tokens: state.tokens,
textDelta,
};
}
/**
* Fold one SDK event into the state, returning the next state and the snapshot to
* emit (snapshot undefined for events that don't change the surface).
*/
export function reduceTurnProgress(
state: TurnProgressState,
event: AgentRuntimeEvent,
): { state: TurnProgressState; snapshot?: TurnProgressSnapshot } {
switch (event.type) {
case "run-started":
case "turn-started": {
const next: TurnProgressState = { ...state, phase: restingPhase(state) };
return { state: next, snapshot: toSnapshot(next) };
}
case "assistant-text-delta": {
const next: TurnProgressState = {
...state,
phase: state.activeTools.length ? "tool" : "writing",
chars: event.accumulatedText.length,
sawText: true,
};
return { state: next, snapshot: toSnapshot(next, event.text) };
}
case "assistant-reasoning-delta": {
// Reasoning TEXT is not surfaced (operator fork); collapse to motion only.
const next: TurnProgressState = { ...state, phase: state.activeTools.length ? "tool" : "thinking" };
return { state: next, snapshot: toSnapshot(next) };
}
case "tool-started": {
const activeTools = [...state.activeTools, event.toolCall.toolName];
const next: TurnProgressState = { ...state, phase: "tool", activeTools };
return { state: next, snapshot: toSnapshot(next) };
}
case "tool-updated": {
const next: TurnProgressState = { ...state, phase: "tool" };
return { state: next, snapshot: toSnapshot(next) };
}
case "tool-finished": {
const name = event.toolCall.toolName;
const idx = state.activeTools.lastIndexOf(name);
const activeTools = idx >= 0 ? state.activeTools.filter((_, i) => i !== idx) : state.activeTools.slice(0, -1);
const next: TurnProgressState = { ...state, activeTools, phase: "thinking" };
next.phase = restingPhase(next);
return { state: next, snapshot: toSnapshot(next) };
}
case "usage-updated": {
const tokens = event.usage.inputTokens + event.usage.outputTokens || undefined;
const next: TurnProgressState = { ...state, tokens };
return { state: next, snapshot: toSnapshot(next) };
}
default:
return { state };
}
}
/** Render the notification activity line from a snapshot (pure; spec §2.1). */
export function formatProgressLine(s: TurnProgressSnapshot): string {
let head: string;
if (s.phase === "tool") head = `running ${s.tool ?? "tool"}`;
else if (s.phase === "writing") head = `writing… (${s.chars} chars)`;
else head = "thinking…";
return s.tokens ? `${head} · ${formatTokens(s.tokens)} tokens` : head;
}
export function formatTokens(n: number): string {
return n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n);
}
+26
View File
@@ -60,3 +60,29 @@ export function selectionRejection(ctx: SelectionContext): string | null {
}
return null;
}
export interface EditRouteContext {
/** Was the command invoked on a specific resource (a tab right-click)? */
hasUri: boolean;
/** Does that resource match the focused editor's document? */
uriMatchesActiveEditor: boolean;
/** Is there a focused text editor at all? */
hasActiveEditor: boolean;
/** Is the focused editor's selection empty (no highlight)? */
selectionEmpty: boolean;
}
/**
* Route the single "Ask Claude to Edit" gesture (`cowriting.edit`) to the
* selection or whole-document flow. One conceptual command, two destinations:
*
* - A tab right-click on a document that ISN'T the focused editor has no
* selection to act on edit the whole document.
* - Otherwise a non-empty selection in the focused editor edit the selection;
* an empty selection (or no editor) edit the whole document.
*/
export function routeEdit(ctx: EditRouteContext): "selection" | "document" {
if (ctx.hasUri && !ctx.uriMatchesActiveEditor) return "document";
if (ctx.hasActiveEditor && !ctx.selectionEmpty) return "selection";
return "document";
}
+52 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { buildFingerprint, resolve, shift, type OffsetRange } from "../src/anchorer";
import { buildFingerprint, resolve, shift, shiftTracked, type OffsetRange } from "../src/anchorer";
const DOC = "line0\nline1 needle here\nline2\nneedle again on line3\n";
@@ -67,3 +67,54 @@ describe("shift", () => {
expect(shift(r, { start: 3, end: 7, newLength: 1 })).toEqual({ start: 3, end: 7 });
});
});
describe("shiftTracked (#70 — tracked applied spans)", () => {
const r: OffsetRange = { start: 10, end: 20 };
it("edit entirely before the span shifts both endpoints by the delta", () => {
expect(shiftTracked(r, { start: 0, end: 0, newLength: 3 })).toEqual({ start: 13, end: 23 });
});
it("edit entirely after the span leaves it unchanged", () => {
expect(shiftTracked(r, { start: 25, end: 28, newLength: 0 })).toEqual({ start: 10, end: 20 });
});
it("edit fully inside the span keeps the start and grows/shrinks the end", () => {
// replace [12,15) with 5 chars (delta = +2)
expect(shiftTracked(r, { start: 12, end: 15, newLength: 5 })).toEqual({ start: 10, end: 22 });
});
it("replacing the span's exact full contents is an interior edit", () => {
expect(shiftTracked(r, { start: 10, end: 20, newLength: 4 })).toEqual({ start: 10, end: 14 });
});
it("an insertion exactly at the span start lands BEFORE the span (never absorbed)", () => {
expect(shiftTracked(r, { start: 10, end: 10, newLength: 6 })).toEqual({ start: 16, end: 26 });
});
it("an insertion exactly at the span end lands AFTER the span (never absorbed)", () => {
expect(shiftTracked(r, { start: 20, end: 20, newLength: 6 })).toEqual({ start: 10, end: 20 });
});
it("an insertion at an EMPTY span's point lands before it (deletion-proposal span)", () => {
const empty: OffsetRange = { start: 10, end: 10 };
expect(shiftTracked(empty, { start: 10, end: 10, newLength: 3 })).toEqual({ start: 13, end: 13 });
});
it("an edit straddling the span's start boundary is distrusted (INV-11)", () => {
expect(shiftTracked(r, { start: 8, end: 12, newLength: 0 })).toBe("distrusted");
});
it("an edit straddling the span's end boundary is distrusted (INV-11)", () => {
expect(shiftTracked(r, { start: 18, end: 22, newLength: 1 })).toBe("distrusted");
});
it("an edit fully containing the span (e.g. a wholesale replace) is distrusted (INV-11)", () => {
expect(shiftTracked(r, { start: 0, end: 30, newLength: 12 })).toBe("distrusted");
});
it("a deletion consuming an EMPTY span's neighborhood is distrusted (INV-11)", () => {
const empty: OffsetRange = { start: 10, end: 10 };
expect(shiftTracked(empty, { start: 8, end: 12, newLength: 0 })).toBe("distrusted");
});
});
+19
View File
@@ -107,6 +107,25 @@ describe("multi-edit sequences", () => {
});
});
describe("applyChange — geometric-only (undo/redo, #38)", () => {
it("attributeInserted=false adds NO span for re-inserted text (restored text stays neutral)", () => {
// Undo re-inserts 'bravo ' at offset 6 into an empty span list → no span.
const r = applyChange([], { start: 6, end: 6, newLength: 6 }, HUMAN, ctx(), false);
expect(r).toEqual([]);
});
it("attributeInserted=false still SHIFTS existing spans by the edit delta", () => {
// A real human span sits after the re-insert point; it must shift right by 6,
// but the re-inserted chars themselves get no new span.
const r = applyChange([span("tail", 20, 30, HUMAN)], { start: 6, end: 6, newLength: 6 }, HUMAN, ctx(), false);
expect(ranges(r)).toEqual([[26, 36]]);
expect(authors(r)).toEqual(["human"]);
});
it("attributeInserted=false still reconciles geometry of a deletion (removes a covered span)", () => {
const r = applyChange([span("a", 3, 6, AGENT)], { start: 0, end: 10, newLength: 0 }, HUMAN, ctx(), false);
expect(r).toEqual([]);
});
});
describe("coalesce", () => {
it("merges adjacent same-author same-turn spans, keeps earliest createdAt", () => {
const a = { ...span("a", 0, 3, HUMAN), createdAt: "2026-06-09T00:00:00.000Z" };
+39 -3
View File
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach } from "vitest";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { BaselineStore, type Baseline } from "../src/baselineStore";
import { BaselineStore, normalizeReason, type Baseline } from "../src/baselineStore";
let dir: string;
@@ -16,9 +16,23 @@ afterEach(() => {
const KEY = "a1b2c3"; // a stand-in for the controller's sha256(uri) key
function sample(uri = "file:///ws/notes/chapter-1.md"): Baseline {
return { uri, text: "hello\nworld\n", capturedAt: "2026-06-11T00:00:00.000Z", reason: "opened" };
return { uri, text: "hello\nworld\n", capturedAt: "2026-06-11T00:00:00.000Z", reason: "entered" };
}
describe("normalizeReason (INV-7 legacy migration)", () => {
it("migrates legacy 'opened' → 'entered'", () => {
expect(normalizeReason("opened")).toBe("entered");
});
it("migrates legacy 'machine-landing' → 'entered'", () => {
expect(normalizeReason("machine-landing")).toBe("entered");
});
it("passes current reasons through unchanged", () => {
expect(normalizeReason("entered")).toBe("entered");
expect(normalizeReason("pinned")).toBe("pinned");
expect(normalizeReason("head")).toBe("head");
});
});
describe("BaselineStore", () => {
it("returns null for a key with no baseline", () => {
expect(new BaselineStore(dir).load(KEY)).toBeNull();
@@ -36,9 +50,31 @@ describe("BaselineStore", () => {
expect(store.load(KEY)).toEqual(b);
});
it("migrates a legacy on-disk reason ('opened') to 'entered' on load", () => {
const store = new BaselineStore(dir);
fs.mkdirSync(path.dirname(store.baselinePath(KEY)), { recursive: true });
fs.writeFileSync(
store.baselinePath(KEY),
JSON.stringify({ uri: "file:///a.md", text: "legacy\n", capturedAt: "2026-06-11T00:00:00.000Z", reason: "opened" }),
"utf8",
);
expect(store.load(KEY)?.reason).toBe("entered");
});
it("migrates a legacy on-disk reason ('machine-landing') to 'entered' on load", () => {
const store = new BaselineStore(dir);
fs.mkdirSync(path.dirname(store.baselinePath(KEY)), { recursive: true });
fs.writeFileSync(
store.baselinePath(KEY),
JSON.stringify({ uri: "file:///a.md", text: "legacy\n", capturedAt: "2026-06-11T00:00:00.000Z", reason: "machine-landing" }),
"utf8",
);
expect(store.load(KEY)?.reason).toBe("entered");
});
it("overwrites in place: the newest epoch wins, no history kept", () => {
const store = new BaselineStore(dir);
store.save(KEY, { uri: "untitled:Untitled-1", text: "v1", capturedAt: "2026-06-11T00:00:00.000Z", reason: "opened" });
store.save(KEY, { uri: "untitled:Untitled-1", text: "v1", capturedAt: "2026-06-11T00:00:00.000Z", reason: "entered" });
store.save(KEY, { uri: "untitled:Untitled-1", text: "v2", capturedAt: "2026-06-11T00:01:00.000Z", reason: "pinned" });
expect(store.load(KEY)).toEqual({ uri: "untitled:Untitled-1", text: "v2", capturedAt: "2026-06-11T00:01:00.000Z", reason: "pinned" });
});
+169
View File
@@ -0,0 +1,169 @@
import { describe, expect, it, vi } from "vitest";
// The pure set logic lives in a vscode-free helper the class wraps, so unit-test the class
// with a Memento stub (vitest runs vscode-free; the module imports vscode only for types +
// EventEmitter — mock it).
vi.mock("vscode", () => ({
EventEmitter: class {
private handlers: Array<(e: unknown) => void> = [];
event = (h: (e: unknown) => void) => { this.handlers.push(h); return { dispose() {} }; };
fire(e: unknown) { for (const h of this.handlers) h(e); }
dispose() {}
},
Uri: { parse: (s: string) => ({ toString: () => s }) },
commands: { executeCommand: vi.fn() },
window: { activeTextEditor: undefined },
}));
import * as vscode from "vscode";
import { CoeditingRegistry } from "../src/coeditingRegistry";
function memento(): { store: Map<string, unknown> } & { get: any; update: any } {
const store = new Map<string, unknown>();
return {
store,
get: (k: string, dflt?: unknown) => (store.has(k) ? store.get(k) : dflt),
update: (k: string, v: unknown) => { store.set(k, v); return Promise.resolve(); },
} as any;
}
describe("CoeditingRegistry", () => {
it("enter/exit flips membership and fires onDidChange", () => {
const reg = new CoeditingRegistry(memento() as any);
const events: Array<{ uri: string; coediting: boolean }> = [];
reg.onDidChange((e) => events.push(e as any));
const uri = { toString: () => "file:///a.md" } as any;
expect(reg.isCoediting(uri)).toBe(false);
reg.enter(uri);
expect(reg.isCoediting(uri)).toBe(true);
reg.exit(uri);
expect(reg.isCoediting(uri)).toBe(false);
expect(events).toEqual([
{ uri: "file:///a.md", coediting: true },
{ uri: "file:///a.md", coediting: false },
]);
});
it("persists the set across construction (reload survival)", () => {
const m = memento();
const reg1 = new CoeditingRegistry(m as any);
reg1.enter({ toString: () => "file:///a.md" } as any);
const reg2 = new CoeditingRegistry(m as any);
expect(reg2.isCoediting({ toString: () => "file:///a.md" } as any)).toBe(true);
expect(reg2.list()).toEqual(["file:///a.md"]);
});
it("enter is idempotent (no duplicate events)", () => {
const reg = new CoeditingRegistry(memento() as any);
const events: unknown[] = [];
reg.onDidChange((e) => events.push(e));
const uri = { toString: () => "file:///a.md" } as any;
reg.enter(uri);
reg.enter(uri);
expect(events).toHaveLength(1);
});
it("exit is idempotent (no duplicate events on double exit)", () => {
const reg = new CoeditingRegistry(memento() as any);
const events: unknown[] = [];
reg.onDidChange((e) => events.push(e));
const uri = { toString: () => "file:///a.md" } as any;
reg.enter(uri);
reg.exit(uri);
reg.exit(uri);
expect(events).toHaveLength(2); // enter, then exit (second exit fires nothing)
});
it("syncContext: active editor with coediting doc sets isCoediting=true", () => {
const reg = new CoeditingRegistry(memento() as any);
const uri = { toString: () => "file:///a.md" } as any;
reg.enter(uri);
const mockExecuteCommand = vscode.commands.executeCommand as any;
mockExecuteCommand.mockClear();
const editor = { document: { uri } } as any;
reg.syncContext(editor);
expect(mockExecuteCommand).toHaveBeenCalledWith("setContext", "cowriting.isCoediting", true);
});
it("syncContext: undefined editor sets isCoediting=false", () => {
const reg = new CoeditingRegistry(memento() as any);
const mockExecuteCommand = vscode.commands.executeCommand as any;
mockExecuteCommand.mockClear();
reg.syncContext(undefined);
expect(mockExecuteCommand).toHaveBeenCalledWith("setContext", "cowriting.isCoediting", false);
});
it("syncContext: non-coediting doc sets isCoediting=false", () => {
const reg = new CoeditingRegistry(memento() as any);
const mockExecuteCommand = vscode.commands.executeCommand as any;
mockExecuteCommand.mockClear();
const uri = { toString: () => "file:///nonexistent.md" } as any;
const editor = { document: { uri } } as any;
reg.syncContext(editor);
expect(mockExecuteCommand).toHaveBeenCalledWith("setContext", "cowriting.isCoediting", false);
});
it("syncContext: baselineMode defaults to empty string when not coediting", () => {
const reg = new CoeditingRegistry(memento() as any);
const mockExecuteCommand = vscode.commands.executeCommand as any;
mockExecuteCommand.mockClear();
const uri = { toString: () => "file:///a.md" } as any;
const editor = { document: { uri } } as any;
reg.syncContext(editor);
expect(mockExecuteCommand).toHaveBeenCalledWith("setContext", "cowriting.baselineMode", "");
});
it("syncContext: baselineMode uses hook result when coediting and hook is set", () => {
const reg = new CoeditingRegistry(memento() as any);
const uri = { toString: () => "file:///a.md" } as any;
reg.enter(uri);
reg.baselineModeOf = vi.fn((): "head" | "snapshot" | undefined => "snapshot");
const mockExecuteCommand = vscode.commands.executeCommand as any;
mockExecuteCommand.mockClear();
const editor = { document: { uri } } as any;
reg.syncContext(editor);
expect(mockExecuteCommand).toHaveBeenCalledWith("setContext", "cowriting.baselineMode", "snapshot");
});
it("syncContext: baselineMode defaults to empty string when hook returns undefined", () => {
const reg = new CoeditingRegistry(memento() as any);
const uri = { toString: () => "file:///a.md" } as any;
reg.enter(uri);
reg.baselineModeOf = vi.fn((): "head" | "snapshot" | undefined => undefined);
const mockExecuteCommand = vscode.commands.executeCommand as any;
mockExecuteCommand.mockClear();
const editor = { document: { uri } } as any;
reg.syncContext(editor);
expect(mockExecuteCommand).toHaveBeenCalledWith("setContext", "cowriting.baselineMode", "");
});
it("syncContext: calls executeCommand twice (isCoediting and baselineMode)", () => {
const reg = new CoeditingRegistry(memento() as any);
const uri = { toString: () => "file:///a.md" } as any;
reg.enter(uri);
const mockExecuteCommand = vscode.commands.executeCommand as any;
mockExecuteCommand.mockClear();
const editor = { document: { uri } } as any;
reg.syncContext(editor);
expect(mockExecuteCommand).toHaveBeenCalledTimes(2);
});
});
+27
View File
@@ -0,0 +1,27 @@
import { describe, test, expect } from "vitest";
import { decorationPlan } from "../src/trackChangesModel";
describe("decorationPlan (INV-49)", () => {
test("derives insertion ranges + deletion hints from original→replacement", () => {
// anchorStart 10; original 'the brown fox' → 'the red fox'
const plan = decorationPlan(10, "the brown fox", "the red fox");
// one changed run: 'brown ' → 'red ' (word-level); insertion 'red ' tinted,
// deletion hint 'brown ' shown at the run start.
expect(plan.insertions.length).toBeGreaterThan(0);
expect(plan.deletions.length).toBeGreaterThan(0);
// insertion offsets are in buffer coords (>= anchorStart) and lie within the applied text
for (const ins of plan.insertions) {
expect(ins.start).toBeGreaterThanOrEqual(10);
expect(ins.end).toBeLessThanOrEqual(10 + "the red fox".length);
}
// a deletion hint carries the struck original text at a buffer offset
expect(plan.deletions[0].text).toContain("brown");
});
test("pure insertion (no deletion) yields an insertion range and no deletion hint", () => {
// 'hello' → 'hello world': ' world' is a pure word-level insertion (no removed tokens)
const plan = decorationPlan(0, "hello", "hello world");
expect(plan.insertions.length).toBe(1);
expect(plan.deletions.length).toBe(0);
});
});
+12 -2
View File
@@ -13,11 +13,20 @@ async function main(): Promise<void> {
const workspace = fs.mkdtempSync(path.join(os.tmpdir(), "cowriting-e2e-"));
fs.cpSync(fixture, workspace, { recursive: true });
// VS Code derives its IPC socket path from --user-data-dir. The default
// (`<projectRoot>/.vscode-test/user-data`) plus `/<version>-main.sock` can blow
// past macOS's ~103-char UNIX-socket limit (`listen EINVAL`) when the project
// lives at a long path (e.g. a git worktree). Pin the user-data dir to a SHORT
// root under /tmp so the socket path stays well under the limit. (os.tmpdir() on
// macOS is itself a long /var/folders/… path, so we use /tmp directly.)
const userDataRoot = process.platform === "win32" ? os.tmpdir() : "/tmp";
const userDataDir = fs.mkdtempSync(path.join(userDataRoot, "cwud-"));
try {
await runTests({
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [workspace, "--disable-extensions"],
launchArgs: [workspace, "--disable-extensions", "--user-data-dir", userDataDir],
extensionTestsEnv: { E2E_WORKSPACE: workspace },
});
@@ -26,10 +35,11 @@ async function main(): Promise<void> {
await runTests({
extensionDevelopmentPath,
extensionTestsPath: path.resolve(__dirname, "./suite-no-workspace/index"),
launchArgs: ["--disable-extensions"],
launchArgs: ["--disable-extensions", "--user-data-dir", userDataDir],
});
} finally {
fs.rmSync(workspace, { recursive: true, force: true });
fs.rmSync(userDataDir, { recursive: true, force: true });
}
}
+77
View File
@@ -0,0 +1,77 @@
import * as fs from "node:fs";
import * as path from "node:path";
import * as vscode from "vscode";
import type { CowritingApi } from "../../../src/extension";
import { settleUntil } from "../suite/helpers";
/**
* Task 9 (spec §7.1 rung 3 the real @cline/sdk, manual gate). Drives the
* SAME comments-first ask -> reply -> offer -> proposal loop as
* test/e2e/suite/commentLoop.test.ts / fullLoop.test.ts, but with BOTH turns
* left UNSTUBBED: ThreadController's reply loop and EditFlow's edit turn hit
* the real local `claude` login (INV-8 -- the extension itself holds no
* credentials). Not part of `npm run test:e2e` / CI -- driven only by
* `scripts/smoke-native-loop.mjs` (`npm run smoke:native`), mirroring
* `scripts/smoke-live-turn.mjs`'s "manual gate, real SDK" role for F3.
*
* There is no VS Code API to drive the native comment-widget's OWN submit
* gesture from outside the UI (see commentLoop.test.ts's "Finding 1" note) --
* this calls the exact seam a real comment submission reaches
* (ThreadController.createThreadOnSelection / .makeThreadEdit), which is the
* closest a headless script gets to "post a real comment" on a live doc.
*/
export async function run(): Promise<void> {
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin");
if (!ext) throw new Error("extension not found -- is extensionDevelopmentPath wired correctly?");
const api = (await ext.activate()) as CowritingApi;
if (!api?.threadController || !api?.editFlow || !api?.proposalController || !api?.sidecarRouter) {
throw new Error("extension did not export the expected controllers");
}
const folder = vscode.workspace.workspaceFolders?.[0];
if (!folder) throw new Error("no workspace folder -- launch with the sandbox/ folder open");
const target = path.join(folder.uri.fsPath, `.smoke-native-loop-${process.pid}.md`);
const ORIGINAL = "The quick brown fox jumps over the lazy dog paragraph.";
fs.writeFileSync(target, `# Native-loop smoke\n\n${ORIGINAL}\n`, "utf8");
try {
const uri = vscode.Uri.file(target);
const doc = await vscode.workspace.openTextDocument(uri);
const ed = await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
const docKey = api.proposalController.keyFor(doc);
const start = doc.getText().indexOf(ORIGINAL);
ed.selection = new vscode.Selection(doc.positionAt(start), doc.positionAt(start + ORIGINAL.length));
console.log("[smoke-native-loop] posting a real comment (createThreadOnSelection)...");
const threadId = await api.threadController.createThreadOnSelection(
"Rewrite this sentence to be more concise.",
);
if (!threadId) throw new Error("createThreadOnSelection refused (not coediting, or no selection)");
console.log(`[smoke-native-loop] thread created: ${threadId} -- waiting for the real @cline/sdk reply (this calls the live SDK; may take a while)...`);
await settleUntil(() => {
const t = api.sidecarRouter.load(docKey)?.threads.find((x) => x.id === threadId);
return (t?.messages.length ?? 0) >= 2 && t!.messages[1].author.kind === "agent";
}, 180000);
console.log("[smoke-native-loop] reply landed -- thread flipped to \"offer\". Making the edit (real SDK)...");
const ids = await api.threadController.makeThreadEdit(threadId, docKey);
if (ids.length === 0) throw new Error("makeThreadEdit produced no proposals (the turn returned an empty/no-op replacement)");
console.log(`[smoke-native-loop] proposal id(s) created: ${ids.join(", ")}`);
await settleUntil(() => ids.every((id) => api.proposalController.isApplied(docKey, id)), 10000);
for (const id of ids) {
const ok = await api.proposalController.finalizeInPlace(docKey, id);
console.log(`[smoke-native-loop] accepted proposal ${id}: ${ok}`);
}
console.log(`[smoke-native-loop] PASS -- ${ids.length} proposal(s) created and accepted: ${ids.join(", ")}`);
} finally {
try {
fs.rmSync(target, { force: true });
} catch {
// best-effort cleanup -- never let cleanup mask the real result
}
}
}
+22 -16
View File
@@ -28,6 +28,7 @@ suite("no-workspace authoring (F8 — real folder-less, #8 lineage)", () => {
"cowriting.reply",
"cowriting.resolveThread",
"cowriting.reopenThread",
"cowriting.edit",
"cowriting.editSelection",
"cowriting.applyAgentEdit",
"cowriting.proposeAgentEdit",
@@ -40,8 +41,11 @@ suite("no-workspace authoring (F8 — real folder-less, #8 lineage)", () => {
"cowriting.toggleAttribution",
"cowriting.acceptProposal",
"cowriting.rejectProposal",
// #34: the F6 two-pane diff VIEW was removed (F10 preview is the single
// review surface); its toggle command is gone (the baseline store stays).
"cowriting.toggleDiffView",
]) {
assert.ok(!all.includes(retired), `${retired} is retired (F10 preview-only)`);
assert.ok(!all.includes(retired), `${retired} is retired`);
}
});
@@ -73,25 +77,27 @@ suite("no-workspace authoring (F8 — real folder-less, #8 lineage)", () => {
assert.strictEqual(api.sidecarRouter.sidecarPath(key), undefined, "untitled artifact is in-memory only (no disk)");
});
// F6 (#19) is workspace-INDEPENDENT: its commands are real (not stubs) even
// with no folder open, and the toggle works on an untitled buffer.
test("F6 toggle works with no folder open (untitled buffer)", async () => {
// F6 (#19) baseline data layer is workspace-INDEPENDENT: it captures a baseline
// for an untitled buffer even with no folder open (the two-pane VIEW was
// removed in #34; only the data layer remains). markReviewed stays real.
// Native-surfaces migration (Task 2, INV-7): the baseline is established on
// coediting ENTRY, not merely on open.
test("F6 baseline data layer works with no folder open (untitled buffer)", async () => {
const all = await vscode.commands.getCommands(true);
assert.ok(all.includes("cowriting.toggleDiffView"), "toggleDiffView registered");
assert.ok(all.includes("cowriting.pinDiffBaseline"), "pinDiffBaseline registered");
assert.ok(all.includes("cowriting.markReviewed"), "markReviewed registered");
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!;
const api = (await ext.activate()) as CowritingApi;
const untitled = await vscode.workspace.openTextDocument({ content: "no-folder scratch\n", language: "markdown" });
await vscode.window.showTextDocument(untitled);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await new Promise((r) => setTimeout(r, 300));
await vscode.commands.executeCommand("cowriting.toggleDiffView");
const key = untitled.uri.toString();
const baseline = api.diffViewController.getBaseline(key);
assert.ok(baseline, "baseline captured for the untitled buffer with no folder");
assert.strictEqual(baseline!.reason, "entered");
// markReviewed resets the baseline to now — works folder-less.
await vscode.commands.executeCommand("cowriting.markReviewed");
await new Promise((r) => setTimeout(r, 300));
const opened = vscode.window.tabGroups.all.some((g) =>
g.tabs.some(
(t) =>
t.input instanceof vscode.TabInputTextDiff &&
t.input.original.scheme === "cowriting-baseline" &&
t.input.modified.toString() === untitled.uri.toString(),
),
);
assert.ok(opened, "a cowriting-baseline diff opened for the untitled buffer with no folder");
assert.strictEqual(api.diffViewController.getBaseline(key)!.reason, "pinned", "markReviewed works with no folder");
});
});
+3
View File
@@ -18,6 +18,9 @@ async function openDoc(): Promise<vscode.TextDocument> {
const uri = vscode.Uri.file(path.join(WS, DOC_REL));
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
// INV-10: attribution tracking is gated on coediting (Task 4) — idempotent
// across this order-dependent suite's repeated openDoc() calls.
await vscode.commands.executeCommand("cowriting.coeditDocument");
return doc;
}
async function getApi(): Promise<CowritingApi> {
+32 -18
View File
@@ -3,6 +3,7 @@ import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import type { CowritingApi } from "../../../src/extension";
import { renderHtmlFor } from "./helpers";
const WS = process.env.E2E_WORKSPACE!;
const settle = () => new Promise((r) => setTimeout(r, 300));
@@ -10,38 +11,40 @@ const settle = () => new Promise((r) => setTimeout(r, 300));
async function getApi(): Promise<CowritingApi> {
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!;
const api = (await ext.activate()) as CowritingApi;
assert.ok(api?.trackChangesPreviewController && api?.proposalController, "exports preview + proposal");
assert.ok(api?.attributionController && api?.proposalController, "exports attribution + proposal");
return api;
}
// F10 host E2E (no LLM): the rewrite of the obsolete F9 authorship-mode test.
// F9's "authorship" mode / renderAuthorship is gone — the on-state renderReview
// now author-colors Claude's landed prose. This suite confirms a Claude-landed
// span renders as a cw-by-claude span in the on-state preview HTML. Owns its own
// markdown doc, disjoint from the other suites' fixtures.
suite("F10 review preview — Claude-authored prose is cw-by-claude in the on-state (host E2E, no LLM)", () => {
// now author-colors Claude's PENDING proposal blocks as cw-ins-claude. This
// suite checks that render BEFORE accepting (the class to check is cw-ins-claude,
// the proposal block, NOT cw-by-claude — the old F9 authorship render) and that
// the attribution data layer survives the accept; it does not exercise the F6
// baseline (see diffView.test.ts / baselineRouter.test.ts for the post-accept
// baseline behavior, INV-7/D21). Owns its own markdown doc. Task 8: the render
// probe is now the pure `renderReview` directly (the webview that used to wrap
// it, `renderHtmlFor`, is gone).
suite("F10 review preview — pending Claude proposal renders cw-ins-claude in the on-state (host E2E, no LLM)", () => {
const DOC_REL = "docs/f10claude.md";
const TARGET = "The sentence Claude will compose over.";
const REPLACEMENT = "The sentence CLAUDE COMPOSED via the seam.";
test("an accepted Claude edit author-colors as cw-by-claude in the on-state render; mode defaults to on", async () => {
test("a pending Claude proposal carries cw-ins-claude; accepted proposal leaves attribution data intact", async () => {
const abs = path.join(WS, DOC_REL);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, `# F10\n\n${TARGET}\n`, "utf8");
const uri = vscode.Uri.file(abs);
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
// INV-10: entering coediting is required for attribution tracking + F12
// optimistic-apply to fire — this suite never entered it (pre-migration).
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
const api = await getApi();
const key = uri.toString();
// open the preview — annotations default ON (F10/INV-33)
await vscode.commands.executeCommand("cowriting.showTrackChangesPreview");
await settle();
assert.ok(api.trackChangesPreviewController.isOpen(key), "preview open");
assert.strictEqual(api.trackChangesPreviewController.getMode(key), "on", "annotations default to on");
// Claude composes via the seam (propose → accept)
// Claude proposes via the seam (F12 optimistic-apply; proposal stays PENDING)
const start = doc.getText().indexOf(TARGET);
const id = await vscode.commands.executeCommand<string>("cowriting.proposeAgentEdit", {
uri: key,
@@ -52,17 +55,28 @@ suite("F10 review preview — Claude-authored prose is cw-by-claude in the on-st
sessionId: "e2e-f10",
turnId: "turn-f10",
});
assert.ok(id, "propose returns an id");
await settle();
// PENDING proposal: the preview renders the proposal block with cw-ins-claude.
// After accepting, the baseline advances (INV-18) → the text becomes "unchanged"
// and renders plain — so we check BEFORE accepting.
const pendingHtml = renderHtmlFor(api, doc, key);
assert.ok(
pendingHtml.includes("cw-ins-claude"),
"pending Claude proposal block carries cw-ins-claude in the on-state render",
);
// accept via the seam — the proposal lands; the baseline no longer auto-advances
// (INV-18/#48 retired, spec INV-7), but that isn't asserted here (no coediting
// entry / baseline established for this doc — only the attribution data layer).
assert.ok(await api.proposalController.acceptById(DOC_REL, id!), "accept applies via the seam");
await settle();
// attribution recorded a Claude (agent) span (data layer intact)
const claudeSpan = api.attributionController.getSpans(DOC_REL).find((s) => s.authorKind === "agent");
assert.ok(claudeSpan, "Claude span recorded by F3");
assert.ok(claudeSpan, "Claude span recorded by F3 (data layer intact after accept)");
const spans = api.attributionController.spansFor(doc);
assert.ok(spans.some((s) => s.author === "claude"), "spansFor reports a Claude span for the preview");
// the on-state render author-colors the landed Claude text as cw-by-claude
const html = api.trackChangesPreviewController.renderHtmlFor(key);
assert.match(html, /<span class="cw-by-claude">/, "landed Claude prose is author-colored in the on-state render");
});
});
+87
View File
@@ -0,0 +1,87 @@
import * as assert from "node:assert";
import { execFileSync } from "node:child_process";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import * as vscode from "vscode";
import { activateApi, settle, settleUntil } from "./helpers";
const WS = process.env.E2E_WORKSPACE!;
suite("baseline router (PUC-1, INV-7/D13/D14)", () => {
test("snapshot mode: enter captures; markReviewed re-pins clean", async () => {
const api = await activateApi();
const doc = await vscode.workspace.openTextDocument({ language: "markdown", content: "one\n" });
const ed = await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
assert.strictEqual(api.diffViewController.modeOf(doc.uri.toString()), "snapshot");
assert.strictEqual(api.diffViewController.getBaseline(doc.uri.toString())?.text, "one\n");
await ed.edit((b) => b.insert(new vscode.Position(1, 0), "two\n"));
// Task 3 (INV-13): the status-bar/SCM change count tracks the live edit.
await settleUntil(() => api.scmSurfaceController.changeCount(doc.uri.toString()) === 1);
await vscode.commands.executeCommand("cowriting.markReviewed");
const b = api.diffViewController.getBaseline(doc.uri.toString());
assert.strictEqual(b?.text, "one\ntwo\n");
assert.strictEqual(b?.reason, "pinned");
// A pin re-baselines to the clean current text, so the change count resets.
await settleUntil(() => api.scmSurfaceController.changeCount(doc.uri.toString()) === 0);
});
test("head mode: baseline = HEAD; commit advances it", async function () {
this.timeout(30000);
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cw-git-"));
const git = (...a: string[]) => execFileSync("git", ["-C", dir, ...a], { encoding: "utf8" });
git("init", "-q");
fs.writeFileSync(path.join(dir, "doc.md"), "committed\n");
git("add", "doc.md");
git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "c1");
const api = await activateApi();
const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(path.join(dir, "doc.md")));
const ed = await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settleUntil(() => api.diffViewController.modeOf(doc.uri.toString()) === "head", 10000);
assert.strictEqual(api.diffViewController.getBaseline(doc.uri.toString())?.text, "committed\n");
await ed.edit((b) => b.insert(new vscode.Position(1, 0), "uncommitted\n"));
await doc.save();
git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-aqm", "c2");
await settleUntil(
() => api.diffViewController.getBaseline(doc.uri.toString())?.text === "committed\nuncommitted\n",
15000,
);
assert.strictEqual(api.diffViewController.getBaseline(doc.uri.toString())?.reason, "head");
});
// Finding 3 (final whole-branch review, session native-surfaces-exec):
// DiffViewController only establishes a baseline on the registry's ENTER
// transition (a doc it can find already open) and, at startup, for docs
// already open at that moment. A registry member entered while its document
// was NOT yet open (the real-world case: a persisted coediting set restored
// after a window reload, whose member is only lazily opened later) used to
// never get a baseline at all — reproduced here directly via
// `coeditingRegistry.enter(uri)` on a URI with no open document yet (the
// registry API doesn't require one), then a genuine later open.
test("a registry member entered before its doc was open gets its baseline established once the doc IS opened", async () => {
const api = await activateApi();
const rel = "docs/finding3-establish-on-open.md";
const abs = path.join(WS, rel);
const content = "# late open\n\nestablish-on-open should catch this.\n";
fs.writeFileSync(abs, content, "utf8");
const uri = vscode.Uri.file(abs);
// Membership without an open document — DiffViewController's enter-
// transition handler no-ops (`textDocuments.find` misses), reproducing
// the gap: a coediting member with no mode/baseline yet.
api.coeditingRegistry.enter(uri);
assert.strictEqual(api.coeditingRegistry.isCoediting(uri), true, "sanity: registry membership recorded");
assert.strictEqual(api.diffViewController.modeOf(uri.toString()), undefined, "sanity: the gap is reproduced");
// A genuine later open — the fix's `renderIfOpen`/`onDidOpenTextDocument`
// path must establish the baseline it missed.
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
await settleUntil(() => api.diffViewController.modeOf(uri.toString()) !== undefined, 10000);
assert.strictEqual(api.diffViewController.modeOf(uri.toString()), "snapshot");
assert.strictEqual(api.diffViewController.getBaseline(uri.toString())?.text, content);
});
});
+223
View File
@@ -0,0 +1,223 @@
import * as assert from "node:assert";
import * as fs from "node:fs";
import * as path from "node:path";
import MarkdownIt from "markdown-it";
import * as vscode from "vscode";
import { cowritingMarkdownItPlugin } from "../../../src/previewAnnotations";
import { activateApi, settle, settleUntil } from "./helpers";
const WS = process.env.E2E_WORKSPACE!;
async function api() {
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!;
return (await ext.activate()) as import("../../../src/extension").CowritingApi;
}
suite("coediting registry (PUC-7)", () => {
test("enter → isCoediting true; exit → false; persists in list()", async () => {
const { coeditingRegistry } = await api();
const doc = await vscode.workspace.openTextDocument({ language: "markdown", content: "# t\n\nbody\n" });
await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
assert.strictEqual(coeditingRegistry.isCoediting(doc.uri), true);
assert.ok(coeditingRegistry.list().includes(doc.uri.toString()));
await vscode.commands.executeCommand("cowriting.stopCoediting");
assert.strictEqual(coeditingRegistry.isCoediting(doc.uri), false);
});
// Task 3 (INV-10): a document that was never entered into coediting gets no
// native-diff surface at all. QuickDiff's `provideOriginalResource` isn't
// directly queryable from a host-E2E test, so this asserts the gate INPUTS
// structurally: `isCoediting` stays false and the SCM change count — which
// `ScmSurfaceController` only ever populates for a coediting doc — stays 0.
test("non-entered doc: no coediting, no change count (structural, INV-10)", async () => {
const api = await activateApi();
const doc = await vscode.workspace.openTextDocument({ language: "markdown", content: "untouched\n" });
await vscode.window.showTextDocument(doc);
await settle();
assert.strictEqual(api.coeditingRegistry.isCoediting(doc.uri), false);
assert.strictEqual(api.scmSurfaceController.changeCount(doc.uri.toString()), 0);
});
// Task 4 (INV-10, PUC-7): the no-hijack scenario — a non-entered doc gets no
// thread-creation surface at all; entering unlocks it; exiting hides the
// rendered thread (not the sidecar-backed data); re-entering restores it.
test("non-entered doc gets no surfaces; exit detaches; re-enter restores threads", async () => {
const api = await activateApi();
// Task 6 (D10): createThreadOnSelection now also fires the respond-in-
// thread turn once the doc is coediting — stub it off (not under test
// here; this suite asserts render/hide/restore, not the reply loop).
api.threadController.setTurnRunnerForTest(async () => {
throw new Error("coediting suite: reply-loop turn stubbed off");
});
const doc = await vscode.workspace.openTextDocument({ language: "markdown", content: "# a\n\npara\n" });
const ed = await vscode.window.showTextDocument(doc);
// not entered → no thread creation
ed.selection = new vscode.Selection(2, 0, 2, 4);
const before = await api.threadController.createThreadOnSelection("hi");
assert.strictEqual(before, undefined);
// entered → works
await vscode.commands.executeCommand("cowriting.coeditDocument");
const id = await api.threadController.createThreadOnSelection("hi");
assert.ok(id);
// exit → rendered thread set empty; re-enter → restored from sidecar
await vscode.commands.executeCommand("cowriting.stopCoediting");
assert.strictEqual(api.threadController.getRendered(api.proposalController.keyFor(doc)).length, 0);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
assert.strictEqual(api.threadController.getRendered(api.proposalController.keyFor(doc)).length, 1);
});
// PUC-7 restore-on-enter gap: unlike ThreadController (Task 4, above),
// ProposalController.renderAll and AttributionController.loadAll were never
// wired to registry.onDidChange — so a document's FIRST enter into
// coediting with pre-existing sidecar proposals/attribution showed threads
// but not proposal decorations/CodeLens or committed author-coloring until
// a later edit happened to fire renderAll/loadAll. A same-session
// exit-then-re-enter doesn't reproduce this on its own (nothing clears the
// in-memory render caches on exit), so this test wipes those caches after
// exit — simulating "never rendered in this session yet" — then re-enters
// and asserts BOTH surfaces restore via the registry.onDidChange path
// alone, with no subsequent edit.
test("re-enter after exit restores proposal + attribution surfaces without an edit (PUC-7)", async () => {
const api = await activateApi();
// A workspace FILE (not untitled) — attribution only persists to the
// sidecar on save, and this test needs the sidecar (not just in-memory
// live state) to hold the seeded data before re-entering.
const rel = "docs/puc7-restore.md";
const abs = path.join(WS, rel);
fs.writeFileSync(abs, "# t\n\nfoo bar baz\n", "utf8");
const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(abs));
await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
// Seed a pending proposal (F4) + a committed agent-attributed edit (F3) —
// the two surfaces the gap left un-restored.
const target = "bar";
const start = doc.getText().indexOf(target);
const proposalId = await vscode.commands.executeCommand<string>("cowriting.proposeAgentEdit", {
uri: doc.uri.toString(),
start,
end: start + target.length,
newText: "BAR-PROPOSED",
model: "sonnet",
sessionId: "e2e-puc7",
turnId: "turn-puc7",
});
assert.ok(proposalId, "propose returns an id");
// A real text change (not a no-op replace) — "foo" precedes the "bar"
// target above, so it is unaffected by F12's optimistic-apply of the
// proposal into the buffer.
const fooStart = doc.getText().indexOf("foo");
const applied = await api.attributionController.applyAgentEdit(
doc,
new vscode.Range(doc.positionAt(fooStart), doc.positionAt(fooStart + "foo".length)),
"FOO-CLAUDE",
{ kind: "agent", id: "claude", agent: { sdk: "@cline/sdk", model: "sonnet", sessionId: "e2e-puc7" } },
{ turnId: "turn-puc7" },
);
assert.strictEqual(applied, true, "seam edit applies");
await settle();
await doc.save(); // persists attribution records to the sidecar (F3 PUC-4)
const key = api.proposalController.keyFor(doc);
assert.ok(api.proposalController.getRendered(key).length >= 1, "sanity: proposal rendered pre-exit");
assert.ok(api.attributionController.getSpans(key).length >= 1, "sanity: attribution rendered pre-exit");
await vscode.commands.executeCommand("cowriting.stopCoediting");
// Wipe the controllers' in-memory render caches for this doc — nothing in
// production does this on exit (by design: exit hides, it does not wipe
// sidecar-backed state), so this reflection reproduces the "never
// rendered this session" precondition of a genuinely fresh first-enter.
(api.proposalController as unknown as { docs: Map<string, unknown> }).docs.delete(key);
(api.attributionController as unknown as { docs: Map<string, unknown> }).docs.delete(key);
assert.strictEqual(api.proposalController.getRendered(key).length, 0, "cache cleared pre-re-enter");
assert.strictEqual(api.attributionController.getSpans(key).length, 0, "cache cleared pre-re-enter");
// Re-enter — the registry.onDidChange subscribers (PUC-7 fix) must
// restore both surfaces from the sidecar with no subsequent edit.
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settleUntil(
() => api.proposalController.getRendered(key).length >= 1 && api.attributionController.getSpans(key).length >= 1,
);
assert.strictEqual(api.proposalController.getRendered(key).length, 1, "proposal restored on re-enter");
assert.ok(api.attributionController.getSpans(key).length >= 1, "attribution restored on re-enter");
});
// Finding 1 (final whole-branch review, session native-surfaces-exec): the
// preview-annotation host's INV-10 gate must never leak one document's
// baseline/spans into another document's preview. The built-in preview's
// rendered DOM isn't queryable from a host E2E test (§6.8, mirrored from
// Task 7's own previewAnnotations.test.ts), so this drives the ACTUAL
// production `previewAnnotationHost` through a real markdown-it instance
// and asserts on the rendered HTML.
test("INV-10: a non-coedited doc's preview never inherits another doc's annotations", async () => {
const api = await activateApi();
const docA = await vscode.workspace.openTextDocument({ language: "markdown", content: "alpha original\n" });
await vscode.window.showTextDocument(docA);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
const start = docA.getText().indexOf("original");
const okA = await vscode.commands.executeCommand<boolean>("cowriting.applyAgentEdit", {
uri: docA.uri.toString(),
start,
end: start + "original".length,
newText: "REPLACED",
model: "sonnet",
sessionId: "e2e-inv10-leak",
turnId: "turn-e2e-inv10-leak",
});
assert.strictEqual(okA, true, "seam edit applies to docA");
await settle();
// docB is opened (becomes the active editor, so it would win any
// `lastCoeditedUri` fallback if the gate were wrong) but NEVER entered
// into coediting.
const docB = await vscode.workspace.openTextDocument({ language: "markdown", content: "bravo untouched\n" });
await vscode.window.showTextDocument(docB);
await settle();
const md = new MarkdownIt();
cowritingMarkdownItPlugin(md, api.previewAnnotationHost);
const html = md.render(docB.getText(), { currentDocument: docB.uri });
assert.ok(!html.includes("cw-ins-claude"), `docB must render with no annotations, got:\n${html}`);
assert.ok(!html.includes("original"), `docA's baseline text must never leak into docB's preview:\n${html}`);
assert.ok(html.includes("bravo untouched"), "docB renders its own content unannotated");
});
// Finding 1, failure mode (b): `lastCoeditedUri` is never re-validated
// against the registry, so a doc's OWN preview stayed annotated after
// `stopCoediting` — an INV-10 violation for the doc whose gate was just
// closed, not just for a different doc.
test("INV-10: stopCoediting immediately clears a doc's own preview annotations", async () => {
const api = await activateApi();
const doc = await vscode.workspace.openTextDocument({ language: "markdown", content: "gamma original\n" });
await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
const start = doc.getText().indexOf("original");
const ok = await vscode.commands.executeCommand<boolean>("cowriting.applyAgentEdit", {
uri: doc.uri.toString(),
start,
end: start + "original".length,
newText: "REPLACED",
model: "sonnet",
sessionId: "e2e-inv10-stop",
turnId: "turn-e2e-inv10-stop",
});
assert.strictEqual(ok, true, "seam edit applies");
await settle();
const md = new MarkdownIt();
cowritingMarkdownItPlugin(md, api.previewAnnotationHost);
const before = md.render(doc.getText(), { currentDocument: doc.uri });
assert.ok(before.includes("cw-ins-claude"), `sanity: annotated while coediting, got:\n${before}`);
await vscode.commands.executeCommand("cowriting.stopCoediting");
await settle();
const after = md.render(doc.getText(), { currentDocument: doc.uri });
assert.ok(!after.includes("cw-ins-claude"), `expected a clean render after stopCoediting, got:\n${after}`);
});
});
+117
View File
@@ -0,0 +1,117 @@
import * as assert from "assert";
import * as vscode from "vscode";
import { activateApi, settleUntil } from "./helpers";
// Task 6 (D19/D10/D8, PUC-8): the comments-first ask + comment→reply→offer→
// proposal loop, with a stubbed reply turn (no real @cline/sdk call). Runs on
// an untitled buffer (F8 routes it to the global sidecar) so this suite has no
// workspace-folder dependency.
suite("comment → reply → offer → proposal (PUC-8, D10/D19)", () => {
test("reply on a coedited doc summons a machine reply + offer; accept lands pending proposals", async () => {
const api = await activateApi();
const doc = await vscode.workspace.openTextDocument({
language: "markdown",
content: "# T\n\nThe quick brown fox jumps over the lazy dog paragraph.\n",
});
const ed = await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
api.threadController.setTurnRunnerForTest(async () => ({
replacement: "I would tighten this sentence.",
model: "stub",
sessionId: "s1",
}));
api.editFlow.setEditTurnForTest(async () => ({
replacement: "The quick fox jumps the lazy dog.",
model: "stub",
sessionId: "s1",
}));
ed.selection = new vscode.Selection(2, 0, 2, 20);
const threadId = (await api.threadController.createThreadOnSelection("tighten this"))!;
// reply-loop fires on the human message; wait for the machine message to persist
await settleUntil(() => {
const t = api.sidecarRouter.load(api.proposalController.keyFor(doc))?.threads.find((x) => x.id === threadId);
return (t?.messages.length ?? 0) >= 2 && t!.messages[1].author.kind === "agent";
}, 10000);
// Finding 2 (final whole-branch review): the machine reply flips the
// thread into an "offer" — this used to CLOBBER the "open" status token
// instead of folding in alongside it, so Resolve/Reopen disappeared from
// the menu the moment any thread got a reply. Assert both tokens survive
// together, directly via the rendered vsThread's contextValue (the exact
// string package.json's `comments/commentThread/title` `when`-clauses key
// on) — no DOM/UI query needed.
const docKey = api.proposalController.keyFor(doc);
const afterReply = api.threadController.getRendered(docKey).find((t) => t.id === threadId);
assert.ok(afterReply, "thread is rendered after the reply lands");
assert.match(
afterReply!.contextValue,
/\bopen\b/,
`expected the "open" status token to survive the offer transition, got "${afterReply!.contextValue}"`,
);
assert.match(
afterReply!.contextValue,
/\boffer\b/,
`expected the "offer" token once the machine reply lands, got "${afterReply!.contextValue}"`,
);
const ids = await api.threadController.makeThreadEdit(threadId, api.proposalController.keyFor(doc));
assert.ok(ids.length >= 1);
assert.ok(api.proposalController.listProposals(doc).length >= 1); // pending, INV-5
// Spending the offer flips it to "offerdone" — status token still intact,
// and the plain "offer" token must not still match (it's a DIFFERENT word,
// not just a substring: /\boffer\b/ must not match "offerdone").
const afterMakeEdit = api.threadController.getRendered(docKey).find((t) => t.id === threadId);
assert.match(afterMakeEdit!.contextValue, /\bopen\b/, "status token still intact after spending the offer");
assert.match(afterMakeEdit!.contextValue, /\bofferdone\b/, "offer token flips to offerdone once spent");
assert.doesNotMatch(afterMakeEdit!.contextValue, /\boffer\b(?!done)/, "the bare offer token is gone once spent");
});
test("a comment on a NON-coedited doc summons nothing (INV-10)", async () => {
const api = await activateApi();
const doc = await vscode.workspace.openTextDocument({ language: "markdown", content: "plain\n" });
await vscode.window.showTextDocument(doc);
const id = await api.threadController.createThreadOnSelection("hello");
assert.strictEqual(id, undefined); // gate refuses thread creation entirely
});
// Finding 1 (code review, session native-surfaces-exec): `cowriting.edit` with NO
// selection used to route through `cowriting.editDocument` → the removed
// `askEditInstruction` webview prompt — a rejecting stub `void`'d before any
// try/catch, i.e. a completely silent dead end (no toast, no thread). It must now
// reach ThreadController.askClaude()'s top-anchored whole-document path (D19) —
// the same comments-first ask the selection route already used. There is no API to
// drive VS Code's native comment-widget submission from a host E2E test (the actual
// comment→reply→offer→proposal round trip is exercised above via the
// createThreadOnSelection seam), so this asserts the routing itself: the gesture
// reaches ThreadController.askClaude (never the old broken stub) and does not throw.
test("cowriting.edit with no selection on a coedited doc reaches the top-anchored ask path, not a silent dead end", async () => {
const api = await activateApi();
const doc = await vscode.workspace.openTextDocument({
language: "markdown",
content: "# T\n\nA document-level ask with no selection.\n",
});
const ed = await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
// Defensive stubs, mirroring the suite's other test — no real turn should fire
// from this gesture alone (no comment text is ever submitted), but guard against
// a real @cline/sdk call if the routing regresses.
api.threadController.setTurnRunnerForTest(async () => ({ replacement: "stub", model: "stub", sessionId: "s1" }));
api.editFlow.setEditTurnForTest(async () => ({ replacement: "stub", model: "stub", sessionId: "s1" }));
ed.selection = new vscode.Selection(0, 0, 0, 0); // no selection
let askClaudeCalls = 0;
const origAskClaude = api.threadController.askClaude.bind(api.threadController);
api.threadController.askClaude = async () => {
askClaudeCalls++;
return origAskClaude();
};
try {
await vscode.commands.executeCommand("cowriting.edit");
} finally {
api.threadController.askClaude = origAskClaude;
}
assert.strictEqual(
askClaudeCalls,
1,
"cowriting.edit (no selection) reached ThreadController.askClaude — not the removed editDocument prompt stub",
);
});
});
+9
View File
@@ -17,6 +17,8 @@ async function open(docRel: string): Promise<vscode.TextDocument> {
const uri = vscode.Uri.file(path.join(WS, docRel));
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
// INV-10: thread creation/rendering is gated on coediting (Task 4).
await vscode.commands.executeCommand("cowriting.coeditDocument");
return doc;
}
async function getApi(): Promise<CowritingApi> {
@@ -55,6 +57,13 @@ suite("F5 cross-rung round-trip (host E2E)", () => {
const DOC = "docs/crossrung.md";
const doc = await open(DOC);
const api = await getApi();
// Task 6 (D10): createThreadOnSelection also fires the respond-in-thread
// turn now — stub it off so it can't race the forge stand-in's reply
// below (this test asserts an EXACT 2-message sequence: editor note +
// forge reply).
api.threadController.setTurnRunnerForTest(async () => {
throw new Error("crossrung suite: reply-loop turn stubbed off");
});
const target = "portable record";
const start = doc.getText().indexOf(target);
const editor = vscode.window.activeTextEditor!;
+55 -73
View File
@@ -15,6 +15,16 @@ async function openDoc(): Promise<vscode.TextDocument> {
await vscode.window.showTextDocument(doc);
return doc;
}
/** Open the doc AND enter coediting (INV-10) baselines are established only
* on entry (Task 2). Idempotent across tests: re-entering an already-coedited
* doc is a no-op, so the once-captured baseline survives (order-dependent
* suite, same as before). */
async function openAndCoedit(): Promise<vscode.TextDocument> {
const doc = await openDoc();
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
return doc;
}
async function getApi(): Promise<CowritingApi> {
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!;
const api = (await ext.activate()) as CowritingApi;
@@ -23,40 +33,35 @@ async function getApi(): Promise<CowritingApi> {
}
const settle = () => new Promise((r) => setTimeout(r, 300));
// Order-dependent (F2F4 pattern): later tests consume earlier state. Owns
// docs/diffview.md exclusively. F6 works on ANY file (#19), so the last two
// tests use an out-of-workspace file and an untitled buffer.
suite("F6 diff-view toggle (host E2E — any file, programmatic seam ingress, no LLM)", () => {
// The F6 two-pane vscode.diff VIEW was removed in #34 (F10's rendered preview is
// the single review surface); only the baseline DATA layer survives, consumed by
// F7/F10. This suite covers that data layer. Order-dependent (F2F4 pattern):
// later tests consume earlier state. Owns docs/diffview.md exclusively. The
// baseline works on ANY file (#19), so the last two tests use an out-of-workspace
// file and an untitled buffer.
//
// Native-surfaces migration (Task 2, spec §6.4/INV-7): a baseline is now
// established only once a document ENTERS coediting (not merely opened), and
// the fixture workspace is NOT a git repo, so every doc here resolves to
// SNAPSHOT mode. The shipped machine-landing baseline advance (#48/INV-18) is
// retired — an accepted proposal now stays a visible change-since-baseline
// until "Mark Changes as Reviewed" (D21).
suite("F6 baseline data layer (host E2E — any file, programmatic seam ingress, no LLM)", () => {
const TARGET = "A target sentence Claude will rewrite via the seam.";
const REPLACEMENT = "A SENTENCE CLAUDE REWROTE via the seam.";
test("opening a tracked doc captures an `opened` baseline equal to the buffer (INV-18)", async () => {
const doc = await openDoc();
await getApi();
test("entering coediting on a snapshot-mode doc captures an `entered` baseline equal to the buffer (INV-7)", async () => {
const doc = await openAndCoedit();
const api = await getApi();
assert.strictEqual(api.diffViewController.modeOf(docUri()), "snapshot", "no git repo → snapshot mode");
const baseline = api.diffViewController.getBaseline(docUri());
assert.ok(baseline, "baseline captured on first sight");
assert.strictEqual(baseline!.reason, "opened");
assert.strictEqual(baseline!.text, doc.getText(), "baseline = open-time buffer");
});
test("toggle opens a diff tab (original scheme cowriting-baseline) over the live doc (PUC-1)", async () => {
const api = await getApi();
assert.strictEqual(api.diffViewController.isDiffOpen(docUri()), false, "no diff open yet");
await vscode.commands.executeCommand("cowriting.toggleDiffView");
await settle();
assert.strictEqual(api.diffViewController.isDiffOpen(docUri()), true, "diff tab open");
const active = vscode.window.tabGroups.activeTabGroup.activeTab;
assert.ok(active && active.input instanceof vscode.TabInputTextDiff, "active tab is a diff");
assert.strictEqual(
(active!.input as vscode.TabInputTextDiff).original.scheme,
"cowriting-baseline",
"left side served by the baseline provider",
);
assert.ok(baseline, "baseline captured on coediting entry");
assert.strictEqual(baseline!.reason, "entered");
assert.strictEqual(baseline!.text, doc.getText(), "baseline = entry-time buffer");
});
test("typing leaves the baseline unchanged while the buffer diverges", async () => {
const doc = await openDoc();
const doc = await openAndCoedit();
const api = await getApi();
const before = api.diffViewController.getBaseline(docUri())!.text;
const edit = new vscode.WorkspaceEdit();
@@ -67,9 +72,10 @@ suite("F6 diff-view toggle (host E2E — any file, programmatic seam ingress, no
assert.notStrictEqual(doc.getText(), before, "buffer diverged");
});
test("accepting a proposal advances the baseline past the landed text (PUC-2, INV-18)", async () => {
const doc = await openDoc();
test("accepting a proposal does NOT advance the baseline the landed text stays a change (INV-7/D21, #48 retired)", async () => {
const doc = await openAndCoedit();
const api = await getApi();
const before = api.diffViewController.getBaseline(docUri())!;
const start = doc.getText().indexOf(TARGET);
assert.ok(start >= 0, "fixture contains the target");
const id = await vscode.commands.executeCommand<string>("cowriting.proposeAgentEdit", {
@@ -85,14 +91,14 @@ suite("F6 diff-view toggle (host E2E — any file, programmatic seam ingress, no
assert.ok(await api.proposalController.acceptById(DOC_REL, id!), "accept applies via the seam");
await settle();
const baseline = api.diffViewController.getBaseline(docUri())!;
assert.strictEqual(baseline.reason, "machine-landing", "baseline advanced on the landing");
assert.ok(baseline.text.includes(REPLACEMENT), "landed text is in the baseline (won't show as a change)");
assert.ok(!baseline.text.includes(TARGET), "old target gone from the baseline too");
assert.strictEqual(baseline.text, doc.getText(), "baseline == buffer right after the landing");
assert.strictEqual(baseline.reason, before.reason, "baseline reason untouched by the landing");
assert.strictEqual(baseline.text, before.text, "baseline text untouched by the landing");
assert.ok(!baseline.text.includes(REPLACEMENT), "landed text is NOT folded into the baseline");
assert.notStrictEqual(baseline.text, doc.getText(), "baseline != buffer the landing reads as a change");
});
test("an operator edit after the landing makes baseline ≠ buffer (the operator delta)", async () => {
const doc = await openDoc();
test("an operator edit after the landing keeps baseline ≠ buffer (the operator delta)", async () => {
const doc = await openAndCoedit();
const api = await getApi();
const edit = new vscode.WorkspaceEdit();
edit.insert(doc.uri, new vscode.Position(0, 0), "POST-LANDING OPERATOR LINE\n");
@@ -101,29 +107,29 @@ suite("F6 diff-view toggle (host E2E — any file, programmatic seam ingress, no
assert.notStrictEqual(
api.diffViewController.getBaseline(docUri())!.text,
doc.getText(),
"operator changes show against the advanced baseline",
"operator changes show against the untouched baseline",
);
});
test("pin resets the baseline to now: baseline == buffer, reason pinned (PUC-3)", async () => {
const doc = await openDoc();
test("markReviewed resets the baseline to now: baseline == buffer, reason pinned", async () => {
const doc = await openAndCoedit();
const api = await getApi();
await vscode.commands.executeCommand("cowriting.pinDiffBaseline");
await vscode.commands.executeCommand("cowriting.markReviewed");
await settle();
const baseline = api.diffViewController.getBaseline(docUri())!;
assert.strictEqual(baseline.reason, "pinned");
assert.strictEqual(baseline.text, doc.getText(), "pinned baseline == current buffer (diff empties)");
assert.strictEqual(baseline.text, doc.getText(), "pinned baseline == current buffer (change-marks empty)");
});
test("the baseline is persisted in GLOBAL storage, never the repo (PUC-4, INV-19)", async () => {
await openDoc();
test("the baseline is persisted in GLOBAL storage, never the repo (INV-19)", async () => {
await openAndCoedit();
const api = await getApi();
const p = api.diffViewController.baselineFilePath(docUri());
assert.ok(p, "storage-backed baseline path is available for a file: doc");
assert.ok(fs.existsSync(p!), `baseline file exists at ${p}`);
const onDisk = JSON.parse(fs.readFileSync(p!, "utf8"));
assert.strictEqual(onDisk.uri, docUri(), "baseline records the document URI");
assert.strictEqual(onDisk.reason, "pinned", "last epoch (pin) persisted");
assert.strictEqual(onDisk.reason, "pinned", "last epoch (markReviewed) persisted");
assert.strictEqual(onDisk.text, api.diffViewController.getBaseline(docUri())!.text, "on-disk == in-memory");
// INV-19: baseline lives under the extension's storage dir, not the repo.
assert.ok(!p!.includes(`${path.sep}.threads${path.sep}`), "baseline is NOT in the sidecar tree");
@@ -131,25 +137,7 @@ suite("F6 diff-view toggle (host E2E — any file, programmatic seam ingress, no
assert.ok(p!.includes(`${path.sep}baselines${path.sep}`), "baseline lives under <globalStorage>/baselines/");
});
test("toggle again closes the diff tab and reveals the normal editor (PUC-1)", async () => {
const api = await getApi();
if (!api.diffViewController.isDiffOpen(docUri())) {
await openDoc();
await vscode.commands.executeCommand("cowriting.toggleDiffView");
await settle();
}
assert.strictEqual(api.diffViewController.isDiffOpen(docUri()), true, "diff open before close");
await vscode.commands.executeCommand("cowriting.toggleDiffView");
await settle();
assert.strictEqual(api.diffViewController.isDiffOpen(docUri()), false, "diff tab closed");
assert.strictEqual(
vscode.window.activeTextEditor?.document.uri.toString(),
docUri(),
"the normal editor for the doc is active after closing the diff",
);
});
test("toggle works on a file OUTSIDE the workspace folder, persisted in global storage (#19)", async () => {
test("a baseline is captured + persisted for a file OUTSIDE the workspace folder (#19)", async () => {
const api = await getApi();
const outsideDir = fs.mkdtempSync(path.join(os.tmpdir(), "cowriting-outside-"));
const outsidePath = path.join(outsideDir, "outside.md");
@@ -157,35 +145,29 @@ suite("F6 diff-view toggle (host E2E — any file, programmatic seam ingress, no
const outsideUri = vscode.Uri.file(outsidePath);
const doc = await vscode.workspace.openTextDocument(outsideUri);
await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
// Captured on open even though it is NOT under the workspace folder.
// Captured on coediting entry even though it is NOT under the workspace folder.
const baseline = api.diffViewController.getBaseline(outsideUri.toString());
assert.ok(baseline, "baseline captured for an out-of-folder file");
assert.strictEqual(baseline!.reason, "opened");
await vscode.commands.executeCommand("cowriting.toggleDiffView");
await settle();
assert.strictEqual(api.diffViewController.isDiffOpen(outsideUri.toString()), true, "diff opens for the outside file");
assert.strictEqual(baseline!.reason, "entered");
const fp = api.diffViewController.baselineFilePath(outsideUri.toString());
assert.ok(fp && fs.existsSync(fp), "outside-file baseline persisted in global storage");
assert.ok(!fp!.startsWith(WS + path.sep), "not under the workspace folder");
// close it so it doesn't bleed into the untitled test's active-tab checks
await vscode.commands.executeCommand("cowriting.toggleDiffView");
await settle();
fs.rmSync(outsideDir, { recursive: true, force: true });
void doc;
});
test("toggle works on an UNTITLED buffer, baseline in-memory only (#19)", async () => {
test("an UNTITLED buffer gets an in-memory baseline, never persisted to disk (#19)", async () => {
const api = await getApi();
const untitled = await vscode.workspace.openTextDocument({ content: "scratch line\n", language: "markdown" });
await vscode.window.showTextDocument(untitled);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
const key = untitled.uri.toString();
assert.strictEqual(untitled.uri.scheme, "untitled", "it really is an untitled buffer");
const baseline = api.diffViewController.getBaseline(key);
assert.ok(baseline, "untitled buffer got an in-memory baseline");
assert.strictEqual(api.diffViewController.baselineFilePath(key), undefined, "untitled is NOT persisted to disk");
await vscode.commands.executeCommand("cowriting.toggleDiffView");
await settle();
assert.strictEqual(api.diffViewController.isDiffOpen(key), true, "diff opens for the untitled buffer");
});
});
-243
View File
@@ -1,243 +0,0 @@
import * as assert from "assert";
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import type { CowritingApi } from "../../../src/extension";
import { renderPlain } from "../../../src/trackChangesModel";
const WS = process.env.E2E_WORKSPACE!;
const settle = () => new Promise((r) => setTimeout(r, 400));
async function getApi(): Promise<CowritingApi> {
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!;
const api = (await ext.activate()) as CowritingApi;
assert.ok(api?.trackChangesPreviewController && api?.proposalController, "exports preview + proposal");
return api;
}
/** Create + open a fresh markdown doc under WS, returning the doc + its uri key. */
async function freshDoc(rel: string, body: string): Promise<{ doc: vscode.TextDocument; key: string }> {
const abs = path.join(WS, rel);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, body, "utf8");
const uri = vscode.Uri.file(abs);
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
await settle();
return { doc, key: uri.toString() };
}
async function propose(
doc: vscode.TextDocument,
key: string,
target: string,
newText: string,
turnId: string,
): Promise<string> {
const start = doc.getText().indexOf(target);
assert.ok(start >= 0, `fixture contains "${target}"`);
const id = await vscode.commands.executeCommand<string>("cowriting.proposeAgentEdit", {
uri: key,
start,
end: start + target.length,
newText,
model: "sonnet",
sessionId: "e2e-f10rev",
turnId,
});
assert.ok(id, "propose returns an id");
return id!;
}
// F10 host E2E (no LLM): the rendered preview is the single INTERACTIVE review
// surface. This suite owns docs/f10review.md (its main flow is order-dependent)
// plus its own disjoint fresh docs for the isolated cases (status-bar, toggle).
// The editor is zero-decoration; everything observable here is the data layer +
// the on-state renderReview HTML the panel posts.
suite("F10 interactive review (host E2E — preview is the single review surface, no LLM)", () => {
const DOC_REL = "docs/f10review.md";
const PROSE = "The original review paragraph that lives in this doc.";
const T1 = "A first claude target sentence here.";
const T2 = "A second claude target sentence here.";
test("open on a markdown doc → panel open, fresh baseline all-unchanged, mode is on (PUC-1)", async () => {
const { doc, key } = await freshDoc(DOC_REL, `# F10 review\n\n${PROSE}\n\n${T1}\n\n${T2}\n`);
const api = await getApi();
assert.strictEqual(api.trackChangesPreviewController.isOpen(key), false, "no panel yet");
await vscode.commands.executeCommand("cowriting.showTrackChangesPreview");
await settle();
assert.strictEqual(api.trackChangesPreviewController.isOpen(key), true, "panel open");
assert.strictEqual(api.trackChangesPreviewController.getMode(key), "on", "annotations default on (INV-33)");
const model = api.trackChangesPreviewController.getLastModel(key);
assert.ok(model && model.length > 0, "a model was computed");
assert.ok(model!.every((o) => o.kind === "unchanged"), "fresh baseline == buffer → every block unchanged");
void doc;
});
test("typing produces an added/changed block and a cw-by-human span in the on-state render (PUC-2)", async () => {
const { key } = await reopen(DOC_REL);
const api = await getApi();
const doc = byKey(key)!;
const edit = new vscode.WorkspaceEdit();
edit.insert(doc.uri, doc.positionAt(doc.getText().length), "\n\nA freshly typed human paragraph.\n");
assert.ok(await vscode.workspace.applyEdit(edit), "operator edit applied");
await settle();
const kinds = (api.trackChangesPreviewController.getLastModel(key) ?? []).map((o) => o.kind);
assert.ok(kinds.some((k) => k === "added" || k === "changed"), "an added/changed block after typing");
// Attribution recorded the human span (data layer), and the on-state render
// author-colors that prose as cw-by-human.
assert.ok(
api.attributionController.spansFor(doc).some((s) => s.author === "human"),
"a human span was recorded for the typed text",
);
const html = api.trackChangesPreviewController.renderHtmlFor(key);
assert.match(html, /<span class="cw-by-human">/, "typed text is author-colored human in the on-state");
});
test("propose → the preview surfaces it as a cw-proposal block with ✓/✗ actions (PUC-3)", async () => {
const { doc, key } = await reopen(DOC_REL);
const api = await getApi();
const id = await propose(doc, key, T1, "A FIRST claude REPLACEMENT sentence.", "turn-f10-1");
await settle();
const views = api.proposalController.listProposals(doc);
assert.ok(views.some((v) => v.id === id), "listProposals returns a view with the id");
const html = api.trackChangesPreviewController.renderHtmlFor(key);
assert.ok(html.includes(`data-proposal-id="${id}"`), "the preview renders the proposal block by id");
assert.match(html, /class="cw-actions"/, "the proposal block carries ✓/✗ actions");
// #31: the proposal renders INLINE at its anchor (right after T1's block), not
// trailing the whole document — so it appears BEFORE the following T2 block.
const pIdx = html.indexOf(`data-proposal-id="${id}"`);
const t2Idx = html.indexOf("second claude target");
assert.ok(t2Idx >= 0 && pIdx < t2Idx, "the proposal renders in place, before the following block (#31)");
// INV-10: proposing never touches the document.
assert.ok(doc.getText().includes(T1), "document unchanged by propose");
});
test("accept → the proposal lands, clears from the preview, and the baseline advances (PUC-4)", async () => {
const { doc, key } = await reopen(DOC_REL);
const api = await getApi();
const id = api.proposalController.listProposals(doc).find((v) => v.replaced === T1)!.id;
assert.ok(await api.proposalController.acceptById(DOC_REL, id), "accept applies via the seam");
await settle();
const replacement = "A FIRST claude REPLACEMENT sentence.";
assert.ok(doc.getText().includes(replacement), "replacement landed in the document");
assert.ok(!doc.getText().includes(T1), "original target gone");
assert.ok(!api.proposalController.listProposals(doc).some((v) => v.id === id), "proposal cleared from listProposals");
const html = api.trackChangesPreviewController.renderHtmlFor(key);
assert.ok(!html.includes(`data-proposal-id="${id}"`), "the accepted proposal block is gone from the preview");
// The baseline advanced on the landing (INV-18): the landed text is not marked.
const model = api.trackChangesPreviewController.getLastModel(key) ?? [];
const marked = model.some((o) => o.kind !== "unchanged" && o.block.raw.includes("FIRST claude REPLACEMENT"));
assert.ok(!marked, "the just-landed Claude text renders unmarked (baseline advanced)");
});
test("reject → the proposal vanishes and the document is untouched (PUC-5)", async () => {
const { doc, key } = await reopen(DOC_REL);
const api = await getApi();
const before = doc.getText();
const id2 = await propose(doc, key, T2, "A SECOND would-be replacement.", "turn-f10-2");
await settle();
assert.ok(api.proposalController.listProposals(doc).some((v) => v.id === id2), "second proposal pending");
assert.strictEqual(api.proposalController.rejectById(DOC_REL, id2), true, "reject");
await settle();
assert.ok(!api.proposalController.listProposals(doc).some((v) => v.id === id2), "rejected proposal gone");
assert.strictEqual(doc.getText(), before, "document untouched by reject");
const html = api.trackChangesPreviewController.renderHtmlFor(key);
assert.ok(!html.includes(`data-proposal-id="${id2}"`), "no rejected block in the preview");
});
test("toggle annotations off → mode round-trips and the off-state render is plain (no cw- marks) (INV-33)", async () => {
// A fresh doc with a pending proposal so the on-state DOES carry a cw- mark,
// making the off-state's absence of marks meaningful (not tautological).
const { doc, key } = await freshDoc(
"docs/f10toggle.md",
"# F10 toggle\n\nA toggle target sentence to propose over.\n",
);
const api = await getApi();
await vscode.commands.executeCommand("cowriting.showTrackChangesPreview");
await settle();
const id = await propose(doc, key, "A toggle target sentence to propose over.", "A TOGGLED replacement.", "turn-tog");
await settle();
// on-state: the proposal block + its actions are present.
assert.strictEqual(api.trackChangesPreviewController.getMode(key), "on", "starts on");
const onHtml = api.trackChangesPreviewController.renderHtmlFor(key);
assert.ok(onHtml.includes(`data-proposal-id="${id}"`), "on-state shows the proposal block");
// toggle off → mode round-trips; the off-state body is plain markdown.
api.trackChangesPreviewController.setMode(key, "off");
await settle();
assert.strictEqual(api.trackChangesPreviewController.getMode(key), "off", "mode flipped to off");
assert.ok(api.trackChangesPreviewController.isOpen(key), "panel stays open across the toggle");
// renderHtmlFor is the on-state seam; the off-state body is renderPlain(current)
// (INV-33). Assert the actual off-state body the controller posts has no cw-
// author/proposal marks — meaningful because the on-state above DID carry one.
const offBody = renderPlain(doc.getText());
assert.ok(!/cw-proposal|cw-by-claude|cw-by-human|cw-del/.test(offBody), "off-state render carries no cw- marks");
// toggle back on → marks return.
api.trackChangesPreviewController.setMode(key, "on");
await settle();
assert.strictEqual(api.trackChangesPreviewController.getMode(key), "on", "mode flipped back on");
assert.ok(
api.trackChangesPreviewController.renderHtmlFor(key).includes(`data-proposal-id="${id}"`),
"on-state shows the proposal block again",
);
});
test("status-bar (PUC-6): a pending proposal with NO panel shows the indicator; opening the preview hides it", async () => {
// Isolated fresh doc: no preview opened, so the off-panel indicator is live.
const { doc, key } = await freshDoc("docs/f10status.md", "# F10 status\n\nA status target sentence here.\n");
const api = await getApi();
assert.strictEqual(api.trackChangesPreviewController.isOpen(key), false, "no panel for this doc");
await propose(doc, key, "A status target sentence here.", "A STATUS replacement.", "turn-stat");
await settle();
const text = api.trackChangesPreviewController.statusText();
assert.ok(text && text.length > 0, "the off-panel indicator shows a non-empty status");
assert.match(text!, /1 Claude proposal/, "it mentions the pending count");
// open the preview for this doc → the off-panel indicator hides (undefined).
await vscode.commands.executeCommand("cowriting.showTrackChangesPreview");
await settle();
assert.strictEqual(api.trackChangesPreviewController.isOpen(key), true, "panel open");
assert.strictEqual(api.trackChangesPreviewController.statusText(), undefined, "indicator hidden once the panel is open");
});
test("clean editor: data layers intact AND the retired in-editor surfaces are gone (INV-32)", async () => {
const { doc, key } = await freshDoc("docs/f10clean.md", "# F10 clean\n\nA clean target sentence here.\n");
const api = await getApi();
const id = await propose(doc, key, "A clean target sentence here.", "A CLAUDE clean replacement.", "turn-clean");
await settle();
assert.ok(await api.proposalController.acceptById("docs/f10clean.md", id), "accept lands the Claude edit");
await settle();
// data layer intact: a Claude (agent) attribution span exists.
assert.ok(
api.attributionController.getSpans("docs/f10clean.md").some((s) => s.authorKind === "agent"),
"agent span recorded (attribution data layer intact)",
);
// the retired toggle is gone from the palette (no editor decorations — INV-32).
const all = await vscode.commands.getCommands(true);
assert.ok(!all.includes("cowriting.toggleAttribution"), "cowriting.toggleAttribution is retired");
// the F6 diff toggle's keybinding is hidden (when:false) — it is not a user surface.
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "../../../../package.json"), "utf8"));
const dKb = (pkg.contributes.keybindings as Array<{ command: string; key: string; when?: string }>).find(
(k) => k.command === "cowriting.toggleDiffView" && k.key === "ctrl+alt+d",
);
assert.ok(dKb, "the ctrl+alt+d toggleDiffView keybinding is declared");
assert.strictEqual(dKb!.when, "false", "…but hidden (when:false) — F6 is a data layer, not a user surface");
void key;
});
});
// ---- helpers reused across the order-dependent main-flow tests ----
function byKey(key: string): vscode.TextDocument | undefined {
return vscode.workspace.textDocuments.find((d) => d.uri.toString() === key);
}
async function reopen(rel: string): Promise<{ doc: vscode.TextDocument; key: string }> {
const uri = vscode.Uri.file(path.join(WS, rel));
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
await settle();
return { doc, key: uri.toString() };
}
+173
View File
@@ -0,0 +1,173 @@
import * as assert from "assert";
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import type { CowritingApi } from "../../../src/extension";
const WS = process.env.E2E_WORKSPACE!;
const settle = () => new Promise((r) => setTimeout(r, 400));
async function getApi(): Promise<CowritingApi> {
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!;
const api = (await ext.activate()) as CowritingApi;
assert.ok(api?.editFlow && api?.diffViewController, "exports editFlow + diffView");
return api;
}
/** Create + open a fresh markdown doc under WS, returning the doc + its uri key.
* Also enters coediting (INV-10/Task 2): the baseline is established only on
* entry, not merely on open. */
async function freshDoc(rel: string, body: string): Promise<{ doc: vscode.TextDocument; key: string }> {
const abs = path.join(WS, rel);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, body, "utf8");
const uri = vscode.Uri.file(abs);
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
return { doc, key: uri.toString() };
}
// F11/F12 host E2E (no LLM): EditFlow.runEditAndPropose is the turn→proposal(s)
// cut shared by every Ask-Claude entry point (comments-first ask, editDocument,
// editSelection). Originally exercised via the now-sunset review webview's
// toolbar (Task 8 deleted that webview and its message-routing seam,
// `receiveMessage`/`pinBaseline` webview-message coverage duplicates
// diffView.test.ts / baselineRouter.test.ts's `markReviewed` tests) — these
// tests drive `runEditAndPropose` directly, the same programmatic seam the
// deleted toolbar itself called into.
suite("F11/F12 — EditFlow document/selection edit turns (host E2E, no LLM)", () => {
// SLICE-1 reachability: the orphaned pin command gets a real palette `when`.
// Renamed cowriting.pinDiffBaseline → cowriting.markReviewed (Task 2, D14).
test("markReviewed is reachable from the command palette (when: editorLangId == markdown)", async () => {
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "../../../../package.json"), "utf8"));
const entry = (pkg.contributes.menus.commandPalette as Array<{ command: string; when?: string }>).find(
(m) => m.command === "cowriting.markReviewed",
);
assert.ok(entry, "markReviewed has a commandPalette entry");
assert.notStrictEqual(entry!.when, "false", "it is no longer hidden (when:false)");
assert.match(entry!.when ?? "", /editorLangId == markdown/, "guarded on markdown");
});
// #47 (was INV-37): Edit Document now cuts at BLOCK granularity — two word
// changes in ONE paragraph are ONE block proposal (INV-39 supersedes INV-37's
// per-word cut). Full block coverage lives in f12Review.test.ts.
test("runEditAndPropose(document) — two word edits in one paragraph → ONE block proposal (PUC-4, INV-39)", async () => {
const { doc, key } = await freshDoc(
"docs/f11doc.md",
"# F11 doc\n\nThe quick brown fox jumps over the lazy dog.\n",
);
const api = await getApi();
const editFlow = api.editFlow;
// Stub the host edit turn (no LLM in CI): rewrite two distinct words.
editFlow.setEditTurnForTest(async () => ({
replacement: "# F11 doc\n\nThe quick RED fox jumps over the lazy CAT.\n",
model: "sonnet",
sessionId: "e2e-f11-doc",
}));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "swap brown→RED and dog→CAT");
await settle();
assert.strictEqual(ids.length, 1, "two changed words in one block → ONE block proposal (INV-39)");
const views = api.proposalController.listProposals(doc);
const view = views.find((v) => v.id === ids[0]);
assert.ok(view, "the returned proposal id is a live pending proposal");
assert.strictEqual(
view!.replacement,
"The quick RED fox jumps over the lazy CAT.",
"the block proposal carries the whole rewritten paragraph",
);
// F12 (INV-48): EditorProposalController optimistically applies the proposed
// text, so after settle the buffer has the replacement. The proposal is still
// pending (not finalized) until Accept.
assert.ok(doc.getText().includes("RED fox") && doc.getText().includes("lazy CAT"), "F12 optimistic-apply: proposed text in buffer");
void key;
});
// SLICE-4: Edit Selection → one single-range proposal over the resolved block-union.
test("runEditAndPropose(range) with a stubbed turn → exactly one proposal over the resolved range (PUC-3, INV-37)", async () => {
const body = "# F11 sel\n\nThe target paragraph Claude will rewrite.\n\nAnother untouched paragraph.\n";
const { doc, key } = await freshDoc("docs/f11sel.md", body);
const api = await getApi();
const editFlow = api.editFlow;
const target = "The target paragraph Claude will rewrite.";
const start = doc.getText().indexOf(target);
const end = start + target.length;
editFlow.setEditTurnForTest(async (_instruction, text) => {
assert.strictEqual(text, target, "the turn receives exactly the selected source range");
return { replacement: "The REWRITTEN paragraph from Claude.", model: "sonnet", sessionId: "e2e-f11-sel" };
});
const ids = await editFlow.runEditAndPropose(doc, { kind: "range", start, end }, "rewrite this paragraph");
await settle();
assert.strictEqual(ids.length, 1, "a selection yields exactly one proposal");
const views = api.proposalController.listProposals(doc);
const view = views.find((v) => v.id === ids[0]);
assert.ok(view, "the proposal is live");
assert.strictEqual(view!.replacement, "The REWRITTEN paragraph from Claude.", "carries the turn replacement");
assert.strictEqual(view!.replaced, target, "replaces exactly the selected range");
// F12 (INV-48): the EditorProposalController optimistically applies, so the buffer
// now has the replacement. `view.replaced` still records the original target text.
assert.ok(doc.getText().includes("The REWRITTEN paragraph from Claude."), "F12 optimistic-apply: proposed text in buffer");
void key;
});
// SLICE-4: a no-op turn (Claude returns the input unchanged) produces no proposal.
test("runEditAndPropose(range) where Claude returns the selection unchanged → no proposal", async () => {
const { doc } = await freshDoc("docs/f11noop.md", "# noop\n\nLeave me exactly as I am.\n");
const api = await getApi();
const editFlow = api.editFlow;
const target = "Leave me exactly as I am.";
const start = doc.getText().indexOf(target);
editFlow.setEditTurnForTest(async (_i, text) => ({ replacement: text, model: "sonnet", sessionId: "e2e-noop" }));
const ids = await editFlow.runEditAndPropose(doc, { kind: "range", start, end: start + target.length }, "no change");
assert.strictEqual(ids.length, 0, "an unchanged replacement proposes nothing");
});
// SLICE-3 (review follow-up): a document rewrite that INSERTS text → an
// acceptable proposal (not a born-orphaned zero-width hunk), and accepting all
// hunks of a multi-hunk rewrite lands the intended document.
test("document rewrite with an insertion → acceptable proposals; accept-all reaches the rewrite", async () => {
const original = "# F11 accept\n\nThe brown fox sleeps.\n";
const rewrite = "# F11 accept\n\nThe brown fox QUIETLY sleeps today.\n";
const { doc } = await freshDoc("docs/f11accept.md", original);
const api = await getApi();
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({ replacement: rewrite, model: "sonnet", sessionId: "e2e-accept" }));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "expand the sentence");
await settle();
assert.ok(ids.length >= 1, "the rewrite produced at least one proposal");
// Every proposal must be acceptable (the insertion-anchoring fix): accept each.
for (const id of ids) {
const ok = await api.proposalController.acceptById("docs/f11accept.md", id);
assert.ok(ok, `proposal ${id} is acceptable (not born-orphaned)`);
await settle();
}
assert.strictEqual(doc.getText(), rewrite, "accepting all hunks reconstructs the intended rewrite");
assert.strictEqual(api.proposalController.listProposals(doc).length, 0, "no proposals left pending");
});
// SLICE-3: the document-scoped command stays registered for #42 reuse (now behind
// the unified `cowriting.edit`). editDocument is hidden from the palette; the
// markdown-guarded user-facing palette command is `cowriting.edit`.
test("cowriting.editDocument stays registered; cowriting.edit is the markdown-guarded palette command", async () => {
const all = await vscode.commands.getCommands(true);
assert.ok(all.includes("cowriting.editDocument"), "editDocument command registered");
assert.ok(all.includes("cowriting.edit"), "unified edit command registered");
const palette = JSON.parse(fs.readFileSync(path.join(__dirname, "../../../../package.json"), "utf8")).contributes
.menus.commandPalette as Array<{ command: string; when?: string }>;
// editDocument is hidden (routed via cowriting.edit).
const docEntry = palette.find((m) => m.command === "cowriting.editDocument");
assert.strictEqual(docEntry?.when, "false", "editDocument hidden from the palette");
// cowriting.edit is the user-facing, markdown-guarded entry.
const editEntry = palette.find((m) => m.command === "cowriting.edit");
assert.ok(editEntry, "cowriting.edit has a commandPalette entry");
assert.match(editEntry!.when ?? "", /editorLangId == markdown/, "guarded on markdown");
});
});
+127
View File
@@ -0,0 +1,127 @@
import * as assert from "assert";
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import type { CowritingApi } from "../../../src/extension";
const WS = process.env.E2E_WORKSPACE!;
const settle = () => new Promise((r) => setTimeout(r, 400));
async function getApi(): Promise<CowritingApi> {
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!;
const api = (await ext.activate()) as CowritingApi;
assert.ok(api?.editFlow && api?.proposalController, "exports controllers");
return api;
}
async function freshDoc(rel: string, body: string): Promise<{ doc: vscode.TextDocument; key: string }> {
const abs = path.join(WS, rel);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, body, "utf8");
const uri = vscode.Uri.file(abs);
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
// INV-10: proposal rendering (F12 optimistic-apply, driven by
// onDidChangeProposals) is gated on coediting (Task 4).
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
return { doc, key: uri.toString() };
}
// #46 SLICE-3 (accept): one "Accept all" gesture applies every pending proposal
// on the current document through the existing F4 accept seam (INV-42) — batched,
// re-anchor-safe (descending), orphan-skip + report. Host E2E, no LLM.
suite("F12 SLICE-3 — accept-all (#46, INV-42)", () => {
// PUC-6: N pending → all applied, text replaced, proposals cleared.
test("acceptAll applies every pending proposal and reconstructs the document", async () => {
const original = "# All\n\nFirst para alpha.\n\nSecond para beta.\n\nThird para gamma.\n";
const rewrite = "# All\n\nFirst para ALPHA.\n\nSecond para BETA.\n\nThird para GAMMA.\n";
const { doc } = await freshDoc("docs/f12-all.md", original);
const api = await getApi();
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({ replacement: rewrite, model: "sonnet", sessionId: "e2e-f12-all" }));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "uppercase the nouns");
await settle();
assert.strictEqual(ids.length, 3, "three changed blocks → three pending proposals");
// Route through the real "Keep all" command (Task 8, PUC-2) — the doc is the
// active editor from freshDoc, matching the top-of-file CodeLens's own target.
await vscode.commands.executeCommand("cowriting.acceptAllProposals");
await settle();
await settle();
assert.strictEqual(doc.getText(), rewrite, "accept-all reconstructs the intended document");
assert.strictEqual(api.proposalController.listProposals(doc).length, 0, "all proposals cleared");
});
// PUC-6 / INV-42: an orphaned proposal is skipped (not force-applied) and the
// resolvable ones still apply — reported via the {applied, skipped} tally.
test("acceptAllProposals skips an orphaned proposal and applies the rest (report)", async () => {
const original = "# Mix\n\nKeep alpha here.\n\nKeep gamma here.\n";
const rewrite = "# Mix\n\nKeep ALPHA here.\n\nKeep GAMMA here.\n";
const { doc } = await freshDoc("docs/f12-orphan.md", original);
const api = await getApi();
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({ replacement: rewrite, model: "sonnet", sessionId: "e2e-f12-orphan" }));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "uppercase");
await settle();
assert.strictEqual(ids.length, 2, "two pending proposals");
// Orphan the FIRST block's proposal by mangling its target text in the buffer.
const para = "Keep alpha here.";
const start = doc.getText().indexOf(para);
const edit = new vscode.WorkspaceEdit();
edit.replace(
doc.uri,
new vscode.Range(doc.positionAt(start), doc.positionAt(start + para.length)),
"Totally different first paragraph now.",
);
assert.ok(await vscode.workspace.applyEdit(edit), "mangling edit applied");
await settle();
const { applied, skipped } = await api.proposalController.acceptAllProposals(doc);
assert.strictEqual(applied, 1, "the still-resolvable proposal applied");
assert.strictEqual(skipped, 1, "the orphaned proposal was skipped, not mangled");
assert.ok(doc.getText().includes("Keep GAMMA here."), "the resolvable block landed");
assert.ok(
doc.getText().includes("Totally different first paragraph now."),
"the orphaned block kept the operator's text (never force-applied)",
);
assert.strictEqual(api.proposalController.listProposals(doc).length, 1, "the orphaned proposal remains pending");
});
// A single pending proposal still applies through the batch path (the
// top-of-file "Keep all" CodeLens is hidden < 2 pending, Task 8 PUC-2, but the
// command/seam handle any count).
test("acceptAllProposals with one pending proposal applies it", async () => {
const { doc } = await freshDoc("docs/f12-one.md", "# One\n\nThe only paragraph here.\n");
const api = await getApi();
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({
replacement: "# One\n\nThe ONLY paragraph here.\n",
model: "sonnet",
sessionId: "e2e-f12-one",
}));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "uppercase only");
await settle();
assert.strictEqual(ids.length, 1, "one pending proposal");
const { applied, skipped } = await api.proposalController.acceptAllProposals(doc);
assert.strictEqual(applied, 1);
assert.strictEqual(skipped, 0);
assert.strictEqual(doc.getText(), "# One\n\nThe ONLY paragraph here.\n");
});
// The command is registered + palette-guarded on markdown.
test("cowriting.acceptAllProposals is registered and palette-guarded on markdown", async () => {
const all = await vscode.commands.getCommands(true);
assert.ok(all.includes("cowriting.acceptAllProposals"), "command registered");
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "../../../../package.json"), "utf8"));
const entry = (pkg.contributes.menus.commandPalette as Array<{ command: string; when?: string }>).find(
(m) => m.command === "cowriting.acceptAllProposals",
);
assert.ok(entry, "has a commandPalette entry");
assert.match(entry!.when ?? "", /editorLangId == markdown/, "guarded on markdown");
});
});
+402
View File
@@ -0,0 +1,402 @@
import * as assert from "assert";
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import type { CowritingApi } from "../../../src/extension";
import { addProposal, setProposalApplied } from "../../../src/proposalModel";
import { buildFingerprint } from "../../../src/anchorer";
const WS = process.env.E2E_WORKSPACE!;
const settle = () => new Promise((r) => setTimeout(r, 400));
async function getApi(): Promise<CowritingApi> {
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!;
const api = (await ext.activate()) as CowritingApi;
assert.ok(api?.diffViewController && api?.proposalController, "exports controllers");
return api;
}
/** Also enters coediting (INV-10/Task 2): the baseline is established only on
* entry, not merely on open. */
async function freshDoc(rel: string, body: string): Promise<{ doc: vscode.TextDocument; key: string }> {
const abs = path.join(WS, rel);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, body, "utf8");
const uri = vscode.Uri.file(abs);
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
await settle();
return { doc, key: uri.toString() };
}
suite("F12 inline diff — seam landBaseline (#64, INV-48)", () => {
test("applyAgentEdit with landBaseline:false applies text but does NOT advance the baseline", async () => {
const { doc, key } = await freshDoc("docs/f12-land.md", "# T\n\nalpha here.\n");
const api = await getApi();
const before = api.diffViewController.getBaseline(key)?.text ?? doc.getText();
const start = doc.getText().indexOf("alpha");
const ok = await api.attributionController.applyAgentEdit(
doc,
new vscode.Range(doc.positionAt(start), doc.positionAt(start + "alpha".length)),
"ALPHA",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } },
{ landBaseline: false },
);
await settle();
assert.ok(ok, "edit applied");
assert.ok(doc.getText().includes("ALPHA"), "text in buffer");
assert.strictEqual(api.diffViewController.getBaseline(key)?.text ?? doc.getText(), before, "baseline unchanged");
});
});
suite("F12 inline diff — finalize / revert in place (#64, INV-51)", () => {
// Optimistic apply lands the text + re-anchors; the buffer becomes the accepted result.
test("optimisticApply puts the proposed text in the buffer and re-anchors", async () => {
const { doc } = await freshDoc("docs/f12-opt.md", "# T\n\nReplace alpha please.\n");
const api = await getApi();
const p = api.proposalController;
const fp = { text: "Replace alpha please.", before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "Replace ALPHA please.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await api.proposalController.optimisticApply(doc, id!);
await settle();
assert.ok(doc.getText().includes("Replace ALPHA please."), "applied to buffer");
// re-anchored: the proposal still resolves against the mutated buffer
assert.strictEqual(p.listProposals(doc)[0].anchorStart !== null, true, "re-anchored, resolves");
assert.strictEqual(p.listProposals(doc)[0].original, "Replace alpha please.", "original stored");
});
test("finalizeInPlace clears the proposal and keeps the applied text (no double-apply)", async () => {
const { doc } = await freshDoc("docs/f12-fin.md", "# T\n\nKeep alpha now.\n");
const api = await getApi();
const p = api.proposalController;
const docPath = p.keyFor(doc);
const fp = { text: "Keep alpha now.", before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "Keep ALPHA now.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await p.optimisticApply(doc, id!);
await settle();
const ok = await p.finalizeInPlace(docPath, id!);
await settle();
assert.ok(ok, "finalized");
assert.strictEqual(doc.getText(), "# T\n\nKeep ALPHA now.\n", "applied text retained, no double-apply");
assert.strictEqual(p.listProposals(doc).length, 0, "proposal cleared");
});
test("revertInPlace restores the original and clears the proposal", async () => {
const { doc } = await freshDoc("docs/f12-rev.md", "# T\n\nUndo alpha here.\n");
const api = await getApi();
const p = api.proposalController;
const docPath = p.keyFor(doc);
const fp = { text: "Undo alpha here.", before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "Undo ALPHA here.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await p.optimisticApply(doc, id!);
await settle();
const ok = await p.revertInPlace(docPath, id!);
await settle();
assert.ok(ok, "reverted");
assert.strictEqual(doc.getText(), "# T\n\nUndo alpha here.\n", "original restored");
assert.strictEqual(p.listProposals(doc).length, 0, "proposal cleared");
});
test("rejectAll reverts every pending proposal", async () => {
const { doc } = await freshDoc("docs/f12-rejall.md", "# T\n\nOne aaa.\n\nTwo bbb.\n");
const api = await getApi();
const editFlow = api.editFlow;
const p = api.proposalController;
editFlow.setEditTurnForTest(async () => ({ replacement: "# T\n\nOne AAA.\n\nTwo BBB.\n", model: "m", sessionId: "s" }));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "up");
await settle();
for (const id of ids) await p.optimisticApply(doc, id);
await settle();
assert.ok(doc.getText().includes("AAA") && doc.getText().includes("BBB"), "both applied");
const { reverted } = await p.rejectAll(doc);
await settle();
assert.strictEqual(reverted, ids.length, "all reverted");
assert.strictEqual(doc.getText(), "# T\n\nOne aaa.\n\nTwo bbb.\n", "document restored");
assert.strictEqual(p.listProposals(doc).length, 0, "all cleared");
});
});
suite("F12 inline diff — INV-50 listProposals.replaced", () => {
test("listProposals reports the original as `replaced` after optimistic apply", async () => {
const { doc } = await freshDoc("docs/f12-replaced.md", "# R\n\nbrown here.\n");
const api = await getApi();
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({ replacement: "# R\n\nred here.\n", model: "m", sessionId: "s" }));
await editFlow.runEditAndPropose(doc, { kind: "document" }, "x");
await settle(); await settle();
assert.strictEqual(api.proposalController.listProposals(doc)[0].replaced, "brown here.");
});
});
suite("F12 inline diff — editor surface (#64, INV-48/52)", () => {
test("proposing optimistically applies into the editor and the buffer is the accepted result", async () => {
const { doc } = await freshDoc("docs/f12-editor.md", "# E\n\nThe brown fox runs.\n");
const api = await getApi();
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({ replacement: "# E\n\nThe red fox runs.\n", model: "m", sessionId: "s" }));
await editFlow.runEditAndPropose(doc, { kind: "document" }, "recolor the fox");
await settle(); await settle();
assert.ok(doc.getText().includes("The red fox runs."), "optimistically applied into the buffer");
const v = api.proposalController.listProposals(doc)[0];
assert.strictEqual(v.original, "The brown fox runs.", "original captured for the deletion hint/revert");
});
test("editing the inserted text then finalizing keeps the human edit", async () => {
const { doc } = await freshDoc("docs/f12-edit-keep.md", "# E\n\nalpha word here.\n");
const api = await getApi();
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({ replacement: "# E\n\nALPHA word here.\n", model: "m", sessionId: "s" }));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "up");
await settle(); await settle();
// human tweaks the inserted text
const at = doc.getText().indexOf("ALPHA");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(at), doc.positionAt(at + 5)), "ALPHA!");
await vscode.workspace.applyEdit(we);
await settle();
// keyFor(doc) gives the repo-relative path that finalizeInPlace uses as its key;
// the `key` from freshDoc is the URI string, which would not match for in-workspace docs.
const docKey = api.proposalController.keyFor(doc);
await api.proposalController.finalizeInPlace(docKey, ids[0]);
await settle();
assert.ok(doc.getText().includes("ALPHA! word here."), "human edit preserved on accept");
assert.strictEqual(api.proposalController.listProposals(doc).length, 0, "cleared");
});
});
suite("F12 inline diff — control parity (#64, INV-53)", () => {
test("reject-one via revertInPlace reverts in place; rejectAll clears every proposal", async () => {
const { doc } = await freshDoc("docs/f12-parity.md", "# P\n\nuno aaa.\n\ndos bbb.\n");
const api = await getApi();
const p = api.proposalController;
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({ replacement: "# P\n\nuno AAA.\n\ndos BBB.\n", model: "m", sessionId: "s" }));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "up");
await settle(); await settle();
assert.ok(doc.getText().includes("AAA") && doc.getText().includes("BBB"));
// reject ONE via the same seam the per-proposal "✗ Reject" CodeLens uses
// (Task 8, PUC-2) → that block reverts, the other stays applied.
const docKey = p.keyFor(doc);
assert.ok(await p.revertInPlace(docKey, ids[0]), "revert one proposal");
await settle(); await settle();
assert.ok(doc.getText().includes("uno aaa.") && doc.getText().includes("BBB"), "one reverted, one applied");
// rejectAll via the same seam the top-of-file "✗ Reject all" CodeLens uses →
// all gone, document restored.
const { reverted } = await p.rejectAll(doc);
assert.strictEqual(reverted, 1, "the one remaining proposal reverted");
await settle(); await settle();
assert.strictEqual(doc.getText(), "# P\n\nuno aaa.\n\ndos bbb.\n", "document restored");
assert.strictEqual(api.proposalController.listProposals(doc).length, 0);
});
test("cowriting.rejectAllProposals is registered + markdown-guarded", async () => {
const all = await vscode.commands.getCommands(true);
assert.ok(all.includes("cowriting.rejectAllProposals"));
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "../../../../package.json"), "utf8"));
const entry = (pkg.contributes.menus.commandPalette as Array<{ command: string; when?: string }>).find(
(m) => m.command === "cowriting.rejectAllProposals",
);
assert.ok(entry && /editorLangId == markdown/.test(entry.when ?? ""), "palette-guarded on markdown");
});
});
// #70 (INV-5): rejecting a proposal AFTER typing inside its pending range must
// still restore the retained original. The exact-substring anchor is orphaned by
// the interior tweak (INV-1/INV-11) — the revert falls back to the F12
// applied-range bookkeeping (appliedSpans), which shift-tracks the span through
// interior edits.
suite("F12 inline diff — #70 reject after interior edit (INV-5)", () => {
test("revertInPlace restores the original after a tweak inside the pending range", async () => {
const ORIGINAL = "The quick brown fox jumps.";
const { doc } = await freshDoc("docs/s70-reject-tweaked.md", `# T\n\n${ORIGINAL}\n`);
const api = await getApi();
const p = api.proposalController;
const docKey = p.keyFor(doc);
const fp = { text: ORIGINAL, before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "The rapid brown fox jumps.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await p.optimisticApply(doc, id!);
await settle();
assert.ok(doc.getText().includes("The rapid brown fox jumps."), "optimistically applied");
// Human types INSIDE the pending range → orphans the exact anchor (INV-11).
const at = doc.getText().indexOf("rapid");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(at), doc.positionAt(at + "rapid".length)), "RAPID-ish");
assert.ok(await vscode.workspace.applyEdit(we), "interior tweak applied");
await settle();
assert.strictEqual(p.listProposals(doc).find((v) => v.id === id)!.anchorStart, null, "anchor orphaned by the tweak");
// ✗ Reject: must restore the retained original EXACTLY (INV-5), not skip.
assert.ok(await p.revertInPlace(docKey, id!), "reject reports success");
await settle();
assert.strictEqual(doc.getText(), `# T\n\n${ORIGINAL}\n`, "original restored exactly (INV-5)");
assert.strictEqual(p.listProposals(doc).length, 0, "proposal cleared");
});
test("rejectByIdInPlace routes the tweaked-applied case the same way", async () => {
const ORIGINAL = "A steady closing line.";
const { doc } = await freshDoc("docs/s70-reject-route.md", `# T\n\n${ORIGINAL}\n`);
const api = await getApi();
const p = api.proposalController;
const docKey = p.keyFor(doc);
const fp = { text: ORIGINAL, before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "A wobbly closing line.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await p.optimisticApply(doc, id!);
await settle();
const at = doc.getText().indexOf("wobbly");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(at), doc.positionAt(at)), "very-");
assert.ok(await vscode.workspace.applyEdit(we), "interior insertion applied");
await settle();
assert.ok(await p.rejectByIdInPlace(docKey, id!), "gesture-level reject succeeds");
await settle();
assert.strictEqual(doc.getText(), `# T\n\n${ORIGINAL}\n`, "original restored exactly (INV-5)");
});
// Fix direction (b): when the tracked span itself is distrusted (an edit
// straddled its boundary — here a deletion running from the heading into the
// span's interior), the reject FAILS honestly: warning path, proposal kept,
// buffer untouched. Silent-skip-and-report-success (the #70 bug) must not come
// back; reverting at a clamped guessed offset (INV-11) must not either.
// (A boundary-straddling DELETION is used because VS Code minimizes workspace
// edits before change events fire — a wholesale rewrite sharing a prefix/suffix
// decomposes into interior hunks the span legitimately survives.)
test("reject fails loudly — proposal kept, buffer untouched — when the span is distrusted", async () => {
const ORIGINAL = "Sentence one stands here.";
const { doc } = await freshDoc("docs/s70-reject-distrust.md", `# T\n\n${ORIGINAL}\n`);
const api = await getApi();
const p = api.proposalController;
const docKey = p.keyFor(doc);
const fp = { text: ORIGINAL, before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "Sentence ONE stands here.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await p.optimisticApply(doc, id!);
await settle();
// Delete from inside the heading through the middle of the applied span:
// the edit straddles the span's start boundary → the tracked span is
// distrusted AND the exact anchor no longer resolves.
const mid = doc.getText().indexOf("ONE");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(2), doc.positionAt(mid + 1)), "");
assert.ok(await vscode.workspace.applyEdit(we), "boundary-straddling deletion applied");
await settle();
const before = doc.getText();
assert.strictEqual(await p.revertInPlace(docKey, id!), false, "reject reports failure (no silent success)");
await settle();
assert.strictEqual(doc.getText(), before, "buffer untouched — never reverted by guess (INV-11)");
assert.ok(p.listProposals(doc).some((v) => v.id === id), "proposal kept (not silently dropped)");
// Cleanup for later tests: the never-locatable husk is discarded via the
// plain record-only reject.
assert.strictEqual(p.rejectById(docKey, id!), true);
});
// rejectAll must revert a tweaked (orphaned-anchor) proposal via the same
// tracked-span fallback, ordered descending by that span so earlier reverts
// never shift later ones — and must count what it could not revert.
test("rejectAll reverts tweaked proposals via the tracked span and reports skips", async () => {
const { doc } = await freshDoc("docs/s70-rejall-tweak.md", "# T\n\nOne aaa.\n\nTwo bbb.\n");
const api = await getApi();
const p = api.proposalController;
const editFlow = api.editFlow;
editFlow.setEditTurnForTest(async () => ({ replacement: "# T\n\nOne AAA.\n\nTwo BBB.\n", model: "m", sessionId: "s" }));
const ids = await editFlow.runEditAndPropose(doc, { kind: "document" }, "up");
await settle();
for (const id of ids) await p.optimisticApply(doc, id);
await settle();
// Tweak INSIDE the first applied block → its exact anchor orphans.
const at = doc.getText().indexOf("AAA");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(at), doc.positionAt(at + 3)), "AAAZ");
assert.ok(await vscode.workspace.applyEdit(we), "interior tweak applied");
await settle();
const { reverted, skipped } = await p.rejectAll(doc);
await settle();
assert.strictEqual(reverted, ids.length, "ALL proposals reverted, tweaked one included");
assert.strictEqual(skipped, 0, "nothing skipped");
assert.strictEqual(doc.getText(), "# T\n\nOne aaa.\n\nTwo bbb.\n", "document fully restored (INV-5)");
assert.strictEqual(p.listProposals(doc).length, 0, "all cleared");
});
// The decision gestures must stay REACHABLE for a tweaked proposal: the ✓/✗
// CodeLens pair anchors at the tracked span when the interior tweak orphans
// the exact anchor — otherwise the fixed reject path exists only at the API.
test("the ✓ Keep / ✗ Reject CodeLens pair survives an interior tweak (tracked-span anchor)", async () => {
const ORIGINAL = "Lens target sentence here.";
const { doc } = await freshDoc("docs/s70-lens-tweak.md", `# T\n\n${ORIGINAL}\n`);
const api = await getApi();
const p = api.proposalController;
const fp = { text: ORIGINAL, before: "", after: "", lineHint: 2 };
const id = await p.propose(doc, fp, "Lens TARGET sentence here.",
{ kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" });
await p.optimisticApply(doc, id!);
await settle();
const at = doc.getText().indexOf("TARGET");
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(at), doc.positionAt(at + 6)), "TARGET-tweaked");
assert.ok(await vscode.workspace.applyEdit(we), "interior tweak applied");
await settle();
assert.strictEqual(p.listProposals(doc).find((v) => v.id === id)!.anchorStart, null, "anchor orphaned");
const lenses = api.editorProposalController.provideCodeLenses(doc);
const titles = lenses.map((l) => l.command?.title);
assert.deepStrictEqual(titles, ["✓ Keep", "✗ Reject"], "the pair survives the tweak");
assert.deepStrictEqual(lenses[1].command!.arguments, [id], "reject lens still targets the tweaked proposal");
// The reject it routes at still restores the original (the Task-1 contract).
assert.ok(await p.revertInPlace(p.keyFor(doc), id!), "reject via the surviving gesture succeeds");
await settle();
assert.strictEqual(doc.getText(), `# T\n\n${ORIGINAL}\n`, "original restored exactly (INV-5)");
});
});
// Reload-safety: a proposal that was optimistically applied in a PRIOR session
// persists with `original` set and its fp re-anchored to the applied text. A fresh
// controller (empty in-memory `applied` set) must NOT re-capture `original` from the
// already-applied buffer — doing so would clobber the true revert target and break
// Reject. (Regression for the final-review CRITICAL; INV-51/54.)
suite("F12 inline diff — reload-safety (#64, INV-51/54)", () => {
test("optimisticApply does not clobber a previously-persisted original", async () => {
const TRUE_ORIGINAL = "The original sentence here.";
const APPLIED = "The APPLIED sentence here.";
const { doc } = await freshDoc("docs/f12-reload.md", `# R\n\n${TRUE_ORIGINAL}\n`);
const api = await getApi();
const p = api.proposalController;
const key = p.keyFor(doc);
// 1) The buffer holds the APPLIED text (as the saved-while-pending file would).
const at = doc.getText().indexOf(TRUE_ORIGINAL);
const we = new vscode.WorkspaceEdit();
we.replace(doc.uri, new vscode.Range(doc.positionAt(at), doc.positionAt(at + TRUE_ORIGINAL.length)), APPLIED);
assert.ok(await vscode.workspace.applyEdit(we), "apply the prior-session applied text");
await settle();
// 2) Record the proposal directly in the sidecar exactly as a prior session left
// it: fp anchored to the APPLIED text + `original` = the TRUE original. We do
// NOT call optimisticApply, so this controller's in-memory `applied` stays empty.
const appliedAt = doc.getText().indexOf(APPLIED);
const appliedFp = buildFingerprint(doc.getText(), { start: appliedAt, end: appliedAt + APPLIED.length });
let id = "";
api.sidecarRouter.update(key, (a) => {
id = addProposal(a, appliedFp, APPLIED, { kind: "agent", id: "claude", agent: { sdk: "x", model: "m", sessionId: "s" } }, { granularity: "block" }).proposalId;
setProposalApplied(a, id, appliedFp, TRUE_ORIGINAL);
});
p.renderAll(doc);
await settle();
// 3) Re-entry as a reload would trigger (EditorProposalController re-applies on
// onDidChangeProposals because `applied` is empty). The guard must preserve original.
await p.optimisticApply(doc, id);
await settle();
const view = p.listProposals(doc).find((v) => v.id === id);
assert.ok(view, "proposal still present");
assert.strictEqual(view!.original, TRUE_ORIGINAL, "true original preserved, NOT clobbered with the applied text");
// 4) Reject restores the TRUE original (not the applied text).
assert.ok(await p.revertInPlace(key, id), "revert");
await settle();
assert.ok(doc.getText().includes(TRUE_ORIGINAL), "reject restored the true original");
assert.ok(!doc.getText().includes(APPLIED), "applied text removed on reject");
});
});
+157
View File
@@ -0,0 +1,157 @@
import * as assert from "assert";
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import type { CowritingApi } from "../../../src/extension";
const WS = process.env.E2E_WORKSPACE!;
const settle = () => new Promise((r) => setTimeout(r, 400));
async function getApi(): Promise<CowritingApi> {
const ext = vscode.extensions.getExtension("benstull.vscode-cowriting-plugin")!;
const api = (await ext.activate()) as CowritingApi;
assert.ok(api?.threadController, "exports thread controller");
return api;
}
async function freshDoc(rel: string, body: string): Promise<{ doc: vscode.TextDocument; key: string }> {
const abs = path.join(WS, rel);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, body, "utf8");
const uri = vscode.Uri.file(abs);
const doc = await vscode.workspace.openTextDocument(uri);
// INV-10: cowriting.editDocument now warns instead of editing a non-entered
// doc (Task 4) — enter it here (briefly making it active) so the tests below
// exercise the routing/targeting behavior, not the gate. The caller
// re-establishes whichever doc it wants active afterward.
await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand("cowriting.coeditDocument");
return { doc, key: uri.toString() };
}
function pkg(): any {
return JSON.parse(fs.readFileSync(path.join(__dirname, "../../../../package.json"), "utf8"));
}
function menu(id: string): Array<{ command: string; when?: string; group?: string }> {
return pkg().contributes.menus[id] ?? [];
}
// SLICE-1 / #42 (reach): "Ask Claude to Edit" reachable from the editor BODY and
// the editor TAB. The two split commands (editSelection / editDocument) were
// unified behind ONE user-facing command `cowriting.edit` that auto-routes by
// selection at runtime (routeEdit: selection → editSelection, none → editDocument)
// — so the menus carry a SINGLE selection-agnostic entry, both markdown/authorable
// -gated. The routing itself is unit-tested (routeEdit) and exercised through the
// command below; here we assert the declarative menu `when` clauses.
suite("F12 SLICE-1 — Ask-Claude reach (#42, INV-38)", () => {
// PUC-1/2: editor BODY (editor/context) offers the single unified entry,
// markdown + authorable gated, NOT selection-gated (it routes both cases).
test("editor/context offers a single 'Ask Claude to Edit' (cowriting.edit), markdown+authorable", () => {
const m = menu("editor/context");
const edit = m.find((e) => e.command === "cowriting.edit");
assert.ok(edit, "cowriting.edit is in editor/context");
assert.match(edit!.when ?? "", /editorLangId == markdown/, "gated on markdown");
assert.match(edit!.when ?? "", /resourceScheme == file|resourceScheme == untitled/, "gated authorable");
assert.ok(!/editorHasSelection/.test(edit!.when ?? ""), "not selection-gated — one entry routes both cases");
// The old split pair is gone from the menu (the commands stay registered/hidden).
assert.ok(!m.some((e) => e.command === "cowriting.editSelection"), "old editSelection menu entry removed");
assert.ok(!m.some((e) => e.command === "cowriting.editDocument"), "old editDocument menu entry removed");
});
// PUC-3: editor TAB (editor/title/context) carries the same single entry.
test("editor/title/context offers a single 'Ask Claude to Edit' (cowriting.edit), markdown-gated", () => {
const m = menu("editor/title/context");
const edit = m.find((e) => e.command === "cowriting.edit");
assert.ok(edit, "cowriting.edit is in editor/title/context");
assert.match(edit!.when ?? "", /resourceLangId == markdown/, "tab entry gated on markdown");
assert.ok(
!m.some((e) => e.command === "cowriting.editSelection" || e.command === "cowriting.editDocument"),
"old split pair removed from the tab menu",
);
});
// PUC-3 behavior: editDocument invoked with a tab URI targets THAT document,
// not whatever editor happens to be active (mirrors #41's clicked-doc resolution).
// Finding 1 fix (native-surfaces code review): editDocument no longer prompts via
// the removed askEditInstruction webview stub (that path threw a silent, `void`'d,
// unhandled rejection — no toast, no thread, no proposal). It now resolves/focuses
// its target document and hands off to ThreadController.askClaude() (D19), same as
// editSelection. The turn→proposal cut itself (EditFlow.runEditAndPropose against a
// {kind:"document"} target) is exercised directly, without the command, in
// f11Toolbar.test.ts / f12Accept.test.ts / f12Review.test.ts — this test's job is
// the URI-targeting/focus behavior, plus confirming the routing actually reaches
// askClaude (proving Finding 1's dead end is gone).
test("editDocument(uri) targets the clicked tab's document, not the active editor", async () => {
const api = await getApi();
// Doc A is the active editor; Doc B is the "clicked tab" we pass by URI.
const a = await freshDoc("docs/f12-active.md", "# Active\n\nThe active editor paragraph.\n");
const b = await freshDoc("docs/f12-tab.md", "# Tab\n\nThe tab target paragraph to rewrite.\n");
await vscode.window.showTextDocument(a.doc);
await settle();
let askClaudeCalls = 0;
// Capture the active-editor doc INSIDE the spy, before delegating to the real
// askClaude — askClaude's own `workbench.action.addComment` side effect moves
// focus to the ephemeral comment-input widget (a `comment://` URI), which would
// make a post-hoc `activeTextEditor` check meaningless. What we're proving here
// is editDocument's OWN resolve-and-focus step ran against the right document
// before handing off.
let focusedDocAtHandoff: string | undefined;
const origAskClaude = api.threadController.askClaude.bind(api.threadController);
api.threadController.askClaude = async () => {
askClaudeCalls++;
focusedDocAtHandoff = vscode.window.activeTextEditor?.document.uri.toString();
return origAskClaude();
};
try {
await vscode.commands.executeCommand("cowriting.editDocument", b.doc.uri);
await settle();
} finally {
api.threadController.askClaude = origAskClaude;
}
assert.strictEqual(
askClaudeCalls,
1,
"editDocument(uri) reached ThreadController.askClaude — not the removed prompt stub",
);
assert.strictEqual(
focusedDocAtHandoff,
b.doc.uri.toString(),
"editDocument(uri) focused the TAB doc B, not the previously-active doc A, before handing off to askClaude",
);
});
// No URI arg (palette / keybinding) → fall back to the active editor.
test("editDocument() with no arg targets the active editor", async () => {
const api = await getApi();
const a = await freshDoc("docs/f12-noarg.md", "# No arg\n\nThe active doc paragraph here.\n");
await vscode.window.showTextDocument(a.doc);
await settle();
let askClaudeCalls = 0;
// See the sibling test above for why this is captured inside the spy rather
// than after askClaude() returns.
let focusedDocAtHandoff: string | undefined;
const origAskClaude = api.threadController.askClaude.bind(api.threadController);
api.threadController.askClaude = async () => {
askClaudeCalls++;
focusedDocAtHandoff = vscode.window.activeTextEditor?.document.uri.toString();
return origAskClaude();
};
try {
await vscode.commands.executeCommand("cowriting.editDocument");
await settle();
} finally {
api.threadController.askClaude = origAskClaude;
}
assert.strictEqual(askClaudeCalls, 1, "editDocument() with no arg reached ThreadController.askClaude");
assert.strictEqual(
focusedDocAtHandoff,
a.doc.uri.toString(),
"editDocument() with no arg kept the active editor's doc active before handing off to askClaude",
);
});
});

Some files were not shown because too many files have changed in this diff Show More