@blackbelt-technology/pi-dashboard-extension 0.3.0 → 0.4.1
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 +14 -4
- package/src/__tests__/ask-user-tool.test.ts +53 -8
- package/src/__tests__/bridge-entry-id-pi-070.test.ts +174 -0
- package/src/__tests__/event-forwarder.test.ts +30 -0
- package/src/__tests__/fork-entryid-timing.test.ts +64 -76
- package/src/__tests__/multiselect-list.test.ts +137 -0
- package/src/__tests__/no-session-replacement-calls.test.ts +99 -0
- package/src/__tests__/openspec-activity-detector.test.ts +37 -0
- package/src/__tests__/ui-decorators.test.ts +309 -0
- package/src/__tests__/ui-modules.test.ts +293 -0
- package/src/ask-user-tool.ts +105 -57
- package/src/bridge.ts +133 -15
- package/src/multiselect-list.ts +146 -0
- package/src/multiselect-polyfill.ts +43 -0
- package/src/server-launcher.ts +15 -3
- package/src/ui-modules.ts +272 -0
package/package.json
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blackbelt-technology/pi-dashboard-extension",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Pi bridge extension for pi-dashboard",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/BlackBeltTechnology/pi-agent-dashboard",
|
|
9
|
+
"directory": "packages/extension"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
6
14
|
"pi": {
|
|
7
15
|
"extensions": [
|
|
8
16
|
"src/bridge.ts"
|
|
@@ -16,12 +24,13 @@
|
|
|
16
24
|
".pi/skills/pi-dashboard/"
|
|
17
25
|
],
|
|
18
26
|
"dependencies": {
|
|
19
|
-
"@blackbelt-technology/pi-dashboard-shared": "
|
|
27
|
+
"@blackbelt-technology/pi-dashboard-shared": "^0.4.1",
|
|
20
28
|
"ws": "^8.18.0"
|
|
21
29
|
},
|
|
22
30
|
"peerDependencies": {
|
|
23
31
|
"@mariozechner/pi-coding-agent": "*",
|
|
24
|
-
"@mariozechner/pi-tui": "*"
|
|
32
|
+
"@mariozechner/pi-tui": "*",
|
|
33
|
+
"typebox": "*"
|
|
25
34
|
},
|
|
26
35
|
"peerDependenciesMeta": {
|
|
27
36
|
"@mariozechner/pi-coding-agent": {
|
|
@@ -33,6 +42,7 @@
|
|
|
33
42
|
},
|
|
34
43
|
"devDependencies": {
|
|
35
44
|
"@mariozechner/pi-tui": "*",
|
|
36
|
-
"@types/ws": "^8.18.1"
|
|
45
|
+
"@types/ws": "^8.18.1",
|
|
46
|
+
"typebox": "^1.1.33"
|
|
37
47
|
}
|
|
38
48
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, it, expect, vi } from "vitest";
|
|
2
2
|
|
|
3
3
|
// Mock modules before importing
|
|
4
|
-
vi.mock("
|
|
4
|
+
vi.mock("typebox", () => ({
|
|
5
5
|
Type: {
|
|
6
6
|
Object: vi.fn(() => ({})),
|
|
7
7
|
String: vi.fn(() => ({})),
|
|
@@ -44,20 +44,37 @@ describe("registerAskUserTool", () => {
|
|
|
44
44
|
expect(tool.promptGuidelines.length).toBeGreaterThan(0);
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
+
it("description instructs agents not to add a Select all option", () => {
|
|
48
|
+
const pi = createMockPi();
|
|
49
|
+
registerAskUserTool(pi as any);
|
|
50
|
+
const tool = pi.registerTool.mock.calls[0][0];
|
|
51
|
+
expect(tool.description).toMatch(/UI provides a Select all/i);
|
|
52
|
+
});
|
|
53
|
+
|
|
47
54
|
describe("message passthrough", () => {
|
|
48
55
|
function getToolAndMockCtx() {
|
|
49
56
|
const pi = createMockPi();
|
|
50
57
|
registerAskUserTool(pi as any);
|
|
51
58
|
const tool = pi.registerTool.mock.calls[0][0];
|
|
59
|
+
// `custom` stands in for the multiselect polyfill: it invokes the factory
|
|
60
|
+
// with a `done` callback; the factory-returned component exposes
|
|
61
|
+
// onConfirm/onCancel. We auto-confirm with ["A"] to preserve the legacy
|
|
62
|
+
// mock return value that the multiselect assertions expected.
|
|
63
|
+
const custom = vi.fn().mockImplementation(async (factory: any) => {
|
|
64
|
+
return await new Promise<unknown>((resolve) => {
|
|
65
|
+
const component: any = factory({}, {}, {}, (r: unknown) => resolve(r));
|
|
66
|
+
component?.onConfirm?.(["A"]);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
52
69
|
const ctx = {
|
|
53
70
|
ui: {
|
|
54
71
|
confirm: vi.fn().mockResolvedValue(true),
|
|
55
72
|
select: vi.fn().mockResolvedValue("A"),
|
|
56
73
|
input: vi.fn().mockResolvedValue("hello"),
|
|
57
|
-
|
|
74
|
+
custom,
|
|
58
75
|
},
|
|
59
76
|
};
|
|
60
|
-
return { tool, ctx };
|
|
77
|
+
return { tool, ctx, custom };
|
|
61
78
|
}
|
|
62
79
|
|
|
63
80
|
it("passes message through opts for input", async () => {
|
|
@@ -72,10 +89,19 @@ describe("registerAskUserTool", () => {
|
|
|
72
89
|
expect(ctx.ui.select).toHaveBeenCalledWith("Pick", ["A", "B"], { message: "Context" });
|
|
73
90
|
});
|
|
74
91
|
|
|
75
|
-
it("
|
|
92
|
+
it("dispatches multiselect through the polyfill via ctx.ui.custom", async () => {
|
|
76
93
|
const { tool, ctx } = getToolAndMockCtx();
|
|
77
|
-
await tool.execute(
|
|
78
|
-
|
|
94
|
+
const result = await tool.execute(
|
|
95
|
+
"id",
|
|
96
|
+
{ method: "multiselect", title: "Multi", message: "Info", options: ["A"] },
|
|
97
|
+
undefined,
|
|
98
|
+
undefined,
|
|
99
|
+
ctx,
|
|
100
|
+
);
|
|
101
|
+
// Polyfill routes via custom(factory); multiselect is not called directly.
|
|
102
|
+
expect(ctx.ui.custom).toHaveBeenCalledTimes(1);
|
|
103
|
+
expect(result.details.method).toBe("multiselect");
|
|
104
|
+
expect(result.details.result).toEqual(["A"]);
|
|
79
105
|
});
|
|
80
106
|
|
|
81
107
|
it("does not pass opts when message is undefined", async () => {
|
|
@@ -123,7 +149,7 @@ describe("registerAskUserTool", () => {
|
|
|
123
149
|
await expect(
|
|
124
150
|
tool.execute("id", { method: "multiselect", title: "Pick" }, undefined, undefined, ctx),
|
|
125
151
|
).rejects.toThrow(/options/i);
|
|
126
|
-
expect(ctx.ui.
|
|
152
|
+
expect(ctx.ui.custom).not.toHaveBeenCalled();
|
|
127
153
|
});
|
|
128
154
|
});
|
|
129
155
|
|
|
@@ -134,6 +160,19 @@ describe("registerAskUserTool", () => {
|
|
|
134
160
|
return pi.registerTool.mock.calls[0][0];
|
|
135
161
|
}
|
|
136
162
|
|
|
163
|
+
it("leaves empty {} args untouched (no synthetic method) so schema rejection still fires", () => {
|
|
164
|
+
// Regression test for the Opus-emits-empty-args bug seen in session 019dd05c.
|
|
165
|
+
// Our rescue layer must NOT silently fabricate a method/title when there is
|
|
166
|
+
// nothing to rescue — the framework's schema validator must still reject {}
|
|
167
|
+
// so the model is forced to retry with valid args.
|
|
168
|
+
const tool = getTool();
|
|
169
|
+
const result = tool.prepareArguments({});
|
|
170
|
+
expect(result.method).toBeUndefined();
|
|
171
|
+
expect(result.title).toBeUndefined();
|
|
172
|
+
expect(result.questions).toBeUndefined();
|
|
173
|
+
expect(Object.keys(result).filter((k) => k !== "__normalizations")).toHaveLength(0);
|
|
174
|
+
});
|
|
175
|
+
|
|
137
176
|
it("parses stringified options array", () => {
|
|
138
177
|
const tool = getTool();
|
|
139
178
|
const result = tool.prepareArguments({ method: "select", title: "Pick", options: '["A", "B"]' });
|
|
@@ -311,12 +350,18 @@ describe("registerAskUserTool", () => {
|
|
|
311
350
|
const pi = createMockPi();
|
|
312
351
|
registerAskUserTool(pi as any);
|
|
313
352
|
const tool = pi.registerTool.mock.calls[0][0];
|
|
353
|
+
const custom = vi.fn().mockImplementation(async (factory: any) => {
|
|
354
|
+
return await new Promise<unknown>((resolve) => {
|
|
355
|
+
const component: any = factory({}, {}, {}, (r: unknown) => resolve(r));
|
|
356
|
+
component?.onConfirm?.(["A"]);
|
|
357
|
+
});
|
|
358
|
+
});
|
|
314
359
|
const ctx = {
|
|
315
360
|
ui: {
|
|
316
361
|
confirm: vi.fn().mockResolvedValue(true),
|
|
317
362
|
select: vi.fn().mockResolvedValue("A"),
|
|
318
363
|
input: vi.fn().mockResolvedValue("hello"),
|
|
319
|
-
|
|
364
|
+
custom,
|
|
320
365
|
},
|
|
321
366
|
};
|
|
322
367
|
return { tool, ctx };
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the bridge entryId stamping under pi 0.70.x's emit-then-await-then-append
|
|
3
|
+
* ordering. Pi 0.70.x's _processAgentEvent does (paraphrased):
|
|
4
|
+
*
|
|
5
|
+
* await this._emitExtensionEvent(event); // <-- bridge runs here, awaited
|
|
6
|
+
* this._emit(event); // sync legacy listeners
|
|
7
|
+
* if (event.type === "message_end") {
|
|
8
|
+
* sessionManager.appendMessage(event.message); // <-- entry id GENERATED HERE
|
|
9
|
+
* }
|
|
10
|
+
*
|
|
11
|
+
* The bridge's old `queueMicrotask` deferral resolves INSIDE the awaited dispatcher,
|
|
12
|
+
* before appendMessage runs — so getLeafId() still returns the previous leaf. The fix
|
|
13
|
+
* is `setTimeout(0)` (macrotask) so the entire await chain unwinds and appendMessage
|
|
14
|
+
* runs first; OR reading `event.message.id` after pi mutates it in-place.
|
|
15
|
+
*
|
|
16
|
+
* This test simulates that ordering and asserts the correct mechanisms.
|
|
17
|
+
*/
|
|
18
|
+
import { describe, it, expect } from "vitest";
|
|
19
|
+
|
|
20
|
+
interface SimMessage {
|
|
21
|
+
role: string;
|
|
22
|
+
content: string;
|
|
23
|
+
id?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Simulate pi 0.70.x's _processAgentEvent ordering. Returns a promise that
|
|
28
|
+
* resolves when the entire event has been processed (including appendMessage).
|
|
29
|
+
*
|
|
30
|
+
* The `bridgeHandler` is registered as an "extension handler" — runs awaited
|
|
31
|
+
* inside _emitExtensionEvent. It receives the event and a pseudo-ctx with
|
|
32
|
+
* sessionManager.getLeafId().
|
|
33
|
+
*/
|
|
34
|
+
async function simulatePi070Emit(opts: {
|
|
35
|
+
event: { type: string; message: SimMessage };
|
|
36
|
+
state: { leafId: string; nextId: string };
|
|
37
|
+
appendMessage: (msg: SimMessage) => string; // returns the new id
|
|
38
|
+
bridgeHandler: (event: any, ctx: any) => Promise<void> | void;
|
|
39
|
+
}): Promise<void> {
|
|
40
|
+
const ctx = {
|
|
41
|
+
sessionManager: { getLeafId: () => opts.state.leafId },
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// Step 1: await _emitExtensionEvent — runs the bridge handler awaited.
|
|
45
|
+
await opts.bridgeHandler(opts.event, ctx);
|
|
46
|
+
|
|
47
|
+
// Step 2: _emit (sync legacy listeners) — no-op in this simulation.
|
|
48
|
+
|
|
49
|
+
// Step 3: persistence on message_end.
|
|
50
|
+
if (opts.event.type === "message_end") {
|
|
51
|
+
const id = opts.appendMessage(opts.event.message);
|
|
52
|
+
opts.state.leafId = id;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
describe("pi 0.70 emit/append ordering", () => {
|
|
57
|
+
it("queueMicrotask deferral DOES NOT capture the post-persist id (the bug)", async () => {
|
|
58
|
+
const state = { leafId: "prev", nextId: "new-id-42" };
|
|
59
|
+
let captured: string | undefined;
|
|
60
|
+
|
|
61
|
+
const buggyBridge = async (event: any, ctx: any) => {
|
|
62
|
+
// What the OLD bridge does today:
|
|
63
|
+
await new Promise<void>((resolve) => queueMicrotask(resolve));
|
|
64
|
+
captured = ctx.sessionManager.getLeafId();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
await simulatePi070Emit({
|
|
68
|
+
event: { type: "message_end", message: { role: "assistant", content: "hi" } },
|
|
69
|
+
state,
|
|
70
|
+
appendMessage: (m) => {
|
|
71
|
+
m.id = state.nextId;
|
|
72
|
+
return state.nextId;
|
|
73
|
+
},
|
|
74
|
+
bridgeHandler: buggyBridge,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Bug: captured is the previous leaf, NOT the just-appended id.
|
|
78
|
+
expect(captured).toBe("prev");
|
|
79
|
+
expect(captured).not.toBe("new-id-42");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("setTimeout(0) deferral DOES capture the post-persist id (the fix)", async () => {
|
|
83
|
+
const state = { leafId: "prev", nextId: "new-id-42" };
|
|
84
|
+
let capturedFromGetLeafId: string | undefined;
|
|
85
|
+
let capturedFromMessageId: string | undefined;
|
|
86
|
+
let sendDone!: () => void;
|
|
87
|
+
const sendCompleted = new Promise<void>((r) => { sendDone = r; });
|
|
88
|
+
|
|
89
|
+
// The bridge schedules and returns synchronously — the only way the
|
|
90
|
+
// awaited dispatcher can unwind so appendMessage runs before the timeout.
|
|
91
|
+
const fixedBridge = (event: any, ctx: any) => {
|
|
92
|
+
setTimeout(() => {
|
|
93
|
+
capturedFromMessageId = (event.message as any).id;
|
|
94
|
+
capturedFromGetLeafId = ctx.sessionManager.getLeafId();
|
|
95
|
+
sendDone();
|
|
96
|
+
}, 0);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
await simulatePi070Emit({
|
|
100
|
+
event: { type: "message_end", message: { role: "assistant", content: "hi" } },
|
|
101
|
+
state,
|
|
102
|
+
appendMessage: (m) => {
|
|
103
|
+
m.id = state.nextId;
|
|
104
|
+
return state.nextId;
|
|
105
|
+
},
|
|
106
|
+
bridgeHandler: fixedBridge,
|
|
107
|
+
});
|
|
108
|
+
await sendCompleted;
|
|
109
|
+
|
|
110
|
+
// Both signals should now point at the just-persisted entry.
|
|
111
|
+
expect(capturedFromMessageId).toBe("new-id-42");
|
|
112
|
+
expect(capturedFromGetLeafId).toBe("new-id-42");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("WeakMap-on-appendMessage captures the id even before the macrotask", async () => {
|
|
116
|
+
const state = { leafId: "prev", nextId: "new-id-77" };
|
|
117
|
+
const idByMessage = new WeakMap<object, string>();
|
|
118
|
+
const wrappedAppend = (m: SimMessage): string => {
|
|
119
|
+
m.id = state.nextId;
|
|
120
|
+
idByMessage.set(m as object, m.id);
|
|
121
|
+
return m.id;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
let viaWeakMap: string | undefined;
|
|
125
|
+
let viaMutation: string | undefined;
|
|
126
|
+
let sendDone!: () => void;
|
|
127
|
+
const sentP = new Promise<void>((r) => { sendDone = r; });
|
|
128
|
+
|
|
129
|
+
// CRITICAL: bridge SCHEDULES the send and RETURNS IMMEDIATELY.
|
|
130
|
+
// It does NOT await its own setTimeout — that would keep the
|
|
131
|
+
// outer dispatcher awaiting and we'd be back to the queueMicrotask
|
|
132
|
+
// bug (timeout fires before appendMessage).
|
|
133
|
+
const fixedBridge = (event: any) => {
|
|
134
|
+
setTimeout(() => {
|
|
135
|
+
viaMutation = (event.message as any).id;
|
|
136
|
+
viaWeakMap = idByMessage.get(event.message as object);
|
|
137
|
+
sendDone();
|
|
138
|
+
}, 0);
|
|
139
|
+
// Return synchronously — let the awaited dispatcher unwind.
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
await simulatePi070Emit({
|
|
143
|
+
event: { type: "message_end", message: { role: "assistant", content: "hi" } },
|
|
144
|
+
state,
|
|
145
|
+
appendMessage: wrappedAppend,
|
|
146
|
+
bridgeHandler: fixedBridge,
|
|
147
|
+
});
|
|
148
|
+
await sentP;
|
|
149
|
+
|
|
150
|
+
expect(viaMutation).toBe("new-id-77");
|
|
151
|
+
expect(viaWeakMap).toBe("new-id-77");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("user message_start has NO id (pi defers user persistence to message_end)", async () => {
|
|
155
|
+
const state = { leafId: "prev-assistant", nextId: "new-user-id" };
|
|
156
|
+
let captured: string | undefined;
|
|
157
|
+
|
|
158
|
+
const fixedBridge = async (event: any) => {
|
|
159
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 0));
|
|
160
|
+
captured = (event.message as any).id; // still undefined for message_start
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
await simulatePi070Emit({
|
|
164
|
+
event: { type: "message_start", message: { role: "user", content: "hello" } },
|
|
165
|
+
state,
|
|
166
|
+
appendMessage: () => state.nextId, // not called for message_start
|
|
167
|
+
bridgeHandler: fixedBridge,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// No id available at message_start time — must rely on entry_persisted
|
|
171
|
+
// back-fill (delivered when the message_end of the SAME message fires later).
|
|
172
|
+
expect(captured).toBeUndefined();
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -74,6 +74,36 @@ describe("mapEventToProtocol", () => {
|
|
|
74
74
|
expect(result.event.data).toEqual(piEvent);
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
+
it("should map an entry_persisted event (per fix-per-message-fork)", () => {
|
|
78
|
+
const piEvent = {
|
|
79
|
+
type: "entry_persisted",
|
|
80
|
+
entryId: "abc-123",
|
|
81
|
+
nonce: "n-7",
|
|
82
|
+
};
|
|
83
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
84
|
+
expect(result.type).toBe("event_forward");
|
|
85
|
+
expect(result.sessionId).toBe(sessionId);
|
|
86
|
+
expect(result.event.eventType).toBe("entry_persisted");
|
|
87
|
+
expect(result.event.data).toMatchObject({
|
|
88
|
+
type: "entry_persisted",
|
|
89
|
+
entryId: "abc-123",
|
|
90
|
+
nonce: "n-7",
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("should map a message_end event with nonce (per fix-per-message-fork)", () => {
|
|
95
|
+
const piEvent = {
|
|
96
|
+
type: "message_end",
|
|
97
|
+
message: { role: "assistant", content: "hi", id: "asst-9" },
|
|
98
|
+
entryId: "asst-9",
|
|
99
|
+
nonce: "n-7",
|
|
100
|
+
};
|
|
101
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
102
|
+
expect(result.event.eventType).toBe("message_end");
|
|
103
|
+
expect((result.event.data as any).nonce).toBe("n-7");
|
|
104
|
+
expect((result.event.data as any).entryId).toBe("asst-9");
|
|
105
|
+
});
|
|
106
|
+
|
|
77
107
|
it("should strip non-serializable fields", () => {
|
|
78
108
|
const piEvent = {
|
|
79
109
|
type: "test_event",
|
|
@@ -1,100 +1,88 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Tests for
|
|
2
|
+
* Tests for bridge entryId stamping on message_end events.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* HISTORY: Originally this file modelled pi <0.69's synchronous emit pattern,
|
|
5
|
+
* where the bridge's `queueMicrotask` deferral ran BEFORE
|
|
6
|
+
* sessionManager.appendMessage. That design no longer reflects pi 0.70.x:
|
|
7
|
+
* pi awaits extension handlers inside _emitExtensionEvent, so the microtask
|
|
8
|
+
* resolves *inside* the awaited dispatcher, before persistence. The fix
|
|
9
|
+
* (see change: fix-per-message-fork) is `setTimeout(0)` (a macrotask)
|
|
10
|
+
* combined with reading `event.message.id` (which pi mutates in place
|
|
11
|
+
* during appendMessage) or a WeakMap populated by the wrapped appendMessage.
|
|
7
12
|
*
|
|
8
|
-
* The
|
|
9
|
-
*
|
|
10
|
-
* The
|
|
11
|
-
*
|
|
13
|
+
* The previous test "message_start should still capture entryId immediately
|
|
14
|
+
* (no deferral)" codified the off-by-one bug as expected behavior — it has
|
|
15
|
+
* been REMOVED. The current test suite below models pi 0.70.x semantics
|
|
16
|
+
* directly. Detailed pi-0.70-specific scenarios live in
|
|
17
|
+
* `bridge-entry-id-pi-070.test.ts`.
|
|
12
18
|
*/
|
|
13
|
-
import { describe, it, expect
|
|
19
|
+
import { describe, it, expect } from "vitest";
|
|
14
20
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* 1. _emit(event) — bridge handler called (async, not awaited)
|
|
20
|
-
* 2. appendMessage(msg) — updates leafId synchronously
|
|
21
|
-
*
|
|
22
|
-
* The bridge handler (async) should yield via queueMicrotask before reading
|
|
23
|
-
* getLeafId(), so that appendMessage has already run.
|
|
24
|
-
*/
|
|
25
|
-
describe("message_end entryId timing", () => {
|
|
26
|
-
it("deferred getLeafId() captures the post-persist entry ID", async () => {
|
|
27
|
-
// Simulate sessionManager with mutable leafId
|
|
28
|
-
let leafId = "user-entry-100"; // stale leaf before appendMessage
|
|
21
|
+
describe("message_end entryId timing on pi 0.70.x", () => {
|
|
22
|
+
it("setTimeout(0) deferral captures the post-persist entry ID", async () => {
|
|
23
|
+
// Simulate pi 0.70.x: bridge handler runs awaited, THEN appendMessage runs.
|
|
24
|
+
let leafId = "user-entry-100";
|
|
29
25
|
const sessionManager = {
|
|
30
26
|
getLeafId: () => leafId,
|
|
27
|
+
appendMessage: (msg: any) => {
|
|
28
|
+
msg.id = "assistant-entry-101";
|
|
29
|
+
leafId = msg.id;
|
|
30
|
+
return msg.id;
|
|
31
|
+
},
|
|
31
32
|
};
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
const event = { type: "message_end", message: { role: "assistant" } as any };
|
|
35
|
+
let captured: string | undefined;
|
|
36
|
+
let sendDone!: () => void;
|
|
37
|
+
const sentP = new Promise<void>((r) => { sendDone = r; });
|
|
38
|
+
|
|
39
|
+
// Bridge handler: schedules a setTimeout(0) and returns synchronously.
|
|
40
|
+
// The awaited dispatcher then unwinds, appendMessage runs, AND finally
|
|
41
|
+
// the timeout fires.
|
|
42
|
+
const bridgeHandler = (ev: any) => {
|
|
43
|
+
setTimeout(() => {
|
|
44
|
+
captured = ev.message.id ?? sessionManager.getLeafId();
|
|
45
|
+
sendDone();
|
|
46
|
+
}, 0);
|
|
40
47
|
};
|
|
41
48
|
|
|
42
|
-
// Simulate
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
leafId = "assistant-entry-101";
|
|
47
|
-
|
|
48
|
-
// Wait for the deferred handler to complete
|
|
49
|
-
await handlerPromise;
|
|
49
|
+
// Simulate the dispatcher: await handler, then call appendMessage.
|
|
50
|
+
await bridgeHandler(event);
|
|
51
|
+
sessionManager.appendMessage(event.message);
|
|
52
|
+
await sentP;
|
|
50
53
|
|
|
51
|
-
expect(
|
|
54
|
+
expect(captured).toBe("assistant-entry-101");
|
|
52
55
|
});
|
|
53
56
|
|
|
54
|
-
it("
|
|
57
|
+
it("queueMicrotask deferral would NOT work on pi 0.70.x (regression demonstration)", async () => {
|
|
58
|
+
// Reproduces why we abandoned queueMicrotask: it resolves inside the
|
|
59
|
+
// awaited dispatcher, before appendMessage runs.
|
|
55
60
|
let leafId = "user-entry-100";
|
|
56
|
-
|
|
57
|
-
getLeafId: () => leafId,
|
|
58
|
-
};
|
|
61
|
+
let captured: string | undefined;
|
|
59
62
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const buggyBridgeHandler = async () => {
|
|
64
|
-
// No deferral — reads leafId before appendMessage runs
|
|
65
|
-
capturedEntryId = sessionManager.getLeafId();
|
|
63
|
+
const buggyBridge = async () => {
|
|
64
|
+
await new Promise<void>((resolve) => queueMicrotask(resolve));
|
|
65
|
+
captured = leafId;
|
|
66
66
|
};
|
|
67
67
|
|
|
68
|
-
//
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
await handlerPromise;
|
|
68
|
+
// Pi 0.70.x: await the bridge, THEN persist. Mirroring the real ordering:
|
|
69
|
+
const handlerP = buggyBridge();
|
|
70
|
+
await handlerP;
|
|
71
|
+
leafId = "assistant-entry-101"; // appendMessage runs after await
|
|
73
72
|
|
|
74
|
-
|
|
75
|
-
expect(
|
|
73
|
+
expect(captured).toBe("user-entry-100");
|
|
74
|
+
expect(captured).not.toBe("assistant-entry-101");
|
|
76
75
|
});
|
|
77
76
|
|
|
78
|
-
it("
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
// Simulate bridge handler for message_start (immediate, no deferral)
|
|
87
|
-
const messageStartHandler = async () => {
|
|
88
|
-
capturedEntryId = sessionManager.getLeafId();
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const handlerPromise = messageStartHandler();
|
|
92
|
-
// User entry gets written after message_start
|
|
93
|
-
leafId = "user-entry-100";
|
|
94
|
-
|
|
95
|
-
await handlerPromise;
|
|
77
|
+
it("entry_persisted is the back-fill mechanism for user message_start (where event.message.id is unavailable)", () => {
|
|
78
|
+
// Behavioural assertion (pure data shape): when the bridge sends a
|
|
79
|
+
// user message_start, it stamps a nonce; later when pi persists the
|
|
80
|
+
// user entry, the bridge sends entry_persisted { nonce, entryId }.
|
|
81
|
+
// The reducer pairs them by nonce. See change: fix-per-message-fork.
|
|
82
|
+
const start = { type: "message_start", message: { role: "user" }, nonce: "n-1" };
|
|
83
|
+
const persisted = { type: "entry_persisted", entryId: "user-200", nonce: "n-1" };
|
|
96
84
|
|
|
97
|
-
|
|
98
|
-
expect(
|
|
85
|
+
expect(start.nonce).toBe(persisted.nonce);
|
|
86
|
+
expect(persisted.entryId).toBe("user-200");
|
|
99
87
|
});
|
|
100
88
|
});
|