repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

refactor(ultra): split complete runner path

66a87ef7d12bdfe7c5b4e1eb92c21753ad1d0c45

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/ultra/__tests__/runner.test.ts

Modified
253 253 expect(seen[2].length).toBeGreaterThan(seen[1].length);
256 + it("returns a later valid retry result after corrective feedback", async () => {
257 + const seen: CompleteMessage[][] = [];
258 + const completeFn = vi.fn(
259 + async (req: CompleteRequest): Promise<CompleteResponse> => {
260 + seen.push(req.messages.map((m) => ({ ...m })));
261 + return seen.length === 1
262 + ? { text: '{ "wrong": 1 }' }
263 + : { text: '{ "verdict": "ok" }' };
264 + },
265 + );
266 + const drivers = makeDrivers({ completeFn });
268 + const result = await runStep(
269 + toollessStep,
270 + makeCtx(),
271 + drivers,
272 + baseOpts({ maxRetries: 2 }),
273 + );
275 + expect(result).toEqual({ ok: true, value: { verdict: "ok" } });
276 + expect(completeFn).toHaveBeenCalledTimes(2);
277 + expect(seen[1].at(-1)?.content).toContain("schema validation failed");
278 + });
256 280 it("drops before calling completeFn when auth is unavailable", async () => {
257 281 const completeFn = vi.fn(
258 282 async (): Promise<CompleteResponse> => ({ text: "{}" }),

extensions/ultra/runner.ts

Modified
349 349 validator: CompiledValidator | undefined,
350 350 timeoutMs: number,
351 351 ): Promise<RunStepResult> {
352 - const { agentId, phase, onProgress } = opts;
353 - onProgress?.({ agentId, phase, kind: "start", controls: NOOP_CONTROLS });
355 - const finish = (result: RunStepResult): RunStepResult => {
356 - // `status` must be a token the progress reducer can classify (its
357 - // DROP_STATUSES set), so a drop is COUNTED as dropped, not silently as
358 - // done; the human-readable reason rides on `text`.
359 - onProgress?.({
360 - agentId,
361 - phase,
362 - kind: "end",
363 - status: result.ok ? "done" : "dropped",
364 - text: result.ok ? undefined : result.reason,
365 - controls: NOOP_CONTROLS,
366 - });
367 - return result;
368 - };
352 + emitCompleteStart(opts);
370 354 const model = drivers.resolveModel(step.model);
371 355 const auth = await drivers.resolveAuth(model);
372 - // `ok` is the registry's authoritative success signal: on `{ ok: true }` the
373 - // auth is resolved even when `apiKey` is undefined — header-auth providers
374 - // (e.g. MiniMax) deliver the credential entirely via `headers`. Requiring
375 - // `apiKey` here wrongly drops every such model. We forward BOTH `apiKey` (may
376 - // be undefined) and `headers` to `completeFn`, so whichever the provider uses
377 - // flows through. (The tool-using path never reaches this guard — the SDK
378 - // session resolves its own auth.)
379 356 if (!auth.ok) {
380 - return finish({
357 + return finishComplete(opts, {
381 358 ok: false,
382 359 value: null,
383 360 reason: auth.error ?? "model auth unavailable",
387 - const systemPrompt = [
388 - opts.systemPrompt,
389 - "Respond with ONLY a single JSON object — no prose, no markdown code fences.",
390 - ]
391 - .filter(Boolean)
392 - .join("\n\n");
394 364 const messages: CompleteMessage[] = [{ role: "user", content: step.prompt }];
395 365 let lastReason = "schema validation failed";
397 367 for (let attempt = 0; attempt <= opts.maxRetries; attempt++) {
398 - const guarded = await raceGuards(
399 - (signal) =>
400 - drivers.completeFn({
401 - model,
402 - systemPrompt,
403 - messages,
404 - apiKey: auth.apiKey,
405 - headers: auth.headers,
406 - signal,
407 - }),
368 + const attemptResult = await completeAttempt({
369 + drivers,
370 + model,
371 + auth,
372 + messages,
373 + validator,
374 + systemPrompt: completeSystemPrompt(opts.systemPrompt),
408 375 timeoutMs,
409 - opts.signal,
410 - );
411 - if (guarded.outcome === "timeout")
412 - return finish({ ok: false, value: null, reason: "timeout" });
413 - if (guarded.outcome === "aborted")
414 - return finish({ ok: false, value: null, reason: "aborted" });
376 + signal: opts.signal,
377 + });
416 - const response = guarded.value;
417 - const text = response?.text ?? "";
379 + if (attemptResult.done)
380 + return finishComplete(opts, attemptResult.result);
419 - // Surface the REAL failure rather than letting it fall through to a
420 - // misleading "no JSON object in response": a thrown `complete` (carried on
421 - // `guarded.error`) or a provider error envelope (`errorMessage`) is the
422 - // actual drop reason. Only when the call genuinely succeeded do we parse.
423 - if (!response || guarded.error !== undefined) {
424 - lastReason =
425 - guarded.error instanceof Error
426 - ? `completion failed: ${guarded.error.message}`
427 - : "completion failed (no response)";
428 - } else if (response.errorMessage) {
429 - lastReason = `model error: ${response.errorMessage}`;
430 - } else {
431 - const parsed = parseJsonObject(text);
432 - if (!parsed.ok) {
433 - lastReason = parsed.reason;
434 - } else if (validator && !validator.Check(parsed.value)) {
435 - lastReason = `schema validation failed: ${formatValidatorErrors(validator, parsed.value)}`;
436 - } else {
437 - return finish({ ok: true, value: parsed.value });
438 - }
439 - }
441 - if (attempt < opts.maxRetries) {
442 - messages.push({ role: "assistant", content: text });
443 - messages.push({
444 - role: "user",
445 - content: `Your previous response was invalid (${lastReason}). Respond again with ONLY a valid JSON object matching the required schema.`,
446 - });
447 - }
382 + lastReason = attemptResult.reason;
383 + if (attempt < opts.maxRetries)
384 + appendCorrection(messages, attemptResult.text, lastReason);
450 - return finish({ ok: false, value: null, reason: lastReason });
387 + return finishComplete(opts, { ok: false, value: null, reason: lastReason });
390 +function emitCompleteStart(opts: RunStepOptions): void {
391 + opts.onProgress?.({
392 + agentId: opts.agentId,
393 + phase: opts.phase,
394 + kind: "start",
395 + controls: NOOP_CONTROLS,
396 + });
399 +function finishComplete(
400 + opts: RunStepOptions,
401 + result: RunStepResult,
402 +): RunStepResult {
403 + // `status` must be a reducer token. The free-form reason rides on `text`.
404 + opts.onProgress?.({
405 + agentId: opts.agentId,
406 + phase: opts.phase,
407 + kind: "end",
408 + status: result.ok ? "done" : "dropped",
409 + text: result.ok ? undefined : result.reason,
410 + controls: NOOP_CONTROLS,
411 + });
412 + return result;
415 +function completeSystemPrompt(extra: string | undefined): string {
416 + return [
417 + extra,
418 + "Respond with ONLY a single JSON object — no prose, no markdown code fences.",
420 + .filter(Boolean)
421 + .join("\n\n");
424 +type CompleteAttempt =
425 + | { done: true; result: RunStepResult }
426 + | { done: false; reason: string; text: string };
428 +interface CompleteAttemptInput {
429 + drivers: StepDrivers;
430 + model: unknown;
431 + auth: ResolvedAuth;
432 + messages: CompleteMessage[];
433 + validator: CompiledValidator | undefined;
434 + systemPrompt: string;
435 + timeoutMs: number;
436 + signal?: AbortSignal;
439 +async function completeAttempt(
440 + input: CompleteAttemptInput,
441 +): Promise<CompleteAttempt> {
442 + const guarded = await raceGuards(
443 + (signal) =>
444 + input.drivers.completeFn({
445 + model: input.model,
446 + systemPrompt: input.systemPrompt,
447 + messages: input.messages,
448 + apiKey: input.auth.apiKey,
449 + headers: input.auth.headers,
450 + signal,
451 + }),
452 + input.timeoutMs,
453 + input.signal,
454 + );
456 + if (guarded.outcome === "timeout")
457 + return { done: true, result: { ok: false, value: null, reason: "timeout" } };
458 + if (guarded.outcome === "aborted")
459 + return { done: true, result: { ok: false, value: null, reason: "aborted" } };
461 + return evaluateCompletion(guarded, input.validator);
464 +function evaluateCompletion(
465 + guarded: Extract<GuardOutcome<CompleteResponse>, { outcome: "done" }>,
466 + validator: CompiledValidator | undefined,
467 +): CompleteAttempt {
468 + const response = guarded.value;
469 + const text = response?.text ?? "";
470 + const transportFailure = completionFailureReason(response, guarded.error);
471 + if (transportFailure) return { done: false, reason: transportFailure, text };
472 + return evaluateJson(text, validator);
475 +function completionFailureReason(
476 + response: CompleteResponse | undefined,
477 + error: unknown,
478 +): string | undefined {
479 + if (!response || error !== undefined) {
480 + return error instanceof Error
481 + ? `completion failed: ${error.message}`
482 + : "completion failed (no response)";
484 + return response.errorMessage
485 + ? `model error: ${response.errorMessage}`
486 + : undefined;
489 +function evaluateJson(
490 + text: string,
491 + validator: CompiledValidator | undefined,
492 +): CompleteAttempt {
493 + const parsed = parseJsonObject(text);
494 + if (!parsed.ok) return { done: false, reason: parsed.reason, text };
495 + if (validator && !validator.Check(parsed.value)) {
496 + return {
497 + done: false,
498 + reason: `schema validation failed: ${formatValidatorErrors(validator, parsed.value)}`,
499 + text,
500 + };
502 + return { done: true, result: { ok: true, value: parsed.value } };
505 +function appendCorrection(
506 + messages: CompleteMessage[],
507 + badText: string,
508 + reason: string,
509 +): void {
510 + messages.push({ role: "assistant", content: badText });
511 + messages.push({
512 + role: "user",
513 + content: `Your previous response was invalid (${reason}). Respond again with ONLY a valid JSON object matching the required schema.`,
514 + });
453 517 // ---- tool-using path ------------------------------------------------------