@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
package/src/modes/DESIGN.md
CHANGED
|
@@ -520,3 +520,39 @@ at `80x24/ascii-no-color`, `compact-collapsed-mixed/48x36/unicode-color`, and
|
|
|
520
520
|
the viewport is captured once (`top`); an overflowing entry is captured at
|
|
521
521
|
`top`, `middle`, and `bottom` so no tail is hidden. Every capture has
|
|
522
522
|
`terminal.txt`, `terminal-ansi.txt`, `terminal.html`, and `metadata.json`.
|
|
523
|
+
|
|
524
|
+
## Held-session warning and picker marker
|
|
525
|
+
|
|
526
|
+
Source: `components/session-held-warning.ts` (the dialog), `components/session-selector.ts`
|
|
527
|
+
(the marker), session single-owner design §3.1. This section extends the confirmation
|
|
528
|
+
grammar above; it adds no tokens and no new component class (`session-held-warning.ts` is a
|
|
529
|
+
factory over `HookSelectorComponent`).
|
|
530
|
+
|
|
531
|
+
- **Anatomy:** a `HookSelectorComponent`, the same frame as "Resume this session?": border,
|
|
532
|
+
a bold heading `This session is open in another process`, one line naming the session by
|
|
533
|
+
its short id and the holder by its fixed phrase (`another terminal (pid N)`), one line
|
|
534
|
+
`Only one process can write a session at a time.`, then the options `Fork into a new
|
|
535
|
+
session — copy the conversation and continue there` (focused), `Continue here — close the
|
|
536
|
+
session host and open it here` (only when the holder is a broker-launched host) and
|
|
537
|
+
`Cancel`, the standard dim key hint, border. At launch and in the picker, `f`, `h` and `c`
|
|
538
|
+
are accelerators; the `/resume` hook selector has none.
|
|
539
|
+
- **Continue here refused:** when the host is running a turn, the same dialog shows again
|
|
540
|
+
with one blank line and `The session is running a turn; try again when it finishes.` under
|
|
541
|
+
the warning lines; any other refusal shows its fixed reason there.
|
|
542
|
+
- **Where it appears:** before the TUI starts (a one-shot TUI after one blank row, as the
|
|
543
|
+
picker does), appended under the `--resume` picker like the resume confirmation, and as
|
|
544
|
+
the TUI's hook selector for `/resume`. Escape is Cancel everywhere.
|
|
545
|
+
- **States:** focused Fork (default); Cancel returns where the user was (exits at launch,
|
|
546
|
+
back to the list in the picker, stays in the session for `/resume`); Fork opens the copy
|
|
547
|
+
idle and a status message (at launch, a startup notice) says `Forked into a new session.
|
|
548
|
+
The original stays with <holder>.`
|
|
549
|
+
- **Marker:** a session list row another process holds ends with
|
|
550
|
+
`<format.bullet> in terminal` (or `in app`, `in gjc -p`, `in session host`, `in SDK app`,
|
|
551
|
+
`in subagent`, `in use`) in `warning`. The title is truncated first so the marker stays
|
|
552
|
+
visible. The marker carries a word, not only a symbol and a color.
|
|
553
|
+
- **Content safety:** only fixed copy, the holder phrase (validated surface and pid) and a
|
|
554
|
+
safe short id reach the dialog; no title, path or record text another process wrote.
|
|
555
|
+
- **Responsive:** at 80 columns the heading, the options and a terminal holder's line fit; a
|
|
556
|
+
longer holder phrase (`an application using the gjc SDK (pid N)`) wraps. At 60 columns the
|
|
557
|
+
holder line and the Fork label wrap inside the frame. The copy is English only, with no CJK
|
|
558
|
+
text.
|
|
@@ -74,6 +74,7 @@ import { validateRequiredPromptText } from "../../sdk/protocol/adapter-validatio
|
|
|
74
74
|
import { type SessionAttachment, SessionRouter, type SessionRouterFrame } from "../../sdk/router";
|
|
75
75
|
import { SessionListTraversalError, sessionListPageFromResponse, traverseSessionList } from "../../sdk/session-list";
|
|
76
76
|
import { findOperatorMessage } from "../../session/operator-error-message";
|
|
77
|
+
import { sessionOwnerHolderSummary } from "../../session/session-owner-holder";
|
|
77
78
|
import { MAX_STAGED_PROMPT_IMAGES } from "../../utils/image-limits";
|
|
78
79
|
import { resolveAcpAbortScope } from "./abort-scope";
|
|
79
80
|
import {
|
|
@@ -471,6 +472,30 @@ function isAcpUnavailableSlashCommand(text: string): boolean {
|
|
|
471
472
|
return /^\/import-session(?:\s|:|$)/u.test(text);
|
|
472
473
|
}
|
|
473
474
|
|
|
475
|
+
/**
|
|
476
|
+
* The broker returned no saved session for an exact lookup. The code stays `not_found`, which
|
|
477
|
+
* session/load callers retry on; `omissionReason` is the broker's `savedSessionOmission.reason`.
|
|
478
|
+
* Only `candidate_not_found` (or no reason) means the transcript is gone: a busy migration, a
|
|
479
|
+
* changed source or an unavailable scope clears up on its own.
|
|
480
|
+
*/
|
|
481
|
+
class SavedSessionUnavailableError extends AcpSdkAdapterError {
|
|
482
|
+
readonly omissionReason: string | undefined;
|
|
483
|
+
constructor(sessionId: string, omissionReason: string | undefined) {
|
|
484
|
+
super("not_found", `Saved ACP session does not exist: ${sessionId}`);
|
|
485
|
+
this.omissionReason = omissionReason;
|
|
486
|
+
}
|
|
487
|
+
get gone(): boolean {
|
|
488
|
+
return this.omissionReason === undefined || this.omissionReason === "candidate_not_found";
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** This connection's own cwd or scope authority disagrees with the broker's: a retry cannot fix it. */
|
|
493
|
+
class AcpSessionScopeConflictError extends AcpSdkAdapterError {
|
|
494
|
+
constructor(message: string) {
|
|
495
|
+
super("conflict", message);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
474
499
|
async function collectAcpSessionList(
|
|
475
500
|
request: (input: JsonObject) => Promise<unknown>,
|
|
476
501
|
input: JsonObject = {},
|
|
@@ -1691,6 +1716,13 @@ export function acpRequestFailure(error: unknown): unknown {
|
|
|
1691
1716
|
// terminals use (#5615). `code`/`details` and the JSON-RPC message are untouched.
|
|
1692
1717
|
const operatorMessage = findOperatorMessage(error);
|
|
1693
1718
|
if (operatorMessage !== undefined) Object.assign(data, { operatorMessage });
|
|
1719
|
+
// A resume the broker refused because another process holds the session names that process
|
|
1720
|
+
// (surface and pid) beside the code, as the broker reported it.
|
|
1721
|
+
const holder =
|
|
1722
|
+
code === "live_session" && error instanceof SdkClientError
|
|
1723
|
+
? sessionOwnerHolderSummary(object(error.details)?.holder)
|
|
1724
|
+
: undefined;
|
|
1725
|
+
if (holder !== undefined) Object.assign(data, { holder });
|
|
1694
1726
|
switch (code) {
|
|
1695
1727
|
case "authentication_failed":
|
|
1696
1728
|
return RequestError.authRequired(data, message);
|
|
@@ -1905,6 +1937,14 @@ export class AcpAgent implements Agent {
|
|
|
1905
1937
|
deps: {
|
|
1906
1938
|
onAttachment: attachment => {
|
|
1907
1939
|
const record = this.#sessions.get(attachment.sessionId);
|
|
1940
|
+
if (record && this.#ownerEndpointChanged(record, attachment)) {
|
|
1941
|
+
// A new endpoint generation can be another process: a terminal that took the
|
|
1942
|
+
// session over with Continue here (session single-owner design §13 H1). The
|
|
1943
|
+
// record's provenance (lifecycle ownership, reverse providers) belongs to the
|
|
1944
|
+
// old host, so re-establish the session through the broker instead of rebinding.
|
|
1945
|
+
void this.#reattachInPlace(attachment.sessionId, record);
|
|
1946
|
+
return;
|
|
1947
|
+
}
|
|
1908
1948
|
const adapter = record?.adapter ?? this.#pendingRouterAdapters.get(attachment.sessionId);
|
|
1909
1949
|
if (!adapter) return;
|
|
1910
1950
|
if (record) record.attachment = attachment;
|
|
@@ -1912,6 +1952,7 @@ export class AcpAgent implements Agent {
|
|
|
1912
1952
|
},
|
|
1913
1953
|
onAttachmentReady: async attachment => {
|
|
1914
1954
|
const record = this.#sessions.get(attachment.sessionId);
|
|
1955
|
+
if (record && this.#ownerEndpointChanged(record, attachment)) return;
|
|
1915
1956
|
const adapter = record?.adapter ?? this.#pendingRouterAdapters.get(attachment.sessionId);
|
|
1916
1957
|
if (!adapter) return;
|
|
1917
1958
|
if (record) record.attachment = attachment;
|
|
@@ -2197,13 +2238,19 @@ export class AcpAgent implements Agent {
|
|
|
2197
2238
|
|
|
2198
2239
|
closeSession(params: CloseSessionRequest): Promise<CloseSessionResponse> {
|
|
2199
2240
|
const record = this.#sessions.get(params.sessionId);
|
|
2241
|
+
// A session re-attaching in place is still this connection's: the close wins. Advancing the
|
|
2242
|
+
// epoch makes the re-attach fail without restoring its record; the close then runs after it.
|
|
2243
|
+
const reattaching = this.#reattachingSessions.get(params.sessionId);
|
|
2244
|
+
if (reattaching) this.#advanceSessionEpoch(params.sessionId);
|
|
2200
2245
|
// ACP close has no cwd. Broker lifecycle control stays owner-only, but a live
|
|
2201
2246
|
// local attachment is always released: a CLI-primary session the client closed
|
|
2202
2247
|
// must not keep its adapter, lease heartbeat, subscriptions and pending prompt.
|
|
2203
|
-
if (!this.#ownedSessionIds.has(params.sessionId) && record === undefined
|
|
2248
|
+
if (!this.#ownedSessionIds.has(params.sessionId) && record === undefined && !reattaching)
|
|
2249
|
+
return Promise.resolve({});
|
|
2204
2250
|
const cwd = record?.cwd ?? this.#knownSessionCwds.get(params.sessionId);
|
|
2205
2251
|
if (!cwd) return Promise.resolve({});
|
|
2206
2252
|
return this.#enqueueLifecycleOperation(params.sessionId, async () => {
|
|
2253
|
+
await reattaching;
|
|
2207
2254
|
// A preceding delete in the same lifecycle chain already completed the terminal operation.
|
|
2208
2255
|
if (!this.#ownedSessionIds.has(params.sessionId) && !this.#sessions.has(params.sessionId)) return {};
|
|
2209
2256
|
return this.#closeOwnedSession(params.sessionId);
|
|
@@ -2367,6 +2414,11 @@ export class AcpAgent implements Agent {
|
|
|
2367
2414
|
}
|
|
2368
2415
|
|
|
2369
2416
|
async prompt(params: PromptRequest): Promise<PromptResponse> {
|
|
2417
|
+
return await this.#prompt(params, false);
|
|
2418
|
+
}
|
|
2419
|
+
|
|
2420
|
+
/** `rebound`: this prompt already re-attached its session once and is not re-attached again. */
|
|
2421
|
+
async #prompt(params: PromptRequest, rebound: boolean): Promise<PromptResponse> {
|
|
2370
2422
|
const record = this.#sessions.get(params.sessionId);
|
|
2371
2423
|
// An unknown session must still report not_found; the retry loop only guards a
|
|
2372
2424
|
// live session's first turn against a startup readiness race (issue #5574).
|
|
@@ -2374,13 +2426,27 @@ export class AcpAgent implements Agent {
|
|
|
2374
2426
|
const reattaching = this.#reattachingSessions.get(params.sessionId);
|
|
2375
2427
|
if (reattaching) {
|
|
2376
2428
|
await reattaching;
|
|
2377
|
-
return await this
|
|
2429
|
+
return await this.#prompt(params, rebound);
|
|
2378
2430
|
}
|
|
2379
2431
|
// A session whose host attachment could not be restored reports its typed failure.
|
|
2380
2432
|
const unrecoverable = this.#unrecoverableSessions.get(params.sessionId);
|
|
2381
2433
|
if (unrecoverable) throw unrecoverable;
|
|
2382
2434
|
return await this.#submitPrompt(params, true);
|
|
2383
2435
|
}
|
|
2436
|
+
// The host may have closed between turns, as Continue here closes a broker-launched host for
|
|
2437
|
+
// the terminal taking the session over (session single-owner design §13 H1). Reconcile first,
|
|
2438
|
+
// so the Router learns of it before anything is dispatched; with no successor attached yet,
|
|
2439
|
+
// re-attach the session to its current owner through the broker, which waits for that owner's
|
|
2440
|
+
// endpoint, then submit there. If that fails the session keeps its record, and this prompt
|
|
2441
|
+
// fails as it did before; a later prompt or attachment recovers it.
|
|
2442
|
+
if (!rebound && this.#idleForReattach(params.sessionId, record)) {
|
|
2443
|
+
await this.#router.reconcile({ waitForReplay: false }).catch(() => undefined);
|
|
2444
|
+
if (this.#sessions.get(params.sessionId) !== record) return await this.#prompt(params, true);
|
|
2445
|
+
if (this.#idleForReattach(params.sessionId, record) && !record.attachment.isCurrent()) {
|
|
2446
|
+
await this.#reattachInPlace(params.sessionId, record);
|
|
2447
|
+
return await this.#prompt(params, true);
|
|
2448
|
+
}
|
|
2449
|
+
}
|
|
2384
2450
|
// Identity of THIS caller's first-turn retry. Held on the record across the backoff so a
|
|
2385
2451
|
// concurrent prompt racing the gap is rejected as a conflict rather than admitted (review P1).
|
|
2386
2452
|
const retryReservation: FirstPromptRetryReservation = { firstPromptRetry: true };
|
|
@@ -3217,6 +3283,12 @@ export class AcpAgent implements Agent {
|
|
|
3217
3283
|
|
|
3218
3284
|
async cancel(params: CancelNotification): Promise<void> {
|
|
3219
3285
|
const record = this.#sessions.get(params.sessionId);
|
|
3286
|
+
// A session re-attaching in place is still there: cancel whatever prompt it carries once it settles.
|
|
3287
|
+
const reattaching = record ? undefined : this.#reattachingSessions.get(params.sessionId);
|
|
3288
|
+
if (reattaching) {
|
|
3289
|
+
await reattaching;
|
|
3290
|
+
if (this.#sessions.has(params.sessionId)) return await this.cancel(params);
|
|
3291
|
+
}
|
|
3220
3292
|
if (!record) throw new AcpSdkAdapterError("not_found", `Unknown session, not found: ${params.sessionId}`);
|
|
3221
3293
|
// Record the client's intent before awaiting the SDK so a prompt that rejects
|
|
3222
3294
|
// mid-cancel (e.g. preflight `busy`) can still settle as `cancelled`.
|
|
@@ -3693,7 +3765,7 @@ export class AcpAgent implements Agent {
|
|
|
3693
3765
|
const attached = this.#sessions.get(id);
|
|
3694
3766
|
if (attached) {
|
|
3695
3767
|
if (path.resolve(attached.cwd) !== path.resolve(cwd))
|
|
3696
|
-
throw new
|
|
3768
|
+
throw new AcpSessionScopeConflictError(`ACP session ${id} has conflicting cwd authority.`);
|
|
3697
3769
|
// ACP clients replay their declared MCP servers when reconnecting. The live
|
|
3698
3770
|
// session host remains authoritative for its immutable configuration, so
|
|
3699
3771
|
// attachment must not reinterpret the replay as a mutation request.
|
|
@@ -3703,7 +3775,7 @@ export class AcpAgent implements Agent {
|
|
|
3703
3775
|
}
|
|
3704
3776
|
const knownCwd = this.#knownSessionCwds.get(id);
|
|
3705
3777
|
if (knownCwd && path.resolve(knownCwd) !== path.resolve(cwd))
|
|
3706
|
-
throw new
|
|
3778
|
+
throw new AcpSessionScopeConflictError(`ACP session ${id} has conflicting cwd authority.`);
|
|
3707
3779
|
const resolving = this.#resolvingExisting.get(id);
|
|
3708
3780
|
if (resolving?.epoch === epoch) {
|
|
3709
3781
|
await resolving.task;
|
|
@@ -3711,7 +3783,7 @@ export class AcpAgent implements Agent {
|
|
|
3711
3783
|
const resolved = this.#sessions.get(id);
|
|
3712
3784
|
if (!resolved) throw new AcpSdkAdapterError("unavailable", `ACP session ${id} did not attach.`);
|
|
3713
3785
|
if (path.resolve(resolved.cwd) !== path.resolve(cwd))
|
|
3714
|
-
throw new
|
|
3786
|
+
throw new AcpSessionScopeConflictError(`ACP session ${id} has conflicting cwd authority.`);
|
|
3715
3787
|
return;
|
|
3716
3788
|
}
|
|
3717
3789
|
|
|
@@ -3782,10 +3854,10 @@ export class AcpAgent implements Agent {
|
|
|
3782
3854
|
const session = object(item) as BrokerSession | undefined;
|
|
3783
3855
|
if (session?.sessionId !== id) continue;
|
|
3784
3856
|
if (typeof session.locator?.cwd !== "string" || session.locator.cwd !== canonicalCwd)
|
|
3785
|
-
throw new
|
|
3857
|
+
throw new AcpSessionScopeConflictError(`Broker returned conflicting session scope for ${id}.`);
|
|
3786
3858
|
matches.push(session);
|
|
3787
3859
|
}
|
|
3788
|
-
if (matches.length > 1) throw new
|
|
3860
|
+
if (matches.length > 1) throw new AcpSessionScopeConflictError(`Broker returned duplicate session id: ${id}`);
|
|
3789
3861
|
return matches[0];
|
|
3790
3862
|
}
|
|
3791
3863
|
|
|
@@ -3794,7 +3866,7 @@ export class AcpAgent implements Agent {
|
|
|
3794
3866
|
const existing = this.#sessions.get(id);
|
|
3795
3867
|
if (existing) {
|
|
3796
3868
|
if (path.resolve(existing.cwd) !== path.resolve(cwd))
|
|
3797
|
-
throw new
|
|
3869
|
+
throw new AcpSessionScopeConflictError(`ACP session ${id} has conflicting cwd authority.`);
|
|
3798
3870
|
if (existing.primaryControlSurface === "sdk") await existing.adapter.ensureProviders();
|
|
3799
3871
|
return;
|
|
3800
3872
|
}
|
|
@@ -3805,7 +3877,7 @@ export class AcpAgent implements Agent {
|
|
|
3805
3877
|
const attached = this.#sessions.get(id);
|
|
3806
3878
|
if (!attached) throw new AcpSdkAdapterError("unavailable", `ACP session ${id} did not attach.`);
|
|
3807
3879
|
if (path.resolve(attached.cwd) !== path.resolve(cwd))
|
|
3808
|
-
throw new
|
|
3880
|
+
throw new AcpSessionScopeConflictError(`ACP session ${id} has conflicting cwd authority.`);
|
|
3809
3881
|
return;
|
|
3810
3882
|
}
|
|
3811
3883
|
|
|
@@ -4645,35 +4717,47 @@ export class AcpAgent implements Agent {
|
|
|
4645
4717
|
|
|
4646
4718
|
async #reattachSession(id: string, cwd: string): Promise<void> {
|
|
4647
4719
|
if (this.#disposed || this.#knownSessionCwds.get(id) !== cwd) return;
|
|
4720
|
+
const epoch = this.#sessionEpoch(id);
|
|
4648
4721
|
const mcpServers = this.#knownSessionMcpServers.get(id) ?? [];
|
|
4649
4722
|
try {
|
|
4650
4723
|
await this.#attachExisting(id, cwd, mcpServers);
|
|
4651
4724
|
const current = this.#sessions.get(id);
|
|
4652
4725
|
if (current) void this.#publishPromptPhase(id, current.adapter, this.#promptPhaseOwner(current));
|
|
4653
4726
|
} catch (attachError) {
|
|
4654
|
-
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
new AcpSdkAdapterError(
|
|
4660
|
-
"session_closed",
|
|
4661
|
-
`ACP session ${id} lost its SDK session host and could not be reattached (${detail}). ` +
|
|
4662
|
-
"Load it again with session/load or start a new session.",
|
|
4663
|
-
),
|
|
4664
|
-
);
|
|
4665
|
-
try {
|
|
4666
|
-
await this.#connection.sessionUpdate({
|
|
4667
|
-
sessionId: id,
|
|
4668
|
-
update: {
|
|
4669
|
-
sessionUpdate: "session_info_update",
|
|
4670
|
-
_meta: { gjcRecoverFailed: true, gjcRecoverError: detail },
|
|
4671
|
-
},
|
|
4672
|
-
});
|
|
4673
|
-
} catch {}
|
|
4727
|
+
logger.warn(`ACP session ${id} auto-reattach after transport loss failed: ${String(attachError)}`);
|
|
4728
|
+
// A close, delete or disposal that started meanwhile now owns the session: it is not a
|
|
4729
|
+
// recovery failure. Same predicate as the in-place re-attach's `closing`.
|
|
4730
|
+
if (this.#disposed || this.#tearingDown.has(id) || this.#sessionEpoch(id) !== epoch) return;
|
|
4731
|
+
await this.#reportReattachFailed(id, cwd, attachError);
|
|
4674
4732
|
}
|
|
4675
4733
|
}
|
|
4676
4734
|
|
|
4735
|
+
/**
|
|
4736
|
+
* The session lost its host and cannot be re-established: later prompts report `session_closed`
|
|
4737
|
+
* with the session/load guidance, and the client is told through `gjcRecoverFailed`.
|
|
4738
|
+
*/
|
|
4739
|
+
async #reportReattachFailed(id: string, cwd: string, attachError: unknown): Promise<void> {
|
|
4740
|
+
const detail = attachError instanceof Error ? attachError.message : String(attachError);
|
|
4741
|
+
if (!this.#sessions.has(id) && this.#knownSessionCwds.get(id) === cwd)
|
|
4742
|
+
this.#unrecoverableSessions.set(
|
|
4743
|
+
id,
|
|
4744
|
+
new AcpSdkAdapterError(
|
|
4745
|
+
"session_closed",
|
|
4746
|
+
`ACP session ${id} lost its SDK session host and could not be reattached (${detail}). ` +
|
|
4747
|
+
"Load it again with session/load or start a new session.",
|
|
4748
|
+
),
|
|
4749
|
+
);
|
|
4750
|
+
try {
|
|
4751
|
+
await this.#connection.sessionUpdate({
|
|
4752
|
+
sessionId: id,
|
|
4753
|
+
update: {
|
|
4754
|
+
sessionUpdate: "session_info_update",
|
|
4755
|
+
_meta: { gjcRecoverFailed: true, gjcRecoverError: detail },
|
|
4756
|
+
},
|
|
4757
|
+
});
|
|
4758
|
+
} catch {}
|
|
4759
|
+
}
|
|
4760
|
+
|
|
4677
4761
|
async #discardNewSession(id: string): Promise<void> {
|
|
4678
4762
|
await this.#teardownSession(id, "discarded", true);
|
|
4679
4763
|
this.#knownSessionCwds.delete(id);
|
|
@@ -4849,6 +4933,104 @@ export class AcpAgent implements Agent {
|
|
|
4849
4933
|
} catch {}
|
|
4850
4934
|
}
|
|
4851
4935
|
|
|
4936
|
+
/**
|
|
4937
|
+
* The Router attached a new endpoint generation for an idle session: possibly another process,
|
|
4938
|
+
* whose lifecycle ownership and provider rules the record does not know. A prompt in flight keeps
|
|
4939
|
+
* the old rebind, so its own settlement and recovery paths stay as they are.
|
|
4940
|
+
*/
|
|
4941
|
+
#ownerEndpointChanged(record: SessionRecord, attachment: SessionAttachment): boolean {
|
|
4942
|
+
return (
|
|
4943
|
+
attachment.generation !== record.attachment.generation && this.#idleForReattach(attachment.sessionId, record)
|
|
4944
|
+
);
|
|
4945
|
+
}
|
|
4946
|
+
|
|
4947
|
+
/**
|
|
4948
|
+
* Whether an idle session may be re-attached in place. Temporary sessions keep their own
|
|
4949
|
+
* recovery (`ephemeral_session_unrecoverable`); a prompt in flight, an admission waiting for one,
|
|
4950
|
+
* or a first-turn retry in its backoff (#5574) keep the session busy.
|
|
4951
|
+
*/
|
|
4952
|
+
#idleForReattach(id: string, record: SessionRecord): boolean {
|
|
4953
|
+
return (
|
|
4954
|
+
!this.#ephemeralSessionIds.has(id) &&
|
|
4955
|
+
!record.activePrompt &&
|
|
4956
|
+
!record.pendingPromptAdmission &&
|
|
4957
|
+
!record.pendingFirstPromptRetry &&
|
|
4958
|
+
!this.#reattachingSessions.has(id)
|
|
4959
|
+
);
|
|
4960
|
+
}
|
|
4961
|
+
|
|
4962
|
+
/**
|
|
4963
|
+
* Re-attach an idle session to its current owner through the broker without giving it up first
|
|
4964
|
+
* (session single-owner design §13 H1). The record leaves the session table only while the new
|
|
4965
|
+
* attachment is built, and comes back unchanged when that fails (the owner is still starting, for
|
|
4966
|
+
* example), so the next prompt or attachment can recover it. `true` once the session runs on the
|
|
4967
|
+
* new attachment. A prompt arriving meanwhile waits for the outcome.
|
|
4968
|
+
*/
|
|
4969
|
+
async #reattachInPlace(id: string, record: SessionRecord): Promise<boolean> {
|
|
4970
|
+
if (this.#disposed || this.#sessions.get(id) !== record || !this.#idleForReattach(id, record)) return false;
|
|
4971
|
+
const cwd = record.cwd;
|
|
4972
|
+
const mcpServers = this.#knownSessionMcpServers.get(id);
|
|
4973
|
+
const owned = this.#ownedSessionIds.has(id);
|
|
4974
|
+
const epoch = this.#sessionEpoch(id);
|
|
4975
|
+
const done = Promise.withResolvers<void>();
|
|
4976
|
+
this.#reattachingSessions.set(id, done.promise);
|
|
4977
|
+
this.#sessions.delete(id);
|
|
4978
|
+
try {
|
|
4979
|
+
await this.#attachExisting(id, cwd, mcpServers ?? []);
|
|
4980
|
+
const current = this.#sessions.get(id);
|
|
4981
|
+
if (!current) throw new AcpSdkAdapterError("unavailable", `ACP session ${id} did not attach.`);
|
|
4982
|
+
if (record.firstPromptDone) current.firstPromptDone = true;
|
|
4983
|
+
await this.#retireReplacedRecord(record, current);
|
|
4984
|
+
return true;
|
|
4985
|
+
} catch (error) {
|
|
4986
|
+
logger.warn(`ACP session ${id} could not re-attach to its new owner yet: ${String(error)}`);
|
|
4987
|
+
const current = this.#sessions.get(id);
|
|
4988
|
+
if (current) {
|
|
4989
|
+
await this.#retireReplacedRecord(record, current);
|
|
4990
|
+
return true;
|
|
4991
|
+
}
|
|
4992
|
+
// A close, delete or disposal that ran meanwhile owns the session now: never resurrect it.
|
|
4993
|
+
const closing = this.#disposed || this.#tearingDown.has(id) || this.#sessionEpoch(id) !== epoch;
|
|
4994
|
+
// The saved session is gone (the broker found no candidate) or this connection's own cwd or
|
|
4995
|
+
// scope authority conflicts: retrying would only fail again, so it is reported closed with
|
|
4996
|
+
// the session/load guidance, as after a lost transport. A migration in progress, a changed
|
|
4997
|
+
// source, an unavailable scope or a host's own conflict answer is transient.
|
|
4998
|
+
const permanent =
|
|
4999
|
+
(error instanceof SavedSessionUnavailableError && error.gone) ||
|
|
5000
|
+
error instanceof AcpSessionScopeConflictError;
|
|
5001
|
+
if (closing || permanent) {
|
|
5002
|
+
record.unsubscribe();
|
|
5003
|
+
record.reconnectUnsubscribe();
|
|
5004
|
+
await record.adapter.close().catch(() => undefined);
|
|
5005
|
+
// A close queued behind this re-attach still needs the session's cwd and ownership.
|
|
5006
|
+
if (!this.#disposed && !this.#tearingDown.has(id)) {
|
|
5007
|
+
this.#knownSessionCwds.set(id, cwd);
|
|
5008
|
+
if (owned) this.#ownedSessionIds.add(id);
|
|
5009
|
+
}
|
|
5010
|
+
if (permanent && !closing) await this.#reportReattachFailed(id, cwd, error);
|
|
5011
|
+
return false;
|
|
5012
|
+
}
|
|
5013
|
+
// Anything else (the new owner still starting, a busy index) is transient: keep the session.
|
|
5014
|
+
this.#sessions.set(id, record);
|
|
5015
|
+
this.#knownSessionCwds.set(id, cwd);
|
|
5016
|
+
if (mcpServers) this.#knownSessionMcpServers.set(id, mcpServers);
|
|
5017
|
+
if (owned) this.#ownedSessionIds.add(id);
|
|
5018
|
+
return false;
|
|
5019
|
+
} finally {
|
|
5020
|
+
if (this.#reattachingSessions.get(id) === done.promise) this.#reattachingSessions.delete(id);
|
|
5021
|
+
done.resolve();
|
|
5022
|
+
}
|
|
5023
|
+
}
|
|
5024
|
+
|
|
5025
|
+
/** Releases an idle record that a re-attachment replaced; its prompts are already settled. */
|
|
5026
|
+
async #retireReplacedRecord(record: SessionRecord, replacement: SessionRecord): Promise<void> {
|
|
5027
|
+
record.unsubscribe();
|
|
5028
|
+
record.reconnectUnsubscribe();
|
|
5029
|
+
if (record.adapter !== replacement.adapter) await record.adapter.close().catch(() => undefined);
|
|
5030
|
+
if (record.attachment !== replacement.attachment)
|
|
5031
|
+
await Promise.resolve(record.attachment.retire?.()).catch(() => undefined);
|
|
5032
|
+
}
|
|
5033
|
+
|
|
4852
5034
|
async #brokerAdapter(): Promise<AcpSdkAdapter> {
|
|
4853
5035
|
return (await this.#brokerConnection()).adapter;
|
|
4854
5036
|
}
|
|
@@ -4925,8 +5107,10 @@ export class AcpAgent implements Agent {
|
|
|
4925
5107
|
cwd,
|
|
4926
5108
|
});
|
|
4927
5109
|
const saved = object(result?.savedSession);
|
|
4928
|
-
if (saved?.id !== id || typeof saved.path !== "string")
|
|
4929
|
-
|
|
5110
|
+
if (saved?.id !== id || typeof saved.path !== "string") {
|
|
5111
|
+
const reason = object(result?.savedSessionOmission)?.reason;
|
|
5112
|
+
throw new SavedSessionUnavailableError(id, typeof reason === "string" ? reason : undefined);
|
|
5113
|
+
}
|
|
4930
5114
|
return saved.path;
|
|
4931
5115
|
}
|
|
4932
5116
|
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The held-session warning (session single-owner design §3.1): a session another process holds is
|
|
3
|
+
* never opened here, and the user can fork it or cancel. When the holder is a broker-launched host,
|
|
4
|
+
* the user can also continue here (design §12, §13 L1), which closes that host between turns. One
|
|
5
|
+
* copy and one option list for every surface that shows it: the launch before the TUI starts, the
|
|
6
|
+
* resume picker and `/resume`.
|
|
7
|
+
*/
|
|
8
|
+
import { type SessionHeldError, sessionHeldWarning } from "../../session/session-ownership";
|
|
9
|
+
import { HookSelectorComponent } from "./hook-selector";
|
|
10
|
+
|
|
11
|
+
export type HeldSessionChoice = "fork" | "continue" | "cancel";
|
|
12
|
+
|
|
13
|
+
export interface HeldSessionWarningOptions {
|
|
14
|
+
/** Offer Continue here: the holder is a broker-launched session host. */
|
|
15
|
+
readonly continueHere?: boolean;
|
|
16
|
+
/** Why the last Continue here did not go through, shown under the warning. */
|
|
17
|
+
readonly note?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Shown while the warning waits for a launched session host to publish (design §13 H3). */
|
|
21
|
+
export const SESSION_HOST_WAITING_MESSAGE = "Waiting for the session host…";
|
|
22
|
+
|
|
23
|
+
const FORK_OPTION = "Fork into a new session — copy the conversation and continue there";
|
|
24
|
+
const CONTINUE_OPTION = "Continue here — close the session host and open it here";
|
|
25
|
+
const CANCEL_OPTION = "Cancel";
|
|
26
|
+
|
|
27
|
+
/** The selector title (Markdown, as `HookSelectorComponent` renders it) and its options, Fork first. */
|
|
28
|
+
export function heldSessionWarningDialog(
|
|
29
|
+
error: SessionHeldError,
|
|
30
|
+
options: HeldSessionWarningOptions = {},
|
|
31
|
+
): {
|
|
32
|
+
readonly title: string;
|
|
33
|
+
readonly options: string[];
|
|
34
|
+
} {
|
|
35
|
+
const warning = sessionHeldWarning(error);
|
|
36
|
+
return {
|
|
37
|
+
title: [`**${warning.heading}**`, ...warning.lines, ...(options.note ? ["", options.note] : [])].join("\n"),
|
|
38
|
+
options: [FORK_OPTION, ...(options.continueHere ? [CONTINUE_OPTION] : []), CANCEL_OPTION],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The choice a selected option means; anything else (Escape, a closed dialog) cancels. */
|
|
43
|
+
export function heldSessionChoice(option: string | undefined): HeldSessionChoice {
|
|
44
|
+
if (option === FORK_OPTION) return "fork";
|
|
45
|
+
return option === CONTINUE_OPTION ? "continue" : "cancel";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The warning as a focused selector: Enter on Fork forks, Enter on Cancel or Escape cancels. */
|
|
49
|
+
export function createHeldSessionWarning(
|
|
50
|
+
error: SessionHeldError,
|
|
51
|
+
onChoice: (choice: HeldSessionChoice) => void,
|
|
52
|
+
options: HeldSessionWarningOptions = {},
|
|
53
|
+
): HookSelectorComponent {
|
|
54
|
+
const dialog = heldSessionWarningDialog(error, options);
|
|
55
|
+
return new HookSelectorComponent(
|
|
56
|
+
dialog.title,
|
|
57
|
+
dialog.options,
|
|
58
|
+
option => onChoice(heldSessionChoice(option)),
|
|
59
|
+
() => onChoice("cancel"),
|
|
60
|
+
{ acceleratorMap: { f: FORK_OPTION, h: CONTINUE_OPTION, c: CANCEL_OPTION } },
|
|
61
|
+
);
|
|
62
|
+
}
|