@gajae-code/coding-agent 0.22.2 → 0.22.3
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/CHANGELOG.md +115 -0
- package/dist/types/cli/append-system-prompt.d.ts +7 -0
- package/dist/types/cli/doctor/session-owners.d.ts +8 -2
- package/dist/types/cli/public-command-errors.d.ts +1 -0
- package/dist/types/cli/session-picker.d.ts +16 -3
- package/dist/types/cli/update-cli.d.ts +0 -2
- package/dist/types/cli-ordinary.d.ts +1 -1
- package/dist/types/commands/sdk.d.ts +2 -0
- package/dist/types/config/machine-identity.d.ts +11 -0
- package/dist/types/config/model-registry.d.ts +9 -0
- package/dist/types/config/settings-schema.d.ts +84 -4
- package/dist/types/lsp/client.d.ts +9 -0
- package/dist/types/lsp/types.d.ts +2 -0
- package/dist/types/main.d.ts +40 -3
- package/dist/types/modes/components/session-held-warning.d.ts +27 -0
- package/dist/types/modes/components/session-selector.d.ts +32 -1
- package/dist/types/modes/controllers/selector-controller.d.ts +8 -1
- package/dist/types/modes/interactive-mode.d.ts +12 -1
- package/dist/types/sdk/broker/broker.d.ts +3 -0
- package/dist/types/sdk/broker/lifecycle-ledger.d.ts +5 -2
- package/dist/types/sdk/broker/lifecycle.d.ts +2 -0
- package/dist/types/sdk/broker/session-index.d.ts +11 -0
- package/dist/types/sdk/bus/index.d.ts +1 -1
- package/dist/types/sdk/host/control/dispatch.d.ts +3 -0
- package/dist/types/sdk/host/registration-test-hold.d.ts +7 -0
- package/dist/types/sdk/index.d.ts +1 -0
- package/dist/types/sdk/lifecycle/service.d.ts +3 -0
- package/dist/types/sdk/session-api-key.d.ts +11 -4
- package/dist/types/sdk/session-readonly/format.d.ts +7 -0
- package/dist/types/sdk/session.d.ts +11 -0
- package/dist/types/sdk/startup-capability.d.ts +11 -2
- package/dist/types/session/agent-session.d.ts +1 -2
- package/dist/types/session/internal/managed-session-storage.d.ts +1 -1
- package/dist/types/session/internal/session-claim-set.d.ts +168 -13
- package/dist/types/session/session-handover.d.ts +62 -0
- package/dist/types/session/session-manager.d.ts +75 -15
- package/dist/types/session/session-owner-holder.d.ts +77 -0
- package/dist/types/session/session-ownership-mode.d.ts +45 -2
- package/dist/types/session/session-ownership.d.ts +25 -29
- package/dist/types/session/yield-queue.d.ts +13 -0
- package/dist/types/tools/bash.d.ts +7 -0
- package/dist/types/tools/browser/deferred-profile-cleanup.d.ts +3 -0
- package/dist/types/tools/browser/launch.d.ts +24 -1
- package/dist/types/tools/index.d.ts +6 -0
- package/dist/types/tools/output-meta.d.ts +17 -11
- package/dist/types/tools/tool-result.d.ts +1 -0
- package/package.json +9 -7
- package/scripts/compile-args.ts +6 -3
- package/scripts/run-acp-conformance.ts +36 -10
- package/scripts/verify-gjc-sdk-canonicalization.ts +2 -0
- package/src/cli/append-system-prompt.ts +34 -0
- package/src/cli/auth-broker-cli.ts +5 -1
- package/src/cli/doctor/session-owners.ts +8 -2
- package/src/cli/public-command-errors.ts +4 -0
- package/src/cli/session-picker.ts +57 -7
- package/src/cli/update-cli.ts +0 -22
- package/src/cli-ordinary.ts +2 -17
- package/src/commands/sdk.ts +37 -15
- package/src/config/machine-identity.ts +31 -2
- package/src/config/model-registry.ts +40 -6
- package/src/config/settings-schema.ts +50 -6
- package/src/config/settings.ts +65 -3
- package/src/gjc-runtime/launch-tmux.ts +4 -1
- package/src/internal-urls/docs-index.generated.ts +12 -12
- package/src/lsp/client.ts +60 -6
- package/src/lsp/index.ts +2 -6
- package/src/lsp/types.ts +2 -0
- package/src/main.ts +364 -93
- package/src/modes/DESIGN.md +36 -0
- package/src/modes/acp/acp-agent.ts +215 -31
- package/src/modes/components/session-held-warning.ts +62 -0
- package/src/modes/components/session-selector.ts +234 -11
- package/src/modes/controllers/goal-mode-controller.ts +14 -2
- package/src/modes/controllers/selector-controller.ts +192 -3
- package/src/modes/interactive-mode.ts +40 -2
- package/src/prompts/tools/bash.md +1 -1
- package/src/prompts/tools/monitor.md +1 -1
- package/src/runtime-mcp/manager.ts +5 -1
- package/src/sdk/acp/adapter.ts +2 -0
- package/src/sdk/broker/broker.ts +29 -2
- package/src/sdk/broker/lifecycle-ledger.ts +9 -7
- package/src/sdk/broker/lifecycle.ts +280 -39
- package/src/sdk/broker/session-index.ts +24 -0
- package/src/sdk/bus/index.ts +1 -0
- package/src/sdk/bus/lifecycle-commands.ts +4 -0
- package/src/sdk/cli/session-cli.ts +8 -0
- package/src/sdk/host/control/dispatch.ts +18 -0
- package/src/sdk/host/host.ts +5 -0
- package/src/sdk/host/registration-test-hold.ts +51 -0
- package/src/sdk/host/session-runtime.test.ts +42 -0
- package/src/sdk/index.ts +10 -0
- package/src/sdk/lifecycle/service.ts +33 -10
- package/src/sdk/prompt-failure.ts +4 -0
- package/src/sdk/protocol/operation-registry.ts +7 -1
- package/src/sdk/session-api-key.ts +31 -16
- package/src/sdk/session-readonly/format.ts +10 -0
- package/src/sdk/session-readonly/scope.ts +2 -1
- package/src/sdk/session.ts +60 -7
- package/src/sdk/startup-capability.ts +27 -2
- package/src/session/agent-session.ts +411 -58
- package/src/session/internal/managed-session-storage.ts +16 -2
- package/src/session/internal/session-claim-set.ts +527 -49
- package/src/session/session-handover.ts +283 -0
- package/src/session/session-manager.ts +907 -148
- package/src/session/session-owner-holder.ts +177 -0
- package/src/session/session-ownership-mode.ts +84 -12
- package/src/session/session-ownership.ts +115 -89
- package/src/session/yield-queue.ts +43 -0
- package/src/session-import/provider-service.ts +8 -0
- package/src/setup/provider-onboarding.ts +5 -5
- package/src/task/executor.ts +2 -0
- package/src/tools/bash.ts +10 -0
- package/src/tools/browser/deferred-profile-cleanup.ts +20 -0
- package/src/tools/browser/launch.ts +133 -14
- package/src/tools/browser/registry.ts +72 -19
- package/src/tools/index.ts +10 -1
- package/src/tools/monitor.ts +267 -29
- package/src/tools/output-meta.ts +61 -28
- package/src/tools/tool-catalog.generated.ts +2 -2
- package/src/tools/tool-result.ts +5 -0
- package/dist/types/cli/macos-community-app.d.ts +0 -41
- package/src/cli/macos-community-app.ts +0 -1449
|
@@ -2,24 +2,241 @@
|
|
|
2
2
|
* The per-manager claim set (design §2.5): the session claims one SessionManager holds, keyed by
|
|
3
3
|
* session id, with every release point of the design's table.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* releases a claim only when its id is no longer the
|
|
7
|
-
* so a predecessor stays claimed until its rotation
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* Factories and transitions claim through {@link SessionClaimSet.claim}, the one place a manager
|
|
6
|
+
* takes a claim; the detector never claims. It releases a claim only when its id is no longer the
|
|
7
|
+
* manager's current id and no transition is open, so a predecessor stays claimed until its rotation
|
|
8
|
+
* commits, and a rollback that restores the predecessor releases the target instead. In `off`, and
|
|
9
|
+
* for an exempt manager, nothing is ever claimed and every operation is a no-op.
|
|
10
10
|
*/
|
|
11
11
|
import { logger } from "@gajae-code/utils";
|
|
12
12
|
import type { SessionManager } from "../session-manager";
|
|
13
|
-
import { countSessionOwnershipEvent, type SessionOwnerClaim } from "../session-ownership";
|
|
14
13
|
import {
|
|
14
|
+
claimSessionOwner,
|
|
15
|
+
countSessionOwnershipEvent,
|
|
16
|
+
probeSessionOwners,
|
|
17
|
+
releaseSessionAdmissions,
|
|
18
|
+
SessionHeldError,
|
|
19
|
+
type SessionOwnerClaim,
|
|
20
|
+
type SessionOwnerHolder,
|
|
21
|
+
type SessionOwnerProbe,
|
|
22
|
+
type SessionOwnerSurface,
|
|
23
|
+
SessionOwnershipError,
|
|
24
|
+
sessionOwnerHolderPhrase,
|
|
25
|
+
sessionOwnerSurface,
|
|
26
|
+
} from "../session-ownership";
|
|
27
|
+
import {
|
|
28
|
+
liveSessionOwnershipModeFor,
|
|
15
29
|
type SessionOwnershipMode,
|
|
16
30
|
sessionOwnershipMode,
|
|
17
31
|
sessionOwnershipTestHookSlot,
|
|
18
32
|
} from "../session-ownership-mode";
|
|
19
33
|
|
|
34
|
+
/**
|
|
35
|
+
* A factory's ownership decision (design §2.4): claim under `surface`, or never claim (`exempt`).
|
|
36
|
+
* `mode` is the mode as the factory read it; a factory reads it once and hands it on.
|
|
37
|
+
*/
|
|
38
|
+
export type SessionOwnershipDecision =
|
|
39
|
+
| "exempt"
|
|
40
|
+
| { readonly surface: SessionOwnerSurface; readonly mode?: SessionOwnershipMode };
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The decision a persisting manager is constructed with. Required by the constructor, so a factory
|
|
44
|
+
* cannot build a manager without deciding.
|
|
45
|
+
*/
|
|
46
|
+
export type ResolvedSessionOwnership =
|
|
47
|
+
| "exempt"
|
|
48
|
+
| { readonly surface: SessionOwnerSurface; readonly mode: SessionOwnershipMode };
|
|
49
|
+
|
|
50
|
+
/** Read the mode once, for one factory; without a decision, claim as this process's surface. */
|
|
51
|
+
export function resolveSessionOwnership(
|
|
52
|
+
decision: SessionOwnershipDecision = { surface: sessionOwnerSurface() },
|
|
53
|
+
): ResolvedSessionOwnership {
|
|
54
|
+
if (decision === "exempt") return "exempt";
|
|
55
|
+
return { surface: decision.surface, mode: decision.mode ?? sessionOwnershipMode() };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Whether `ownership` takes claims at all. Callers check this before awaiting a claim, so `off` and
|
|
60
|
+
* exempt managers add no await and keep their ordering exactly as before ownership existed.
|
|
61
|
+
*/
|
|
62
|
+
export function claimsUnder(ownership: ResolvedSessionOwnership): boolean {
|
|
63
|
+
return ownership !== "exempt" && ownership.mode !== "off";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The outcome of one claim attempt. `none`: nothing was attempted (off, exempt) or the claim failed
|
|
68
|
+
* open, with `skipped` naming why (`unavailable` is permanent, `timed_out` is that one call);
|
|
69
|
+
* `failed`: a failure enforce refuses (counted `claim_failed`), with that failure; `held`: another
|
|
70
|
+
* process holds the session (counted `claim_conflict`), with its holder if known.
|
|
71
|
+
*/
|
|
72
|
+
type SessionClaimRefusable =
|
|
73
|
+
| { readonly status: "held"; readonly holder?: SessionOwnerHolder }
|
|
74
|
+
| { readonly status: "failed"; readonly error: SessionOwnershipError };
|
|
75
|
+
type SessionClaimNone = { readonly status: "none"; readonly skipped?: "unavailable" | "timed_out" };
|
|
76
|
+
export type SessionClaimOutcome = { readonly status: "claimed" } | SessionClaimNone | SessionClaimRefusable;
|
|
77
|
+
export type SessionClaimAttempt =
|
|
78
|
+
| { readonly status: "claimed"; readonly claim: SessionOwnerClaim }
|
|
79
|
+
| SessionClaimNone
|
|
80
|
+
| SessionClaimRefusable;
|
|
81
|
+
export type SessionClaimStatus = SessionClaimOutcome["status"];
|
|
82
|
+
|
|
83
|
+
/** An attempt as the claim set records it: without the claim handle. */
|
|
84
|
+
function outcomeOf(attempt: SessionClaimAttempt): SessionClaimOutcome {
|
|
85
|
+
return attempt.status === "claimed" ? { status: "claimed" } : attempt;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* One claim attempt under `ownership`, never throwing (design §13 M2). A held session (counted
|
|
90
|
+
* `claim_conflict`) and an owners-directory failure (counted `claim_failed`) are logged and returned;
|
|
91
|
+
* {@link claimRefusal} decides whether enforce refuses them. `previous`, the outcome a re-attempt
|
|
92
|
+
* repeats, keeps the same outcome again from being counted or logged again. `admission` marks an
|
|
93
|
+
* open's or a switch's cover, which the opener's own claim releases when it lands.
|
|
94
|
+
*/
|
|
95
|
+
export async function attemptSessionClaim(
|
|
96
|
+
ownership: ResolvedSessionOwnership,
|
|
97
|
+
sessionId: string,
|
|
98
|
+
previous?: SessionClaimOutcome,
|
|
99
|
+
options?: { readonly admission?: boolean },
|
|
100
|
+
): Promise<SessionClaimAttempt> {
|
|
101
|
+
if (ownership === "exempt" || ownership.mode === "off") return { status: "none" };
|
|
102
|
+
const repeat =
|
|
103
|
+
previous?.status === "held"
|
|
104
|
+
? "held"
|
|
105
|
+
: previous?.status === "none" && previous.skipped === "timed_out"
|
|
106
|
+
? "timed_out"
|
|
107
|
+
: undefined;
|
|
108
|
+
try {
|
|
109
|
+
const result = await claimSessionOwner(sessionId, {
|
|
110
|
+
surface: ownership.surface,
|
|
111
|
+
repeat,
|
|
112
|
+
admission: options?.admission,
|
|
113
|
+
});
|
|
114
|
+
if (result.status === "claimed") return result;
|
|
115
|
+
if (result.status === "held") {
|
|
116
|
+
if (repeat !== "held")
|
|
117
|
+
logger.warn("Session is open for writing in another process", {
|
|
118
|
+
mode: ownership.mode,
|
|
119
|
+
holder: sessionOwnerHolderPhrase(result.holder),
|
|
120
|
+
});
|
|
121
|
+
return { status: "held", holder: result.holder };
|
|
122
|
+
}
|
|
123
|
+
return { status: "none", skipped: result.reason };
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (previous?.status !== "failed") {
|
|
126
|
+
countSessionOwnershipEvent("claim_failed");
|
|
127
|
+
logger.warn("Session claim failed", { mode: ownership.mode, error: String(error) });
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
status: "failed",
|
|
131
|
+
error:
|
|
132
|
+
error instanceof SessionOwnershipError
|
|
133
|
+
? error
|
|
134
|
+
: new SessionOwnershipError("claim_failed", { cause: error }),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The one refusal decision (design §3.3, 5a): the error enforce raises for a claim outcome, or
|
|
141
|
+
* `undefined` to go on without a claim. Only `held` (a {@link SessionHeldError} naming the holder)
|
|
142
|
+
* and `failed` (the owners-directory failure, fail-closed like the append lock) refuse, and only when
|
|
143
|
+
* both the mode the factory read and the live mode are `enforce`: the live read only ever lowers it.
|
|
144
|
+
*/
|
|
145
|
+
export function claimRefusal(
|
|
146
|
+
ownership: ResolvedSessionOwnership,
|
|
147
|
+
sessionId: string,
|
|
148
|
+
outcome: SessionClaimOutcome | SessionClaimAttempt | undefined,
|
|
149
|
+
): SessionHeldError | SessionOwnershipError | undefined {
|
|
150
|
+
if (ownership === "exempt" || ownership.mode !== "enforce") return undefined;
|
|
151
|
+
if (outcome?.status !== "held" && outcome?.status !== "failed") return undefined;
|
|
152
|
+
if (liveSessionOwnershipModeFor(ownership.mode) !== "enforce") return undefined;
|
|
153
|
+
return outcome.status === "held" ? new SessionHeldError(sessionId, outcome.holder) : outcome.error;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Run `write`, a short write to a session that is not the caller's own (a picker star or delete,
|
|
158
|
+
* design §1 row 17), under a scoped claim: claim, write, release. Enforce refuses a session another
|
|
159
|
+
* process holds, and a claim that failed, with `Can't <action> this session: …` (the refusal is the
|
|
160
|
+
* `cause`), so the holder keeps its transcript; observe logs and counts the conflict and writes as
|
|
161
|
+
* before; off and exempt make no lock call. A session this process holds shares its claim (D10).
|
|
162
|
+
*/
|
|
163
|
+
export async function withScopedSessionClaim<T>(
|
|
164
|
+
ownership: ResolvedSessionOwnership,
|
|
165
|
+
sessionId: string,
|
|
166
|
+
action: string,
|
|
167
|
+
write: () => Promise<T>,
|
|
168
|
+
): Promise<T> {
|
|
169
|
+
if (!claimsUnder(ownership)) return await write();
|
|
170
|
+
const attempt = await attemptSessionClaim(ownership, sessionId);
|
|
171
|
+
try {
|
|
172
|
+
const refusal = claimRefusal(ownership, sessionId, attempt);
|
|
173
|
+
if (refusal) {
|
|
174
|
+
const reason =
|
|
175
|
+
refusal instanceof SessionHeldError
|
|
176
|
+
? `it is open in ${sessionOwnerHolderPhrase(refusal.holder)}`
|
|
177
|
+
: "gjc could not check whether another process has it open";
|
|
178
|
+
throw new Error(`Can't ${action} this session: ${reason}.`, { cause: refusal });
|
|
179
|
+
}
|
|
180
|
+
return await write();
|
|
181
|
+
} finally {
|
|
182
|
+
if (attempt.status === "claimed") attempt.claim.release();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The sessions enforce would refuse because another process holds them, each with the refusal that
|
|
188
|
+
* names its holder: what the held-session warning and the session list's markers show (design §1
|
|
189
|
+
* rows 1-3). A probe, never a claim: the claim stays the authority, and a race falls back to its
|
|
190
|
+
* refusal. `undefined`, with nothing awaited, outside enforce; a probe that fails reads as free.
|
|
191
|
+
*/
|
|
192
|
+
export function probeHeldSessions(
|
|
193
|
+
ownership: ResolvedSessionOwnership,
|
|
194
|
+
sessionIds: readonly string[],
|
|
195
|
+
): Promise<Map<string, SessionHeldError>> | undefined {
|
|
196
|
+
if (ownership === "exempt" || ownership.mode !== "enforce") return undefined;
|
|
197
|
+
return heldSessionRefusals(ownership, sessionIds);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function heldSessionRefusals(
|
|
201
|
+
ownership: ResolvedSessionOwnership,
|
|
202
|
+
sessionIds: readonly string[],
|
|
203
|
+
): Promise<Map<string, SessionHeldError>> {
|
|
204
|
+
const held = new Map<string, SessionHeldError>();
|
|
205
|
+
let probes: Map<string, SessionOwnerProbe>;
|
|
206
|
+
try {
|
|
207
|
+
probes = await probeSessionOwners(sessionIds.filter(id => id.length > 0));
|
|
208
|
+
} catch (error) {
|
|
209
|
+
logger.debug("Session owner probe failed; sessions read as free", { error: String(error) });
|
|
210
|
+
return held;
|
|
211
|
+
}
|
|
212
|
+
for (const [sessionId, probe] of probes) {
|
|
213
|
+
if (probe.state !== "held" || probe.self) continue;
|
|
214
|
+
const refusal = claimRefusal(ownership, sessionId, { status: "held", holder: probe.holder });
|
|
215
|
+
if (refusal instanceof SessionHeldError) held.set(sessionId, refusal);
|
|
216
|
+
}
|
|
217
|
+
return held;
|
|
218
|
+
}
|
|
219
|
+
|
|
20
220
|
/** How long a deferred owner shutdown may keep its predecessor's claim (design §2.5). */
|
|
21
221
|
export const DEFERRED_RELEASE_CAP_MS = 60_000;
|
|
22
222
|
|
|
223
|
+
/**
|
|
224
|
+
* Turn start's re-attempt backoff (design §13.1, timeout-latch policy). A held session costs one
|
|
225
|
+
* non-blocking lock call, so it is retried after a short floor; a timed-out or failed claim can cost
|
|
226
|
+
* the full native budget, so it backs off exponentially up to the cap.
|
|
227
|
+
*/
|
|
228
|
+
export const RECLAIM_HELD_FLOOR_MS = 5_000;
|
|
229
|
+
export const RECLAIM_BACKOFF_BASE_MS = 10_000;
|
|
230
|
+
export const RECLAIM_BACKOFF_CAP_MS = 300_000;
|
|
231
|
+
|
|
232
|
+
/** What turn start's re-attempt found ({@link SessionClaimSet.reclaimCurrent}). */
|
|
233
|
+
export interface SessionReclaim {
|
|
234
|
+
/** The current session's outcome now; `timed_out` is a `none` that a lock stall caused. */
|
|
235
|
+
readonly status: SessionClaimStatus | "timed_out";
|
|
236
|
+
/** Enforce's refusal of the turn ({@link claimRefusal}): the holder keeps the session. */
|
|
237
|
+
readonly refusal?: SessionHeldError | SessionOwnershipError;
|
|
238
|
+
}
|
|
239
|
+
|
|
23
240
|
/** Thrown only under the strict test seam, for a write the detector flags. */
|
|
24
241
|
export class SessionWriteWithoutClaimError extends Error {
|
|
25
242
|
readonly code = "session_write_without_claim";
|
|
@@ -34,8 +251,6 @@ export class SessionWriteWithoutClaimError extends Error {
|
|
|
34
251
|
|
|
35
252
|
interface DeferredHold {
|
|
36
253
|
readonly sessionId: string;
|
|
37
|
-
/** The set that holds the claim now; a manager swap moves holds with their claims. */
|
|
38
|
-
set: SessionClaimSet;
|
|
39
254
|
timer: NodeJS.Timeout | undefined;
|
|
40
255
|
}
|
|
41
256
|
|
|
@@ -54,18 +269,43 @@ const abandonedSets = new FinalizationRegistry<Map<string, SessionOwnerClaim>>(c
|
|
|
54
269
|
export class SessionClaimSet {
|
|
55
270
|
readonly #claims = new Map<string, SessionOwnerClaim>();
|
|
56
271
|
readonly #holds = new Map<string, DeferredHold>();
|
|
272
|
+
/** Claims in flight, by id. The detector counts them as covered: a factory started them before any write. */
|
|
273
|
+
readonly #pending = new Map<string, Promise<unknown>>();
|
|
274
|
+
/** Ids whose write a pending claim covered: if that claim does not land, the write is counted then. */
|
|
275
|
+
readonly #coveredByPending = new Map<string, string>();
|
|
276
|
+
/** The last claim outcome per id, read by the refusal gates (5a). */
|
|
277
|
+
readonly #outcomes = new Map<string, SessionClaimOutcome>();
|
|
57
278
|
#current: string | undefined;
|
|
58
279
|
#transitions = 0;
|
|
59
280
|
#closed = false;
|
|
60
|
-
/**
|
|
61
|
-
#
|
|
281
|
+
/** The factory's decision, with the mode it read once; a settings reload never changes it under a manager. */
|
|
282
|
+
readonly #ownership: ResolvedSessionOwnership;
|
|
62
283
|
/** Detector reports already logged, as `<id> <site>`: log once, count every time. */
|
|
63
284
|
readonly #reported = new Set<string>();
|
|
285
|
+
/** Turn start's re-attempts per id since its last claim: how many, and when the next may run. */
|
|
286
|
+
readonly #reclaims = new Map<string, { readonly attempts: number; readonly notBefore: number }>();
|
|
287
|
+
/** Ids a refused write queued a background re-attempt for. */
|
|
288
|
+
readonly #reclaimQueued = new Set<string>();
|
|
64
289
|
|
|
65
|
-
constructor() {
|
|
290
|
+
constructor(ownership: ResolvedSessionOwnership = resolveSessionOwnership()) {
|
|
291
|
+
this.#ownership = ownership;
|
|
66
292
|
abandonedSets.register(this, this.#claims);
|
|
67
293
|
}
|
|
68
294
|
|
|
295
|
+
/** False in `off` and for an exempt manager: check it before awaiting {@link claim} or {@link settled}. */
|
|
296
|
+
get claiming(): boolean {
|
|
297
|
+
return this.#mode !== "off";
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** The decision this set claims under, for a scoped claim made on the manager's behalf. */
|
|
301
|
+
get ownership(): ResolvedSessionOwnership {
|
|
302
|
+
return this.#ownership;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
get #mode(): SessionOwnershipMode {
|
|
306
|
+
return this.#ownership === "exempt" ? "off" : this.#ownership.mode;
|
|
307
|
+
}
|
|
308
|
+
|
|
69
309
|
/** The session id the manager's identity helper last bound. */
|
|
70
310
|
get currentSessionId(): string | undefined {
|
|
71
311
|
return this.#current;
|
|
@@ -80,35 +320,282 @@ export class SessionClaimSet {
|
|
|
80
320
|
}
|
|
81
321
|
|
|
82
322
|
/**
|
|
83
|
-
* The identity helper's record of the manager's id. It never claims
|
|
84
|
-
*
|
|
323
|
+
* The identity helper's record of the manager's id. It never claims. A change of id outside any
|
|
324
|
+
* transition is counted (when ownership is on): nothing settles after it. The first bind settles,
|
|
325
|
+
* so a claim a factory took for an id it did not adopt (a fork attempt that fell back) goes.
|
|
85
326
|
*/
|
|
86
327
|
bind(sessionId: string): void {
|
|
87
328
|
const previous = this.#current;
|
|
88
329
|
this.#current = sessionId;
|
|
89
|
-
|
|
330
|
+
if (previous === undefined) this.settle();
|
|
90
331
|
if (this.#mode === "off" || previous === undefined || previous === sessionId || this.#transitions > 0) return;
|
|
91
332
|
countSessionOwnershipEvent("bind_outside_transition");
|
|
92
333
|
logger.debug("Session id changed outside a claim-set transition", { mode: this.#mode });
|
|
93
334
|
}
|
|
94
335
|
|
|
336
|
+
/**
|
|
337
|
+
* Claim `sessionId` into this set (design §2.4): a factory's id before its first write, or a
|
|
338
|
+
* transition's target before its rotation. Catches lock errors, but rejects if `add` throws
|
|
339
|
+
* `session_claim_released`; a no-op in `off`, for an exempt manager, and for an id already held.
|
|
340
|
+
*
|
|
341
|
+
* Outside any transition the claim is a factory's, made before the factory's first write and
|
|
342
|
+
* before it hands the manager out: there enforce refuses a held or failed outcome by throwing
|
|
343
|
+
* {@link claimRefusal}'s error. A transition's claim never refuses: a switch is refused before its
|
|
344
|
+
* transition begins, by the manager's switch-target admission.
|
|
345
|
+
*/
|
|
346
|
+
async claim(sessionId: string): Promise<SessionClaimStatus> {
|
|
347
|
+
return this.#claim(sessionId, false);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** {@link claim}; a turn-start `reattempt` repeats the id's last outcome without counting it again. */
|
|
351
|
+
async #claim(sessionId: string, reattempt: boolean): Promise<SessionClaimStatus> {
|
|
352
|
+
if (this.#mode === "off" || this.#closed) return "none";
|
|
353
|
+
if (this.#claims.has(sessionId)) return "claimed";
|
|
354
|
+
const inFlight = this.#pending.get(sessionId);
|
|
355
|
+
if (inFlight) {
|
|
356
|
+
await inFlight;
|
|
357
|
+
return this.#admit(sessionId);
|
|
358
|
+
}
|
|
359
|
+
const previous = reattempt ? this.#outcomes.get(sessionId) : undefined;
|
|
360
|
+
const attempt = attemptSessionClaim(this.#ownership, sessionId, previous);
|
|
361
|
+
this.#pending.set(sessionId, attempt);
|
|
362
|
+
let status: SessionClaimStatus = "none";
|
|
363
|
+
try {
|
|
364
|
+
const result = await attempt;
|
|
365
|
+
status = result.status;
|
|
366
|
+
this.#record(sessionId, outcomeOf(result));
|
|
367
|
+
if (result.status === "claimed") {
|
|
368
|
+
// A re-attempt whose session the manager left while it was in flight is no staged target:
|
|
369
|
+
// released, so no `claimed` outcome stays recorded for it.
|
|
370
|
+
if (reattempt && sessionId !== this.#current && this.#transitions === 0) {
|
|
371
|
+
result.claim.release();
|
|
372
|
+
this.#outcomes.delete(sessionId);
|
|
373
|
+
} else this.add(result.claim);
|
|
374
|
+
}
|
|
375
|
+
} finally {
|
|
376
|
+
this.#pending.delete(sessionId);
|
|
377
|
+
const coveredSite = this.#coveredByPending.get(sessionId);
|
|
378
|
+
this.#coveredByPending.delete(sessionId);
|
|
379
|
+
// Only a session another process holds is a write without a claim; a fail-open or failed
|
|
380
|
+
// claim (no lock on this platform, a stall, an owners-directory failure) is not.
|
|
381
|
+
if (coveredSite !== undefined && status === "held")
|
|
382
|
+
this.#reportWithoutClaim(`claim in flight: ${coveredSite}`, sessionId);
|
|
383
|
+
}
|
|
384
|
+
return this.#admit(sessionId);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** A factory's claim outcome, or its refusal; a transition's outcome is never refused. */
|
|
388
|
+
#admit(sessionId: string): SessionClaimStatus {
|
|
389
|
+
const outcome = this.#outcomes.get(sessionId);
|
|
390
|
+
if (this.#transitions === 0) {
|
|
391
|
+
const refusal = claimRefusal(this.#ownership, sessionId, outcome);
|
|
392
|
+
if (refusal) throw refusal;
|
|
393
|
+
}
|
|
394
|
+
return outcome?.status ?? "none";
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* A sync factory's claim (`create`): started now, settled later. A write made while it is in flight
|
|
399
|
+
* is covered, because the id is fresh and no other process can hold it.
|
|
400
|
+
*/
|
|
401
|
+
claimInBackground(sessionId: string): void {
|
|
402
|
+
if (!this.claiming) return;
|
|
403
|
+
void this.claim(sessionId)
|
|
404
|
+
.then(() => {
|
|
405
|
+
// Landed after a rotation already settled away from it: release it now, not at the next settle.
|
|
406
|
+
if (this.#current !== sessionId && this.#transitions === 0) this.settle();
|
|
407
|
+
})
|
|
408
|
+
// `claim` catches lock errors but rejects with enforce's refusal (the gate in `refusal()` reports
|
|
409
|
+
// it again) or if `add` throws `session_claim_released`; the settle can throw too. A background
|
|
410
|
+
// claim must not become an unhandled rejection in any process.
|
|
411
|
+
.catch(error => {
|
|
412
|
+
logger.warn("Background session claim failed", {
|
|
413
|
+
sessionId,
|
|
414
|
+
error: error instanceof Error ? error.message : String(error),
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Record the outcome of a claim taken before this set existed (a strict open's, handed over). */
|
|
420
|
+
noteOutcome(sessionId: string, outcome: SessionClaimOutcome): void {
|
|
421
|
+
this.#record(sessionId, outcome);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Record an id's claim outcome and when turn start may re-attempt it. An open's own outcome seeds
|
|
426
|
+
* the backoff, so the first turn after a stalled open does not stall again at once. A held session
|
|
427
|
+
* waits the short floor and restarts the exponential count; a timeout or failure doubles it.
|
|
428
|
+
*/
|
|
429
|
+
#record(sessionId: string, outcome: SessionClaimOutcome): void {
|
|
430
|
+
this.#outcomes.set(sessionId, outcome);
|
|
431
|
+
const retried =
|
|
432
|
+
outcome.status === "held" ||
|
|
433
|
+
outcome.status === "failed" ||
|
|
434
|
+
(outcome.status === "none" && outcome.skipped === "timed_out");
|
|
435
|
+
if (!retried) {
|
|
436
|
+
this.#reclaims.delete(sessionId);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
if (outcome.status === "held") {
|
|
440
|
+
this.#reclaims.set(sessionId, { attempts: 0, notBefore: Date.now() + RECLAIM_HELD_FLOOR_MS });
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
const attempts = this.#reclaims.get(sessionId)?.attempts ?? 0;
|
|
444
|
+
const delay = Math.min(RECLAIM_BACKOFF_BASE_MS * 2 ** attempts, RECLAIM_BACKOFF_CAP_MS);
|
|
445
|
+
this.#reclaims.set(sessionId, { attempts: attempts + 1, notBefore: Date.now() + delay });
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Wait for the claims in flight, then the outcome for the current id. `undefined` when nothing
|
|
450
|
+
* was attempted.
|
|
451
|
+
*/
|
|
452
|
+
async settled(): Promise<SessionClaimStatus | undefined> {
|
|
453
|
+
await Promise.all(this.#pending.values());
|
|
454
|
+
return this.#current === undefined ? undefined : this.#outcomes.get(this.#current)?.status;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* The gate for a manager about to be used for writing (`createAgentSession`, session import):
|
|
459
|
+
* waits for the claims in flight and returns enforce's refusal for the current id, if any. It
|
|
460
|
+
* catches what no factory refused, such as the background claim of `create`.
|
|
461
|
+
*/
|
|
462
|
+
async refusal(): Promise<SessionHeldError | SessionOwnershipError | undefined> {
|
|
463
|
+
await Promise.all(this.#pending.values());
|
|
464
|
+
const current = this.#current;
|
|
465
|
+
return current === undefined ? undefined : claimRefusal(this.#ownership, current, this.#outcomes.get(current));
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Turn start's re-attempt of the current id's claim (design §2.7, §13.1): a claim another process
|
|
470
|
+
* held, or one that timed out or failed, is tried again, so a writer whose open did not claim
|
|
471
|
+
* becomes visible once it can. `undefined`, with nothing awaited, when there is nothing to
|
|
472
|
+
* re-attempt (off, exempt, closed, inside a transition, already claimed, a permanent fail-open, a
|
|
473
|
+
* live mode lowered to off, inside the backoff) and whenever the turn does not depend on it: in
|
|
474
|
+
* observe, or with the live mode lowered from enforce, the re-attempt runs in the background, and
|
|
475
|
+
* the writes it overlaps are covered as a pending claim's are. Only under enforce does the turn wait,
|
|
476
|
+
* joining a claim already in flight; there a held or failed claim is re-attempted at every turn, so
|
|
477
|
+
* no turn passes it unchecked. Never rejects: enforce's refusal comes back as a value, and the
|
|
478
|
+
* session is never taken from its holder.
|
|
479
|
+
*/
|
|
480
|
+
reclaimCurrent(): Promise<SessionReclaim> | undefined {
|
|
481
|
+
const sessionId = this.#current;
|
|
482
|
+
if (sessionId === undefined || this.#mode === "off" || this.#closed || this.#transitions > 0) return undefined;
|
|
483
|
+
if (this.#claims.has(sessionId)) return undefined;
|
|
484
|
+
const inFlight = this.#pending.get(sessionId);
|
|
485
|
+
const outcome = this.#outcomes.get(sessionId);
|
|
486
|
+
if (!inFlight) {
|
|
487
|
+
if (outcome === undefined || outcome.status === "claimed") return undefined;
|
|
488
|
+
if (outcome.status === "none" && outcome.skipped !== "timed_out") return undefined;
|
|
489
|
+
}
|
|
490
|
+
const live = liveSessionOwnershipModeFor(this.#mode);
|
|
491
|
+
if (live === "off") return undefined;
|
|
492
|
+
const enforced = this.#mode === "enforce" && live === "enforce";
|
|
493
|
+
// A claim in flight already covers the writes; only enforce's turn waits for its outcome.
|
|
494
|
+
if (inFlight) return enforced ? this.#joinReclaim(sessionId, inFlight) : undefined;
|
|
495
|
+
if (outcome === undefined) return undefined;
|
|
496
|
+
// What enforce refuses (another holder, a failed claim) is checked at every turn: a backoff
|
|
497
|
+
// would let the turns inside it through, writing.
|
|
498
|
+
const checksEveryTurn = enforced && (outcome.status === "held" || outcome.status === "failed");
|
|
499
|
+
const backoff = this.#reclaims.get(sessionId);
|
|
500
|
+
if (!checksEveryTurn && backoff !== undefined && Date.now() < backoff.notBefore) return undefined;
|
|
501
|
+
const reclaim = this.#reclaim(sessionId);
|
|
502
|
+
if (enforced) return reclaim;
|
|
503
|
+
void reclaim;
|
|
504
|
+
return undefined;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
async #reclaim(sessionId: string): Promise<SessionReclaim> {
|
|
508
|
+
try {
|
|
509
|
+
await this.#claim(sessionId, true);
|
|
510
|
+
} catch (error) {
|
|
511
|
+
// `#claim` rejects with enforce's refusal, recorded as the outcome and returned below.
|
|
512
|
+
if (!(error instanceof SessionHeldError || error instanceof SessionOwnershipError))
|
|
513
|
+
logger.warn("Session claim re-attempt failed", { error: String(error) });
|
|
514
|
+
}
|
|
515
|
+
return this.#reclaimResult(sessionId);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
async #joinReclaim(sessionId: string, inFlight: Promise<unknown>): Promise<SessionReclaim> {
|
|
519
|
+
await inFlight.catch(() => undefined);
|
|
520
|
+
return this.#reclaimResult(sessionId);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** The re-attempt's result for the turn: nothing to refuse once the manager rotated away. */
|
|
524
|
+
#reclaimResult(sessionId: string): SessionReclaim {
|
|
525
|
+
if (this.#current !== sessionId) return { status: "none" };
|
|
526
|
+
const outcome = this.#outcomes.get(sessionId);
|
|
527
|
+
const status =
|
|
528
|
+
outcome?.status === "none" && outcome.skipped === "timed_out" ? "timed_out" : (outcome?.status ?? "none");
|
|
529
|
+
return { status, refusal: claimRefusal(this.#ownership, sessionId, outcome) };
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Enforce's write gate (design §2.5, §13.1): a write to an id whose recorded outcome enforce
|
|
534
|
+
* refuses (`held`: a turn-start re-attempt found another holder, or a transition landed held;
|
|
535
|
+
* `failed`: the claim failed, fail-closed) is refused with {@link claimRefusal}'s error, so no
|
|
536
|
+
* writer outside a turn (a title, a rename, a compaction, an extension's entry, an SDK append)
|
|
537
|
+
* writes over a holder. It reads only the recorded outcome and makes no lock call; the turn gate is
|
|
538
|
+
* what finds the holder. A refusal starts a background re-attempt (within the backoff), so the gate
|
|
539
|
+
* lifts on its own soon after the holder exits. Inside a transition nothing is refused.
|
|
540
|
+
*/
|
|
541
|
+
refuseWriteIfRefused(sessionId: string | undefined = this.#current): void {
|
|
542
|
+
if (sessionId === undefined || this.#mode !== "enforce" || this.#transitions > 0) return;
|
|
543
|
+
if (this.#claims.has(sessionId)) return;
|
|
544
|
+
const outcome = this.#outcomes.get(sessionId);
|
|
545
|
+
if (outcome?.status !== "held" && outcome?.status !== "failed") return;
|
|
546
|
+
const refusal = claimRefusal(this.#ownership, sessionId, outcome);
|
|
547
|
+
if (!refusal) return;
|
|
548
|
+
this.#reclaimSoon(sessionId);
|
|
549
|
+
throw refusal;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** A background re-attempt of `sessionId` after this write returns, unless one is in flight or backing off. */
|
|
553
|
+
#reclaimSoon(sessionId: string): void {
|
|
554
|
+
if (this.#reclaimQueued.has(sessionId)) return;
|
|
555
|
+
this.#reclaimQueued.add(sessionId);
|
|
556
|
+
queueMicrotask(() => {
|
|
557
|
+
this.#reclaimQueued.delete(sessionId);
|
|
558
|
+
const backoff = this.#reclaims.get(sessionId);
|
|
559
|
+
if (this.#closed || this.#transitions > 0 || this.#pending.has(sessionId) || this.#claims.has(sessionId))
|
|
560
|
+
return;
|
|
561
|
+
if (backoff !== undefined && Date.now() < backoff.notBefore) return;
|
|
562
|
+
void this.#reclaim(sessionId);
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
|
|
95
566
|
/**
|
|
96
567
|
* The write-path detector (design §2.5), called before a transcript write. When ownership is on
|
|
97
568
|
* and this set holds no claim for `sessionId` (the current id unless a successor is named), it
|
|
98
569
|
* logs `session_write_without_claim` with the call site and counts it. It never claims, and it
|
|
99
570
|
* throws only under the strict test seam. In `off` it returns at once.
|
|
571
|
+
*
|
|
572
|
+
* A write made before the manager's first bind (a factory publishing a new transcript before it
|
|
573
|
+
* adopts it) must name its id: with nothing bound there is no current id.
|
|
100
574
|
*/
|
|
101
575
|
checkWrite(site: string, sessionId: string | undefined = this.#current): void {
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
576
|
+
if (sessionId === undefined) return;
|
|
577
|
+
if (this.#mode === "off" || this.#claims.has(sessionId)) return;
|
|
578
|
+
this.refuseWriteIfRefused(sessionId);
|
|
579
|
+
// The claim for this id failed open (`none` while claiming) or failed: nothing to detect, and
|
|
580
|
+
// counting it would make every Windows or stalled-filesystem process report writes.
|
|
581
|
+
const outcome = this.#outcomes.get(sessionId)?.status;
|
|
582
|
+
if (outcome === "none" || outcome === "failed") return;
|
|
583
|
+
if (this.#pending.has(sessionId)) {
|
|
584
|
+
if (!this.#coveredByPending.has(sessionId)) this.#coveredByPending.set(sessionId, site);
|
|
585
|
+
return;
|
|
108
586
|
}
|
|
587
|
+
this.#reportWithoutClaim(site, sessionId);
|
|
109
588
|
if (sessionOwnershipTestHookSlot().strictWriteDetection) throw new SessionWriteWithoutClaimError(site);
|
|
110
589
|
}
|
|
111
590
|
|
|
591
|
+
#reportWithoutClaim(site: string, sessionId: string): void {
|
|
592
|
+
countSessionOwnershipEvent("session_write_without_claim");
|
|
593
|
+
const report = `${sessionId} ${site}`;
|
|
594
|
+
if (this.#reported.has(report)) return;
|
|
595
|
+
this.#reported.add(report);
|
|
596
|
+
logger.warn("session_write_without_claim", { site, sessionId, mode: this.#mode });
|
|
597
|
+
}
|
|
598
|
+
|
|
112
599
|
/**
|
|
113
600
|
* Take ownership of a claim: a factory's, or a transition's target claimed before the rotation.
|
|
114
601
|
* The key is the id, so a second claim of an id already here is released at once (one claim per id).
|
|
@@ -120,6 +607,9 @@ export class SessionClaimSet {
|
|
|
120
607
|
return;
|
|
121
608
|
}
|
|
122
609
|
this.#claims.set(claim.sessionId, claim);
|
|
610
|
+
// The opener's own claim has landed: the covers its open or switch took go now, not when the
|
|
611
|
+
// caller's flow settles, so they are never a second holder of the open session.
|
|
612
|
+
releaseSessionAdmissions(claim);
|
|
123
613
|
// A target staged outside the transition that rotates to it is exposed to any unrelated
|
|
124
614
|
// transition's settle. Visible in observe; off stays inert.
|
|
125
615
|
if (
|
|
@@ -169,17 +659,17 @@ export class SessionClaimSet {
|
|
|
169
659
|
*/
|
|
170
660
|
holdUntil(sessionId: string, finalization: Promise<unknown>, capMs = DEFERRED_RELEASE_CAP_MS): void {
|
|
171
661
|
if (!this.#claims.has(sessionId) || this.#holds.has(sessionId)) return;
|
|
172
|
-
const hold: DeferredHold = { sessionId,
|
|
662
|
+
const hold: DeferredHold = { sessionId, timer: undefined };
|
|
173
663
|
this.#holds.set(sessionId, hold);
|
|
174
664
|
const end = (timedOut: boolean) => {
|
|
175
|
-
if (
|
|
665
|
+
if (this.#holds.get(sessionId) !== hold) return;
|
|
176
666
|
if (hold.timer) clearTimeout(hold.timer);
|
|
177
|
-
|
|
667
|
+
this.#holds.delete(sessionId);
|
|
178
668
|
if (timedOut) countSessionOwnershipEvent("release_after_deferred_timeout");
|
|
179
|
-
if (
|
|
669
|
+
if (this.#closed) this.#releaseOne(sessionId);
|
|
180
670
|
// Only the held id: any other non-current claim (a target staged before its rotation)
|
|
181
671
|
// waits for the transition that commits or abandons it.
|
|
182
|
-
else if (
|
|
672
|
+
else if (this.#transitions === 0 && this.#current !== sessionId) this.#releaseOne(sessionId);
|
|
183
673
|
};
|
|
184
674
|
hold.timer = setTimeout(() => end(true), capMs);
|
|
185
675
|
hold.timer.unref();
|
|
@@ -189,32 +679,10 @@ export class SessionClaimSet {
|
|
|
189
679
|
);
|
|
190
680
|
}
|
|
191
681
|
|
|
192
|
-
/**
|
|
193
|
-
* Cold-sidecar manager swap: every claim and hold moves to `target`, and this set is emptied
|
|
194
|
-
* without unlocking anything. The target then settles: a claim for its current id and any held
|
|
195
|
-
* claim survive, everything else is released (a fork's predecessor, once the swap commits).
|
|
196
|
-
* Open transitions are not carried across: a caller that swaps inside a transition must call
|
|
197
|
-
* this after that transition ends, or open one on the target.
|
|
198
|
-
*/
|
|
199
|
-
moveTo(target: SessionClaimSet): void {
|
|
200
|
-
if (target === this) return;
|
|
201
|
-
for (const claim of this.#claims.values()) target.add(claim);
|
|
202
|
-
this.#claims.clear();
|
|
203
|
-
for (const [sessionId, hold] of this.#holds) {
|
|
204
|
-
if (target.#holds.has(sessionId)) {
|
|
205
|
-
if (hold.timer) clearTimeout(hold.timer);
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
hold.set = target;
|
|
209
|
-
target.#holds.set(sessionId, hold);
|
|
210
|
-
}
|
|
211
|
-
this.#holds.clear();
|
|
212
|
-
target.settle();
|
|
213
|
-
}
|
|
214
|
-
|
|
215
682
|
/**
|
|
216
683
|
* `close()`: release every claim. A claim under a deferred hold is released when its hold ends,
|
|
217
|
-
* at most the cap later; nothing can be added afterwards
|
|
684
|
+
* at most the cap later; nothing can be added afterwards, and a claim still in flight is released
|
|
685
|
+
* when it lands.
|
|
218
686
|
*/
|
|
219
687
|
releaseAll(): void {
|
|
220
688
|
this.#closed = true;
|
|
@@ -240,3 +708,13 @@ interface ClaimSetOwner {
|
|
|
240
708
|
export function sessionClaimSetOf(manager: SessionManager): SessionClaimSet {
|
|
241
709
|
return (manager as unknown as ClaimSetOwner)[sessionClaimSetCapability]();
|
|
242
710
|
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Turn start's re-attempt for `manager`'s current session ({@link SessionClaimSet.reclaimCurrent});
|
|
714
|
+
* `undefined` for a stand-in manager that has no claim set, as a test's double has none.
|
|
715
|
+
*/
|
|
716
|
+
export function reclaimCurrentSession(manager: SessionManager): Promise<SessionReclaim> | undefined {
|
|
717
|
+
const owner = manager as unknown as Partial<ClaimSetOwner>;
|
|
718
|
+
if (typeof owner[sessionClaimSetCapability] !== "function") return undefined;
|
|
719
|
+
return sessionClaimSetOf(manager).reclaimCurrent();
|
|
720
|
+
}
|