@atolis-hq/wake 0.3.111 → 0.3.112

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.
@@ -108,4 +108,4 @@ export function resolveWakeVersion(options = {}) {
108
108
  return `g${headHash.slice(0, 7)}`;
109
109
  return '0.1.0-dev';
110
110
  }
111
- export const wakeVersion = "g6ea9313";
111
+ export const wakeVersion = "gadbb014";
@@ -0,0 +1,209 @@
1
+ export function structuredResult(value) {
2
+ return outputTexts(value).reduce(mergeResult, {});
3
+ }
4
+ function mergeResult(result, text) {
5
+ const jsonExitCode = jsonExitCodeInText(text);
6
+ const session = sessionIdInText(text);
7
+ const legacyExitCode = legacyExitCodeInText(text);
8
+ const exitCode = jsonExitCode ?? legacyExitCode;
9
+ return {
10
+ ...result,
11
+ ...(jsonExitCode === undefined ? {} : { jsonExitCode }),
12
+ ...(exitCode === undefined ? {} : { exitCode }),
13
+ ...(session === undefined ? {} : { sessionId: session }),
14
+ ...matchValue(text, /Script running with cell ID ([^\s]+)/, 'pendingCellId'),
15
+ ...matchValue(text, /exec cell ([^\s]+) not found/, 'notFoundCellId'),
16
+ };
17
+ }
18
+ function matchValue(text, pattern, key) {
19
+ const value = pattern.exec(text)?.[1];
20
+ return value === undefined ? {} : { [key]: value };
21
+ }
22
+ function jsonExitCodeInText(text) {
23
+ const result = parseJson(text);
24
+ return result === undefined ? undefined : number(result.exit_code);
25
+ }
26
+ function legacyExitCodeInText(text) {
27
+ const rendered = /Exit code:\s*(-?\d+)/.exec(text)?.[1];
28
+ return rendered === undefined ? undefined : Number(rendered);
29
+ }
30
+ function sessionIdInText(text) {
31
+ const result = parseJson(text);
32
+ return result === undefined ? undefined : sessionId(result.session_id);
33
+ }
34
+ function outputTexts(value) {
35
+ if (typeof value === 'string')
36
+ return [value];
37
+ if (!Array.isArray(value))
38
+ return [];
39
+ return value.flatMap((item) => {
40
+ const content = record(item);
41
+ const text = content === undefined ? undefined : string(content.text);
42
+ return text === undefined ? [] : [text];
43
+ });
44
+ }
45
+ export function writeStdinSessionArgument(input) {
46
+ if (typeof input !== 'string')
47
+ return {};
48
+ const code = codeMask(input);
49
+ const calls = [...code.matchAll(/\btools\.write_stdin\s*\(\s*\{/g)];
50
+ if (calls.length !== 1)
51
+ return {};
52
+ const call = calls[0];
53
+ if (call === undefined || call.index === undefined)
54
+ return {};
55
+ const openObject = call.index + call[0].lastIndexOf('{');
56
+ const literal = directSessionIdArgument(input, code, openObject);
57
+ return literal === undefined ? {} : { writeStdinSessionId: literal };
58
+ }
59
+ function directSessionIdArgument(source, code, openObject) {
60
+ const literals = directSessionLiterals(source, code, openObject);
61
+ return literals.length === 1 ? literals[0] : undefined;
62
+ }
63
+ function directSessionLiterals(source, code, openObject) {
64
+ const literals = [];
65
+ let depth = 1;
66
+ for (let index = openObject + 1; index < code.length && depth > 0; index += 1) {
67
+ depth = nextDepth(code[index], depth);
68
+ if (depth !== 1 || !isSessionIdAt(code, index))
69
+ continue;
70
+ const literal = literalAtProperty(source, code, index);
71
+ if (literal === undefined)
72
+ return [];
73
+ literals.push(literal);
74
+ }
75
+ return literals;
76
+ }
77
+ function nextDepth(character, depth) {
78
+ if (character === '{' || character === '[' || character === '(')
79
+ return depth + 1;
80
+ if (character === '}' || character === ']' || character === ')')
81
+ return depth - 1;
82
+ return depth;
83
+ }
84
+ function isSessionIdAt(code, index) {
85
+ const name = 'session_id';
86
+ return (code.startsWith(name, index) &&
87
+ !/[A-Za-z0-9_$]/.test(code[index - 1] ?? '') &&
88
+ !/[A-Za-z0-9_$]/.test(code[index + name.length] ?? ''));
89
+ }
90
+ function literalAtProperty(source, code, index) {
91
+ let valueStart = index + 'session_id'.length;
92
+ while (/\s/.test(code[valueStart] ?? ''))
93
+ valueStart += 1;
94
+ return code[valueStart] === ':' ? sourceSessionLiteralAt(source, valueStart + 1) : undefined;
95
+ }
96
+ function codeMask(source) {
97
+ const characters = source.split('');
98
+ for (let index = 0; index < characters.length; index += 1)
99
+ index = maskAt(characters, index);
100
+ return characters.join('');
101
+ }
102
+ function maskAt(characters, index) {
103
+ const character = characters[index];
104
+ if (character === '/' && characters[index + 1] === '/')
105
+ return maskLineComment(characters, index);
106
+ if (character === '/' && characters[index + 1] === '*')
107
+ return maskBlockComment(characters, index);
108
+ return isQuote(character) ? maskQuoted(characters, index, character) : index;
109
+ }
110
+ function maskLineComment(characters, index) {
111
+ while (index < characters.length && characters[index] !== '\n')
112
+ characters[index++] = ' ';
113
+ return index;
114
+ }
115
+ function maskBlockComment(characters, index) {
116
+ characters[index++] = ' ';
117
+ characters[index++] = ' ';
118
+ while (index < characters.length && !(characters[index] === '*' && characters[index + 1] === '/'))
119
+ characters[index++] = ' ';
120
+ characters[index++] = ' ';
121
+ characters[index] = ' ';
122
+ return index;
123
+ }
124
+ function isQuote(value) {
125
+ return value === '"' || value === "'" || value === '`';
126
+ }
127
+ function maskQuoted(characters, index, quote) {
128
+ characters[index++] = ' ';
129
+ while (index < characters.length) {
130
+ const current = characters[index];
131
+ characters[index++] = ' ';
132
+ if (current === '\\') {
133
+ characters[index++] = ' ';
134
+ continue;
135
+ }
136
+ if (current === quote)
137
+ break;
138
+ }
139
+ return index;
140
+ }
141
+ function sourceSessionLiteralAt(source, start) {
142
+ while (/\s/.test(source[start] ?? ''))
143
+ start += 1;
144
+ const numeric = sourceNumberLiteralAt(source, start);
145
+ if (numeric !== undefined)
146
+ return sessionId(numeric);
147
+ if (source[start] === "'")
148
+ return sessionId(singleQuotedStringAt(source, start));
149
+ if (source[start] !== '"')
150
+ return undefined;
151
+ const quoted = doubleQuotedStringAt(source, start);
152
+ return quoted === undefined ? undefined : sessionId(quoted);
153
+ }
154
+ function doubleQuotedStringAt(source, start) {
155
+ let end = start + 1;
156
+ while (end < source.length) {
157
+ if (source[end] === '\\') {
158
+ end += 2;
159
+ continue;
160
+ }
161
+ if (source[end] === '"') {
162
+ const value = parseJson(`{"value":${source.slice(start, end + 1)}}`);
163
+ return value === undefined ? undefined : string(value.value);
164
+ }
165
+ end += 1;
166
+ }
167
+ return undefined;
168
+ }
169
+ function sourceNumberLiteralAt(source, start) {
170
+ const match = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/y;
171
+ match.lastIndex = start;
172
+ const literal = match.exec(source)?.[0];
173
+ if (literal === undefined || /[A-Za-z0-9_$.]/.test(source[start + literal.length] ?? ''))
174
+ return undefined;
175
+ const value = Number(literal);
176
+ return Number.isFinite(value) ? value : undefined;
177
+ }
178
+ function singleQuotedStringAt(source, start) {
179
+ let end = start + 1;
180
+ while (end < source.length && source[end] !== "'") {
181
+ if (source[end] === '\\')
182
+ return undefined;
183
+ end += 1;
184
+ }
185
+ return end < source.length ? source.slice(start + 1, end) : undefined;
186
+ }
187
+ function parseJson(value) {
188
+ try {
189
+ return record(JSON.parse(value));
190
+ }
191
+ catch {
192
+ return undefined;
193
+ }
194
+ }
195
+ function record(value) {
196
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
197
+ ? value
198
+ : undefined;
199
+ }
200
+ function string(value) {
201
+ return typeof value === 'string' ? value : undefined;
202
+ }
203
+ function number(value) {
204
+ return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
205
+ }
206
+ function sessionId(value) {
207
+ const text = string(value);
208
+ return text === undefined || text.length === 0 ? number(value) : text;
209
+ }
@@ -1,6 +1,7 @@
1
1
  import { readFile, readdir } from 'node:fs/promises';
2
2
  import { join } from 'node:path';
3
3
  import { pathToFileURL } from 'node:url';
4
+ import { structuredResult, writeStdinSessionArgument, } from './codex-stop-hook-protocol.js';
4
5
  export const unverifiedCodexCompletionReason = 'unverified: could not confirm background command completion from Codex structured telemetry';
5
6
  /**
6
7
  * Bound on how long this hook will keep forcing continuation for the same
@@ -46,6 +47,7 @@ export function inspectCodexTranscript(transcript) {
46
47
  pendingCells: new Set(),
47
48
  resolvedCells: new Set(),
48
49
  unresolvableCells: new Set(),
50
+ sessionsByCell: new Map(),
49
51
  };
50
52
  for (const line of lines.slice(boundary + 1)) {
51
53
  const event = parseJson(line);
@@ -144,6 +146,7 @@ function recordToolCall(payload, calls) {
144
146
  name,
145
147
  ...(name === 'wait' ? cellArgument(payload.arguments ?? payload.input) : {}),
146
148
  ...(name === 'exec' ? { spawnsBackgroundJob: spawnsBackgroundJob(payload.input) } : {}),
149
+ ...(name === 'exec' ? writeStdinSessionArgument(payload.input) : {}),
147
150
  });
148
151
  }
149
152
  /**
@@ -166,23 +169,34 @@ function spawnsBackgroundJob(input) {
166
169
  }
167
170
  function recordToolOutput(payload, telemetry) {
168
171
  const callId = string(payload.call_id);
169
- if (callId === undefined)
170
- return;
171
- const call = telemetry.calls.get(callId);
172
+ const call = callId === undefined ? undefined : telemetry.calls.get(callId);
172
173
  if (call === undefined)
173
174
  return;
174
175
  const result = structuredResult(payload.output);
175
- if (call.name === 'exec') {
176
- if (call.spawnsBackgroundJob === true &&
177
- result.exitCode === undefined &&
178
- result.pendingCellId !== undefined)
179
- telemetry.pendingCells.add(result.pendingCellId);
176
+ if (call.name === 'exec')
177
+ return recordExecOutput(call, result, telemetry);
178
+ recordWaitOutput(call, result, telemetry);
179
+ }
180
+ function recordExecOutput(call, result, telemetry) {
181
+ if (call.spawnsBackgroundJob === true && result.exitCode === undefined && result.pendingCellId)
182
+ telemetry.pendingCells.add(result.pendingCellId);
183
+ if (call.writeStdinSessionId === undefined || result.jsonExitCode === undefined)
180
184
  return;
185
+ resolveSession(call.writeStdinSessionId, telemetry);
186
+ }
187
+ function resolveSession(sessionId, telemetry) {
188
+ for (const [cellId, recordedSessionId] of telemetry.sessionsByCell) {
189
+ if (recordedSessionId === sessionId)
190
+ telemetry.resolvedCells.add(cellId);
181
191
  }
192
+ }
193
+ function recordWaitOutput(call, result, telemetry) {
182
194
  if (call.cellId === undefined)
183
195
  return;
184
196
  if (result.exitCode !== undefined)
185
197
  telemetry.resolvedCells.add(call.cellId);
198
+ if (result.sessionId !== undefined)
199
+ telemetry.sessionsByCell.set(call.cellId, result.sessionId);
186
200
  if (result.notFoundCellId === call.cellId)
187
201
  telemetry.unresolvableCells.add(call.cellId);
188
202
  }
@@ -224,44 +238,6 @@ async function transcriptForSession(directory, sessionId) {
224
238
  }
225
239
  return undefined;
226
240
  }
227
- function structuredResult(value) {
228
- const texts = outputTexts(value);
229
- let exitCode;
230
- let pendingCellId;
231
- let notFoundCellId;
232
- for (const text of texts) {
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;
239
- }
240
- return {
241
- ...(exitCode === undefined ? {} : { exitCode }),
242
- ...(pendingCellId === undefined ? {} : { pendingCellId }),
243
- ...(notFoundCellId === undefined ? {} : { notFoundCellId }),
244
- };
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
- }
254
- function outputTexts(value) {
255
- if (typeof value === 'string')
256
- return [value];
257
- if (!Array.isArray(value))
258
- return [];
259
- return value.flatMap((item) => {
260
- const content = record(item);
261
- const text = content === undefined ? undefined : string(content.text);
262
- return text === undefined ? [] : [text];
263
- });
264
- }
265
241
  function cellArgument(value) {
266
242
  const arguments_ = typeof value === 'string' ? parseJson(value) : record(value);
267
243
  const cellId = arguments_ === undefined ? undefined : string(arguments_.cell_id);
@@ -297,9 +273,6 @@ function record(value) {
297
273
  function string(value) {
298
274
  return typeof value === 'string' ? value : undefined;
299
275
  }
300
- function number(value) {
301
- return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
302
- }
303
276
  if (process.argv[1] !== undefined && import.meta.url === pathToFileURL(process.argv[1]).href) {
304
277
  let input = '';
305
278
  process.stdin.setEncoding('utf8');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.3.111",
3
+ "version": "0.3.112",
4
4
  "description": "Local autonomous agent control plane for software development",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {