F3 SLICE-3: pending-edit registry + sidecar section-merge update (INV-9 groundwork) (#6)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ben Stull
2026-06-10 09:51:19 -07:00
parent 363fd098fc
commit decdfbf31d
5 changed files with 142 additions and 2 deletions
+48
View File
@@ -0,0 +1,48 @@
/**
* PendingEditRegistry — the seam's bookkeeping (spec §6.2, INV-9). Before
* applyAgentEdit applies a WorkspaceEdit, it registers the exact expected
* change; the controller's change handler consumes a matching registration to
* attribute that change to the agent. Anything unmatched is human typing.
* vscode-free and unit-testable.
*/
import type { Provenance } from "./model";
export interface PendingEdit {
docPath: string;
/** half-open [start, end) offsets of the replaced range. */
start: number;
end: number;
newText: string;
provenance: Provenance;
turnId?: string;
}
export class PendingEditRegistry {
private pending: PendingEdit[] = [];
register(edit: PendingEdit): void {
this.pending.push(edit);
}
/** Remove a registration that failed to apply. */
unregister(edit: PendingEdit): void {
this.pending = this.pending.filter((p) => p !== edit);
}
/**
* Find-and-consume the registration exactly matching a change event
* (same doc, same replaced range, same inserted text). Null → human edit.
*/
match(docPath: string, change: { start: number; end: number; text: string }): PendingEdit | null {
const i = this.pending.findIndex(
(p) =>
p.docPath === docPath &&
p.start === change.start &&
p.end === change.end &&
p.newText === change.text,
);
if (i === -1) return null;
const [hit] = this.pending.splice(i, 1);
return hit;
}
}
+22 -1
View File
@@ -6,7 +6,7 @@
*/
import * as fs from "node:fs";
import * as path from "node:path";
import { serializeArtifact, type Artifact } from "./model";
import { emptyArtifact, serializeArtifact, type Artifact } from "./model";
export class CoauthorStore {
/** @param rootDir absolute workspace-folder root that owns the `.threads/` tree. */
@@ -28,4 +28,25 @@ export class CoauthorStore {
fs.mkdirSync(path.dirname(p), { recursive: true });
fs.writeFileSync(p, serializeArtifact(artifact), "utf8");
}
/**
* Read-modify-write for sidecar co-ownership (spec F3 §6.2): each controller
* mutates only its own section against a FRESH load, so ThreadController and
* AttributionController never clobber each other. After the mutation, anchors
* referenced by neither threads nor attributions are pruned (proposals are
* still empty in F3 — revisit in F4).
*/
update(docPath: string, mutate: (artifact: Artifact) => void): Artifact {
const artifact = this.load(docPath) ?? emptyArtifact(docPath);
mutate(artifact);
const referenced = new Set<string>([
...artifact.threads.map((t) => t.anchorId),
...artifact.attributions.map((a) => a.anchorId),
]);
for (const id of Object.keys(artifact.anchors)) {
if (!referenced.has(id)) delete artifact.anchors[id];
}
this.save(docPath, artifact);
return artifact;
}
}
+6 -1
View File
@@ -84,7 +84,12 @@ export class ThreadController implements vscode.Disposable {
private persist(state: DocState): void {
this.selfWrites.add(this.store.sidecarPath(state.docPath));
this.store.save(state.docPath, state.artifact);
this.store.update(state.docPath, (a) => {
a.threads = state.artifact.threads;
for (const t of state.artifact.threads) {
a.anchors[t.anchorId] = state.artifact.anchors[t.anchorId];
}
});
}
private ensureState(document: vscode.TextDocument): DocState {