@uselazuli/cli 0.0.0-stage → 1.0.1
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/CHANGELOG.md +98 -0
- package/LICENSE +21 -0
- package/README.md +202 -2
- package/agents/lazuli-design-reviewer.md +30 -0
- package/agents/lazuli-first-look.md +22 -0
- package/agents/lazuli-task-tester.md +25 -0
- package/bin/lazuli.mjs +699 -0
- package/package.json +11 -4
- package/rules/accessibility/HYPOTHESES.md +8 -0
- package/rules/accessibility/_area.yaml +7 -0
- package/rules/accessibility/operable.yaml +100 -0
- package/rules/accessibility/perceivable.yaml +153 -0
- package/rules/accessibility/robust.yaml +24 -0
- package/rules/accessibility/understandable.yaml +72 -0
- package/rules/color/HYPOTHESES.md +27 -0
- package/rules/color/_area.yaml +10 -0
- package/rules/color/adapt.yaml +183 -0
- package/rules/color/create.yaml +259 -0
- package/rules/color/review.yaml +215 -0
- package/rules/color/signal.yaml +196 -0
- package/rules/content/HYPOTHESES.md +18 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +347 -0
- package/rules/content/voice.yaml +144 -0
- package/rules/content/writing.yaml +202 -0
- package/rules/data/HYPOTHESES.md +11 -0
- package/rules/data/_area.yaml +7 -0
- package/rules/data/charts.yaml +189 -0
- package/rules/data/dashboards.yaml +141 -0
- package/rules/data/review.yaml +159 -0
- package/rules/graphic/HYPOTHESES.md +17 -0
- package/rules/graphic/_area.yaml +8 -0
- package/rules/graphic/identity.yaml +141 -0
- package/rules/graphic/pieces.yaml +164 -0
- package/rules/graphic/review.yaml +242 -0
- package/rules/graphic/symbol.yaml +141 -0
- package/rules/motion/HYPOTHESES.md +19 -0
- package/rules/motion/_area.yaml +7 -0
- package/rules/motion/micro.yaml +143 -0
- package/rules/motion/review.yaml +347 -0
- package/rules/motion/transitions.yaml +169 -0
- package/rules/typography/HYPOTHESES.md +12 -0
- package/rules/typography/_area.yaml +8 -0
- package/rules/typography/adapt.yaml +45 -0
- package/rules/typography/create.yaml +126 -0
- package/rules/typography/review.yaml +127 -0
- package/rules/ui/HYPOTHESES.md +23 -0
- package/rules/ui/_area.yaml +9 -0
- package/rules/ui/compose.yaml +219 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +399 -0
- package/rules/ui/states.yaml +257 -0
- package/rules/ux/HYPOTHESES.md +13 -0
- package/rules/ux/_area.yaml +8 -0
- package/rules/ux/flows.yaml +164 -0
- package/rules/ux/forms.yaml +142 -0
- package/rules/ux/review.yaml +186 -0
- package/skills/lazuli/SKILL.md +180 -0
- package/skills/lazuli/procedures/create.md +29 -0
- package/skills/lazuli/procedures/explain-decisions.md +22 -0
- package/skills/lazuli/procedures/improve.md +30 -0
- package/skills/lazuli/procedures/remember-this.md +41 -0
- package/skills/lazuli/procedures/review-design.md +35 -0
- package/skills/lazuli/procedures/show-options.md +32 -0
- package/skills/lazuli-accessibility-check/SKILL.md +68 -0
- package/skills/lazuli-color-context-and-culture/SKILL.md +53 -0
- package/skills/lazuli-color-context-and-culture/references/sectors.md +26 -0
- package/skills/lazuli-color-palette-by-intent/SKILL.md +54 -0
- package/skills/lazuli-color-palette-by-intent/references/intent-chords.md +51 -0
- package/skills/lazuli-color-screen-review/SKILL.md +51 -0
- package/skills/lazuli-color-states-and-signals/SKILL.md +53 -0
- package/skills/lazuli-content-interface-writing/SKILL.md +53 -0
- package/skills/lazuli-content-voice/SKILL.md +39 -0
- package/skills/lazuli-data-charts-and-dashboards/SKILL.md +59 -0
- package/skills/lazuli-graphic-derived-pieces/SKILL.md +40 -0
- package/skills/lazuli-graphic-derived-pieces/references/piece-template.html +24 -0
- package/skills/lazuli-graphic-identity/SKILL.md +43 -0
- package/skills/lazuli-motion-micro-and-transitions/SKILL.md +49 -0
- package/skills/lazuli-typography-choose-and-pair/SKILL.md +50 -0
- package/skills/lazuli-typography-screen-review/SKILL.md +51 -0
- package/skills/lazuli-ui-composition/SKILL.md +42 -0
- package/skills/lazuli-ui-composition/references/option-template.html +34 -0
- package/skills/lazuli-ui-layout-review/SKILL.md +47 -0
- package/skills/lazuli-ui-states-and-system/SKILL.md +54 -0
- package/skills/lazuli-ux-flows-and-forms/SKILL.md +51 -0
- package/skills/lazuli-ux-task-test/SKILL.md +44 -0
- package/src/browser.mjs +161 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +428 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +97 -0
- package/src/commands/draft-memo.mjs +161 -0
- package/src/commands/hooks.mjs +343 -0
- package/src/commands/install-hooks.mjs +120 -0
- package/src/commands/install.mjs +132 -0
- package/src/commands/list-rules.mjs +49 -0
- package/src/commands/map-page.mjs +594 -0
- package/src/commands/record-answers.mjs +79 -0
- package/src/commands/record-log.mjs +296 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +164 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +383 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +549 -0
- package/src/measure/chart-read.mjs +401 -0
- package/src/measure/chart.mjs +264 -0
- package/src/measure/coherence.mjs +145 -0
- package/src/measure/color.mjs +79 -0
- package/src/measure/content.mjs +82 -0
- package/src/measure/cvd.mjs +24 -0
- package/src/measure/extreme.mjs +241 -0
- package/src/measure/forms.mjs +150 -0
- package/src/measure/in-page/a11y.js +489 -0
- package/src/measure/in-page/color.js +346 -0
- package/src/measure/in-page/composition.js +1496 -0
- package/src/measure/in-page/contrast.js +15 -0
- package/src/measure/in-page/cvd.js +173 -0
- package/src/measure/in-page/extreme.js +355 -0
- package/src/measure/in-page/frames.js +781 -0
- package/src/measure/in-page/look.js +28 -0
- package/src/measure/in-page/motion-probe.js +169 -0
- package/src/measure/in-page/style.js +107 -0
- package/src/measure/in-page/svg-strokes.js +112 -0
- package/src/measure/in-page/svg-uses.js +50 -0
- package/src/measure/in-page/symbol.js +123 -0
- package/src/measure/in-page/type.js +266 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +91 -0
- package/src/measure/motion.mjs +508 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +252 -0
- package/src/measure/rhythm.mjs +81 -0
- package/src/measure/signature.mjs +65 -0
- package/src/measure/states.mjs +240 -0
- package/src/measure/style.mjs +13 -0
- package/src/measure/symbol.mjs +268 -0
- package/src/measure/terms.mjs +153 -0
- package/src/measure/tiles.mjs +20 -0
- package/src/measure/timeline.mjs +621 -0
- package/src/measure/type.mjs +167 -0
- package/src/measure/weight.mjs +18 -0
- package/src/private-strings.mjs +81 -0
- package/src/project/blind.mjs +70 -0
- package/src/project/edits.mjs +154 -0
- package/src/project/gate.mjs +159 -0
- package/src/project/lazuli-md.mjs +497 -0
- package/src/project/log.mjs +329 -0
- package/src/project/match.mjs +112 -0
- package/src/project/records.mjs +380 -0
- package/src/project/tasks.mjs +153 -0
- package/src/report/evaluate.mjs +27 -0
- package/src/report/evaluators/a11y.mjs +206 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +86 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +53 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +94 -0
- package/src/report/evaluators/motion.mjs +273 -0
- package/src/report/evaluators/piece.mjs +69 -0
- package/src/report/evaluators/rhythm.mjs +99 -0
- package/src/report/evaluators/states.mjs +39 -0
- package/src/report/evaluators/style.mjs +35 -0
- package/src/report/evaluators/symbol.mjs +110 -0
- package/src/report/evaluators/terms.mjs +49 -0
- package/src/report/evaluators/type.mjs +163 -0
- package/src/report/evaluators/weight.mjs +51 -0
- package/src/report/evidence.mjs +4 -0
- package/src/report/finding.mjs +11 -0
- package/src/report/format.mjs +215 -0
- package/src/rules/hypotheses.mjs +28 -0
- package/src/rules/load.mjs +256 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +212 -0
- package/templates/LAZULI.md +86 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { readFile, writeFile, access, rename, rm, mkdir } from "node:fs/promises";
|
|
2
|
+
import { isDeepStrictEqual } from "node:util";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { KIT_RULES_DIR, loadKit } from "../rules/load.mjs";
|
|
5
|
+
import { parseLazuliMd } from "../project/lazuli-md.mjs";
|
|
6
|
+
import { writeBreak, writeDecision, LogInputError } from "../project/log.mjs";
|
|
7
|
+
import { RecordError, findCapture, readRecords, withLock, writeFileAtomic } from "../project/records.mjs";
|
|
8
|
+
|
|
9
|
+
export class RecordInputError extends Error {
|
|
10
|
+
constructor(problems) {
|
|
11
|
+
super(problems.join("\n"));
|
|
12
|
+
this.name = "RecordInputError";
|
|
13
|
+
this.code = "RECORD_INPUT";
|
|
14
|
+
this.problems = problems;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const collapse = (s) => String(s ?? "").replace(/\s+/g, " ").trim();
|
|
19
|
+
const norm = (s) => collapse(s).toLowerCase();
|
|
20
|
+
const REFUSED = "Nothing was written: the change would alter other entries.";
|
|
21
|
+
|
|
22
|
+
// The same file readLazuliMd reads: LAZULI.md first, then lazuli.md.
|
|
23
|
+
async function findFile(cwd) {
|
|
24
|
+
for (const name of ["LAZULI.md", "lazuli.md"]) {
|
|
25
|
+
try {
|
|
26
|
+
await access(path.join(cwd, name));
|
|
27
|
+
return path.join(cwd, name);
|
|
28
|
+
} catch {
|
|
29
|
+
// try the next name
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
throw new RecordInputError(["No LAZULI.md here. Run lazuli start-project first."]);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Strict UTF-8 that keeps a BOM in the text, so what is written back is what was read.
|
|
36
|
+
async function readText(file) {
|
|
37
|
+
try {
|
|
38
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(await readFile(file));
|
|
39
|
+
} catch (err) {
|
|
40
|
+
if (err instanceof TypeError) {
|
|
41
|
+
throw new RecordInputError(["LAZULI.md is not UTF-8; nothing was written."]);
|
|
42
|
+
}
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function prepare({ cwd, piece, rule }) {
|
|
48
|
+
const file = await findFile(cwd);
|
|
49
|
+
const text = await readText(file);
|
|
50
|
+
const goals = parseLazuliMd(text).goals;
|
|
51
|
+
const goal = goals.find((g) => norm(g.piece) === norm(piece));
|
|
52
|
+
if (!goal) {
|
|
53
|
+
throw new RecordInputError([`Piece "${piece}" is not a goal in LAZULI.md. Goals: ${goals.length > 0 ? goals.map((g) => g.piece).join(", ") : "none"}.`]);
|
|
54
|
+
}
|
|
55
|
+
const kit = await loadKit({
|
|
56
|
+
dirs: [process.env.LAZULI_RULES_DIR ?? KIT_RULES_DIR],
|
|
57
|
+
projectDir: path.join(cwd, ".lazuli", "rules"),
|
|
58
|
+
});
|
|
59
|
+
if (rule === null) {
|
|
60
|
+
return { file, text, goal, known: null, kit };
|
|
61
|
+
}
|
|
62
|
+
const known = kit.rules.find((r) => norm(r.id) === norm(rule));
|
|
63
|
+
if (!known) {
|
|
64
|
+
throw new RecordInputError([`Unknown rule ${rule}.`]);
|
|
65
|
+
}
|
|
66
|
+
return { file, text, goal, known, kit };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Everything the person had, except the entry being written, must read the same from the new text,
|
|
70
|
+
// and the entry itself must read back with the given values and its other fields as they were.
|
|
71
|
+
function safe(before, after, kind, isTarget, expected) {
|
|
72
|
+
const other = kind === "breaks" ? "decisions" : "breaks";
|
|
73
|
+
if (
|
|
74
|
+
!isDeepStrictEqual(before.goals, after.goals) ||
|
|
75
|
+
!isDeepStrictEqual(before[other], after[other]) ||
|
|
76
|
+
before.mode !== after.mode ||
|
|
77
|
+
before.idea !== after.idea ||
|
|
78
|
+
!isDeepStrictEqual(before[kind].filter((e) => !isTarget(e)), after[kind].filter((e) => !isTarget(e)))
|
|
79
|
+
) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const was = before[kind].filter(isTarget);
|
|
83
|
+
const now = after[kind].filter(isTarget);
|
|
84
|
+
if (now.length !== Math.max(was.length, 1) || !expected(now[0], was[0])) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
return isDeepStrictEqual(was.slice(1), now.slice(1));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Writes next to the file and renames over it, so a failed write never leaves a half file.
|
|
91
|
+
async function replaceFile(file, next) {
|
|
92
|
+
const tmp = `${file}.tmp-${process.pid}`;
|
|
93
|
+
try {
|
|
94
|
+
await writeFile(tmp, next);
|
|
95
|
+
} catch (err) {
|
|
96
|
+
await rm(tmp, { force: true });
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
await rename(tmp, file);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
await rm(tmp, { force: true });
|
|
103
|
+
if (err?.code !== "EPERM" && err?.code !== "EBUSY") {
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
await writeFile(file, next);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function commit({ file, text, next, kind, isTarget, expected, beforeWrite }) {
|
|
111
|
+
const copies = parseLazuliMd(text)[kind].filter(isTarget).length;
|
|
112
|
+
if (!safe(parseLazuliMd(text), parseLazuliMd(next), kind, isTarget, expected)) {
|
|
113
|
+
throw new RecordInputError([REFUSED]);
|
|
114
|
+
}
|
|
115
|
+
if (beforeWrite) {
|
|
116
|
+
await beforeWrite();
|
|
117
|
+
}
|
|
118
|
+
if (next !== text) {
|
|
119
|
+
await replaceFile(file, next);
|
|
120
|
+
}
|
|
121
|
+
return copies;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function guard(fn) {
|
|
125
|
+
try {
|
|
126
|
+
return fn();
|
|
127
|
+
} catch (err) {
|
|
128
|
+
throw err instanceof LogInputError ? new RecordInputError(err.problems) : err;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// .lazuli/breaks.json: when each break was last set by record-break with a status other than kept.
|
|
133
|
+
// { "breaks": { "<rule>|<piece>": "<ISO time>" } }, keys lower case. A file that cannot be read holds no times.
|
|
134
|
+
const stampsFile = (cwd) => path.join(path.resolve(cwd), ".lazuli", "breaks.json");
|
|
135
|
+
const stampKey = (rule, piece) => `${norm(rule)}|${norm(piece)}`;
|
|
136
|
+
|
|
137
|
+
async function readStamps(cwd) {
|
|
138
|
+
try {
|
|
139
|
+
const parsed = JSON.parse((await readFile(stampsFile(cwd), "utf8")).replace(/^/, ""));
|
|
140
|
+
return parsed?.breaks !== null && typeof parsed?.breaks === "object" && !Array.isArray(parsed.breaks) ? parsed.breaks : {};
|
|
141
|
+
} catch {
|
|
142
|
+
return {};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function stampBreak(cwd, rule, piece) {
|
|
147
|
+
const file = stampsFile(cwd);
|
|
148
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
149
|
+
await withLock(file, async () => {
|
|
150
|
+
const breaks = { ...(await readStamps(cwd)), [stampKey(rule, piece)]: new Date().toISOString() };
|
|
151
|
+
await writeFileAtomic(file, JSON.stringify({ breaks }, null, 2));
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// A break becomes kept only after the separate reviewer has seen its declared effect: the latest review of a page
|
|
156
|
+
// of the piece judged the piece, on a capture taken after the break was last set. The builder never confirms it alone.
|
|
157
|
+
async function assertReviewed({ cwd, goals, rule, piece, was, given }) {
|
|
158
|
+
const refuse = (why) => {
|
|
159
|
+
throw new RecordError([`Break ${rule} on ${piece} cannot be kept yet: ${why}`]);
|
|
160
|
+
};
|
|
161
|
+
if (!was) {
|
|
162
|
+
refuse("record it with --status trying first, then review the page with review-design.");
|
|
163
|
+
}
|
|
164
|
+
const changes = ["effect", "check"].filter((k) => given[k] !== undefined && norm(given[k]) !== norm(was[k]));
|
|
165
|
+
if (changes.length > 0) {
|
|
166
|
+
refuse(`the ${changes.join(" and ")} would change. Record the change with --status trying, then review the page again with review-design.`);
|
|
167
|
+
}
|
|
168
|
+
if (was.status === "kept") {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (was.status === "reverted") {
|
|
172
|
+
refuse("it is reverted. Set it to trying again with record-break --status trying, then review the page with review-design.");
|
|
173
|
+
}
|
|
174
|
+
const at = Date.parse((await readStamps(cwd))[stampKey(rule, piece)]);
|
|
175
|
+
if (Number.isNaN(at)) {
|
|
176
|
+
refuse("Lazuli has no record of when this break was last set. Run record-break again with --status trying, then review the page with review-design.");
|
|
177
|
+
}
|
|
178
|
+
const pages = goals.filter((g) => norm(g.piece) === norm(piece) && g.target);
|
|
179
|
+
if (pages.length === 0) {
|
|
180
|
+
refuse(`"${piece}" has no Target, so no review can check the declared effect. Add a Target with set-goals, or revert the break.`);
|
|
181
|
+
}
|
|
182
|
+
for (const g of pages) {
|
|
183
|
+
const review = [...(await readRecords(cwd, g.target))].reverse().find((r) => r.kind === "review");
|
|
184
|
+
const judged = (review?.data?.goals ?? []).find((j) => norm(j.piece) === norm(piece));
|
|
185
|
+
if (!judged) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const manifest = await findCapture(cwd, review.captureId);
|
|
189
|
+
if (manifest && Date.parse(manifest.capturedAt) > at) {
|
|
190
|
+
// The review that counts must not reject the piece.
|
|
191
|
+
if (review.data.verdict === "redo") {
|
|
192
|
+
refuse("the latest review of this piece has verdict redo. Act on it and review again, or revert the break.");
|
|
193
|
+
}
|
|
194
|
+
const missed = ["understand", "feel", "do"].filter((k) => judged[k] === "missed");
|
|
195
|
+
if (missed.length > 0) {
|
|
196
|
+
refuse(`the latest review judged ${missed.join(", ")} missed for this piece. Act on it and review again, or revert the break.`);
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
refuse("no review of this piece was recorded on a capture taken after the break was last set. Run review-design (check-design, capture, first look, record-review), then keep the break or revert it.");
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// What a break on one id must hold before anything is written: a known rule that is not a norm, and for kept a review.
|
|
205
|
+
async function checkBreak({ cwd, rule, piece, status, effect, check }) {
|
|
206
|
+
const base = await prepare({ cwd, piece, rule });
|
|
207
|
+
const { text, goal, known } = base;
|
|
208
|
+
if (known.origin === "norm") {
|
|
209
|
+
throw new RecordInputError([`${known.id} is a norm and cannot be broken. Fix it instead.`]);
|
|
210
|
+
}
|
|
211
|
+
const given = { rule: known.id, piece: goal.piece, status, ...(effect !== undefined ? { effect } : {}), ...(check !== undefined ? { check } : {}) };
|
|
212
|
+
const next = guard(() => writeBreak(text, given));
|
|
213
|
+
const isTarget = (b) => norm(b.rule) === norm(known.id) && norm(b.piece) === norm(goal.piece);
|
|
214
|
+
const keeping = String(status ?? "").trim() === "kept";
|
|
215
|
+
if (keeping) {
|
|
216
|
+
const md = parseLazuliMd(text);
|
|
217
|
+
await assertReviewed({ cwd, goals: md.goals, rule: known.id, piece: goal.piece, was: md.breaks.find(isTarget), given });
|
|
218
|
+
}
|
|
219
|
+
return { ...base, given, next, isTarget, keeping };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function breakOne(args) {
|
|
223
|
+
const { cwd, status } = args;
|
|
224
|
+
const { file, text, goal, known, given, next, isTarget, keeping } = await checkBreak(args);
|
|
225
|
+
const copies = await commit({
|
|
226
|
+
file,
|
|
227
|
+
text,
|
|
228
|
+
next,
|
|
229
|
+
kind: "breaks",
|
|
230
|
+
isTarget,
|
|
231
|
+
expected: (now, was) =>
|
|
232
|
+
["effect", "check", "status"].every((k) => (k in given ? norm(now[k]) === norm(given[k]) : was !== undefined && now[k] === was[k])) &&
|
|
233
|
+
(was === undefined || now.piece === was.piece),
|
|
234
|
+
// Stamped before LAZULI.md is written: a write that fails after it only asks for a newer review.
|
|
235
|
+
beforeWrite: keeping ? undefined : () => stampBreak(cwd, known.id, goal.piece),
|
|
236
|
+
});
|
|
237
|
+
const notes = copies > 1 ? [`Another entry for ${known.id} on ${goal.piece} was left as it was.`] : [];
|
|
238
|
+
return { path: file, rule: known.id, piece: goal.piece, status, notes };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// One entry per id of a comma list. Every id is checked before the first is written, so a bad id writes nothing.
|
|
242
|
+
// A rule that shares its measure with kit rules outside the list reports the same finding under them: they are named.
|
|
243
|
+
export async function recordBreak({ cwd = process.cwd(), rule, piece, effect, check, status }) {
|
|
244
|
+
const ids = [...new Set(String(rule ?? "").split(",").map((s) => s.trim()).filter((s) => s !== ""))];
|
|
245
|
+
if (ids.length === 0) {
|
|
246
|
+
throw new RecordInputError(["--rule needs a rule id."]);
|
|
247
|
+
}
|
|
248
|
+
const args = (id) => ({ cwd, rule: id, piece, effect, check, status });
|
|
249
|
+
const checked = [];
|
|
250
|
+
for (const id of ids) {
|
|
251
|
+
checked.push(await checkBreak(args(id)));
|
|
252
|
+
}
|
|
253
|
+
const entries = [];
|
|
254
|
+
for (const id of ids) {
|
|
255
|
+
entries.push(await breakOne(args(id)));
|
|
256
|
+
}
|
|
257
|
+
const kit = checked[0].kit;
|
|
258
|
+
const listed = new Set(entries.map((e) => norm(e.rule)));
|
|
259
|
+
const notes = entries.flatMap((e) => e.notes);
|
|
260
|
+
for (const e of entries) {
|
|
261
|
+
const measure = kit.rules.find((r) => norm(r.id) === norm(e.rule))?.measure;
|
|
262
|
+
const others = measure === undefined ? [] : kit.rules.filter((r) => r.measure === measure && r.origin !== "norm" && !listed.has(norm(r.id))).map((r) => r.id);
|
|
263
|
+
if (others.length > 0) {
|
|
264
|
+
notes.push(`${others.join(", ")} share the measure of ${e.rule} (${measure}), so the same finding is reported under them; add them to --rule to break them too.`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return { path: entries[0].path, rule: entries[0].rule, rules: entries.map((e) => e.rule), piece: entries[0].piece, status, entries, notes };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// --rule of a decision: "<id> followed|broken|accepted", or "none" for a decision tied to no rule.
|
|
271
|
+
export async function recordDecision({ cwd = process.cwd(), decision, piece, goal: served, rule, measure }) {
|
|
272
|
+
const written = String(rule ?? "").trim();
|
|
273
|
+
const none = /^none$/i.test(written);
|
|
274
|
+
const match = /^(\S+)\s+(followed|broken|accepted)$/i.exec(written);
|
|
275
|
+
if (!match && !none) {
|
|
276
|
+
throw new RecordInputError(['--rule needs a rule id and followed, broken or accepted, for example "PA4 followed", or none.']);
|
|
277
|
+
}
|
|
278
|
+
const { file, text, goal, known } = await prepare({ cwd, piece, rule: none ? null : match[1] });
|
|
279
|
+
const action = none ? "none" : match[2].toLowerCase();
|
|
280
|
+
if (action === "accepted" && known.origin === "norm") {
|
|
281
|
+
throw new RecordInputError([`${known.id} is a norm and cannot be accepted. Fix it instead.`]);
|
|
282
|
+
}
|
|
283
|
+
const ruleId = none ? "none" : known.id;
|
|
284
|
+
const given = { decision, piece: goal.piece, goal: served, rule: none ? "none" : `${known.id} ${action}`, measure };
|
|
285
|
+
const next = guard(() => writeDecision(text, given));
|
|
286
|
+
const copies = await commit({
|
|
287
|
+
file,
|
|
288
|
+
text,
|
|
289
|
+
next,
|
|
290
|
+
kind: "decisions",
|
|
291
|
+
isTarget: (d) => norm(d.decision) === norm(decision) && norm(d.piece) === norm(goal.piece),
|
|
292
|
+
expected: (now) => norm(now.goal) === norm(served) && now.rule === ruleId && now.action === action && norm(now.measure) === norm(measure),
|
|
293
|
+
});
|
|
294
|
+
const notes = copies > 1 ? [`Another entry for decision "${decision}" on ${goal.piece} was left as it was.`] : [];
|
|
295
|
+
return { path: file, decision, piece: goal.piece, notes };
|
|
296
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { MAX_TEXT_CHARS, RecordError, appendRecord, recordFilePath, withLock, writeFileAtomic } from "../project/records.mjs";
|
|
4
|
+
import { LEAVES, STOP_CHECKOUT, STOP_CONTROL, STOP_PAGE, findTask, flowStopUrl, isLocalTarget, noTaskMessage } from "../project/tasks.mjs";
|
|
5
|
+
import { readAnswerFile } from "./record-answers.mjs";
|
|
6
|
+
|
|
7
|
+
// One answer of the tester per step of a simulated task. Everything is checked before anything is written;
|
|
8
|
+
// a task that ends gets one task record, made of counts from the step maps and the answers, never a score.
|
|
9
|
+
|
|
10
|
+
const OUTCOMES = ["continue", "done", "stuck"];
|
|
11
|
+
const isText = (v) => typeof v === "string" && v.trim() !== "";
|
|
12
|
+
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
13
|
+
const oneLine = (s) => String(s ?? "").replace(/\s+/g, " ").trim();
|
|
14
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
15
|
+
const readJson = async (file) => JSON.parse(await fs.readFile(file, "utf8"));
|
|
16
|
+
const mapFile = (dir, n) => path.join(dir, `step-${n}.json`);
|
|
17
|
+
// Text the page controls (a link's address, an element's name, a reason naming an origin) is clipped to the
|
|
18
|
+
// longest text a record keeps, marked as clipped, so the page can never make the record refuse the close.
|
|
19
|
+
export const CLIPPED = " [clipped]";
|
|
20
|
+
export const clip = (text) => (typeof text === "string" && text.length > MAX_TEXT_CHARS ? `${text.slice(0, MAX_TEXT_CHARS - CLIPPED.length)}${CLIPPED}` : text);
|
|
21
|
+
|
|
22
|
+
// The reason a stop page gives: the one map-page wrote on its actions or in its note.
|
|
23
|
+
function pageReason(map) {
|
|
24
|
+
const fromAction = map.actions?.find((a) => isText(a?.reason))?.reason;
|
|
25
|
+
if (fromAction) return fromAction;
|
|
26
|
+
for (const n of map.notes ?? []) {
|
|
27
|
+
const m = /^Every action is stop: (.+)\.$/.exec(String(n));
|
|
28
|
+
if (m) return m[1];
|
|
29
|
+
}
|
|
30
|
+
return STOP_PAGE;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// The answer's problems, each naming the step. On a stop page the tester's choice changes nothing, so
|
|
34
|
+
// only taskId and step are required there; any other field given is still checked.
|
|
35
|
+
function checkAnswer(data, map, n) {
|
|
36
|
+
const problems = [];
|
|
37
|
+
const say = (text) => problems.push(`Step ${n}: ${text}`);
|
|
38
|
+
const relaxed = map.stopPage === true;
|
|
39
|
+
const ids = new Set((map.actions ?? []).map((a) => a.id));
|
|
40
|
+
const has = (k) => data[k] !== undefined;
|
|
41
|
+
for (const k of ["tester", "why"]) {
|
|
42
|
+
if ((!relaxed || has(k)) && !isText(data[k])) say(`${k} is required.`);
|
|
43
|
+
}
|
|
44
|
+
const outcomeOk = OUTCOMES.includes(data.outcome);
|
|
45
|
+
if ((!relaxed || has("outcome")) && !outcomeOk) say(`outcome must be one of ${OUTCOMES.join(", ")}.`);
|
|
46
|
+
if (typeof data.chose === "string") {
|
|
47
|
+
if (!ids.has(data.chose)) say(`chose ${JSON.stringify(data.chose)} is not in the map of step ${n}.`);
|
|
48
|
+
} else if (data.chose === null || (relaxed && !has("chose"))) {
|
|
49
|
+
if (!relaxed && outcomeOk && data.outcome === "continue") say("chose is null, which only a done or stuck outcome allows.");
|
|
50
|
+
} else {
|
|
51
|
+
say(`chose must be an id of the map of step ${n}, or null.`);
|
|
52
|
+
}
|
|
53
|
+
if (!relaxed || has("considered")) {
|
|
54
|
+
if (!Array.isArray(data.considered) || !data.considered.every((c) => typeof c === "string")) {
|
|
55
|
+
say(`considered must be a list of ids of the map of step ${n}.`);
|
|
56
|
+
} else {
|
|
57
|
+
for (const c of data.considered.filter((x) => !ids.has(x))) say(`considered ${JSON.stringify(c)} is not in the map of step ${n}.`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if ((!relaxed || has("hesitated")) && typeof data.hesitated !== "boolean") {
|
|
61
|
+
say("hesitated must be true or false.");
|
|
62
|
+
} else if (data.hesitated === true && !isText(data.hesitation)) {
|
|
63
|
+
say("hesitation is required when hesitated is true.");
|
|
64
|
+
}
|
|
65
|
+
if (has("hesitation") && typeof data.hesitation !== "string") say("hesitation must be text.");
|
|
66
|
+
if (data.outcome === "done" || data.outcome === "stuck") {
|
|
67
|
+
if (!isText(data.remembered)) say(`remembered is required when the outcome is ${data.outcome}.`);
|
|
68
|
+
} else if (has("remembered") && typeof data.remembered !== "string") {
|
|
69
|
+
say("remembered must be text.");
|
|
70
|
+
}
|
|
71
|
+
return problems;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// The fields task.json keeps of an answer, and nothing else.
|
|
75
|
+
function cleanAnswer(data, taskId, n) {
|
|
76
|
+
const text = (v) => (typeof v === "string" ? v : "");
|
|
77
|
+
return {
|
|
78
|
+
taskId,
|
|
79
|
+
step: n,
|
|
80
|
+
tester: text(data.tester),
|
|
81
|
+
chose: typeof data.chose === "string" ? data.chose : null,
|
|
82
|
+
considered: Array.isArray(data.considered) ? [...data.considered] : [],
|
|
83
|
+
hesitated: data.hesitated === true,
|
|
84
|
+
hesitation: text(data.hesitation),
|
|
85
|
+
why: text(data.why),
|
|
86
|
+
outcome: OUTCOMES.includes(data.outcome) ? data.outcome : null,
|
|
87
|
+
remembered: text(data.remembered),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// How the step ends. A stop page closes as stopped whatever was chosen; the tester's done or stuck
|
|
92
|
+
// closes as itself; a continue goes where the chosen action allows.
|
|
93
|
+
function decide(map, answer, taskId) {
|
|
94
|
+
if (map.stopPage === true) {
|
|
95
|
+
return { closed: { outcome: "stopped", reason: pageReason(map) }, next: null };
|
|
96
|
+
}
|
|
97
|
+
if (answer.outcome === "done" || answer.outcome === "stuck") {
|
|
98
|
+
return { closed: { outcome: answer.outcome, reason: null }, next: null };
|
|
99
|
+
}
|
|
100
|
+
const action = map.actions.find((a) => a.id === answer.chose);
|
|
101
|
+
if (action.act === "follow") {
|
|
102
|
+
// map-page never marks such a link follow; checked again here so a walk never opens one.
|
|
103
|
+
if (typeof action.href === "string" && !isLocalTarget(action.href) && flowStopUrl(action.href)) {
|
|
104
|
+
return { closed: { outcome: "stopped", reason: STOP_CHECKOUT }, next: null };
|
|
105
|
+
}
|
|
106
|
+
// The next step takes its address from the map, so no page text goes into the printed line.
|
|
107
|
+
return { closed: null, next: `map-page --task ${taskId}` };
|
|
108
|
+
}
|
|
109
|
+
if (action.act === "act") {
|
|
110
|
+
// map-page marks a control act only on a local page; checked again so the act line never names one elsewhere.
|
|
111
|
+
if (map.local !== true) {
|
|
112
|
+
return { closed: { outcome: "stopped", reason: STOP_CONTROL }, next: null };
|
|
113
|
+
}
|
|
114
|
+
const name = oneLine(action.name);
|
|
115
|
+
const label = name === "" ? action.id : `${JSON.stringify(name)} (${action.id})`;
|
|
116
|
+
return { closed: null, next: `${label} is a ${action.kind} on a local page: act on it in your own browser on the local app, or open the screen it leads to by its own URL, then run map-page <url> --task ${taskId}.` };
|
|
117
|
+
}
|
|
118
|
+
if (action.act === "leave") {
|
|
119
|
+
return { closed: { outcome: "left", reason: action.reason ?? LEAVES }, next: null };
|
|
120
|
+
}
|
|
121
|
+
return { closed: { outcome: "stopped", reason: action.reason ?? null }, next: null };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// The task record: one entry per mapped step, counted from that step's map and its answer. The tester is
|
|
125
|
+
// the latest answer that names one (a stop page may close on a bare answer, or with none when map-page
|
|
126
|
+
// closes it); remembered is the last step's. piece and task come from LAZULI.md and are clipped like page text.
|
|
127
|
+
function taskData(m, maps, answers, closed) {
|
|
128
|
+
const tester = [...answers].reverse().find((a) => isText(a?.tester))?.tester ?? "";
|
|
129
|
+
const last = answers.find((a) => a?.step === m.steps.at(-1)?.n);
|
|
130
|
+
const steps = m.steps.map((s, i) => {
|
|
131
|
+
const map = maps[i];
|
|
132
|
+
const a = answers.find((x) => x?.step === s.n) ?? cleanAnswer({}, m.taskId, s.n);
|
|
133
|
+
const action = typeof a.chose === "string" ? map.actions.find((x) => x.id === a.chose) : null;
|
|
134
|
+
return {
|
|
135
|
+
step: s.n,
|
|
136
|
+
target: clip(s.target),
|
|
137
|
+
offered: map.offered?.firstView ?? 0,
|
|
138
|
+
total: map.offered?.total ?? 0,
|
|
139
|
+
chose: a.chose,
|
|
140
|
+
chosenName: action ? clip(oneLine(action.name)) : null,
|
|
141
|
+
scrolled: action ? action.firstView !== true : false,
|
|
142
|
+
considered: a.considered,
|
|
143
|
+
hesitated: a.hesitated,
|
|
144
|
+
hesitation: a.hesitation,
|
|
145
|
+
why: a.why,
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
const counts = { steps: steps.length, hesitations: steps.filter((s) => s.hesitated).length, scrolls: steps.filter((s) => s.scrolled).length };
|
|
149
|
+
return { taskId: m.taskId, piece: clip(m.piece), task: clip(m.task), tester, outcome: closed.outcome, reason: closed.reason === null ? null : clip(closed.reason), remembered: last?.remembered ?? "", steps, counts };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function taskSummary(data) {
|
|
153
|
+
const hes = data.steps.filter((s) => s.hesitated);
|
|
154
|
+
const said = hes.length === 0 ? plural(0, "hesitation") : `${plural(hes.length, "hesitation")} (${hes.map((s) => `step ${s.step}: "${oneLine(s.hesitation)}"`).join("; ")})`;
|
|
155
|
+
const remembered = isText(data.remembered) ? `"${oneLine(data.remembered)}"` : "nothing";
|
|
156
|
+
return `Task "${oneLine(data.task)}" ${data.outcome} after ${plural(data.counts.steps, "step")}; ${said}; choices in the first view per step: ${data.steps.map((s) => s.offered).join(", ")}; remembered: ${remembered}.`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// This task's stored record, when the records file already holds one (a run that stopped between the
|
|
160
|
+
// record and task.json). An unreadable file is left to appendRecord, which refuses it.
|
|
161
|
+
async function storedRecord(file, taskId) {
|
|
162
|
+
try {
|
|
163
|
+
const parsed = await readJson(file);
|
|
164
|
+
return (Array.isArray(parsed?.records) && parsed.records.find((r) => r?.kind === "task" && r.taskId === taskId)) || null;
|
|
165
|
+
} catch {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Closes a task: the one task record first, then task.json with its answers and the outcome. record-task
|
|
171
|
+
// and map-page (a next screen that cannot be mapped or opened) both close through here, under the task's
|
|
172
|
+
// lock, with m read again under it. A record already stored wins: the summary and task.json follow it.
|
|
173
|
+
export async function closeTask({ root, m, answers, closed }) {
|
|
174
|
+
const maps = [];
|
|
175
|
+
for (const st of m.steps) maps.push(await readJson(mapFile(m.dir, st.n)));
|
|
176
|
+
const recordsFile = recordFilePath(root, m.folderKey);
|
|
177
|
+
const stored = await storedRecord(recordsFile, m.taskId);
|
|
178
|
+
let data = stored?.data;
|
|
179
|
+
if (!data) {
|
|
180
|
+
data = taskData(m, maps, answers, closed);
|
|
181
|
+
await appendRecord({ cwd: root, kind: "task", data });
|
|
182
|
+
}
|
|
183
|
+
const final = { outcome: data.outcome, reason: data.reason ?? null };
|
|
184
|
+
const taskFile = path.join(m.dir, "task.json");
|
|
185
|
+
const raw = await readJson(taskFile);
|
|
186
|
+
await writeFileAtomic(taskFile, `${JSON.stringify({ ...raw, answers, closed: { ...final, at: new Date().toISOString() } }, null, 2)}\n`);
|
|
187
|
+
return { closed: final, summary: taskSummary(data), path: recordsFile };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export async function recordTaskStep({ cwd = process.cwd(), file }) {
|
|
191
|
+
const root = path.resolve(cwd);
|
|
192
|
+
const data = await readAnswerFile(root, file);
|
|
193
|
+
if (!isObject(data)) {
|
|
194
|
+
throw new RecordError(["The answer must be a JSON object."]);
|
|
195
|
+
}
|
|
196
|
+
if (!isText(data.taskId)) {
|
|
197
|
+
throw new RecordError(["taskId is required."]);
|
|
198
|
+
}
|
|
199
|
+
const found = await findTask(root, data.taskId);
|
|
200
|
+
if (!found) {
|
|
201
|
+
throw new RecordError([noTaskMessage(data.taskId)]);
|
|
202
|
+
}
|
|
203
|
+
const taskFile = path.join(found.dir, "task.json");
|
|
204
|
+
// The same lock map-page takes, so a step is never mapped and answered at once.
|
|
205
|
+
return withLock(taskFile, async () => {
|
|
206
|
+
const m = await findTask(root, data.taskId);
|
|
207
|
+
if (!m) {
|
|
208
|
+
throw new RecordError([noTaskMessage(data.taskId)]);
|
|
209
|
+
}
|
|
210
|
+
const n = m.steps.length;
|
|
211
|
+
const answers = Array.isArray(m.answers) ? m.answers : [];
|
|
212
|
+
const s = data.step;
|
|
213
|
+
if (!Number.isInteger(s)) {
|
|
214
|
+
throw new RecordError([`step must be the number of the step answered; the last mapped step of task ${m.taskId} is ${n}.`]);
|
|
215
|
+
}
|
|
216
|
+
if (m.closed) {
|
|
217
|
+
throw new RecordError([`Task ${m.taskId} is closed (${m.closed.outcome}) after step ${n}; step ${s} takes no answer.`]);
|
|
218
|
+
}
|
|
219
|
+
if (s < 1 || s > n) {
|
|
220
|
+
throw new RecordError([`Step ${s} of task ${m.taskId} was never mapped; the last mapped step is ${n}.`]);
|
|
221
|
+
}
|
|
222
|
+
if (answers.some((a) => a?.step === s)) {
|
|
223
|
+
throw new RecordError([`Step ${s} of task ${m.taskId} already has an answer.`]);
|
|
224
|
+
}
|
|
225
|
+
if (s !== n) {
|
|
226
|
+
throw new RecordError([`Step ${s} of task ${m.taskId} is not the last mapped step (${n}).`]);
|
|
227
|
+
}
|
|
228
|
+
const map = await readJson(mapFile(m.dir, n));
|
|
229
|
+
const problems = checkAnswer(data, map, n);
|
|
230
|
+
const answer = cleanAnswer(data, m.taskId, n);
|
|
231
|
+
const texts = [answer.tester, answer.chose, ...answer.considered, answer.hesitation, answer.why, answer.remembered];
|
|
232
|
+
if (texts.some((t) => typeof t === "string" && t.length > MAX_TEXT_CHARS)) {
|
|
233
|
+
problems.push(`Step ${n}: a text is longer than ${MAX_TEXT_CHARS} characters.`);
|
|
234
|
+
}
|
|
235
|
+
if (problems.length > 0) {
|
|
236
|
+
throw new RecordError(problems);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const { closed, next } = decide(map, answer, m.taskId);
|
|
240
|
+
const raw = await readJson(taskFile);
|
|
241
|
+
const allAnswers = [...answers, answer];
|
|
242
|
+
if (!closed) {
|
|
243
|
+
await writeFileAtomic(taskFile, `${JSON.stringify({ ...raw, answers: allAnswers }, null, 2)}\n`);
|
|
244
|
+
return { taskId: m.taskId, step: n, closed: null, next, summary: null, path: null };
|
|
245
|
+
}
|
|
246
|
+
// The record first: a failed append leaves task.json as it was, so the same answer can be sent again.
|
|
247
|
+
const done = await closeTask({ root, m, answers: allAnswers, closed });
|
|
248
|
+
return { taskId: m.taskId, step: n, next: null, ...done };
|
|
249
|
+
});
|
|
250
|
+
}
|