@gajae-code/coding-agent 0.22.1 → 0.22.2

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.
Files changed (75) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/dist/types/cli/doctor/probe-types.d.ts +1 -1
  3. package/dist/types/cli/doctor/session-owners-probe.d.ts +7 -0
  4. package/dist/types/cli/doctor/session-owners.d.ts +16 -0
  5. package/dist/types/cli/github-release.d.ts +16 -0
  6. package/dist/types/cli/session-owners-cli.d.ts +13 -0
  7. package/dist/types/cli/update-cli.d.ts +2 -0
  8. package/dist/types/commands/sdk.d.ts +23 -14
  9. package/dist/types/config/settings-schema.d.ts +6 -0
  10. package/dist/types/exec/bash-shell-guardian.d.ts +61 -0
  11. package/dist/types/export/html/html-export-destination.d.ts +16 -0
  12. package/dist/types/sdk/bus/telegram-daemon-contract.d.ts +1 -1
  13. package/dist/types/sdk/host/control/runtime-gate.d.ts +7 -0
  14. package/dist/types/session/internal/graph-successor-proof.d.ts +33 -0
  15. package/dist/types/session/internal/managed-session-storage.d.ts +6 -1
  16. package/dist/types/session/internal/session-claim-set.d.ts +69 -0
  17. package/dist/types/session/operator-error-message.d.ts +29 -0
  18. package/dist/types/session/session-manager.d.ts +57 -3
  19. package/dist/types/session/session-owners-layout.d.ts +24 -0
  20. package/dist/types/session/session-ownership-mode.d.ts +45 -0
  21. package/dist/types/session/session-ownership.d.ts +195 -0
  22. package/dist/types/session/session-storage.d.ts +36 -0
  23. package/dist/types/slash-commands/helpers/parse.d.ts +1 -0
  24. package/package.json +12 -8
  25. package/scripts/verify-gjc-sdk-canonicalization.ts +2 -0
  26. package/src/cli/doctor/probe-types.ts +1 -1
  27. package/src/cli/doctor/probe.ts +9 -5
  28. package/src/cli/doctor/runner.ts +6 -1
  29. package/src/cli/doctor/session-owners-probe.ts +69 -0
  30. package/src/cli/doctor/session-owners.ts +106 -0
  31. package/src/cli/github-release.ts +100 -0
  32. package/src/cli/session-owners-cli.ts +118 -0
  33. package/src/cli/stats-cli.ts +19 -1
  34. package/src/cli/update-cli.ts +50 -3
  35. package/src/cli-ordinary.ts +1 -1
  36. package/src/commands/sdk.ts +36 -33
  37. package/src/commands/session.ts +10 -2
  38. package/src/config/settings-schema.ts +18 -0
  39. package/src/config/settings.ts +127 -13
  40. package/src/exec/bash-shell-guardian.ts +181 -12
  41. package/src/exec/isolated-shell.ts +13 -3
  42. package/src/export/html/html-export-destination.ts +47 -2
  43. package/src/export/html/index.ts +161 -13
  44. package/src/internal-urls/docs-index.generated.ts +4 -4
  45. package/src/main.ts +20 -14
  46. package/src/modes/acp/acp-agent.ts +6 -0
  47. package/src/modes/action-registry.ts +4 -1
  48. package/src/modes/controllers/command-controller.ts +12 -5
  49. package/src/modes/controllers/extension-ui-controller.ts +6 -22
  50. package/src/modes/controllers/input-controller.ts +3 -2
  51. package/src/modes/controllers/selector-controller.ts +14 -1
  52. package/src/modes/interactive-mode.ts +6 -3
  53. package/src/modes/print-mode.ts +2 -1
  54. package/src/modes/runtime-init.ts +2 -17
  55. package/src/modes/utils/ui-helpers.ts +5 -3
  56. package/src/sdk/bus/index.ts +22 -0
  57. package/src/sdk/bus/telegram-daemon-contract.ts +1 -1
  58. package/src/sdk/host/control/dispatch.ts +5 -1
  59. package/src/sdk/host/control/runtime-gate.ts +22 -0
  60. package/src/sdk/host/host.ts +4 -1
  61. package/src/sdk/session-readonly/metadata.ts +24 -18
  62. package/src/sdk/session.ts +2 -0
  63. package/src/session/agent-session.ts +101 -22
  64. package/src/session/internal/graph-successor-proof.ts +108 -0
  65. package/src/session/internal/managed-session-storage.ts +8 -1
  66. package/src/session/internal/session-claim-set.ts +242 -0
  67. package/src/session/operator-error-message.ts +79 -0
  68. package/src/session/session-manager.ts +524 -108
  69. package/src/session/session-owners-layout.ts +52 -0
  70. package/src/session/session-ownership-mode.ts +176 -0
  71. package/src/session/session-ownership.ts +851 -0
  72. package/src/session/session-storage.ts +60 -16
  73. package/src/setup/credential-import.ts +2 -1
  74. package/src/slash-commands/builtin-registry.ts +2 -0
  75. package/src/slash-commands/helpers/parse.ts +3 -1
@@ -0,0 +1,242 @@
1
+ /**
2
+ * The per-manager claim set (design §2.5): the session claims one SessionManager holds, keyed by
3
+ * session id, with every release point of the design's table.
4
+ *
5
+ * Bookkeeping only. It never claims; factories and transitions claim, then hand the claim in. It
6
+ * releases a claim only when its id is no longer the manager's current id and no transition is open,
7
+ * so a predecessor stays claimed until its rotation commits, and a rollback that restores the
8
+ * predecessor releases the target instead. With no claim in the set, every operation is a no-op,
9
+ * which is what `off` gives: nothing ever adds a claim.
10
+ */
11
+ import { logger } from "@gajae-code/utils";
12
+ import type { SessionManager } from "../session-manager";
13
+ import { countSessionOwnershipEvent, type SessionOwnerClaim } from "../session-ownership";
14
+ import {
15
+ type SessionOwnershipMode,
16
+ sessionOwnershipMode,
17
+ sessionOwnershipTestHookSlot,
18
+ } from "../session-ownership-mode";
19
+
20
+ /** How long a deferred owner shutdown may keep its predecessor's claim (design §2.5). */
21
+ export const DEFERRED_RELEASE_CAP_MS = 60_000;
22
+
23
+ /** Thrown only under the strict test seam, for a write the detector flags. */
24
+ export class SessionWriteWithoutClaimError extends Error {
25
+ readonly code = "session_write_without_claim";
26
+ readonly site: string;
27
+
28
+ constructor(site: string) {
29
+ super(`session_write_without_claim: ${site}`);
30
+ this.name = "SessionWriteWithoutClaimError";
31
+ this.site = site;
32
+ }
33
+ }
34
+
35
+ interface DeferredHold {
36
+ readonly sessionId: string;
37
+ /** The set that holds the claim now; a manager swap moves holds with their claims. */
38
+ set: SessionClaimSet;
39
+ timer: NodeJS.Timeout | undefined;
40
+ }
41
+
42
+ /**
43
+ * A manager dropped with claims still in its set never released them; the locks stay until the
44
+ * process exits. Count and log it, so the observe phase shows the missed release. The held value is
45
+ * the claims map, which does not reference its set.
46
+ */
47
+ const abandonedSets = new FinalizationRegistry<Map<string, SessionOwnerClaim>>(claims => {
48
+ const left = [...claims.values()].filter(claim => !claim.released).length;
49
+ if (left === 0) return;
50
+ for (let index = 0; index < left; index++) countSessionOwnershipEvent("claims_abandoned");
51
+ logger.warn("Session claims left on an abandoned session manager", { claims: left });
52
+ });
53
+
54
+ export class SessionClaimSet {
55
+ readonly #claims = new Map<string, SessionOwnerClaim>();
56
+ readonly #holds = new Map<string, DeferredHold>();
57
+ #current: string | undefined;
58
+ #transitions = 0;
59
+ #closed = false;
60
+ /** Read once per bind, so a settings reload never changes the mode under a bound session. */
61
+ #mode: SessionOwnershipMode = "off";
62
+ /** Detector reports already logged, as `<id> <site>`: log once, count every time. */
63
+ readonly #reported = new Set<string>();
64
+
65
+ constructor() {
66
+ abandonedSets.register(this, this.#claims);
67
+ }
68
+
69
+ /** The session id the manager's identity helper last bound. */
70
+ get currentSessionId(): string | undefined {
71
+ return this.#current;
72
+ }
73
+
74
+ get sessionIds(): readonly string[] {
75
+ return [...this.#claims.keys()];
76
+ }
77
+
78
+ has(sessionId: string): boolean {
79
+ return this.#claims.has(sessionId);
80
+ }
81
+
82
+ /**
83
+ * The identity helper's record of the manager's id. It never claims and never releases. A change
84
+ * of id outside any transition is counted (when ownership is on): nothing settles after it.
85
+ */
86
+ bind(sessionId: string): void {
87
+ const previous = this.#current;
88
+ this.#current = sessionId;
89
+ this.#mode = sessionOwnershipMode();
90
+ if (this.#mode === "off" || previous === undefined || previous === sessionId || this.#transitions > 0) return;
91
+ countSessionOwnershipEvent("bind_outside_transition");
92
+ logger.debug("Session id changed outside a claim-set transition", { mode: this.#mode });
93
+ }
94
+
95
+ /**
96
+ * The write-path detector (design §2.5), called before a transcript write. When ownership is on
97
+ * and this set holds no claim for `sessionId` (the current id unless a successor is named), it
98
+ * logs `session_write_without_claim` with the call site and counts it. It never claims, and it
99
+ * throws only under the strict test seam. In `off` it returns at once.
100
+ */
101
+ checkWrite(site: string, sessionId: string | undefined = this.#current): void {
102
+ if (this.#mode === "off" || sessionId === undefined || this.#claims.has(sessionId)) return;
103
+ countSessionOwnershipEvent("session_write_without_claim");
104
+ const report = `${sessionId} ${site}`;
105
+ if (!this.#reported.has(report)) {
106
+ this.#reported.add(report);
107
+ logger.warn("session_write_without_claim", { site, sessionId, mode: this.#mode });
108
+ }
109
+ if (sessionOwnershipTestHookSlot().strictWriteDetection) throw new SessionWriteWithoutClaimError(site);
110
+ }
111
+
112
+ /**
113
+ * Take ownership of a claim: a factory's, or a transition's target claimed before the rotation.
114
+ * The key is the id, so a second claim of an id already here is released at once (one claim per id).
115
+ */
116
+ add(claim: SessionOwnerClaim): void {
117
+ if (claim.released) throw new Error("session_claim_released");
118
+ if (this.#claims.has(claim.sessionId) || this.#closed) {
119
+ claim.release();
120
+ return;
121
+ }
122
+ this.#claims.set(claim.sessionId, claim);
123
+ // A target staged outside the transition that rotates to it is exposed to any unrelated
124
+ // transition's settle. Visible in observe; off stays inert.
125
+ if (
126
+ this.#mode !== "off" &&
127
+ this.#current !== undefined &&
128
+ claim.sessionId !== this.#current &&
129
+ this.#transitions === 0
130
+ )
131
+ countSessionOwnershipEvent("target_staged_outside_transition");
132
+ }
133
+
134
+ /**
135
+ * Open a transition: no claim is released until the outermost transition ends. Disposing settles,
136
+ * so the claims kept are exactly those for the id current at the end (rotation committed: the
137
+ * predecessor goes; rolled back or cancelled: the target goes).
138
+ */
139
+ transition(): Disposable {
140
+ this.#transitions += 1;
141
+ let open = true;
142
+ return {
143
+ [Symbol.dispose]: () => {
144
+ if (!open) return;
145
+ open = false;
146
+ this.#transitions -= 1;
147
+ this.settle();
148
+ },
149
+ };
150
+ }
151
+
152
+ /**
153
+ * Release every claim whose id is not current and not held for a deferred shutdown. Runs when the
154
+ * outermost transition ends, so a target added outside any transition stays until the next one
155
+ * settles; stage targets inside the transition that rotates to them.
156
+ */
157
+ settle(): void {
158
+ if (this.#transitions > 0 || this.#current === undefined) return;
159
+ for (const [sessionId, claim] of [...this.#claims]) {
160
+ if (sessionId === this.#current || this.#holds.has(sessionId)) continue;
161
+ this.#claims.delete(sessionId);
162
+ claim.release();
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Deferred owner shutdown: keep `sessionId` while `finalization` runs, at most `capMs`. A hold that
168
+ * reaches the cap releases anyway and counts `release_after_deferred_timeout`.
169
+ */
170
+ holdUntil(sessionId: string, finalization: Promise<unknown>, capMs = DEFERRED_RELEASE_CAP_MS): void {
171
+ if (!this.#claims.has(sessionId) || this.#holds.has(sessionId)) return;
172
+ const hold: DeferredHold = { sessionId, set: this, timer: undefined };
173
+ this.#holds.set(sessionId, hold);
174
+ const end = (timedOut: boolean) => {
175
+ if (hold.set.#holds.get(sessionId) !== hold) return;
176
+ if (hold.timer) clearTimeout(hold.timer);
177
+ hold.set.#holds.delete(sessionId);
178
+ if (timedOut) countSessionOwnershipEvent("release_after_deferred_timeout");
179
+ if (hold.set.#closed) hold.set.#releaseOne(sessionId);
180
+ // Only the held id: any other non-current claim (a target staged before its rotation)
181
+ // waits for the transition that commits or abandons it.
182
+ else if (hold.set.#transitions === 0 && hold.set.#current !== sessionId) hold.set.#releaseOne(sessionId);
183
+ };
184
+ hold.timer = setTimeout(() => end(true), capMs);
185
+ hold.timer.unref();
186
+ finalization.then(
187
+ () => end(false),
188
+ () => end(false),
189
+ );
190
+ }
191
+
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
+ /**
216
+ * `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.
218
+ */
219
+ releaseAll(): void {
220
+ this.#closed = true;
221
+ for (const sessionId of [...this.#claims.keys()]) if (!this.#holds.has(sessionId)) this.#releaseOne(sessionId);
222
+ }
223
+
224
+ #releaseOne(sessionId: string): void {
225
+ const claim = this.#claims.get(sessionId);
226
+ if (!claim) return;
227
+ this.#claims.delete(sessionId);
228
+ claim.release();
229
+ }
230
+ }
231
+
232
+ /** Opaque capability for a manager's claim set. Excluded from package exports with `internal/`. */
233
+ export const sessionClaimSetCapability: unique symbol = Symbol("SessionManager claim set capability");
234
+
235
+ interface ClaimSetOwner {
236
+ [sessionClaimSetCapability](): SessionClaimSet;
237
+ }
238
+
239
+ /** The claim set of a SessionManager, for factories, transitions and tests in this package. */
240
+ export function sessionClaimSetOf(manager: SessionManager): SessionClaimSet {
241
+ return (manager as unknown as ClaimSetOwner)[sessionClaimSetCapability]();
242
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Operator-facing text for errors raised by session operations.
3
+ *
4
+ * Session persistence errors keep a stable machine code in `message` (for example
5
+ * `managed_append_identity_mismatch`) and carry the human explanation, including the
6
+ * recovery, in `operatorMessage`. Every surface that shows such an error to a person
7
+ * must show the operator text; machine fields (`code`) stay as they are.
8
+ */
9
+
10
+ const MAX_CAUSE_DEPTH = 8;
11
+
12
+ function ownOperatorMessage(error: Error): string | undefined {
13
+ const operatorMessage = (error as { operatorMessage?: unknown }).operatorMessage;
14
+ return typeof operatorMessage === "string" && operatorMessage.trim().length > 0 ? operatorMessage : undefined;
15
+ }
16
+
17
+ interface OperatorMessageMatch {
18
+ operatorMessage: string;
19
+ /** The outer error's own message when it reports a different failure than the refusal. */
20
+ wrapperMessage: string | undefined;
21
+ }
22
+
23
+ /**
24
+ * The operator message of `error` or of the nearest error in its `cause` chain. A wrapper
25
+ * that only re-raises the persistence error (same message or the same machine code) has no
26
+ * message of its own; any other wrapper's message is returned separately.
27
+ */
28
+ function matchOperatorMessage(error: unknown): OperatorMessageMatch | undefined {
29
+ if (!(error instanceof Error)) return undefined;
30
+ const own = ownOperatorMessage(error);
31
+ if (own) return { operatorMessage: own, wrapperMessage: undefined };
32
+ let current: unknown = error.cause;
33
+ for (let depth = 1; depth < MAX_CAUSE_DEPTH && current instanceof Error; depth++) {
34
+ const operatorMessage = ownOperatorMessage(current);
35
+ if (operatorMessage) {
36
+ const code = (current as { code?: unknown }).code;
37
+ const outer = error.message.trim();
38
+ const reraise = outer.length === 0 || outer === current.message || outer === code;
39
+ return { operatorMessage, wrapperMessage: reraise ? undefined : outer };
40
+ }
41
+ current = current.cause;
42
+ }
43
+ return undefined;
44
+ }
45
+
46
+ /**
47
+ * Boundary-safe operator text for SDK, ACP and other protocol surfaces: the fixed,
48
+ * path-free `operatorMessage` of `error` or of the refusal it purely re-raises. A wrapper
49
+ * that reports its own failure yields `undefined`, because its message may carry local
50
+ * paths or other detail those surfaces keep opaque.
51
+ */
52
+ export function findOperatorMessage(error: unknown): string | undefined {
53
+ const match = matchOperatorMessage(error);
54
+ return match && match.wrapperMessage === undefined ? match.operatorMessage : undefined;
55
+ }
56
+
57
+ /**
58
+ * Operator text for a person reading a local surface (TUI, print mode, CLI stderr). A
59
+ * wrapper with its own failure, such as a failed rollback or an invariant failure raised
60
+ * while handling the refusal, keeps that message first so the operator text cannot hide
61
+ * the real failure.
62
+ */
63
+ export function composeOperatorMessage(error: unknown): string | undefined {
64
+ const match = matchOperatorMessage(error);
65
+ if (!match) return undefined;
66
+ const outer = match.wrapperMessage;
67
+ if (outer === undefined) return match.operatorMessage;
68
+ return `${outer}${/[.!?]$/.test(outer) ? "" : "."} ${match.operatorMessage}`;
69
+ }
70
+
71
+ /**
72
+ * Text to show a person on a local surface for `error`: its operator message (composed
73
+ * with a wrapper's own failure, see {@link composeOperatorMessage}) when one exists, otherwise
74
+ * `error.message`. A non-Error value renders as `fallback`, so each call site keeps its
75
+ * existing rendering for thrown non-errors.
76
+ */
77
+ export function operatorErrorMessage(error: unknown, fallback = "Unknown error occurred"): string {
78
+ return composeOperatorMessage(error) ?? (error instanceof Error ? error.message : fallback);
79
+ }