@pikiloom/kernel 0.3.15 → 0.3.16
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.
- package/dist/drivers/codex.d.ts +8 -1
- package/dist/drivers/codex.js +151 -7
- package/package.json +1 -1
package/dist/drivers/codex.d.ts
CHANGED
|
@@ -19,8 +19,15 @@ export declare function captureCodexAgentMessage(item: any, s: CodexContentState
|
|
|
19
19
|
export declare function captureCodexReasoning(text: string, s: CodexContentState, emit: (e: DriverEvent) => void): void;
|
|
20
20
|
export declare function codexFinalText(s: CodexContentState): string;
|
|
21
21
|
export declare function codexFinalReasoning(s: CodexContentState): string;
|
|
22
|
+
export interface CodexLivenessOptions {
|
|
23
|
+
/** Silence after an accepted steer before the turn is healed (interrupt + replay). */
|
|
24
|
+
steerStallMs?: number;
|
|
25
|
+
/** Idle silence (no running tool, no pending HITL request) before the turn is force-closed. */
|
|
26
|
+
turnStallMs?: number;
|
|
27
|
+
}
|
|
22
28
|
export declare class CodexDriver implements AgentDriver {
|
|
23
29
|
private readonly bin;
|
|
30
|
+
private readonly liveness;
|
|
24
31
|
readonly id = "codex";
|
|
25
32
|
readonly capabilities: {
|
|
26
33
|
steer: boolean;
|
|
@@ -29,7 +36,7 @@ export declare class CodexDriver implements AgentDriver {
|
|
|
29
36
|
tui: boolean;
|
|
30
37
|
fork: boolean;
|
|
31
38
|
};
|
|
32
|
-
constructor(bin?: string);
|
|
39
|
+
constructor(bin?: string, liveness?: CodexLivenessOptions);
|
|
33
40
|
run(input: AgentTurnInput, ctx: DriverContext): Promise<DriverResult>;
|
|
34
41
|
tui(input: TuiInput): TuiSpec;
|
|
35
42
|
listNativeSessions(opts: {
|
package/dist/drivers/codex.js
CHANGED
|
@@ -234,12 +234,47 @@ export function codexFinalText(s) {
|
|
|
234
234
|
export function codexFinalReasoning(s) {
|
|
235
235
|
return s.reasoning.trim() ? s.reasoning : s.thinkParts.join('\n\n');
|
|
236
236
|
}
|
|
237
|
+
// ── Turn liveness (steer race + silent-stall recovery) ──────────────────────────
|
|
238
|
+
// codex app-server has a turn-boundary race: a `turn/steer` that lands in the instant an
|
|
239
|
+
// item completes is ACCEPTED (recorded into the rollout) but never dispatched — the agent
|
|
240
|
+
// loop goes idle, no further notifications arrive, and `turn/completed` never fires, so the
|
|
241
|
+
// turn hangs forever while the process sits at 0% CPU (observed on codex-cli 0.144.x;
|
|
242
|
+
// same family as openai/codex#15714 / #23807). Two defenses below:
|
|
243
|
+
//
|
|
244
|
+
// 1. A successful steer is treated as ACCEPTED, NOT CONSUMED: it stays pending until a
|
|
245
|
+
// progress notification proves the loop picked it up. If the turn instead goes silent
|
|
246
|
+
// (or completes without consuming it), the driver heals in place — interrupt the wedged
|
|
247
|
+
// turn and restart it with the same input. The steered text is already in the thread
|
|
248
|
+
// history, so the worst false-positive cost is one duplicated user message; the turn
|
|
249
|
+
// keeps running under the same run()/task, invisible to upper layers.
|
|
250
|
+
// 2. A generic silence backstop: a turn with no notifications for a long stretch while
|
|
251
|
+
// nothing is visibly in flight (no running tool call, no server->client request awaiting
|
|
252
|
+
// a human) is declared stalled and force-closed, so the session ends as a visible error
|
|
253
|
+
// instead of spinning forever.
|
|
254
|
+
const CODEX_STEER_STALL_MS = 300_000; // matches codex core's own 300s stream watchdog
|
|
255
|
+
const CODEX_TURN_STALL_MS = 900_000; // long: silent thinking stretches are legitimate
|
|
256
|
+
// Notifications that prove the agent loop is making forward progress AFTER a steer.
|
|
257
|
+
// item/completed and tokenUsage are deliberately excluded — they can be the trailing edge
|
|
258
|
+
// of work that finished before the steer landed (the exact race signature). Progress that
|
|
259
|
+
// arrives within CODEX_STEER_CONSUMED_MS of the steer only refreshes the pending marker's
|
|
260
|
+
// clock rather than clearing it: the app-server can flush pre-acceptance stream output in
|
|
261
|
+
// the same write as the steer response, so near-simultaneous events are not proof the
|
|
262
|
+
// loop survived the injection. Progress beyond that window is.
|
|
263
|
+
const CODEX_STEER_CONSUMED_MS = 2_000;
|
|
264
|
+
const CODEX_PROGRESS_METHODS = new Set([
|
|
265
|
+
'turn/started', 'item/started', 'item/agentMessage/delta',
|
|
266
|
+
'item/reasoning/textDelta', 'item/reasoning/summaryTextDelta',
|
|
267
|
+
'item/commandExecution/outputDelta', 'item/fileChange/outputDelta',
|
|
268
|
+
'item/fileChange/patchUpdated', 'turn/plan/updated', 'rawResponseItem/completed',
|
|
269
|
+
]);
|
|
237
270
|
export class CodexDriver {
|
|
238
271
|
bin;
|
|
272
|
+
liveness;
|
|
239
273
|
id = 'codex';
|
|
240
274
|
capabilities = { steer: true, interact: false, resume: true, tui: true, fork: true };
|
|
241
|
-
constructor(bin = 'codex') {
|
|
275
|
+
constructor(bin = 'codex', liveness = {}) {
|
|
242
276
|
this.bin = bin;
|
|
277
|
+
this.liveness = liveness;
|
|
243
278
|
}
|
|
244
279
|
async run(input, ctx) {
|
|
245
280
|
// BYOK provider routing arrives as `-c key=value` overrides; pass them through so the
|
|
@@ -261,6 +296,15 @@ export class CodexDriver {
|
|
|
261
296
|
const deltaItems = new Set();
|
|
262
297
|
let lastTextItemId = null;
|
|
263
298
|
let steerRegistered = false;
|
|
299
|
+
// Liveness state (see the CODEX_STEER_STALL_MS block comment above).
|
|
300
|
+
const steerStallMs = this.liveness.steerStallMs ?? CODEX_STEER_STALL_MS;
|
|
301
|
+
const turnStallMs = this.liveness.turnStallMs ?? CODEX_TURN_STALL_MS;
|
|
302
|
+
let lastEventAt = Date.now();
|
|
303
|
+
let pendingSteer = null;
|
|
304
|
+
let pendingServerRequests = 0;
|
|
305
|
+
let healing = false;
|
|
306
|
+
let stalled = false;
|
|
307
|
+
let livenessTimer = null;
|
|
264
308
|
if (!srv.start())
|
|
265
309
|
return { ok: false, text: '', error: 'failed to start codex app-server', stopReason: 'error' };
|
|
266
310
|
let settled = false;
|
|
@@ -320,23 +364,78 @@ export class CodexDriver {
|
|
|
320
364
|
state.sessionId = threadId;
|
|
321
365
|
ctx.emit({ type: 'session', sessionId: threadId });
|
|
322
366
|
}
|
|
367
|
+
// Heal a wedged/lost steer in place: (optionally) interrupt the dead turn, then restart
|
|
368
|
+
// it with the same input. Runs under the SAME run()/turnDone, so upper layers just see
|
|
369
|
+
// the turn continue. `healing` suppresses the interrupt's own turn/completed echo.
|
|
370
|
+
const heal = async (opts) => {
|
|
371
|
+
if (settled || healing || !pendingSteer)
|
|
372
|
+
return;
|
|
373
|
+
healing = true;
|
|
374
|
+
const replayInput = pendingSteer.input;
|
|
375
|
+
pendingSteer = null;
|
|
376
|
+
if (opts.interrupt && state.sessionId && state.turnId) {
|
|
377
|
+
await srv.request('turn/interrupt', { threadId: state.sessionId, turnId: state.turnId }, 5_000);
|
|
378
|
+
}
|
|
379
|
+
if (settled) {
|
|
380
|
+
healing = false;
|
|
381
|
+
return;
|
|
382
|
+
} // raced with abort / process death
|
|
383
|
+
const resp = await srv.request('turn/start', {
|
|
384
|
+
threadId: state.sessionId,
|
|
385
|
+
input: replayInput,
|
|
386
|
+
model: input.model || undefined,
|
|
387
|
+
effort: input.effort || undefined,
|
|
388
|
+
});
|
|
389
|
+
if (settled) {
|
|
390
|
+
healing = false;
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (resp.error) {
|
|
394
|
+
state.status = 'error';
|
|
395
|
+
state.error = `steer recovery failed: ${resp.error.message || 'turn/start failed'}`;
|
|
396
|
+
healing = false;
|
|
397
|
+
settle();
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
state.turnId = resp.result?.turn?.id ?? state.turnId;
|
|
401
|
+
lastEventAt = Date.now();
|
|
402
|
+
healing = false;
|
|
403
|
+
};
|
|
323
404
|
// Codex emits no explicit compaction event, so track peak occupancy: a sharp
|
|
324
405
|
// mid-turn drop is an auto-compaction, surfaced as a live `compaction` signal.
|
|
325
406
|
let compactPeakTokens = 0;
|
|
326
407
|
srv.onNotification((method, params) => {
|
|
327
408
|
if (params?.threadId && params.threadId !== state.sessionId && method !== 'turn/started')
|
|
328
409
|
return;
|
|
410
|
+
lastEventAt = Date.now();
|
|
411
|
+
if (pendingSteer && CODEX_PROGRESS_METHODS.has(method)) {
|
|
412
|
+
const now = Date.now();
|
|
413
|
+
if (now - pendingSteer.at >= CODEX_STEER_CONSUMED_MS)
|
|
414
|
+
pendingSteer = null; // loop provably alive post-steer
|
|
415
|
+
else
|
|
416
|
+
pendingSteer.progressAt = now; // maybe pre-acceptance flush — keep watching
|
|
417
|
+
}
|
|
329
418
|
switch (method) {
|
|
330
419
|
case 'turn/started':
|
|
331
420
|
state.turnId = params?.turn?.id ?? null;
|
|
332
421
|
if (!steerRegistered && state.turnId) {
|
|
333
422
|
steerRegistered = true;
|
|
334
423
|
ctx.registerSteer(async (prompt, attachments = []) => {
|
|
335
|
-
if (!state.sessionId || !state.turnId)
|
|
424
|
+
if (settled || !state.sessionId || !state.turnId)
|
|
336
425
|
return false;
|
|
337
|
-
const
|
|
338
|
-
|
|
426
|
+
const steerInput = buildTurnInput(prompt, attachments);
|
|
427
|
+
// Arm BEFORE sending — accepted, not yet consumed. Arming after the response
|
|
428
|
+
// would race the response's own microtask against progress notifications the
|
|
429
|
+
// server flushed in the same write, mis-arming against already-consumed steers.
|
|
430
|
+
// Back-to-back steers accumulate so a heal replays everything swallowed.
|
|
431
|
+
const armed = { input: [...(pendingSteer?.input ?? []), ...steerInput], at: Date.now(), progressAt: null };
|
|
432
|
+
pendingSteer = armed;
|
|
433
|
+
const r = await srv.request('turn/steer', { threadId: state.sessionId, expectedTurnId: state.turnId, input: steerInput }, 30_000);
|
|
434
|
+
if (r.error) {
|
|
435
|
+
if (pendingSteer === armed)
|
|
436
|
+
pendingSteer = null; // rejected — nothing to watch
|
|
339
437
|
return false;
|
|
438
|
+
}
|
|
340
439
|
state.turnId = r.result?.turnId ?? state.turnId;
|
|
341
440
|
return true;
|
|
342
441
|
});
|
|
@@ -466,11 +565,21 @@ export class CodexDriver {
|
|
|
466
565
|
}
|
|
467
566
|
case 'turn/completed': {
|
|
468
567
|
const turn = params?.turn || {};
|
|
568
|
+
applyCodexTokenUsage(state, params?.tokenUsage || turn.tokenUsage || turn.usage);
|
|
569
|
+
ctx.emit({ type: 'usage', usage: codexUsageOf(state) });
|
|
570
|
+
if (healing)
|
|
571
|
+
break; // completion echo of the turn heal() just interrupted
|
|
572
|
+
if (pendingSteer && !pendingSteer.progressAt && !ctx.signal.aborted && (turn.status ?? 'completed') === 'completed') {
|
|
573
|
+
// The other face of the steer race: the turn finished without ever consuming
|
|
574
|
+
// the injected input. Replay it as a fresh turn instead of settling — the
|
|
575
|
+
// upper layers already dequeued the message on steer-ok, so settling here
|
|
576
|
+
// would silently drop it.
|
|
577
|
+
void heal({ interrupt: false });
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
469
580
|
state.status = turn.status ?? 'completed';
|
|
470
581
|
if (turn.error)
|
|
471
582
|
state.error = turn.error.message || turn.error.code || 'turn error';
|
|
472
|
-
applyCodexTokenUsage(state, params?.tokenUsage || turn.tokenUsage || turn.usage);
|
|
473
|
-
ctx.emit({ type: 'usage', usage: codexUsageOf(state) });
|
|
474
583
|
settle();
|
|
475
584
|
break;
|
|
476
585
|
}
|
|
@@ -480,6 +589,7 @@ export class CodexDriver {
|
|
|
480
589
|
// accept approvals by default (parity with the legacy codex driver). Never throw —
|
|
481
590
|
// an unanswerable request degrades to an empty response, not a JSON-RPC error.
|
|
482
591
|
srv.onRequest(async (method, params, id) => {
|
|
592
|
+
pendingServerRequests++; // a request awaiting a human legitimately silences the turn
|
|
483
593
|
try {
|
|
484
594
|
if (method === 'item/tool/requestUserInput') {
|
|
485
595
|
const interaction = codexUserInputToInteraction(params, `codex-input-${id}`);
|
|
@@ -497,6 +607,9 @@ export class CodexDriver {
|
|
|
497
607
|
catch {
|
|
498
608
|
return {};
|
|
499
609
|
}
|
|
610
|
+
finally {
|
|
611
|
+
pendingServerRequests--;
|
|
612
|
+
}
|
|
500
613
|
});
|
|
501
614
|
const turnResp = await srv.request('turn/start', {
|
|
502
615
|
threadId: state.sessionId,
|
|
@@ -506,6 +619,35 @@ export class CodexDriver {
|
|
|
506
619
|
});
|
|
507
620
|
if (turnResp.error)
|
|
508
621
|
return { ok: false, text: state.text, error: turnResp.error.message || 'turn/start failed', stopReason: 'error', sessionId: state.sessionId };
|
|
622
|
+
// Liveness checker: heals a stalled steer, force-closes a silently dead turn. The
|
|
623
|
+
// silence clock only accumulates while nothing is visibly in flight — a running tool
|
|
624
|
+
// call or a server->client request parked on a human keeps the turn alive forever.
|
|
625
|
+
const checkEveryMs = Math.max(25, Math.min(5_000, Math.floor(Math.min(steerStallMs, turnStallMs) / 4)));
|
|
626
|
+
livenessTimer = setInterval(() => {
|
|
627
|
+
if (settled || healing)
|
|
628
|
+
return;
|
|
629
|
+
const now = Date.now();
|
|
630
|
+
if (pendingSteer) {
|
|
631
|
+
if (now - (pendingSteer.progressAt ?? pendingSteer.at) >= steerStallMs)
|
|
632
|
+
void heal({ interrupt: true });
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const busy = pendingServerRequests > 0 || [...toolCalls.values()].some(c => c.status === 'running');
|
|
636
|
+
if (busy) {
|
|
637
|
+
lastEventAt = now;
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
if (now - lastEventAt < turnStallMs)
|
|
641
|
+
return;
|
|
642
|
+
stalled = true;
|
|
643
|
+
state.error = state.error || `codex app-server went silent mid-turn (no events for ${Math.round(turnStallMs / 1000)}s); closing the turn`;
|
|
644
|
+
if (state.sessionId && state.turnId) {
|
|
645
|
+
srv.request('turn/interrupt', { threadId: state.sessionId, turnId: state.turnId }, 5_000).finally(() => settle());
|
|
646
|
+
}
|
|
647
|
+
else
|
|
648
|
+
settle();
|
|
649
|
+
}, checkEveryMs);
|
|
650
|
+
livenessTimer.unref?.();
|
|
509
651
|
await turnDone;
|
|
510
652
|
const usage = codexUsageOf(state);
|
|
511
653
|
const ok2 = (state.status === 'completed' || state.status == null) && !state.error && !ctx.signal.aborted;
|
|
@@ -515,7 +657,7 @@ export class CodexDriver {
|
|
|
515
657
|
text: codexFinalText(state),
|
|
516
658
|
reasoning: finalReasoning || undefined,
|
|
517
659
|
error: state.error || (ctx.signal.aborted ? 'Interrupted by user.' : null),
|
|
518
|
-
stopReason: ctx.signal.aborted ? 'interrupted' : (state.status || 'end_turn'),
|
|
660
|
+
stopReason: ctx.signal.aborted ? 'interrupted' : stalled ? 'stalled' : (state.status || 'end_turn'),
|
|
519
661
|
sessionId: state.sessionId,
|
|
520
662
|
// Fork anchor: the turn id is the inclusive keep-boundary thread/fork's lastTurnId takes.
|
|
521
663
|
anchor: state.turnId,
|
|
@@ -523,6 +665,8 @@ export class CodexDriver {
|
|
|
523
665
|
};
|
|
524
666
|
}
|
|
525
667
|
finally {
|
|
668
|
+
if (livenessTimer)
|
|
669
|
+
clearInterval(livenessTimer);
|
|
526
670
|
srv.kill();
|
|
527
671
|
}
|
|
528
672
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pikiloom/kernel",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.16",
|
|
4
4
|
"description": "Heterogeneous coding agents (Claude / Codex / Gemini / ACP) -> an interaction-friendly, accumulating session snapshot + control handle, over pluggable surfaces (IM, Web, tunnel, TUI). The reusable core pikiloom itself is built on.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|