@atolis-hq/wake 0.3.105 → 0.3.106
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.
|
@@ -2,19 +2,52 @@ import { readFile, readdir } from 'node:fs/promises';
|
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { pathToFileURL } from 'node:url';
|
|
4
4
|
export const unverifiedCodexCompletionReason = 'unverified: could not confirm background command completion from Codex structured telemetry';
|
|
5
|
+
/**
|
|
6
|
+
* Bound on how long this hook will keep forcing continuation for the same
|
|
7
|
+
* unresolved cell within one process segment before giving up and letting
|
|
8
|
+
* the turn end anyway. A retry *count* doesn't track this reliably — cycle
|
|
9
|
+
* length varies enormously with how long the model's own `wait` calls choose
|
|
10
|
+
* to yield for (observed from ~1s to 50s+ per call in production), so a
|
|
11
|
+
* fixed count either gives up far too early on a real multi-minute command
|
|
12
|
+
* or far too late on a broken loop that isn't waiting on anything real.
|
|
13
|
+
*
|
|
14
|
+
* Real observed `npm run verify`/`verify:ci` durations range from ~90s to
|
|
15
|
+
* over 20 minutes, so this value is a deliberate tradeoff rather than a safe
|
|
16
|
+
* upper bound on every legitimate case: a genuinely long-running command that
|
|
17
|
+
* outlasts it will have its live retry loop give up before finishing, and
|
|
18
|
+
* fall through to a `BLOCKED` outcome needing a human `/retry` instead of
|
|
19
|
+
* confirming cleanly on its own. That's an accepted cost, not a correctness
|
|
20
|
+
* gap — giving up here never weakens the guarantee, since the post-run
|
|
21
|
+
* fallback (`verifyCodexSession`) independently re-scans the same transcript
|
|
22
|
+
* after the process exits and still reports the run as unverified if the
|
|
23
|
+
* cell is genuinely still unresolved. This limit exists only to bound
|
|
24
|
+
* wasted time/tokens against a detection gap we haven't found yet, not to
|
|
25
|
+
* replace correct detection.
|
|
26
|
+
*/
|
|
27
|
+
export const maxLiveHookRetryMs = 10 * 60 * 1000;
|
|
5
28
|
/**
|
|
6
29
|
* Inspect only Codex's structured function-call protocol. Command output is
|
|
7
30
|
* opaque except for the protocol's managed-cell and terminal-result envelopes.
|
|
31
|
+
*
|
|
32
|
+
* Exec/wait cells live in one `codex exec` process's memory and never survive
|
|
33
|
+
* a resume into a new process, so only the segment of the transcript since
|
|
34
|
+
* the current process attached (its `session_meta`/`turn_context` marker)
|
|
35
|
+
* can hold a cell that might still be genuinely pending. A cell referenced
|
|
36
|
+
* only in an earlier segment belongs to a process that has already exited
|
|
37
|
+
* and can never return an exit code again — scoping to the latest segment
|
|
38
|
+
* is what makes that structurally certain, rather than trying to recognise
|
|
39
|
+
* every shape of "this cell is gone" text a stale reference might produce.
|
|
8
40
|
*/
|
|
9
41
|
export function inspectCodexTranscript(transcript) {
|
|
42
|
+
const lines = transcript.split(/\r?\n/).filter((line) => line.trim().length > 0);
|
|
43
|
+
const boundary = lastProcessBoundary(lines);
|
|
10
44
|
const telemetry = {
|
|
11
45
|
calls: new Map(),
|
|
12
46
|
pendingCells: new Set(),
|
|
13
47
|
resolvedCells: new Set(),
|
|
48
|
+
unresolvableCells: new Set(),
|
|
14
49
|
};
|
|
15
|
-
for (const line of
|
|
16
|
-
if (line.trim().length === 0)
|
|
17
|
-
continue;
|
|
50
|
+
for (const line of lines.slice(boundary + 1)) {
|
|
18
51
|
const event = parseJson(line);
|
|
19
52
|
if (event === undefined)
|
|
20
53
|
return blocked('could not parse Codex structured telemetry');
|
|
@@ -23,9 +56,76 @@ export function inspectCodexTranscript(transcript) {
|
|
|
23
56
|
consumePayload(payload, telemetry);
|
|
24
57
|
}
|
|
25
58
|
const unresolved = [...telemetry.pendingCells].filter((cellId) => !telemetry.resolvedCells.has(cellId));
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
59
|
+
if (unresolved.length === 0)
|
|
60
|
+
return {};
|
|
61
|
+
// A cell whose own `wait` call has already reported it gone (a distinct,
|
|
62
|
+
// cell-identity-specific protocol error, not a general failure) can never
|
|
63
|
+
// return an exit code no matter how many more times it's polled. Asking
|
|
64
|
+
// the model to keep retrying it is not just wasted effort, it's actively
|
|
65
|
+
// wrong instructions — so stop retrying that specific cell immediately
|
|
66
|
+
// rather than waiting out the general time backstop. The cell still
|
|
67
|
+
// counts as unresolved for the block decision itself: it genuinely never
|
|
68
|
+
// confirmed completion, it just isn't worth telling the model to poll
|
|
69
|
+
// again.
|
|
70
|
+
const retryable = unresolved.filter((cellId) => !telemetry.unresolvableCells.has(cellId));
|
|
71
|
+
if (retryable.length === 0)
|
|
72
|
+
return {};
|
|
73
|
+
if (liveHookRetryElapsedMs(lines.slice(boundary + 1)) >= maxLiveHookRetryMs)
|
|
74
|
+
return {};
|
|
75
|
+
return blocked(`Codex cell ${retryable[0]} has no terminal exit_code; continue polling that cell`);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Elapsed wall-clock time since this hook's own first continuation prompt
|
|
79
|
+
* within the current process segment, using the transcript's own event
|
|
80
|
+
* timestamps rather than a call/retry count. Returns 0 (never give up) when
|
|
81
|
+
* there is no prior retry yet, or when a timestamp can't be established —
|
|
82
|
+
* giving up is the direction that stops enforcing, so any doubt should keep
|
|
83
|
+
* blocking rather than silently switch to time-based give-up on weak evidence.
|
|
84
|
+
*/
|
|
85
|
+
function liveHookRetryElapsedMs(segment) {
|
|
86
|
+
let firstRetryAt;
|
|
87
|
+
let latestAt;
|
|
88
|
+
for (const line of segment) {
|
|
89
|
+
const event = parseJson(line);
|
|
90
|
+
if (event === undefined)
|
|
91
|
+
continue;
|
|
92
|
+
const at = timestampMs(event.timestamp);
|
|
93
|
+
if (at !== undefined)
|
|
94
|
+
latestAt = at;
|
|
95
|
+
const payload = record(event.payload);
|
|
96
|
+
if (payload?.type !== 'message' || payload.role !== 'user')
|
|
97
|
+
continue;
|
|
98
|
+
if (firstRetryAt === undefined &&
|
|
99
|
+
at !== undefined &&
|
|
100
|
+
userMessageText(payload.content).includes('<hook_prompt'))
|
|
101
|
+
firstRetryAt = at;
|
|
102
|
+
}
|
|
103
|
+
return firstRetryAt === undefined || latestAt === undefined ? 0 : latestAt - firstRetryAt;
|
|
104
|
+
}
|
|
105
|
+
function timestampMs(value) {
|
|
106
|
+
if (typeof value !== 'string')
|
|
107
|
+
return undefined;
|
|
108
|
+
const parsed = Date.parse(value);
|
|
109
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
110
|
+
}
|
|
111
|
+
function userMessageText(content) {
|
|
112
|
+
if (!Array.isArray(content))
|
|
113
|
+
return '';
|
|
114
|
+
return content
|
|
115
|
+
.map((item) => {
|
|
116
|
+
const entry = record(item);
|
|
117
|
+
return entry === undefined ? '' : (string(entry.text) ?? '');
|
|
118
|
+
})
|
|
119
|
+
.join('');
|
|
120
|
+
}
|
|
121
|
+
function lastProcessBoundary(lines) {
|
|
122
|
+
let boundary = -1;
|
|
123
|
+
lines.forEach((line, index) => {
|
|
124
|
+
const event = parseJson(line);
|
|
125
|
+
if (event?.type === 'session_meta' || event?.type === 'turn_context')
|
|
126
|
+
boundary = index;
|
|
127
|
+
});
|
|
128
|
+
return boundary;
|
|
29
129
|
}
|
|
30
130
|
function consumePayload(payload, telemetry) {
|
|
31
131
|
if (payload.type === 'function_call' || payload.type === 'custom_tool_call') {
|
|
@@ -43,8 +143,27 @@ function recordToolCall(payload, calls) {
|
|
|
43
143
|
calls.set(callId, {
|
|
44
144
|
name,
|
|
45
145
|
...(name === 'wait' ? cellArgument(payload.arguments ?? payload.input) : {}),
|
|
146
|
+
...(name === 'exec' ? { spawnsBackgroundJob: spawnsBackgroundJob(payload.input) } : {}),
|
|
46
147
|
});
|
|
47
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* The `exec` tool is a shared JS sandbox: `tools.exec_command(...)` spawns a
|
|
151
|
+
* genuine one-shot background job whose cell should eventually resolve to a
|
|
152
|
+
* real exit code. `tools.write_stdin(...)` instead interacts with an
|
|
153
|
+
* already-existing, intentionally long-lived session (e.g. a dev server kept
|
|
154
|
+
* running for e2e checks) — that cell is never meant to terminate, so
|
|
155
|
+
* flagging it as an unresolved background command would be a false positive.
|
|
156
|
+
* Default to treating a call as job-spawning whenever it can't be positively
|
|
157
|
+
* identified as pure session interaction, matching the fail-closed posture
|
|
158
|
+
* used everywhere else in this file.
|
|
159
|
+
*/
|
|
160
|
+
function spawnsBackgroundJob(input) {
|
|
161
|
+
if (typeof input !== 'string')
|
|
162
|
+
return true;
|
|
163
|
+
const interactsWithSession = /tools\.write_stdin\s*\(/.test(input);
|
|
164
|
+
const spawnsJob = /tools\.exec_command\s*\(/.test(input);
|
|
165
|
+
return spawnsJob || !interactsWithSession;
|
|
166
|
+
}
|
|
48
167
|
function recordToolOutput(payload, telemetry) {
|
|
49
168
|
const callId = string(payload.call_id);
|
|
50
169
|
if (callId === undefined)
|
|
@@ -54,12 +173,18 @@ function recordToolOutput(payload, telemetry) {
|
|
|
54
173
|
return;
|
|
55
174
|
const result = structuredResult(payload.output);
|
|
56
175
|
if (call.name === 'exec') {
|
|
57
|
-
if (
|
|
176
|
+
if (call.spawnsBackgroundJob === true &&
|
|
177
|
+
result.exitCode === undefined &&
|
|
178
|
+
result.pendingCellId !== undefined)
|
|
58
179
|
telemetry.pendingCells.add(result.pendingCellId);
|
|
59
180
|
return;
|
|
60
181
|
}
|
|
61
|
-
if (call.cellId
|
|
182
|
+
if (call.cellId === undefined)
|
|
183
|
+
return;
|
|
184
|
+
if (result.exitCode !== undefined)
|
|
62
185
|
telemetry.resolvedCells.add(call.cellId);
|
|
186
|
+
if (result.notFoundCellId === call.cellId)
|
|
187
|
+
telemetry.unresolvableCells.add(call.cellId);
|
|
63
188
|
}
|
|
64
189
|
export async function runCodexStopHook(input) {
|
|
65
190
|
const payload = record(input);
|
|
@@ -103,24 +228,29 @@ function structuredResult(value) {
|
|
|
103
228
|
const texts = outputTexts(value);
|
|
104
229
|
let exitCode;
|
|
105
230
|
let pendingCellId;
|
|
231
|
+
let notFoundCellId;
|
|
106
232
|
for (const text of texts) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
else if (renderedCode !== undefined)
|
|
114
|
-
exitCode = Number(renderedCode);
|
|
115
|
-
const pending = /Script running with cell ID ([^\s]+)/.exec(text)?.[1];
|
|
116
|
-
if (pending !== undefined)
|
|
117
|
-
pendingCellId = pending;
|
|
233
|
+
exitCode = exitCodeInText(text) ?? exitCode;
|
|
234
|
+
pendingCellId = /Script running with cell ID ([^\s]+)/.exec(text)?.[1] ?? pendingCellId;
|
|
235
|
+
// A cell-identity-specific protocol error: the runtime has discarded
|
|
236
|
+
// this cell's bookkeeping and no exit code will ever be available for
|
|
237
|
+
// it, distinct from a generic tool/script failure with a real message.
|
|
238
|
+
notFoundCellId = /exec cell ([^\s]+) not found/.exec(text)?.[1] ?? notFoundCellId;
|
|
118
239
|
}
|
|
119
240
|
return {
|
|
120
241
|
...(exitCode === undefined ? {} : { exitCode }),
|
|
121
242
|
...(pendingCellId === undefined ? {} : { pendingCellId }),
|
|
243
|
+
...(notFoundCellId === undefined ? {} : { notFoundCellId }),
|
|
122
244
|
};
|
|
123
245
|
}
|
|
246
|
+
function exitCodeInText(text) {
|
|
247
|
+
const result = parseJson(text);
|
|
248
|
+
const code = result === undefined ? undefined : number(result.exit_code);
|
|
249
|
+
if (code !== undefined)
|
|
250
|
+
return code;
|
|
251
|
+
const rendered = /Exit code:\s*(-?\d+)/.exec(text)?.[1];
|
|
252
|
+
return rendered === undefined ? undefined : Number(rendered);
|
|
253
|
+
}
|
|
124
254
|
function outputTexts(value) {
|
|
125
255
|
if (typeof value === 'string')
|
|
126
256
|
return [value];
|