ccdeck 3.82.3 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/LICENSING.md +42 -4
  2. package/README.md +183 -72
  3. package/THIRD_PARTY_NOTICES.md +243 -17
  4. package/bin/cli/login-item.js +5 -0
  5. package/bin/cli/tui.js +58 -4
  6. package/bin/deck.js +27 -1
  7. package/dist/web/assets/AccountBridge-OQHT5qmS.js +1 -0
  8. package/dist/web/assets/AccountSection-BpIHPhF3.js +1 -0
  9. package/dist/web/assets/BrowserWatchModal-DBdt52v7.js +1 -0
  10. package/dist/web/assets/FeedbackConversations-BJjgXI0-.js +1 -0
  11. package/dist/web/assets/IntegrationModal-DQ0vEz4X.js +7 -0
  12. package/dist/web/assets/IntegrationSkeleton-JCPbqp96.js +1 -0
  13. package/dist/web/assets/RadioPanel-Be6AkrKl.js +1 -0
  14. package/dist/web/assets/StageShell-Dq0JOQhd.js +4 -0
  15. package/dist/web/assets/TrafficRadar-7jqJbhaK.js +8 -0
  16. package/dist/web/assets/UsageHistoryModal-ckawZGoe.js +1 -0
  17. package/dist/web/assets/addon-unicode-graphemes-DW3WJwyh.js +18 -0
  18. package/dist/web/assets/addon-web-links-BWjmmSgS.js +12 -0
  19. package/dist/web/assets/addon-webgl-CLK4R-_u.js +75 -0
  20. package/dist/web/assets/app-40gNTTfq.js +361 -0
  21. package/dist/web/assets/app-BRjYF6oD.css +1 -0
  22. package/dist/web/assets/{index-B4c5MZCc.js → index-XIY3yFiN.js} +1 -1
  23. package/dist/web/assets/inspector-status-pane-BV_m7eBv.js +1 -0
  24. package/dist/web/assets/screens-9_ivqfcF.js +12 -0
  25. package/dist/web/assets/screens-DUniTTbC.css +1 -0
  26. package/dist/web/assets/stage-board-NF_wgGRA.js +2 -0
  27. package/dist/web/assets/use-account-BUmI-A8e.js +1 -0
  28. package/dist/web/assets/xterm-4jYizCpg.js +27 -0
  29. package/dist/web/assets/xterm-sheet-6GBZ9nXN.css +32 -0
  30. package/dist/web/index.html +16 -6
  31. package/package.json +13 -1
  32. package/release-notes.json +79 -0
  33. package/src/server/account-client.mjs +196 -0
  34. package/src/server/account-connect.mjs +238 -0
  35. package/src/server/account-flag.mjs +106 -0
  36. package/src/server/account-projects.mjs +1 -1
  37. package/src/server/account-service.mjs +532 -0
  38. package/src/server/account-status.mjs +293 -0
  39. package/src/server/account-store.mjs +137 -0
  40. package/src/server/account-sync.mjs +473 -0
  41. package/src/server/account-token-store.mjs +216 -0
  42. package/src/server/account-ui-fields.mjs +126 -0
  43. package/src/server/account-watch.mjs +1 -1
  44. package/src/server/browser-watch-log.mjs +1 -1
  45. package/src/server/browser-watch-store.mjs +1 -1
  46. package/src/server/ccdeck-account-routes.mjs +252 -0
  47. package/src/server/ccusage-install.mjs +2 -2
  48. package/src/server/ccusage-registry.mjs +4 -4
  49. package/src/server/ccusage-session.mjs +11 -0
  50. package/src/server/claude-accounts.mjs +1 -1
  51. package/src/server/claude-recovery.mjs +9 -4
  52. package/src/server/claude-settings-catalog.mjs +6 -6
  53. package/src/server/claude-settings.mjs +1 -1
  54. package/src/server/codex-auto.mjs +1 -1
  55. package/src/server/codex-cost-usage.mjs +7 -2
  56. package/src/server/codex-live-checkpoint.mjs +24 -9
  57. package/src/server/codex-live-engine.mjs +14 -4
  58. package/src/server/codex-live-handoff.mjs +53 -9
  59. package/src/server/codex-names.mjs +49 -0
  60. package/src/server/codex-translate.mjs +1 -1
  61. package/src/server/codex-ultrafast.d.mts +3 -0
  62. package/src/server/codex-ultrafast.mjs +5 -0
  63. package/src/server/codex-watch.mjs +25 -0
  64. package/src/server/command-panel-engine.mjs +300 -0
  65. package/src/server/command-panel-platform.mjs +99 -0
  66. package/src/server/cswap-auto-choice.mjs +1 -1
  67. package/src/server/cswap-auto-loop.mjs +1 -1
  68. package/src/server/cswap-install.mjs +21 -40
  69. package/src/server/deck-home.mjs +6 -0
  70. package/src/server/deck-prefs.mjs +53 -1
  71. package/src/server/detach.mjs +1 -1
  72. package/src/server/engine-download.mjs +277 -0
  73. package/src/server/engine-install.mjs +378 -0
  74. package/src/server/engine-package.mjs +267 -0
  75. package/src/server/event-log.mjs +7 -1
  76. package/src/server/event-pipeline.mjs +29 -1
  77. package/src/server/ghostty-handoff.mjs +81 -0
  78. package/src/server/index.mjs +143 -5
  79. package/src/server/installer.mjs +4 -1
  80. package/src/server/integrations/jira-write-guard.mjs +14 -208
  81. package/src/server/integrations/store.mjs +37 -19
  82. package/src/server/integrations/write-guard.mjs +223 -0
  83. package/src/server/intel-cpu-temperature.mjs +26 -0
  84. package/src/server/lifecycle.mjs +69 -6
  85. package/src/server/log-replay.mjs +4 -0
  86. package/src/server/login-service.mjs +116 -14
  87. package/src/server/macmon.mjs +1 -1
  88. package/src/server/npm-latest.mjs +1 -1
  89. package/src/server/org-telemetry.mjs +1 -1
  90. package/src/server/output-watch.mjs +4 -1
  91. package/src/server/panel-provenance.mjs +390 -0
  92. package/src/server/pinned-build.mjs +14 -0
  93. package/src/server/prefs-refusal.mjs +8 -1
  94. package/src/server/prefs-routes.mjs +60 -11
  95. package/src/server/pro-engine-client.mjs +339 -0
  96. package/src/server/pro-engine-install.mjs +294 -0
  97. package/src/server/pro-engine-terminal.mjs +308 -0
  98. package/src/server/project-discovery.mjs +139 -0
  99. package/src/server/provider-session-name.mjs +58 -0
  100. package/src/server/quota-closes.mjs +1 -1
  101. package/src/server/quota-history.mjs +1 -1
  102. package/src/server/reports.mjs +86 -21
  103. package/src/server/request-gates.mjs +34 -5
  104. package/src/server/restart-note.mjs +1 -1
  105. package/src/server/seed-boundary.mjs +489 -0
  106. package/src/server/session-counts.mjs +244 -0
  107. package/src/server/session-deck-names.mjs +143 -0
  108. package/src/server/session-enrichment.mjs +43 -6
  109. package/src/server/session-name-route.mjs +73 -0
  110. package/src/server/session-summary.mjs +12 -1
  111. package/src/server/session-transcript-routes.mjs +167 -0
  112. package/src/server/swap-log.mjs +1 -1
  113. package/src/server/system-metrics.mjs +57 -0
  114. package/src/server/terminal-bridge.mjs +485 -0
  115. package/src/server/terminal-folder.mjs +61 -0
  116. package/src/server/terminal-js-routes.mjs +752 -0
  117. package/src/server/terminal-routes.mjs +553 -0
  118. package/src/server/terminal-sessions.mjs +759 -0
  119. package/src/server/terminal-socket.mjs +403 -0
  120. package/src/server/terminal-target.mjs +928 -0
  121. package/src/server/thermal-metrics.mjs +13 -9
  122. package/src/server/tmux-control.mjs +1319 -0
  123. package/src/server/tmux-download.mjs +220 -0
  124. package/src/server/tmux.mjs +542 -0
  125. package/src/server/token-redact.mjs +42 -3
  126. package/src/server/tool-audit-report.mjs +44 -6
  127. package/src/server/tool-audit-routes.mjs +12 -1
  128. package/src/server/tool-audit.mjs +42 -8
  129. package/src/server/traffic-radar-monitor.mjs +1 -1
  130. package/src/server/traffic-radar-permission.mjs +1 -1
  131. package/src/server/transcript-gate.mjs +1 -1
  132. package/src/server/transcript-scan.mjs +15 -7
  133. package/src/server/transcript-turns.mjs +154 -0
  134. package/src/server/tui-network.mjs +1 -0
  135. package/src/server/tui-view.mjs +28 -0
  136. package/src/server/utf8-carry.mjs +159 -0
  137. package/src/server/uv-bootstrap.mjs +2 -2
  138. package/src/server/windows-acl.mjs +158 -0
  139. package/src/server/ws-frames.mjs +690 -0
  140. package/dist/web/assets/BrowserWatchModal-BmRTh_xT.js +0 -1
  141. package/dist/web/assets/IntegrationModal-C_f526aV.js +0 -7
  142. package/dist/web/assets/RadioPanel-D2rkf9cg.js +0 -1
  143. package/dist/web/assets/TrafficRadar-BOgVBLIN.js +0 -8
  144. package/dist/web/assets/UsageHistoryModal-BJPTFwM9.js +0 -1
  145. package/dist/web/assets/app-CbXmr9rC.css +0 -1
  146. package/dist/web/assets/app-DZdZoqJ6.js +0 -358
@@ -0,0 +1,390 @@
1
+ // Which sessions the Command Panel started, as this deck recorded it (F6-R).
2
+ //
3
+ // On Windows a session the panel launched can be out of this deck's reach —
4
+ // running under another Windows sign-in, behind another session host — and
5
+ // the engine answers case C for it, which the deck's own sentence calls
6
+ // "started outside the deck". The page knew better only while the tab that
7
+ // launched it stayed open (panel-launch.ts): a reload, another tab or a deck
8
+ // restart lost it. This is the deck's own record of those launches, kept
9
+ // where the deck already keeps what it knows about its sessions: events.jsonl,
10
+ // as its own internal events, replayed at boot like every other line.
11
+ //
12
+ // WHAT IS RECORDED. One event, `__panel_launch`, at these moments:
13
+ // - "started": before the engine is asked to launch, or to reopen a session
14
+ // (adopt) — the provider, the page's launch key, a reference for the
15
+ // answer, and for an adopt the session's id;
16
+ // - "bound": once the engine has said which session that is — a launch's
17
+ // own answer naming one (Claude), a claim it confirmed (Codex), an adopt
18
+ // it ran;
19
+ // - "refused": the engine answered that it started nothing;
20
+ // - "held": every live record at once, written now and then (REFRESH_EVERY)
21
+ // so a restart's replay, which reads only the newest ring's worth of the
22
+ // log, still finds them. Only while the Command Panel is switched on: a
23
+ // deck without it writes nothing of its own here.
24
+ // Ids, providers, launch keys and times: no folder, prompt, output, account or
25
+ // environment. A Clear forgets every record, as it forgets the sessions: what
26
+ // the log held before it is not written back. The id is never where a hook puts `session_id`, so nothing
27
+ // that keys off a session — the LRU, the transcript scans, the log election,
28
+ // the board — takes the deck's note for the session speaking.
29
+ //
30
+ // WHO MAY WRITE IT. Only this deck, from its own routes' answers. The name
31
+ // starts `__`: `POST /api/event` refuses it (isReservedEventName) and the
32
+ // boot replay honours it only from the server's own source, so an event is
33
+ // read as a record only when its source is "internal".
34
+ //
35
+ // WHAT IT ANSWERS. For a session, one of three:
36
+ // - "panel": a bound record;
37
+ // - "external": no record, and the deck saw the session start at a time
38
+ // its records cover in full (`since`), outside every launch that never
39
+ // said which session it became;
40
+ // - "unknown": anything else — a start the deck did not see, one before the
41
+ // records begin, one inside such a launch, an id started again after its
42
+ // record was bound, a session another deck logs, an id that is not one.
43
+ // It is never asked whether a session may be attached, opened or reopened:
44
+ // the engine decides that, and the routes act exactly as they did without it.
45
+ //
46
+ // HOW LONG. A record goes when its session definitively ends (its SessionEnd),
47
+ // never because this deck cannot reach it. Past LAUNCHES_MAX records, or
48
+ // LAUNCH_MAX_AGE_MS old, the oldest goes and `since` moves past its start, so
49
+ // a session started before then becomes "unknown", never "external".
50
+ import { randomUUID } from "node:crypto";
51
+ import { terminalOn } from "./deck-prefs.mjs";
52
+ import { isForeignSession } from "./event-log.mjs";
53
+ import { pushEvent } from "./event-sink.mjs";
54
+ import { heldPrefs } from "./prefs-state.mjs";
55
+ import { MAX_BUFFER, isEnrichment } from "./ring-bounds.mjs";
56
+
57
+ /** The deck's own event name for these records. Reserved: see isReservedEventName. */
58
+ export const PANEL_LAUNCH = "__panel_launch";
59
+ /** The three answers. */
60
+ export const PROVENANCE = Object.freeze(["panel", "external", "unknown"]);
61
+
62
+ /** Live records kept at once. A deck has a handful of panel sessions running. */
63
+ export const LAUNCHES_MAX = 128;
64
+ /** Session starts remembered, for "external". */
65
+ export const STARTS_MAX = 2048;
66
+ /** Launches waiting for the engine to say which session they are. */
67
+ export const PENDING_MAX = 32;
68
+ /** Spans of launches that never said, kept for "unknown". */
69
+ export const GAPS_MAX = 64;
70
+ /** How long a launch may take to say which session it is — a Codex is claimed
71
+ * within seconds of appearing on the board — before its span is a gap. */
72
+ export const PENDING_MS = 10 * 60_000;
73
+ /** The oldest a record may be. A Codex writes no SessionEnd. */
74
+ export const LAUNCH_MAX_AGE_MS = 30 * 24 * 60 * 60_000;
75
+ /** Hook events between two "held" records: a quarter of the replay window. */
76
+ export const REFRESH_EVERY = Math.floor(MAX_BUFFER / 4);
77
+
78
+ const PROVIDERS = new Set(["claude", "codex"]);
79
+ const SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
80
+ const LAUNCH_KEY = /^[A-Za-z0-9_-]{1,96}$/;
81
+ const REF = SESSION_ID;
82
+
83
+ const isId = (v) => typeof v === "string" && SESSION_ID.test(v);
84
+ const isKey = (v) => typeof v === "string" && LAUNCH_KEY.test(v);
85
+ const isTime = (v) => Number.isFinite(v) && v > 0;
86
+ const slot = (provider, id) => `${provider}:${id.toLowerCase()}`;
87
+ const providerOf = (raw) => (raw?.provider === "codex" ? "codex" : "claude");
88
+
89
+ /** Drop a Map's oldest entries until it holds `max`, handing each to `gone`. */
90
+ function trim(map, max, gone = () => {}) {
91
+ while (map.size > max) {
92
+ const [k, v] = map.entries().next().value;
93
+ map.delete(k);
94
+ gone(k, v);
95
+ }
96
+ }
97
+
98
+ /**
99
+ * One deck's record. `emit` sends an event into the pipeline (pushEvent);
100
+ * `foreign` says another deck logs a session; `now` is the clock; `active`
101
+ * says the Command Panel is switched on (the routes' own switch).
102
+ */
103
+ export function createPanelProvenance({
104
+ emit = (raw) => pushEvent(raw, "internal"),
105
+ foreign = isForeignSession,
106
+ now = Date.now,
107
+ active = () => terminalOn(heldPrefs.current()),
108
+ } = {}) {
109
+ /** slot → { provider, session, at, state: "panel" | "unknown", started } */
110
+ const launches = new Map();
111
+ /** slot → when the deck last saw that session start */
112
+ const starts = new Map();
113
+ /** ref → { provider, at, key?, session? } */
114
+ const pending = new Map();
115
+ /** { provider, from, to } */
116
+ let gaps = [];
117
+ /** From when the records are whole: a replayed "held" says, else this
118
+ * deck's own start, before which no launch of its can have been made. */
119
+ const bootAt = now();
120
+ let since = null;
121
+ let heard = 0;
122
+ let held = false;
123
+ let refreshQueued = false;
124
+
125
+ function moveSince(at) {
126
+ since = Math.max(since ?? bootAt, at);
127
+ }
128
+
129
+ function addRecord(provider, session, at, state, started) {
130
+ const k = slot(provider, session);
131
+ launches.delete(k);
132
+ launches.set(k, { provider, session: session.toLowerCase(), at, state, started });
133
+ trim(launches, LAUNCHES_MAX, (gk, r) => forget(gk, r));
134
+ }
135
+
136
+ /** A record that leaves for room or age: whatever started before it is no longer covered. */
137
+ function forget(k, r) {
138
+ const s = starts.get(k);
139
+ moveSince(Math.max(r.at, s ?? 0) + 1);
140
+ }
141
+
142
+ function addGap(provider, from, to) {
143
+ gaps.push({ provider, from, to });
144
+ while (gaps.length > GAPS_MAX) moveSince(gaps.shift().to + 1);
145
+ }
146
+
147
+ function close(ref) {
148
+ const p = pending.get(ref);
149
+ pending.delete(ref);
150
+ return p ?? null;
151
+ }
152
+
153
+ /** Launches past PENDING_MS: a launch becomes a gap, an adopt makes its session unknown. */
154
+ function expire(at) {
155
+ for (const [ref, p] of pending) {
156
+ if (at - p.at <= PENDING_MS) continue;
157
+ pending.delete(ref);
158
+ if (p.session) addRecord(p.provider, p.session, p.at, "unknown", true);
159
+ else addGap(p.provider, p.at, p.at + PENDING_MS);
160
+ }
161
+ for (const [k, r] of launches) {
162
+ if (at - r.at <= LAUNCH_MAX_AGE_MS) break;
163
+ launches.delete(k);
164
+ forget(k, r);
165
+ }
166
+ }
167
+
168
+ function bind(raw, at) {
169
+ const provider = raw.provider;
170
+ const session = raw.session;
171
+ if (!PROVIDERS.has(provider) || !isId(session)) return;
172
+ let p = REF.test(raw.ref ?? "") ? close(raw.ref) : null;
173
+ if (!p && isKey(raw.key)) {
174
+ for (const [ref, q] of pending) {
175
+ if (q.key === raw.key && q.provider === provider) { p = close(ref); break; }
176
+ }
177
+ }
178
+ const from = p?.at ?? at;
179
+ const k = slot(provider, session);
180
+ const s = starts.get(k);
181
+ addRecord(provider, session, at, "panel", s !== undefined && s >= from);
182
+ }
183
+
184
+ function start(raw, at) {
185
+ if (!isId(raw.session_id)) return;
186
+ // A cleared conversation goes on in the same process under a new id: who
187
+ // started that process is not known from here. A compaction is the same
188
+ // session going on.
189
+ if (raw.source === "clear" || raw.source === "compact") return;
190
+ const provider = providerOf(raw);
191
+ const k = slot(provider, raw.session_id);
192
+ const r = launches.get(k);
193
+ if (r) {
194
+ const reopening = [...pending.values()].some((p) => p.session && slot(p.provider, p.session) === k);
195
+ // The id started again after the panel's own start: somebody resumed
196
+ // it, and only an adopt in flight says that somebody was this deck.
197
+ if (r.started && !reopening) r.state = "unknown";
198
+ else r.started = true;
199
+ }
200
+ starts.delete(k);
201
+ starts.set(k, at);
202
+ trim(starts, STARTS_MAX);
203
+ }
204
+
205
+ function end(raw) {
206
+ if (!isId(raw.session_id)) return;
207
+ const k = slot(providerOf(raw), raw.session_id);
208
+ launches.delete(k);
209
+ starts.delete(k);
210
+ }
211
+
212
+ /** A Clear: every session before it is forgotten, so is every record of one. */
213
+ function cleared(at) {
214
+ launches.clear();
215
+ starts.clear();
216
+ moveSince(at);
217
+ held = false;
218
+ heard = 0;
219
+ }
220
+
221
+ function restore(raw) {
222
+ launches.clear();
223
+ pending.clear();
224
+ gaps = [];
225
+ since = isTime(raw.since) ? raw.since : since;
226
+ for (const r of Array.isArray(raw.launches) ? raw.launches.slice(-LAUNCHES_MAX) : []) {
227
+ if (!r || !PROVIDERS.has(r.provider) || !isId(r.session) || !isTime(r.at)) continue;
228
+ addRecord(r.provider, r.session, r.at, r.state === "unknown" ? "unknown" : "panel", r.started === true);
229
+ }
230
+ for (const p of Array.isArray(raw.pending) ? raw.pending.slice(-PENDING_MAX) : []) {
231
+ if (!p || !REF.test(p.ref ?? "") || !PROVIDERS.has(p.provider) || !isTime(p.at)) continue;
232
+ pending.set(p.ref, { provider: p.provider, at: p.at, ...(isKey(p.key) ? { key: p.key } : {}), ...(isId(p.session) ? { session: p.session } : {}) });
233
+ }
234
+ for (const g of Array.isArray(raw.gaps) ? raw.gaps.slice(-GAPS_MAX) : []) {
235
+ if (g && PROVIDERS.has(g.provider) && isTime(g.from) && isTime(g.to)) gaps.push({ provider: g.provider, from: g.from, to: g.to });
236
+ }
237
+ }
238
+
239
+ function record(raw, at) {
240
+ switch (raw.launch) {
241
+ case "started":
242
+ if (!REF.test(raw.ref ?? "") || !PROVIDERS.has(raw.provider)) return;
243
+ pending.delete(raw.ref);
244
+ pending.set(raw.ref, {
245
+ provider: raw.provider, at,
246
+ ...(isKey(raw.key) ? { key: raw.key } : {}),
247
+ ...(isId(raw.session) ? { session: raw.session } : {}),
248
+ });
249
+ trim(pending, PENDING_MAX, (_, p) => (p.session ? addRecord(p.provider, p.session, p.at, "unknown", true) : addGap(p.provider, p.at, p.at + PENDING_MS)));
250
+ return;
251
+ case "bound": bind(raw, at); return;
252
+ case "refused": if (REF.test(raw.ref ?? "")) close(raw.ref); return;
253
+ case "held": restore(raw); return;
254
+ default: return;
255
+ }
256
+ }
257
+
258
+ function snapshot() {
259
+ return {
260
+ hook_event_name: PANEL_LAUNCH,
261
+ launch: "held",
262
+ since,
263
+ launches: [...launches.values()].map((r) => ({ ...r })),
264
+ pending: [...pending.entries()].map(([ref, p]) => ({ ref, ...p })),
265
+ gaps: gaps.map((g) => ({ ...g })),
266
+ };
267
+ }
268
+
269
+ /** "held" now, once the event that asked for it has gone through. */
270
+ function refreshSoon() {
271
+ if (refreshQueued) return;
272
+ refreshQueued = true;
273
+ queueMicrotask(() => {
274
+ refreshQueued = false;
275
+ heard = 0;
276
+ held = true;
277
+ if (since === null) since = bootAt;
278
+ emit(snapshot());
279
+ });
280
+ }
281
+
282
+ return {
283
+ /**
284
+ * Every event on its way into the pipeline (pushEvent), the boot replay's
285
+ * included, at the time it was received.
286
+ */
287
+ observe(raw, source, at, opts = {}) {
288
+ if (!raw || typeof raw !== "object" || !isTime(at)) return;
289
+ const name = raw.hook_event_name;
290
+ if (name === PANEL_LAUNCH) {
291
+ if (source === "internal") record(raw, at);
292
+ return;
293
+ }
294
+ if (name === "__clear") {
295
+ // Nothing is written into the log a Clear just emptied: the next live
296
+ // event writes what is left.
297
+ if (source === "internal") cleared(at);
298
+ return;
299
+ }
300
+ expire(at);
301
+ if (name === "SessionStart" && (source === "hook" || source === "codex")) start(raw, at);
302
+ else if (name === "SessionEnd" && source === "hook") end(raw);
303
+ if (opts.replay) return;
304
+ // The first live event after a boot carries the records forward; after
305
+ // that, every REFRESH_EVERY hook events do. With the panel off, none
306
+ // does, until it is switched on again.
307
+ if (isEnrichment(raw)) return;
308
+ if (!active()) { held = false; return; }
309
+ heard += 1;
310
+ if (!held || heard >= REFRESH_EVERY) refreshSoon();
311
+ },
312
+
313
+ /** Before the engine is asked to launch or to reopen: a reference for its answer. */
314
+ started(method, question) {
315
+ if (!question || typeof question !== "object") return null;
316
+ const provider = method === "launch" ? question.agent : question.provider;
317
+ if (!PROVIDERS.has(provider)) return null;
318
+ if (method === "adopt" && !isId(question.sessionId)) return null;
319
+ const ref = randomUUID();
320
+ emit({
321
+ hook_event_name: PANEL_LAUNCH, launch: "started", ref, provider,
322
+ ...(isKey(question.key) ? { key: question.key } : {}),
323
+ ...(method === "adopt" ? { session: question.sessionId.toLowerCase() } : {}),
324
+ });
325
+ return ref;
326
+ },
327
+
328
+ /**
329
+ * The engine's answer to `method`: a record where it says which session
330
+ * this deck's panel started. Nothing else is ever read from it.
331
+ */
332
+ answered(method, question, ref, status, body) {
333
+ const ok = status >= 200 && status < 300 && body && typeof body === "object" && body.ok === true;
334
+ if (method === "launch") {
335
+ if (!ref) return;
336
+ if (!ok) { emit({ hook_event_name: PANEL_LAUNCH, launch: "refused", ref }); return; }
337
+ // A Codex has not chosen its id yet: its claim binds it.
338
+ if (isId(body.sessionId)) emit({ hook_event_name: PANEL_LAUNCH, launch: "bound", ref, provider: question.agent, session: body.sessionId.toLowerCase() });
339
+ return;
340
+ }
341
+ if (method === "adopt") {
342
+ if (!ref) return;
343
+ if (!ok) { emit({ hook_event_name: PANEL_LAUNCH, launch: "refused", ref }); return; }
344
+ emit({ hook_event_name: PANEL_LAUNCH, launch: "bound", ref, provider: question.provider, session: question.sessionId.toLowerCase() });
345
+ return;
346
+ }
347
+ if (method === "claim" && ok && body.claimed === true && PROVIDERS.has(question?.provider) && isId(question?.sessionId)) {
348
+ emit({
349
+ hook_event_name: PANEL_LAUNCH, launch: "bound", provider: question.provider, session: question.sessionId.toLowerCase(),
350
+ ...(isKey(question.key) ? { key: question.key } : {}),
351
+ });
352
+ }
353
+ },
354
+
355
+ /** "panel", "external" or "unknown" for one session. */
356
+ classify(provider, sessionId) {
357
+ if (!PROVIDERS.has(provider) || !isId(sessionId)) return "unknown";
358
+ expire(now());
359
+ const k = slot(provider, sessionId);
360
+ const r = launches.get(k);
361
+ if (r) return r.state === "panel" ? "panel" : "unknown";
362
+ if (foreign(sessionId)) return "unknown";
363
+ const s = starts.get(k);
364
+ if (s === undefined || s < (since ?? bootAt)) return "unknown";
365
+ for (const p of pending.values()) {
366
+ if (p.provider === provider && !p.session && p.at <= s) return "unknown";
367
+ }
368
+ for (const g of gaps) {
369
+ if (g.provider === provider && s >= g.from && s <= g.to) return "unknown";
370
+ }
371
+ return "external";
372
+ },
373
+
374
+ /** The "held" record, for the suite. */
375
+ snapshot,
376
+ };
377
+ }
378
+
379
+ /** This deck's. */
380
+ export const panelProvenance = createPanelProvenance();
381
+
382
+ /**
383
+ * `body` with its target's provenance, when the target is case C for a
384
+ * session the question named. Every other answer is handed back unchanged.
385
+ */
386
+ export function withProvenance(provenance, question, body) {
387
+ const target = body && typeof body === "object" ? body.target : null;
388
+ if (!provenance || !target || typeof target !== "object" || target.kind !== "C") return body;
389
+ return { ...body, target: { ...target, provenance: provenance.classify(question?.provider, question?.sessionId) } };
390
+ }
@@ -121,6 +121,20 @@ const PINNED_MODULES = [
121
121
  "cswap-install.mjs",
122
122
  ];
123
123
 
124
+ /**
125
+ * Reached only through `import()` as well, and deliberately NOT pinned: the
126
+ * hidden ccdeck account's routes, and everything they import. A deck with the
127
+ * account off must never evaluate a line of them (account-flag.mjs,
128
+ * account-flag-off.test.ts), so they cannot be on the list above, which every
129
+ * deck loads. A deck with the account on loads them at boot instead —
130
+ * index.mjs startAccountWhenOn, right after the listen, as early as the pin
131
+ * itself — so they are in memory before any install can start. The one case
132
+ * left is the account switched on at runtime by a hand edit of prefs.json,
133
+ * which loads them on the first request after it, as any lazy module was
134
+ * loaded before #1042. self-update-pins-build.test.ts names this list.
135
+ */
136
+ export const PINNED_AT_BOOT_WHEN_ON = Object.freeze(["ccdeck-account-routes.mjs"]);
137
+
124
138
  let _pinned = null;
125
139
 
126
140
  /**
@@ -28,9 +28,15 @@ export function unreadablePrefs(path, why, blocked) {
28
28
  * often a settings folder a `sudo` run left owned by root (#1335). */
29
29
  const NOT_WRITABLE = new Set(["EACCES", "EPERM", "EROFS"]);
30
30
 
31
+ /** The errors a filesystem raises when there is no room left for the write: the
32
+ * disk is full, or this user's quota on it is. The machine's to fix as much
33
+ * as a folder this user may not write, and no owner or folder the detail
34
+ * could name would help, so prefsRefusalDetail has none for them. */
35
+ const NO_SPACE = new Set(["ENOSPC", "EDQUOT"]);
36
+
31
37
  /**
32
38
  * Why a settings write failed, as a reason the panel can name — or null when
33
- * the failure is not one of the two a person can fix from outside the deck.
39
+ * the failure is not one of the three a person can fix from outside the deck.
34
40
  *
35
41
  * A code, never the message: the message carries the absolute path, and a
36
42
  * route's body is readable by a DNS-rebound page (see sendInternalError). The
@@ -39,6 +45,7 @@ const NOT_WRITABLE = new Set(["EACCES", "EPERM", "EROFS"]);
39
45
  export function prefsWriteRefusal(err) {
40
46
  if (err?.code === "PREFS_UNREADABLE") return "prefs_unreadable";
41
47
  if (NOT_WRITABLE.has(err?.code)) return "prefs_not_writable";
48
+ if (NO_SPACE.has(err?.code)) return "prefs_no_space";
42
49
  return null;
43
50
  }
44
51
 
@@ -7,13 +7,17 @@
7
7
  // engine had (lan-deck.mjs): what is left of them is a body read, a write
8
8
  // through the one, a push to the other, and an answer. The notifier stays in
9
9
  // index.mjs, beside the desktop-app connections it notifies through.
10
- import { notificationsOn, notificationsVetoed, pagePatch, prefsRefusalDetail, prefsWriteRefusal, publicPrefs, reportsVetoed } from "./deck-prefs.mjs";
10
+ import { notificationsOn, notificationsVetoed, pagePatch, prefsRefusalDetail, prefsWriteRefusal, publicPrefs, reportsVetoed, terminalOn } from "./deck-prefs.mjs";
11
11
  import { PRODUCT } from "./brand.mjs";
12
12
  import { heldPrefs } from "./prefs-state.mjs";
13
13
  import { applyLanPrefs, forgetReach, resetLanLoaded } from "./lan-deck.mjs";
14
14
  import { giveBackDeckFolders } from "./prefs-give-back.mjs";
15
15
  import { readBody, send } from "./http-io.mjs";
16
16
  import { noteContinueSwitchedOn } from "./stop-continue.mjs";
17
+ import { closeOpenTerminals } from "./terminal-socket.mjs";
18
+ import { commandPanel, commandPanelSupported } from "./command-panel-engine.mjs";
19
+ import { panelInSessionHost } from "./command-panel-platform.mjs";
20
+ import { accountOn, accountRequested, accountVetoed } from "./account-flag.mjs";
17
21
 
18
22
  /**
19
23
  * GET the deck's settings, and what the machine is allowing.
@@ -28,7 +32,7 @@ import { noteContinueSwitchedOn } from "./stop-continue.mjs";
28
32
  * was somebody else's doing. `notificationsVetoed` answers only the machine's
29
33
  * half.
30
34
  */
31
- function prefsPayload() {
35
+ function prefsPayload(panel) {
32
36
  const prefs = heldPrefs.current();
33
37
  return {
34
38
  ok: true,
@@ -40,15 +44,43 @@ function prefsPayload() {
40
44
  notificationsAllowed: notificationsOn(prefs),
41
45
  notificationsVetoed: notificationsVetoed(),
42
46
  reportsVetoed: reportsVetoed(),
47
+ // The Command Panel's switch: the page renders no panel at all, and
48
+ // never fetches its code, unless this is true. It is here rather than on
49
+ // a route of its own because this is the one read every page already
50
+ // makes at boot — and a route of its own would have to exist with the
51
+ // switch off, which is the state it reports.
52
+ terminalOn: terminalOn(prefs),
53
+ // Whether this deck's platform has the panel at all (Linux, macOS and Windows), so
54
+ // Settings can say so rather than offer a switch that leads nowhere. The
55
+ // deck's platform, not the browser's: the panel runs where the deck does.
56
+ // `panel` is startServer's one answer for this deck
57
+ // (command-panel-platform.mjs); a caller with none asks the platform, as this always did.
58
+ terminalSupported: panel ? panel.supported === true : commandPanelSupported(),
59
+ // Whether the panel's sessions run in the Windows session host rather than
60
+ // tmux — on Windows, whenever the panel is supported — so the page's words
61
+ // name the place they keep running in.
62
+ terminalWindows: panelInSessionHost(panel),
63
+ // The ccdeck account, on the read every page already makes at boot: the
64
+ // page draws Settings › Account, and fetches its code, only when this says
65
+ // so. A route of its own would have to exist with the flag off, which is
66
+ // the state it reports. `accountVetoed` only for a deck somebody asked to
67
+ // have one — the switch or the variable — so a machine launched with
68
+ // AGENTS_DECK_NO_INSTALL and no account in mind is not shown a section
69
+ // about a feature that is hidden.
70
+ accountOn: accountOn(prefs),
71
+ accountVetoed: accountRequested(prefs) && accountVetoed(),
43
72
  };
44
73
  }
45
74
 
46
- export function handlePrefsRead(req, res) {
47
- return send(res, 200, prefsPayload());
75
+ /** What a panel that was open says once the switch in Settings has closed it. */
76
+ export const TERMINAL_TURNED_OFF = "The Command Panel was turned off in Settings. The session is still running.";
77
+
78
+ export function handlePrefsRead(req, res, panel) {
79
+ return send(res, 200, prefsPayload(panel));
48
80
  }
49
81
 
50
82
  /** POST a patch. Fields nobody sent keep their value — see writePrefs. */
51
- export async function handlePrefsWrite(req, res) {
83
+ export async function handlePrefsWrite(req, res, panel) {
52
84
  const raw = await readBody(req, res).catch(() => null);
53
85
  let body = null;
54
86
  try { body = JSON.parse(raw ?? ""); } catch { /* handled below */ }
@@ -57,12 +89,14 @@ export async function handlePrefsWrite(req, res) {
57
89
  // reports switch (#1853), the reporter's state, the accounts the deck signed
58
90
  // in (#1893) — are the deck's own and keep what is on disk; see pagePatch.
59
91
  const patch = pagePatch(body);
92
+ const terminalWasOn = terminalOn(heldPrefs.current());
60
93
  try {
61
94
  await heldPrefs.write(patch);
62
95
  } catch (err) {
63
- // A settings file or folder this user cannot reach is the machine's to fix,
64
- // not the deck's, and `guard`'s bare 500 left the panel with nothing to say
65
- // about it (#1335). Anything else is still a bug and still goes to `guard`.
96
+ // A settings file or folder this user cannot reach, or a disk with no room
97
+ // left for it, is the machine's to fix, not the deck's, and `guard`'s bare
98
+ // 500 left the panel with nothing to say about it (#1335). Anything else is
99
+ // still a bug and still goes to `guard`.
66
100
  const reason = prefsWriteRefusal(err);
67
101
  if (!reason) throw err;
68
102
  console.error(`${PRODUCT}: settings were not saved:`, err?.message ?? err);
@@ -71,6 +105,21 @@ export async function handlePrefsWrite(req, res) {
71
105
  const detail = prefsRefusalDetail(err);
72
106
  return send(res, 500, detail ? { ok: false, reason, detail } : { ok: false, reason });
73
107
  }
108
+ // Off means no way in, not only no new one: a panel still open in another
109
+ // tab is a write channel into the session, so it is closed with a sentence,
110
+ // at once rather than after the LAN engine has had its turn. Asked of what
111
+ // was written, not of what was sent — any value that is not true turns the
112
+ // switch off. The sessions keep running; closing a panel never stops an
113
+ // agent. The engine that served them stops once they have closed.
114
+ const terminalNowOn = terminalOn(heldPrefs.current());
115
+ if (terminalWasOn && !terminalNowOn) {
116
+ closeOpenTerminals("error", TERMINAL_TURNED_OFF);
117
+ commandPanel.switchedOff();
118
+ } else if (!terminalWasOn && terminalNowOn) {
119
+ // On again before the engine's stop has begun: the stop is called off,
120
+ // and the engine kept with what it holds (command-panel-engine.mjs).
121
+ commandPanel.switchedOn();
122
+ }
74
123
  // The engine reads its settings from here rather than holding its own copy,
75
124
  // so turning the switch off in the panel really does stop the sockets rather
76
125
  // than only changing what the panel says.
@@ -82,7 +131,7 @@ export async function handlePrefsWrite(req, res) {
82
131
  // A stopped background session already waiting for a working login is
83
132
  // looked at now, not at the next pass (stop-continue.mjs).
84
133
  if (patch.continueStopped === true) noteContinueSwitchedOn();
85
- return send(res, 200, prefsPayload());
134
+ return send(res, 200, prefsPayload(panel));
86
135
  }
87
136
 
88
137
  /**
@@ -97,12 +146,12 @@ export async function handlePrefsWrite(req, res) {
97
146
  * the file again — the stored key and pairings — which is what a restart would
98
147
  * have done, without the restart.
99
148
  */
100
- export async function handlePrefsGiveBack(req, res) {
149
+ export async function handlePrefsGiveBack(req, res, panel) {
101
150
  const out = await giveBackDeckFolders();
102
151
  if (!out.ok) return send(res, 409, { ok: false, reason: out.reason });
103
152
  if ((await heldPrefs.reload()) === "file") {
104
153
  resetLanLoaded();
105
154
  await applyLanPrefs();
106
155
  }
107
- return send(res, 200, { ...prefsPayload(), changed: out.changed });
156
+ return send(res, 200, { ...prefsPayload(panel), changed: out.changed });
108
157
  }