F4 SLICE-1: proposalModel helpers — add/remove + fenced-diff body (#12)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* proposalModel — pure helpers over the F4 `proposals[]` section (spec §6.3,
|
||||
* INV-13: pending-only). Mirrors threadModel.ts: vscode-free mutations against
|
||||
* an Artifact, used inside CoauthorStore.update() by ProposalController.
|
||||
* The diff body is PRESENTATION ONLY — the truth is fingerprint.text →
|
||||
* replacement (INV-11); a whole-range -/+ rendering, not an LCS diff (§6.7).
|
||||
*/
|
||||
import { newId, type Artifact, type Fingerprint, type Proposal, type Provenance } from "./model";
|
||||
|
||||
export function addProposal(
|
||||
artifact: Artifact,
|
||||
fp: Fingerprint,
|
||||
replacement: string,
|
||||
author: Provenance,
|
||||
opts?: { turnId?: string; instruction?: string },
|
||||
): { proposalId: string; anchorId: string } {
|
||||
const anchorId = newId("a");
|
||||
const proposalId = newId("pr");
|
||||
artifact.anchors[anchorId] = { fingerprint: fp };
|
||||
artifact.proposals.push({
|
||||
id: proposalId,
|
||||
anchorId,
|
||||
replacement,
|
||||
author,
|
||||
createdAt: new Date().toISOString(),
|
||||
...(opts?.turnId !== undefined ? { turnId: opts.turnId } : {}),
|
||||
...(opts?.instruction !== undefined ? { instruction: opts.instruction } : {}),
|
||||
});
|
||||
return { proposalId, anchorId };
|
||||
}
|
||||
|
||||
/** Remove a pending proposal (accept and reject both end here — INV-13). */
|
||||
export function removeProposal(artifact: Artifact, proposalId: string): boolean {
|
||||
const before = artifact.proposals.length;
|
||||
artifact.proposals = artifact.proposals.filter((p) => p.id !== proposalId);
|
||||
return artifact.proposals.length < before;
|
||||
}
|
||||
|
||||
/** 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**";
|
||||
const diffLines = [
|
||||
...targetText.split("\n").map((l) => `- ${l}`),
|
||||
...p.replacement.split("\n").map((l) => `+ ${l}`),
|
||||
].join("\n");
|
||||
return `${header}\n\n\`\`\`diff\n${diffLines}\n\`\`\`\n\n✓ Accept applies this replacement · ✗ Reject discards it`;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
type Proposal,
|
||||
} from "../src/model";
|
||||
import { CoauthorStore } from "../src/store";
|
||||
import { addProposal, proposalBody, removeProposal } from "../src/proposalModel";
|
||||
|
||||
const agent = {
|
||||
kind: "agent" as const,
|
||||
@@ -73,3 +74,34 @@ describe("CoauthorStore prune with proposals (spec §6.3)", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("proposalModel helpers (spec §6.4)", () => {
|
||||
const fp = { text: "old line one\nold line two", before: "", after: "", lineHint: 3 };
|
||||
|
||||
it("addProposal creates the anchor + pending record; removeProposal deletes by id", () => {
|
||||
const a = emptyArtifact("docs/x.md");
|
||||
const { proposalId, anchorId } = addProposal(a, fp, "new line", agent, {
|
||||
turnId: "turn-9", instruction: "shorten",
|
||||
});
|
||||
expect(a.anchors[anchorId].fingerprint).toEqual(fp);
|
||||
expect(a.proposals).toHaveLength(1);
|
||||
expect(a.proposals[0].id).toBe(proposalId);
|
||||
expect(a.proposals[0].replacement).toBe("new line");
|
||||
expect(a.proposals[0].turnId).toBe("turn-9");
|
||||
expect(a.proposals[0].instruction).toBe("shorten");
|
||||
expect(removeProposal(a, proposalId)).toBe(true);
|
||||
expect(a.proposals).toHaveLength(0);
|
||||
expect(removeProposal(a, proposalId)).toBe(false);
|
||||
});
|
||||
|
||||
it("proposalBody renders instruction + a fenced unified diff of old → new", () => {
|
||||
const a = emptyArtifact("docs/x.md");
|
||||
addProposal(a, fp, "new only line", agent, { instruction: "merge the lines" });
|
||||
const body = proposalBody(fp.text, a.proposals[0]);
|
||||
expect(body).toContain("**Claude proposes** — _merge the lines_");
|
||||
expect(body).toContain("```diff");
|
||||
expect(body).toContain("- old line one");
|
||||
expect(body).toContain("- old line two");
|
||||
expect(body).toContain("+ new only line");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user