@lionel-guo/pi-continue 0.0.0-stage → 0.1.0
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/GLOSSARY.md +5 -0
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/extensions/pi-continue.js +165 -0
- package/package.json +51 -4
- package/test/continue.test.js +436 -0
package/GLOSSARY.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Glossary
|
|
2
|
+
|
|
3
|
+
- **Partial reply**: An assistant message whose stop reason is `aborted` or `error`. Pi retains it in the transcript, while its shared message filter omits it from provider context.
|
|
4
|
+
- **Continuation trigger**: The hidden custom message that starts one new model request. The extension filters it from each provider context and records a context edit to omit it from later session projections.
|
|
5
|
+
- **Continuation marker**: A custom session entry shown in the transcript to identify the skipped partial reply. It is not sent to the model.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lionel Guo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-continue
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Install the extension with Pi:
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pi install npm:@lionel-guo/pi-continue
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
After an assistant reply ends with an aborted or error stop, run `/continue` to ask the current model to generate again from the complete context. The partial reply remains visible in the transcript but is omitted by Pi's shared message filter. A marker records its stop reason and content-block composition. The command does not resume at the next token, and it does not execute tool calls contained in the failed reply.
|
|
10
|
+
|
|
11
|
+
The command refuses while a reply is running, while messages are queued, when arguments are supplied, or when the latest assistant reply is not aborted or errored.
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
const MARKER_TYPE = "pi-continue";
|
|
4
|
+
const TRIGGER_TYPE = "pi-continue-trigger";
|
|
5
|
+
const FILTER_DESCRIPTION =
|
|
6
|
+
"Pi's shared transformMessages fixture check (aborted/error fixtures omitted); final provider payload was not inspected";
|
|
7
|
+
const loadSharedFilter = () => import("@earendil-works/pi-ai/api/transform-messages");
|
|
8
|
+
|
|
9
|
+
export default function registerContinue(pi, { loadTransformMessages = loadSharedFilter } = {}) {
|
|
10
|
+
let pendingMarker;
|
|
11
|
+
pi.registerCommand("continue", {
|
|
12
|
+
description: "Recover from the latest aborted or errored assistant reply",
|
|
13
|
+
handler: async (args, ctx) => {
|
|
14
|
+
if (args.trim()) {
|
|
15
|
+
ctx.ui.notify("/continue does not accept arguments.", "warning");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (!ctx.isIdle()) {
|
|
19
|
+
ctx.ui.notify("/continue is unavailable while a reply is running.", "warning");
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (ctx.hasPendingMessages()) {
|
|
23
|
+
ctx.ui.notify("/continue is unavailable while messages are queued.", "warning");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const partial = findLatestFailedReply(ctx.sessionManager.buildSessionProjection().messages);
|
|
28
|
+
if (!partial) {
|
|
29
|
+
ctx.ui.notify("The latest assistant reply is not aborted or errored.", "info");
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!ctx.model || !(await passesSharedFilter(ctx.model, loadTransformMessages))) {
|
|
34
|
+
ctx.ui.notify("/continue is unavailable because Pi's shared message filter could not be verified.", "warning");
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
pendingMarker = {
|
|
39
|
+
stopReason: partial.stopReason,
|
|
40
|
+
contentBlocks: Array.isArray(partial.content) ? partial.content.map((block) => block.type) : [],
|
|
41
|
+
filter: FILTER_DESCRIPTION,
|
|
42
|
+
};
|
|
43
|
+
try {
|
|
44
|
+
pi.sendMessage(
|
|
45
|
+
{
|
|
46
|
+
customType: TRIGGER_TYPE,
|
|
47
|
+
content: [],
|
|
48
|
+
display: false,
|
|
49
|
+
details: undefined,
|
|
50
|
+
},
|
|
51
|
+
{ triggerTurn: true },
|
|
52
|
+
);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
pendingMarker = undefined;
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
pi.registerEntryRenderer(MARKER_TYPE, (entry, _options, theme) => {
|
|
61
|
+
const { stopReason, contentBlocks = [], filter = FILTER_DESCRIPTION } = entry.data ?? {};
|
|
62
|
+
const composition = describeContentBlocks(contentBlocks);
|
|
63
|
+
return new Text(
|
|
64
|
+
theme.fg(
|
|
65
|
+
"dim",
|
|
66
|
+
`[continue] Skipped ${stopReason} reply (${composition}); passed ${filter}.`,
|
|
67
|
+
),
|
|
68
|
+
0,
|
|
69
|
+
0,
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
pi.on("before_agent_start", (event) => {
|
|
74
|
+
if (event.messageType !== "custom" || event.message.customType !== TRIGGER_TYPE) return;
|
|
75
|
+
if (!pendingMarker) return;
|
|
76
|
+
const marker = pendingMarker;
|
|
77
|
+
pendingMarker = undefined;
|
|
78
|
+
pi.appendEntry(MARKER_TYPE, marker);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
pi.on("context", (event) => {
|
|
82
|
+
if (!event.messages.some((message) => message.role === "custom" && message.customType === TRIGGER_TYPE)) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
messages: event.messages.filter(
|
|
87
|
+
(message) => message.role !== "custom" || message.customType !== TRIGGER_TYPE,
|
|
88
|
+
),
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
pi.on("turn_end", (event) => {
|
|
93
|
+
const trigger = event.context.contextEntries.findLast(
|
|
94
|
+
({ sourceEntry, messages }) =>
|
|
95
|
+
sourceEntry.type === "custom_message" &&
|
|
96
|
+
sourceEntry.customType === TRIGGER_TYPE &&
|
|
97
|
+
messages.length > 0,
|
|
98
|
+
);
|
|
99
|
+
if (!trigger) return;
|
|
100
|
+
return {
|
|
101
|
+
entries: [
|
|
102
|
+
...event.entries,
|
|
103
|
+
{ type: "context_edit", targetId: trigger.sourceEntry.id, replacement: null },
|
|
104
|
+
],
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function passesSharedFilter(model, loadTransformMessages) {
|
|
110
|
+
try {
|
|
111
|
+
const { transformMessages } = await loadTransformMessages();
|
|
112
|
+
if (typeof transformMessages !== "function") return false;
|
|
113
|
+
|
|
114
|
+
const aborted = assistantFixture(model, "aborted", "aborted fixture");
|
|
115
|
+
const errored = assistantFixture(model, "error", "error fixture");
|
|
116
|
+
const filtered = transformMessages([aborted, errored], model);
|
|
117
|
+
return !filtered.some(
|
|
118
|
+
(message) =>
|
|
119
|
+
message.role === "assistant" &&
|
|
120
|
+
message.content.some(
|
|
121
|
+
(block) =>
|
|
122
|
+
block.type === "text" &&
|
|
123
|
+
(block.text === "aborted fixture" || block.text === "error fixture"),
|
|
124
|
+
),
|
|
125
|
+
);
|
|
126
|
+
} catch {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function assistantFixture(model, stopReason, text) {
|
|
132
|
+
return {
|
|
133
|
+
role: "assistant",
|
|
134
|
+
content: [{ type: "text", text }],
|
|
135
|
+
api: model.api,
|
|
136
|
+
provider: model.provider,
|
|
137
|
+
model: model.id,
|
|
138
|
+
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 },
|
|
139
|
+
stopReason,
|
|
140
|
+
timestamp: 0,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function findLatestFailedReply(messages) {
|
|
145
|
+
const index = messages.findLastIndex((message) => message.role === "assistant");
|
|
146
|
+
if (index < 0) return undefined;
|
|
147
|
+
const partial = messages[index];
|
|
148
|
+
if (partial.stopReason !== "aborted" && partial.stopReason !== "error") return undefined;
|
|
149
|
+
if (messages.slice(index + 1).some((message) =>
|
|
150
|
+
message.role === "user" || message.role === "toolResult" || message.role === "custom")) {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
return partial;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function describeContentBlocks(contentBlocks) {
|
|
157
|
+
if (contentBlocks.length === 0) return "empty";
|
|
158
|
+
const counts = new Map();
|
|
159
|
+
for (const type of contentBlocks) counts.set(type, (counts.get(type) ?? 0) + 1);
|
|
160
|
+
return [...counts]
|
|
161
|
+
.map(([type, count]) => `${count} ${type === "toolCall" ? "tool call" : type}`)
|
|
162
|
+
.join(", ");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export { MARKER_TYPE, TRIGGER_TYPE };
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lionel-guo/pi-continue",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Recover Pi conversations from aborted and errored assistant replies",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./extensions/pi-continue.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./extensions/pi-continue.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"extensions",
|
|
13
|
+
"test",
|
|
14
|
+
"README.md",
|
|
15
|
+
"GLOSSARY.md",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"package.json"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"pi-package",
|
|
21
|
+
"pi-extension"
|
|
22
|
+
],
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/LionelGuo/pi-continue.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/LionelGuo/pi-continue/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/LionelGuo/pi-continue#readme",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"pi": {
|
|
36
|
+
"extensions": [
|
|
37
|
+
"./extensions/pi-continue.js"
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@earendil-works/pi-ai": "*",
|
|
42
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
43
|
+
"@earendil-works/pi-tui": "*"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@earendil-works/pi-ai": "1.6.0",
|
|
47
|
+
"@earendil-works/pi-coding-agent": "1.6.0",
|
|
48
|
+
"@earendil-works/pi-tui": "1.6.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"test": "node --test"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import test from "node:test";
|
|
6
|
+
import {
|
|
7
|
+
createAgentSession,
|
|
8
|
+
DefaultResourceLoader,
|
|
9
|
+
ModelRuntime,
|
|
10
|
+
SessionManager,
|
|
11
|
+
} from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import { fauxAssistantMessage, fauxProvider, fauxToolCall } from "@earendil-works/pi-ai/providers/faux";
|
|
13
|
+
import { transformMessages } from "@earendil-works/pi-ai/api/transform-messages";
|
|
14
|
+
|
|
15
|
+
const now = "2026-01-01T00:00:00.000Z";
|
|
16
|
+
const userText = "Inspect the project";
|
|
17
|
+
const partialText = "half written";
|
|
18
|
+
const priorToolResult = "earlier tool result";
|
|
19
|
+
|
|
20
|
+
test("/continue retries an aborted reply from the last complete context", async () => {
|
|
21
|
+
await withFixture({
|
|
22
|
+
partial: fauxAssistantMessage(
|
|
23
|
+
[
|
|
24
|
+
{ type: "text", text: partialText },
|
|
25
|
+
fauxToolCall("write", { path: "danger.txt", content: "must not run" }, { id: "partial-call" }),
|
|
26
|
+
],
|
|
27
|
+
{ stopReason: "aborted" },
|
|
28
|
+
),
|
|
29
|
+
}, async (fixture) => {
|
|
30
|
+
fixture.faux.setResponses([
|
|
31
|
+
(context, _options, _state, model) => {
|
|
32
|
+
fixture.providerTranscripts.push(transformMessages(context.messages, model));
|
|
33
|
+
return fauxAssistantMessage("recovered", { stopReason: "stop" });
|
|
34
|
+
},
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
await fixture.session.prompt("/continue");
|
|
38
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
39
|
+
await fixture.session.waitForIdle();
|
|
40
|
+
|
|
41
|
+
assert.equal(fixture.faux.state.callCount, 1);
|
|
42
|
+
assert.equal(fixture.providerTranscripts.length, 1);
|
|
43
|
+
const transcript = fixture.providerTranscripts[0].filter((message) => message.role !== "system");
|
|
44
|
+
assert.deepEqual(transcript.map((message) => message.role), ["user", "assistant", "toolResult"]);
|
|
45
|
+
assert.equal(JSON.stringify(transcript).includes(partialText), false);
|
|
46
|
+
assert.equal(JSON.stringify(transcript).includes("partial-call"), false);
|
|
47
|
+
assert.equal(JSON.stringify(transcript).includes("pi-continue-trigger"), false);
|
|
48
|
+
assert.equal(JSON.stringify(transcript).includes(priorToolResult), true);
|
|
49
|
+
|
|
50
|
+
const entries = fixture.sessionManager.getEntries();
|
|
51
|
+
const marker = entries.find((entry) => entry.type === "custom" && entry.customType === "pi-continue");
|
|
52
|
+
const trigger = entries.find((entry) => entry.type === "custom_message" && entry.customType === "pi-continue-trigger");
|
|
53
|
+
const recovered = entries.find((entry) => entry.type === "message" && entry.message.role === "assistant" && entry.message.content.some((block) => block.type === "text" && block.text === "recovered"));
|
|
54
|
+
const edit = entries.find((entry) => entry.type === "context_edit");
|
|
55
|
+
const originalPartial = entries.find((entry) => entry.type === "message" && entry.message.role === "assistant" && entry.message.stopReason === "aborted");
|
|
56
|
+
|
|
57
|
+
assert.ok(marker);
|
|
58
|
+
assert.equal(marker.data.stopReason, "aborted");
|
|
59
|
+
assert.deepEqual(marker.data.contentBlocks, ["text", "toolCall"]);
|
|
60
|
+
assert.equal(
|
|
61
|
+
marker.data.filter,
|
|
62
|
+
"Pi's shared transformMessages fixture check (aborted/error fixtures omitted); final provider payload was not inspected",
|
|
63
|
+
);
|
|
64
|
+
assert.ok(trigger);
|
|
65
|
+
assert.equal(trigger.display, false);
|
|
66
|
+
assert.deepEqual(trigger.content, []);
|
|
67
|
+
assert.ok(recovered);
|
|
68
|
+
assert.ok(edit);
|
|
69
|
+
assert.equal(edit.targetId, trigger.id);
|
|
70
|
+
assert.equal(edit.replacement, null);
|
|
71
|
+
assert.ok(entries.indexOf(marker) < entries.indexOf(trigger));
|
|
72
|
+
assert.ok(entries.indexOf(trigger) < entries.indexOf(recovered));
|
|
73
|
+
assert.equal(entries.filter((entry) => entry.type === "custom" && entry.customType === "pi-continue").length, 1);
|
|
74
|
+
assert.equal(entries.filter((entry) => entry.type === "message" && entry.message.role === "user").length, 1);
|
|
75
|
+
assert.equal(originalPartial.message.stopReason, "aborted");
|
|
76
|
+
assert.deepEqual(originalPartial.message.content.map((block) => block.type), ["text", "toolCall"]);
|
|
77
|
+
const projected = fixture.sessionManager.buildSessionProjection().messages;
|
|
78
|
+
assert.equal(projected.some((message) => message.role === "custom" && message.customType === "pi-continue-trigger"), false);
|
|
79
|
+
assert.equal(projected.some((message) => message.role === "assistant" && message.stopReason === "aborted"), true);
|
|
80
|
+
assert.equal(fixture.toolExecutionState.count, 0);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("/continue retries an empty errored reply", async () => {
|
|
85
|
+
await withFixture({
|
|
86
|
+
partial: fauxAssistantMessage([], { stopReason: "error" }),
|
|
87
|
+
priorTool: false,
|
|
88
|
+
}, async (fixture) => {
|
|
89
|
+
fixture.faux.setResponses([
|
|
90
|
+
(context, _options, _state, model) => {
|
|
91
|
+
fixture.providerTranscripts.push(transformMessages(context.messages, model));
|
|
92
|
+
return fauxAssistantMessage("recovered", { stopReason: "stop" });
|
|
93
|
+
},
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
await fixture.session.prompt("/continue");
|
|
97
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
98
|
+
await fixture.session.waitForIdle();
|
|
99
|
+
|
|
100
|
+
assert.equal(fixture.faux.state.callCount, 1);
|
|
101
|
+
assert.deepEqual(fixture.providerTranscripts[0].filter((message) => message.role !== "system").map((message) => message.role), ["user"]);
|
|
102
|
+
const entries = fixture.sessionManager.getEntries();
|
|
103
|
+
const marker = entries.find((entry) => entry.type === "custom" && entry.customType === "pi-continue");
|
|
104
|
+
assert.ok(marker);
|
|
105
|
+
assert.equal(marker.data.stopReason, "error");
|
|
106
|
+
assert.deepEqual(marker.data.contentBlocks, []);
|
|
107
|
+
assert.ok(entries.some((entry) => entry.type === "context_edit" && entry.replacement === null));
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("/continue refuses unsupported latest states through the RPC and print SDK seams", async (t) => {
|
|
112
|
+
const cases = [
|
|
113
|
+
{ name: "arguments", command: "/continue again", partial: fauxAssistantMessage("partial", { stopReason: "aborted" }) },
|
|
114
|
+
{ name: "length stop", command: "/continue", partial: fauxAssistantMessage("partial", { stopReason: "length" }) },
|
|
115
|
+
{ name: "length stop in RPC mode", command: "/continue", partial: fauxAssistantMessage("partial", { stopReason: "length" }), mode: "rpc" },
|
|
116
|
+
{ name: "completed stop", command: "/continue", partial: fauxAssistantMessage("partial", { stopReason: "stop" }) },
|
|
117
|
+
{
|
|
118
|
+
name: "tool-use stop",
|
|
119
|
+
command: "/continue",
|
|
120
|
+
partial: fauxAssistantMessage(fauxToolCall("read", { path: "notes.txt" }, { id: "unfinished-call" }), { stopReason: "toolUse" }),
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: "trailing user message",
|
|
124
|
+
command: "/continue",
|
|
125
|
+
partial: fauxAssistantMessage("partial", { stopReason: "aborted" }),
|
|
126
|
+
trailing: [messageEntry("trailing-user", "partial", {
|
|
127
|
+
role: "user",
|
|
128
|
+
content: [{ type: "text", text: "follow-up" }],
|
|
129
|
+
timestamp: 3,
|
|
130
|
+
})],
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
name: "trailing tool result",
|
|
134
|
+
command: "/continue",
|
|
135
|
+
partial: fauxAssistantMessage("partial", { stopReason: "aborted" }),
|
|
136
|
+
trailing: [messageEntry("trailing-result", "partial", {
|
|
137
|
+
role: "toolResult",
|
|
138
|
+
toolCallId: "unfinished-call",
|
|
139
|
+
toolName: "read",
|
|
140
|
+
content: [{ type: "text", text: "already returned" }],
|
|
141
|
+
isError: false,
|
|
142
|
+
timestamp: 3,
|
|
143
|
+
})],
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: "trailing model-facing custom message",
|
|
147
|
+
command: "/continue",
|
|
148
|
+
partial: fauxAssistantMessage("partial", { stopReason: "aborted" }),
|
|
149
|
+
trailing: [customMessageEntry("trailing-custom", "partial", "model-facing", "context message")],
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
|
|
153
|
+
for (const scenario of cases) {
|
|
154
|
+
await t.test(scenario.name, async () => {
|
|
155
|
+
await withFixture({
|
|
156
|
+
partial: scenario.partial,
|
|
157
|
+
trailing: scenario.trailing,
|
|
158
|
+
priorTool: false,
|
|
159
|
+
mode: scenario.mode,
|
|
160
|
+
}, async (fixture) => {
|
|
161
|
+
const before = fixture.sessionManager.getEntryCount();
|
|
162
|
+
await fixture.session.prompt(scenario.command);
|
|
163
|
+
|
|
164
|
+
assertRefused(fixture, { before });
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("/continue refuses when the shared filter keeps failed replies", async () => {
|
|
171
|
+
await withFixture({
|
|
172
|
+
partial: fauxAssistantMessage("partial", { stopReason: "aborted" }),
|
|
173
|
+
priorTool: false,
|
|
174
|
+
loadTransformMessages: async () => ({
|
|
175
|
+
transformMessages(messages) {
|
|
176
|
+
return messages;
|
|
177
|
+
},
|
|
178
|
+
}),
|
|
179
|
+
}, async (fixture) => {
|
|
180
|
+
const before = fixture.sessionManager.getEntryCount();
|
|
181
|
+
await fixture.session.prompt("/continue");
|
|
182
|
+
|
|
183
|
+
assertRefused(fixture, { before });
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("/continue refuses during an active streaming reply", async () => {
|
|
188
|
+
await withFixture({
|
|
189
|
+
partial: fauxAssistantMessage("partial", { stopReason: "aborted" }),
|
|
190
|
+
priorTool: false,
|
|
191
|
+
fauxOptions: { tokensPerSecond: 1, tokenSize: { min: 1, max: 1 } },
|
|
192
|
+
}, async (fixture) => {
|
|
193
|
+
fixture.faux.setResponses([fauxAssistantMessage("a deliberately long active response", { stopReason: "stop" })]);
|
|
194
|
+
const running = fixture.session.prompt("start a reply");
|
|
195
|
+
await waitUntil(() => fixture.session.isStreaming);
|
|
196
|
+
await fixture.session.prompt("/continue");
|
|
197
|
+
|
|
198
|
+
assert.equal(fixture.faux.state.callCount, 1);
|
|
199
|
+
assertRefused(fixture, { callCount: 1 });
|
|
200
|
+
|
|
201
|
+
fixture.session.clearQueue();
|
|
202
|
+
await fixture.session.abort();
|
|
203
|
+
await running;
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("/continue refuses when steering and follow-up text is queued", async () => {
|
|
208
|
+
await withFixture({
|
|
209
|
+
partial: fauxAssistantMessage("partial", { stopReason: "aborted" }),
|
|
210
|
+
priorTool: false,
|
|
211
|
+
fauxOptions: { tokensPerSecond: 1, tokenSize: { min: 1, max: 1 } },
|
|
212
|
+
}, async (fixture) => {
|
|
213
|
+
fixture.faux.setResponses([fauxAssistantMessage("a deliberately long active response", { stopReason: "stop" })]);
|
|
214
|
+
const running = fixture.session.prompt("start a reply");
|
|
215
|
+
await waitUntil(() => fixture.session.isStreaming);
|
|
216
|
+
await fixture.session.steer("queued steering");
|
|
217
|
+
await fixture.session.followUp("queued follow-up");
|
|
218
|
+
await fixture.session.prompt("/continue");
|
|
219
|
+
|
|
220
|
+
assert.equal(fixture.session.pendingMessageCount, 2);
|
|
221
|
+
assertRefused(fixture, { callCount: 1 });
|
|
222
|
+
|
|
223
|
+
fixture.session.clearQueue();
|
|
224
|
+
await fixture.session.abort();
|
|
225
|
+
await running;
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("repeated continuations skip all old failures and mark only each latest partial", async () => {
|
|
230
|
+
await withFixture({
|
|
231
|
+
partial: fauxAssistantMessage("initial partial", { stopReason: "aborted" }),
|
|
232
|
+
priorTool: false,
|
|
233
|
+
}, async (fixture) => {
|
|
234
|
+
fixture.faux.setResponses([
|
|
235
|
+
(context, _options, _state, model) => {
|
|
236
|
+
fixture.providerTranscripts.push(transformMessages(context.messages, model));
|
|
237
|
+
return fauxAssistantMessage("first continuation failed", { stopReason: "error" });
|
|
238
|
+
},
|
|
239
|
+
(context, _options, _state, model) => {
|
|
240
|
+
fixture.providerTranscripts.push(transformMessages(context.messages, model));
|
|
241
|
+
return fauxAssistantMessage("second continuation failed", { stopReason: "aborted" });
|
|
242
|
+
},
|
|
243
|
+
]);
|
|
244
|
+
|
|
245
|
+
await fixture.session.prompt("/continue");
|
|
246
|
+
await fixture.session.waitForIdle();
|
|
247
|
+
await fixture.session.prompt("/continue");
|
|
248
|
+
await fixture.session.waitForIdle();
|
|
249
|
+
|
|
250
|
+
assert.equal(fixture.faux.state.callCount, 2);
|
|
251
|
+
assert.equal(fixture.notifications.length, 0);
|
|
252
|
+
assert.equal(fixture.sessionManager.getEntries().filter((entry) => entry.customType === "pi-continue").length, 2);
|
|
253
|
+
assert.equal(fixture.sessionManager.getEntries().filter((entry) => entry.customType === "pi-continue-trigger").length, 2);
|
|
254
|
+
for (const transcript of fixture.providerTranscripts) {
|
|
255
|
+
const serialized = JSON.stringify(transcript);
|
|
256
|
+
assert.equal(serialized.includes("initial partial"), false);
|
|
257
|
+
assert.equal(serialized.includes("first continuation failed"), false);
|
|
258
|
+
assert.equal(serialized.includes("second continuation failed"), false);
|
|
259
|
+
}
|
|
260
|
+
const triggerIds = fixture.sessionManager.getEntries()
|
|
261
|
+
.filter((entry) => entry.customType === "pi-continue-trigger")
|
|
262
|
+
.map((entry) => entry.id);
|
|
263
|
+
const contextEdits = fixture.sessionManager.getEntries()
|
|
264
|
+
.filter((entry) => entry.type === "context_edit");
|
|
265
|
+
assert.equal(contextEdits.length, triggerIds.length);
|
|
266
|
+
assert.deepEqual(
|
|
267
|
+
contextEdits.map((entry) => entry.targetId),
|
|
268
|
+
triggerIds,
|
|
269
|
+
);
|
|
270
|
+
assert.ok(contextEdits.every((entry) => entry.replacement === null));
|
|
271
|
+
const projected = fixture.sessionManager.buildSessionProjection().messages;
|
|
272
|
+
assert.equal(
|
|
273
|
+
projected.some((message) => message.role === "custom" && message.customType === "pi-continue-trigger"),
|
|
274
|
+
false,
|
|
275
|
+
);
|
|
276
|
+
assert.deepEqual(
|
|
277
|
+
fixture.sessionManager.getEntries()
|
|
278
|
+
.filter((entry) => entry.customType === "pi-continue")
|
|
279
|
+
.map((entry) => entry.data.stopReason),
|
|
280
|
+
["aborted", "error"],
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test("a completed continuation cannot be continued again", async () => {
|
|
286
|
+
await withFixture({
|
|
287
|
+
partial: fauxAssistantMessage("partial", { stopReason: "aborted" }),
|
|
288
|
+
priorTool: false,
|
|
289
|
+
}, async (fixture) => {
|
|
290
|
+
fixture.faux.setResponses([fauxAssistantMessage("recovered", { stopReason: "stop" })]);
|
|
291
|
+
await fixture.session.prompt("/continue");
|
|
292
|
+
await fixture.session.waitForIdle();
|
|
293
|
+
const before = fixture.sessionManager.getEntryCount();
|
|
294
|
+
|
|
295
|
+
await fixture.session.prompt("/continue");
|
|
296
|
+
|
|
297
|
+
assert.equal(fixture.faux.state.callCount, 1);
|
|
298
|
+
assert.equal(fixture.sessionManager.getEntryCount(), before);
|
|
299
|
+
assert.equal(fixture.notifications.length, 1);
|
|
300
|
+
assert.equal(fixture.sessionManager.getEntries().filter((entry) => entry.customType === "pi-continue").length, 1);
|
|
301
|
+
assert.equal(fixture.sessionManager.getEntries().filter((entry) => entry.customType === "pi-continue-trigger").length, 1);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
async function withFixture(options, callback) {
|
|
306
|
+
const fixture = await createFixture(options);
|
|
307
|
+
try {
|
|
308
|
+
return await callback(fixture);
|
|
309
|
+
} finally {
|
|
310
|
+
fixture.session.dispose();
|
|
311
|
+
rmSync(fixture.agentDir, { recursive: true, force: true });
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function assertRefused(fixture, { before, callCount = 0 } = {}) {
|
|
316
|
+
assert.equal(fixture.faux.state.callCount, callCount);
|
|
317
|
+
if (before !== undefined) assert.equal(fixture.sessionManager.getEntryCount(), before);
|
|
318
|
+
assert.equal(fixture.notifications.length, 1);
|
|
319
|
+
assert.equal(fixture.sessionManager.getEntries().some((entry) => entry.customType === "pi-continue"), false);
|
|
320
|
+
assert.equal(fixture.sessionManager.getEntries().some((entry) => entry.customType === "pi-continue-trigger"), false);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async function createFixture({ partial, priorTool = true, trailing = [], loadTransformMessages, fauxOptions, mode = "print" } = {}) {
|
|
324
|
+
const agentDir = mkdtempSync(path.join(tmpdir(), "pi-continue-sdk-"));
|
|
325
|
+
const faux = fauxProvider({ provider: "pi-continue-test", ...fauxOptions });
|
|
326
|
+
const providerTranscripts = [];
|
|
327
|
+
const modelRuntime = await ModelRuntime.create({
|
|
328
|
+
authPath: path.join(agentDir, "auth.json"),
|
|
329
|
+
modelsPath: null,
|
|
330
|
+
allowModelNetwork: false,
|
|
331
|
+
refreshOnCreate: false,
|
|
332
|
+
});
|
|
333
|
+
modelRuntime.registerNativeProvider(faux.provider);
|
|
334
|
+
const model = modelRuntime.getModel(faux.provider.id, faux.getModel().id);
|
|
335
|
+
assert.ok(model);
|
|
336
|
+
|
|
337
|
+
const user = {
|
|
338
|
+
role: "user",
|
|
339
|
+
content: [{ type: "text", text: userText }],
|
|
340
|
+
timestamp: 1,
|
|
341
|
+
};
|
|
342
|
+
const entries = [
|
|
343
|
+
{ type: "session", version: 3, id: "session", timestamp: now, cwd: process.cwd() },
|
|
344
|
+
messageEntry("user", null, user),
|
|
345
|
+
];
|
|
346
|
+
let parentId = "user";
|
|
347
|
+
if (priorTool) {
|
|
348
|
+
const assistant = fauxAssistantMessage(
|
|
349
|
+
fauxToolCall("read", { path: "notes.txt" }, { id: "prior-call" }),
|
|
350
|
+
{ stopReason: "toolUse" },
|
|
351
|
+
);
|
|
352
|
+
const result = {
|
|
353
|
+
role: "toolResult",
|
|
354
|
+
toolCallId: "prior-call",
|
|
355
|
+
toolName: "read",
|
|
356
|
+
content: [{ type: "text", text: priorToolResult }],
|
|
357
|
+
isError: false,
|
|
358
|
+
timestamp: 2,
|
|
359
|
+
};
|
|
360
|
+
entries.push(messageEntry("prior-assistant", parentId, assistant));
|
|
361
|
+
entries.push(messageEntry("prior-result", "prior-assistant", result));
|
|
362
|
+
parentId = "prior-result";
|
|
363
|
+
}
|
|
364
|
+
entries.push(messageEntry("partial", parentId, partial));
|
|
365
|
+
entries.push(...trailing);
|
|
366
|
+
entries.push({
|
|
367
|
+
type: "custom",
|
|
368
|
+
id: "bookkeeping",
|
|
369
|
+
parentId: trailing.at(-1)?.id ?? "partial",
|
|
370
|
+
timestamp: now,
|
|
371
|
+
customType: "unrelated-bookkeeping",
|
|
372
|
+
data: { retained: true },
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
const sessionManager = SessionManager.inMemory(process.cwd(), undefined, entries);
|
|
376
|
+
const resourceLoader = new DefaultResourceLoader({
|
|
377
|
+
cwd: process.cwd(),
|
|
378
|
+
agentDir,
|
|
379
|
+
extensionFactories: [
|
|
380
|
+
async (pi) => {
|
|
381
|
+
const { default: extension } = await import("../extensions/pi-continue.js");
|
|
382
|
+
extension(pi, loadTransformMessages ? { loadTransformMessages } : undefined);
|
|
383
|
+
},
|
|
384
|
+
],
|
|
385
|
+
});
|
|
386
|
+
await resourceLoader.reload();
|
|
387
|
+
const { session } = await createAgentSession({
|
|
388
|
+
cwd: process.cwd(),
|
|
389
|
+
model,
|
|
390
|
+
modelRuntime,
|
|
391
|
+
noTools: "all",
|
|
392
|
+
resourceLoader,
|
|
393
|
+
sessionManager,
|
|
394
|
+
});
|
|
395
|
+
const notifications = [];
|
|
396
|
+
const currentUi = session.extensionRunner.getUIContext();
|
|
397
|
+
session.extensionRunner.setUIContext(
|
|
398
|
+
{
|
|
399
|
+
...currentUi,
|
|
400
|
+
notify(message, type) {
|
|
401
|
+
notifications.push({ message, type });
|
|
402
|
+
},
|
|
403
|
+
},
|
|
404
|
+
mode,
|
|
405
|
+
);
|
|
406
|
+
const toolExecutionState = { count: 0 };
|
|
407
|
+
session.subscribe((event) => {
|
|
408
|
+
if (event.type === "tool_execution_start") toolExecutionState.count++;
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
return { agentDir, faux, notifications, providerTranscripts, session, sessionManager, toolExecutionState };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function customMessageEntry(id, parentId, customType, text) {
|
|
415
|
+
return {
|
|
416
|
+
type: "custom_message",
|
|
417
|
+
id,
|
|
418
|
+
parentId,
|
|
419
|
+
timestamp: now,
|
|
420
|
+
customType,
|
|
421
|
+
content: [{ type: "text", text }],
|
|
422
|
+
display: false,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function messageEntry(id, parentId, message) {
|
|
427
|
+
return { type: "message", id, parentId, timestamp: now, message };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
async function waitUntil(predicate, timeoutMs = 2000) {
|
|
431
|
+
const deadline = Date.now() + timeoutMs;
|
|
432
|
+
while (!predicate()) {
|
|
433
|
+
if (Date.now() >= deadline) throw new Error("Timed out waiting for the agent session state");
|
|
434
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
435
|
+
}
|
|
436
|
+
}
|