|
| 1 | +/** |
| 2 | + * Last-hop pins for drained queue items: live parent-boundary steers |
| 3 | + * Agent.deliver (deliverSteer); leftover / fleet-hold / interrupt use send. |
| 4 | + */ |
| 5 | +import { describe, expect, test } from "bun:test"; |
| 6 | +import { attachSessionBridge, type SessionBridge } from "./runtime-bridge"; |
| 7 | +import { createLiveSessionPort } from "./live-session-port"; |
| 8 | +import { createAppShell } from "./shell"; |
| 9 | +import { withTestRenderer } from "./harness"; |
| 10 | +import { routeQueuedDelivery } from "./queued-delivery.js"; |
| 11 | +import { badgeCount } from "./session-queue"; |
| 12 | + |
| 13 | +function lastHopPort(bridgeRef: { current: SessionBridge | undefined }) { |
| 14 | + const sends: string[] = []; |
| 15 | + const steers: string[] = []; |
| 16 | + const port = createLiveSessionPort({ |
| 17 | + send: (text) => { |
| 18 | + sends.push(text); |
| 19 | + }, |
| 20 | + interrupt: () => {}, |
| 21 | + deliver: routeQueuedDelivery({ |
| 22 | + send: (text) => { |
| 23 | + sends.push(text); |
| 24 | + }, |
| 25 | + deliverSteer: (text) => { |
| 26 | + steers.push(text); |
| 27 | + }, |
| 28 | + parentCycleLive: () => bridgeRef.current?.parentCycleLive === true, |
| 29 | + }), |
| 30 | + }); |
| 31 | + return { port, sends, steers }; |
| 32 | +} |
| 33 | + |
| 34 | +describe("queued delivery last hop", () => { |
| 35 | + test("busy parent tool.boundary steer last-hops to deliverSteer, not send", async () => { |
| 36 | + await withTestRenderer( |
| 37 | + async (h) => { |
| 38 | + const shell = createAppShell(h.renderer, { |
| 39 | + terminal: { columns: 80, rows: 24 }, |
| 40 | + wireKeys: false, |
| 41 | + run: "busy", |
| 42 | + }); |
| 43 | + const bridgeRef: { current: SessionBridge | undefined } = { current: undefined }; |
| 44 | + const { port, sends, steers } = lastHopPort(bridgeRef); |
| 45 | + const bridge = attachSessionBridge(shell, port); |
| 46 | + bridgeRef.current = bridge; |
| 47 | + try { |
| 48 | + bridge.submit("asap", "steer"); |
| 49 | + expect(badgeCount(shell.session)).toBe(1); |
| 50 | + bridge.handle({ type: "tool.boundary" }); |
| 51 | + expect(steers).toEqual(["asap"]); |
| 52 | + expect(sends).toEqual([]); |
| 53 | + } finally { |
| 54 | + bridge.dispose(); |
| 55 | + shell.dispose(); |
| 56 | + } |
| 57 | + }, |
| 58 | + { width: 80, height: 24 }, |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test("text-only settle leftover steer last-hops to send", async () => { |
| 63 | + await withTestRenderer( |
| 64 | + async (h) => { |
| 65 | + const shell = createAppShell(h.renderer, { |
| 66 | + terminal: { columns: 80, rows: 24 }, |
| 67 | + wireKeys: false, |
| 68 | + run: "busy", |
| 69 | + }); |
| 70 | + const bridgeRef: { current: SessionBridge | undefined } = { current: undefined }; |
| 71 | + const { port, sends, steers } = lastHopPort(bridgeRef); |
| 72 | + const bridge = attachSessionBridge(shell, port); |
| 73 | + bridgeRef.current = bridge; |
| 74 | + try { |
| 75 | + bridge.submit("leftover", "steer"); |
| 76 | + bridge.handle({ type: "inference.start", data: {} }); |
| 77 | + bridge.handle({ type: "inference.text.delta", data: { token: "hi" } }); |
| 78 | + bridge.handle({ type: "inference.done", data: {} }); |
| 79 | + expect(sends).toEqual(["leftover"]); |
| 80 | + expect(steers).toEqual([]); |
| 81 | + } finally { |
| 82 | + bridge.dispose(); |
| 83 | + shell.dispose(); |
| 84 | + } |
| 85 | + }, |
| 86 | + { width: 80, height: 24 }, |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + test("interrupt leftover steer last-hops to send", async () => { |
| 91 | + await withTestRenderer( |
| 92 | + async (h) => { |
| 93 | + const shell = createAppShell(h.renderer, { |
| 94 | + terminal: { columns: 80, rows: 24 }, |
| 95 | + wireKeys: false, |
| 96 | + run: "busy", |
| 97 | + }); |
| 98 | + const bridgeRef: { current: SessionBridge | undefined } = { current: undefined }; |
| 99 | + const { port, sends, steers } = lastHopPort(bridgeRef); |
| 100 | + const bridge = attachSessionBridge(shell, port); |
| 101 | + bridgeRef.current = bridge; |
| 102 | + try { |
| 103 | + bridge.submit("after stop", "steer"); |
| 104 | + bridge.interrupt(); |
| 105 | + expect(sends).toEqual(["after stop"]); |
| 106 | + expect(steers).toEqual([]); |
| 107 | + } finally { |
| 108 | + bridge.dispose(); |
| 109 | + shell.dispose(); |
| 110 | + } |
| 111 | + }, |
| 112 | + { width: 80, height: 24 }, |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + test("idle-with-fleet leftover steer last-hops to send", async () => { |
| 117 | + await withTestRenderer( |
| 118 | + async (h) => { |
| 119 | + const shell = createAppShell(h.renderer, { |
| 120 | + terminal: { columns: 80, rows: 24 }, |
| 121 | + wireKeys: false, |
| 122 | + run: "idle", |
| 123 | + }); |
| 124 | + const bridgeRef: { current: SessionBridge | undefined } = { current: undefined }; |
| 125 | + const { port, sends, steers } = lastHopPort(bridgeRef); |
| 126 | + const bridge = attachSessionBridge(shell, port); |
| 127 | + bridgeRef.current = bridge; |
| 128 | + try { |
| 129 | + bridge.submit("dispatch", "immediate"); |
| 130 | + bridge.submit("one more worker", "steer"); |
| 131 | + bridge.handle({ type: "fleet", running: 1 }); |
| 132 | + bridge.handle({ type: "inference.start", data: {} }); |
| 133 | + bridge.handle({ type: "inference.done", data: {} }); |
| 134 | + expect(sends).toEqual(["dispatch", "one more worker"]); |
| 135 | + expect(steers).toEqual([]); |
| 136 | + } finally { |
| 137 | + bridge.dispose(); |
| 138 | + shell.dispose(); |
| 139 | + } |
| 140 | + }, |
| 141 | + { width: 80, height: 24 }, |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + test("/clear drops queued steers so a later boundary does not deliver or send", async () => { |
| 146 | + await withTestRenderer( |
| 147 | + async (h) => { |
| 148 | + const shell = createAppShell(h.renderer, { |
| 149 | + terminal: { columns: 80, rows: 24 }, |
| 150 | + wireKeys: false, |
| 151 | + run: "busy", |
| 152 | + }); |
| 153 | + const bridgeRef: { current: SessionBridge | undefined } = { current: undefined }; |
| 154 | + const { port, sends, steers } = lastHopPort(bridgeRef); |
| 155 | + const bridge = attachSessionBridge(shell, port); |
| 156 | + bridgeRef.current = bridge; |
| 157 | + try { |
| 158 | + bridge.submit("old steer", "steer"); |
| 159 | + expect(badgeCount(shell.session)).toBe(1); |
| 160 | + bridge.clearQueuedDelivery(); |
| 161 | + bridge.handle({ type: "tool.boundary" }); |
| 162 | + expect(sends).toEqual([]); |
| 163 | + expect(steers).toEqual([]); |
| 164 | + } finally { |
| 165 | + bridge.dispose(); |
| 166 | + shell.dispose(); |
| 167 | + } |
| 168 | + }, |
| 169 | + { width: 80, height: 24 }, |
| 170 | + ); |
| 171 | + }); |
| 172 | +}); |
0 commit comments