Skip to content

Commit 9659800

Browse files
Duncanwpfleger96
andcommitted
fix(acp): classify channel_full as transient in transcript reducer
handlePermissionDecisionResult treated every non-sent/already_decided status as an authoritative delivery failure, incrementing deliveryFailed and triggering the PermissionDecisionButtons useEffect to call setPending(null). With channel_full now keeping the retransmit orchestrator alive, this caused two concurrent delivery loops for the same nonce: the original retransmit loop still subscribed, plus a second loop from the newly re-enabled button click. Add channel_full as an early-return alongside sent/already_decided in the reducer. Both channel_full producers (mixed owner-Full/sibling-Sent and pure-Full) are transient queue-saturation conditions; the orchestrator handles resend automatically and the card must stay disabled until the retry settles or the deadline expires. Update LifecycleActivity.tsx deliveryFailed prop doc and the re-enable comment to name the three authoritative statuses (no_active_turn, channel_closed, no_channel) and explain that channel_full does not increment the token. Tests: - Fix buildTranscript_control_result_second_failure_increments_delivery_failed to use no_channel for the second failure (channel_closed would still work, but channel_full was the prior value and is now transient) - Add buildTranscript_control_result_channel_full_does_not_mark_delivery_failed: transcript 79→80; mutation (removing the guard) → test red at expected undefined got 1 - Add test_channel_full_reducer_to_component_buttons_stay_disabled in LifecycleActivity.render.test.mjs: drives full pipeline acp_read → buildTranscript → click → channel_full through reducer → assert both buttons stay disabled + no second delivery starts; companion case (no_active_turn) asserts buttons DO re-enable; mutation → red at disabled assertion Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent a0309cd commit 9659800

4 files changed

Lines changed: 324 additions & 17 deletions

File tree

‎desktop/src/features/agents/ui/activityRenderClasses/LifecycleActivity.render.test.mjs‎

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,249 @@ test("test_f3_cross_layer_four_options_acp_read_to_lifecycle_activity_two_button
517517
);
518518
});
519519

520+
// ---------------------------------------------------------------------------
521+
// Cross-layer reducer+mounted regression: channel_full leaves buttons disabled
522+
//
523+
// Proves that the full pipeline — acp_read → buildTranscript reducer →
524+
// LifecycleActivity component — correctly leaves both buttons DISABLED after
525+
// a `channel_full` control_result, matching the "transient, retransmit
526+
// orchestrator keeps going" contract.
527+
//
528+
// The companion case proves authoritative failures (`no_active_turn`) DO
529+
// re-enable buttons — so the effect path is also covered.
530+
//
531+
// Mutation proof: restoring `channel_full` to increment `deliveryFailed` in
532+
// `handlePermissionDecisionResult` → the card acquires `deliveryFailed: 1` →
533+
// the component re-renders with `deliveryFailed={1}` → the useEffect fires →
534+
// `setPending(null)` re-enables both buttons → the disabled assertion fails.
535+
// ---------------------------------------------------------------------------
536+
537+
test("test_channel_full_reducer_to_component_buttons_stay_disabled", async () => {
538+
const { createElement, act } = await import("react");
539+
const { render, fireEvent } = await import("@testing-library/react");
540+
541+
const FAKE_NOW_SECS = Math.floor(FAKE_NOW_MS / 1000);
542+
const FUTURE_EXPIRY = FAKE_NOW_SECS + 9_999_999;
543+
const nonce = "nonce-cross-layer-cf";
544+
545+
// Base acp_read event.
546+
const acpReadEvent = {
547+
seq: 1,
548+
timestamp: "2026-09-01T10:00:00.000Z",
549+
kind: "acp_read",
550+
agentIndex: 0,
551+
channelId: "ch-cross-cf",
552+
sessionId: "sess-cross-cf",
553+
turnId: "turn-cross-cf",
554+
payload: {
555+
jsonrpc: "2.0",
556+
id: "req-cross-cf",
557+
method: "session/request_permission",
558+
params: {
559+
title: "Tool requires approval",
560+
toolCallId: "tc-cross-cf",
561+
options: [
562+
{
563+
optionId: "opt-allow-once",
564+
kind: "allow_once",
565+
name: "Allow once",
566+
},
567+
{ optionId: "opt-reject-once", kind: "reject_once", name: "Deny" },
568+
],
569+
},
570+
},
571+
authorization: {
572+
requestNonce: nonce,
573+
actionable: true,
574+
expiresAt: FUTURE_EXPIRY,
575+
},
576+
};
577+
578+
// `channel_full` control_result — transient; must NOT set deliveryFailed.
579+
const channelFullResult = {
580+
seq: 2,
581+
timestamp: "2026-09-01T10:00:01.000Z",
582+
kind: "control_result",
583+
agentIndex: 0,
584+
channelId: "ch-cross-cf",
585+
sessionId: "sess-cross-cf",
586+
turnId: "turn-cross-cf",
587+
payload: {
588+
type: "permission_decision",
589+
status: "channel_full",
590+
requestNonce: nonce,
591+
optionId: "opt-allow-once",
592+
},
593+
};
594+
595+
// `no_active_turn` control_result — authoritative failure; MUST set deliveryFailed.
596+
const authoritativeFailure = {
597+
seq: 2,
598+
timestamp: "2026-09-01T10:00:01.000Z",
599+
kind: "control_result",
600+
agentIndex: 0,
601+
channelId: "ch-cross-cf",
602+
sessionId: "sess-cross-cf",
603+
turnId: "turn-cross-cf",
604+
payload: {
605+
type: "permission_decision",
606+
status: "no_active_turn",
607+
requestNonce: nonce,
608+
optionId: "opt-allow-once",
609+
},
610+
};
611+
612+
// Build both card states through the real transcript reducer.
613+
const cardAfterChannelFull = buildTranscript([
614+
acpReadEvent,
615+
channelFullResult,
616+
]).find((i) => i.renderClass === "permission");
617+
const cardAfterAuthoritativeFailure = buildTranscript([
618+
acpReadEvent,
619+
authoritativeFailure,
620+
]).find((i) => i.renderClass === "permission");
621+
assert.ok(cardAfterChannelFull, "card must exist after channel_full");
622+
assert.ok(
623+
cardAfterAuthoritativeFailure,
624+
"card must exist after authoritative failure",
625+
);
626+
627+
// Reducer-level gate: channel_full must NOT set deliveryFailed.
628+
assert.equal(
629+
cardAfterChannelFull.deliveryFailed,
630+
undefined,
631+
"channel_full must not set deliveryFailed in the reducer (mutation: restoring increment → 1 here → test fails)",
632+
);
633+
// Reducer-level gate: no_active_turn MUST set deliveryFailed.
634+
assert.equal(
635+
cardAfterAuthoritativeFailure.deliveryFailed,
636+
1,
637+
"no_active_turn must set deliveryFailed in the reducer",
638+
);
639+
640+
// ── Component: channel_full → buttons stay disabled ───────────────────────
641+
// Start with the initial card (no deliveryFailed), click Allow to set pending.
642+
const initialCard = buildTranscript([acpReadEvent]).find(
643+
(i) => i.renderClass === "permission",
644+
);
645+
646+
// Track delivery calls to ensure no second delivery is started.
647+
const deliveryCalls = [];
648+
// The first delivery is intentionally stalled — never resolves.
649+
function stalledDelivery({ optionId }) {
650+
deliveryCalls.push(optionId);
651+
return new Promise(() => {});
652+
}
653+
654+
let container, rerender;
655+
await act(async () => {
656+
({ container, rerender } = render(
657+
createElement(LifecycleActivity, {
658+
...BASE_PROPS,
659+
item: initialCard,
660+
_deliveryFn: stalledDelivery,
661+
}),
662+
));
663+
});
664+
665+
// Click Allow — sets pending, disables both buttons.
666+
const allowBtn = container.querySelector(
667+
'[data-testid="permission-decision-opt-allow-once"]',
668+
);
669+
assert.ok(allowBtn, "allow_once button must be present before click");
670+
await act(async () => {
671+
fireEvent.click(allowBtn);
672+
await Promise.resolve();
673+
});
674+
assert.equal(deliveryCalls.length, 1, "first delivery must fire on click");
675+
676+
// Now rerender with the post-channel_full card (deliveryFailed undefined).
677+
// The useEffect must NOT fire (deliveryFailed didn't change), so pending stays
678+
// set and both buttons remain disabled.
679+
await act(async () => {
680+
rerender(
681+
createElement(LifecycleActivity, {
682+
...BASE_PROPS,
683+
item: cardAfterChannelFull,
684+
_deliveryFn: stalledDelivery,
685+
}),
686+
);
687+
await Promise.resolve();
688+
});
689+
690+
const allowBtnAfterCF = container.querySelector(
691+
'[data-testid="permission-decision-opt-allow-once"]',
692+
);
693+
const denyBtnAfterCF = container.querySelector(
694+
'[data-testid="permission-decision-opt-reject-once"]',
695+
);
696+
assert.ok(allowBtnAfterCF, "allow button must still be in DOM");
697+
assert.ok(denyBtnAfterCF, "deny button must still be in DOM");
698+
assert.ok(
699+
allowBtnAfterCF.disabled,
700+
"allow button must remain DISABLED after channel_full (mutation: increment deliveryFailed → setPending(null) fires → button enabled → this fails)",
701+
);
702+
assert.ok(
703+
denyBtnAfterCF.disabled,
704+
"deny button must remain DISABLED after channel_full — both buttons stay disabled during automatic retry",
705+
);
706+
assert.equal(
707+
deliveryCalls.length,
708+
1,
709+
"no second delivery must start after channel_full — retransmit orchestrator handles resend, not a second click",
710+
);
711+
712+
// ── Companion: authoritative failure re-enables buttons ───────────────────
713+
// Render a fresh card, click, then rerender with deliveryFailed: 1.
714+
const deliveryCalls2 = [];
715+
function stalledDelivery2({ optionId }) {
716+
deliveryCalls2.push(optionId);
717+
return new Promise(() => {});
718+
}
719+
720+
let container2, rerender2;
721+
await act(async () => {
722+
({ container: container2, rerender: rerender2 } = render(
723+
createElement(LifecycleActivity, {
724+
...BASE_PROPS,
725+
item: initialCard,
726+
_deliveryFn: stalledDelivery2,
727+
}),
728+
));
729+
});
730+
731+
const allowBtn2 = container2.querySelector(
732+
'[data-testid="permission-decision-opt-allow-once"]',
733+
);
734+
assert.ok(allowBtn2, "allow button must be present for companion case");
735+
await act(async () => {
736+
fireEvent.click(allowBtn2);
737+
await Promise.resolve();
738+
});
739+
740+
// Rerender with authoritative failure card (deliveryFailed: 1).
741+
// useEffect sees deliveryFailed change 0→1 → setPending(null) → buttons enabled.
742+
await act(async () => {
743+
rerender2(
744+
createElement(LifecycleActivity, {
745+
...BASE_PROPS,
746+
item: cardAfterAuthoritativeFailure,
747+
_deliveryFn: stalledDelivery2,
748+
}),
749+
);
750+
await Promise.resolve();
751+
await Promise.resolve();
752+
});
753+
754+
const allowBtnAfterFail = container2.querySelector(
755+
'[data-testid="permission-decision-opt-allow-once"]',
756+
);
757+
assert.ok(
758+
!allowBtnAfterFail.disabled,
759+
"allow button must be RE-ENABLED after no_active_turn — user can retry",
760+
);
761+
});
762+
520763
// ---------------------------------------------------------------------------
521764
// F3 interactive delivery-seam: acp_read → buildTranscript → LifecycleActivity
522765
// click buttons → assert _deliveryFn called with ruled allow_once/reject_once IDs

‎desktop/src/features/agents/ui/activityRenderClasses/LifecycleActivity.tsx‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ function PermissionDecisionButtons({
9090
requestNonce: string;
9191
/**
9292
* Monotonically increasing failure token from the reducer — incremented on
93-
* every non-`sent` `control_result`. Keying the effect on this number (not a
94-
* boolean) ensures a second failure after a retry also re-enables buttons.
93+
* every authoritative delivery failure (`no_active_turn`, `channel_closed`,
94+
* `no_channel`). The transient `channel_full` status does NOT increment this
95+
* token; the retransmit orchestrator handles that status automatically.
96+
* Keying the effect on this number (not a boolean) ensures a second failure
97+
* after a retry also re-enables buttons.
9598
*/
9699
deliveryFailed?: number;
97100
/**
@@ -108,9 +111,11 @@ function PermissionDecisionButtons({
108111
const deliveryFn = _deliveryFn ?? startPermissionDecisionDelivery;
109112
const [pending, setPending] = React.useState<string | null>(null);
110113

111-
// Re-enable buttons when the reducer signals delivery failure (non-`sent`
112-
// control_result status). The relay send succeeded but the harness couldn't
113-
// route the click — the user should be able to retry.
114+
// Re-enable buttons when the reducer signals an authoritative delivery
115+
// failure (`no_active_turn`, `channel_closed`, `no_channel`). The transient
116+
// `channel_full` status does NOT increment this token — the retransmit
117+
// orchestrator stays subscribed and keeps resending automatically, so buttons
118+
// must remain disabled until the retry settles or the deadline expires.
114119
React.useEffect(() => {
115120
if (deliveryFailed) {
116121
setPending(null);

‎desktop/src/features/agents/ui/agentSessionTranscript.test.mjs‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2403,7 +2403,7 @@ test("buildTranscript_control_result_second_failure_increments_delivery_failed",
24032403
turnId: "turn-1",
24042404
payload: {
24052405
type: "permission_decision",
2406-
status: "channel_closed",
2406+
status: "no_channel",
24072407
requestNonce: nonce,
24082408
optionId: "allow_once",
24092409
},
@@ -2499,6 +2499,53 @@ test("buildTranscript_control_result_already_decided_does_not_mark_delivery_fail
24992499
);
25002500
});
25012501

2502+
test("buildTranscript_control_result_channel_full_does_not_mark_delivery_failed", () => {
2503+
// `channel_full` is a transient queue-saturation status — the retransmit
2504+
// orchestrator stays subscribed and keeps resending automatically. The card
2505+
// must remain DISABLED (deliveryFailed must NOT be incremented) so a second
2506+
// click cannot start a racing delivery loop while the first is still alive.
2507+
//
2508+
// Mutation proof: restoring `channel_full` to increment `deliveryFailed` in
2509+
// `handlePermissionDecisionResult` → `deliveryFailed` becomes 1 → this
2510+
// assertion fails at `expected undefined, got 1`.
2511+
const nonce = "nonce-channel-full";
2512+
const events = [
2513+
makePermissionRequestWithAuth(1, "req-cf", nonce),
2514+
{
2515+
seq: 2,
2516+
timestamp: "2026-07-01T10:00:01.000Z",
2517+
kind: "control_result",
2518+
agentIndex: 0,
2519+
channelId: "ch-1",
2520+
sessionId: "session-1",
2521+
turnId: "turn-1",
2522+
payload: {
2523+
type: "permission_decision",
2524+
status: "channel_full",
2525+
requestNonce: nonce,
2526+
optionId: "allow_once",
2527+
},
2528+
},
2529+
];
2530+
const transcript = buildTranscript(events);
2531+
2532+
const card = transcript.find(
2533+
(i) => i.renderClass === "permission" && i.requestNonce === nonce,
2534+
);
2535+
assert.ok(card, "permission card must exist");
2536+
assert.equal(
2537+
card.deliveryFailed,
2538+
undefined,
2539+
"deliveryFailed must not be set on channel_full — it is transient; buttons must stay disabled while the retransmit loop is active",
2540+
);
2541+
// Card must remain actionable (the request is still live).
2542+
assert.equal(
2543+
card.actionable,
2544+
true,
2545+
"card must remain actionable after channel_full — the retransmit loop is still in progress",
2546+
);
2547+
});
2548+
25022549
// ─── permission index cleanup + FOREIGN-nonce tests (Pass 4) ─────────────────
25032550

25042551
import { buildTranscriptState } from "./agentSessionTranscript.ts";

‎desktop/src/features/agents/ui/agentSessionTranscriptPermissions.ts‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,25 @@ export function handlePermissionWrite(
481481

482482
/**
483483
* Handle a `control_result` frame for a `permission_decision` delivery.
484-
* A non-success status means the click did not reach the harness — marks the
485-
* card with an incremented `deliveryFailed` counter so buttons re-enable for
486-
* retry.
487484
*
488-
* `sent` and `already_decided` are both success: `sent` means the harness
489-
* forwarded the decision to the live read loop; `already_decided` means a
490-
* retransmit matched a nonce the harness had previously forwarded (delivery
491-
* suppressed — the deciding task has since ended). Neither may fail the card —
492-
* an `already_decided` that incremented `deliveryFailed` would flip a
493-
* correctly-resolved card back to a clickable/failed state, the exact P1 the
494-
* retransmit loop exists to avoid.
485+
* Three statuses are treated as authoritative failures — the harness cannot
486+
* route the decision at all, so the card's `deliveryFailed` token is
487+
* incremented and the buttons re-enable for a manual retry:
488+
* `no_active_turn`, `channel_closed`, `no_channel`
489+
*
490+
* Two statuses are success and must not fail the card:
491+
* `sent` — the harness forwarded the decision to the live read loop.
492+
* `already_decided` — a retransmit matched a nonce the harness had already
493+
* applied (suppressed); incrementing `deliveryFailed` here would flip a
494+
* correctly-resolved card back to clickable, the exact P1 this exists to
495+
* prevent.
496+
*
497+
* `channel_full` is a transient queue-saturation condition (one or more
498+
* eligible queues could not accept the frame). The retransmit orchestrator
499+
* (`retransmitPermissionDecision.ts`) stays subscribed and keeps resending
500+
* until the harness accepts or the deadline expires. The card must remain
501+
* DISABLED while the automatic retry is in progress — do NOT increment
502+
* `deliveryFailed` here.
495503
*/
496504
export function handlePermissionDecisionResult(
497505
d: PermissionDraftSlice,
@@ -500,8 +508,12 @@ export function handlePermissionDecisionResult(
500508
const frameType = asString(payload.type);
501509
if (frameType !== "permission_decision") return;
502510
const deliveryStatus = asString(payload.status);
511+
// Success statuses — no card update needed.
503512
if (deliveryStatus === "sent" || deliveryStatus === "already_decided") return;
504-
// Delivery failed — find the card by nonce and mark it retryable.
513+
// Transient queue-saturation — retransmit orchestrator keeps retrying;
514+
// do not re-enable the buttons mid-retry.
515+
if (deliveryStatus === "channel_full") return;
516+
// Authoritative failure — find the card by nonce and mark it retryable.
505517
const nonce = asString(payload.requestNonce);
506518
if (!nonce) return;
507519
const itemId = d.pendingPermissionsByNonce.get(nonce);

0 commit comments

Comments
 (0)