@intentius/chant 0.89.0 → 0.90.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/handlers/serve.d.ts.map +1 -1
- package/dist/cli/mcp/server.d.ts +10 -5
- package/dist/cli/mcp/server.d.ts.map +1 -1
- package/dist/cli/mcp/types.d.ts +13 -5
- package/dist/cli/mcp/types.d.ts.map +1 -1
- package/dist/cli/mcp/workspace-tools.d.ts +53 -0
- package/dist/cli/mcp/workspace-tools.d.ts.map +1 -0
- package/dist/op/op-verb-class.d.ts.map +1 -1
- package/dist/workspace/conformance/index.d.ts +42 -2
- package/dist/workspace/conformance/index.d.ts.map +1 -1
- package/dist/workspace/conformance/vitest.d.ts.map +1 -1
- package/dist/workspace/reason-codes.d.ts +3 -0
- package/dist/workspace/reason-codes.d.ts.map +1 -1
- package/dist/workspace/records-write.d.ts +35 -3
- package/dist/workspace/records-write.d.ts.map +1 -1
- package/dist/workspace/records.d.ts +4 -1
- package/dist/workspace/records.d.ts.map +1 -1
- package/dist/workspace/source-block.d.ts +85 -0
- package/dist/workspace/source-block.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/cli/handlers/serve.ts +2 -1
- package/src/cli/mcp/docs-parity.test.ts +20 -2
- package/src/cli/mcp/server.ts +23 -6
- package/src/cli/mcp/types.ts +15 -2
- package/src/cli/mcp/workspace-tools.test.ts +198 -0
- package/src/cli/mcp/workspace-tools.ts +405 -0
- package/src/op/op-verb-class.ts +6 -0
- package/src/workspace/conformance/__fixture__/decisions/decision.kind.mjs +4 -0
- package/src/workspace/conformance/__fixture__/decisions/decision.schema.json +100 -6
- package/src/workspace/conformance/index.mjs +3 -0
- package/src/workspace/conformance/index.ts +185 -7
- package/src/workspace/conformance/vitest.ts +17 -8
- package/src/workspace/reason-codes.ts +3 -0
- package/src/workspace/records-amend.schema.json +2 -1
- package/src/workspace/records-close.schema.json +2 -1
- package/src/workspace/records-new.schema.json +4 -1
- package/src/workspace/records-review.schema.json +2 -1
- package/src/workspace/records-write.ts +85 -7
- package/src/workspace/records.schema.json +1 -0
- package/src/workspace/records.ts +28 -0
- package/src/workspace/source-block.test.ts +167 -0
- package/src/workspace/source-block.ts +129 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A record's stored source block (#2708): the decision schema documents it,
|
|
3
|
+
* records new writes it and records returns it unchanged, a harvested record
|
|
4
|
+
* opens proposed, and records warns source-transcript-drift when the pinned
|
|
5
|
+
* transcript is here and hashes to something else.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { cpSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { pathToFileURL } from "node:url";
|
|
13
|
+
import { afterEach, beforeEach, describe, expect, test } from "vitest";
|
|
14
|
+
import { parseFrontMatter, type RecordEntry } from "./records";
|
|
15
|
+
import { queryRecords } from "./records-cli";
|
|
16
|
+
import { newRecord, renderRecord, stableJson } from "./records-write";
|
|
17
|
+
import { sourceBlockProblems, transcriptDrift, transcriptFile } from "./source-block";
|
|
18
|
+
|
|
19
|
+
const REPO = join(import.meta.dirname, "..", "..", "..", "..");
|
|
20
|
+
const DECISIONS = join(REPO, "docs", "design", "decisions");
|
|
21
|
+
const KIND = "decisions/decision.kind.mjs";
|
|
22
|
+
|
|
23
|
+
type Data = Record<string, unknown>;
|
|
24
|
+
|
|
25
|
+
const SAMPLE = (() => {
|
|
26
|
+
const fm = parseFrontMatter(readFileSync(join(DECISIONS, "ws-003-seal-scope.md"), "utf-8"));
|
|
27
|
+
if (!fm.ok) throw new Error(fm.message);
|
|
28
|
+
return fm.value;
|
|
29
|
+
})();
|
|
30
|
+
|
|
31
|
+
/** A proposed decision like ws-003, with `over` laid on top. */
|
|
32
|
+
function proposal(over: Data = {}): Data {
|
|
33
|
+
const d: Data = { ...structuredClone(SAMPLE), state: "proposed", choice: null, decided_by: null, decided_on: null, reviews: [], ...over };
|
|
34
|
+
delete d.id;
|
|
35
|
+
return d;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const sha = (text: string) => createHash("sha256").update(text).digest("hex");
|
|
39
|
+
const TRANSCRIPT = '{"turn":1,"role":"user","text":"which way do we deploy?"}\n';
|
|
40
|
+
|
|
41
|
+
const HARNESS = {
|
|
42
|
+
via: "mcp",
|
|
43
|
+
client: { name: "claude-code", version: "2.1.0" },
|
|
44
|
+
harness: "claude-code",
|
|
45
|
+
model: "claude-opus-5-5",
|
|
46
|
+
session: { id: "0f4c2a", record: "S-0002" },
|
|
47
|
+
turns: { from: 12, to: 18 },
|
|
48
|
+
transcript: { path: "transcripts/0f4c2a.jsonl", sha256: sha(TRANSCRIPT) },
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
let dir: string;
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
dir = realpathSync(mkdtempSync(join(tmpdir(), "chant-source-block-")));
|
|
54
|
+
mkdirSync(join(dir, "decisions"));
|
|
55
|
+
mkdirSync(join(dir, "transcripts"));
|
|
56
|
+
cpSync(join(DECISIONS, "decision.kind.mjs"), join(dir, "decisions", "decision.kind.mjs"));
|
|
57
|
+
cpSync(join(DECISIONS, "decision.schema.json"), join(dir, "decisions", "decision.schema.json"));
|
|
58
|
+
writeFileSync(join(dir, "decisions", "ws-001-one.md"), renderRecord({ ...proposal({ title: "One" }), id: "ws-001" }, "\n# One\n"));
|
|
59
|
+
writeFileSync(join(dir, "transcripts", "0f4c2a.jsonl"), TRANSCRIPT);
|
|
60
|
+
});
|
|
61
|
+
afterEach(() => rmSync(dir, { recursive: true, force: true }));
|
|
62
|
+
|
|
63
|
+
const code = (doc: object) => ("error" in doc ? (doc as { error: { code: string } }).error.code : "ok");
|
|
64
|
+
|
|
65
|
+
async function read(): Promise<RecordEntry[]> {
|
|
66
|
+
const doc = await queryRecords({ kind: KIND, cwd: dir });
|
|
67
|
+
if (!("records" in doc)) throw new Error(JSON.stringify(doc));
|
|
68
|
+
return doc.records;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function write(source: unknown, over: Data = {}) {
|
|
72
|
+
return newRecord({ kind: KIND, fields: JSON.stringify(proposal({ title: "Two", source, ...over })), cwd: dir });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
describe("the decision schema documents the block", () => {
|
|
76
|
+
test("each form takes the proposal fields, and a harness form needs via", async () => {
|
|
77
|
+
for (const source of [
|
|
78
|
+
HARNESS,
|
|
79
|
+
{ ...HARNESS, issue: "INTENTIUS/chant#2708" },
|
|
80
|
+
{ issue: "INTENTIUS/chant#2708", row: "Source", revision: null, ...HARNESS },
|
|
81
|
+
{ kind: "workspace", member: "app", ...HARNESS, session: "S-0001" },
|
|
82
|
+
{ via: "cli" },
|
|
83
|
+
]) {
|
|
84
|
+
const doc = await write(source, { title: `T ${Math.random()}` });
|
|
85
|
+
expect(code(doc), JSON.stringify(source)).toBe("ok");
|
|
86
|
+
}
|
|
87
|
+
const { via: _via, ...noVia } = HARNESS;
|
|
88
|
+
for (const source of [
|
|
89
|
+
noVia,
|
|
90
|
+
{ ...HARNESS, via: "email" },
|
|
91
|
+
{ ...HARNESS, transcript: { path: "a", uri: "file:///a", sha256: sha("a") } },
|
|
92
|
+
{ ...HARNESS, transcript: { path: "a", sha256: "ABC" } },
|
|
93
|
+
{ ...HARNESS, turns: { from: 3 } },
|
|
94
|
+
{ ...HARNESS, session: {} },
|
|
95
|
+
{ ...HARNESS, client: { version: "1" } },
|
|
96
|
+
{ ...HARNESS, row: "a row without an issue" },
|
|
97
|
+
]) {
|
|
98
|
+
const doc = await write(source);
|
|
99
|
+
expect(code(doc), JSON.stringify(source)).toBe("record-schema-invalid");
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("chant checks the shapes the schema can't: turns that end before they start", async () => {
|
|
104
|
+
const doc = await write({ ...HARNESS, turns: { from: 18, to: 12 } });
|
|
105
|
+
expect(doc).toMatchObject({ error: { code: "record-schema-invalid", message: expect.stringContaining("/source/turns ends before it starts") } });
|
|
106
|
+
expect(sourceBlockProblems({ ...HARNESS, turns: { from: 18, to: 12 } }, "source")).toEqual(["/source/turns ends before it starts"]);
|
|
107
|
+
expect(sourceBlockProblems({ issue: "o/r#1", row: "anything the kind says" }, "source")).toEqual([]);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("records new and records", () => {
|
|
112
|
+
test("records new --from with a source block writes it, and records --json returns it unchanged", async () => {
|
|
113
|
+
const doc = await write(HARNESS);
|
|
114
|
+
expect(code(doc)).toBe("ok");
|
|
115
|
+
const written = (await read()).find((r) => r.id === "ws-002");
|
|
116
|
+
expect(stableJson(written?.data?.source)).toBe(stableJson(HARNESS));
|
|
117
|
+
expect(written?.valid).toBe(true);
|
|
118
|
+
expect(written?.warnings.map((w) => w.code)).not.toContain("source-transcript-drift");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("a harvested record opens proposed; any other state is refused with a remedy", async () => {
|
|
122
|
+
const decided = await write(
|
|
123
|
+
{ ...HARNESS, via: "harvest" },
|
|
124
|
+
{ state: "decided", choice: SAMPLE.choice, decided_by: "alice", decided_on: "2026-09-25" },
|
|
125
|
+
);
|
|
126
|
+
expect(decided).toMatchObject({ error: { code: "source-harvest-not-proposed", message: expect.stringContaining('write it with state "proposed"') } });
|
|
127
|
+
const withdrawn = await write({ ...HARNESS, via: "harvest" }, { state: "withdrawn" });
|
|
128
|
+
expect(code(withdrawn)).toBe("source-harvest-not-proposed");
|
|
129
|
+
expect(code(await write({ ...HARNESS, via: "harvest" }))).toBe("ok");
|
|
130
|
+
// Only a harvest opens proposed: a decision written at the CLI may still be decided in one go.
|
|
131
|
+
const cli = await write({ via: "cli" }, { title: "Three", state: "decided", choice: SAMPLE.choice, decided_by: "alice", decided_on: "2026-09-25" });
|
|
132
|
+
expect(code(cli)).toBe("ok");
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("source-transcript-drift", () => {
|
|
137
|
+
test("warns when the transcript is here and its bytes changed, and not when they match or it is missing", async () => {
|
|
138
|
+
expect(code(await write(HARNESS))).toBe("ok");
|
|
139
|
+
const drift = async () => (await read()).find((r) => r.id === "ws-002")!.warnings.filter((w) => w.code === "source-transcript-drift");
|
|
140
|
+
expect(await drift()).toEqual([]);
|
|
141
|
+
writeFileSync(join(dir, "transcripts", "0f4c2a.jsonl"), TRANSCRIPT + '{"turn":2}\n');
|
|
142
|
+
const warned = await drift();
|
|
143
|
+
expect(warned).toHaveLength(1);
|
|
144
|
+
expect(warned[0].message).toContain("transcripts/0f4c2a.jsonl is pinned at sha256");
|
|
145
|
+
rmSync(join(dir, "transcripts", "0f4c2a.jsonl"));
|
|
146
|
+
expect(await drift()).toEqual([]);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("reads an absolute path, ~/ and file: URIs, and never fetches another URI", () => {
|
|
150
|
+
const file = join(dir, "transcripts", "0f4c2a.jsonl");
|
|
151
|
+
const bad = "0".repeat(64);
|
|
152
|
+
expect(transcriptDrift({ transcript: { path: file, sha256: sha(TRANSCRIPT) } }, "source", "/nowhere")).toBeUndefined();
|
|
153
|
+
expect(transcriptDrift({ transcript: { path: file, sha256: bad } }, "source", "/nowhere")).toContain("is pinned at sha256 000000000000");
|
|
154
|
+
expect(transcriptDrift({ transcript: { uri: pathToFileURL(file).href, sha256: bad } }, "source", "/nowhere")).toContain("file:");
|
|
155
|
+
expect(transcriptDrift({ transcript: { uri: "https://example.com/t.jsonl", sha256: bad } }, "source", dir)).toBeUndefined();
|
|
156
|
+
expect(transcriptFile({ path: "~/t.jsonl" }, dir)).toMatch(/\/t\.jsonl$/);
|
|
157
|
+
expect(transcriptFile({ path: "t.jsonl" }, dir)).toBe(join(dir, "t.jsonl"));
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("a kind that does not opt in is not checked", async () => {
|
|
161
|
+
const kindFile = join(dir, "decisions", "decision.kind.mjs");
|
|
162
|
+
writeFileSync(kindFile, readFileSync(kindFile, "utf-8").replace(/\n\s*source: \{ field: "source" \},/, ""));
|
|
163
|
+
expect(code(await write({ ...HARNESS, turns: { from: 18, to: 12 } }))).toBe("ok");
|
|
164
|
+
writeFileSync(join(dir, "transcripts", "0f4c2a.jsonl"), "changed");
|
|
165
|
+
expect((await read()).flatMap((r) => r.warnings.map((w) => w.code))).not.toContain("source-transcript-drift");
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A record's stored source block (#2708): where a proposal came from.
|
|
3
|
+
*
|
|
4
|
+
* chant's provenance (`trust/provenance.ts`) is computed from git: who
|
|
5
|
+
* committed a record, and with which key. It says nothing about the harness,
|
|
6
|
+
* model and session a decision was proposed in, or the conversation behind it.
|
|
7
|
+
* A record kind that declares `source: { field }` opts in to a stored block
|
|
8
|
+
* holding that, next to whatever else its source field says (a decision's
|
|
9
|
+
* issue row or workspace member, a work item's finding):
|
|
10
|
+
*
|
|
11
|
+
* - `via`: how the record reached the workspace, `cli`, `mcp` or `harvest`;
|
|
12
|
+
* - `client`: the MCP client's `clientInfo` (`name`, `version`), or the CLI;
|
|
13
|
+
* - `harness`: the harness's id, such as `claude-code` or `codex`;
|
|
14
|
+
* - `model`: the model id the harness reports;
|
|
15
|
+
* - `session`: the harness's session or conversation id, as a string, or as
|
|
16
|
+
* `{ id, record }` with the chant session record it was held in (#2697);
|
|
17
|
+
* - `turns`: the turn range the decision was made in, `{ from, to }`;
|
|
18
|
+
* - `transcript`: `{ path | uri, sha256 }`, pinning a transcript by hash and
|
|
19
|
+
* never copying it, the way evidence pins a file.
|
|
20
|
+
*
|
|
21
|
+
* Every field is optional, and the block is data about the proposal, not
|
|
22
|
+
* trust: nothing here raises or lowers a record's standing. The rules are
|
|
23
|
+
* three. The fields that are present must have these shapes
|
|
24
|
+
* (`record-schema-invalid` otherwise, beside the kind's own schema). A record
|
|
25
|
+
* written with `via: "harvest"` must open in the kind's first state, since a
|
|
26
|
+
* harvest proposes and a person decides (`source-harvest-not-proposed`, a
|
|
27
|
+
* write refusal). And `records` warns `source-transcript-drift` when the
|
|
28
|
+
* pinned transcript can be read here and its bytes hash to something else.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { readFileSync } from "node:fs";
|
|
32
|
+
import { homedir } from "node:os";
|
|
33
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
34
|
+
import { fileURLToPath } from "node:url";
|
|
35
|
+
import { z } from "zod";
|
|
36
|
+
import { sha256Hex } from "../content-digest";
|
|
37
|
+
|
|
38
|
+
/** How a record reached the workspace. Closed. */
|
|
39
|
+
export const SOURCE_VIAS = ["cli", "mcp", "harvest"] as const;
|
|
40
|
+
export type SourceVia = (typeof SOURCE_VIAS)[number];
|
|
41
|
+
|
|
42
|
+
const text = z.string().min(1);
|
|
43
|
+
const sha256 = z.string().regex(/^[0-9a-f]{64}$/, "must be the lowercase hex SHA-256 of the transcript's bytes");
|
|
44
|
+
|
|
45
|
+
/** The proposal fields of a source block. Other fields of the block are the kind's, and left to its schema. */
|
|
46
|
+
export const sourceBlockSchema = z.object({
|
|
47
|
+
via: z.enum(SOURCE_VIAS).optional(),
|
|
48
|
+
client: z.object({ name: text, version: text.optional(), title: text.optional() }).strict().optional(),
|
|
49
|
+
harness: text.optional(),
|
|
50
|
+
model: text.optional(),
|
|
51
|
+
session: z
|
|
52
|
+
.union([
|
|
53
|
+
text,
|
|
54
|
+
z.null(),
|
|
55
|
+
z
|
|
56
|
+
.object({ id: text.optional(), record: text.optional() })
|
|
57
|
+
.strict()
|
|
58
|
+
.refine((s) => s.id !== undefined || s.record !== undefined, { message: "names neither the harness's session id nor a chant session record" }),
|
|
59
|
+
])
|
|
60
|
+
.optional(),
|
|
61
|
+
turns: z
|
|
62
|
+
.object({ from: z.number().int().min(0), to: z.number().int().min(0) })
|
|
63
|
+
.strict()
|
|
64
|
+
.refine((t) => t.to >= t.from, { message: "ends before it starts" })
|
|
65
|
+
.optional(),
|
|
66
|
+
transcript: z
|
|
67
|
+
.union([z.object({ path: text, sha256 }).strict(), z.object({ uri: text, sha256 }).strict()])
|
|
68
|
+
.optional(),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
export type SourceBlock = z.infer<typeof sourceBlockSchema>;
|
|
72
|
+
|
|
73
|
+
/** The source block of `data`, when `field` holds an object. */
|
|
74
|
+
export function sourceBlock(data: Record<string, unknown> | null, field: string): Record<string, unknown> | undefined {
|
|
75
|
+
const v = data?.[field];
|
|
76
|
+
return v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** What is wrong with the proposal fields of `block`, as `<path> <message>` lines. Empty when nothing is. */
|
|
80
|
+
export function sourceBlockProblems(block: Record<string, unknown>, field: string): string[] {
|
|
81
|
+
const parsed = sourceBlockSchema.safeParse(block);
|
|
82
|
+
if (parsed.success) return [];
|
|
83
|
+
return parsed.error.issues.map((i) => `/${[field, ...i.path].join("/")} ${i.message}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The file a transcript pin names, when it can be read here: a `path`
|
|
88
|
+
* (absolute, `~/` from the home directory, or else from the workspace root)
|
|
89
|
+
* or a `file:` URI. Any other URI is not fetched, so it is never reachable.
|
|
90
|
+
*/
|
|
91
|
+
export function transcriptFile(transcript: { path?: string; uri?: string }, workspaceDir: string): string | undefined {
|
|
92
|
+
if (transcript.path !== undefined) {
|
|
93
|
+
const p = transcript.path;
|
|
94
|
+
if (p === "~" || p.startsWith("~/")) return join(homedir(), p.slice(1));
|
|
95
|
+
return isAbsolute(p) ? p : resolve(workspaceDir, p);
|
|
96
|
+
}
|
|
97
|
+
if (transcript.uri !== undefined && transcript.uri.startsWith("file:")) {
|
|
98
|
+
try {
|
|
99
|
+
return fileURLToPath(transcript.uri);
|
|
100
|
+
} catch {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The `source-transcript-drift` message for `block`, or undefined: when its
|
|
109
|
+
* transcript pin is well formed, the file it names can be read here, and its
|
|
110
|
+
* bytes hash to something other than the pin. A transcript that can't be
|
|
111
|
+
* read says nothing either way.
|
|
112
|
+
*/
|
|
113
|
+
export function transcriptDrift(block: Record<string, unknown>, field: string, workspaceDir: string): string | undefined {
|
|
114
|
+
const parsed = sourceBlockSchema.shape.transcript.safeParse(block.transcript);
|
|
115
|
+
if (!parsed.success || parsed.data === undefined) return undefined;
|
|
116
|
+
const t = parsed.data as { path?: string; uri?: string; sha256: string };
|
|
117
|
+
const file = transcriptFile(t, workspaceDir);
|
|
118
|
+
if (file === undefined) return undefined;
|
|
119
|
+
let bytes: Uint8Array;
|
|
120
|
+
try {
|
|
121
|
+
bytes = readFileSync(file);
|
|
122
|
+
} catch {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
const actual = sha256Hex(bytes);
|
|
126
|
+
if (actual === t.sha256) return undefined;
|
|
127
|
+
const named = t.path ?? t.uri!;
|
|
128
|
+
return `${field}.transcript ${named} is pinned at sha256 ${t.sha256.slice(0, 12)}, and the file here hashes to ${actual.slice(0, 12)}: it is not the transcript the record means`;
|
|
129
|
+
}
|