repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(ultra): remove subagent timeouts

3ce30eaafcded80ef49d404651291420f2b5ea56

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (13)

extensions/ultra/README.md

Modified
82 82 `<cwd>/settings.json` (later overriding earlier), with the runtime settings
83 83 manager on top.
85 -| Key | Default | Meaning |
86 -| -------------------------- | -------- | -------------------------------------------------- |
87 -| `ultra.enabled` | `true` | When false, the tool + command refuse to run. |
88 -| `ultra.concurrency` | `5` | Max concurrent sub-agents in a `fanout` phase. |
89 -| `ultra.maxRetries` | `2` | Schema-validation retries per step. |
90 -| `ultra.defaultTimeoutMs` | `180000` | Per-step timeout when a step omits `timeoutMs`. |
91 -| `ultra.subagentExtensions` | `"all"` | Load host extensions into sub-agents, or not. |
92 -| `ultra.modelTiers` | `{}` | Optional `small` / `medium` / `big` model aliases. |
85 +| Key | Default | Meaning |
86 +| -------------------------- | ------- | -------------------------------------------------- |
87 +| `ultra.enabled` | `true` | When false, the tool + command refuse to run. |
88 +| `ultra.concurrency` | `5` | Max concurrent sub-agents in a `fanout` phase. |
89 +| `ultra.maxRetries` | `2` | Schema-validation retries per step. |
90 +| `ultra.subagentExtensions` | `"all"` | Load host extensions into sub-agents, or not. |
91 +| `ultra.modelTiers` | `{}` | Optional `small` / `medium` / `big` model aliases. |
94 93 `ultra.modelTiers` maps workflow step `model` values of `small`, `medium`, or `big` to concrete model refs.
95 94 Unset tiers fall back to the session model.

extensions/ultra/__tests__/progress.test.ts

Modified
252 252 const s = fold([
253 253 pe({ agentId: "a", phase: "p1", kind: "end", status: "done" }),
254 254 pe({ agentId: "b", phase: "p1", kind: "end", status: "dropped" }),
255 - pe({ agentId: "c", phase: "p1", kind: "end", status: "timeout" }),
255 + pe({ agentId: "c", phase: "p1", kind: "end", status: "failed" }),
257 257 expect(s).toMatchObject({ total: 3, done: 1, dropped: 2 });

extensions/ultra/__tests__/runner.test.ts

Modified
44 44 function baseOpts(over: Partial<RunStepOptions> = {}): RunStepOptions {
45 45 return {
46 46 maxRetries: 2,
47 - defaultTimeoutMs: 1000,
48 47 phase: "review",
49 48 agentId: "review#0",
50 49 ...over,
84 83 interface FakeSessionBehavior {
85 84 /** Scripted prompt body; receives the live emit + the captured factory args. */
86 85 drive?: (api: DriveApi) => Promise<void> | void;
87 - /** When true, `prompt()` never resolves (used for timeout / mid-run abort). */
86 + /** When true, `prompt()` never resolves (used for cancellation). */
88 87 hang?: boolean;
490 489 expect(state.transcripts["review#0"]).toContain("schema validation failed");
493 - it("times out a stalled completeFn → reason 'timeout'", async () => {
494 - const completeFn = vi.fn(() => new Promise<CompleteResponse>(() => {}));
495 - const drivers = makeDrivers({ completeFn });
492 + it("observes cancellation triggered while a completion starts", async () => {
493 + const controller = new AbortController();
494 + const completeFn = vi.fn(() => {
495 + controller.abort();
496 + return new Promise<CompleteResponse>(() => {});
497 + });
497 - const result = await runStep(
499 + const result = await Promise.race([
500 + runStep(
501 + toollessStep,
502 + makeCtx(),
503 + makeDrivers({ completeFn }),
504 + baseOpts({ signal: controller.signal }),
505 + ),
506 + new Promise<"missed">((resolve) =>
507 + setTimeout(() => resolve("missed"), 20),
508 + ),
509 + ]);
511 + expect(result).toEqual({ ok: false, value: null, reason: "aborted" });
512 + });
514 + it("lets a stalled completeFn run until it completes", async () => {
515 + let finish!: (value: CompleteResponse) => void;
516 + const completeFn = vi.fn(
517 + () =>
518 + new Promise<CompleteResponse>((resolve) => {
519 + finish = resolve;
520 + }),
521 + );
522 + const pending = runStep(
498 523 toollessStep,
499 524 makeCtx(),
500 - drivers,
501 - baseOpts({ defaultTimeoutMs: 10 }),
525 + makeDrivers({ completeFn }),
526 + baseOpts(),
528 + let settled = false;
529 + void pending.finally(() => {
530 + settled = true;
531 + });
504 - expect(result).toEqual({ ok: false, value: null, reason: "timeout" });
533 + await new Promise((resolve) => setTimeout(resolve, 20));
534 + expect(settled).toBe(false);
535 + finish({ text: '{ "verdict": "ok" }' });
536 + await expect(pending).resolves.toEqual({
537 + ok: true,
538 + value: { verdict: "ok" },
539 + });
686 721 expect(harness.captured.spies?.dispose).toHaveBeenCalledTimes(1);
689 - it("times out a stalled session → reason 'timeout' and calls session.abort()", async () => {
690 - const harness = makeSessionHarness({ hang: true });
691 - const drivers = makeDrivers({ sessionFactory: harness.factory });
693 - const result = await runStep(
724 + it("lets a stalled session run until it completes", async () => {
725 + let release!: () => void;
726 + const harness = makeSessionHarness({
727 + drive: async ({ emit, callStructured }) => {
728 + await new Promise<void>((resolve) => {
729 + release = resolve;
730 + });
731 + await callStructured({ verdict: "ok" });
732 + emit({ type: "agent_end", messages: [], willRetry: false });
733 + },
734 + });
735 + const pending = runStep(
694 736 toolStep,
695 737 makeCtx(),
696 - drivers,
697 - baseOpts({ defaultTimeoutMs: 10 }),
738 + makeDrivers({ sessionFactory: harness.factory }),
739 + baseOpts(),
741 + let settled = false;
742 + void pending.finally(() => {
743 + settled = true;
744 + });
700 - expect(result).toEqual({ ok: false, value: null, reason: "timeout" });
701 - expect(harness.captured.spies?.abort).toHaveBeenCalledTimes(1);
746 + await new Promise((resolve) => setTimeout(resolve, 20));
747 + expect(settled).toBe(false);
748 + release();
749 + await expect(pending).resolves.toEqual({
750 + ok: true,
751 + value: { verdict: "ok" },
752 + });
753 + expect(harness.captured.spies?.abort).not.toHaveBeenCalled();
702 754 expect(harness.captured.spies?.unsubscribe).toHaveBeenCalledTimes(1);
703 755 expect(harness.captured.spies?.dispose).toHaveBeenCalledTimes(1);
712 764 toolStep,
713 765 makeCtx(),
714 766 drivers,
715 - baseOpts({ signal: controller.signal, defaultTimeoutMs: 5000 }),
767 + baseOpts({ signal: controller.signal }),
717 769 // let the factory resolve + the abort listener register, then abort
718 770 await new Promise((r) => setTimeout(r, 0));
733 785 toolStep,
734 786 makeCtx(),
735 787 drivers,
736 - baseOpts({ signal: AbortSignal.abort(), defaultTimeoutMs: 5000 }),
788 + baseOpts({ signal: AbortSignal.abort() }),
739 791 expect(result).toEqual({ ok: false, value: null, reason: "aborted" });
758 810 const runner = makeStepRunner(drivers, ctx, {
759 811 maxRetries: 2,
760 - defaultTimeoutMs: 1000,
762 813 const result = await runner({
763 814 step: toollessStep,
811 862 const runner = makeStepRunner(drivers, ctx, {
812 863 maxRetries: 1,
813 - defaultTimeoutMs: 1000,
815 865 const result = await runner({
816 866 step: toolStep,

extensions/ultra/__tests__/settings.test.ts

Modified
12 12 enabled: true,
13 13 concurrency: 5,
14 14 maxRetries: 2,
15 - defaultTimeoutMs: 180000,
16 15 subagentExtensions: "all",
17 16 modelTiers: {},
61 60 concurrency: 8,
62 61 maxRetries: 1,
63 62 enabled: false,
64 - defaultTimeoutMs: 5000,
65 63 modelTiers: {
66 64 small: "openai/gpt-5-mini:low",
67 65 medium: "anthropic/claude-sonnet-4",
74 72 enabled: false,
75 73 concurrency: 8,
76 74 maxRetries: 1,
77 - defaultTimeoutMs: 5000,
78 75 modelTiers: {
79 76 small: "openai/gpt-5-mini:low",
80 77 medium: "anthropic/claude-sonnet-4",
118 115 enabled: true,
119 116 concurrency: 9,
120 117 maxRetries: 4,
121 - defaultTimeoutMs: 180000,
122 118 subagentExtensions: "all",
123 119 modelTiers: { big: "openai/gpt-5" },
144 140 enabled: true,
145 141 concurrency: 10,
146 142 maxRetries: 9,
147 - defaultTimeoutMs: 180000,
148 143 subagentExtensions: "all",
149 144 modelTiers: {},

extensions/ultra/__tests__/spec.test.ts

Modified
154 154 expect(() => parseWorkflow(s)).toThrow();
157 + it("rejects unknown step fields", () => {
158 + const s = validSpec();
159 + s.phases[0].step.unknown = true;
160 + expect(() => parseWorkflow(s)).toThrow(/additional properties/i);
161 + });
157 163 it("throws a clear message when a step references an unknown schema name", () => {
158 164 const s = validSpec();
159 165 s.phases[1].step.schema = "Nonexistent";

extensions/ultra/__tests__/ui.test.ts

Modified
127 127 // A run with a running agent whose latest action + transcript carry distinct
128 - // sentinels, plus a dropped (timeout) agent.
128 + // sentinels, plus a dropped agent.
129 129 const scripted = stateFrom([
130 130 pe({ agentId: "a", phase: "review", kind: "start" }),
142 142 text: "SECRET-TRANSCRIPT-BODY",
144 144 pe({ agentId: "b", phase: "review", kind: "start" }),
145 - pe({ agentId: "b", phase: "review", kind: "end", status: "timeout" }),
145 + pe({ agentId: "b", phase: "review", kind: "end", status: "failed" }),
148 148 it("collapsed: shows phase/agent counts + summary + expand hint, never bodies", () => {

extensions/ultra/__tests__/wire.test.ts

Modified
49 49 phaseResults: {
50 50 review: [
51 - { ok: false, value: null, reason: "timeout" },
51 + { ok: false, value: null, reason: "provider error" },
53 53 ok: true,
54 54 value: {
75 75 expect(markdown).toContain("# ultra review");
76 76 expect(markdown).toContain("No final results");
77 77 expect(markdown).toContain("review: 2/3 ok, 1 dropped");
78 - expect(markdown).toContain("review#0: timeout");
78 + expect(markdown).toContain("review#0: provider error");
79 79 expect(markdown).toContain("Refuted findings");
80 80 expect(markdown).toContain("possible issue");
81 81 expect(markdown).toContain("not reproducible");

extensions/ultra/index.ts

Modified
141 141 const baseOpts: BaseStepOptions = {
142 142 maxRetries: cfg.maxRetries,
143 - defaultTimeoutMs: cfg.defaultTimeoutMs,
144 143 schemas: spec.schemas,
145 144 modelTiers: cfg.modelTiers,

extensions/ultra/progress.ts

Modified
301 301 // Statuses on an `end` event that mean the agent did not produce a usable
302 302 // result. Anything else (incl. "done"/undefined on an `end`) counts as done.
303 -const DROP_STATUSES = new Set([
304 - "dropped",
305 - "error",
306 - "aborted",
307 - "timeout",
308 - "failed",
309 -]);
303 +const DROP_STATUSES = new Set(["dropped", "error", "aborted", "failed"]);
311 305 function classifyEnd(status: string | undefined): AgentStatus {
312 306 return status !== undefined && DROP_STATUSES.has(status) ? "dropped" : "done";

extensions/ultra/runner.ts

Modified
24 24 // Validated SDK facts honoured (prototypes P1/P4):
25 25 // - `createAgentSession`/`prompt` take NO external AbortSignal — the only way
26 26 // to stop a running sub-agent is the instance method `session.abort()`, so
27 -// BOTH the timeout and the external-abort paths call it.
27 +// the external-abort path calls it.
28 28 // - streaming text arrives on `event.assistantMessageEvent` as
29 29 // `{ type:"text_delta", delta }`; a terminating tool's structured payload is
30 30 // on `tool_execution_end.result.details` (read by progress.ts's mapper).
161 161 /** Run-level options closed over by `makeStepRunner`. */
162 162 export interface BaseStepOptions {
163 163 maxRetries: number;
164 - defaultTimeoutMs: number;
165 164 /** Named schemas (`spec.schemas`) for resolving a `step.schema` string ref. */
166 165 schemas?: Record<string, object>;
167 166 /** Optional local aliases used by step.model: small|medium|big. */
359 358 type GuardOutcome<T> =
360 359 | { outcome: "done"; value: T; error?: unknown }
361 - | { outcome: "timeout" }
362 360 | { outcome: "aborted" };
365 - * Race `work` against a per-call timeout and an external abort signal. `work`
366 - * receives an internal `AbortSignal` that fires on timeout/abort — the tool-less
367 - * `complete()` path threads it through; the session path ignores it (sessions
368 - * accept no external signal — P1) and the caller calls `session.abort()` itself.
363 + * Race `work` against external cancellation only. Tool-less completions receive
364 + * that signal directly; sessions ignore it and are aborted by their caller.
369 365 * Resolves (never rejects); a thrown `work` is reported as `done` with `value`
370 366 * left `undefined` so the caller can retry/drop.
372 -async function raceGuards<T>(
373 - work: (signal: AbortSignal) => Promise<T>,
374 - timeoutMs: number,
368 +async function raceAbort<T>(
369 + work: (signal: AbortSignal | undefined) => Promise<T>,
375 370 externalSignal: AbortSignal | undefined,
376 371 ): Promise<GuardOutcome<T>> {
377 372 if (externalSignal?.aborted) return { outcome: "aborted" };
379 - const controller = new AbortController();
380 - let timeoutId: ReturnType<typeof setTimeout> | undefined;
381 - let onAbort: (() => void) | undefined;
374 + const run = (): Promise<GuardOutcome<T>> =>
375 + work(externalSignal).then(
376 + (value) => ({ outcome: "done", value }) as GuardOutcome<T>,
377 + // A thrown `work` is reported as `done` with no `value` so the caller can
378 + // retry/drop — but the error rides along on `error` so the drop reason can
379 + // name the real failure (an API/auth throw from `complete`) instead of the
380 + // misleading downstream "no JSON object in response".
381 + (error) =>
382 + ({
383 + outcome: "done",
384 + value: undefined as unknown as T,
385 + error,
386 + }) as GuardOutcome<T>,
387 + );
388 + if (!externalSignal) return run();
383 - const guard = new Promise<GuardOutcome<T>>((resolve) => {
384 - timeoutId = setTimeout(() => {
385 - controller.abort();
386 - resolve({ outcome: "timeout" });
387 - }, timeoutMs);
388 - if (externalSignal) {
389 - onAbort = () => {
390 - controller.abort();
391 - resolve({ outcome: "aborted" });
392 - };
393 - externalSignal.addEventListener("abort", onAbort, { once: true });
394 - }
390 + let onAbort!: () => void;
391 + const aborted = new Promise<GuardOutcome<T>>((resolve) => {
392 + onAbort = () => resolve({ outcome: "aborted" });
393 + externalSignal.addEventListener("abort", onAbort, { once: true });
397 - const workP: Promise<GuardOutcome<T>> = work(controller.signal).then(
398 - (value) => ({ outcome: "done", value }) as GuardOutcome<T>,
399 - // A thrown `work` is reported as `done` with no `value` so the caller can
400 - // retry/drop — but the error rides along on `error` so the drop reason can
401 - // name the real failure (an API/auth throw from `complete`) instead of the
402 - // misleading downstream "no JSON object in response".
403 - (error) =>
404 - ({
405 - outcome: "done",
406 - value: undefined as unknown as T,
407 - error,
408 - }) as GuardOutcome<T>,
409 - );
395 + if (externalSignal.aborted) onAbort();
412 - return await Promise.race([workP, guard]);
397 + return await Promise.race([run(), aborted]);
413 398 } finally {
414 - if (timeoutId) clearTimeout(timeoutId);
415 - if (externalSignal && onAbort)
416 - externalSignal.removeEventListener("abort", onAbort);
399 + externalSignal.removeEventListener("abort", onAbort);
433 416 const validator = schemaObj
434 417 ? (compileSchema(schemaObj) as CompiledValidator)
435 418 : undefined;
436 - const timeoutMs = resolvedStep.timeoutMs ?? opts.defaultTimeoutMs;
437 419 const usesTools =
438 420 Array.isArray(resolvedStep.tools) && resolvedStep.tools.length > 0;
446 428 schemaObj,
447 429 validator,
448 - timeoutMs,
451 - : runCompleteStep(resolvedStep, drivers, opts, validator, timeoutMs, tier);
432 + : runCompleteStep(resolvedStep, drivers, opts, validator, tier);
454 435 // ---- tool-less path -------------------------------------------------------
458 439 drivers: StepDrivers,
459 440 opts: RunStepOptions,
460 441 validator: CompiledValidator | undefined,
461 - timeoutMs: number,
462 442 tier: string | undefined,
463 443 ): Promise<RunStepResult> {
464 444 const startedAt = Date.now();
489 469 messages,
490 470 validator,
491 471 systemPrompt: completeSystemPrompt(opts.systemPrompt),
492 - timeoutMs,
493 472 signal: opts.signal,
556 535 messages: CompleteMessage[];
557 536 validator: CompiledValidator | undefined;
558 537 systemPrompt: string;
559 - timeoutMs: number;
560 538 signal?: AbortSignal;
563 541 async function completeAttempt(
564 542 input: CompleteAttemptInput,
565 543 ): Promise<CompleteAttempt> {
566 - const guarded = await raceGuards(
544 + const guarded = await raceAbort(
567 545 (signal) =>
568 546 input.drivers.completeFn({
569 547 model: input.model,
573 551 headers: input.auth.headers,
574 552 signal,
576 - input.timeoutMs,
577 554 input.signal,
580 - if (guarded.outcome === "timeout")
581 - return {
582 - done: true,
583 - result: { ok: false, value: null, reason: "timeout" },
584 - };
585 557 if (guarded.outcome === "aborted")
586 558 return {
587 559 done: true,
654 626 opts: RunStepOptions,
655 627 schemaObj: object | undefined,
656 628 validator: CompiledValidator | undefined,
657 - timeoutMs: number,
658 629 tier: string | undefined,
659 630 ): Promise<RunStepResult> {
660 631 const { agentId, phase, onProgress } = opts;
736 - const guarded = await raceGuards(
707 + const guarded = await raceAbort(
737 708 () => live.prompt(step.prompt, { source: "extension" }),
738 - timeoutMs,
739 709 opts.signal,
742 712 if (guarded.outcome !== "done") {
743 713 // Sessions accept no external signal (P1) — the ONLY way to stop a
744 - // running sub-agent is its own `abort()`, on both timeout and abort.
714 + // running sub-agent is its own `abort()`.
745 715 await Promise.resolve(live.abort()).catch(() => {});
746 716 return { ok: false, value: null, reason: guarded.outcome };

extensions/ultra/settings.ts

Modified
13 13 concurrency: number;
14 14 /** Schema-validation retries per step. */
15 15 maxRetries: number;
16 - /** Per-step timeout when the step omits `timeoutMs`. */
17 - defaultTimeoutMs: number;
18 16 /** Load host extensions into sub-agents so their tools are grantable
19 17 * (`"all"`, default) or keep sub-agents isolated to core tools (`"none"`).
20 18 * The `step.tools` allowlist still gates which tools are actually active. */
28 26 enabled: true,
29 27 concurrency: 5,
30 28 maxRetries: 2,
31 - defaultTimeoutMs: 180000,
32 29 subagentExtensions: "all",
33 30 modelTiers: {},
61 58 if (typeof ultra.concurrency === "number")
62 59 out.concurrency = ultra.concurrency;
63 60 if (typeof ultra.maxRetries === "number") out.maxRetries = ultra.maxRetries;
64 - if (typeof ultra.defaultTimeoutMs === "number")
65 - out.defaultTimeoutMs = ultra.defaultTimeoutMs;
66 61 if (ultra.subagentExtensions === "none" || ultra.subagentExtensions === "all")
67 62 out.subagentExtensions = ultra.subagentExtensions;
68 63 const modelTiers = pickModelTiers(ultra.modelTiers);
82 77 typeof p.concurrency === "number" ? p.concurrency : into.concurrency,
83 78 maxRetries:
84 79 typeof p.maxRetries === "number" ? p.maxRetries : into.maxRetries,
85 - defaultTimeoutMs:
86 - typeof p.defaultTimeoutMs === "number"
87 - ? p.defaultTimeoutMs
88 - : into.defaultTimeoutMs,
89 80 subagentExtensions:
90 81 p.subagentExtensions === "none" || p.subagentExtensions === "all"
91 82 ? p.subagentExtensions

extensions/ultra/skills/authoring-workflows/references/spec-format.md

Modified
59 59 | `model` | no | string | `provider/id` override, or configured tier `small`/`medium`/`big`; defaults to the session model. |
60 60 | `thinkingLevel` | no | off…xhigh | Reasoning budget for this step. |
61 61 | `schema` | no | string \| JSON Schema | A `schemas` name, or an inline schema. Forces structured output. |
62 -| `timeoutMs` | no | number | Per-agent timeout. |
64 63 A string `schema` is a **name reference** and must resolve in the spec's
65 64 `schemas` map, or validation fails.

extensions/ultra/spec.ts

Modified
25 25 /** A step's `schema` is either a named reference (string) or an inline object. */
26 26 const StepSchemaRef = Type.Union([Type.String(), JsonSchemaObject]);
28 -export const StepSchema = Type.Object({
29 - prompt: Type.String(),
30 - tools: Type.Optional(Type.Array(Type.String())),
31 - model: Type.Optional(Type.String()),
32 - thinkingLevel: Type.Optional(ThinkingLevelSchema),
33 - schema: Type.Optional(StepSchemaRef),
34 - timeoutMs: Type.Optional(Type.Number()),
35 -});
28 +export const StepSchema = Type.Object(
29 + {
30 + prompt: Type.String(),
31 + tools: Type.Optional(Type.Array(Type.String())),
32 + model: Type.Optional(Type.String()),
33 + thinkingLevel: Type.Optional(ThinkingLevelSchema),
34 + schema: Type.Optional(StepSchemaRef),
35 + },
36 + { additionalProperties: false },
37 +);
37 39 /** `over` accepts a literal array or an interpolation expression string. */
38 40 const OverSchema = Type.Union([Type.Array(Type.Unknown()), Type.String()]);