wachiman 1.16.0 → 1.17.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/README.md +12 -0
- package/package.json +1 -1
- package/skills/wachiman-loop/SKILL.md +43 -0
- package/src/cli.ts +37 -6
- package/src/config.ts +21 -2
- package/src/connectors/local-git.ts +82 -1
- package/src/connectors/working-tree.ts +23 -1
- package/src/engine.ts +848 -127
- package/src/formats.ts +44 -3
- package/src/help.ts +121 -6
- package/src/history/command.ts +5 -5
- package/src/history/match.ts +24 -4
- package/src/history/store.ts +1 -1
- package/src/history/types.ts +7 -2
- package/src/init/agents.ts +135 -0
- package/src/init/args.ts +4 -2
- package/src/init/json.ts +82 -0
- package/src/init/options.ts +15 -5
- package/src/init/render.ts +3 -1
- package/src/init/run.ts +172 -18
- package/src/init/settings.ts +10 -0
- package/src/json-schema.ts +8 -0
- package/src/ledger.ts +2 -2
- package/src/local.ts +37 -9
- package/src/loop/command.ts +24 -2
- package/src/memory/candidates.ts +425 -0
- package/src/memory/capture.ts +298 -0
- package/src/memory/command.ts +195 -0
- package/src/memory/entries.ts +213 -0
- package/src/memory/hygiene.ts +291 -0
- package/src/memory/inspect.ts +74 -0
- package/src/memory/load.ts +283 -0
- package/src/memory/mute.ts +259 -0
- package/src/memory/personal.ts +253 -0
- package/src/memory/proposal.ts +92 -0
- package/src/memory/propose.ts +414 -0
- package/src/memory/remember.ts +187 -0
- package/src/memory/report.ts +250 -0
- package/src/memory/settings.ts +20 -0
- package/src/pr-template/install.ts +23 -0
- package/src/precision.ts +10 -2
- package/src/priority.ts +106 -0
- package/src/repo-settings.ts +166 -3
- package/src/risk.ts +38 -9
- package/src/sinks/common.ts +12 -6
- package/src/sinks/markdown.ts +3 -0
- package/src/sinks/terminal.ts +3 -0
- package/src/store/figures.ts +5 -4
- package/src/store/html.ts +1 -1
- package/src/verify.ts +20 -2
- package/templates/wachiman-review-caller.yml +7 -7
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Personal memory (ADR 0056, spec decision 16): the same entry format as `.wachiman/memory/`, kept
|
|
3
|
+
* outside the repository at `<wachiman home>/memory/<host>/<owner>/<repository>/`, for a developer
|
|
4
|
+
* whose team has not adopted Wachiman. Nothing here is ever committed or pushed, so nothing appears
|
|
5
|
+
* in `git status`.
|
|
6
|
+
*
|
|
7
|
+
* The folder is keyed by the URL of the remote named `origin`: every worktree and clone of a
|
|
8
|
+
* repository shares it and no other repository sees it. A clone with no `origin` has none.
|
|
9
|
+
* Only a laptop review loads it (`localMemory`); a review in Actions never does.
|
|
10
|
+
*/
|
|
11
|
+
import { type Dirent, chmodSync, closeSync, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, writeSync } from "node:fs";
|
|
12
|
+
import { join, sep } from "node:path";
|
|
13
|
+
import { instructionFileOf, MAX_INSTRUCTION_BYTES } from "../engine";
|
|
14
|
+
import { spawnGit, type GitRunner } from "../evidence";
|
|
15
|
+
import { normaliseRemote } from "../history/store";
|
|
16
|
+
import { candidateProblem, CANDIDATE_FORMAT, type Candidate } from "./candidates";
|
|
17
|
+
import { parseMemoryFile } from "./entries";
|
|
18
|
+
import { ARCHIVE_FILE, cutMemoryNote, type MemorySource } from "./load";
|
|
19
|
+
import { applyCandidate, introducedProblems, Refused, type ProposeIo, type ProposeResult } from "./propose";
|
|
20
|
+
|
|
21
|
+
const GIT_TIMEOUT_MS = 30_000;
|
|
22
|
+
|
|
23
|
+
/** A path segment that cannot climb out of its folder or hide: only letters, digits, `.`, `_`, `-`. */
|
|
24
|
+
const segment = (text: string) => text.replace(/[^A-Za-z0-9._-]/g, "-").replace(/^[.-]+/, "");
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The URL of `origin` as configured (`remote.origin.url`), or undefined when the clone has none.
|
|
28
|
+
* Not `git remote get-url`: that applies `insteadOf` rewrites, so one repository would get a
|
|
29
|
+
* different key on a machine that rewrites https to ssh.
|
|
30
|
+
*/
|
|
31
|
+
function originOf(repoDir: string, git: GitRunner): string | undefined {
|
|
32
|
+
const r = git(["config", "--get", "remote.origin.url"], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
|
|
33
|
+
const raw = r.status === 0 ? r.stdout.trim() : "";
|
|
34
|
+
return raw === "" ? undefined : raw;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* `<home>/memory/<host>/<owner>.../<repository>` (one folder per owner part, as GitLab nests groups) for the repository at `repoDir`, or undefined when it
|
|
39
|
+
* has no `origin`, or one whose URL does not name a host, an owner and a repository. `home` is the
|
|
40
|
+
* Wachiman home itself (`wachimanHome(env)`).
|
|
41
|
+
*/
|
|
42
|
+
export function personalMemoryDir(home: string, repoDir: string, git: GitRunner = spawnGit): string | undefined {
|
|
43
|
+
const raw = originOf(repoDir, git);
|
|
44
|
+
if (raw === undefined) return undefined;
|
|
45
|
+
const { text, parsed } = normaliseRemote(raw);
|
|
46
|
+
if (!parsed) return undefined;
|
|
47
|
+
const url = new URL(text);
|
|
48
|
+
const parts = url.pathname.split("/").filter(Boolean).map((p) => {
|
|
49
|
+
try {
|
|
50
|
+
return decodeURIComponent(p);
|
|
51
|
+
} catch {
|
|
52
|
+
return p;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
if (parts.length < 2) return undefined;
|
|
56
|
+
const host = segment(`${url.hostname}${url.port ? `-${url.port}` : ""}`);
|
|
57
|
+
// Each owner part is its own folder, so GitLab's `group-sub/repo` and `group/sub/repo` do not share one.
|
|
58
|
+
const owners = parts.slice(0, -1).map(segment);
|
|
59
|
+
const repository = segment(parts[parts.length - 1]);
|
|
60
|
+
if (host === "" || owners.includes("") || repository === "") return undefined;
|
|
61
|
+
return join(home, "memory", host, ...owners, repository);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The plain `.md` files directly inside `dir`: not links, not hidden. Without `warn`, a file or
|
|
66
|
+
* folder that cannot be read throws (a write must never start from a partial picture). With
|
|
67
|
+
* `warn`, it is named there and left out, so a review goes on without it, and a file over the size
|
|
68
|
+
* cap of committed memory is cut at it with the same notice. Without `warn` a file is never cut:
|
|
69
|
+
* a write must not save a partial copy.
|
|
70
|
+
*/
|
|
71
|
+
export function readPersonalFiles(dir: string, warn?: (line: string) => void): Record<string, string> {
|
|
72
|
+
if (!existsSync(dir)) return {};
|
|
73
|
+
const out: Record<string, string> = {};
|
|
74
|
+
const skip = (path: string, error: unknown) => {
|
|
75
|
+
if (!warn) throw error;
|
|
76
|
+
warn(`personal memory: ${path} could not be read (${error instanceof Error ? error.message : String(error)}), so it is left out of this review.`);
|
|
77
|
+
};
|
|
78
|
+
let entries: Dirent[] = [];
|
|
79
|
+
try {
|
|
80
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
81
|
+
} catch (error) {
|
|
82
|
+
skip(dir, error);
|
|
83
|
+
}
|
|
84
|
+
for (const e of entries) {
|
|
85
|
+
if (!(e.isFile() && !e.isSymbolicLink() && !e.name.startsWith(".") && e.name.endsWith(".md"))) continue;
|
|
86
|
+
try {
|
|
87
|
+
const bytes = readFileSync(join(dir, e.name));
|
|
88
|
+
if (!warn) {
|
|
89
|
+
out[e.name] = bytes.toString("utf8");
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const file = instructionFileOf(e.name, bytes);
|
|
93
|
+
if (file.truncated) warn(cutMemoryNote(e.name, MAX_INSTRUCTION_BYTES / 1024));
|
|
94
|
+
out[e.name] = file.content;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
skip(join(dir, e.name), error);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The personal folder as a `MemorySource`, so a review loads it with the same reader as committed
|
|
104
|
+
* memory. Read once, on first use; a problem is reported through `warn` and never throws.
|
|
105
|
+
*/
|
|
106
|
+
export function personalMemorySource(dir: string, warn: (line: string) => void = () => {}): MemorySource {
|
|
107
|
+
let files: Record<string, string> | undefined;
|
|
108
|
+
const all = () => (files ??= readPersonalFiles(dir, warn));
|
|
109
|
+
return {
|
|
110
|
+
list: () => Object.keys(all()),
|
|
111
|
+
read: (name) => (Object.hasOwn(all(), name) ? all()[name] : null),
|
|
112
|
+
// Personal memory has no settings of its own: the budget is the repository's.
|
|
113
|
+
settings: () => undefined,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The personal source of `repoDir`, or undefined when it has none or its folder is empty. */
|
|
118
|
+
export function personalSourceFor(home: string, repoDir: string, git: GitRunner = spawnGit, warn?: (line: string) => void): MemorySource | undefined {
|
|
119
|
+
const dir = personalMemoryDir(home, repoDir, git);
|
|
120
|
+
if (dir === undefined) return undefined;
|
|
121
|
+
const source = personalMemorySource(dir, warn);
|
|
122
|
+
return source.list().length > 0 ? source : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function privateDirs(home: string, dir: string) {
|
|
126
|
+
const rel = dir.slice(home.length).split(sep).filter(Boolean);
|
|
127
|
+
let at = home;
|
|
128
|
+
for (const part of ["", ...rel]) {
|
|
129
|
+
at = part === "" ? home : join(at, part);
|
|
130
|
+
if (existsSync(at)) continue;
|
|
131
|
+
mkdirSync(at, { mode: 0o700 });
|
|
132
|
+
chmodSync(at, 0o700);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function writeFileAtomic(path: string, text: string) {
|
|
137
|
+
const tmp = `${path}.tmp`;
|
|
138
|
+
const fd = openSync(tmp, "w", 0o600);
|
|
139
|
+
try {
|
|
140
|
+
writeSync(fd, text);
|
|
141
|
+
fsyncSync(fd);
|
|
142
|
+
} finally {
|
|
143
|
+
closeSync(fd);
|
|
144
|
+
}
|
|
145
|
+
chmodSync(tmp, 0o600);
|
|
146
|
+
renameSync(tmp, path);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* `wachiman memory propose <id> --personal`: writes the entry exactly as shown into the personal
|
|
151
|
+
* folder. No commit, no push, no fetch, no pull request. The same rules as the team path apply to
|
|
152
|
+
* the text: an entry already there only gains the evidence it lacks, and an entry that would make
|
|
153
|
+
* `memory check` fail on the personal folder is refused.
|
|
154
|
+
*/
|
|
155
|
+
export function proposePersonal(candidate: Candidate, repoDir: string, home: string, io: ProposeIo, git: GitRunner = spawnGit): ProposeResult {
|
|
156
|
+
const top = git(["rev-parse", "--show-toplevel"], { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
|
|
157
|
+
const root = top.status === 0 ? top.stdout.trim() : repoDir;
|
|
158
|
+
try {
|
|
159
|
+
if (originOf(root, git) === undefined) throw new Refused("this repository has no remote named origin, so it has no personal memory (the folder is keyed by that URL); nothing was written.");
|
|
160
|
+
const dir = personalMemoryDir(home, root, git);
|
|
161
|
+
if (dir === undefined) throw new Refused("the URL of origin does not name a host, an owner and a repository, so there is no personal memory folder for it; nothing was written.");
|
|
162
|
+
if (existsSync(dir) && (lstatSync(dir).isSymbolicLink() || !lstatSync(dir).isDirectory())) throw new Refused(`${dir} is not a plain directory; nothing was written.`);
|
|
163
|
+
const current = readPersonalFiles(dir);
|
|
164
|
+
const applied = applyCandidate(current, candidate);
|
|
165
|
+
if (applied.outcome === "unchanged") {
|
|
166
|
+
io.stdout(`already recorded: ${candidate.id} is in your personal memory with the same evidence; nothing was written.\n`);
|
|
167
|
+
return { recorded: true };
|
|
168
|
+
}
|
|
169
|
+
const introduced = introducedProblems(root, current, applied.files);
|
|
170
|
+
if (introduced.length > 0) throw new Refused(`the entry would make \`wachiman memory check\` fail on your personal memory, so nothing was written:\n${introduced.map((l) => ` ${l}`).join("\n")}`);
|
|
171
|
+
privateDirs(home, dir);
|
|
172
|
+
for (const [name, text] of Object.entries(applied.files)) writeFileAtomic(join(dir, name), text);
|
|
173
|
+
const what = applied.outcome === "inserted" ? `entry ${candidate.id}` : `evidence for entry ${candidate.id}, which was already there,`;
|
|
174
|
+
io.stdout(`recorded: ${what} in your personal memory (${dir}). Nothing was committed or pushed; only your laptop reviews of this repository load it.\n`);
|
|
175
|
+
return { recorded: true };
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (!(error instanceof Refused)) throw error;
|
|
178
|
+
io.stderr(`memory propose: ${error.message}`);
|
|
179
|
+
return { recorded: false };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export type MemoryChoice = "personal" | "team";
|
|
184
|
+
const CHOICE_FORMAT = "wachiman.memory-choice/1";
|
|
185
|
+
|
|
186
|
+
/** `<home>/memory-choice/<repo key>.json`: where a repository's answer to the first-propose question is kept (the key is `repositoryKey(repoDir).key`). */
|
|
187
|
+
const choicePath = (home: string, key: string) => join(home, "memory-choice", `${key}.json`);
|
|
188
|
+
|
|
189
|
+
/** The stored answer for this repository, or undefined when none was given (or the file is not one). */
|
|
190
|
+
export function readMemoryChoice(home: string, key: string): MemoryChoice | undefined {
|
|
191
|
+
try {
|
|
192
|
+
const parsed = JSON.parse(readFileSync(choicePath(home, key), "utf8"));
|
|
193
|
+
return parsed?.format === CHOICE_FORMAT && (parsed.choice === "personal" || parsed.choice === "team") ? parsed.choice : undefined;
|
|
194
|
+
} catch {
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function writeMemoryChoice(home: string, key: string, choice: MemoryChoice, now: Date): void {
|
|
200
|
+
privateDirs(home, join(home, "memory-choice"));
|
|
201
|
+
writeFileAtomic(choicePath(home, key), `${JSON.stringify({ format: CHOICE_FORMAT, choice, at: now.toISOString() }, null, 2)}\n`);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The words of an answer to the first-propose question: `1`, `p` or `personal`; `2`, `t` or `team`. */
|
|
205
|
+
export function parseMemoryChoice(answer: string | undefined): MemoryChoice | undefined {
|
|
206
|
+
const a = (answer ?? "").trim().toLowerCase();
|
|
207
|
+
if (a === "1" || a === "p" || a === "personal") return "personal";
|
|
208
|
+
if (a === "2" || a === "t" || a === "team") return "team";
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export const FIRST_PROPOSE_QUESTION = [
|
|
213
|
+
"This repository's default branch has no .wachiman/memory/, so its team has not adopted Wachiman memory.",
|
|
214
|
+
"Where should this entry go?",
|
|
215
|
+
" 1. personal: kept on this machine, never pushed; your laptop reviews of this repository load it",
|
|
216
|
+
" 2. team: proposed as a pull request on this repository's wachiman/memory branch",
|
|
217
|
+
"The answer is remembered for this repository.",
|
|
218
|
+
].join("\n");
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The personal entries of `dir` as candidates for the team path, with their text and evidence lines
|
|
222
|
+
* exactly as they are in personal memory (`Evidence.text`). `only` limits them to those ids; an id
|
|
223
|
+
* that is not there is listed in `unknown`. An entry whose text a terminal could show differently
|
|
224
|
+
* from what would be committed is skipped and named in `skipped`.
|
|
225
|
+
*/
|
|
226
|
+
export function candidatesFromPersonal(dir: string, only: string[]): { candidates: Candidate[]; unknown: string[]; skipped: string[] } {
|
|
227
|
+
// archive.md is never loaded into a review, so it is not memory to send (the report leaves it out too).
|
|
228
|
+
const entries = Object.entries(readPersonalFiles(dir)).filter(([name]) => name !== ARCHIVE_FILE).flatMap(([name, text]) => parseMemoryFile(name, text).entries);
|
|
229
|
+
const seen = new Set<string>();
|
|
230
|
+
const candidates: Candidate[] = [];
|
|
231
|
+
const skipped: string[] = [];
|
|
232
|
+
for (const e of entries) {
|
|
233
|
+
if (seen.has(e.id) || (only.length > 0 && !only.includes(e.id))) continue;
|
|
234
|
+
seen.add(e.id);
|
|
235
|
+
const candidate: Candidate = {
|
|
236
|
+
format: CANDIDATE_FORMAT,
|
|
237
|
+
id: e.id,
|
|
238
|
+
title: e.title,
|
|
239
|
+
kind: e.kind,
|
|
240
|
+
scope: e.scope,
|
|
241
|
+
rule: e.rule,
|
|
242
|
+
why: e.why,
|
|
243
|
+
...(e.holdsWhile ? { holdsWhile: e.holdsWhile } : {}),
|
|
244
|
+
added: e.added,
|
|
245
|
+
evidence: e.evidence.map((text) => ({ date: /^\d{4}-\d{2}-\d{2}/.exec(text)?.[0] ?? e.added, source: "instruction" as const, agent: false, branch: "personal", text })),
|
|
246
|
+
};
|
|
247
|
+
const problem = candidateProblem(candidate);
|
|
248
|
+
if (problem) skipped.push(`${e.id}: ${problem}`);
|
|
249
|
+
else candidates.push(candidate);
|
|
250
|
+
}
|
|
251
|
+
const unknown = only.filter((id) => !seen.has(id));
|
|
252
|
+
return { candidates, unknown, skipped };
|
|
253
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The memory proposal a thread reply carries (layer 5, spec decisions 9 and 10).
|
|
3
|
+
*
|
|
4
|
+
* A reply that proposes an entry shows its exact text and its id inside two markers the engine writes
|
|
5
|
+
* (model text is neutralised, so no model can write them). The operator approves that text with
|
|
6
|
+
* `/wachiman remember <id>`; the write job reads the text back from the reply, checks that its digest
|
|
7
|
+
* is the id it was asked for, and commits that text with the layer 3 write path. This file is pure:
|
|
8
|
+
* it formats and parses, and it never reads or writes anything.
|
|
9
|
+
*/
|
|
10
|
+
import { candidateProblem, parseEvidenceLine, renderEntry, type Candidate, type Evidence } from "./candidates";
|
|
11
|
+
import { parseMemoryFile } from "./entries";
|
|
12
|
+
|
|
13
|
+
/** Written on the reply that confirms or refuses a `/wachiman remember`. Not the thread-reply signature, so it does not count toward the two-reply cap (spec decision 10). */
|
|
14
|
+
export const REMEMBER_SIGNATURE = "<!-- wachi:remember -->";
|
|
15
|
+
|
|
16
|
+
const OPEN = (id: string) => `<!-- wachi:memory id=${id} -->`;
|
|
17
|
+
const CLOSE = "<!-- /wachi:memory -->";
|
|
18
|
+
|
|
19
|
+
/** A fence longer than any run of backticks in `text`, so the text cannot end its own block. */
|
|
20
|
+
const fenceFor = (text: string) => "`".repeat(Math.max(3, ...(text.match(/`+/g) ?? []).map((run) => run.length + 1)));
|
|
21
|
+
|
|
22
|
+
/** The part of a reply that proposes `c`: the entry as it will be written, its id and how to approve it. */
|
|
23
|
+
export function renderProposal(c: Candidate, mention: string): string {
|
|
24
|
+
const text = renderEntry(c).trimEnd();
|
|
25
|
+
const fence = fenceFor(text);
|
|
26
|
+
return [
|
|
27
|
+
`**Proposed memory entry** \`${c.id}\`. Nothing is recorded yet. This is the exact text that would be written:`,
|
|
28
|
+
"",
|
|
29
|
+
OPEN(c.id),
|
|
30
|
+
`${fence}markdown`,
|
|
31
|
+
text,
|
|
32
|
+
fence,
|
|
33
|
+
CLOSE,
|
|
34
|
+
"",
|
|
35
|
+
`To record exactly this text, the operator replies \`${mention} remember ${c.id}\` in this thread. It becomes one commit on the branch \`wachiman/memory\` in a draft pull request, and it is not active until a person merges that pull request.`,
|
|
36
|
+
].join("\n");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ShownProposal {
|
|
40
|
+
id: string;
|
|
41
|
+
text: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const BLOCK = /<!-- wachi:memory id=([0-9a-f]{7}) -->\n(`{3,})markdown\n([\s\S]*?)\n\2\n<!-- \/wachi:memory -->/g;
|
|
45
|
+
|
|
46
|
+
/** The proposals between the engine's markers in a comment body, in order. */
|
|
47
|
+
export function proposalsIn(body: string): ShownProposal[] {
|
|
48
|
+
return [...body.matchAll(BLOCK)].map((m) => ({ id: m[1], text: m[3].trimEnd() }));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface FromShown {
|
|
52
|
+
candidate?: Candidate;
|
|
53
|
+
/** Why nothing may be written, when there is no candidate. */
|
|
54
|
+
problem?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The candidate whose text is `text`, for the id the operator approved. Nothing is trusted: the text
|
|
59
|
+
* must be exactly one entry, its heading id must be the digest of its fields (which `memory check`
|
|
60
|
+
* also requires), that id must be the one asked for, its evidence must be lines the engine writes,
|
|
61
|
+
* and rendering the rebuilt candidate must give `text` back, byte for byte. A candidate that passes is
|
|
62
|
+
* the text that was shown, so the commit carries the digest the operator approved.
|
|
63
|
+
*/
|
|
64
|
+
export function candidateFromShown(text: string, id: string): FromShown {
|
|
65
|
+
const parsed = parseMemoryFile("index.md", text);
|
|
66
|
+
if (parsed.problems.length > 0) return { problem: `the shown text is not a valid entry (${parsed.problems[0].message}); its digest does not match ${id}.` };
|
|
67
|
+
if (parsed.entries.length !== 1) return { problem: `the shown text holds ${parsed.entries.length} entries, not one.` };
|
|
68
|
+
const entry = parsed.entries[0];
|
|
69
|
+
if (entry.id !== id) return { problem: `the shown text has the digest ${entry.id}, not ${id}.` };
|
|
70
|
+
const evidence: Evidence[] = [];
|
|
71
|
+
for (const line of entry.evidence) {
|
|
72
|
+
const e = parseEvidenceLine(line);
|
|
73
|
+
if (!e) return { problem: `its evidence line "${line}" is not one this tool writes.` };
|
|
74
|
+
evidence.push(e);
|
|
75
|
+
}
|
|
76
|
+
const candidate: Candidate = {
|
|
77
|
+
format: "wachiman.memory-candidate/1",
|
|
78
|
+
id: entry.id,
|
|
79
|
+
title: entry.title,
|
|
80
|
+
kind: entry.kind,
|
|
81
|
+
scope: entry.scope,
|
|
82
|
+
rule: entry.rule,
|
|
83
|
+
why: entry.why,
|
|
84
|
+
...(entry.holdsWhile ? { holdsWhile: entry.holdsWhile } : {}),
|
|
85
|
+
added: entry.added,
|
|
86
|
+
evidence,
|
|
87
|
+
};
|
|
88
|
+
const problem = candidateProblem(candidate);
|
|
89
|
+
if (problem) return { problem };
|
|
90
|
+
if (renderEntry(candidate).trimEnd() !== text.trimEnd()) return { problem: "the shown text would not be written as shown." };
|
|
91
|
+
return { candidate };
|
|
92
|
+
}
|