@ouro.bot/cli 0.1.0-alpha.1430 → 0.1.0-alpha.1432
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.json +12 -0
- package/deploy/unraid/sanctuary.ouro/bundle-meta.json +1 -1
- package/deploy/unraid/sanctuary.xml +1 -1
- package/dist/eval/interview-guest-entry.js +17 -0
- package/dist/eval/interviews/allocation.js +64 -0
- package/dist/eval/interviews/guest/arms.js +173 -0
- package/dist/eval/interviews/guest/ax/ax-command.js +792 -0
- package/dist/eval/interviews/guest/ax/effects.js +51 -0
- package/dist/eval/interviews/guest/ax/exit-codes.js +51 -0
- package/dist/eval/interviews/guest/ax/help.js +181 -0
- package/dist/eval/interviews/guest/ax/mcp.js +246 -0
- package/dist/eval/interviews/guest/ax/outcome.js +211 -0
- package/dist/eval/interviews/guest/ax/output.js +53 -0
- package/dist/eval/interviews/guest/ax/precise-copy.js +200 -0
- package/dist/eval/interviews/guest/ax/problem.js +95 -0
- package/dist/eval/interviews/guest/ax/voice.js +83 -0
- package/dist/eval/interviews/guest/ax/wait.js +158 -0
- package/dist/eval/interviews/guest/books-core.js +54 -0
- package/dist/eval/interviews/guest/cli-shell.js +267 -0
- package/dist/eval/interviews/guest/loop.js +78 -0
- package/dist/eval/interviews/guest/operations.js +108 -0
- package/dist/eval/interviews/guest/run.js +62 -0
- package/dist/eval/interviews/latch.js +122 -0
- package/dist/eval/interviews/protocol.js +77 -0
- package/dist/eval/remote-entry.js +2 -0
- package/dist/eval/sandbox/docker.js +23 -6
- package/dist/eval/sandbox/remote.js +2 -2
- package/dist/heart/daemon/cli-exec.js +14 -1
- package/dist/heart/versioning/self-update.js +14 -1
- package/dist/heart/versioning/update-state.js +1 -1
- package/dist/nerves/coverage/file-completeness.js +3 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OUTCOME_VERSION = exports.OUTCOME_STATES = exports.DEFAULT_OUTCOME_EXITS = void 0;
|
|
4
|
+
exports.isGenericAudience = isGenericAudience;
|
|
5
|
+
exports.isOutcome = isOutcome;
|
|
6
|
+
exports.nextStep = nextStep;
|
|
7
|
+
exports.outcome = outcome;
|
|
8
|
+
exports.outcomeExit = outcomeExit;
|
|
9
|
+
exports.outcomeText = outcomeText;
|
|
10
|
+
exports.validateOutcome = validateOutcome;
|
|
11
|
+
// The export list comes first because the shadcn CLI drops comments before a file's
|
|
12
|
+
// first statement, which would take the JSDoc below with it.
|
|
13
|
+
const effects_1 = require("./effects");
|
|
14
|
+
const precise_copy_1 = require("./precise-copy");
|
|
15
|
+
const problem_1 = require("./problem");
|
|
16
|
+
/**
|
|
17
|
+
* The outcome of an action: what became of it, who can see it, whether that was checked, how to find it again and what
|
|
18
|
+
* to run next, in one shape over a CLI's `--json`, an HTTP route and an MCP tool.
|
|
19
|
+
*/
|
|
20
|
+
const OUTCOME_VERSION = "outcome/1";
|
|
21
|
+
exports.OUTCOME_VERSION = OUTCOME_VERSION;
|
|
22
|
+
/**
|
|
23
|
+
* What became of an action.
|
|
24
|
+
* - `done`: it happened; the postcondition says how that is known.
|
|
25
|
+
* - `queued`: accepted, not performed yet; `receipt.id` and `next` follow it.
|
|
26
|
+
* - `planned`: a dry run; nothing happened.
|
|
27
|
+
* - `needs-human`: only a person can take the next step; `human` says why and how to resume.
|
|
28
|
+
* - `uncertain`: it may or may not have happened; reconcile before any retry.
|
|
29
|
+
* - `refused`: checked and not done, with the reason; nothing happened.
|
|
30
|
+
* - `failed`: tried and proven not done.
|
|
31
|
+
*/
|
|
32
|
+
const OUTCOME_STATES = Object.freeze(["done", "queued", "planned", "needs-human", "uncertain", "refused", "failed"]);
|
|
33
|
+
exports.OUTCOME_STATES = OUTCOME_STATES;
|
|
34
|
+
/** An exact next step. */
|
|
35
|
+
function nextStep(command, why) {
|
|
36
|
+
return { command, why };
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Builds an outcome with honest defaults: private, only you, nothing checked, no receipt and no next steps. Pass what is
|
|
40
|
+
* known; {@link validateOutcome} says what is missing.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* outcome({ action: "message.send", effect: "others", audience: { only: false, who: 'everyone in "Design sync"' },
|
|
44
|
+
* state: "queued", summary: 'Queued for "Design sync".',
|
|
45
|
+
* receipt: { id: "a1b2c3", at: new Date().toISOString(), by: "agent:planner", why: "Jo asked" },
|
|
46
|
+
* next: [nextStep("tool outcome a1b2c3 --timeout 60s", "wait until it is sent")] });
|
|
47
|
+
*
|
|
48
|
+
* @claim "Builds an outcome with honest defaults" registry/ouro/ax/outcome.test.mts verify:testExists(registry/ouro/ax/outcome.test.mts::outcome fills honest defaults: private, only you, nothing checked, no receipt; an error gets its message)
|
|
49
|
+
*/
|
|
50
|
+
function outcome(fields) {
|
|
51
|
+
const { action, state, summary, effect = "private", audience = effects_1.ONLY_YOU, target = null, postcondition, receipt = null, next = [], error, human } = fields;
|
|
52
|
+
return {
|
|
53
|
+
ax: OUTCOME_VERSION,
|
|
54
|
+
action,
|
|
55
|
+
effect,
|
|
56
|
+
audience: { ...audience },
|
|
57
|
+
target,
|
|
58
|
+
state,
|
|
59
|
+
summary,
|
|
60
|
+
postcondition: postcondition ?? { checked: false, holds: null, how: "not checked" },
|
|
61
|
+
receipt,
|
|
62
|
+
next,
|
|
63
|
+
...(error ? { error: { ...error, message: (0, problem_1.errorMessage)(error) } } : {}),
|
|
64
|
+
...(human ? { human } : {}),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
68
|
+
const isText = (v) => typeof v === "string" && v.trim().length > 0;
|
|
69
|
+
const isStep = (v) => isRecord(v) && isText(v["command"]) && isText(v["why"]);
|
|
70
|
+
/**
|
|
71
|
+
* Whether an audience's `who` names no one in particular ("other people", "someone", "everyone"), so an agent reading
|
|
72
|
+
* the JSON learns less than one reading the text.
|
|
73
|
+
*
|
|
74
|
+
* @claim "names no one in particular" registry/ouro/ax/outcome.test.mts verify:testExists(registry/ouro/ax/outcome.test.mts::an action for other people that names no one is warned about)
|
|
75
|
+
*/
|
|
76
|
+
function isGenericAudience(who) {
|
|
77
|
+
return /^(?:other people|others|people|someone|somebody|anyone|anybody|everyone|everybody|the audience|recipients|users?)\.?$/i.test(who.trim());
|
|
78
|
+
}
|
|
79
|
+
/** Whether a value is an outcome of this version (its shape only; {@link validateOutcome} checks its meaning). */
|
|
80
|
+
function isOutcome(v) {
|
|
81
|
+
return isRecord(v) && v["ax"] === OUTCOME_VERSION && OUTCOME_STATES.includes(v["state"]);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Checks an outcome's meaning, not only its shape:
|
|
85
|
+
* - `done` never rests on a postcondition that failed; one not checked is a warning.
|
|
86
|
+
* - `refused` and `failed` carry an error and never claim the effect holds.
|
|
87
|
+
* - `queued` and `uncertain` carry a receipt and a next step, and `uncertain` cannot claim to know.
|
|
88
|
+
* - an effect other people see carries a receipt with a why, unless nothing happened, and an audience that is not only you.
|
|
89
|
+
* - `needs-human` carries the reason and the step that resumes.
|
|
90
|
+
* - a next step is an exact command with its why.
|
|
91
|
+
*
|
|
92
|
+
* @claim "never claim the effect holds" registry/ouro/ax/outcome.test.mts verify:testExists(registry/ouro/ax/outcome.test.mts::the rules: done, queued, uncertain, refused, failed, needs-human, planned and effects other people see)
|
|
93
|
+
*/
|
|
94
|
+
function validateOutcome(v) {
|
|
95
|
+
const errors = [];
|
|
96
|
+
const warnings = [];
|
|
97
|
+
if (!isOutcome(v))
|
|
98
|
+
return { ok: false, errors: [`not an ${OUTCOME_VERSION} envelope: ax must be "${OUTCOME_VERSION}" and state one of ${OUTCOME_STATES.join(", ")}`], warnings };
|
|
99
|
+
if (!isText(v.action))
|
|
100
|
+
errors.push("action is empty");
|
|
101
|
+
if (v.effect !== "private" && v.effect !== "others")
|
|
102
|
+
errors.push('effect is "private" or "others"');
|
|
103
|
+
if (!isRecord(v.audience) || typeof v.audience.only !== "boolean" || !isText(v.audience.who))
|
|
104
|
+
errors.push("audience needs only (true or false) and who (words)");
|
|
105
|
+
else if (v.effect === "others" && v.audience.only)
|
|
106
|
+
errors.push("an effect other people see cannot have an audience of only you");
|
|
107
|
+
else if (v.effect === "others" && isGenericAudience(v.audience.who))
|
|
108
|
+
warnings.push(`audience.who is "${v.audience.who}", which names no one: say who will see it, as the text does (everyone in "Design sync", Jo Park)`);
|
|
109
|
+
if (v.target !== null && !(isRecord(v.target) && isText(v.target.kind) && isText(v.target.id)))
|
|
110
|
+
errors.push("target is null or { kind, id }");
|
|
111
|
+
if (!isText(v.summary))
|
|
112
|
+
errors.push("summary is empty");
|
|
113
|
+
const p = v.postcondition;
|
|
114
|
+
const pOk = isRecord(p) && typeof p["checked"] === "boolean" && (p["holds"] === null || typeof p["holds"] === "boolean") && isText(p["how"]);
|
|
115
|
+
if (!pOk)
|
|
116
|
+
errors.push("postcondition needs checked, holds (true, false or null) and how");
|
|
117
|
+
const holds = pOk ? p["holds"] : null;
|
|
118
|
+
if (!Array.isArray(v.next) || !v.next.every(isStep))
|
|
119
|
+
errors.push("every next step needs a command and a why");
|
|
120
|
+
const r = v.receipt;
|
|
121
|
+
const rOk = r === null || (isRecord(r) && isText(r["id"]) && isText(r["at"]) && isText(r["by"]) && (r["why"] === null || typeof r["why"] === "string"));
|
|
122
|
+
if (!rOk)
|
|
123
|
+
errors.push("receipt needs id, at, by and why (or null)");
|
|
124
|
+
const why = rOk && r !== null ? r["why"] : null;
|
|
125
|
+
const hasReceipt = rOk && r !== null;
|
|
126
|
+
const hasNext = Array.isArray(v.next) && v.next.length > 0;
|
|
127
|
+
const e = v.error;
|
|
128
|
+
const hasError = isRecord(e) && isText(e["code"]) && isText(e["message"]);
|
|
129
|
+
if (e !== undefined && !hasError)
|
|
130
|
+
errors.push("error needs a code and a message");
|
|
131
|
+
if (isRecord(e) && (0, precise_copy_1.preciseFailureProblem)(e))
|
|
132
|
+
errors.push(`error.${(0, precise_copy_1.preciseFailureProblem)(e)}`);
|
|
133
|
+
if (isRecord(v.human) && (0, precise_copy_1.preciseFailureProblem)(v.human))
|
|
134
|
+
errors.push(`human.${(0, precise_copy_1.preciseFailureProblem)(v.human)}`);
|
|
135
|
+
switch (v.state) {
|
|
136
|
+
case "done":
|
|
137
|
+
if (holds === false)
|
|
138
|
+
errors.push("done, but the postcondition does not hold");
|
|
139
|
+
if (pOk && p["checked"] === false)
|
|
140
|
+
warnings.push("done without an independent check: say what it rests on in postcondition.how");
|
|
141
|
+
if (hasError)
|
|
142
|
+
errors.push("done carries an error");
|
|
143
|
+
break;
|
|
144
|
+
case "queued":
|
|
145
|
+
case "uncertain":
|
|
146
|
+
if (!hasReceipt)
|
|
147
|
+
errors.push(`${v.state} needs a receipt, so the request can be found again`);
|
|
148
|
+
if (!hasNext)
|
|
149
|
+
errors.push(`${v.state} needs a next step`);
|
|
150
|
+
if (v.state === "uncertain" && holds !== null)
|
|
151
|
+
errors.push("uncertain, but the postcondition claims to know");
|
|
152
|
+
break;
|
|
153
|
+
case "refused":
|
|
154
|
+
case "failed":
|
|
155
|
+
if (!hasError)
|
|
156
|
+
errors.push(`${v.state} needs an error with a code and a message`);
|
|
157
|
+
if (holds === true)
|
|
158
|
+
errors.push(`${v.state}, but the postcondition says the effect holds`);
|
|
159
|
+
if (!hasNext && !(hasError && isRecord(e) && isText(e["fix"])))
|
|
160
|
+
warnings.push(`${v.state} without a next step or a fix: say what to do instead`);
|
|
161
|
+
break;
|
|
162
|
+
case "needs-human":
|
|
163
|
+
if (!isRecord(v.human) || !isText(v.human.reason) || !isStep(v.human.resume))
|
|
164
|
+
errors.push("needs-human needs human.reason and human.resume");
|
|
165
|
+
break;
|
|
166
|
+
case "planned":
|
|
167
|
+
if (holds === true)
|
|
168
|
+
errors.push("planned, but the postcondition says the effect holds");
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
if (v.effect === "others" && (v.state === "done" || v.state === "queued" || v.state === "uncertain") && !isText(why))
|
|
172
|
+
errors.push("an effect other people see needs a receipt with a why");
|
|
173
|
+
return { ok: errors.length === 0, errors, warnings };
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The default: what was done, queued, planned or handed to the person exits `ok` (the outcome says which); a refusal
|
|
177
|
+
* exits `usage`; a failure or an uncertain result exits `error`. A tool with its own codes (such as "possibly done")
|
|
178
|
+
* passes its own names.
|
|
179
|
+
*/
|
|
180
|
+
const DEFAULT_OUTCOME_EXITS = Object.freeze({ done: "ok", queued: "ok", planned: "ok", "needs-human": "ok", uncertain: "error", refused: "usage", failed: "error" });
|
|
181
|
+
exports.DEFAULT_OUTCOME_EXITS = DEFAULT_OUTCOME_EXITS;
|
|
182
|
+
/** The exit code an outcome gives: a refusal's or failure's own error exit, else its state's name in the table. */
|
|
183
|
+
function outcomeExit(o, exits, names = DEFAULT_OUTCOME_EXITS) {
|
|
184
|
+
if ((o.state === "refused" || o.state === "failed") && o.error)
|
|
185
|
+
return o.error.exit;
|
|
186
|
+
return exits.code(names[o.state]);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* The outcome as text for a person: the summary, what was checked (or why it is not checked yet), what only the person
|
|
190
|
+
* can do, then the next steps.
|
|
191
|
+
* A refusal or failure with an error is the one error line, as every Ouro AX error is.
|
|
192
|
+
*
|
|
193
|
+
* @claim "as every Ouro AX error is" registry/ouro/ax/outcome.test.mts verify:testExists(registry/ouro/ax/outcome.test.mts::outcomeText: the summary, the check, the receipt, the person's step and the next steps; a refusal is its error line)
|
|
194
|
+
*/
|
|
195
|
+
function outcomeText(o, tool) {
|
|
196
|
+
if ((o.state === "refused" || o.state === "failed") && o.error)
|
|
197
|
+
return (0, problem_1.errorLine)(tool, o.error);
|
|
198
|
+
const lines = [o.summary];
|
|
199
|
+
const how = o.postcondition.how.replace(/[.\s]+$/, "");
|
|
200
|
+
if (o.postcondition.checked && o.postcondition.holds !== null)
|
|
201
|
+
lines.push(`Checked: ${how}${o.postcondition.holds === false ? " (it does not hold)" : ""}.`);
|
|
202
|
+
else if (how !== "not checked")
|
|
203
|
+
lines.push(`Not checked yet: ${how}.`);
|
|
204
|
+
if (o.receipt)
|
|
205
|
+
lines.push(`Receipt: ${o.receipt.id}`);
|
|
206
|
+
if (o.human)
|
|
207
|
+
lines.push(`Needs the person: ${o.human.reason.replace(/[.\s]+$/, "")}. Then: ${o.human.resume.command}`);
|
|
208
|
+
for (const n of o.next)
|
|
209
|
+
lines.push(`Next: ${n.command} (${n.why})`);
|
|
210
|
+
return lines.join("\n");
|
|
211
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.emptyText = emptyText;
|
|
4
|
+
exports.page = page;
|
|
5
|
+
exports.render = render;
|
|
6
|
+
exports.truncate = truncate;
|
|
7
|
+
// The export list comes first because the shadcn CLI drops comments before a file's
|
|
8
|
+
// first statement, which would take the JSDoc below with it.
|
|
9
|
+
/**
|
|
10
|
+
* One result, two renderings: `--json` prints the data, text prints what the command's text() makes of the same data.
|
|
11
|
+
* Long results are cut predictably, and lists page with a cursor, so an agent can read a long answer in pieces.
|
|
12
|
+
*
|
|
13
|
+
* @claim "One result, two renderings" registry/ouro/ax/output.test.mts verify:testExists(registry/ouro/ax/output.test.mts::one result renders as JSON or as its text, with one trailing newline)
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* What stdout gets for a result: the JSON document, or the text. Always one trailing newline.
|
|
17
|
+
*
|
|
18
|
+
* @claim "Always one trailing newline" registry/ouro/ax/output.test.mts verify:testExists(registry/ouro/ax/output.test.mts::one result renders as JSON or as its text, with one trailing newline)
|
|
19
|
+
*/
|
|
20
|
+
function render(result, options) {
|
|
21
|
+
const body = options.json || !options.text ? JSON.stringify(result, null, 2) : options.text(result);
|
|
22
|
+
return body.endsWith("\n") ? body : `${body}\n`;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Cuts text to at most `max` characters at a line break where it can, and says so on its last line with how to get the
|
|
26
|
+
* rest.
|
|
27
|
+
*/
|
|
28
|
+
function truncate(text, max, more = "ask for less, such as a smaller limit") {
|
|
29
|
+
if (text.length <= max)
|
|
30
|
+
return { text, truncated: false, total: text.length };
|
|
31
|
+
const note = `\n[truncated: ${max} of ${text.length} characters shown; ${more}]`;
|
|
32
|
+
const room = Math.max(0, max - note.length);
|
|
33
|
+
const cut = text.lastIndexOf("\n", room);
|
|
34
|
+
return { text: text.slice(0, cut > room / 2 ? cut : room) + note, truncated: true, total: text.length };
|
|
35
|
+
}
|
|
36
|
+
/** One page of a list. The cursor is the next item's offset, so it is stable while the list is. */
|
|
37
|
+
function page(items, options) {
|
|
38
|
+
const start = options.cursor ? Number.parseInt(options.cursor, 10) : 0;
|
|
39
|
+
if (!Number.isSafeInteger(start) || start < 0 || start > items.length)
|
|
40
|
+
throw new Error(`cursor "${options.cursor}" is not one this list gave. Fix: start again without a cursor.`);
|
|
41
|
+
const end = Math.min(items.length, start + Math.max(1, options.limit));
|
|
42
|
+
return { items: items.slice(start, end), total: items.length, nextCursor: end < items.length ? String(end) : null };
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The text for a list that came back empty: what is empty, by the filter that emptied it ("Nothing unread."), never
|
|
46
|
+
* "nothing held yet" when the filter is why, then the command to run next. A tool's voice may add its `empty` line after.
|
|
47
|
+
*
|
|
48
|
+
* @claim "by the filter that emptied it" registry/ouro/ax/output.test.mts verify:testExists(registry/ouro/ax/output.test.mts::an empty list says what is empty and what to run next)
|
|
49
|
+
*/
|
|
50
|
+
function emptyText(what, next) {
|
|
51
|
+
const fact = `Nothing ${what.replace(/[.\s]+$/, "")}.`;
|
|
52
|
+
return next ? `${fact}\nNext: ${next.command} (${next.why})` : fact;
|
|
53
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VAGUE_FAILURE_PHRASES = exports.VAGUE_CAUSE = exports.PRECISE_FAILURE_KEY = exports.NOT_BARE_LEADS = exports.NOT_A_CAUSE = exports.LEAD_FAILURE = exports.FAILURE_KEYS = exports.CAUSE_MARKER = exports.BARE_FAILURE = void 0;
|
|
4
|
+
exports.checkFailureCopyAllow = checkFailureCopyAllow;
|
|
5
|
+
exports.preciseFailureProblem = preciseFailureProblem;
|
|
6
|
+
exports.statesCause = statesCause;
|
|
7
|
+
exports.vagueFailureIn = vagueFailureIn;
|
|
8
|
+
exports.vagueFailuresIn = vagueFailuresIn;
|
|
9
|
+
/** A phrase's name, then its pattern. */
|
|
10
|
+
const VAGUE_FAILURE_PHRASES = Object.freeze([
|
|
11
|
+
["couldn't", /\bcouldn[’']?t\b/i],
|
|
12
|
+
["could not", /\bcould not\b/i],
|
|
13
|
+
["something went wrong", /\bsomething went wrong\b/i],
|
|
14
|
+
["unable to", /\bunable to\b/i],
|
|
15
|
+
["failed to load", /\bfailed to load\b/i],
|
|
16
|
+
["an error occurred", /\ban? (?:unknown |unexpected )?error (?:has )?occurred\b|\ban (?:unknown|unexpected) error\b/i],
|
|
17
|
+
]);
|
|
18
|
+
exports.VAGUE_FAILURE_PHRASES = VAGUE_FAILURE_PHRASES;
|
|
19
|
+
/**
|
|
20
|
+
* A bare failure: the whole text is "error" or "failed" (or "failure"), at most two words before it, or "Error:" with nothing after.
|
|
21
|
+
*
|
|
22
|
+
* @claim "at most two words before it" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
23
|
+
*/
|
|
24
|
+
const BARE_FAILURE = /^[^\p{L}\p{N}]*(?:(?:\p{L}[\p{L}'’-]*\s+){0,2}(?:error|errors|failed|failure)|error\s*:)[^\p{L}\p{N}]*$/iu;
|
|
25
|
+
exports.BARE_FAILURE = BARE_FAILURE;
|
|
26
|
+
/** What marks a failure's cause after the phrase, in the same sentence: a colon, a dash or "because". */
|
|
27
|
+
const CAUSE_MARKER = /:|\s[—–-]\s|\bbecause\b/iu;
|
|
28
|
+
exports.CAUSE_MARKER = CAUSE_MARKER;
|
|
29
|
+
/**
|
|
30
|
+
* First words that make the words after a marker a next step ("try again") or a vague word ("error") in place of a cause.
|
|
31
|
+
*
|
|
32
|
+
* @claim "First words that make the words after a marker a next step" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
33
|
+
*/
|
|
34
|
+
const NOT_A_CAUSE = Object.freeze(["try", "please", "retry", "contact", "see", "error", "errors", "unknown", "failed", "failure"]);
|
|
35
|
+
exports.NOT_A_CAUSE = NOT_A_CAUSE;
|
|
36
|
+
/**
|
|
37
|
+
* A cause that names nothing: the whole of it is "it broke", "it didn't work", "there was a problem", "an issue occurred"
|
|
38
|
+
* and the like, so it says neither what failed nor why. A cause that goes on ("it failed because the disk is full") names
|
|
39
|
+
* something and passes.
|
|
40
|
+
*
|
|
41
|
+
* @claim "A cause that names nothing" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
42
|
+
*/
|
|
43
|
+
const VAGUE_CAUSE = /^[^\p{L}\p{N}]*(?:(?:it|this|that|something)\s+(?:broke|failed|crashed|errored|stopped(?:\s+working)?|went\s+wrong|(?:didn[’']?t|did\s+not|doesn[’']?t|does\s+not)\s+work|(?:isn[’']?t|is\s+not)\s+working)|there\s+(?:was|is)\s+(?:an?\s+)?(?:problem|issue|error)|an?\s+(?:problem|issue|error)\s+(?:has\s+)?(?:occurred|happened))[^\p{L}\p{N}]*$/iu;
|
|
44
|
+
exports.VAGUE_CAUSE = VAGUE_CAUSE;
|
|
45
|
+
/** A failure that leads the text ("Error: …", "Upload failed - …"), held to the same test as a phrase; the group is the marker. */
|
|
46
|
+
const LEAD_FAILURE = /^[^\p{L}\p{N}]*(?:\p{L}[\p{L}'’-]*\s+){0,2}(?:error|errors|failed|failure)\s*(:|\s[—–-]\s)/iu;
|
|
47
|
+
exports.LEAD_FAILURE = LEAD_FAILURE;
|
|
48
|
+
/**
|
|
49
|
+
* Words before "error" or "failed" that make the text other than a failure: a negation ("No errors") or an action ("Retry failed").
|
|
50
|
+
*
|
|
51
|
+
* @claim "that make the text other than a failure" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
52
|
+
*/
|
|
53
|
+
const NOT_BARE_LEADS = Object.freeze(["no", "nothing", "zero", "none", "show", "hide", "retry", "clear"]);
|
|
54
|
+
exports.NOT_BARE_LEADS = NOT_BARE_LEADS;
|
|
55
|
+
/**
|
|
56
|
+
* The fields of an answer that speak to the agent or the person: a refusal's or an outcome's `error`, `human` (its
|
|
57
|
+
* `reason` and `resume`), next steps and fixes, and in an Ouro AX answer (one with an `ax` field: an outcome, a checks
|
|
58
|
+
* report) its `summary` and each finding's `detail`.
|
|
59
|
+
*
|
|
60
|
+
* @claim "each finding's `detail`" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::walks a value, reading an outcome's error, human reason and summary and a checks report's findings, not the person's data)
|
|
61
|
+
*/
|
|
62
|
+
const FAILURE_KEYS = new Set(["error", "human", "reason", "next", "fix", "resume"]);
|
|
63
|
+
exports.FAILURE_KEYS = FAILURE_KEYS;
|
|
64
|
+
const AX_KEYS = new Set(["summary", "detail"]);
|
|
65
|
+
/** The field on an outcome's `error` or `human`, or a checks row, that says why its short failure text is precise. */
|
|
66
|
+
const PRECISE_FAILURE_KEY = "preciseFailure";
|
|
67
|
+
exports.PRECISE_FAILURE_KEY = PRECISE_FAILURE_KEY;
|
|
68
|
+
/**
|
|
69
|
+
* What is wrong with a record's `preciseFailure`, or null when the field is absent or holds a reason. A blank or
|
|
70
|
+
* non-text value is skipped, so the detectors keep reading the record's text.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* preciseFailureProblem({ code: "x", preciseFailure: " " }); // "preciseFailure needs a reason in words"
|
|
74
|
+
*/
|
|
75
|
+
function preciseFailureProblem(record) {
|
|
76
|
+
const reason = record[PRECISE_FAILURE_KEY];
|
|
77
|
+
return reason === undefined || (typeof reason === "string" && reason.trim().length > 0) ? null : "preciseFailure needs a reason in words";
|
|
78
|
+
}
|
|
79
|
+
/** A code or a status ("failed", "not_found"), not copy. */
|
|
80
|
+
const TOKEN = /^[a-z][\w.:-]*$/;
|
|
81
|
+
/**
|
|
82
|
+
* The vague failure phrase in a message, by its name, or null when there is none or an allow-list entry lets it
|
|
83
|
+
* through. A text that is only "Error" or "Upload failed" is "bare error" or "bare failed".
|
|
84
|
+
*
|
|
85
|
+
* @claim "or null when there is none or an allow-list entry lets it through" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* vagueFailureIn("Couldn't show this picture"); // "couldn't"
|
|
89
|
+
* vagueFailureIn("The photo service says this picture was deleted. Ask its owner to share it again."); // null
|
|
90
|
+
*/
|
|
91
|
+
function vagueFailureIn(text, allow = []) {
|
|
92
|
+
const phrase = phraseIn(text);
|
|
93
|
+
return phrase && !allowedBy(text, allow) ? phrase : null;
|
|
94
|
+
}
|
|
95
|
+
const SENTENCE_END = /[.!?](?=\s|$)/u;
|
|
96
|
+
const WORD = /\p{L}[\p{L}\p{N}'’-]*/gu;
|
|
97
|
+
const wordsOf = (text) => text.toLowerCase().match(WORD) ?? [];
|
|
98
|
+
/**
|
|
99
|
+
* Whether the text after a failure phrase states the cause in the same sentence: after a colon, a dash or "because", a
|
|
100
|
+
* substitution (`${…}`) or at least two words whose first is not in {@link NOT_A_CAUSE}, that are not a cause naming nothing
|
|
101
|
+
* ({@link VAGUE_CAUSE}), with no vague phrase in it.
|
|
102
|
+
*
|
|
103
|
+
* @claim "at least two words whose first is not in" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* statesCause(" send: you're offline. Try again later."); // true
|
|
107
|
+
* statesCause(" send: try again."); // false
|
|
108
|
+
*/
|
|
109
|
+
function statesCause(rest) {
|
|
110
|
+
const sentence = rest.split(SENTENCE_END)[0];
|
|
111
|
+
const marker = CAUSE_MARKER.exec(sentence);
|
|
112
|
+
if (!marker)
|
|
113
|
+
return false;
|
|
114
|
+
const cause = sentence.slice(marker.index + marker[0].length);
|
|
115
|
+
if (cause.includes("${…}"))
|
|
116
|
+
return true;
|
|
117
|
+
const words = wordsOf(cause);
|
|
118
|
+
return words.length >= 2 && !NOT_A_CAUSE.includes(words[0]) && !VAGUE_CAUSE.test(cause) && !VAGUE_FAILURE_PHRASES.some(([, pattern]) => pattern.test(cause));
|
|
119
|
+
}
|
|
120
|
+
function phraseIn(text) {
|
|
121
|
+
const trimmed = text.trim();
|
|
122
|
+
if (TOKEN.test(trimmed))
|
|
123
|
+
return null;
|
|
124
|
+
const found = VAGUE_FAILURE_PHRASES.find(([, pattern]) => [...text.matchAll(new RegExp(pattern.source, `${pattern.flags}g`))].some((match) => !statesCause(text.slice(match.index + match[0].length))));
|
|
125
|
+
if (found)
|
|
126
|
+
return found[0];
|
|
127
|
+
const word = (failure) => (/\berrors?\b/i.test(failure) ? "bare error" : "bare failed");
|
|
128
|
+
const lead = LEAD_FAILURE.exec(trimmed);
|
|
129
|
+
if (lead && !wordsOf(lead[0]).some((w) => NOT_BARE_LEADS.includes(w)))
|
|
130
|
+
return statesCause(trimmed.slice(lead[0].length - lead[1].length)) ? null : word(lead[0]);
|
|
131
|
+
if (!BARE_FAILURE.test(trimmed))
|
|
132
|
+
return null;
|
|
133
|
+
if (wordsOf(trimmed).slice(0, -1).some((w) => NOT_BARE_LEADS.includes(w)))
|
|
134
|
+
return null;
|
|
135
|
+
return word(trimmed);
|
|
136
|
+
}
|
|
137
|
+
const allowedBy = (text, allow) => allow.find((entry) => text.includes(entry.match)) ?? null;
|
|
138
|
+
/**
|
|
139
|
+
* Every vague failure message in a value: a message, a `help --json`, an outcome, a refusal or a checks report. A
|
|
140
|
+
* finding an allow-list entry lets through carries the entry. With `messagesOnly`, only what is under
|
|
141
|
+
* {@link FAILURE_KEYS}, and an Ouro AX answer's `summary` and `detail`; a `code` is never read. A record with a
|
|
142
|
+
* `preciseFailure` reason (an outcome's `error` or `human`, a checks row) lets its own short texts, and a list of them, through, with
|
|
143
|
+
* the reason carried as the allow entry; the X-ray's `copy.precise-failure` reads the same list.
|
|
144
|
+
*
|
|
145
|
+
* @claim "a `code` is never read" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::walks a value, reading an outcome's error, human reason and summary and a checks report's findings, not the person's data)
|
|
146
|
+
* @claim "Every vague failure message in a value" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::walks a value, reading an outcome's error, human reason and summary and a checks report's findings, not the person's data)
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* vagueFailuresIn({ ax: "outcome/1", state: "failed", summary: "Not sent", error: { code: "send_failed", message: "Something went wrong" } }, { messagesOnly: true });
|
|
150
|
+
* // [{ phrase: "something went wrong", where: "error.message", text: "Something went wrong", allowed: null }]
|
|
151
|
+
*/
|
|
152
|
+
function vagueFailuresIn(value, options = {}) {
|
|
153
|
+
const out = [];
|
|
154
|
+
const visit = (v, where, speaks, ax, why = null) => {
|
|
155
|
+
if (typeof v === "string") {
|
|
156
|
+
const phrase = speaks ? phraseIn(v) : null;
|
|
157
|
+
if (phrase)
|
|
158
|
+
out.push({ phrase, where, text: v, allowed: why ? { match: v, reason: why } : allowedBy(v, options.allow ?? []) });
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (Array.isArray(v))
|
|
162
|
+
return v.forEach((x, i) => visit(x, `${where}[${i}]`, speaks, ax, why));
|
|
163
|
+
if (typeof v !== "object" || v === null)
|
|
164
|
+
return;
|
|
165
|
+
const record = v;
|
|
166
|
+
const inAx = ax || typeof record["ax"] === "string";
|
|
167
|
+
const reason = record[PRECISE_FAILURE_KEY];
|
|
168
|
+
const own = typeof reason === "string" && reason.trim() ? reason.trim() : null;
|
|
169
|
+
for (const [k, x] of Object.entries(record)) {
|
|
170
|
+
if (k === "code" || k === PRECISE_FAILURE_KEY)
|
|
171
|
+
continue;
|
|
172
|
+
visit(x, where ? `${where}.${k}` : k, speaks || FAILURE_KEYS.has(k) || (inAx && AX_KEYS.has(k)), inAx, own);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
visit(value, "", !options.messagesOnly, false);
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* An allow-list read from JSON, checked: a list of `{ match, reason }`, each with a non-empty `match` and a `reason`.
|
|
180
|
+
* Throws naming each entry that is not one, with the fix.
|
|
181
|
+
*
|
|
182
|
+
* @claim "each with a non-empty `match` and a `reason`" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::an allow-list needs a match and a reason in every entry)
|
|
183
|
+
* @claim "Throws naming each entry that is not one" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::an allow-list needs a match and a reason in every entry)
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* checkFailureCopyAllow([{ match: "could not be charged", reason: "names the card service and its answer" }]);
|
|
187
|
+
*/
|
|
188
|
+
function checkFailureCopyAllow(value) {
|
|
189
|
+
const shape = '{ "match": "<part of the text>", "reason": "<why this wording is precise>" }';
|
|
190
|
+
if (!Array.isArray(value))
|
|
191
|
+
throw new Error(`the failure copy allow-list is a list. Fix: [${shape}].`);
|
|
192
|
+
const problems = value.flatMap((entry, i) => {
|
|
193
|
+
const e = entry;
|
|
194
|
+
const ok = typeof e === "object" && e !== null && typeof e.match === "string" && e.match.length > 0 && typeof e.reason === "string" && e.reason.trim().length > 0;
|
|
195
|
+
return ok ? [] : [`entry ${i} ${JSON.stringify(entry)} needs a non-empty "match" and a "reason". Fix: ${shape}.`];
|
|
196
|
+
});
|
|
197
|
+
if (problems.length)
|
|
198
|
+
throw new Error(`the failure copy allow-list has ${problems.length} problem${problems.length === 1 ? "" : "s"}:\n${problems.join("\n")}`);
|
|
199
|
+
return value;
|
|
200
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AxError = void 0;
|
|
4
|
+
exports.commandLine = commandLine;
|
|
5
|
+
exports.didYouMean = didYouMean;
|
|
6
|
+
exports.errorJson = errorJson;
|
|
7
|
+
exports.errorLine = errorLine;
|
|
8
|
+
exports.errorMessage = errorMessage;
|
|
9
|
+
exports.isAxError = isAxError;
|
|
10
|
+
exports.shellWord = shellWord;
|
|
11
|
+
/** The error the kit's primitives throw: its message is the one line, its shape the data. */
|
|
12
|
+
class AxError extends Error {
|
|
13
|
+
shape;
|
|
14
|
+
constructor(shape) {
|
|
15
|
+
super(errorMessage(shape));
|
|
16
|
+
this.name = "AxError";
|
|
17
|
+
this.shape = shape;
|
|
18
|
+
}
|
|
19
|
+
get exit() {
|
|
20
|
+
return this.shape.exit;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.AxError = AxError;
|
|
24
|
+
function isAxError(value) {
|
|
25
|
+
return value instanceof AxError;
|
|
26
|
+
}
|
|
27
|
+
const sentence = (text) => text.trim().replace(/[.\s]+$/, "");
|
|
28
|
+
/** `<thing>: <problem>. Fix: <fix>.`, without the tool's prefix (a CLI adds its own). */
|
|
29
|
+
function errorMessage(shape) {
|
|
30
|
+
const head = `${shape.thing}: ${sentence(shape.problem)}.`;
|
|
31
|
+
return shape.fix === null ? head : `${head} Fix: ${sentence(shape.fix)}.`;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The one stderr line: `<tool>: <thing>: <problem>. Fix: <fix>.` Line breaks in any part become spaces. A thing that is
|
|
35
|
+
* the tool itself (a one-command CLI names its only command after the tool) is not said twice.
|
|
36
|
+
*/
|
|
37
|
+
function errorLine(tool, shape) {
|
|
38
|
+
const message = shape.thing === tool ? errorMessage({ ...shape, thing: "" }).replace(/^: /, "") : errorMessage(shape);
|
|
39
|
+
return `${tool}: ${message}`.replace(/\s*[\r\n]+\s*/g, " ");
|
|
40
|
+
}
|
|
41
|
+
/** The `--json` and MCP form: `{ error: { code, exit, thing, problem, fix, message } }`. */
|
|
42
|
+
function errorJson(shape) {
|
|
43
|
+
return { error: { ...shape, message: errorMessage(shape) } };
|
|
44
|
+
}
|
|
45
|
+
// Edits between two words, a swap of two letters side by side counting as one: "romos" is one edit from "rooms".
|
|
46
|
+
function distance(a, b) {
|
|
47
|
+
const rows = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
|
|
48
|
+
for (let i = 1; i <= a.length; i++) {
|
|
49
|
+
for (let j = 1; j <= b.length; j++) {
|
|
50
|
+
const row = rows[i];
|
|
51
|
+
row[j] = Math.min(rows[i - 1][j] + 1, row[j - 1] + 1, rows[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
52
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
|
|
53
|
+
row[j] = Math.min(row[j], rows[i - 2][j - 2] + 1);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return rows[a.length][b.length];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The candidate an agent most likely meant: the nearest within an edit for every three letters of the word (at least one,
|
|
60
|
+
* at most two), else a unique prefix; null when neither applies.
|
|
61
|
+
*
|
|
62
|
+
* @claim "the nearest within an edit for every three letters of the word" registry/ouro/ax/problem.test.mts verify:testExists(registry/ouro/ax/problem.test.mts::didYouMean: the nearest within a couple of edits, else a unique prefix, else null) verify:testExists(registry/ouro/ax/problem.test.mts::didYouMean: a short word suggests only a name an edit away, not one that changes half its letters)
|
|
63
|
+
*/
|
|
64
|
+
function didYouMean(word, candidates) {
|
|
65
|
+
if (word === "")
|
|
66
|
+
return null;
|
|
67
|
+
let best = null;
|
|
68
|
+
// Scaled to the word: two edits in a four-letter word change half of it, which is a different word, not a typo (#1670).
|
|
69
|
+
let bestDistance = Math.min(2, Math.max(1, Math.floor(word.length / 3))) + 1;
|
|
70
|
+
for (const c of candidates) {
|
|
71
|
+
const d = distance(word, c);
|
|
72
|
+
if (d < bestDistance) {
|
|
73
|
+
best = c;
|
|
74
|
+
bestDistance = d;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (best)
|
|
78
|
+
return best;
|
|
79
|
+
const prefixed = candidates.filter((c) => c.startsWith(word) || word.startsWith(c));
|
|
80
|
+
return prefixed.length === 1 ? prefixed[0] : null;
|
|
81
|
+
}
|
|
82
|
+
/** Quotes a word for a POSIX shell only when it needs it, so a fix copies and runs as printed. */
|
|
83
|
+
function shellWord(word) {
|
|
84
|
+
if (/^<[^>\s]+>$/.test(word) || /^[A-Za-z0-9_./:@#,=+-]+$/.test(word))
|
|
85
|
+
return word;
|
|
86
|
+
return `"${word.replace(/(["\\$`])/g, "\\$1")}"`;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A command line from words, each quoted only when it needs it.
|
|
90
|
+
*
|
|
91
|
+
* @claim "each quoted only when" registry/ouro/ax/problem.test.mts verify:testExists(registry/ouro/ax/problem.test.mts::commandLine quotes only what a shell would split or expand)
|
|
92
|
+
*/
|
|
93
|
+
function commandLine(words) {
|
|
94
|
+
return words.map(shellWord).join(" ");
|
|
95
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VOICE_MAX = void 0;
|
|
4
|
+
exports.checkVoice = checkVoice;
|
|
5
|
+
exports.checkVoiceLine = checkVoiceLine;
|
|
6
|
+
exports.createVoice = createVoice;
|
|
7
|
+
exports.isCi = isCi;
|
|
8
|
+
exports.voiceLines = voiceLines;
|
|
9
|
+
exports.voiceOn = voiceOn;
|
|
10
|
+
/**
|
|
11
|
+
* The longest voice line, in characters.
|
|
12
|
+
*
|
|
13
|
+
* @claim "The longest voice line" registry/ouro/ax/voice.test.mts verify:testExists(registry/ouro/ax/voice.test.mts::a line is one line of plain ASCII, at most 72 characters, checked when the tool is built)
|
|
14
|
+
*/
|
|
15
|
+
const VOICE_MAX = 72;
|
|
16
|
+
exports.VOICE_MAX = VOICE_MAX;
|
|
17
|
+
/** What is wrong with a voice line, or null when it is one line of printable ASCII, at most {@link VOICE_MAX} characters. */
|
|
18
|
+
function checkVoiceLine(line) {
|
|
19
|
+
if (line.trim() === "")
|
|
20
|
+
return "is empty";
|
|
21
|
+
if (/[\r\n]/.test(line))
|
|
22
|
+
return "is more than one line";
|
|
23
|
+
if (/[^\x20-\x7e]/.test(line))
|
|
24
|
+
return "is not plain ASCII (no emoji, curly quotes or tabs)";
|
|
25
|
+
if (line.length > VOICE_MAX)
|
|
26
|
+
return `is ${line.length} characters; the most is ${VOICE_MAX}`;
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
/** Throws at definition time when a fixed voice line breaks the rules; `done` lines are checked as they are made. */
|
|
30
|
+
function checkVoice(tool, voice) {
|
|
31
|
+
for (const slot of ["banner", "summary", "empty", "healthy", "waited"]) {
|
|
32
|
+
for (const line of voice[slot] ?? []) {
|
|
33
|
+
const problem = checkVoiceLine(line);
|
|
34
|
+
if (problem)
|
|
35
|
+
throw new Error(`${tool}: the voice's ${slot} line ${JSON.stringify(line)} ${problem}. Fix: one line of plain ASCII, at most ${VOICE_MAX} characters.`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** The fixed voice lines (banner, summary, empty, healthy and waited), as `help --json` lists them for a checker. */
|
|
40
|
+
function voiceLines(voice) {
|
|
41
|
+
return [...(voice.banner ?? []), ...(voice.summary ?? []), ...(voice.empty ?? []), ...(voice.healthy ?? []), ...(voice.waited ?? [])];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Whether the `CI` environment variable says this is a CI run: set, and not empty, "0" or "false".
|
|
45
|
+
*
|
|
46
|
+
* @claim "set, and not empty" registry/ouro/ax/voice.test.mts verify:testExists(registry/ouro/ax/voice.test.mts::voiceOn: only a terminal, without --json, --quiet or CI)
|
|
47
|
+
*/
|
|
48
|
+
function isCi(env) {
|
|
49
|
+
const ci = env["CI"];
|
|
50
|
+
return ci !== undefined && !["", "0", "false"].includes(ci.trim().toLowerCase());
|
|
51
|
+
}
|
|
52
|
+
/** Whether a voice line may print: stdout is a terminal, and not `--json`, `--quiet` or CI. */
|
|
53
|
+
function voiceOn(options) {
|
|
54
|
+
return options.tty && !options.json && !options.quiet && !isCi(options.env);
|
|
55
|
+
}
|
|
56
|
+
function processMemory() {
|
|
57
|
+
const seen = new Map();
|
|
58
|
+
return { last: (slot) => seen.get(slot) ?? null, remember: (slot, index) => void seen.set(slot, index) };
|
|
59
|
+
}
|
|
60
|
+
/** One process's voice: it rotates the variants of a slot and gives at most one `done` line. */
|
|
61
|
+
function createVoice(voice, options) {
|
|
62
|
+
const on = voiceOn(options);
|
|
63
|
+
const memory = options.memory ?? processMemory();
|
|
64
|
+
const start = options.start ?? Math.floor(Date.now() / 1000);
|
|
65
|
+
let celebrated = false;
|
|
66
|
+
return {
|
|
67
|
+
on,
|
|
68
|
+
line(slot, outcome) {
|
|
69
|
+
if (!on)
|
|
70
|
+
return null;
|
|
71
|
+
const all = slot === "done" ? (outcome && voice.done ? voice.done(outcome) : []) : (voice[slot] ?? []);
|
|
72
|
+
const lines = all.filter((l) => checkVoiceLine(l) === null);
|
|
73
|
+
if (!lines.length || (slot === "done" && celebrated))
|
|
74
|
+
return null;
|
|
75
|
+
if (slot === "done")
|
|
76
|
+
celebrated = true;
|
|
77
|
+
const last = memory.last(slot);
|
|
78
|
+
const index = last === null ? start % lines.length : (last + 1) % lines.length;
|
|
79
|
+
memory.remember(slot, index);
|
|
80
|
+
return lines[index];
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|