ace-wake 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -1
- data/README.md +120 -108
- data/exe/ace-watch +5 -0
- data/handbook/extensions/ace-wake/index.js +173 -437
- data/lib/ace/wake/actor_store.rb +106 -0
- data/lib/ace/wake/cli.rb +149 -0
- data/lib/ace/wake/version.rb +1 -1
- data/lib/ace/wake.rb +6 -5
- data/libexec/ace-watch/command.js +358 -0
- data/libexec/ace-watch/package.json +1 -0
- data/libexec/ace-watch/runner.js +105 -0
- data/libexec/ace-watch/sources.js +32 -0
- data/libexec/ace-watch/wake/eyes-digest.js +440 -0
- data/{handbook/extensions/ace-wake → libexec/ace-watch}/wake/eyes.js +118 -4
- data/{handbook/extensions/ace-wake → libexec/ace-watch}/wake/wake-command-parser.js +1 -1
- data/{handbook/extensions/ace-wake → libexec/ace-watch}/wake/watch-subscription.js +31 -38
- metadata +46 -18
- data/handbook/extensions/ace-wake/wake/eyes-runtime.js +0 -276
- /data/{handbook/extensions/ace-wake → libexec/ace-watch}/wake/ask-bridge.js +0 -0
- /data/{handbook/extensions/ace-wake → libexec/ace-watch}/wake/inbox-discovery.js +0 -0
- /data/{handbook/extensions/ace-wake → libexec/ace-watch}/wake/loop-subscription.js +0 -0
- /data/{handbook/extensions/ace-wake → libexec/ace-watch}/wake/types.js +0 -0
- /data/{handbook/extensions/ace-wake → libexec/ace-watch}/wake/wake-dispatcher.js +0 -0
- /data/{handbook/extensions/ace-wake → libexec/ace-watch}/wake/wake-registry.js +0 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { createWatchPort, nodeWatchFactory, nodeStatFn } from "./wake/watch-subscription.js";
|
|
4
|
+
import { armPendingAskWatches, requestsDirFor } from "./wake/ask-bridge.js";
|
|
5
|
+
|
|
6
|
+
const config = JSON.parse(process.argv[2]);
|
|
7
|
+
const parentPid = process.ppid;
|
|
8
|
+
const prefix = [...config.cli.slice(1), "--actor", config.actor, "--target", config.target, "--json", "--defer-confirm"];
|
|
9
|
+
let stopping = false, lastEyes = 0, lastFlush = 0, lastAsk = 0;
|
|
10
|
+
const errors = new Map();
|
|
11
|
+
const watches = new Map();
|
|
12
|
+
const signatureOf = ({ baseline, error, ...definition }) => JSON.stringify(definition);
|
|
13
|
+
const port = createWatchPort({ watchFactory: nodeWatchFactory, statFn: nodeStatFn, baseDir: config.cwd,
|
|
14
|
+
onDeactivate: () => {} });
|
|
15
|
+
|
|
16
|
+
function read() { return JSON.parse(fs.readFileSync(config.path, "utf8")); }
|
|
17
|
+
function call(args, input) {
|
|
18
|
+
const out = execFileSync(config.cli[0], [...prefix, ...args], {
|
|
19
|
+
cwd: config.cwd, encoding: "utf8", maxBuffer: 16 * 1024 * 1024, timeout: 130000,
|
|
20
|
+
input, stdio: [input === undefined ? "ignore" : "pipe", "pipe", "pipe"],
|
|
21
|
+
});
|
|
22
|
+
return JSON.parse(out);
|
|
23
|
+
}
|
|
24
|
+
function emit(result) {
|
|
25
|
+
if (!result.events?.length) return;
|
|
26
|
+
// Confirm only after the OS accepts the complete output. Async stdout.write
|
|
27
|
+
// can report EPIPE after a premature confirmation has already cleared work.
|
|
28
|
+
const bytes = Buffer.from(config.json ? JSON.stringify(result) + "\n" : result.text + "\n");
|
|
29
|
+
for (let offset = 0; offset < bytes.length;) offset += fs.writeSync(1, bytes, offset, bytes.length - offset);
|
|
30
|
+
if (config.target === "terminal") {
|
|
31
|
+
for (const event of result.events) call(["delivery", "confirm", event.id]);
|
|
32
|
+
call(["delivery", "settle"]);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function error(key, message) {
|
|
36
|
+
if (errors.get(key) === message) return;
|
|
37
|
+
errors.set(key, message);
|
|
38
|
+
process.stderr.write(`ace-watch ${key}: ${message}; inspect ace-watch status\n`);
|
|
39
|
+
}
|
|
40
|
+
function guarded(key, action) {
|
|
41
|
+
try { const result = action(); errors.delete(key); return result; }
|
|
42
|
+
catch { error(key, "operation unavailable or refused by its owner"); }
|
|
43
|
+
}
|
|
44
|
+
function notify(definition, delta) {
|
|
45
|
+
const safe = definition.redact ? delta.map(({ excerpt, ...reference }) => reference) : delta;
|
|
46
|
+
const result = guarded(`watch:${definition.name}`, () => call(["watch", "notify", definition.name,
|
|
47
|
+
"--revision", definition.generation ?? "", "--delta-stdin"], JSON.stringify(safe)));
|
|
48
|
+
if (!result) return { delivered: false, reason: "rejected" };
|
|
49
|
+
emit(result);
|
|
50
|
+
return { delivered: true };
|
|
51
|
+
}
|
|
52
|
+
function reconcile() {
|
|
53
|
+
if (stopping) return;
|
|
54
|
+
if (process.ppid !== parentPid) { stop(); return; }
|
|
55
|
+
const state = read();
|
|
56
|
+
const definitions = state.subscriptions?.watches ?? [];
|
|
57
|
+
for (const [name, signature] of watches) {
|
|
58
|
+
const next = definitions.find(row => row.name === name);
|
|
59
|
+
if (!next || signatureOf(next) !== signature) { port.stop(name); watches.delete(name); }
|
|
60
|
+
}
|
|
61
|
+
for (const definition of definitions) {
|
|
62
|
+
if (watches.has(definition.name)) {
|
|
63
|
+
const failed = port.errorOf(definition.name);
|
|
64
|
+
if (failed && definition.error !== failed)
|
|
65
|
+
guarded(`watch:${definition.name}`, () => emit(call(["watch", "fail", definition.name,
|
|
66
|
+
"--revision", definition.generation ?? "", "--message", failed])));
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (definition.error) { watches.set(definition.name, signatureOf(definition)); continue; }
|
|
70
|
+
guarded(`watch:${definition.name}`, () => {
|
|
71
|
+
try { port.start(definition, (_def, delta) => notify(definition, delta)); }
|
|
72
|
+
catch (error) { port.fail(definition.name, String(error.message ?? "watch unavailable")); }
|
|
73
|
+
watches.set(definition.name, signatureOf(definition));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (Date.now() - lastAsk >= 1000) guarded("ask", () => {
|
|
77
|
+
lastAsk = Date.now();
|
|
78
|
+
return armPendingAskWatches({ requestsDir: requestsDirFor(config.cwd), registry: {
|
|
79
|
+
list: () => [...(read().subscriptions?.watches ?? [])],
|
|
80
|
+
addWatch: (definition) => call(["watch", "add", definition.name, "--path", definition.path,
|
|
81
|
+
"--message", definition.message, ...(definition.redact ? ["--redact"] : [])]),
|
|
82
|
+
} }); });
|
|
83
|
+
for (const loop of state.subscriptions?.loops ?? []) {
|
|
84
|
+
if (Date.now() - loop.lastTick >= loop.intervalSeconds * 1000)
|
|
85
|
+
guarded(`loop:${loop.name}`, () => emit(call(["loop", "tick", loop.name])));
|
|
86
|
+
}
|
|
87
|
+
if (state.eyes?.enabled && !state.eyes.retired && !state.eyes.breaker &&
|
|
88
|
+
Date.now() - lastEyes >= state.eyes.config.intervalSeconds * 1000) {
|
|
89
|
+
lastEyes = Date.now();
|
|
90
|
+
guarded("eyes", () => emit(call(["eyes", "observe", "--scheduled"])));
|
|
91
|
+
}
|
|
92
|
+
if (Object.keys(state.subscriptions?.pending ?? {}).length)
|
|
93
|
+
guarded("delivery", () => emit(call(["delivery", "drain"])));
|
|
94
|
+
if (Date.now() - lastFlush >= 1000) { lastFlush = Date.now(); port.flushDirty(notify); }
|
|
95
|
+
}
|
|
96
|
+
const timer = setInterval(() => guarded("runner", reconcile), 250);
|
|
97
|
+
function stop() {
|
|
98
|
+
if (stopping) return;
|
|
99
|
+
stopping = true; clearInterval(timer); port.stopAll(); process.exitCode = 0;
|
|
100
|
+
process.stdin.pause();
|
|
101
|
+
}
|
|
102
|
+
for (const signal of ["SIGINT", "SIGTERM"]) process.on(signal, stop);
|
|
103
|
+
if (config.attached) { process.stdin.on("end", stop); process.stdin.resume(); }
|
|
104
|
+
for (const loop of read().subscriptions?.loops ?? []) guarded(`loop:${loop.name}`, () => call(["loop", "reset", loop.name]));
|
|
105
|
+
guarded("runner", reconcile);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
const execute = promisify(execFile);
|
|
4
|
+
|
|
5
|
+
export async function observeHitl(cwd, config) {
|
|
6
|
+
const options = { cwd, timeout: config.timeoutMs, maxBuffer: 1024 * 1024, encoding: "utf8" };
|
|
7
|
+
for (const source of config.syncRoutes) {
|
|
8
|
+
// One source per project: each entry names the project hitl config and,
|
|
9
|
+
// when several hermes_native routes exist there, the route to reconcile.
|
|
10
|
+
const argv = ["sync", "--config", source.config];
|
|
11
|
+
if (source.route !== undefined) argv.push("--route", source.route);
|
|
12
|
+
const { stdout } = await execute("ace-hitl-hermes", argv, options);
|
|
13
|
+
const result = JSON.parse(stdout);
|
|
14
|
+
if (!Array.isArray(result.errors) || result.errors.length) throw new Error("Native synchronization incomplete");
|
|
15
|
+
}
|
|
16
|
+
const { stdout } = await execute("ace-overseer", ["observe", "--owner", config.recipient, "--scope", config.scope, "--limit", String(config.maxEvents)], options);
|
|
17
|
+
const result = JSON.parse(stdout);
|
|
18
|
+
if (!result?.sources || typeof result.sources !== "object" || Array.isArray(result.sources) ||
|
|
19
|
+
Object.keys(result.sources).length === 0) throw new Error("Observation source health is missing");
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function processReports(cwd, config, signal) {
|
|
24
|
+
const { stdout } = await execute("ace-overseer", ["process-reports", "--json",
|
|
25
|
+
"--owner", config.recipient, "--scope", config.scope],
|
|
26
|
+
{ cwd, signal, maxBuffer: 8 * 1024 * 1024, encoding: "utf8" });
|
|
27
|
+
const result = JSON.parse(stdout);
|
|
28
|
+
if (!Array.isArray(result?.outcomes) || result.dry_run !== false)
|
|
29
|
+
throw new Error("Invalid report-processing receipt");
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
// Bounded, readable digests for every model-visible eyes surface (task
|
|
2
|
+
// 8yf.t.imq). Wakes, ace_eyes tool results and /eyes notifications render
|
|
3
|
+
// semantic deltas — never full pending maps, 64-hex keys/revisions or
|
|
4
|
+
// receipt timings. Exact revisions stay internal to the Eyes bookkeeping
|
|
5
|
+
// (eyes.js); acknowledgement resolves short ids inside the tool.
|
|
6
|
+
|
|
7
|
+
/** Hard character bound for any rendered eyes text. */
|
|
8
|
+
export const MAX_STATUS_CHARS = 2048;
|
|
9
|
+
|
|
10
|
+
/** Hard line bound for rendered status and wake text. */
|
|
11
|
+
export const MAX_STATUS_LINES = 10;
|
|
12
|
+
|
|
13
|
+
/** Maximum characters for one event note fragment. */
|
|
14
|
+
export const MAX_NOTE_CHARS = 120;
|
|
15
|
+
|
|
16
|
+
/** Maximum detail lines rendered before the omitted marker appears. */
|
|
17
|
+
const MAX_DETAIL_LINES = 8;
|
|
18
|
+
|
|
19
|
+
const HEX64 = /^[a-f0-9]{64}$/;
|
|
20
|
+
export { HEX64 };
|
|
21
|
+
|
|
22
|
+
const WAKE_GUIDANCE = "Read the original state owners. Reconcile uncertain effects; " +
|
|
23
|
+
"never repeat external actions on wake alone. Settlement alone is not acknowledgement.";
|
|
24
|
+
|
|
25
|
+
export const clip = (value, max) => {
|
|
26
|
+
const text = String(value ?? "")
|
|
27
|
+
.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ")
|
|
28
|
+
.replace(/\s+/g, " ").trim();
|
|
29
|
+
return text.length > max ? `${text.slice(0, Math.max(0, max - 1))}…` : text;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const countable = (value) => value !== undefined && value !== null && value !== "" &&
|
|
33
|
+
Number.isInteger(Number(value));
|
|
34
|
+
|
|
35
|
+
const TRUNCATION_MARKER = "… [ace-eyes: truncated]";
|
|
36
|
+
|
|
37
|
+
/** Enforce the hard character bound, reserving room for the marker. */
|
|
38
|
+
export function boundText(text, max = MAX_STATUS_CHARS) {
|
|
39
|
+
const source = String(text ?? "");
|
|
40
|
+
if (Buffer.byteLength(source) <= max) return source;
|
|
41
|
+
const limit = Math.max(0, max - Buffer.byteLength(TRUNCATION_MARKER));
|
|
42
|
+
let prefix = "", used = 0;
|
|
43
|
+
for (const character of source) {
|
|
44
|
+
const bytes = Buffer.byteLength(character);
|
|
45
|
+
if (used + bytes > limit) break;
|
|
46
|
+
prefix += character; used += bytes;
|
|
47
|
+
}
|
|
48
|
+
return prefix + TRUNCATION_MARKER;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** How many of `lines` land fully inside boundText's preserved prefix: a line
|
|
52
|
+
* cut at the character bound no longer presents what it carries. */
|
|
53
|
+
function fullyVisibleLines(lines, max = MAX_STATUS_CHARS) {
|
|
54
|
+
const joined = lines.join("\n");
|
|
55
|
+
if (Buffer.byteLength(joined) <= max) return lines.length;
|
|
56
|
+
const limit = Math.max(0, max - Buffer.byteLength(TRUNCATION_MARKER));
|
|
57
|
+
let used = 0;
|
|
58
|
+
let count = 0;
|
|
59
|
+
for (const line of lines) {
|
|
60
|
+
const add = Buffer.byteLength(line) + (count > 0 ? 1 : 0);
|
|
61
|
+
if (used + add > limit) break;
|
|
62
|
+
used += add;
|
|
63
|
+
count += 1;
|
|
64
|
+
}
|
|
65
|
+
return count;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Keep only candidates whose presenting line is fully inside the text. */
|
|
69
|
+
function presentedWithinCharBound(candidates, lines, max = MAX_STATUS_CHARS) {
|
|
70
|
+
const visible = fullyVisibleLines(lines, max);
|
|
71
|
+
return candidates
|
|
72
|
+
.filter((entry) => entry.line < visible)
|
|
73
|
+
.map(({ key, revision }) => ({ key, revision }));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const basename = (value) =>
|
|
77
|
+
typeof value === "string" && value ? value.split("/").filter(Boolean).pop() : undefined;
|
|
78
|
+
|
|
79
|
+
const has = (value) => typeof value === "string" && value.length > 0;
|
|
80
|
+
|
|
81
|
+
/** The short, ackable reference of an observation event — never a hash. */
|
|
82
|
+
function shortRef(event) {
|
|
83
|
+
if (has(event.source_id) && !HEX64.test(event.source_id)) return event.source_id.slice(0, 64);
|
|
84
|
+
if (has(event.id) && !HEX64.test(event.id)) return event.id.slice(0, 64);
|
|
85
|
+
switch (event.type) {
|
|
86
|
+
case "worktree":
|
|
87
|
+
case "executor": {
|
|
88
|
+
const base = basename(event.path);
|
|
89
|
+
if (base) return base.slice(0, 64);
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
case "project-session":
|
|
93
|
+
case "project-ready":
|
|
94
|
+
if (has(event.project)) return String(event.project).slice(0, 64);
|
|
95
|
+
break;
|
|
96
|
+
case "pr":
|
|
97
|
+
case "pr-activity":
|
|
98
|
+
if (has(event.forge) && countable(event.number)) return `${event.forge}#${event.number}`.slice(0, 64);
|
|
99
|
+
break;
|
|
100
|
+
case "issues":
|
|
101
|
+
if (has(event.forge)) return event.forge.slice(0, 64);
|
|
102
|
+
break;
|
|
103
|
+
default:
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function statusWord(event) {
|
|
110
|
+
return clip(event.result ?? event.status ?? event.state ?? "changed", 40);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function subjectNote(kind, event, ref) {
|
|
114
|
+
const task = event.task ?? event.assignment;
|
|
115
|
+
const tail = has(task) ? ` (task ${clip(task, 24)})` : "";
|
|
116
|
+
switch (kind) {
|
|
117
|
+
case "report":
|
|
118
|
+
return `report ${ref} ${statusWord(event)}${tail}`;
|
|
119
|
+
case "question":
|
|
120
|
+
return `question ${ref} awaiting answer${tail}`;
|
|
121
|
+
case "task":
|
|
122
|
+
case "overseer-task": {
|
|
123
|
+
const title = has(event.title) ? ` "${clip(event.title, 48)}"` : "";
|
|
124
|
+
return `${kind} ${ref}${title} → ${statusWord(event)}`;
|
|
125
|
+
}
|
|
126
|
+
case "assignment": {
|
|
127
|
+
const progress = has(event.progress) ? ` (${clip(event.progress, 24)})` : "";
|
|
128
|
+
return `assignment ${ref} ${statusWord(event)}${progress}`;
|
|
129
|
+
}
|
|
130
|
+
case "pr": {
|
|
131
|
+
const title = has(event.title) ? ` "${clip(event.title, 48)}"` : "";
|
|
132
|
+
const draft = event.draft === true ? " (draft)" : "";
|
|
133
|
+
return `pr ${ref} ${statusWord(event)}${draft}${title}`;
|
|
134
|
+
}
|
|
135
|
+
case "pr-activity": {
|
|
136
|
+
const title = has(event.title) ? ` "${clip(event.title, 48)}"` : "";
|
|
137
|
+
return `pr activity ${ref} ${clip(event.event_kind ?? "activity", 24)} by ${clip(event.author ?? "?", 24)}${title}`;
|
|
138
|
+
}
|
|
139
|
+
case "issues":
|
|
140
|
+
return `issues ${ref}: ${countable(event.count) ? Number(event.count) : "?"} open`;
|
|
141
|
+
case "worktree": {
|
|
142
|
+
const branch = has(event.branch) ? ` (${clip(event.branch, 48)})` : "";
|
|
143
|
+
return `worktree ${ref}${branch} changed`;
|
|
144
|
+
}
|
|
145
|
+
case "executor":
|
|
146
|
+
return `executor ${ref} ${event.active === true ? "active" : "idle"}`;
|
|
147
|
+
case "project-session":
|
|
148
|
+
return `project session ${ref} ${clip(event.state ?? event.status ?? "changed", 40)}`;
|
|
149
|
+
case "project-ready":
|
|
150
|
+
return `project ${ref} overseer session ready`;
|
|
151
|
+
default:
|
|
152
|
+
return `${kind} ${ref} ${statusWord(event)}`;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function sourceNote(event) {
|
|
157
|
+
const name = clip(sourceRef(event) ?? "source", 40);
|
|
158
|
+
if (event.status === "degraded") return `source ${name} degraded — ${clip(event.error, 64)}`;
|
|
159
|
+
return `source ${name} healthy`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function sourceRef(event) {
|
|
163
|
+
if (has(event.project)) return String(event.project).slice(0, 64);
|
|
164
|
+
const key = event.source_key ?? event.key;
|
|
165
|
+
if (has(key) && key.startsWith("source:")) return key.slice("source:".length, 64 + 7);
|
|
166
|
+
if (has(key) && !HEX64.test(key)) return key.slice(0, 64);
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* One-line semantic identity of an observation event: kind, short reference
|
|
172
|
+
* and a bounded note. No keys, revisions or private content.
|
|
173
|
+
*
|
|
174
|
+
* @param {object} event Validated observation event row.
|
|
175
|
+
* @returns {{kind: string, ref: string|undefined, note: string}}
|
|
176
|
+
*/
|
|
177
|
+
export function describeEvent(event) {
|
|
178
|
+
const kind = has(event?.type) ? clip(event.type, 40) : "event";
|
|
179
|
+
if (kind === "source-health") {
|
|
180
|
+
return { kind, ref: sourceRef(event), note: clip(sourceNote(event), MAX_NOTE_CHARS) };
|
|
181
|
+
}
|
|
182
|
+
const ref = shortRef(event ?? {});
|
|
183
|
+
const safeRef = ref === undefined ? undefined : clip(ref, 64);
|
|
184
|
+
const note = !safeRef ? `${kind} changed`
|
|
185
|
+
: kind === "event" ? `${safeRef} changed`
|
|
186
|
+
: subjectNote(kind, event ?? {}, safeRef);
|
|
187
|
+
return { kind, ref: safeRef, note: clip(note, MAX_NOTE_CHARS) };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Tolerant view of a persisted pending entry: sessions restored from older
|
|
192
|
+
* snapshots may lack descriptors and still render without leaking hashes.
|
|
193
|
+
*
|
|
194
|
+
* @param {object} entry Pending entry ({id, revision, kind?, ref?, note?}).
|
|
195
|
+
* @returns {{kind: string, ref: string|undefined, note: string}}
|
|
196
|
+
*/
|
|
197
|
+
export function describeEntry(entry) {
|
|
198
|
+
if (entry && typeof entry.note === "string" && entry.note) {
|
|
199
|
+
return { kind: entry.kind ?? "event", ref: entry.ref, note: entry.note };
|
|
200
|
+
}
|
|
201
|
+
return { kind: "event", ref: undefined, note: "event changed (restored session state)" };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function nextAction(entry) {
|
|
205
|
+
switch (entry.kind) {
|
|
206
|
+
case "source-health":
|
|
207
|
+
return "inspect ace-overseer observe for this source; repair at its owner before accepting work";
|
|
208
|
+
case "report":
|
|
209
|
+
return "run ace_eyes action process-reports to adopt completed reports and acknowledge them";
|
|
210
|
+
case "question":
|
|
211
|
+
return entry.ref ? `answer via ace-hitl, then run ace_eyes action ack id ${entry.ref}`
|
|
212
|
+
: "answer via ace-hitl, then run ace_eyes action status for details";
|
|
213
|
+
case "deadline":
|
|
214
|
+
return entry.ref ? `handle it, then run ace_eyes action ack id deadline:${entry.ref}`
|
|
215
|
+
: "handle it, then run ace_eyes action status for details";
|
|
216
|
+
default:
|
|
217
|
+
return entry.ref ? `handle it, then run ace_eyes action ack id ${entry.ref}` :
|
|
218
|
+
"handle it, then run ace_eyes action status for details";
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** The entries actually included in renderWake's text: the delivery budget
|
|
223
|
+
* prefix of the batch, exactly within the character bound. The runtime
|
|
224
|
+
* records these revisions as presented so acknowledgement stays honest. */
|
|
225
|
+
export function wakePresentation(batch, omitted = 0) {
|
|
226
|
+
const { lines, rows } = wakeLayout(batch, omitted);
|
|
227
|
+
return presentedWithinCharBound(
|
|
228
|
+
rows.map((row, index) => ({ ...row, line: index + 1 })), lines.slice(0, MAX_STATUS_LINES));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function wakeLayout(batch, omitted = 0) {
|
|
232
|
+
const total = Array.isArray(batch) ? batch.length : 0;
|
|
233
|
+
const pairs = (Array.isArray(batch) ? batch : []).slice(0, wakeDetailBudget(total, omitted));
|
|
234
|
+
const pendingOverflow = Number(omitted) > 0;
|
|
235
|
+
const entries = pairs.map(([, value]) => describeEntry(value));
|
|
236
|
+
// The header reports the whole delivered batch; the prefix below shows as
|
|
237
|
+
// many readable lines as the layout budget allows, with overflow markers.
|
|
238
|
+
const lines = [`[ace-eyes] ${total} pending change${total === 1 ? "" : "s"}:`];
|
|
239
|
+
for (const entry of entries) lines.push(`- ${entry.note} — ${nextAction(entry)}`);
|
|
240
|
+
if (pairs.length < total) lines.push(`(+${total - pairs.length} more in this batch — run ace_eyes action status)`);
|
|
241
|
+
if (pendingOverflow) lines.push(`(+${Number(omitted)} more pending — run ace_eyes action status)`);
|
|
242
|
+
lines.push(WAKE_GUIDANCE);
|
|
243
|
+
return { lines, rows: pairs.map(([key, value]) => ({ key: String(key ?? ""), revision: String(value?.revision ?? "") })) };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const wakeDetailBudget = (count, omitted = 0) => {
|
|
247
|
+
const batchOverflow = count > MAX_DETAIL_LINES;
|
|
248
|
+
const pendingOverflow = Number(omitted) > 0;
|
|
249
|
+
return Math.max(0, MAX_STATUS_LINES - 2 - (batchOverflow ? 1 : 0) - (pendingOverflow ? 1 : 0));
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Wake text for one delivered batch: the semantic delta plus the next action.
|
|
254
|
+
*
|
|
255
|
+
* @param {Array<[string, object]>} batch Delivered [key, pending entry] pairs.
|
|
256
|
+
* @param {number} [omitted] Pending changes beyond the delivered batch.
|
|
257
|
+
* @returns {string}
|
|
258
|
+
*/
|
|
259
|
+
export function renderWake(batch, omitted = 0) {
|
|
260
|
+
return boundText(wakeLayout(batch, omitted).lines.slice(0, MAX_STATUS_LINES).join("\n"));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function sourcesHealth(sources) {
|
|
264
|
+
const rows = Object.entries(sources ?? {});
|
|
265
|
+
if (rows.length === 0) return { summary: "", degraded: [] };
|
|
266
|
+
const degraded = rows.filter(([, source]) => source?.status !== "ok");
|
|
267
|
+
const summary = degraded.length === 0 ? `sources: ${rows.length} ok`
|
|
268
|
+
: `sources: ${rows.length - degraded.length} ok, ${degraded.length} degraded`;
|
|
269
|
+
return { summary, degraded };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const decisionNeeded = (entry) => entry.kind === "question";
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Shared layout for the compact status overview and its presentation record.
|
|
276
|
+
* `presented` carries the pending entries whose notes are actually rendered,
|
|
277
|
+
* so acknowledgement can be held to the revision the model was shown.
|
|
278
|
+
*/
|
|
279
|
+
function statusLayout(snapshot, { filter } = {}) {
|
|
280
|
+
const { summary, sources, pending, deadlines = 0 } = snapshot ?? {};
|
|
281
|
+
const state = !summary?.enabled ? "eyes off" : summary?.retired ? "eyes retired"
|
|
282
|
+
: summary?.breaker ? `eyes breaker: ${summary.breaker.reason ?? "unknown"}`
|
|
283
|
+
: summary?.observationUnavailable ? "eyes observation unavailable" : "eyes observing";
|
|
284
|
+
const health = sourcesHealth(sources);
|
|
285
|
+
const all = (Array.isArray(pending) ? pending : []).map(([key, value]) => ({
|
|
286
|
+
key, revision: value?.revision, ...describeEntry(value),
|
|
287
|
+
}));
|
|
288
|
+
const wanted = typeof filter === "string" && filter.trim() ? filter.trim().toLowerCase() : undefined;
|
|
289
|
+
const selected = wanted
|
|
290
|
+
? all.filter((entry) => entry.kind === wanted || (entry.ref ?? "").toLowerCase().includes(wanted))
|
|
291
|
+
: [
|
|
292
|
+
// Observation failures first, then decision-needed events, then the rest
|
|
293
|
+
// in insertion order — critical blockers are never silently displaced.
|
|
294
|
+
...all.filter((entry) => entry.kind === "source-health" && entry.note.includes("degraded")),
|
|
295
|
+
...all.filter((entry) => decisionNeeded(entry)),
|
|
296
|
+
...all.filter((entry) => !decisionNeeded(entry) &&
|
|
297
|
+
!(entry.kind === "source-health" && entry.note.includes("degraded"))),
|
|
298
|
+
];
|
|
299
|
+
|
|
300
|
+
const headParts = [state];
|
|
301
|
+
if (summary?.enabled && !summary.retired) {
|
|
302
|
+
// A filtered view names its share of the backlog so the model can never
|
|
303
|
+
// read the selected count as the whole pending set.
|
|
304
|
+
const pendingPhrase = !wanted && selected.length === 0 ? "no changes pending"
|
|
305
|
+
: wanted && selected.length === 0 ? `no matches · ${all.length} pending`
|
|
306
|
+
: wanted ? `pending ${selected.length} of ${all.length}` : `pending ${selected.length}`;
|
|
307
|
+
headParts.push(pendingPhrase);
|
|
308
|
+
headParts.push(`wakes ${summary.wakes ?? 0}/${summary.wakeLimit ?? "?"} this hour`);
|
|
309
|
+
if (summary.wakeLimitedUntil) headParts.push(`wake-limited until ${new Date(summary.wakeLimitedUntil).toISOString()}`);
|
|
310
|
+
if (deadlines > 0) headParts.push(`deadlines ${deadlines}`);
|
|
311
|
+
}
|
|
312
|
+
if (health.summary && (summary?.enabled || health.degraded.length > 0)) headParts.push(health.summary);
|
|
313
|
+
|
|
314
|
+
const lines = [headParts.join(" · ")];
|
|
315
|
+
if (wanted && selected.length === 0) lines.push(`no pending event matches "${clip(filter, 64)}"`);
|
|
316
|
+
// Degraded sources are always named with their error head, even without a
|
|
317
|
+
// pending health marker — observation failures are never silently hidden.
|
|
318
|
+
// Beyond the line budget each hidden group gets its own omitted marker.
|
|
319
|
+
const degradedNames = new Set();
|
|
320
|
+
const degradedRows = health.degraded.map(([rawName, source]) => {
|
|
321
|
+
const name = clip(rawName, 64);
|
|
322
|
+
degradedNames.add(name);
|
|
323
|
+
const error = source?.error ? ` — ${clip(source.error, 64)}` : "";
|
|
324
|
+
return `- source ${name} degraded${error}`;
|
|
325
|
+
});
|
|
326
|
+
const visible = selected.filter((entry) =>
|
|
327
|
+
!(entry.kind === "source-health" && entry.note.includes("degraded") && degradedNames.has(entry.ref)));
|
|
328
|
+
const detailTexts = [...degradedRows, ...visible.map((entry) => `- ${entry.note}`)];
|
|
329
|
+
const capacity = Math.max(0, MAX_STATUS_LINES - lines.length);
|
|
330
|
+
// Reserve one marker line per hidden group; iterate to a stable layout —
|
|
331
|
+
// markers shrink the shown budget, which can hide one more group.
|
|
332
|
+
let markers = 0;
|
|
333
|
+
let shown = capacity;
|
|
334
|
+
for (let pass = 0; pass < 3; pass += 1) {
|
|
335
|
+
shown = Math.max(0, capacity - markers);
|
|
336
|
+
const hiddenDegraded = degradedRows.length - Math.min(degradedRows.length, shown);
|
|
337
|
+
const hiddenPending = visible.length - Math.max(0, shown - degradedRows.length);
|
|
338
|
+
const next = (hiddenDegraded > 0 ? 1 : 0) + (hiddenPending > 0 ? 1 : 0);
|
|
339
|
+
if (next === markers) break;
|
|
340
|
+
markers = next;
|
|
341
|
+
}
|
|
342
|
+
const finalHiddenDegraded = degradedRows.length - Math.min(degradedRows.length, shown);
|
|
343
|
+
const shownVisible = Math.max(0, shown - degradedRows.length);
|
|
344
|
+
const renderedDegradedNames = new Set(health.degraded.slice(0, Math.min(degradedRows.length, shown)).map(([rawName]) => clip(rawName, 64)));
|
|
345
|
+
const detailStart = lines.length;
|
|
346
|
+
for (const text of detailTexts.slice(0, shown)) lines.push(text);
|
|
347
|
+
if (finalHiddenDegraded > 0) {
|
|
348
|
+
// No filter can enlarge the degraded-row budget, so the marker stays honest
|
|
349
|
+
// about what is hidden instead of advertising an escape that repeats it.
|
|
350
|
+
lines.push(`(+${finalHiddenDegraded} degraded sources omitted)`);
|
|
351
|
+
}
|
|
352
|
+
if (visible.length > shownVisible) {
|
|
353
|
+
const omitted = visible.length - shownVisible;
|
|
354
|
+
const kinds = {};
|
|
355
|
+
for (const entry of visible.slice(shownVisible)) kinds[entry.kind] = (kinds[entry.kind] ?? 0) + 1;
|
|
356
|
+
const hint = Object.entries(kinds).sort((a, b) => b[1] - a[1])[0]?.[0] ?? "event";
|
|
357
|
+
lines.push(`(+${omitted} omitted — ace_eyes action status filter ${hint})`);
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
lines,
|
|
361
|
+
presented: [
|
|
362
|
+
...visible.slice(0, shownVisible).map((entry, position) => (
|
|
363
|
+
{ key: entry.key, revision: entry.revision, line: detailStart + Math.min(degradedRows.length, shown) + position })),
|
|
364
|
+
// A degraded source's pending entry renders through the live degraded
|
|
365
|
+
// row (same name and note) — that row presents it just as honestly.
|
|
366
|
+
...all.filter((entry) => entry.kind === "source-health" && entry.note.includes("degraded") &&
|
|
367
|
+
renderedDegradedNames.has(entry.ref)).map((entry) => ({
|
|
368
|
+
key: entry.key, revision: entry.revision,
|
|
369
|
+
line: detailStart + health.degraded.findIndex(([name]) => name === entry.ref),
|
|
370
|
+
})),
|
|
371
|
+
],
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Compact current overview for ace_eyes status and /eyes status: at most
|
|
377
|
+
* MAX_STATUS_LINES lines and MAX_STATUS_CHARS. Decision-needed events first;
|
|
378
|
+
* degraded sources are always visible; larger backlogs group behind an
|
|
379
|
+
* omitted count with a filter hint. A healthy idle observation is one line.
|
|
380
|
+
*
|
|
381
|
+
* @param {object} snapshot {summary, sources, pending, deadlines}.
|
|
382
|
+
* @param {{filter?: string}} [options] Optional kind/id substring filter.
|
|
383
|
+
* @returns {string}
|
|
384
|
+
*/
|
|
385
|
+
export function renderStatus(snapshot, options = {}) {
|
|
386
|
+
return boundText(statusLayout(snapshot, options).lines.slice(0, MAX_STATUS_LINES).join("\n"));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** The pending entries actually included in renderStatus's text. */
|
|
390
|
+
export function statusPresentation(snapshot, options = {}) {
|
|
391
|
+
const { lines, presented } = statusLayout(snapshot, options);
|
|
392
|
+
return presentedWithinCharBound(presented, lines.slice(0, MAX_STATUS_LINES));
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function countsPhrase(counts) {
|
|
396
|
+
const order = ["success", "needs_answer", "error", "duplicate", "unmatched"];
|
|
397
|
+
const phrase = order.filter((name) => Number(counts?.[name] ?? 0) > 0)
|
|
398
|
+
.map((name) => `${counts[name]} ${name}`).join(", ");
|
|
399
|
+
return phrase || "none";
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function processingLayout(processing, pendingAfter = 0) {
|
|
403
|
+
const outcomes = Array.isArray(processing?.outcomes) ? processing.outcomes : [];
|
|
404
|
+
const acknowledged = outcomes.filter((row) =>
|
|
405
|
+
row.acknowledge === true && row.accepted === true && row.cleanup === "complete").length;
|
|
406
|
+
const lines = [`process-reports: ${outcomes.length} outcome${outcomes.length === 1 ? "" : "s"} — ${countsPhrase(processing?.counts)}`];
|
|
407
|
+
const detailBudget = Math.max(0, MAX_STATUS_LINES - 2 - (outcomes.length > MAX_STATUS_LINES - 2 ? 1 : 0));
|
|
408
|
+
const shown = outcomes.slice(0, detailBudget);
|
|
409
|
+
for (const row of shown) {
|
|
410
|
+
const message = has(row.message) ? ` — ${clip(row.message, 140)}` : "";
|
|
411
|
+
lines.push(`- ${clip(row.event_id ?? "?", 64)} ${clip(row.status ?? "unknown", 24)}${message}`);
|
|
412
|
+
}
|
|
413
|
+
if (outcomes.length > shown.length) lines.push(`(+${outcomes.length - shown.length} more outcomes)`);
|
|
414
|
+
lines.push(`acknowledged ${acknowledged} completed report revision(s); ${Number(pendingAfter)} pending change(s) remain`);
|
|
415
|
+
return { lines, rows: shown.map((row, index) => ({ key: row.key, revision: row.revision, line: index + 1 })) };
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** The receipt rows actually rendered, with their exact key/revision: the
|
|
419
|
+
* runtime binds them against current pending entries so the digest presents
|
|
420
|
+
* nothing it does not name. Rows are numbered by render position before
|
|
421
|
+
* filtering, so a key-less row cannot shift another row's line. */
|
|
422
|
+
export function processingPresentation(processing) {
|
|
423
|
+
const { lines, rows } = processingLayout(processing);
|
|
424
|
+
return presentedWithinCharBound(
|
|
425
|
+
rows.filter((row) => typeof row.key === "string" && typeof row.revision === "string"),
|
|
426
|
+
lines.slice(0, MAX_STATUS_LINES));
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Outcome digest for one process-reports receipt: statuses, per-event lines
|
|
431
|
+
* with bounded messages, and the acknowledgement tail. Operational failures
|
|
432
|
+
* never reach this renderer — they surface as tool errors.
|
|
433
|
+
*
|
|
434
|
+
* @param {object} processing Validated process-reports receipt.
|
|
435
|
+
* @param {number} [pendingAfter] Pending change count after acknowledgements.
|
|
436
|
+
* @returns {string}
|
|
437
|
+
*/
|
|
438
|
+
export function renderProcessing(processing, pendingAfter = 0) {
|
|
439
|
+
return boundText(processingLayout(processing, pendingAfter).lines.slice(0, MAX_STATUS_LINES).join("\n"));
|
|
440
|
+
}
|