pi-invisible-continue 0.2.3 → 0.3.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/continue.ts +34 -108
- package/package.json +1 -3
package/continue.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { Agent } from "@earendil-works/pi-agent-core";
|
|
3
2
|
import {
|
|
4
3
|
CONTINUE_COMMAND_DESCRIPTION,
|
|
5
4
|
getLastAssistantMessageText,
|
|
@@ -9,115 +8,45 @@ import {
|
|
|
9
8
|
* pi-invisible-continue — resume the agentic loop without the LLM seeing any new prompt.
|
|
10
9
|
*
|
|
11
10
|
* Strategy:
|
|
12
|
-
* -
|
|
13
|
-
* -
|
|
14
|
-
*
|
|
15
|
-
* -
|
|
16
|
-
*
|
|
11
|
+
* - Use pi.sendMessage() with triggerTurn: true to start a new agent turn
|
|
12
|
+
* - The message uses role: "custom" which is filtered by convertToLlm(),
|
|
13
|
+
* so the LLM receives the exact same message list it had before
|
|
14
|
+
* - This goes through AgentSession._runAgentPrompt, so auto-compaction,
|
|
15
|
+
* auto-retry, and other session lifecycle features work correctly
|
|
16
|
+
* - No prototype monkey-patching needed — no fragile Agent import, no
|
|
17
|
+
* module duplication issues
|
|
17
18
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* Why not agent.prompt([])?
|
|
20
|
+
* The previous approach monkey-patched Agent.prototype.subscribe to capture
|
|
21
|
+
* the Agent instance, then called agent.prompt([]) directly. This broke when
|
|
22
|
+
* the extension's import of Agent resolved to a different class (from the
|
|
23
|
+
* package's own node_modules/@earendil-works/pi-agent-core@0.75.4) than the
|
|
24
|
+
* one AgentSession uses (from pi's bundled 0.79.1). The subscribe patch was
|
|
25
|
+
* applied to the wrong prototype, so the agent was never captured.
|
|
26
|
+
*
|
|
27
|
+
* The new approach avoids this entirely by using the extension API's
|
|
28
|
+
* sendMessage() with triggerTurn: true, which is resolved at runtime by the
|
|
29
|
+
* AgentSession and doesn't depend on importing Agent at all.
|
|
22
30
|
*/
|
|
23
31
|
|
|
24
|
-
// Capture the live Agent instance when AgentSession subscribes to it.
|
|
25
|
-
// subscribe() is called during AgentSession construction — fires on both
|
|
26
|
-
// fresh sessions and session resumes, unlike prompt().
|
|
27
|
-
//
|
|
28
|
-
// Chain the previous patch (if pi-retry or pi-vcc already patched it)
|
|
29
|
-
// so all extensions can coexist.
|
|
30
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
-
let _agent: Agent | null = null;
|
|
32
|
-
const _origSubscribe = Agent.prototype.subscribe as (this: Agent, ...args: any[]) => any;
|
|
33
|
-
Agent.prototype.subscribe = function (this: Agent, ...args: any[]) {
|
|
34
|
-
_agent = this;
|
|
35
|
-
return _origSubscribe.apply(this, args);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
32
|
// Mutex: only one invisible continue may be in-flight at a time.
|
|
39
|
-
// Without this, concurrent /continue (or /continue during pi-retry/pi-vcc
|
|
40
|
-
// auto-continuation) race through waitForIdle() and both call prompt([]),
|
|
41
|
-
// producing "Agent is already processing".
|
|
42
33
|
let _continueInProgress = false;
|
|
43
34
|
|
|
44
|
-
// Timestamp of the last completed invisible continue.
|
|
45
|
-
// Used to avoid double continuation when continue() unblocks after
|
|
46
|
-
// triggerInvisibleContinue just ran.
|
|
47
|
-
let _lastInvisibleContinueTime = 0;
|
|
48
|
-
|
|
49
|
-
// Monkey-patch continue() so the session's built-in loop cooperates with
|
|
50
|
-
// our mutex AND can convert the "Cannot continue from assistant" error
|
|
51
|
-
// into a prompt([]) call when the agent was mid-task.
|
|
52
|
-
// Without this, the session's continue() would throw when the last message
|
|
53
|
-
// is an assistant (common after compaction), and the agent loop would die
|
|
54
|
-
// leaving mid-task work unfinished.
|
|
55
|
-
//
|
|
56
|
-
// When continue() throws "Cannot continue from message role: assistant":
|
|
57
|
-
// - stopReason "stop" → agent finished cleanly, don't continue
|
|
58
|
-
// - stopReason "aborted" → user cancelled, don't continue
|
|
59
|
-
// - stopReason "error" → pi-retry handles errors, don't race it
|
|
60
|
-
// - stopReason "toolUse" or "length" → mid-task, fall back to prompt([])
|
|
61
|
-
//
|
|
62
|
-
// Chains the previous patch (pi-retry, pi-vcc) so all mutexes are respected.
|
|
63
|
-
const _origContinue = Agent.prototype.continue;
|
|
64
|
-
Agent.prototype.continue = function (this: Agent) {
|
|
65
|
-
const self = this;
|
|
66
|
-
return (async (): Promise<void> => {
|
|
67
|
-
while (_continueInProgress) {
|
|
68
|
-
await new Promise(r => setTimeout(r, 10));
|
|
69
|
-
}
|
|
70
|
-
try {
|
|
71
|
-
await _origContinue.call(self);
|
|
72
|
-
} catch (e: any) {
|
|
73
|
-
const msg = e?.message ?? '';
|
|
74
|
-
if (msg.includes('Cannot continue from message role') ||
|
|
75
|
-
msg.includes('Cannot continue from an assistant message')) {
|
|
76
|
-
// Check stopReason — only continue if the agent was mid-task
|
|
77
|
-
const lastMsg = self.state.messages[self.state.messages.length - 1];
|
|
78
|
-
if (lastMsg?.role === 'assistant' &&
|
|
79
|
-
lastMsg.stopReason !== 'stop' &&
|
|
80
|
-
lastMsg.stopReason !== 'aborted' &&
|
|
81
|
-
lastMsg.stopReason !== 'error') {
|
|
82
|
-
// Agent was mid-task — fall back to prompt([])
|
|
83
|
-
// Guard: if an invisible continue just completed, don't double-run
|
|
84
|
-
if (!_continueInProgress && Date.now() - _lastInvisibleContinueTime > 500) {
|
|
85
|
-
_continueInProgress = true;
|
|
86
|
-
try {
|
|
87
|
-
await self.prompt([]);
|
|
88
|
-
} catch {
|
|
89
|
-
// Agent already processing or other transient error
|
|
90
|
-
} finally {
|
|
91
|
-
_continueInProgress = false;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
// For stop/aborted/error: return void, the session loop exits naturally
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
if (msg.includes('Agent is already processing')) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
throw e;
|
|
102
|
-
}
|
|
103
|
-
})();
|
|
104
|
-
};
|
|
105
|
-
|
|
106
35
|
export default function (pi: ExtensionAPI) {
|
|
107
36
|
pi.registerCommand("continue", {
|
|
108
37
|
description: CONTINUE_COMMAND_DESCRIPTION,
|
|
109
38
|
handler: async (args, ctx) => {
|
|
110
|
-
await runContinueCommand(ctx, args);
|
|
39
|
+
await runContinueCommand(pi, ctx, args);
|
|
111
40
|
},
|
|
112
41
|
});
|
|
113
42
|
|
|
114
43
|
pi.on("session_start", () => {
|
|
115
44
|
_continueInProgress = false;
|
|
116
|
-
_lastInvisibleContinueTime = 0;
|
|
117
45
|
});
|
|
118
46
|
}
|
|
119
47
|
|
|
120
48
|
async function runContinueCommand(
|
|
49
|
+
pi: ExtensionAPI,
|
|
121
50
|
ctx: ExtensionCommandContext,
|
|
122
51
|
args: string,
|
|
123
52
|
): Promise<void> {
|
|
@@ -128,7 +57,6 @@ async function runContinueCommand(
|
|
|
128
57
|
[
|
|
129
58
|
"pi-invisible-continue status:",
|
|
130
59
|
` Agent idle: ${idle ? "yes" : "no"}`,
|
|
131
|
-
` Captured agent: ${_agent ? "yes" : "no"}`,
|
|
132
60
|
` Last assistant: ${last ?? "(none)"}`.slice(0, 120),
|
|
133
61
|
].join("\n"),
|
|
134
62
|
"info",
|
|
@@ -148,20 +76,12 @@ async function runContinueCommand(
|
|
|
148
76
|
return;
|
|
149
77
|
}
|
|
150
78
|
|
|
151
|
-
if (!_agent) {
|
|
152
|
-
ctx.ui.notify(
|
|
153
|
-
"pi-invisible-continue: Agent instance not captured. Internal error?",
|
|
154
|
-
"warning",
|
|
155
|
-
);
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
79
|
if (!ctx.isIdle()) {
|
|
160
80
|
await ctx.waitForIdle();
|
|
161
81
|
}
|
|
162
82
|
|
|
163
83
|
// Guard: if pi-retry or pi-vcc already has an invisible continue in-flight,
|
|
164
|
-
// skip — their
|
|
84
|
+
// skip — their triggerTurn will resume the loop.
|
|
165
85
|
if (_continueInProgress) {
|
|
166
86
|
ctx.ui.notify(
|
|
167
87
|
"pi-invisible-continue: Another invisible continue is already in progress.",
|
|
@@ -172,14 +92,20 @@ async function runContinueCommand(
|
|
|
172
92
|
_continueInProgress = true;
|
|
173
93
|
|
|
174
94
|
try {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
95
|
+
// Send an invisible custom message that triggers a new agent turn.
|
|
96
|
+
// convertToLlm() filters role:"custom" messages, so the LLM sees
|
|
97
|
+
// the same context as before — no injected prompt.
|
|
98
|
+
pi.sendMessage(
|
|
99
|
+
{ customType: "pi-invisible-continue", content: "", display: false, details: undefined },
|
|
100
|
+
{ triggerTurn: true },
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// Wait for the triggered turn (and any post-run continuations like
|
|
104
|
+
// auto-compaction and auto-retry) to complete.
|
|
105
|
+
await ctx.waitForIdle();
|
|
106
|
+
} catch {
|
|
107
|
+
// Agent is already processing — something else is driving.
|
|
181
108
|
} finally {
|
|
182
109
|
_continueInProgress = false;
|
|
183
|
-
_lastInvisibleContinueTime = Date.now();
|
|
184
110
|
}
|
|
185
111
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-invisible-continue",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Invisible session continuation for pi — resume the agentic loop without sending ANY prompt the LLM can see",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Tom X Nguyen",
|
|
@@ -38,8 +38,6 @@
|
|
|
38
38
|
"lint:dead": "knip --no-gitignore"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@earendil-works/pi-agent-core": "0.75.4",
|
|
42
|
-
"@earendil-works/pi-ai": "0.75.4",
|
|
43
41
|
"@earendil-works/pi-coding-agent": "0.75.4",
|
|
44
42
|
"@types/node": "25.9.1",
|
|
45
43
|
"@vitest/coverage-v8": "4.1.7",
|