@pikiloom/kernel 0.2.7 → 0.2.8

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.
@@ -32,6 +32,8 @@ export declare function claudeEffectiveContextWindow(advertised: number | null):
32
32
  export declare function handleClaudeEvent(ev: any, s: any, emit: (e: DriverEvent) => void): void;
33
33
  export declare function claudeBgHoldCapMs(): number;
34
34
  export declare function claudeBgSettleQuietMs(): number;
35
+ export declare function claudeModelStallMs(): number;
36
+ export declare function claudeUserEventHasToolResult(ev: any): boolean;
35
37
  export declare function isTerminalTaskStatus(status: unknown): boolean;
36
38
  export declare function trackClaudeBackgroundTask(ev: any, s: any): void;
37
39
  export declare function markClaudeTaskNotificationTerminal(content: any, s: any): void;
@@ -77,6 +77,10 @@ export class ClaudeDriver {
77
77
  // wake-up turns can still land before we close (see claudeBgSettleQuietMs).
78
78
  let holdCapTimer = null;
79
79
  let quietTimer = null;
80
+ // modelStall: post-tool watchdog — fires only while the turn is waiting on the MODEL (a
81
+ // tool_result handed control back and no reply is streaming), never while a tool or
82
+ // background task is still running (see armModelStall).
83
+ let modelStallTimer = null;
80
84
  const usageOf = () => this.usage(state);
81
85
  const unref = (tm) => { if (tm && typeof tm.unref === 'function')
82
86
  tm.unref(); };
@@ -88,6 +92,10 @@ export class ClaudeDriver {
88
92
  clearTimeout(quietTimer);
89
93
  quietTimer = null;
90
94
  } };
95
+ const clearModelStall = () => { if (modelStallTimer) {
96
+ clearTimeout(modelStallTimer);
97
+ modelStallTimer = null;
98
+ } };
91
99
  // kill=true SIGTERMs immediately — fast exit, used once nothing is left running in the
92
100
  // background (a normal turn, or a wake-up turn after every background task finished).
93
101
  // kill=false only ends stdin and lets Claude shut down on its own, so any still-running
@@ -99,6 +107,7 @@ export class ClaudeDriver {
99
107
  settled = true;
100
108
  clearHoldCap();
101
109
  clearQuiet();
110
+ clearModelStall();
102
111
  try {
103
112
  child?.stdin?.end();
104
113
  }
@@ -121,7 +130,7 @@ export class ClaudeDriver {
121
130
  resolve(r);
122
131
  };
123
132
  const settleResult = (opts = {}) => finish({
124
- ok: !state.error, text: state.text, reasoning: state.reasoning || undefined,
133
+ ok: opts.ok ?? !state.error, text: state.text, reasoning: state.reasoning || undefined,
125
134
  error: state.error, stopReason: opts.stopReason ?? state.stopReason, sessionId: state.sessionId, usage: usageOf(),
126
135
  }, opts.kill ?? true);
127
136
  // Absolute cap while holding for a still-running background task (stopReason marks it as
@@ -141,6 +150,29 @@ export class ClaudeDriver {
141
150
  settleResult({ kill: false }); }, claudeBgSettleQuietMs());
142
151
  unref(quietTimer);
143
152
  };
153
+ // Post-tool model-stall watchdog: after a tool_result hands control back to the model, the
154
+ // model normally streams its next message within a couple of seconds. If instead it goes
155
+ // fully silent — no stream/assistant events — the turn is stuck waiting on the MODEL (a
156
+ // provider stall / rate-limit backoff), NOT on a tool (a running tool has no tool_result
157
+ // yet, so this is never armed then) and NOT on background work (its own hold, re-checked
158
+ // below). Left alone the kernel turn hangs forever with the answer never delivered. Bound
159
+ // it: settle gracefully (no kill) as incomplete with stopReason 'stalled' so the terminal
160
+ // shows a clear "resend to continue" note instead of a dead spinner. Armed on tool_result,
161
+ // cleared the moment the model emits anything.
162
+ const armModelStall = () => {
163
+ clearModelStall();
164
+ modelStallTimer = setTimeout(() => {
165
+ if (settled)
166
+ return;
167
+ // Background work legitimately produces no stream events; keep waiting (the bg hold owns it).
168
+ if (pendingClaudeBackgroundTasks(state) > 0) {
169
+ armModelStall();
170
+ return;
171
+ }
172
+ settleResult({ stopReason: 'stalled', kill: false, ok: false });
173
+ }, claudeModelStallMs());
174
+ unref(modelStallTimer);
175
+ };
144
176
  try {
145
177
  child = spawn(this.bin, args, { cwd: input.workdir, env: { ...process.env, ...(input.env || {}) }, stdio: ['pipe', 'pipe', 'pipe'] });
146
178
  }
@@ -190,7 +222,11 @@ export class ClaudeDriver {
190
222
  }
191
223
  handleClaudeEvent(ev, state, ctx.emit);
192
224
  const pending = pendingClaudeBackgroundTasks(state);
225
+ // Any model output means the model is alive and streaming — cancel the post-tool stall watchdog.
226
+ if (ev.type === 'stream_event' || ev.type === 'assistant')
227
+ clearModelStall();
193
228
  if (ev.type === 'result') {
229
+ clearModelStall();
194
230
  const hasError = !!ev.is_error || (Array.isArray(ev.errors) && ev.errors.length > 0) || !!state.error;
195
231
  const decision = decideClaudeResultSettle({ hasError, pendingBackground: pending, sawBackground: state.bgStarted.size > 0 });
196
232
  if (decision === 'settle') {
@@ -221,6 +257,10 @@ export class ClaudeDriver {
221
257
  else
222
258
  armQuiet();
223
259
  }
260
+ // A tool_result handed control back to the model with no background pending — arm the
261
+ // post-tool stall watchdog. Cleared above the moment the model's next event streams in.
262
+ if (ev.type === 'user' && pending === 0 && claudeUserEventHasToolResult(ev))
263
+ armModelStall();
224
264
  }
225
265
  });
226
266
  child.stderr.on('data', (c) => { stderr += c.toString('utf8'); });
@@ -559,6 +599,23 @@ export function claudeBgSettleQuietMs() {
559
599
  const raw = Number(process.env.PIKILOOM_CLAUDE_BG_SETTLE_QUIET_MS);
560
600
  return Number.isFinite(raw) && raw > 0 ? raw : CLAUDE_BG_SETTLE_QUIET_DEFAULT_MS;
561
601
  }
602
+ // How long the model may stay COMPLETELY silent after a tool_result (control handed back to it,
603
+ // no background pending) before the driver gives up and settles the turn as 'stalled'. Deliberately
604
+ // generous: legitimate silent extended-thinking (subscription accounts stream no thinking text) and
605
+ // slow providers must not trip it, and a still-running tool never trips it (it has no tool_result
606
+ // yet). This is the backstop for a turn that would otherwise hang forever with the answer never
607
+ // delivered. Override with PIKILOOM_CLAUDE_MODEL_STALL_MS.
608
+ const CLAUDE_MODEL_STALL_DEFAULT_MS = 120_000;
609
+ export function claudeModelStallMs() {
610
+ const raw = Number(process.env.PIKILOOM_CLAUDE_MODEL_STALL_MS);
611
+ return Number.isFinite(raw) && raw > 0 ? raw : CLAUDE_MODEL_STALL_DEFAULT_MS;
612
+ }
613
+ // True when a claude `type:'user'` stream event carries at least one tool_result block — i.e. the
614
+ // tool loop just handed control back to the model. Pure + exported for hermetic testing.
615
+ export function claudeUserEventHasToolResult(ev) {
616
+ const c = ev?.message?.content;
617
+ return Array.isArray(c) && c.some((b) => b?.type === 'tool_result');
618
+ }
562
619
  // After we settle a held turn gracefully (stdin closed, no kill), force-kill the lingering
563
620
  // process only if it hasn't exited on its own within this window. Backstop against leaks.
564
621
  const CLAUDE_EXIT_LEAK_GUARD_MS = 15_000;
package/dist/index.d.ts CHANGED
@@ -9,7 +9,7 @@ export type { SessionStore, CoreSessionRecord, ModelResolver, ModelInjection, To
9
9
  export type { LoomIO, PromptInput, Surface, SurfaceCapabilities, Plugin, SpawnContribution, } from './contracts/surface.js';
10
10
  export { FsSessionStore, NullModelResolver, NoopToolProvider, PassthroughSystemPromptBuilder, AutoCancelInteractionHandler, DeferToTerminalInteractionHandler, NoopCatalog, defaultBaseDir, } from './ports/defaults.js';
11
11
  export { EchoDriver } from './drivers/echo.js';
12
- export { ClaudeDriver, isTerminalTaskStatus, trackClaudeBackgroundTask, pendingClaudeBackgroundTasks, markClaudeTaskNotificationTerminal, decideClaudeResultSettle, claudeBgHoldCapMs, claudeBgSettleQuietMs, type ClaudeResultSettleDecision, } from './drivers/claude.js';
12
+ export { ClaudeDriver, isTerminalTaskStatus, trackClaudeBackgroundTask, pendingClaudeBackgroundTasks, markClaudeTaskNotificationTerminal, decideClaudeResultSettle, claudeBgHoldCapMs, claudeBgSettleQuietMs, type ClaudeResultSettleDecision, claudeModelStallMs, claudeUserEventHasToolResult, } from './drivers/claude.js';
13
13
  export { CodexDriver } from './drivers/codex.js';
14
14
  export { GeminiDriver } from './drivers/gemini.js';
15
15
  export { AcpDriver, type AcpDriverConfig } from './drivers/acp.js';
package/dist/index.js CHANGED
@@ -19,7 +19,7 @@ export { attachTui } from './runtime/tui.js';
19
19
  export { FsSessionStore, NullModelResolver, NoopToolProvider, PassthroughSystemPromptBuilder, AutoCancelInteractionHandler, DeferToTerminalInteractionHandler, NoopCatalog, defaultBaseDir, } from './ports/defaults.js';
20
20
  // Drivers & surfaces (also available via subpath exports)
21
21
  export { EchoDriver } from './drivers/echo.js';
22
- export { ClaudeDriver, isTerminalTaskStatus, trackClaudeBackgroundTask, pendingClaudeBackgroundTasks, markClaudeTaskNotificationTerminal, decideClaudeResultSettle, claudeBgHoldCapMs, claudeBgSettleQuietMs, } from './drivers/claude.js';
22
+ export { ClaudeDriver, isTerminalTaskStatus, trackClaudeBackgroundTask, pendingClaudeBackgroundTasks, markClaudeTaskNotificationTerminal, decideClaudeResultSettle, claudeBgHoldCapMs, claudeBgSettleQuietMs, claudeModelStallMs, claudeUserEventHasToolResult, } from './drivers/claude.js';
23
23
  export { CodexDriver } from './drivers/codex.js';
24
24
  export { GeminiDriver } from './drivers/gemini.js';
25
25
  export { AcpDriver } from './drivers/acp.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikiloom/kernel",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
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",