@atolis-hq/wake 0.3.110 → 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 = "g8195817";
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');
@@ -270,7 +270,7 @@ export class InboundTranslator {
270
270
  const intake = evaluateIntakeRules(this.intake, gitHubIntakeFacts(payload));
271
271
  if (intake.ignored)
272
272
  return;
273
- const identity = await this.resolveIdentity({ adapter: this.adapter, key: payload.externalKey }, intake.admitted);
273
+ const identity = await this.resolveIdentity(event.eventId, { adapter: this.adapter, key: payload.externalKey }, intake.admitted);
274
274
  if (identity === null)
275
275
  return;
276
276
  if (!identity.created) {
@@ -281,6 +281,7 @@ export class InboundTranslator {
281
281
  const workItemIdValue = identity.workItemId;
282
282
  if (workItemIdValue === null)
283
283
  throw new Error('Created identity is missing a WorkItem');
284
+ await this.recordAdmissionStarted(event, identity);
284
285
  const isPullRequest = payload.kind === 'pull-request';
285
286
  await admitObservedWork(this.admissionServices(), {
286
287
  adapter: this.adapter,
@@ -453,8 +454,13 @@ export class InboundTranslator {
453
454
  routing: this.routing,
454
455
  };
455
456
  }
456
- async resolveIdentity(externalKey, admitted) {
457
+ async resolveIdentity(sourceEventId, externalKey, admitted) {
457
458
  const key = `${externalKey.adapter}:${externalKey.key}`;
459
+ const recorded = await this.recordedAdmissionIdentity(sourceEventId);
460
+ if (recorded !== undefined) {
461
+ this.minted.set(key, recorded);
462
+ return this.resumeAdmissionIdentity(recorded);
463
+ }
458
464
  const inBatch = this.minted.get(key);
459
465
  if (inBatch !== undefined)
460
466
  return this.inBatchIdentity(inBatch);
@@ -468,6 +474,49 @@ export class InboundTranslator {
468
474
  return null;
469
475
  return { ...this.mintIdentity(externalKey), created: true, deleted: false };
470
476
  }
477
+ async recordedAdmissionIdentity(sourceEventId) {
478
+ const event = (await this.journal.readStream(integrationStream(this.adapter)))
479
+ .map(selectGitHubAdapterEvent)
480
+ .find((candidate) => candidate?.eventType === GitHubEventType.AdmissionStarted &&
481
+ candidate.payload.sourceEventId === sourceEventId);
482
+ if (event?.eventType !== GitHubEventType.AdmissionStarted)
483
+ return undefined;
484
+ return { resourceId: event.payload.resourceId, workItemId: event.payload.workItemId };
485
+ }
486
+ async resumeAdmissionIdentity(identity) {
487
+ const work = await this.work.get(identity.workItemId);
488
+ if (work?.deleted)
489
+ return { ...identity, created: false, deleted: true };
490
+ const correlation = (await this.resources.correlations(identity.resourceId)).find((candidate) => candidate.role === ResourceCorrelationRole.Primary &&
491
+ candidate.workItemId === identity.workItemId);
492
+ return { ...identity, created: correlation === undefined || work === null, deleted: false };
493
+ }
494
+ async recordAdmissionStarted(source, identity) {
495
+ if (identity.workItemId === null)
496
+ throw new Error('Created identity is missing a WorkItem');
497
+ const stream = integrationStream(this.adapter);
498
+ const eventId = `github:admission-started:${this.adapter}:${source.eventId}`;
499
+ const existing = await this.journal.readStream(stream);
500
+ if (existing.some((event) => event.eventId === eventId))
501
+ return;
502
+ await this.journal.append(stream, existing.length, [
503
+ createEventDraft({
504
+ eventId,
505
+ eventType: GitHubEventType.AdmissionStarted,
506
+ occurredAt: source.occurredAt,
507
+ correlationId: source.correlationId,
508
+ causationId: source.eventId,
509
+ actor: { kind: EventActorKind.Integration, id: this.adapter },
510
+ source: { kind: EventSourceKind.Internal, id: 'github-inbound-translator' },
511
+ stream,
512
+ payload: {
513
+ sourceEventId: source.eventId,
514
+ resourceId: identity.resourceId,
515
+ workItemId: identity.workItemId,
516
+ },
517
+ }),
518
+ ]);
519
+ }
471
520
  async inBatchIdentity(identity) {
472
521
  const existing = await this.work?.get(identity.workItemId);
473
522
  return { ...identity, created: false, deleted: existing?.deleted === true };
@@ -1,6 +1,7 @@
1
1
  import { z } from 'zod';
2
2
  import { ProviderPermission, PullRequestCheckState, PullRequestState, ReviewActorKind, ReviewerAuthorizationSource, } from '../../../activities/index.js';
3
3
  import { brandedStringSchema, eventEnvelopeSchema, } from '../../../kernel/index.js';
4
+ import { resourceId } from '../../../resources/index.js';
4
5
  import { workItemId } from '../../../work/index.js';
5
6
  import { adapterId } from '../../contracts/identifiers.js';
6
7
  import { ExternalWorkOutcome } from '../../contracts/outcome-vocabulary.js';
@@ -10,6 +11,7 @@ export const GitHubEventType = {
10
11
  CommentObserved: 'integration.github.comment-observed',
11
12
  DeliveryObserved: 'integration.github.delivery-observed',
12
13
  DeletedWorkObservationSkipped: 'integration.github.deleted-work-observation-skipped',
14
+ AdmissionStarted: 'integration.github.admission-started',
13
15
  InboundTranslationRetried: 'integration.github.inbound-translation-retried',
14
16
  InboundTranslationRecovered: 'integration.github.inbound-translation-recovered',
15
17
  InboundTranslationFailed: 'integration.github.inbound-translation-failed',
@@ -77,6 +79,17 @@ const eventSchema = z.discriminatedUnion('eventType', [
77
79
  })
78
80
  .strict(),
79
81
  }),
82
+ eventEnvelopeSchema.extend({
83
+ eventType: z.literal(GitHubEventType.AdmissionStarted),
84
+ stream: streamSchema,
85
+ payload: z
86
+ .object({
87
+ sourceEventId: z.string(),
88
+ resourceId: brandedStringSchema(resourceId),
89
+ workItemId: brandedStringSchema(workItemId),
90
+ })
91
+ .strict(),
92
+ }),
80
93
  eventEnvelopeSchema.extend({
81
94
  eventType: z.literal(GitHubEventType.CommentObserved),
82
95
  stream: streamSchema,
@@ -76,7 +76,7 @@ export class FileEventJournal {
76
76
  this.changeSignalSource.notify();
77
77
  }
78
78
  return finalizedEnvelopes;
79
- });
79
+ }, { waitMs: 5_000, retryIntervalMs: 25 });
80
80
  }
81
81
  // Reads via the persisted per-segment manifest when the in-memory cache is
82
82
  // cold (fresh process, or an on-disk change this instance didn't make),
@@ -317,10 +317,11 @@ async function linuxProcessStartTicks(pid) {
317
317
  throw new Error('Invalid process start ticks');
318
318
  return startTicks;
319
319
  }
320
- export async function withFileLock(path, operation) {
321
- const lock = await acquireFileLock(path, {
320
+ export async function withFileLock(path, operation, options = {}) {
321
+ const lock = await acquireFileLockWithWait(path, {
322
322
  staleAfterMs: 60_000,
323
323
  staleRequiresDeadProcess: true,
324
+ ...options,
324
325
  });
325
326
  if (!lock.acquired)
326
327
  throw new Error(`File lock is already held: ${path}`);
@@ -331,3 +332,18 @@ export async function withFileLock(path, operation) {
331
332
  await lock.release();
332
333
  }
333
334
  }
335
+ async function acquireFileLockWithWait(path, options) {
336
+ const waitMs = options.waitMs ?? 0;
337
+ const retryIntervalMs = options.retryIntervalMs ?? 25;
338
+ const deadline = Date.now() + waitMs;
339
+ do {
340
+ const lock = await acquireFileLock(path, options);
341
+ if (lock.acquired || Date.now() >= deadline)
342
+ return lock;
343
+ await delay(Math.min(retryIntervalMs, Math.max(0, deadline - Date.now())));
344
+ } while (Date.now() < deadline);
345
+ return acquireFileLock(path, options);
346
+ }
347
+ function delay(milliseconds) {
348
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
349
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.3.110",
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": {