@blackbelt-technology/pi-dashboard-extension 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/__tests__/bridge-default-model-gate.test.ts +47 -0
- package/src/__tests__/bridge-followup-chat-order.test.ts +215 -0
- package/src/__tests__/bridge-followup-multi-entry.test.ts +202 -0
- package/src/__tests__/bridge-queue-update-forward.test.ts +77 -0
- package/src/__tests__/bridge-retry-ordering.test.ts +148 -0
- package/src/__tests__/bridge-shadow-queue-drain.test.ts +221 -0
- package/src/__tests__/bridge-shadow-queue-gate.test.ts +299 -0
- package/src/__tests__/bridge-shutdown-reset.test.ts +238 -0
- package/src/__tests__/bridge-slash-command-routing.test.ts +127 -31
- package/src/__tests__/command-handler.test.ts +105 -3
- package/src/__tests__/fixtures/usage-limit-error-strings.ts +127 -0
- package/src/__tests__/source-detector.test.ts +15 -0
- package/src/__tests__/usage-limit-orderer.test.ts +12 -0
- package/src/bridge-default-model-gate.ts +32 -0
- package/src/bridge.ts +299 -20
- package/src/command-handler.ts +53 -7
- package/src/dashboard-default-adapter.ts +5 -0
- package/src/prompt-bus.ts +15 -0
- package/src/slash-dispatch.ts +30 -15
- package/src/source-detector.ts +13 -5
- package/src/usage-limit-orderer.ts +18 -1
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge wire-ordering invariant for synthesized retry events.
|
|
3
|
+
*
|
|
4
|
+
* Verifies that the bridge updates `RetryTracker` + `UsageLimitOrderer` state
|
|
5
|
+
* SYNCHRONOUSLY when handling `message_end`, so that a back-to-back `agent_end`
|
|
6
|
+
* (fired in the same event-loop tick by pi-coding-agent) observes the
|
|
7
|
+
* up-to-date state.
|
|
8
|
+
*
|
|
9
|
+
* Pre-fix bug: the synthesizer state lived inside `setTimeout(0)` (intended
|
|
10
|
+
* for entryId capture per `fix-per-message-fork`), so `agent_end` was
|
|
11
|
+
* processed BEFORE the trackers had been updated, the orderer's pending
|
|
12
|
+
* flag was never set, and `auto_retry_start` shipped on the wire AFTER
|
|
13
|
+
* `agent_end` — leaving the dashboard's `retryState` stuck (yellow + red
|
|
14
|
+
* banners both visible).
|
|
15
|
+
*
|
|
16
|
+
* See change: fix-retry-banner-stuck-on-limit-exceeded.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { describe, it, expect } from "vitest";
|
|
20
|
+
import { RetryTracker } from "../retry-tracker.js";
|
|
21
|
+
import { UsageLimitOrderer } from "../usage-limit-orderer.js";
|
|
22
|
+
|
|
23
|
+
interface WireEvent {
|
|
24
|
+
eventType: string;
|
|
25
|
+
data?: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Simulates the bridge's synthesizer pipeline as it runs synchronously
|
|
30
|
+
* inside the message_end / agent_end handlers, capturing all wire sends
|
|
31
|
+
* in order.
|
|
32
|
+
*/
|
|
33
|
+
class BridgeSim {
|
|
34
|
+
readonly wire: WireEvent[] = [];
|
|
35
|
+
private tracker = new RetryTracker();
|
|
36
|
+
private orderer = new UsageLimitOrderer();
|
|
37
|
+
|
|
38
|
+
/** Mirrors bridge.ts message_end handler synthesizer block. */
|
|
39
|
+
onMessageEnd(sessionId: string, message: { role: string; stopReason?: string; errorMessage?: string }): void {
|
|
40
|
+
const synthetic = this.tracker.observeMessageEnd(sessionId, message);
|
|
41
|
+
if (synthetic) {
|
|
42
|
+
if (synthetic.eventType === "auto_retry_start") {
|
|
43
|
+
this.orderer.noteRetryStart(sessionId);
|
|
44
|
+
} else {
|
|
45
|
+
this.orderer.noteRetryEnd(sessionId);
|
|
46
|
+
}
|
|
47
|
+
this.wire.push({ eventType: synthetic.eventType, data: synthetic.data });
|
|
48
|
+
}
|
|
49
|
+
// The actual message_end body send is deferred via setTimeout(0) in the
|
|
50
|
+
// real bridge for entryId capture; for ordering tests we only care about
|
|
51
|
+
// the synthetic events relative to agent_end.
|
|
52
|
+
this.wire.push({ eventType: "message_end" });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Mirrors bridge.ts agent_end handler synthesizer block. */
|
|
56
|
+
onAgentEnd(sessionId: string, agentEnd: { messages?: Array<Record<string, unknown>> }): void {
|
|
57
|
+
const orderedSynth = this.orderer.maybeSynthesize(sessionId, agentEnd);
|
|
58
|
+
if (orderedSynth) {
|
|
59
|
+
this.wire.push({ eventType: orderedSynth.eventType, data: orderedSynth.data });
|
|
60
|
+
this.tracker.noteAbort(sessionId);
|
|
61
|
+
} else {
|
|
62
|
+
const trackerSynth = this.tracker.observeAgentEnd(sessionId, agentEnd);
|
|
63
|
+
if (trackerSynth) {
|
|
64
|
+
this.wire.push({ eventType: trackerSynth.eventType, data: trackerSynth.data });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.wire.push({ eventType: "agent_end" });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe("Bridge retry-event wire ordering", () => {
|
|
72
|
+
it("agent_end fired back-to-back after retryable message_end observes pending retry", () => {
|
|
73
|
+
const sim = new BridgeSim();
|
|
74
|
+
const sessionId = "s1";
|
|
75
|
+
const errorMsg = "429 too many requests";
|
|
76
|
+
|
|
77
|
+
// Pi fires both events synchronously back-to-back.
|
|
78
|
+
sim.onMessageEnd(sessionId, { role: "assistant", stopReason: "error", errorMessage: errorMsg });
|
|
79
|
+
sim.onAgentEnd(sessionId, {
|
|
80
|
+
messages: [{ role: "assistant", stopReason: "error", errorMessage: errorMsg }],
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const types = sim.wire.map((e) => e.eventType);
|
|
84
|
+
// auto_retry_start must precede message_end, which must precede the
|
|
85
|
+
// agent_end-side synthesis. Since the same retryable error is the
|
|
86
|
+
// terminal message, retryTracker.observeAgentEnd surfaces a final
|
|
87
|
+
// auto_retry_end{success:false, finalError:errorMsg} BEFORE agent_end.
|
|
88
|
+
expect(types).toEqual([
|
|
89
|
+
"auto_retry_start",
|
|
90
|
+
"message_end",
|
|
91
|
+
"auto_retry_end",
|
|
92
|
+
"agent_end",
|
|
93
|
+
]);
|
|
94
|
+
// auto_retry_start MUST come before agent_end on the wire.
|
|
95
|
+
const startIdx = types.indexOf("auto_retry_start");
|
|
96
|
+
const agentEndIdx = types.indexOf("agent_end");
|
|
97
|
+
expect(startIdx).toBeLessThan(agentEndIdx);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("Gemini monthly-spending-cap error orders auto_retry_end before agent_end", () => {
|
|
101
|
+
const sim = new BridgeSim();
|
|
102
|
+
const sessionId = "s2";
|
|
103
|
+
// Real fixture from ~/.pi/agent/sessions/...BME-szakdoga.../*.jsonl line 363
|
|
104
|
+
const errorMsg = JSON.stringify({
|
|
105
|
+
error: {
|
|
106
|
+
message:
|
|
107
|
+
"Your project has exceeded its monthly spending cap. Please go to AI Studio at https://ai.studio/spend to manage your project spend cap.",
|
|
108
|
+
status: "RESOURCE_EXHAUSTED",
|
|
109
|
+
},
|
|
110
|
+
code: 429,
|
|
111
|
+
status: "Too Many Requests",
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
sim.onMessageEnd(sessionId, { role: "assistant", stopReason: "error", errorMessage: errorMsg });
|
|
115
|
+
sim.onAgentEnd(sessionId, {
|
|
116
|
+
messages: [{ role: "assistant", stopReason: "error", errorMessage: errorMsg }],
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const types = sim.wire.map((e) => e.eventType);
|
|
120
|
+
expect(types).toEqual([
|
|
121
|
+
"auto_retry_start",
|
|
122
|
+
"message_end",
|
|
123
|
+
"auto_retry_end",
|
|
124
|
+
"agent_end",
|
|
125
|
+
]);
|
|
126
|
+
// The synthetic auto_retry_end MUST come from the usage-limit orderer
|
|
127
|
+
// (not the retry-tracker fallback) because the broadened
|
|
128
|
+
// USAGE_LIMIT_PATTERN matches "monthly spending cap" / RESOURCE_EXHAUSTED.
|
|
129
|
+
const retryEnd = sim.wire.find((e) => e.eventType === "auto_retry_end")!;
|
|
130
|
+
expect(retryEnd.data).toMatchObject({ success: false, finalError: errorMsg });
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("non-retryable message_end produces no synthesis (only message_end on wire)", () => {
|
|
134
|
+
const sim = new BridgeSim();
|
|
135
|
+
sim.onMessageEnd("s3", {
|
|
136
|
+
role: "assistant",
|
|
137
|
+
stopReason: "error",
|
|
138
|
+
errorMessage: "prompt is too long: 300000 tokens > 200000 maximum",
|
|
139
|
+
});
|
|
140
|
+
expect(sim.wire.map((e) => e.eventType)).toEqual(["message_end"]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("successful message_end with no prior retry produces no synthesis", () => {
|
|
144
|
+
const sim = new BridgeSim();
|
|
145
|
+
sim.onMessageEnd("s4", { role: "assistant", stopReason: "end_turn" });
|
|
146
|
+
expect(sim.wire.map((e) => e.eventType)).toEqual(["message_end"]);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the bridge's per-entry shadow-queue drain on user message_start.
|
|
3
|
+
*
|
|
4
|
+
* Pi mirrors this exact algorithm internally (see
|
|
5
|
+
* `@earendil-works/pi-coding-agent/dist/core/agent-session.js`
|
|
6
|
+
* `_processAgentEvent`, around line 270-292):
|
|
7
|
+
*
|
|
8
|
+
* if (event.type === "message_start" && event.message.role === "user") {
|
|
9
|
+
* const text = _getUserMessageText(event.message);
|
|
10
|
+
* const steeringIdx = _steeringMessages.indexOf(text);
|
|
11
|
+
* if (steeringIdx !== -1) {
|
|
12
|
+
* _steeringMessages.splice(steeringIdx, 1);
|
|
13
|
+
* _emitQueueUpdate();
|
|
14
|
+
* } else {
|
|
15
|
+
* const followUpIdx = _followUpMessages.indexOf(text);
|
|
16
|
+
* if (followUpIdx !== -1) {
|
|
17
|
+
* _followUpMessages.splice(followUpIdx, 1);
|
|
18
|
+
* _emitQueueUpdate();
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* Pre-fix bug: bridge bulk-cleared `bridgeFollowUp = []` at every `agent_end`
|
|
24
|
+
* and `bridgeSteering = []` at every `turn_end`. With pi's `mode:"all"`
|
|
25
|
+
* (default), pi drains all queued follow-ups across multiple turns before
|
|
26
|
+
* emitting the final `agent_end`. The dashboard saw the entire queue stay
|
|
27
|
+
* visible for the whole drain window, then disappear all at once at the
|
|
28
|
+
* end — instead of shrinking one entry per drain as the user observes them
|
|
29
|
+
* being processed.
|
|
30
|
+
*
|
|
31
|
+
* Fix: bridge mirrors pi's per-entry matcher on user `message_start`. Bulk
|
|
32
|
+
* clears at `agent_end` / `turn_end` are removed (would otherwise wipe
|
|
33
|
+
* entries the user added DURING a drain).
|
|
34
|
+
*
|
|
35
|
+
* See change: add-followup-edit-and-steer-cancel (per-entry-drain scenario).
|
|
36
|
+
*/
|
|
37
|
+
import { describe, it, expect } from "vitest";
|
|
38
|
+
|
|
39
|
+
interface ShadowQueue {
|
|
40
|
+
steering: string[];
|
|
41
|
+
followUp: string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Pure mirror of the per-entry drain matcher as it lives in bridge.ts'
|
|
46
|
+
* `message_start` handler. If production drifts from this shape, the
|
|
47
|
+
* test should drift in lockstep.
|
|
48
|
+
*/
|
|
49
|
+
function makeShadowDrainMatcher() {
|
|
50
|
+
const queue: ShadowQueue = { steering: [], followUp: [] };
|
|
51
|
+
const emits: ShadowQueue[] = [];
|
|
52
|
+
function emit() {
|
|
53
|
+
emits.push({ steering: [...queue.steering], followUp: [...queue.followUp] });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Mirrors pi's `_getUserMessageText` exactly. */
|
|
57
|
+
function getUserMessageText(message: { role: string; content: unknown }): string {
|
|
58
|
+
if (message.role !== "user") return "";
|
|
59
|
+
const content = message.content;
|
|
60
|
+
if (typeof content === "string") return content;
|
|
61
|
+
if (!Array.isArray(content)) return "";
|
|
62
|
+
return content
|
|
63
|
+
.filter((c: any) => c && c.type === "text")
|
|
64
|
+
.map((c: any) => c.text ?? "")
|
|
65
|
+
.join("");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Call when pi emits a user `message_start`. Mirrors pi's internal logic. */
|
|
69
|
+
function onUserMessageStart(message: { role: string; content: unknown }): void {
|
|
70
|
+
const text = getUserMessageText(message);
|
|
71
|
+
if (!text) return;
|
|
72
|
+
const steeringIdx = queue.steering.indexOf(text);
|
|
73
|
+
if (steeringIdx !== -1) {
|
|
74
|
+
queue.steering.splice(steeringIdx, 1);
|
|
75
|
+
emit();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const followUpIdx = queue.followUp.indexOf(text);
|
|
79
|
+
if (followUpIdx !== -1) {
|
|
80
|
+
queue.followUp.splice(followUpIdx, 1);
|
|
81
|
+
emit();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
queue,
|
|
87
|
+
emits,
|
|
88
|
+
onUserMessageStart,
|
|
89
|
+
recordSteer: (t: string) => { queue.steering.push(t); emit(); },
|
|
90
|
+
recordFollowup: (t: string) => { queue.followUp.push(t); emit(); },
|
|
91
|
+
snapshotQueue: () => ({ steering: [...queue.steering], followUp: [...queue.followUp] }),
|
|
92
|
+
snapshotEmits: () => emits.map((e) => ({ steering: [...e.steering], followUp: [...e.followUp] })),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
describe("Bridge shadow-queue per-entry drain on user message_start", () => {
|
|
97
|
+
it("removes the matching follow-up entry when pi drains it, leaves the rest", () => {
|
|
98
|
+
const m = makeShadowDrainMatcher();
|
|
99
|
+
m.recordFollowup("a");
|
|
100
|
+
m.recordFollowup("b");
|
|
101
|
+
m.recordFollowup("c");
|
|
102
|
+
expect(m.snapshotQueue().followUp).toEqual(["a", "b", "c"]);
|
|
103
|
+
|
|
104
|
+
// Pi drains "a" first.
|
|
105
|
+
m.onUserMessageStart({ role: "user", content: "a" });
|
|
106
|
+
expect(m.snapshotQueue().followUp).toEqual(["b", "c"]);
|
|
107
|
+
|
|
108
|
+
// Then "b".
|
|
109
|
+
m.onUserMessageStart({ role: "user", content: "b" });
|
|
110
|
+
expect(m.snapshotQueue().followUp).toEqual(["c"]);
|
|
111
|
+
|
|
112
|
+
// Then "c".
|
|
113
|
+
m.onUserMessageStart({ role: "user", content: "c" });
|
|
114
|
+
expect(m.snapshotQueue().followUp).toEqual([]);
|
|
115
|
+
|
|
116
|
+
// One emit per drain (plus the three initial record emits).
|
|
117
|
+
expect(m.emits).toHaveLength(6);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("removes the matching steering entry when pi drains it", () => {
|
|
121
|
+
const m = makeShadowDrainMatcher();
|
|
122
|
+
m.recordSteer("focus on X");
|
|
123
|
+
m.recordSteer("ignore Y");
|
|
124
|
+
expect(m.snapshotQueue().steering).toEqual(["focus on X", "ignore Y"]);
|
|
125
|
+
|
|
126
|
+
m.onUserMessageStart({ role: "user", content: "focus on X" });
|
|
127
|
+
expect(m.snapshotQueue().steering).toEqual(["ignore Y"]);
|
|
128
|
+
expect(m.snapshotQueue().followUp).toEqual([]);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("steering queue checked BEFORE follow-up when same text is in both", () => {
|
|
132
|
+
const m = makeShadowDrainMatcher();
|
|
133
|
+
m.recordSteer("hello");
|
|
134
|
+
m.recordFollowup("hello");
|
|
135
|
+
|
|
136
|
+
m.onUserMessageStart({ role: "user", content: "hello" });
|
|
137
|
+
// Steering entry consumed, follow-up untouched.
|
|
138
|
+
expect(m.snapshotQueue().steering).toEqual([]);
|
|
139
|
+
expect(m.snapshotQueue().followUp).toEqual(["hello"]);
|
|
140
|
+
|
|
141
|
+
// Second drain consumes the follow-up.
|
|
142
|
+
m.onUserMessageStart({ role: "user", content: "hello" });
|
|
143
|
+
expect(m.snapshotQueue().followUp).toEqual([]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("non-matching user message_start is a no-op (no queue mutation, no emit)", () => {
|
|
147
|
+
const m = makeShadowDrainMatcher();
|
|
148
|
+
m.recordFollowup("queued");
|
|
149
|
+
const baselineEmits = m.emits.length;
|
|
150
|
+
|
|
151
|
+
// Fresh user send not in any queue (e.g., the user typed something
|
|
152
|
+
// new on an idle session, or a steer was added by a non-dashboard
|
|
153
|
+
// consumer).
|
|
154
|
+
m.onUserMessageStart({ role: "user", content: "fresh send" });
|
|
155
|
+
expect(m.snapshotQueue().followUp).toEqual(["queued"]);
|
|
156
|
+
expect(m.emits.length).toBe(baselineEmits);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("user message_start with array content joins text blocks (matches pi's _getUserMessageText)", () => {
|
|
160
|
+
const m = makeShadowDrainMatcher();
|
|
161
|
+
m.recordFollowup("describe this");
|
|
162
|
+
|
|
163
|
+
m.onUserMessageStart({
|
|
164
|
+
role: "user",
|
|
165
|
+
content: [
|
|
166
|
+
{ type: "text", text: "describe " },
|
|
167
|
+
{ type: "image", data: "<base64>", mimeType: "image/png" },
|
|
168
|
+
{ type: "text", text: "this" },
|
|
169
|
+
],
|
|
170
|
+
});
|
|
171
|
+
expect(m.snapshotQueue().followUp).toEqual([]);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("ignores non-user message_start (assistant role does not touch the queue)", () => {
|
|
175
|
+
const m = makeShadowDrainMatcher();
|
|
176
|
+
m.recordFollowup("a");
|
|
177
|
+
const baselineEmits = m.emits.length;
|
|
178
|
+
|
|
179
|
+
m.onUserMessageStart({ role: "assistant", content: "a" });
|
|
180
|
+
expect(m.snapshotQueue().followUp).toEqual(["a"]);
|
|
181
|
+
expect(m.emits.length).toBe(baselineEmits);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("removes only the FIRST occurrence on duplicate text (FIFO)", () => {
|
|
185
|
+
const m = makeShadowDrainMatcher();
|
|
186
|
+
m.recordFollowup("dup");
|
|
187
|
+
m.recordFollowup("other");
|
|
188
|
+
m.recordFollowup("dup");
|
|
189
|
+
expect(m.snapshotQueue().followUp).toEqual(["dup", "other", "dup"]);
|
|
190
|
+
|
|
191
|
+
m.onUserMessageStart({ role: "user", content: "dup" });
|
|
192
|
+
// First "dup" removed; second one still queued (FIFO).
|
|
193
|
+
expect(m.snapshotQueue().followUp).toEqual(["other", "dup"]);
|
|
194
|
+
|
|
195
|
+
m.onUserMessageStart({ role: "user", content: "dup" });
|
|
196
|
+
expect(m.snapshotQueue().followUp).toEqual(["other"]);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("entries added DURING drain are preserved (no bulk wipe)", () => {
|
|
200
|
+
const m = makeShadowDrainMatcher();
|
|
201
|
+
m.recordFollowup("a");
|
|
202
|
+
m.recordFollowup("b");
|
|
203
|
+
|
|
204
|
+
// Pi drains "a"
|
|
205
|
+
m.onUserMessageStart({ role: "user", content: "a" });
|
|
206
|
+
expect(m.snapshotQueue().followUp).toEqual(["b"]);
|
|
207
|
+
|
|
208
|
+
// User adds a new entry "c" while pi is still draining
|
|
209
|
+
m.recordFollowup("c");
|
|
210
|
+
expect(m.snapshotQueue().followUp).toEqual(["b", "c"]);
|
|
211
|
+
|
|
212
|
+
// Pi drains "b"
|
|
213
|
+
m.onUserMessageStart({ role: "user", content: "b" });
|
|
214
|
+
// "c" must still be present
|
|
215
|
+
expect(m.snapshotQueue().followUp).toEqual(["c"]);
|
|
216
|
+
|
|
217
|
+
// Eventually pi drains "c"
|
|
218
|
+
m.onUserMessageStart({ role: "user", content: "c" });
|
|
219
|
+
expect(m.snapshotQueue().followUp).toEqual([]);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the bridge's shadow-queue streaming gate.
|
|
3
|
+
*
|
|
4
|
+
* Repro for the bug "STEERING (1) appears on the very first message to an
|
|
5
|
+
* idle session". The fix has TWO layers:
|
|
6
|
+
*
|
|
7
|
+
* 1. Capture-before-send (primary gate): at the call site of
|
|
8
|
+
* `pi.sendUserMessage`, the caller MUST snapshot
|
|
9
|
+
* `isAgentStreaming` BEFORE invoking sendUserMessage. Pi flips
|
|
10
|
+
* idle→streaming synchronously inside sendUserMessage by emitting
|
|
11
|
+
* `agent_start`, whose handler in bridge.ts flips the flag in its
|
|
12
|
+
* first sync line. Checking the flag AFTER the send always reads
|
|
13
|
+
* true — the original bug. Tests `record*Sent + wasStreaming`
|
|
14
|
+
* encode that contract.
|
|
15
|
+
*
|
|
16
|
+
* 2. Internal gate (defense in depth): `recordSteerSent` /
|
|
17
|
+
* `recordFollowupSent` themselves re-check `isStreaming()` so a
|
|
18
|
+
* caller that forgets to capture pre-send still doesn't corrupt
|
|
19
|
+
* the shadow queue.
|
|
20
|
+
*
|
|
21
|
+
* See change: add-followup-edit-and-steer-cancel.
|
|
22
|
+
*/
|
|
23
|
+
import { describe, it, expect, vi } from "vitest";
|
|
24
|
+
|
|
25
|
+
interface ShadowQueue {
|
|
26
|
+
steering: string[];
|
|
27
|
+
followUp: string[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Pure version of the gate as it lives in bridge.ts. Mirrors the closure
|
|
32
|
+
* logic 1:1; if the production code drifts from this shape, this test
|
|
33
|
+
* should drift in lockstep.
|
|
34
|
+
*/
|
|
35
|
+
function makeShadowRecorder(opts: {
|
|
36
|
+
isStreaming: () => boolean;
|
|
37
|
+
onEmit: (snapshot: ShadowQueue) => void;
|
|
38
|
+
}) {
|
|
39
|
+
const queue: ShadowQueue = { steering: [], followUp: [] };
|
|
40
|
+
function emit() { opts.onEmit({ steering: [...queue.steering], followUp: [...queue.followUp] }); }
|
|
41
|
+
function recordSteer(text: string) {
|
|
42
|
+
if (!opts.isStreaming()) return;
|
|
43
|
+
queue.steering.push(text);
|
|
44
|
+
emit();
|
|
45
|
+
}
|
|
46
|
+
function recordFollowup(text: string) {
|
|
47
|
+
if (!opts.isStreaming()) return;
|
|
48
|
+
queue.followUp = [text];
|
|
49
|
+
emit();
|
|
50
|
+
}
|
|
51
|
+
function clearSteer() { queue.steering = []; emit(); }
|
|
52
|
+
function clearFollowup() { queue.followUp = []; emit(); }
|
|
53
|
+
function drainSteerOnTurnEnd() { if (queue.steering.length > 0) { queue.steering = []; emit(); } }
|
|
54
|
+
function drainFollowupOnAgentEnd() { if (queue.followUp.length > 0) { queue.followUp = []; emit(); } }
|
|
55
|
+
return { recordSteer, recordFollowup, clearSteer, clearFollowup, drainSteerOnTurnEnd, drainFollowupOnAgentEnd, snapshot: () => ({ ...queue, steering: [...queue.steering], followUp: [...queue.followUp] }) };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe("bridge shadow queue: streaming gate", () => {
|
|
59
|
+
it("recordSteer is a no-op when isStreaming === false (idle first message)", () => {
|
|
60
|
+
let streaming = false;
|
|
61
|
+
const onEmit = vi.fn();
|
|
62
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
63
|
+
|
|
64
|
+
r.recordSteer("hello");
|
|
65
|
+
|
|
66
|
+
expect(r.snapshot().steering).toEqual([]);
|
|
67
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("recordFollowup is a no-op when isStreaming === false (idle first message)", () => {
|
|
71
|
+
let streaming = false;
|
|
72
|
+
const onEmit = vi.fn();
|
|
73
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
74
|
+
|
|
75
|
+
r.recordFollowup("after done");
|
|
76
|
+
|
|
77
|
+
expect(r.snapshot().followUp).toEqual([]);
|
|
78
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("recordSteer appends + emits when streaming", () => {
|
|
82
|
+
let streaming = true;
|
|
83
|
+
const onEmit = vi.fn();
|
|
84
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
85
|
+
|
|
86
|
+
r.recordSteer("A");
|
|
87
|
+
r.recordSteer("B");
|
|
88
|
+
|
|
89
|
+
expect(r.snapshot().steering).toEqual(["A", "B"]);
|
|
90
|
+
expect(onEmit).toHaveBeenCalledTimes(2);
|
|
91
|
+
expect(onEmit.mock.calls[1][0]).toEqual({ steering: ["A", "B"], followUp: [] });
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("recordFollowup replaces slot + emits when streaming (capacity 1)", () => {
|
|
95
|
+
let streaming = true;
|
|
96
|
+
const onEmit = vi.fn();
|
|
97
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
98
|
+
|
|
99
|
+
r.recordFollowup("first");
|
|
100
|
+
r.recordFollowup("second");
|
|
101
|
+
|
|
102
|
+
expect(r.snapshot().followUp).toEqual(["second"]);
|
|
103
|
+
expect(onEmit).toHaveBeenCalledTimes(2);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("bridge shadow queue: drain boundaries", () => {
|
|
108
|
+
it("turn_end drains steering only (followUp untouched)", () => {
|
|
109
|
+
let streaming = true;
|
|
110
|
+
const onEmit = vi.fn();
|
|
111
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
112
|
+
r.recordSteer("s1");
|
|
113
|
+
r.recordFollowup("f1");
|
|
114
|
+
onEmit.mockClear();
|
|
115
|
+
|
|
116
|
+
r.drainSteerOnTurnEnd();
|
|
117
|
+
|
|
118
|
+
expect(r.snapshot()).toEqual({ steering: [], followUp: ["f1"] });
|
|
119
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("agent_end drains followUp only (steering untouched)", () => {
|
|
123
|
+
let streaming = true;
|
|
124
|
+
const onEmit = vi.fn();
|
|
125
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
126
|
+
r.recordSteer("s1");
|
|
127
|
+
r.recordFollowup("f1");
|
|
128
|
+
onEmit.mockClear();
|
|
129
|
+
|
|
130
|
+
r.drainFollowupOnAgentEnd();
|
|
131
|
+
|
|
132
|
+
expect(r.snapshot()).toEqual({ steering: ["s1"], followUp: [] });
|
|
133
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("turn_end on empty steering does NOT emit (idempotent / no spurious broadcasts)", () => {
|
|
137
|
+
const onEmit = vi.fn();
|
|
138
|
+
const r = makeShadowRecorder({ isStreaming: () => true, onEmit });
|
|
139
|
+
|
|
140
|
+
r.drainSteerOnTurnEnd();
|
|
141
|
+
|
|
142
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("agent_end on empty followUp does NOT emit", () => {
|
|
146
|
+
const onEmit = vi.fn();
|
|
147
|
+
const r = makeShadowRecorder({ isStreaming: () => true, onEmit });
|
|
148
|
+
|
|
149
|
+
r.drainFollowupOnAgentEnd();
|
|
150
|
+
|
|
151
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe("bridge shadow queue: clears", () => {
|
|
156
|
+
it("clearSteer wipes + emits regardless of streaming state", () => {
|
|
157
|
+
let streaming = false;
|
|
158
|
+
const onEmit = vi.fn();
|
|
159
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
160
|
+
// Populate via a streaming-on phase, then go idle and clear.
|
|
161
|
+
streaming = true; r.recordSteer("x"); streaming = false; onEmit.mockClear();
|
|
162
|
+
|
|
163
|
+
r.clearSteer();
|
|
164
|
+
|
|
165
|
+
expect(r.snapshot().steering).toEqual([]);
|
|
166
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("clearFollowup wipes + emits regardless of streaming state", () => {
|
|
170
|
+
let streaming = false;
|
|
171
|
+
const onEmit = vi.fn();
|
|
172
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
173
|
+
streaming = true; r.recordFollowup("y"); streaming = false; onEmit.mockClear();
|
|
174
|
+
|
|
175
|
+
r.clearFollowup();
|
|
176
|
+
|
|
177
|
+
expect(r.snapshot().followUp).toEqual([]);
|
|
178
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("bridge shadow queue: capture-before-send semantics (PRIMARY gate)", () => {
|
|
183
|
+
// Simulates the command-handler / sessionPrompt call sites where the bug
|
|
184
|
+
// originally lived. The fix: capture streaming state into a local var
|
|
185
|
+
// BEFORE calling pi.sendUserMessage. The internal recordX gate is a
|
|
186
|
+
// safety net only.
|
|
187
|
+
|
|
188
|
+
/** Stand-in for the simplified call-site logic in command-handler.ts. */
|
|
189
|
+
function send(opts: {
|
|
190
|
+
text: string;
|
|
191
|
+
delivery: "steer" | "followUp";
|
|
192
|
+
isStreaming: () => boolean;
|
|
193
|
+
piSendUserMessage: () => void; // simulates the synchronous agent_start flip
|
|
194
|
+
onSteer: (text: string) => void;
|
|
195
|
+
onFollowup: (text: string) => void;
|
|
196
|
+
}) {
|
|
197
|
+
const wasStreaming = opts.isStreaming();
|
|
198
|
+
opts.piSendUserMessage();
|
|
199
|
+
if (wasStreaming) {
|
|
200
|
+
if (opts.delivery === "steer") opts.onSteer(opts.text);
|
|
201
|
+
else opts.onFollowup(opts.text);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
it("idle send DOES NOT record even when pi flips isStreaming synchronously inside sendUserMessage", () => {
|
|
206
|
+
let streaming = false;
|
|
207
|
+
const onEmit = vi.fn();
|
|
208
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
209
|
+
|
|
210
|
+
send({
|
|
211
|
+
text: "first message",
|
|
212
|
+
delivery: "steer",
|
|
213
|
+
isStreaming: () => streaming,
|
|
214
|
+
// Pi receives the message, fires agent_start synchronously, which
|
|
215
|
+
// flips `streaming` to true. This was the original bug.
|
|
216
|
+
piSendUserMessage: () => { streaming = true; },
|
|
217
|
+
onSteer: r.recordSteer,
|
|
218
|
+
onFollowup: r.recordFollowup,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
expect(r.snapshot()).toEqual({ steering: [], followUp: [] });
|
|
222
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("streaming send DOES record (chip appears for mid-turn steer)", () => {
|
|
226
|
+
let streaming = true;
|
|
227
|
+
const onEmit = vi.fn();
|
|
228
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
229
|
+
|
|
230
|
+
send({
|
|
231
|
+
text: "redirect",
|
|
232
|
+
delivery: "steer",
|
|
233
|
+
isStreaming: () => streaming,
|
|
234
|
+
piSendUserMessage: () => { /* still streaming */ },
|
|
235
|
+
onSteer: r.recordSteer,
|
|
236
|
+
onFollowup: r.recordFollowup,
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
expect(r.snapshot().steering).toEqual(["redirect"]);
|
|
240
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("idle followUp also DOES NOT record (same race shape)", () => {
|
|
244
|
+
let streaming = false;
|
|
245
|
+
const onEmit = vi.fn();
|
|
246
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
247
|
+
|
|
248
|
+
send({
|
|
249
|
+
text: "after done",
|
|
250
|
+
delivery: "followUp",
|
|
251
|
+
isStreaming: () => streaming,
|
|
252
|
+
piSendUserMessage: () => { streaming = true; },
|
|
253
|
+
onSteer: r.recordSteer,
|
|
254
|
+
onFollowup: r.recordFollowup,
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
expect(r.snapshot()).toEqual({ steering: [], followUp: [] });
|
|
258
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
describe("bridge shadow queue: realistic follow-up scenario", () => {
|
|
263
|
+
it("send followUp while idle (first message of session) shows no chip", () => {
|
|
264
|
+
let streaming = false;
|
|
265
|
+
const onEmit = vi.fn();
|
|
266
|
+
const r = makeShadowRecorder({ isStreaming: () => streaming, onEmit });
|
|
267
|
+
|
|
268
|
+
// User sends their initial prompt; pi starts a new turn directly,
|
|
269
|
+
// it doesn't queue. The bridge must NOT record a chip.
|
|
270
|
+
r.recordFollowup("kick off the task");
|
|
271
|
+
|
|
272
|
+
expect(r.snapshot().followUp).toEqual([]);
|
|
273
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
274
|
+
|
|
275
|
+
// Pi fires agent_start → streaming flips on.
|
|
276
|
+
streaming = true;
|
|
277
|
+
|
|
278
|
+
// User adds a follow-up mid-stream → chip appears.
|
|
279
|
+
r.recordFollowup("when you finish, run the tests");
|
|
280
|
+
expect(r.snapshot().followUp).toEqual(["when you finish, run the tests"]);
|
|
281
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
282
|
+
|
|
283
|
+
// Agent finishes → followUp drains.
|
|
284
|
+
r.drainFollowupOnAgentEnd();
|
|
285
|
+
expect(r.snapshot().followUp).toEqual([]);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("rapid edit (steering still active) replaces slot atomically", () => {
|
|
289
|
+
const onEmit = vi.fn();
|
|
290
|
+
const r = makeShadowRecorder({ isStreaming: () => true, onEmit });
|
|
291
|
+
|
|
292
|
+
r.recordFollowup("v1");
|
|
293
|
+
r.recordFollowup("v2");
|
|
294
|
+
r.recordFollowup("v3");
|
|
295
|
+
|
|
296
|
+
expect(r.snapshot().followUp).toEqual(["v3"]);
|
|
297
|
+
expect(onEmit).toHaveBeenCalledTimes(3);
|
|
298
|
+
});
|
|
299
|
+
});
|