repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(angel): preserve consultations across follow-up input

149ad72fc80f7766ecfc57899ff26135576f525d

Oliver Jan Krylow <oliver@bugabinga.net> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (2)

extensions/angel/__tests__/harness.test.ts

Modified
374 374 ).toHaveLength(0);
377 + it.each(["steer", "followUp"] as const)(
378 + "keeps executor advice alive across queued %s input",
379 + async (delivery) => {
380 + const deps = dependencies();
381 + let release: (() => void) | undefined;
382 + let signal: AbortSignal | undefined;
383 + let markStarted: (() => void) | undefined;
384 + const started = new Promise<void>((resolve) => {
385 + markStarted = resolve;
386 + });
387 + deps.runConsultation.mockImplementation(
388 + (_ctx, request, options) =>
389 + new Promise((resolve) => {
390 + signal = options.signal;
391 + release = () => resolve(result(request.origin));
392 + markStarted?.();
393 + }),
394 + );
395 + testSession = await createTestSession({
396 + extensionFactories: [createAngelExtension(deps)],
397 + });
398 + const running = testSession.run(
399 + when("Investigate", [
400 + calls("angel", { question: "What is the cause?" }),
401 + says("First task finished."),
402 + ...(delivery === "followUp" ? [says("Next task finished.")] : []),
403 + ]),
404 + );
405 + await started;
406 + await testSession.session[delivery]("Next task");
407 + expect(signal?.aborted).toBe(false);
408 + release?.();
409 + await running;
410 + expect(testSession.events.toolResultsFor("angel")[0]?.text).toContain(
411 + "executor advice",
412 + );
413 + expect(
414 + testSession.session.sessionManager
415 + .getEntries()
416 + .filter(
417 + (entry: { type: string; customType?: string }) =>
418 + entry.type === "custom" && entry.customType === "angel-advice",
419 + ),
420 + ).toHaveLength(1);
421 + expect(
422 + testSession.session.messages.some(
423 + (message) =>
424 + message.role === "user" &&
425 + JSON.stringify(message).includes("Next task"),
426 + ),
427 + ).toBe(true);
428 + expect(
429 + testSession.session.sessionManager
430 + .buildSessionContext()
431 + .messages.filter(
432 + (message: { role: string; customType?: string }) =>
433 + message.role === "custom" &&
434 + message.customType === "angel-advice",
435 + ),
436 + ).toHaveLength(0);
437 + },
438 + );
440 + it("starts executor work after ordinary input arrives during authentication", async () => {
441 + const deps = dependencies();
442 + testSession = await createTestSession({
443 + extensionFactories: [createAngelExtension(deps)],
444 + });
445 + const runner = testSession.session.extensionRunner;
446 + const context = runner.createCommandContext();
447 + const model = context.model;
448 + if (!model) throw new Error("test model unavailable");
449 + const auth = await context.modelRegistry.getApiKeyAndHeaders(model);
450 + let releaseAuth: ((value: typeof auth) => void) | undefined;
451 + vi.spyOn(context.modelRegistry, "getApiKeyAndHeaders").mockImplementation(
452 + () =>
453 + new Promise((resolve) => {
454 + releaseAuth = resolve;
455 + }),
456 + );
457 + const running = testSession.run(
458 + when("Investigate", [
459 + calls("angel", { question: "What is the cause?" }),
460 + says("Done."),
461 + ]),
462 + );
463 + await vi.waitFor(() => expect(releaseAuth).toBeDefined());
464 + await runner.emit({
465 + type: "input",
466 + text: "Follow up",
467 + source: "interactive",
468 + });
469 + releaseAuth?.(auth);
470 + await running;
471 + expect(deps.runConsultation).toHaveBeenCalledOnce();
472 + expect(testSession.events.toolResultsFor("angel")[0]?.text).toContain(
473 + "executor advice",
474 + );
475 + });
377 477 it("injects advice only after the same opaque operation fails again", async () => {
378 478 const deps = dependencies();
379 479 const contexts: unknown[][] = [];
1188 1288 ).toHaveLength(0);
1291 + it("discards stale automatic advice without aborting it on queued input", async () => {
1292 + const deps = dependencies();
1293 + let finish: ((value: ConsultationResult) => void) | undefined;
1294 + let signal: AbortSignal | undefined;
1295 + deps.runConsultation.mockImplementation(
1296 + (_ctx, _request, options) =>
1297 + new Promise((resolve) => {
1298 + signal = options.signal;
1299 + finish = resolve;
1300 + }),
1301 + );
1302 + testSession = await createTestSession({
1303 + extensionFactories: [createAngelExtension(deps)],
1304 + });
1305 + const runner = testSession.session.extensionRunner;
1306 + await emitTurnEnd(runner, {
1307 + type: "turn_end",
1308 + turnIndex: 0,
1309 + message: assistantCall("bash", "first", { command: "check" }),
1310 + toolResults: [toolResult("bash", "first")],
1311 + });
1312 + const pending = emitTurnEnd(runner, {
1313 + type: "turn_end",
1314 + turnIndex: 1,
1315 + message: assistantCall("bash", "repeat", { command: "check" }),
1316 + toolResults: [toolResult("bash", "repeat")],
1317 + });
1318 + await vi.waitFor(() => expect(finish).toBeDefined());
1319 + await runner.emit({
1320 + type: "input",
1321 + text: "A different task",
1322 + source: "interactive",
1323 + });
1324 + expect(signal?.aborted).toBe(false);
1325 + finish?.(result("error"));
1326 + await pending;
1327 + expect(
1328 + testSession.session.messages.filter(
1329 + (message) =>
1330 + message.role === "custom" && message.customType === "angel-advice",
1331 + ),
1332 + ).toHaveLength(0);
1333 + });
1191 1335 it("reports automatic cancellation without steering failure", async () => {
1192 1336 const deps = dependencies();
1193 1337 deps.runConsultation.mockRejectedValue(

extensions/angel/index.ts

Modified
456 456 let consultationSequence = 0;
457 457 let completedTurnSequence = 0;
458 458 let taskGeneration = 0;
459 + let cancellationGeneration = 0;
459 460 let activeTurnIndex: number | undefined;
460 461 const consultations = new Set<AbortController>();
461 462 const operationKeysByCall = new Map<string, string>();
472 473 const invalidateTask = () => {
473 474 taskGeneration += 1;
474 475 clearFailureTracking();
476 + };
477 + const cancelConsultations = () => {
478 + cancellationGeneration += 1;
475 479 for (const controller of consultations) controller.abort();
477 481 const tracksFailure = (toolName: string) => {
541 545 } = {},
542 546 ): Promise<ConsultationResult> {
543 547 const generation = sessionGeneration;
544 - const task = taskGeneration;
545 548 if (!enabled()) {
546 549 dbg?.("consultation.skip", { reason: "disabled" });
547 550 throw new Error("Angel is disabled in this session");
570 573 !active ||
571 574 generation !== sessionGeneration ||
572 - task !== taskGeneration
575 + controller.signal.aborted
574 577 throw abortError();
575 578 const extensions = resolveSubagentExtensions(
608 611 ? undefined
609 612 : auth.apiKey,
610 613 onProgress: (progress) => {
611 - if (active && generation === sessionGeneration)
614 + if (
615 + active &&
616 + generation === sessionGeneration &&
617 + !controller.signal.aborted
612 619 options.onProgress?.(progress);
616 623 !active ||
617 624 generation !== sessionGeneration ||
618 - task !== taskGeneration
625 + controller.signal.aborted
620 627 throw abortError();
621 628 finish?.("finish", { status: "success" });
721 728 taskGenerationByTurn.delete(event.turnIndex);
722 729 if (activeTurnIndex === event.turnIndex) activeTurnIndex = undefined;
723 730 if (childSession) return;
724 - if (task !== taskGeneration) {
725 - for (const result of event.toolResults)
726 - operationKeysByCall.delete(result.toolCallId);
727 - return;
729 - const turnSequence = completedTurnSequence++;
731 731 for (const result of event.toolResults) {
732 732 if (result.toolName !== "angel") continue;
733 733 if (!isCompleteToolDetails(result.details)) continue;
737 737 metadata: result.details.result.metadata,
740 + if (task !== taskGeneration) {
741 + for (const result of event.toolResults)
742 + operationKeysByCall.delete(result.toolCallId);
743 + return;
745 + const turnSequence = completedTurnSequence++;
741 747 if (!isAvailable(ctx)) {
742 748 for (const result of event.toolResults)
996 1002 const control = question.toLowerCase();
997 1003 if (control === "cancel") {
998 1004 invalidateTask();
1005 + cancelConsultations();
999 1006 if (ctx.hasUI) ctx.ui.notify("Angel consultation cancelled", "info");
1000 1007 return;
1002 1009 if (control === "on" || control === "off") {
1003 1010 sessionEnabled = control === "on";
1004 1011 invalidateTask();
1012 + cancelConsultations();
1005 1013 syncAvailability(ctx);
1006 1014 if (ctx.hasUI) {
1007 1015 const available = pi.getActiveTools().includes("angel");
1023 1031 return;
1025 1033 const generation = sessionGeneration;
1026 - const task = taskGeneration;
1034 + const cancellation = cancellationGeneration;
1027 1035 await ctx.waitForIdle();
1029 1037 !active ||
1030 1038 generation !== sessionGeneration ||
1031 - task !== taskGeneration
1039 + cancellation !== cancellationGeneration
1033 1041 return;
1034 1042 const parentLeafId = ctx.sessionManager.getLeafId();
1099 1107 !active ||
1100 1108 generation !== sessionGeneration ||
1101 - task !== taskGeneration ||
1109 + cancellation !== cancellationGeneration ||
1102 1110 parentLeafId !== ctx.sessionManager.getLeafId()
1104 1112 return;