@eduardo191labs/opencode-free-pi 0.3.11 → 0.3.12
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/README.md +4 -1
- package/dist/session/events.js +18 -1
- package/dist/session/hooks.js +14 -1
- package/dist/session/wire.d.ts +9 -0
- package/dist/session/wire.js +19 -0
- package/dist/state.d.ts +1 -0
- package/dist/state.js +1 -0
- package/package.json +1 -1
- package/src/session/events.ts +21 -1
- package/src/session/hooks.ts +22 -1
- package/src/session/wire.ts +21 -0
- package/src/state.ts +2 -0
package/README.md
CHANGED
|
@@ -160,7 +160,10 @@ time») es la excepción: como OpenCode reutiliza la petición ya preparada al
|
|
|
160
160
|
reintentar, el plugin **repite la petición** desde el hook de respuesta con un
|
|
161
161
|
`x-session-id` nuevo y devuelve el resultado bueno a OpenCode; el turno continúa
|
|
162
162
|
sin fallar ni mostrar reintentos. Además libera el id anterior, y si el replay no
|
|
163
|
-
puede completarse el hook de reintento rota el id para los siguientes pasos.
|
|
163
|
+
puede completarse el hook de reintento rota el id para los siguientes pasos. Un
|
|
164
|
+
turno que se aborta o falla a mitad de stream marca su `x-session-id` como sucio:
|
|
165
|
+
la siguiente petición arranca directamente con un id nuevo y libera el anterior,
|
|
166
|
+
de modo que la sesión continúa sin interrupciones y sin pasar por un reintento. Si
|
|
164
167
|
el catálogo asigna el experimento `compact100`, el plugin baja la ventana a 117
|
|
165
168
|
647 tokens para que OpenCode compacte al llegar a 100k, que es el mismo umbral
|
|
166
169
|
que consigue `reserveTokens: 100000` en el CLI.
|
package/dist/session/events.js
CHANGED
|
@@ -6,7 +6,15 @@ import { handleFreePiError } from "../errors/handle.js";
|
|
|
6
6
|
import { forgetCacheMiss, observeStepUsage } from "./cache.js";
|
|
7
7
|
import { forgetToolStats } from "./tools.js";
|
|
8
8
|
import { beginExecution, cancelAllFinalizeTimers, endExecution, finalizeFreePiSession, forgetFreePiSession, } from "./track.js";
|
|
9
|
+
import { markWireSessionDirty, } from "./wire.js";
|
|
9
10
|
import { recordUsage } from "./usage.js";
|
|
11
|
+
function isAbortLike(error) {
|
|
12
|
+
if (!error)
|
|
13
|
+
return false;
|
|
14
|
+
if (error.type === "aborted")
|
|
15
|
+
return true;
|
|
16
|
+
return /aborted|cancelled|canceled|interrupted|AbortError/i.test(`${error.message ?? ""}`);
|
|
17
|
+
}
|
|
10
18
|
const EVENT_TYPES = [
|
|
11
19
|
"credential.updated",
|
|
12
20
|
"credential.switched",
|
|
@@ -49,6 +57,11 @@ async function handleEvent(ctx, deps, event) {
|
|
|
49
57
|
}
|
|
50
58
|
case "session.execution.failed": {
|
|
51
59
|
const sessionID = event.data?.sessionID;
|
|
60
|
+
// A request that dies mid-stream can leave its slot locked on the server.
|
|
61
|
+
// The next turn will use a fresh wire id instead of waiting on a retry.
|
|
62
|
+
if (isAbortLike(event.data?.error)) {
|
|
63
|
+
markWireSessionDirty(deps, sessionID);
|
|
64
|
+
}
|
|
52
65
|
await handleFreePiError(deps, event.data?.error, {
|
|
53
66
|
allowRaw: typeof sessionID === "string" &&
|
|
54
67
|
deps.state.freePiSessions.has(sessionID),
|
|
@@ -56,8 +69,12 @@ async function handleEvent(ctx, deps, event) {
|
|
|
56
69
|
endExecution(deps, sessionID);
|
|
57
70
|
return;
|
|
58
71
|
}
|
|
59
|
-
case "session.execution.succeeded":
|
|
72
|
+
case "session.execution.succeeded": {
|
|
73
|
+
endExecution(deps, event.data?.sessionID);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
60
76
|
case "session.execution.interrupted": {
|
|
77
|
+
markWireSessionDirty(deps, event.data?.sessionID);
|
|
61
78
|
endExecution(deps, event.data?.sessionID);
|
|
62
79
|
return;
|
|
63
80
|
}
|
package/dist/session/hooks.js
CHANGED
|
@@ -11,7 +11,7 @@ import { watchFreePiNotice } from "./notices.js";
|
|
|
11
11
|
import { rememberReplayBody, replayConcurrentResponse, } from "./replay.js";
|
|
12
12
|
import { localTitle } from "./title.js";
|
|
13
13
|
import { rememberFreePiSession } from "./track.js";
|
|
14
|
-
import { rotateWireSessionID, wireSessionID } from "./wire.js";
|
|
14
|
+
import { markWireSessionDirty, rotateWireSessionID, takeWireSessionDirty, wireSessionID, } from "./wire.js";
|
|
15
15
|
import { recordCompaction, recordToolEnd, recordToolStart, toolHeaders, } from "./tools.js";
|
|
16
16
|
import { rememberTrackedModel } from "./usage.js";
|
|
17
17
|
const PENDING_LIMIT = 400;
|
|
@@ -78,6 +78,16 @@ async function applyModelRequest(deps, event) {
|
|
|
78
78
|
rememberTrackedModel(deps, event.sessionID, providerID, event.model?.id ?? "");
|
|
79
79
|
const agent = event.agent ?? (event.kind === "title" ? "title" : undefined);
|
|
80
80
|
event.headers ??= {};
|
|
81
|
+
// A previous turn that was aborted, interrupted or failed mid-stream may have
|
|
82
|
+
// left its per-session request slot locked on the server. Rotating before the
|
|
83
|
+
// request goes out keeps this turn running instead of surfacing a 429 and a
|
|
84
|
+
// retry: the session simply continues under a fresh wire id.
|
|
85
|
+
if (takeWireSessionDirty(deps, event.sessionID)) {
|
|
86
|
+
const previous = wireSessionID(deps, event.sessionID);
|
|
87
|
+
const next = rotateWireSessionID(deps, event.sessionID);
|
|
88
|
+
void closeFreePiSession(deps, previous).catch(() => undefined);
|
|
89
|
+
deps.log.info("free-pi session ended mid-stream; continuing with a fresh session id", { sessionID: event.sessionID, from: previous, to: next });
|
|
90
|
+
}
|
|
81
91
|
const token = await resolveToken(deps);
|
|
82
92
|
if (token)
|
|
83
93
|
event.headers.authorization = `Bearer ${token}`;
|
|
@@ -197,6 +207,9 @@ function applyRetryPolicy(deps, event) {
|
|
|
197
207
|
return;
|
|
198
208
|
}
|
|
199
209
|
if (isTerminalFailure(event.error)) {
|
|
210
|
+
// Remember the broken stream too, so the next turn rotates its wire id
|
|
211
|
+
// instead of running into the stale per-session lock.
|
|
212
|
+
markWireSessionDirty(deps, event.sessionID);
|
|
200
213
|
event.decision = { retry: false };
|
|
201
214
|
return;
|
|
202
215
|
}
|
package/dist/session/wire.d.ts
CHANGED
|
@@ -9,6 +9,15 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import type { Deps } from "../deps.js";
|
|
11
11
|
export declare function wireSessionID(deps: Deps, sessionID: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Records that a turn ended without a clean stream (aborted, interrupted or
|
|
14
|
+
* failed mid-flight). The server can keep the per-session request slot locked
|
|
15
|
+
* in that case, so the next turn must not reuse the id: it would get a
|
|
16
|
+
* `429 concurrent` and OpenCode would show a retry. The next `model.request`
|
|
17
|
+
* rotates to a fresh id instead and the turn continues normally.
|
|
18
|
+
*/
|
|
19
|
+
export declare function markWireSessionDirty(deps: Deps, sessionID: string): void;
|
|
20
|
+
export declare function takeWireSessionDirty(deps: Deps, sessionID: string): boolean;
|
|
12
21
|
/** Points the OpenCode session at a fresh wire id and returns it. */
|
|
13
22
|
export declare function rotateWireSessionID(deps: Deps, sessionID: string): string;
|
|
14
23
|
export declare function forgetWireSessionID(deps: Deps, sessionID: string): void;
|
package/dist/session/wire.js
CHANGED
|
@@ -7,9 +7,27 @@
|
|
|
7
7
|
* not clear it. Rotating the wire id is the recovery, so the plugin keeps the
|
|
8
8
|
* mapping until the OpenCode session is gone.
|
|
9
9
|
*/
|
|
10
|
+
import { trimSet } from "../utils/async.js";
|
|
11
|
+
const DIRTY_LIMIT = 256;
|
|
10
12
|
export function wireSessionID(deps, sessionID) {
|
|
11
13
|
return deps.state.wireSessions.get(sessionID) ?? sessionID;
|
|
12
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Records that a turn ended without a clean stream (aborted, interrupted or
|
|
17
|
+
* failed mid-flight). The server can keep the per-session request slot locked
|
|
18
|
+
* in that case, so the next turn must not reuse the id: it would get a
|
|
19
|
+
* `429 concurrent` and OpenCode would show a retry. The next `model.request`
|
|
20
|
+
* rotates to a fresh id instead and the turn continues normally.
|
|
21
|
+
*/
|
|
22
|
+
export function markWireSessionDirty(deps, sessionID) {
|
|
23
|
+
if (!sessionID)
|
|
24
|
+
return;
|
|
25
|
+
deps.state.dirtySessions.add(sessionID);
|
|
26
|
+
trimSet(deps.state.dirtySessions, DIRTY_LIMIT);
|
|
27
|
+
}
|
|
28
|
+
export function takeWireSessionDirty(deps, sessionID) {
|
|
29
|
+
return deps.state.dirtySessions.delete(sessionID);
|
|
30
|
+
}
|
|
13
31
|
/** Points the OpenCode session at a fresh wire id and returns it. */
|
|
14
32
|
export function rotateWireSessionID(deps, sessionID) {
|
|
15
33
|
const suffix = Math.random().toString(36).slice(2, 10);
|
|
@@ -19,4 +37,5 @@ export function rotateWireSessionID(deps, sessionID) {
|
|
|
19
37
|
}
|
|
20
38
|
export function forgetWireSessionID(deps, sessionID) {
|
|
21
39
|
deps.state.wireSessions.delete(sessionID);
|
|
40
|
+
deps.state.dirtySessions.delete(sessionID);
|
|
22
41
|
}
|
package/dist/state.d.ts
CHANGED
|
@@ -35,6 +35,7 @@ export type RuntimeState = {
|
|
|
35
35
|
freePiSessions: Set<string>;
|
|
36
36
|
wireSessions: Map<string, string>;
|
|
37
37
|
replayBodies: Map<string, string>;
|
|
38
|
+
dirtySessions: Set<string>;
|
|
38
39
|
finalizedSessions: Set<string>;
|
|
39
40
|
activeExecutions: Set<string>;
|
|
40
41
|
sessionActivity: Map<string, number>;
|
package/dist/state.js
CHANGED
package/package.json
CHANGED
package/src/session/events.ts
CHANGED
|
@@ -15,8 +15,19 @@ import {
|
|
|
15
15
|
finalizeFreePiSession,
|
|
16
16
|
forgetFreePiSession,
|
|
17
17
|
} from "./track.js";
|
|
18
|
+
import {
|
|
19
|
+
markWireSessionDirty,
|
|
20
|
+
} from "./wire.js";
|
|
18
21
|
import { recordUsage } from "./usage.js";
|
|
19
22
|
|
|
23
|
+
function isAbortLike(error: any): boolean {
|
|
24
|
+
if (!error) return false;
|
|
25
|
+
if (error.type === "aborted") return true;
|
|
26
|
+
return /aborted|cancelled|canceled|interrupted|AbortError/i.test(
|
|
27
|
+
`${error.message ?? ""}`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
20
31
|
const EVENT_TYPES = [
|
|
21
32
|
"credential.updated",
|
|
22
33
|
"credential.switched",
|
|
@@ -63,6 +74,11 @@ async function handleEvent(
|
|
|
63
74
|
}
|
|
64
75
|
case "session.execution.failed": {
|
|
65
76
|
const sessionID = event.data?.sessionID;
|
|
77
|
+
// A request that dies mid-stream can leave its slot locked on the server.
|
|
78
|
+
// The next turn will use a fresh wire id instead of waiting on a retry.
|
|
79
|
+
if (isAbortLike(event.data?.error)) {
|
|
80
|
+
markWireSessionDirty(deps, sessionID);
|
|
81
|
+
}
|
|
66
82
|
await handleFreePiError(deps, event.data?.error, {
|
|
67
83
|
allowRaw:
|
|
68
84
|
typeof sessionID === "string" &&
|
|
@@ -71,8 +87,12 @@ async function handleEvent(
|
|
|
71
87
|
endExecution(deps, sessionID);
|
|
72
88
|
return;
|
|
73
89
|
}
|
|
74
|
-
case "session.execution.succeeded":
|
|
90
|
+
case "session.execution.succeeded": {
|
|
91
|
+
endExecution(deps, event.data?.sessionID);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
75
94
|
case "session.execution.interrupted": {
|
|
95
|
+
markWireSessionDirty(deps, event.data?.sessionID);
|
|
76
96
|
endExecution(deps, event.data?.sessionID);
|
|
77
97
|
return;
|
|
78
98
|
}
|
package/src/session/hooks.ts
CHANGED
|
@@ -34,7 +34,12 @@ import {
|
|
|
34
34
|
} from "./replay.js";
|
|
35
35
|
import { localTitle } from "./title.js";
|
|
36
36
|
import { rememberFreePiSession } from "./track.js";
|
|
37
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
markWireSessionDirty,
|
|
39
|
+
rotateWireSessionID,
|
|
40
|
+
takeWireSessionDirty,
|
|
41
|
+
wireSessionID,
|
|
42
|
+
} from "./wire.js";
|
|
38
43
|
import {
|
|
39
44
|
recordCompaction,
|
|
40
45
|
recordToolEnd,
|
|
@@ -145,6 +150,19 @@ async function applyModelRequest(deps: Deps, event: any): Promise<void> {
|
|
|
145
150
|
|
|
146
151
|
const agent = event.agent ?? (event.kind === "title" ? "title" : undefined);
|
|
147
152
|
event.headers ??= {};
|
|
153
|
+
// A previous turn that was aborted, interrupted or failed mid-stream may have
|
|
154
|
+
// left its per-session request slot locked on the server. Rotating before the
|
|
155
|
+
// request goes out keeps this turn running instead of surfacing a 429 and a
|
|
156
|
+
// retry: the session simply continues under a fresh wire id.
|
|
157
|
+
if (takeWireSessionDirty(deps, event.sessionID)) {
|
|
158
|
+
const previous = wireSessionID(deps, event.sessionID);
|
|
159
|
+
const next = rotateWireSessionID(deps, event.sessionID);
|
|
160
|
+
void closeFreePiSession(deps, previous).catch(() => undefined);
|
|
161
|
+
deps.log.info(
|
|
162
|
+
"free-pi session ended mid-stream; continuing with a fresh session id",
|
|
163
|
+
{ sessionID: event.sessionID, from: previous, to: next },
|
|
164
|
+
);
|
|
165
|
+
}
|
|
148
166
|
const token = await resolveToken(deps);
|
|
149
167
|
if (token) event.headers.authorization = `Bearer ${token}`;
|
|
150
168
|
event.headers["x-session-id"] = wireSessionID(deps, event.sessionID);
|
|
@@ -288,6 +306,9 @@ function applyRetryPolicy(deps: Deps, event: any): void {
|
|
|
288
306
|
return;
|
|
289
307
|
}
|
|
290
308
|
if (isTerminalFailure(event.error)) {
|
|
309
|
+
// Remember the broken stream too, so the next turn rotates its wire id
|
|
310
|
+
// instead of running into the stale per-session lock.
|
|
311
|
+
markWireSessionDirty(deps, event.sessionID);
|
|
291
312
|
event.decision = { retry: false };
|
|
292
313
|
return;
|
|
293
314
|
}
|
package/src/session/wire.ts
CHANGED
|
@@ -9,11 +9,31 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import type { Deps } from "../deps.js";
|
|
12
|
+
import { trimSet } from "../utils/async.js";
|
|
13
|
+
|
|
14
|
+
const DIRTY_LIMIT = 256;
|
|
12
15
|
|
|
13
16
|
export function wireSessionID(deps: Deps, sessionID: string): string {
|
|
14
17
|
return deps.state.wireSessions.get(sessionID) ?? sessionID;
|
|
15
18
|
}
|
|
16
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Records that a turn ended without a clean stream (aborted, interrupted or
|
|
22
|
+
* failed mid-flight). The server can keep the per-session request slot locked
|
|
23
|
+
* in that case, so the next turn must not reuse the id: it would get a
|
|
24
|
+
* `429 concurrent` and OpenCode would show a retry. The next `model.request`
|
|
25
|
+
* rotates to a fresh id instead and the turn continues normally.
|
|
26
|
+
*/
|
|
27
|
+
export function markWireSessionDirty(deps: Deps, sessionID: string): void {
|
|
28
|
+
if (!sessionID) return;
|
|
29
|
+
deps.state.dirtySessions.add(sessionID);
|
|
30
|
+
trimSet(deps.state.dirtySessions, DIRTY_LIMIT);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function takeWireSessionDirty(deps: Deps, sessionID: string): boolean {
|
|
34
|
+
return deps.state.dirtySessions.delete(sessionID);
|
|
35
|
+
}
|
|
36
|
+
|
|
17
37
|
/** Points the OpenCode session at a fresh wire id and returns it. */
|
|
18
38
|
export function rotateWireSessionID(deps: Deps, sessionID: string): string {
|
|
19
39
|
const suffix = Math.random().toString(36).slice(2, 10);
|
|
@@ -24,4 +44,5 @@ export function rotateWireSessionID(deps: Deps, sessionID: string): string {
|
|
|
24
44
|
|
|
25
45
|
export function forgetWireSessionID(deps: Deps, sessionID: string): void {
|
|
26
46
|
deps.state.wireSessions.delete(sessionID);
|
|
47
|
+
deps.state.dirtySessions.delete(sessionID);
|
|
27
48
|
}
|
package/src/state.ts
CHANGED
|
@@ -52,6 +52,7 @@ export type RuntimeState = {
|
|
|
52
52
|
freePiSessions: Set<string>;
|
|
53
53
|
wireSessions: Map<string, string>;
|
|
54
54
|
replayBodies: Map<string, string>;
|
|
55
|
+
dirtySessions: Set<string>;
|
|
55
56
|
finalizedSessions: Set<string>;
|
|
56
57
|
activeExecutions: Set<string>;
|
|
57
58
|
sessionActivity: Map<string, number>;
|
|
@@ -81,6 +82,7 @@ export function createRuntimeState(): RuntimeState {
|
|
|
81
82
|
freePiSessions: new Set(),
|
|
82
83
|
wireSessions: new Map(),
|
|
83
84
|
replayBodies: new Map(),
|
|
85
|
+
dirtySessions: new Set(),
|
|
84
86
|
finalizedSessions: new Set(),
|
|
85
87
|
activeExecutions: new Set(),
|
|
86
88
|
sessionActivity: new Map(),
|