@ouro.bot/cli 0.1.0-alpha.1679 → 0.1.0-alpha.1680

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.
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WHY_PLACEHOLDER = exports.WHY_FLAG = exports.ONLY_YOU = exports.DRY_RUN_FLAG = exports.DEFAULT_HEADINGS = void 0;
4
+ exports.audienceLines = audienceLines;
5
+ exports.effectAnnotations = effectAnnotations;
6
+ exports.namesLine = namesLine;
7
+ exports.privateResult = privateResult;
8
+ const DEFAULT_HEADINGS = Object.freeze({ private: "Only you see these", others: "Other people see these" });
9
+ exports.DEFAULT_HEADINGS = DEFAULT_HEADINGS;
10
+ const ONLY_YOU = Object.freeze({ only: true, who: "only you" });
11
+ exports.ONLY_YOU = ONLY_YOU;
12
+ /** The flag an "others" command takes: who asked, and what they said. */
13
+ const WHY_FLAG = "why";
14
+ exports.WHY_FLAG = WHY_FLAG;
15
+ const WHY_PLACEHOLDER = "<who asked you, and what they said>";
16
+ exports.WHY_PLACEHOLDER = WHY_PLACEHOLDER;
17
+ /** The flag that previews an "others" command's effect and audience without acting. */
18
+ const DRY_RUN_FLAG = "dry-run";
19
+ exports.DRY_RUN_FLAG = DRY_RUN_FLAG;
20
+ /** MCP tool annotations for an effect, as [the MCP specification](https://modelcontextprotocol.io/specification/2025-06-18/server/tools) defines them. */
21
+ function effectAnnotations(effect, readOnly) {
22
+ return { readOnlyHint: readOnly, destructiveHint: effect === "others", openWorldHint: effect === "others" };
23
+ }
24
+ /**
25
+ * "Jo Park", "Jo Park and Ana Ruiz", "Jo Park, Ana Ruiz and 3 others".
26
+ *
27
+ * @claim "Ana Ruiz and 3 others" registry/ouro/ax/effects.test.mts verify:testExists(registry/ouro/ax/effects.test.mts::names read as a person would say them)
28
+ */
29
+ function namesLine(names, max = 2) {
30
+ if (names.length === 0)
31
+ return "nobody";
32
+ if (names.length <= max)
33
+ return names.length === 1 ? names[0] : `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`;
34
+ const more = names.length - max;
35
+ return `${names.slice(0, max).join(", ")} and ${more} other${more === 1 ? "" : "s"}`;
36
+ }
37
+ /**
38
+ * The audience preview an "others" action prints before it runs, and the line its result states.
39
+ * @example audienceLines({ doing: "Sending", done: "Sent", where: '"Design sync"', as: "the person", who: ["Jo Park", "Ana Ruiz"] })
40
+ */
41
+ function audienceLines(a) {
42
+ const who = a.who === "everyone" ? `Everyone in ${a.where}` : namesLine(a.who);
43
+ return {
44
+ preview: `${a.doing} to ${a.where} as ${a.as}. ${who} will see it.`,
45
+ result: `${a.done} to ${a.where} as ${a.as}. ${who} can see it.`,
46
+ };
47
+ }
48
+ /** The result line of a private action: what changed, and that nobody else saw it. */
49
+ function privateResult(what) {
50
+ return `${what.replace(/[.\s]+$/, "")}. Only you see it; nothing was sent.`;
51
+ }
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FINDINGS_EXIT_CODE = exports.BASE_EXIT_CODES = void 0;
4
+ exports.defineExitCodes = defineExitCodes;
5
+ const BASE_EXIT_CODES = Object.freeze([
6
+ { code: 0, name: "ok", short: "ok", meaning: "ok" },
7
+ { code: 1, name: "error", short: "error", meaning: "error: the message says what to do" },
8
+ { code: 2, name: "usage", short: "usage", meaning: "usage: a missing or wrong argument; nothing was done" },
9
+ ]);
10
+ exports.BASE_EXIT_CODES = BASE_EXIT_CODES;
11
+ /**
12
+ * The code a check or doctor tool declares beside the base codes: its checks ran and found a problem, with a fix
13
+ * beside it. A check that threw before finishing is a crash and exits with the base `error` code instead, so an agent tells "the
14
+ * tool found something" from "the tool broke" by the code alone.
15
+ *
16
+ * @claim "exits with the base `error` code instead" registry/ouro/ax/checks.test.mts verify:testExists(registry/ouro/ax/checks.test.mts::findings exit with the declared findings code and a check that couldn't run exits 1, never the same code)
17
+ */
18
+ const FINDINGS_EXIT_CODE = Object.freeze({ code: 3, name: "findings", short: "findings", meaning: "findings: a check found a problem; its fix says what to do" });
19
+ exports.FINDINGS_EXIT_CODE = FINDINGS_EXIT_CODE;
20
+ /**
21
+ * The tool's table: the base codes (their meanings may be reworded, never their names) then its own, numbered from 3
22
+ * with no gaps.
23
+ *
24
+ * @claim "numbered from 3 with" registry/ouro/ax/exit-codes.test.mts verify:testExists(registry/ouro/ax/exit-codes.test.mts::a gap, a renamed base code, a short table or a repeated name fails with the fix)
25
+ */
26
+ function defineExitCodes(codes = BASE_EXIT_CODES) {
27
+ codes.forEach((entry, i) => {
28
+ if (entry.code !== i)
29
+ throw new Error(`exit codes: code ${entry.code} is at position ${i}. Fix: number the codes 0, 1, 2, ... with no gaps.`);
30
+ const base = BASE_EXIT_CODES[i];
31
+ if (base && entry.name !== base.name)
32
+ throw new Error(`exit codes: code ${i} is named "${entry.name}". Fix: name it "${base.name}", as every Ouro AX tool does.`);
33
+ });
34
+ if (codes.length < BASE_EXIT_CODES.length)
35
+ throw new Error("exit codes: the table needs 0 ok, 1 error and 2 usage. Fix: start from BASE_EXIT_CODES.");
36
+ const names = new Map(codes.map((entry) => [entry.name, entry.code]));
37
+ if (names.size !== codes.length)
38
+ throw new Error("exit codes: two codes share a name. Fix: give each code its own name.");
39
+ const frozen = Object.freeze(codes.map((entry) => Object.freeze({ ...entry })));
40
+ return {
41
+ codes: frozen,
42
+ code(name) {
43
+ const found = names.get(name);
44
+ if (found === undefined)
45
+ throw new Error(`exit codes: no code named "${name}". Fix: one of ${[...names.keys()].join(", ")}.`);
46
+ return found;
47
+ },
48
+ has: (name) => names.has(name),
49
+ line: () => `Exit codes: ${frozen.map((entry) => `${entry.code} ${entry.short}`).join(", ")}.`,
50
+ };
51
+ }
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_WIDTH = void 0;
4
+ exports.commandManifest = commandManifest;
5
+ exports.definitionLines = definitionLines;
6
+ exports.flagLines = flagLines;
7
+ exports.helpJson = helpJson;
8
+ exports.renderCommand = renderCommand;
9
+ exports.renderOverview = renderOverview;
10
+ exports.wrap = wrap;
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 ax_command_ts_1 = require("./ax-command.js");
14
+ const effects_ts_1 = require("./effects.js");
15
+ const problem_ts_1 = require("./problem.js");
16
+ const voice_ts_1 = require("./voice.js");
17
+ /**
18
+ * Help from the definitions: a one-screen overview under the two effect headings, each command's details, and the same
19
+ * as JSON (`help --json`), which an agent parses instead of scraping text. Every line is wrapped to `width` columns.
20
+ *
21
+ * @claim "a one-screen overview under the two effect headings" registry/ouro/ax/help.test.mts verify:testExists(registry/ouro/ax/help.test.mts::the overview is the two headings, one line per command, and the exit codes)
22
+ * @claim "Every line is wrapped" registry/ouro/ax/help.test.mts verify:testExists(registry/ouro/ax/help.test.mts::a command's help is generated from its definition, and an others command says so first)
23
+ */
24
+ const DEFAULT_WIDTH = 100;
25
+ exports.DEFAULT_WIDTH = DEFAULT_WIDTH;
26
+ /**
27
+ * Wraps text to `width` columns, every line indented; a word longer than a line stays whole.
28
+ *
29
+ * @claim "every line indented" registry/ouro/ax/help.test.mts verify:testExists(registry/ouro/ax/help.test.mts::wrap keeps long words whole and indents every line)
30
+ */
31
+ function wrap(text, indent, first = indent, width = DEFAULT_WIDTH) {
32
+ const lines = [];
33
+ let line = " ".repeat(first);
34
+ let empty = true;
35
+ for (const word of text.split(/\s+/).filter(Boolean)) {
36
+ if (!empty && line.length + 1 + word.length > width) {
37
+ lines.push(line);
38
+ line = " ".repeat(indent);
39
+ empty = true;
40
+ }
41
+ line += (empty ? "" : " ") + word;
42
+ empty = false;
43
+ }
44
+ return [...lines, line];
45
+ }
46
+ const listed = (cli) => cli.commands.filter((c) => !c.hidden && c !== cli.defaultCommand);
47
+ /**
48
+ * Commands of one effect, one per row. A multi-word command ("draft set") shows under its first word once when the CLI
49
+ * gives that word a group summary, and by its whole name otherwise, so no row names a command that does not exist.
50
+ *
51
+ * @claim "by its whole name otherwise" registry/ouro/ax/help.test.mts verify:testExists(registry/ouro/ax/help.test.mts::a multi-word command with no group summary is listed by its whole name, never as its first word)
52
+ */
53
+ function rows(cli, effect) {
54
+ const out = [];
55
+ for (const c of listed(cli).filter((x) => x.effect === effect)) {
56
+ const head = c.name.split(" ")[0];
57
+ const group = cli.groups[head];
58
+ const name = group === undefined ? c.name : head;
59
+ if (out.some((r) => r.name === name))
60
+ continue;
61
+ out.push({ name, summary: group ?? c.summary });
62
+ }
63
+ return out;
64
+ }
65
+ /**
66
+ * The overview: what the tool is, the commands under "Only you see these" and "Other people see these", the words only
67
+ * the person acts on, the footer. A tool defined with `command` shows its one command's usage and summary in place of
68
+ * the command list; a tool with a default command lists the default's usage, arguments and flags under its heading.
69
+ *
70
+ * @claim "the words only the person acts on" registry/ouro/ax/help.test.mts verify:testExists(registry/ouro/ax/help.test.mts::person-only words are listed in the overview and in help --json, with the reason and how the person does it)
71
+ * @claim "shows its one command's usage and summary" registry/ouro/ax/help.test.mts verify:testExists(registry/ouro/ax/help.test.mts::a one-command tool's overview and command help name the tool and its usage, never a command word)
72
+ * @claim "lists the default's usage, arguments and flags" registry/ouro/ax/help.test.mts verify:testExists(registry/ouro/ax/help.test.mts::a default command is listed in the overview under its own heading, with its arguments and flags, and never as a command word)
73
+ */
74
+ function renderOverview(cli, options = {}) {
75
+ const width = options.width ?? DEFAULT_WIDTH;
76
+ const footer = options.footer ?? [...wrap(`Output: --json on every command. ${cli.exits.line()} Errors: one line that says what to do instead.`, 0, 0, width), `Details: ${cli.single ? `${cli.name} --help` : `${cli.name} help <command>`}`];
77
+ if (cli.single) {
78
+ const cmd = cli.commands[0];
79
+ const heading = cmd.effect === "others" ? `${cli.headings.others}${options.othersNote ?? ""}` : cli.headings.private;
80
+ return [...cli.about, "", heading, ...columns((0, ax_command_ts_1.usageOf)(cli, cmd), cmd.summary, width), "", ...footer, ""].join("\n");
81
+ }
82
+ const mine = rows(cli, "private");
83
+ const theirs = rows(cli, "others");
84
+ const pad = Math.max(...[...mine, ...theirs].map((r) => r.name.length)) + 2;
85
+ // A summary longer than the line wraps under itself, so no overview line is wider than `width`.
86
+ const line = (r) => {
87
+ const [first, ...more] = wrap(r.summary, pad + 3, pad + 3, width);
88
+ return [` ${r.name.padEnd(pad)} ${first.slice(pad + 3)}`, ...more];
89
+ };
90
+ const theirsOnly = Object.keys(cli.personOnly);
91
+ const person = theirsOnly.length ? [...wrap(`Only the person does these, never you: ${theirsOnly.join(", ")}. Why and where: ${cli.name} help --json.`, 0, 0, width), ""] : [];
92
+ const def = cli.defaultCommand;
93
+ const defaultUnder = (effect) => (def && !def.hidden && def.effect === effect ? defaultLines(cli, def, width) : []);
94
+ return [...cli.about, "", cli.headings.private, ...defaultUnder("private"), ...mine.flatMap(line), "", `${cli.headings.others}${options.othersNote ?? ""}`, ...defaultUnder("others"), ...theirs.flatMap(line), "", ...person, ...footer, ""].join("\n");
95
+ }
96
+ /** The default command's part of the overview: its usage and summary, then its arguments and flags with what they do. */
97
+ function defaultLines(cli, cmd, width) {
98
+ return [...columns((0, ax_command_ts_1.usageOf)(cli, cmd), cmd.summary, width), ...argLines(cli, cmd, width), ...flagLines(cli, cmd, width)];
99
+ }
100
+ const COL = 34;
101
+ /**
102
+ * `left` from column 2, `text` from column 34, wrapped.
103
+ *
104
+ * @claim "from column 34" registry/ouro/ax/help.ts verify:fileLine(registry/ouro/ax/help.ts, /const COL = 34;/)
105
+ */
106
+ function columns(left, text, width) {
107
+ if (left.length + 4 < COL) {
108
+ const [head, ...more] = wrap(text, COL, COL, width);
109
+ return [` ${left}`.padEnd(COL) + head.slice(COL), ...more];
110
+ }
111
+ return [...wrap(left, 4, 2, width), ...wrap(text, COL, COL, width)];
112
+ }
113
+ /** One line per flag: usage on the left, what it does on the right; the built-ins and the tool's own are left out. */
114
+ function flagLines(cli, cmd, width = DEFAULT_WIDTH) {
115
+ return Object.entries((0, ax_command_ts_1.flagsOf)(cli, cmd))
116
+ .filter(([name]) => !(name in ax_command_ts_1.BUILTIN_FLAGS) && !(name in cli.globalFlags))
117
+ .flatMap(([name, spec]) => columns((0, ax_command_ts_1.flagUsage)(name, spec), `${spec.description}${spec.default === undefined ? "" : ` (default: ${typeof spec.default === "string" ? (0, problem_ts_1.shellWord)(spec.default) : String(spec.default)})`}${spec.required ? (spec.unless === undefined ? " Required." : ` Required unless ${(typeof spec.unless === "string" ? [spec.unless] : spec.unless).map((u) => `--${u}`).join(" or ")}.`) : ""}${spec.repeat ? " Repeat it for each value." : ""}${spec.stdin ? ' "-" reads it from stdin.' : ""}`, width));
118
+ }
119
+ /** One line per argument: its usage on the left, what it is on the right. */
120
+ function argLines(cli, cmd, width = DEFAULT_WIDTH) {
121
+ return (cmd.args ?? []).flatMap((a) => columns((0, ax_command_ts_1.argUsage)(a), `${a.description}${a.source ? ` "${cli.name} ${a.source}" lists them.` : ""}${a.stdin ? ' "-" reads it from stdin.' : ""}`, width));
122
+ }
123
+ /** The generated part of a command's help: its usage from the definition, its arguments and its flags. */
124
+ function definitionLines(cli, cmd, width = DEFAULT_WIDTH) {
125
+ return ["Definition", ...wrap(`${cli.name} ${(0, ax_command_ts_1.usageOf)(cli, cmd)}`, 4, 2, width), ...argLines(cli, cmd, width), ...flagLines(cli, cmd, width)];
126
+ }
127
+ /** One command's whole help, from its definition alone. */
128
+ function renderCommand(cli, cmd, width = DEFAULT_WIDTH) {
129
+ const lines = [
130
+ ...(cmd.effect === "others" ? [...wrap(`${cli.headings.others}: run it only when a human asked you to, with --${effects_ts_1.WHY_FLAG}.`, 0, 0, width), ""] : []),
131
+ ...wrap(`${(0, ax_command_ts_1.invocation)(cli, cmd)}: ${cmd.summary}`, 2, 0, width),
132
+ "",
133
+ ...definitionLines(cli, cmd, width),
134
+ ];
135
+ if (cmd.output)
136
+ lines.push("", "--json", ...wrap(cmd.output, 4, 2, width));
137
+ if (cmd.examples?.length)
138
+ lines.push("", "Examples", ...cmd.examples.map((x) => ` ${x}`));
139
+ lines.push("", ...wrap(cli.exits.line(), 0, 0, width), "");
140
+ return lines.join("\n");
141
+ }
142
+ /** One command as data. */
143
+ function commandManifest(cli, cmd) {
144
+ const flags = (0, ax_command_ts_1.flagsOf)(cli, cmd);
145
+ return {
146
+ name: cmd.name,
147
+ aliases: cmd.aliases ?? [],
148
+ effect: cmd.effect,
149
+ whoSees: cmd.effect === "others" ? cli.headings.others : cli.headings.private,
150
+ readOnly: cmd.readOnly === true,
151
+ needsWhy: flags[effects_ts_1.WHY_FLAG]?.required === true,
152
+ dryRun: effects_ts_1.DRY_RUN_FLAG in flags && typeof cmd.plan === "function",
153
+ runs: typeof cmd.run === "function" && cmd.loose !== true,
154
+ summary: cmd.summary,
155
+ usage: `${cli.name} ${(0, ax_command_ts_1.usageOf)(cli, cmd)}`,
156
+ inputSchema: (0, ax_command_ts_1.inputSchema)(cli, cmd),
157
+ output: cmd.output ?? null,
158
+ examples: cmd.examples ?? [],
159
+ hidden: cmd.hidden === true,
160
+ ...(cmd.protocol ? { protocol: true } : {}),
161
+ ...(cmd.self ? { scope: "self" } : {}),
162
+ ...(cmd === cli.defaultCommand ? { single: true } : {}),
163
+ };
164
+ }
165
+ /**
166
+ * The help as data: the tool, its headings, the commands, the words only the person acts on (`personOnly`: name, reason
167
+ * and how), the exit codes, the tool's fixed voice lines (for a checker
168
+ * that looks for them in an agent's output) and the one error shape.
169
+ */
170
+ function helpJson(cli) {
171
+ return {
172
+ tool: cli.name,
173
+ about: cli.about,
174
+ effects: { private: cli.headings.private, others: cli.headings.others },
175
+ commands: cli.commands.map((c) => commandManifest(cli, c)),
176
+ personOnly: Object.entries(cli.personOnly).map(([name, p]) => ({ name, reason: p.reason, how: p.how })),
177
+ exitCodes: cli.exits.codes,
178
+ voice: (0, voice_ts_1.voiceLines)(cli.voice),
179
+ errorShape: { error: { code: "<slug>", exit: "<exit code>", thing: "<command or flag>", problem: "<what's wrong>", fix: "<exact code to run instead, or null>", message: "<thing>: <problem>. Fix: <fix>." } },
180
+ };
181
+ }
@@ -0,0 +1,221 @@
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.isAxRefShape = isAxRefShape;
5
+ exports.isGenericAudience = isGenericAudience;
6
+ exports.isOutcome = isOutcome;
7
+ exports.nextStep = nextStep;
8
+ exports.outcome = outcome;
9
+ exports.outcomeExit = outcomeExit;
10
+ exports.outcomeText = outcomeText;
11
+ exports.validateOutcome = validateOutcome;
12
+ // The export list comes first because the shadcn CLI drops comments before a file's
13
+ // first statement, which would take the JSDoc below with it.
14
+ const effects_ts_1 = require("./effects.js");
15
+ const precise_copy_ts_1 = require("./precise-copy.js");
16
+ const problem_ts_1 = require("./problem.js");
17
+ /**
18
+ * The outcome of an action: what became of it, who can see it, whether that was checked, how to find it again and what
19
+ * to run next, in one shape over a CLI's `--json`, an HTTP route and an MCP tool.
20
+ */
21
+ const OUTCOME_VERSION = "outcome/1";
22
+ exports.OUTCOME_VERSION = OUTCOME_VERSION;
23
+ /**
24
+ * What became of an action.
25
+ * - `done`: it happened; the postcondition says how that is known.
26
+ * - `queued`: accepted, not performed yet; `receipt.id` and `next` follow it.
27
+ * - `planned`: a dry run; nothing happened.
28
+ * - `needs-human`: only a person can take the next step; `human` says why and how to resume.
29
+ * - `uncertain`: it may or may not have happened; reconcile before any retry.
30
+ * - `refused`: checked and not done, with the reason; nothing happened.
31
+ * - `failed`: tried and proven not done.
32
+ */
33
+ const OUTCOME_STATES = Object.freeze(["done", "queued", "planned", "needs-human", "uncertain", "refused", "failed"]);
34
+ exports.OUTCOME_STATES = OUTCOME_STATES;
35
+ /** An exact next step. */
36
+ function nextStep(command, why) {
37
+ return { command, why };
38
+ }
39
+ /**
40
+ * Builds an outcome with honest defaults: private, only you, nothing checked, no receipt and no next steps. Pass what is
41
+ * known; {@link validateOutcome} says what is missing.
42
+ *
43
+ * @example
44
+ * outcome({ action: "message.send", effect: "others", audience: { only: false, who: 'everyone in "Design sync"' },
45
+ * state: "queued", summary: 'Queued for "Design sync".',
46
+ * receipt: { id: "a1b2c3", at: new Date().toISOString(), by: "agent:planner", why: "Jo asked" },
47
+ * next: [nextStep("tool outcome a1b2c3 --timeout 60s", "wait until it is sent")] });
48
+ *
49
+ * @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)
50
+ */
51
+ function outcome(fields) {
52
+ const { action, state, summary, effect = "private", audience = effects_ts_1.ONLY_YOU, target = null, postcondition, receipt = null, next = [], error, human } = fields;
53
+ return {
54
+ ax: OUTCOME_VERSION,
55
+ action,
56
+ effect,
57
+ audience: { ...audience },
58
+ target,
59
+ state,
60
+ summary,
61
+ postcondition: postcondition ?? { checked: false, holds: null, how: "not checked" },
62
+ receipt,
63
+ next,
64
+ ...(error ? { error: { ...error, message: (0, problem_ts_1.errorMessage)(error) } } : {}),
65
+ ...(human ? { human } : {}),
66
+ };
67
+ }
68
+ const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
69
+ const isText = (v) => typeof v === "string" && v.trim().length > 0;
70
+ const isStep = (v) => isRecord(v) && isText(v["command"]) && isText(v["why"]);
71
+ /**
72
+ * Whether a value is an {@link AxRef}'s shape: `kind` and `id` as text, and, when given, `also` as a list of the
73
+ * same shape. Shared by {@link validateOutcome} and `checkWorkEvent`, so a target is checked the same way in both.
74
+ *
75
+ * @claim "a target is checked the same way in both" registry/ouro/ax/work-event.test.mts verify:testExists(registry/ouro/ax/work-event.test.mts::checkWorkEvent rejects a malformed target the same way validateOutcome does, also included)
76
+ */
77
+ function isAxRefShape(v) {
78
+ return isRecord(v) && isText(v["kind"]) && isText(v["id"]) && (v["also"] === undefined || (Array.isArray(v["also"]) && v["also"].every(isAxRefShape)));
79
+ }
80
+ /**
81
+ * Whether an audience's `who` names no one in particular ("other people", "someone", "everyone"), so an agent reading
82
+ * the JSON learns less than one reading the text.
83
+ *
84
+ * @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)
85
+ */
86
+ function isGenericAudience(who) {
87
+ return /^(?:other people|others|people|someone|somebody|anyone|anybody|everyone|everybody|the audience|recipients|users?)\.?$/i.test(who.trim());
88
+ }
89
+ /** Whether a value is an outcome of this version (its shape only; {@link validateOutcome} checks its meaning). */
90
+ function isOutcome(v) {
91
+ return isRecord(v) && v["ax"] === OUTCOME_VERSION && OUTCOME_STATES.includes(v["state"]);
92
+ }
93
+ /**
94
+ * Checks an outcome's meaning, not only its shape:
95
+ * - `done` never rests on a postcondition that failed; one not checked is a warning.
96
+ * - `refused` and `failed` carry an error and never claim the effect holds.
97
+ * - `queued` and `uncertain` carry a receipt and a next step, and `uncertain` cannot claim to know.
98
+ * - an effect other people see carries a receipt with a why, unless nothing happened, and an audience that is not only you.
99
+ * - `needs-human` carries the reason and the step that resumes.
100
+ * - a next step is an exact command with its why.
101
+ *
102
+ * @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)
103
+ */
104
+ function validateOutcome(v) {
105
+ const errors = [];
106
+ const warnings = [];
107
+ if (!isOutcome(v))
108
+ return { ok: false, errors: [`not an ${OUTCOME_VERSION} envelope: ax must be "${OUTCOME_VERSION}" and state one of ${OUTCOME_STATES.join(", ")}`], warnings };
109
+ if (!isText(v.action))
110
+ errors.push("action is empty");
111
+ if (v.effect !== "private" && v.effect !== "others")
112
+ errors.push('effect is "private" or "others"');
113
+ if (!isRecord(v.audience) || typeof v.audience.only !== "boolean" || !isText(v.audience.who))
114
+ errors.push("audience needs only (true or false) and who (words)");
115
+ else if (v.effect === "others" && v.audience.only)
116
+ errors.push("an effect other people see cannot have an audience of only you");
117
+ else if (v.effect === "others" && isGenericAudience(v.audience.who))
118
+ 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)`);
119
+ if (v.target !== null && !isAxRefShape(v.target))
120
+ errors.push("target is null or { kind, id }");
121
+ if (!isText(v.summary))
122
+ errors.push("summary is empty");
123
+ const p = v.postcondition;
124
+ const pOk = isRecord(p) && typeof p["checked"] === "boolean" && (p["holds"] === null || typeof p["holds"] === "boolean") && isText(p["how"]);
125
+ if (!pOk)
126
+ errors.push("postcondition needs checked, holds (true, false or null) and how");
127
+ const holds = pOk ? p["holds"] : null;
128
+ if (!Array.isArray(v.next) || !v.next.every(isStep))
129
+ errors.push("every next step needs a command and a why");
130
+ const r = v.receipt;
131
+ const rOk = r === null || (isRecord(r) && isText(r["id"]) && isText(r["at"]) && isText(r["by"]) && (r["why"] === null || typeof r["why"] === "string"));
132
+ if (!rOk)
133
+ errors.push("receipt needs id, at, by and why (or null)");
134
+ const why = rOk && r !== null ? r["why"] : null;
135
+ const hasReceipt = rOk && r !== null;
136
+ const hasNext = Array.isArray(v.next) && v.next.length > 0;
137
+ const e = v.error;
138
+ const hasError = isRecord(e) && isText(e["code"]) && isText(e["message"]);
139
+ if (e !== undefined && !hasError)
140
+ errors.push("error needs a code and a message");
141
+ if (isRecord(e) && (0, precise_copy_ts_1.preciseFailureProblem)(e))
142
+ errors.push(`error.${(0, precise_copy_ts_1.preciseFailureProblem)(e)}`);
143
+ if (isRecord(v.human) && (0, precise_copy_ts_1.preciseFailureProblem)(v.human))
144
+ errors.push(`human.${(0, precise_copy_ts_1.preciseFailureProblem)(v.human)}`);
145
+ switch (v.state) {
146
+ case "done":
147
+ if (holds === false)
148
+ errors.push("done, but the postcondition does not hold");
149
+ if (pOk && p["checked"] === false)
150
+ warnings.push("done without an independent check: say what it rests on in postcondition.how");
151
+ if (hasError)
152
+ errors.push("done carries an error");
153
+ break;
154
+ case "queued":
155
+ case "uncertain":
156
+ if (!hasReceipt)
157
+ errors.push(`${v.state} needs a receipt, so the request can be found again`);
158
+ if (!hasNext)
159
+ errors.push(`${v.state} needs a next step`);
160
+ if (v.state === "uncertain" && holds !== null)
161
+ errors.push("uncertain, but the postcondition claims to know");
162
+ break;
163
+ case "refused":
164
+ case "failed":
165
+ if (!hasError)
166
+ errors.push(`${v.state} needs an error with a code and a message`);
167
+ if (holds === true)
168
+ errors.push(`${v.state}, but the postcondition says the effect holds`);
169
+ if (!hasNext && !(hasError && isRecord(e) && isText(e["fix"])))
170
+ warnings.push(`${v.state} without a next step or a fix: say what to do instead`);
171
+ break;
172
+ case "needs-human":
173
+ if (!isRecord(v.human) || !isText(v.human.reason) || !isStep(v.human.resume))
174
+ errors.push("needs-human needs human.reason and human.resume");
175
+ break;
176
+ case "planned":
177
+ if (holds === true)
178
+ errors.push("planned, but the postcondition says the effect holds");
179
+ break;
180
+ }
181
+ if (v.effect === "others" && (v.state === "done" || v.state === "queued" || v.state === "uncertain") && !isText(why))
182
+ errors.push("an effect other people see needs a receipt with a why");
183
+ return { ok: errors.length === 0, errors, warnings };
184
+ }
185
+ /**
186
+ * The default: what was done, queued, planned or handed to the person exits `ok` (the outcome says which); a refusal
187
+ * exits `usage`; a failure or an uncertain result exits `error`. A tool with its own codes (such as "possibly done")
188
+ * passes its own names.
189
+ */
190
+ const DEFAULT_OUTCOME_EXITS = Object.freeze({ done: "ok", queued: "ok", planned: "ok", "needs-human": "ok", uncertain: "error", refused: "usage", failed: "error" });
191
+ exports.DEFAULT_OUTCOME_EXITS = DEFAULT_OUTCOME_EXITS;
192
+ /** The exit code an outcome gives: a refusal's or failure's own error exit, else its state's name in the table. */
193
+ function outcomeExit(o, exits, names = DEFAULT_OUTCOME_EXITS) {
194
+ if ((o.state === "refused" || o.state === "failed") && o.error)
195
+ return o.error.exit;
196
+ return exits.code(names[o.state]);
197
+ }
198
+ /**
199
+ * The outcome as text for a person: the summary, what was checked (or why it is not checked yet), what only the person
200
+ * can do, then the next steps.
201
+ * A refusal or failure with an error is the one error line, as every Ouro AX error is.
202
+ *
203
+ * @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)
204
+ */
205
+ function outcomeText(o, tool) {
206
+ if ((o.state === "refused" || o.state === "failed") && o.error)
207
+ return (0, problem_ts_1.errorLine)(tool, o.error);
208
+ const lines = [o.summary];
209
+ const how = o.postcondition.how.replace(/[.\s]+$/, "");
210
+ if (o.postcondition.checked && o.postcondition.holds !== null)
211
+ lines.push(`Checked: ${how}${o.postcondition.holds === false ? " (it does not hold)" : ""}.`);
212
+ else if (how !== "not checked")
213
+ lines.push(`Not checked yet: ${how}.`);
214
+ if (o.receipt)
215
+ lines.push(`Receipt: ${o.receipt.id}`);
216
+ if (o.human)
217
+ lines.push(`Needs the person: ${o.human.reason.replace(/[.\s]+$/, "")}. Then: ${o.human.resume.command}`);
218
+ for (const n of o.next)
219
+ lines.push(`Next: ${n.command} (${n.why})`);
220
+ return lines.join("\n");
221
+ }