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;
387 + deps.runConsultation.
mockImplementation(
388 + (_ctx, request, options)
=> 389 + new Promise((resolve)
=> {
390 + signal
= options.signal;
391 + release = ()
=> resolve(
result(request.origin));
395 + testSession
= await createTestSession({
396 + extensionFactories: [
createAngelExtension(deps)],
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.")]
: []),
406 + await testSession.session[delivery](
"Next task");
407 + expect(signal?.aborted).
toBe(
false);
410 + expect(testSession.events.
toolResultsFor(
"angel")[
0]?.
text).
toContain(
414 + testSession.session.sessionManager
417 + (entry
: { type
: string; customType
?: string })
=> 418 + entry.
type === "custom" && entry.customType
=== "angel-advice",
422 + testSession.session.messages.
some(
424 + message.role
=== "user" && 425 + JSON.
stringify(message).
includes(
"Next task"),
429 + testSession.session.sessionManager
430 + .
buildSessionContext()
432 + (message
: { role
: string; customType
?: string })
=> 433 + message.role
=== "custom" && 434 + message.customType
=== "angel-advice",
440 + it(
"starts executor work after ordinary input arrives during authentication",
async ()
=> {
441 + const deps
= dependencies();
442 + testSession
= await createTestSession({
443 + extensionFactories: [
createAngelExtension(deps)],
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(
453 + new Promise((resolve)
=> {
454 + releaseAuth
= resolve;
457 + const running
= testSession.
run(
458 + when(
"Investigate", [
459 + calls(
"angel", { question:
"What is the cause?" }),
463 + await vi.
waitFor(()
=> expect(releaseAuth).
toBeDefined());
464 + await runner.
emit({
467 + source:
"interactive",
469 + releaseAuth?.(auth);
471 + expect(deps.runConsultation).
toHaveBeenCalledOnce();
472 + expect(testSession.events.
toolResultsFor(
"angel")[
0]?.
text).
toContain(
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;
1302 + testSession
= await createTestSession({
1303 + extensionFactories: [
createAngelExtension(deps)],
1305 + const runner
= testSession.session.extensionRunner;
1306 + await emitTurnEnd(runner, {
1309 + message:
assistantCall(
"bash",
"first", { command:
"check" }),
1310 + toolResults: [
toolResult(
"bash",
"first")],
1312 + const pending
= emitTurnEnd(runner, {
1315 + message:
assistantCall(
"bash",
"repeat", { command:
"check" }),
1316 + toolResults: [
toolResult(
"bash",
"repeat")],
1318 + await vi.
waitFor(()
=> expect(finish).
toBeDefined());
1319 + await runner.
emit({
1321 + text:
"A different task",
1322 + source:
"interactive",
1324 + expect(signal?.aborted).
toBe(
false);
1325 + finish?.(
result(
"error"));
1328 + testSession.session.messages.
filter(
1330 + message.role
=== "custom" && message.customType
=== "angel-advice",
1332 + ).
toHaveLength(
0);
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();
477 + const cancelConsultations = ()
=> {
478 + cancellationGeneration
+= 1;
475 479 for (
const controller
of consultations) controller.
abort();
477 481 const tracksFailure = (toolName
: string)
=> {
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");
571 574 generation
!== sessionGeneration
|| 572 - task
!== taskGeneration
575 + controller.signal.aborted
574 577 throw abortError();
575 578 const extensions
= resolveSubagentExtensions(
610 613 onProgress: (progress)
=> {
611 - if (active
&& generation
=== sessionGeneration)
616 + generation
=== sessionGeneration
&& 617 + !controller.signal.aborted
612 619 options.
onProgress?.(progress);
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);
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);
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");
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");
1025 1033 const generation
= sessionGeneration;
1026 - const task
= taskGeneration;
1034 + const cancellation
= cancellationGeneration;
1027 1035 await ctx.
waitForIdle();
1030 1038 generation
!== sessionGeneration
|| 1031 - task
!== taskGeneration
1039 + cancellation
!== cancellationGeneration
1034 1042 const parentLeafId
= ctx.sessionManager.
getLeafId();
1100 1108 generation
!== sessionGeneration
|| 1101 - task
!== taskGeneration
|| 1109 + cancellation
!== cancellationGeneration
|| 1102 1110 parentLeafId
!== ctx.sessionManager.
getLeafId()