@cortex-agent/server 2026.9.14 → 2026.9.15
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/defaults/plugins/cortex-commission/.claude-plugin/plugin.json +2 -2
- package/defaults/plugins/cortex-commission/plugin.json +2 -2
- package/defaults/plugins/cortex-commission/skills/commission/SKILL.md +3 -3
- package/dist/agent-adapter/claude/adapter.d.ts +0 -6
- package/dist/agent-adapter/claude/adapter.js +0 -8
- package/dist/agent-adapter/claude/defaults.d.ts +10 -7
- package/dist/agent-adapter/claude/defaults.js +18 -16
- package/dist/agent-adapter/claude/spawn-args.d.ts +0 -3
- package/dist/agent-adapter/claude/spawn-args.js +16 -16
- package/dist/agent-adapter/pi/mcp-bridge-logic.d.ts +11 -2
- package/dist/agent-adapter/pi/mcp-bridge-logic.js +32 -1
- package/dist/agent-adapter/pi/mcp-bridge.js +16 -15
- package/dist/agent-adapter/pi/session-options.d.ts +0 -6
- package/dist/agent-adapter/pi/session-options.js +1 -8
- package/dist/agent-adapter/types.d.ts +0 -1
- package/dist/core/mcp-tool-gate.d.ts +3 -17
- package/dist/core/mcp-tool-gate.js +3 -19
- package/dist/core/settings-spec.d.ts +1 -1
- package/dist/core/settings-spec.js +5 -1
- package/dist/domain/agents/config.d.ts +1 -1
- package/dist/domain/agents/config.js +1 -1
- package/dist/domain/agents/profile-switch.d.ts +1 -1
- package/dist/domain/agents/profile-switch.js +3 -5
- package/dist/domain/agents/subagent/runner.js +0 -1
- package/dist/domain/commissions/commission-draft.d.ts +58 -0
- package/dist/domain/commissions/commission-draft.js +87 -4
- package/dist/domain/commissions/commission-finalize.d.ts +8 -3
- package/dist/domain/commissions/commission-finalize.js +14 -0
- package/dist/domain/mcp/tools/commission-tools.d.ts +12 -1
- package/dist/domain/mcp/tools/commission-tools.js +68 -14
- package/dist/domain/remote/client-manager.js +66 -7
- package/dist/domain/remote/client-ssh-tunnel.d.ts +16 -5
- package/dist/domain/remote/client-ssh-tunnel.js +12 -5
- package/dist/domain/runs/builders.js +0 -1
- package/dist/domain/runs/compact.js +0 -1
- package/dist/domain/runs/engine-spec.d.ts +11 -4
- package/dist/domain/runs/engine-spec.js +7 -4
- package/dist/domain/runs/prompt.js +16 -10
- package/dist/domain/runs/request.d.ts +0 -1
- package/dist/domain/sessions/session-hooks.d.ts +5 -12
- package/dist/domain/sessions/session-hooks.js +6 -13
- package/dist/domain/sessions/session-lifecycle.d.ts +4 -4
- package/dist/domain/sessions/session-lifecycle.js +5 -6
- package/dist/domain/sessions/session-retention.d.ts +1 -3
- package/dist/domain/sessions/session-retention.js +9 -26
- package/dist/domain/sessions/session-use.d.ts +32 -0
- package/dist/domain/sessions/session-use.js +63 -0
- package/dist/domain/sessions/session.js +6 -5
- package/dist/domain/system/server-update-check.js +29 -1
- package/dist/domain/system/update-ui-state.d.ts +32 -0
- package/dist/domain/system/update-ui-state.js +63 -0
- package/dist/domain/threads/runner.js +0 -1
- package/dist/domain/ui-service/app-router.d.ts +30 -0
- package/dist/domain/ui-service/app-router.js +5 -1
- package/dist/domain/ui-service/input-schemas.d.ts +32 -0
- package/dist/domain/ui-service/input-schemas.js +22 -0
- package/dist/domain/ui-service/mutate/sessions.d.ts +13 -1
- package/dist/domain/ui-service/mutate/sessions.js +69 -3
- package/dist/domain/ui-service/mutate/system.d.ts +3 -1
- package/dist/domain/ui-service/mutate/system.js +13 -0
- package/dist/domain/ui-service/query/system.d.ts +2 -1
- package/dist/domain/ui-service/query/system.js +4 -0
- package/dist/domain/ui-service/types.d.ts +44 -3
- package/dist/domain/ui-service/ui-service.js +7 -3
- package/dist/entry/app.js +11 -23
- package/dist/entry/start-ui-http.js +3 -1
- package/dist/events/event-types.d.ts +5 -0
- package/dist/orchestration/agent-runner.d.ts +1 -1
- package/dist/orchestration/agent-runner.js +8 -11
- package/dist/orchestration/conversation-request.d.ts +26 -8
- package/dist/orchestration/conversation-request.js +49 -20
- package/dist/orchestration/edit-retry.js +2 -1
- package/dist/orchestration/interactions/ask-user-resume.js +2 -1
- package/dist/orchestration/interactions/ui-update-prompt.d.ts +8 -0
- package/dist/orchestration/interactions/ui-update-prompt.js +47 -0
- package/dist/orchestration/resume-dispatcher.js +2 -2
- package/dist/orchestration/routing/webhook.js +47 -0
- package/dist/orchestration/session-compact.d.ts +3 -0
- package/dist/orchestration/session-compact.js +3 -2
- package/dist/orchestration/session-events.d.ts +6 -0
- package/dist/orchestration/session-events.js +5 -0
- package/dist/orchestration/turn/turn-prep.js +2 -1
- package/dist/platform/ui-http/app-update.d.ts +11 -0
- package/dist/platform/ui-http/app-update.js +3 -0
- package/dist/store/conversation-ledger-repo.d.ts +10 -19
- package/dist/store/conversation-ledger-repo.js +143 -152
- package/dist/store/session-registry-import.d.ts +34 -0
- package/dist/store/session-registry-import.js +182 -0
- package/dist/store/session-registry-journal.d.ts +107 -0
- package/dist/store/session-registry-journal.js +307 -18
- package/dist/store/session-registry-repo.d.ts +113 -28
- package/dist/store/session-registry-repo.js +287 -104
- package/dist/store/version-migrations.d.ts +16 -0
- package/dist/store/version-migrations.js +34 -0
- package/package.json +1 -1
- package/web/dist/assets/{index-aHbOZSP6.js → index-CEZ0DSei.js} +1 -1
- package/web/dist/assets/{index-CCPxQ94C.css → index-CRDJo39X.css} +1 -1
- package/web/dist/assets/index-DjuPJVZE.js +444 -0
- package/web/dist/index.html +2 -2
- package/dist/store/session-repo.d.ts +0 -65
- package/dist/store/session-repo.js +0 -184
- package/web/dist/assets/index-BuXbCsuz.js +0 -444
|
@@ -37,9 +37,51 @@ export interface SessionRecord {
|
|
|
37
37
|
* has not been named yet. Cleared at finalize, when commissionId is filled in. Mode is ON when
|
|
38
38
|
* either this or commissionId is set. */
|
|
39
39
|
commissionDraft?: string | null;
|
|
40
|
+
/** Which commission binding the `[Commission]` prompt block has already been delivered for —
|
|
41
|
+
* `draft:<dir>` or `active:<commissionId>` (DR-0037 v4). The block used to be a first-turn-only
|
|
42
|
+
* injection, which meant a session that entered the mode (or bound its contract) MID-session
|
|
43
|
+
* never received it at all. Comparing this marker against the current binding is what makes the
|
|
44
|
+
* block follow the state instead of the session's age; compaction clears it, because compaction
|
|
45
|
+
* is what removes the already-delivered copy from backend history. */
|
|
46
|
+
commissionBlockFor?: string | null;
|
|
40
47
|
contextUsage?: unknown;
|
|
41
48
|
browser?: SessionBrowserOption | null;
|
|
42
49
|
}
|
|
50
|
+
/** Copied VERBATIM from store/conversation-ledger-repo.ts:9-22 (B.T1). The journal owns turn history
|
|
51
|
+
* now, but must NOT import the ledger module (dependency direction). Keep this in lock-step with the
|
|
52
|
+
* ledger's LedgerTurn until the ledger is retired in a later task. */
|
|
53
|
+
export type TurnStatus = 'processing' | 'completed' | 'superseded';
|
|
54
|
+
export interface TurnRecord {
|
|
55
|
+
turnIndex: number;
|
|
56
|
+
userMessageTs: string;
|
|
57
|
+
userMessageText: string;
|
|
58
|
+
statusMessageTs: string | null;
|
|
59
|
+
responseMessageTimestamps: string[];
|
|
60
|
+
executionId: string | null;
|
|
61
|
+
backupPath: string | null;
|
|
62
|
+
status: TurnStatus;
|
|
63
|
+
createdAt: string;
|
|
64
|
+
updatedAt: string;
|
|
65
|
+
}
|
|
66
|
+
/** Per-channel conversation header — the identity of the conversation currently hosted on a channel,
|
|
67
|
+
* independent of the live session record. Turns live in `turns` keyed by the same channel. Both are
|
|
68
|
+
* dropped together on `conversation clear` and on `delete-commit` (for every channel bound to the
|
|
69
|
+
* committed session). `sessionId`/`sessionName` are null before the first backend session lands. */
|
|
70
|
+
export interface ConversationHeader {
|
|
71
|
+
sessionId: string | null;
|
|
72
|
+
sessionName: string | null;
|
|
73
|
+
backend: string;
|
|
74
|
+
profileName: string | null;
|
|
75
|
+
updatedAt: string;
|
|
76
|
+
}
|
|
77
|
+
/** Header mutation payload for a `conversation set` event. A key present overwrites; a key absent is
|
|
78
|
+
* left untouched (the reducer merges onto the previous header, or onto a null base for a new one). */
|
|
79
|
+
export type ConversationFields = Partial<ConversationHeader>;
|
|
80
|
+
/** Patch payload: only the record keys a `patch` may carry. `name`/`sessionId` are identity and
|
|
81
|
+
* never travel in a patch (the event keys off them). A key set to `null` means "store null"; a key
|
|
82
|
+
* listed in `unset` is deleted (→ `undefined`); a key in neither is untouched. */
|
|
83
|
+
export type SessionPatchFields = Partial<Omit<SessionRecord, 'name' | 'sessionId'>>;
|
|
84
|
+
export type SessionPatchUnsetKey = keyof SessionPatchFields;
|
|
43
85
|
export type SessionRegistryEvent = {
|
|
44
86
|
v: 1;
|
|
45
87
|
op: 'put';
|
|
@@ -55,11 +97,72 @@ export type SessionRegistryEvent = {
|
|
|
55
97
|
v: 1;
|
|
56
98
|
op: 'delete-commit';
|
|
57
99
|
id: string;
|
|
100
|
+
} | {
|
|
101
|
+
v: 1;
|
|
102
|
+
op: 'patch';
|
|
103
|
+
id: string;
|
|
104
|
+
name: string;
|
|
105
|
+
fields: SessionPatchFields;
|
|
106
|
+
unset?: SessionPatchUnsetKey[];
|
|
107
|
+
} | {
|
|
108
|
+
v: 1;
|
|
109
|
+
op: 'bind';
|
|
110
|
+
channel: string;
|
|
111
|
+
sessionId: string;
|
|
112
|
+
} | {
|
|
113
|
+
v: 1;
|
|
114
|
+
op: 'unbind';
|
|
115
|
+
channel: string;
|
|
116
|
+
} | {
|
|
117
|
+
v: 1;
|
|
118
|
+
op: 'turn';
|
|
119
|
+
channel: string;
|
|
120
|
+
kind: 'begin';
|
|
121
|
+
turn: TurnRecord;
|
|
122
|
+
} | {
|
|
123
|
+
v: 1;
|
|
124
|
+
op: 'turn';
|
|
125
|
+
channel: string;
|
|
126
|
+
kind: 'patch';
|
|
127
|
+
turnIndex: number;
|
|
128
|
+
fields: Partial<TurnRecord>;
|
|
129
|
+
} | {
|
|
130
|
+
v: 1;
|
|
131
|
+
op: 'turn';
|
|
132
|
+
channel: string;
|
|
133
|
+
kind: 'truncate';
|
|
134
|
+
fromIndex: number;
|
|
135
|
+
} | {
|
|
136
|
+
v: 1;
|
|
137
|
+
op: 'turn';
|
|
138
|
+
channel: string;
|
|
139
|
+
kind: 'clear';
|
|
140
|
+
} | {
|
|
141
|
+
v: 1;
|
|
142
|
+
op: 'conversation';
|
|
143
|
+
channel: string;
|
|
144
|
+
kind: 'set';
|
|
145
|
+
fields: ConversationFields;
|
|
146
|
+
} | {
|
|
147
|
+
v: 1;
|
|
148
|
+
op: 'conversation';
|
|
149
|
+
channel: string;
|
|
150
|
+
kind: 'clear';
|
|
58
151
|
};
|
|
59
152
|
export interface SessionRegistryState {
|
|
60
153
|
live: Map<string, SessionRecord>;
|
|
61
154
|
pending: Map<string, PendingSessionDelete>;
|
|
62
155
|
nameIndex: Map<string, string>;
|
|
156
|
+
/** channel → sessionId of the conversation session hosted on that channel (an explicit index; a
|
|
157
|
+
* channel can host several sessions but binds to one at a time). A free map: bind never checks
|
|
158
|
+
* liveness (an unknown or pending session id may be bound). Cleared on delete-commit. */
|
|
159
|
+
bindings: Map<string, string>;
|
|
160
|
+
/** channel → ordered turn history for the conversation hosted on that channel. Independent of
|
|
161
|
+
* liveness; dropped on `conversation clear` and on delete-commit (for every bound channel). */
|
|
162
|
+
turns: Map<string, TurnRecord[]>;
|
|
163
|
+
/** channel → conversation header (session identity of the hosted conversation). Dropped with the
|
|
164
|
+
* turns on `conversation clear` and on delete-commit (for every bound channel). */
|
|
165
|
+
conversations: Map<string, ConversationHeader>;
|
|
63
166
|
eventCount: number;
|
|
64
167
|
fileSize: number;
|
|
65
168
|
}
|
|
@@ -81,3 +184,7 @@ export declare function shouldCompactSessionRegistry(state: SessionRegistryState
|
|
|
81
184
|
export declare function loadSessionRegistryState(filePath: string, options?: SessionRegistryJournalOptions): Promise<SessionRegistryState>;
|
|
82
185
|
export declare function appendSessionRegistryEvent(filePath: string, state: SessionRegistryState, event: SessionRegistryEvent, options?: SessionRegistryJournalOptions): Promise<void>;
|
|
83
186
|
export declare function compactSessionRegistry(filePath: string, state: SessionRegistryState, options?: SessionRegistryJournalOptions): Promise<void>;
|
|
187
|
+
/** Every key a `patch` may carry, derived from the exhaustive reader table so the WRITER (repo
|
|
188
|
+
* `diffRecord`) and the READER can never disagree: adding a field to SessionRecord without a
|
|
189
|
+
* reader is a compile error above, and the writer picks it up from here automatically. */
|
|
190
|
+
export declare const PATCH_KEYS: SessionPatchUnsetKey[];
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// input: fs, readline, STORE_DIR, and append/compact journal callers
|
|
2
2
|
// output: JSONL session registry replay, append, compact, and migration helpers
|
|
3
|
-
//
|
|
3
|
+
// (records + channel bindings + per-CHANNEL conversation header & turn history;
|
|
4
|
+
// events: put/patch/delete-*/bind/unbind/turn/conversation)
|
|
5
|
+
// pos: Low-level journal I/O for session registry state — the single owner of session identity
|
|
4
6
|
// >>> Once I am updated, be sure to update my header comment and the parent folder CORTEX.md <<<
|
|
5
7
|
import fs from 'node:fs';
|
|
6
8
|
import fsp from 'node:fs/promises';
|
|
@@ -17,6 +19,9 @@ export function createSessionRegistryState() {
|
|
|
17
19
|
live: new Map(),
|
|
18
20
|
pending: new Map(),
|
|
19
21
|
nameIndex: new Map(),
|
|
22
|
+
bindings: new Map(),
|
|
23
|
+
turns: new Map(),
|
|
24
|
+
conversations: new Map(),
|
|
20
25
|
eventCount: 0,
|
|
21
26
|
fileSize: 0,
|
|
22
27
|
};
|
|
@@ -42,7 +47,8 @@ export function shouldCompactSessionRegistry(state, options) {
|
|
|
42
47
|
};
|
|
43
48
|
if (options.shouldCompact)
|
|
44
49
|
return options.shouldCompact(stats);
|
|
45
|
-
|
|
50
|
+
// Lowered from 2048 (B.T1): patch/turn ticks are cheap, so compact sooner to keep the tail small.
|
|
51
|
+
const threshold = Math.max(512, state.live.size * 4);
|
|
46
52
|
return state.fileSize > MAX_FILE_BYTES || state.eventCount > threshold;
|
|
47
53
|
}
|
|
48
54
|
export async function loadSessionRegistryState(filePath, options = {}) {
|
|
@@ -66,10 +72,25 @@ export async function compactSessionRegistry(filePath, state, options = {}) {
|
|
|
66
72
|
options.onCompact?.();
|
|
67
73
|
}
|
|
68
74
|
function compactedEvents(state) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
75
|
+
const events = [];
|
|
76
|
+
for (const record of state.live.values())
|
|
77
|
+
events.push(putEvent(record));
|
|
78
|
+
for (const entry of state.pending.values())
|
|
79
|
+
events.push(deleteIntentEvent(entry.session, entry.cleanup));
|
|
80
|
+
// Bindings, conversation headers and turns are channel-keyed and re-materialised as-is: bind is a
|
|
81
|
+
// free map (not filtered by liveness), and headers/turns outlive the session record they name.
|
|
82
|
+
// Order: puts → delete-intents → binds → conversation headers → turns.
|
|
83
|
+
for (const [channel, sessionId] of state.bindings) {
|
|
84
|
+
events.push({ v: 1, op: 'bind', channel, sessionId });
|
|
85
|
+
}
|
|
86
|
+
for (const [channel, header] of state.conversations) {
|
|
87
|
+
events.push({ v: 1, op: 'conversation', channel, kind: 'set', fields: header });
|
|
88
|
+
}
|
|
89
|
+
for (const [channel, turns] of state.turns) {
|
|
90
|
+
for (const turn of turns)
|
|
91
|
+
events.push({ v: 1, op: 'turn', channel, kind: 'begin', turn });
|
|
92
|
+
}
|
|
93
|
+
return events;
|
|
73
94
|
}
|
|
74
95
|
function putEvent(record) {
|
|
75
96
|
return { v: 1, op: 'put', id: record.sessionId, record };
|
|
@@ -230,23 +251,85 @@ function normalizeEvent(raw, lineNo) {
|
|
|
230
251
|
const row = raw;
|
|
231
252
|
const version = row?.v;
|
|
232
253
|
const op = row?.op;
|
|
233
|
-
const id = row?.id;
|
|
234
254
|
if (version !== REGISTRY_VERSION)
|
|
235
255
|
throw new Error(`Unknown session registry version at line ${lineNo}`);
|
|
236
|
-
if (
|
|
237
|
-
|
|
238
|
-
if (op === 'put')
|
|
256
|
+
if (op === 'put') {
|
|
257
|
+
const id = requireId(row, lineNo);
|
|
239
258
|
return { v: 1, op, id, record: assertSessionRecord(row.record, id) };
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
};
|
|
259
|
+
}
|
|
260
|
+
if (op === 'delete-intent') {
|
|
261
|
+
const id = requireId(row, lineNo);
|
|
262
|
+
return { v: 1, op, id, record: assertSessionRecord(row.record, id), cleanup: normalizeDeleteCleanup(row.cleanup) };
|
|
263
|
+
}
|
|
246
264
|
if (op === 'delete-commit')
|
|
247
|
-
return { v: 1, op, id };
|
|
265
|
+
return { v: 1, op, id: requireId(row, lineNo) };
|
|
266
|
+
if (op === 'patch')
|
|
267
|
+
return normalizePatchEvent(row, lineNo);
|
|
268
|
+
if (op === 'bind')
|
|
269
|
+
return normalizeBindEvent(row, lineNo);
|
|
270
|
+
if (op === 'unbind')
|
|
271
|
+
return { v: 1, op: 'unbind', channel: requireField(row, 'channel', lineNo) };
|
|
272
|
+
if (op === 'conversation')
|
|
273
|
+
return normalizeConversationEvent(row, lineNo);
|
|
274
|
+
if (op === 'turn')
|
|
275
|
+
return normalizeTurnEvent(row, lineNo);
|
|
248
276
|
throw new Error(`Unknown session registry op at line ${lineNo}`);
|
|
249
277
|
}
|
|
278
|
+
function requireId(row, lineNo) {
|
|
279
|
+
const id = row?.id;
|
|
280
|
+
if (typeof id !== 'string' || !id)
|
|
281
|
+
throw new Error(`Malformed session registry id at line ${lineNo}`);
|
|
282
|
+
return id;
|
|
283
|
+
}
|
|
284
|
+
function requireField(row, key, lineNo) {
|
|
285
|
+
const value = row?.[key];
|
|
286
|
+
if (typeof value !== 'string' || !value)
|
|
287
|
+
throw new Error(`Malformed session registry ${key} at line ${lineNo}`);
|
|
288
|
+
return value;
|
|
289
|
+
}
|
|
290
|
+
function normalizePatchEvent(row, lineNo) {
|
|
291
|
+
const id = requireId(row, lineNo);
|
|
292
|
+
const name = requireField(row, 'name', lineNo);
|
|
293
|
+
return { v: 1, op: 'patch', id, name, fields: assertPatchFields(row.fields), ...normalizeUnset(row.unset, lineNo) };
|
|
294
|
+
}
|
|
295
|
+
function normalizeUnset(raw, lineNo) {
|
|
296
|
+
if (raw === undefined)
|
|
297
|
+
return {};
|
|
298
|
+
if (!Array.isArray(raw) || raw.some(value => !PATCH_KEY_SET.has(value))) {
|
|
299
|
+
throw new Error(`Invalid session registry patch unset at line ${lineNo}`);
|
|
300
|
+
}
|
|
301
|
+
return raw.length ? { unset: raw } : {};
|
|
302
|
+
}
|
|
303
|
+
function normalizeBindEvent(row, lineNo) {
|
|
304
|
+
return {
|
|
305
|
+
v: 1,
|
|
306
|
+
op: 'bind',
|
|
307
|
+
channel: requireField(row, 'channel', lineNo),
|
|
308
|
+
sessionId: requireField(row, 'sessionId', lineNo),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function normalizeTurnEvent(row, lineNo) {
|
|
312
|
+
const channel = requireField(row, 'channel', lineNo);
|
|
313
|
+
const kind = row?.kind;
|
|
314
|
+
if (kind === 'begin')
|
|
315
|
+
return { v: 1, op: 'turn', channel, kind, turn: assertTurnRecord(row.turn) };
|
|
316
|
+
if (kind === 'patch')
|
|
317
|
+
return { v: 1, op: 'turn', channel, kind, turnIndex: toNumberValue(row.turnIndex), fields: assertPartialTurn(row.fields) };
|
|
318
|
+
if (kind === 'truncate')
|
|
319
|
+
return { v: 1, op: 'turn', channel, kind, fromIndex: toNumberValue(row.fromIndex) };
|
|
320
|
+
if (kind === 'clear')
|
|
321
|
+
return { v: 1, op: 'turn', channel, kind };
|
|
322
|
+
throw new Error(`Unknown session registry turn kind at line ${lineNo}`);
|
|
323
|
+
}
|
|
324
|
+
function normalizeConversationEvent(row, lineNo) {
|
|
325
|
+
const channel = requireField(row, 'channel', lineNo);
|
|
326
|
+
const kind = row?.kind;
|
|
327
|
+
if (kind === 'set')
|
|
328
|
+
return { v: 1, op: 'conversation', channel, kind, fields: assertConversationFields(row.fields) };
|
|
329
|
+
if (kind === 'clear')
|
|
330
|
+
return { v: 1, op: 'conversation', channel, kind };
|
|
331
|
+
throw new Error(`Unknown session registry conversation kind at line ${lineNo}`);
|
|
332
|
+
}
|
|
250
333
|
function normalizeDeleteCleanup(raw) {
|
|
251
334
|
if (raw === undefined)
|
|
252
335
|
return { claudeBackupPaths: [] };
|
|
@@ -278,6 +361,7 @@ function assertSessionRecord(raw, expectedId) {
|
|
|
278
361
|
scheduleId: toOptionalNullableString(row?.scheduleId),
|
|
279
362
|
commissionId: toOptionalNullableString(row?.commissionId),
|
|
280
363
|
commissionDraft: toOptionalNullableString(row?.commissionDraft),
|
|
364
|
+
commissionBlockFor: toOptionalNullableString(row?.commissionBlockFor),
|
|
281
365
|
contextUsage: row?.contextUsage,
|
|
282
366
|
browser: toOptionalBrowserValue(row?.browser),
|
|
283
367
|
};
|
|
@@ -304,6 +388,7 @@ function assertNewFormatRecord(raw, expectedId) {
|
|
|
304
388
|
scheduleId: toOptionalNullableString(row?.scheduleId),
|
|
305
389
|
commissionId: toOptionalNullableString(row?.commissionId),
|
|
306
390
|
commissionDraft: toOptionalNullableString(row?.commissionDraft),
|
|
391
|
+
commissionBlockFor: toOptionalNullableString(row?.commissionBlockFor),
|
|
307
392
|
contextUsage: row?.contextUsage,
|
|
308
393
|
browser: toOptionalBrowserValue(row?.browser),
|
|
309
394
|
};
|
|
@@ -353,6 +438,134 @@ function toOriginValue(value) {
|
|
|
353
438
|
return value;
|
|
354
439
|
throw new Error('Invalid session registry origin');
|
|
355
440
|
}
|
|
441
|
+
function toNumberValue(value) {
|
|
442
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
443
|
+
throw new Error('Invalid session registry number field');
|
|
444
|
+
return value;
|
|
445
|
+
}
|
|
446
|
+
/** Strict nullable string for turn fields: `null` stays null, a string stays a string, anything
|
|
447
|
+
* else (including undefined) is rejected. Unlike `toNullableString`, `''` is preserved. */
|
|
448
|
+
function toTurnNullableString(value) {
|
|
449
|
+
if (value === null)
|
|
450
|
+
return null;
|
|
451
|
+
if (typeof value === 'string')
|
|
452
|
+
return value;
|
|
453
|
+
throw new Error('Invalid session registry turn nullable string');
|
|
454
|
+
}
|
|
455
|
+
function toStringArray(value) {
|
|
456
|
+
if (!Array.isArray(value) || value.some(entry => typeof entry !== 'string')) {
|
|
457
|
+
throw new Error('Invalid session registry string array');
|
|
458
|
+
}
|
|
459
|
+
return [...value];
|
|
460
|
+
}
|
|
461
|
+
function toTurnStatus(value) {
|
|
462
|
+
if (value === 'processing' || value === 'completed' || value === 'superseded')
|
|
463
|
+
return value;
|
|
464
|
+
throw new Error('Invalid session registry turn status');
|
|
465
|
+
}
|
|
466
|
+
/** Whitelist for the `patch` payload. Every field a record can carry (minus identity) has a strict
|
|
467
|
+
* converter here; a key not listed is DROPPED on replay (the toOptionalBrowserValue trap, §8.1). */
|
|
468
|
+
const PATCH_FIELD_READERS = {
|
|
469
|
+
projectId: toStringValue,
|
|
470
|
+
channel: toStringValue,
|
|
471
|
+
backend: toStringValue,
|
|
472
|
+
kind: toKindValue,
|
|
473
|
+
origin: toOriginValue,
|
|
474
|
+
createdAt: toStringValue,
|
|
475
|
+
lastUsedAt: toStringValue,
|
|
476
|
+
label: toNullableString,
|
|
477
|
+
profileName: toNullableString,
|
|
478
|
+
backendSessionId: toOptionalNullableString,
|
|
479
|
+
lastReadAt: toOptionalNullableString,
|
|
480
|
+
scheduleId: toOptionalNullableString,
|
|
481
|
+
commissionId: toOptionalNullableString,
|
|
482
|
+
commissionDraft: toOptionalNullableString,
|
|
483
|
+
commissionBlockFor: toOptionalNullableString,
|
|
484
|
+
contextUsage: value => value,
|
|
485
|
+
browser: toOptionalBrowserValue,
|
|
486
|
+
};
|
|
487
|
+
/** Every key a `patch` may carry, derived from the exhaustive reader table so the WRITER (repo
|
|
488
|
+
* `diffRecord`) and the READER can never disagree: adding a field to SessionRecord without a
|
|
489
|
+
* reader is a compile error above, and the writer picks it up from here automatically. */
|
|
490
|
+
export const PATCH_KEYS = Object.keys(PATCH_FIELD_READERS);
|
|
491
|
+
const PATCH_KEY_SET = new Set(PATCH_KEYS);
|
|
492
|
+
function assertPatchFields(raw) {
|
|
493
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
494
|
+
throw new Error('Invalid session registry patch fields');
|
|
495
|
+
const row = raw;
|
|
496
|
+
const fields = {};
|
|
497
|
+
for (const key of Object.keys(row)) {
|
|
498
|
+
const reader = PATCH_FIELD_READERS[key];
|
|
499
|
+
if (!reader)
|
|
500
|
+
throw new Error(`Unknown session registry patch field: ${key}`);
|
|
501
|
+
fields[key] = reader(row[key]);
|
|
502
|
+
}
|
|
503
|
+
return fields;
|
|
504
|
+
}
|
|
505
|
+
function assertTurnRecord(raw) {
|
|
506
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
507
|
+
throw new Error('Invalid session registry turn record');
|
|
508
|
+
const row = raw;
|
|
509
|
+
return {
|
|
510
|
+
turnIndex: toNumberValue(row.turnIndex),
|
|
511
|
+
userMessageTs: toStringValue(row.userMessageTs),
|
|
512
|
+
userMessageText: toStringValue(row.userMessageText),
|
|
513
|
+
statusMessageTs: toTurnNullableString(row.statusMessageTs),
|
|
514
|
+
responseMessageTimestamps: toStringArray(row.responseMessageTimestamps),
|
|
515
|
+
executionId: toTurnNullableString(row.executionId),
|
|
516
|
+
backupPath: toTurnNullableString(row.backupPath),
|
|
517
|
+
status: toTurnStatus(row.status),
|
|
518
|
+
createdAt: toStringValue(row.createdAt),
|
|
519
|
+
updatedAt: toStringValue(row.updatedAt),
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
const TURN_FIELD_READERS = {
|
|
523
|
+
turnIndex: toNumberValue,
|
|
524
|
+
userMessageTs: toStringValue,
|
|
525
|
+
userMessageText: toStringValue,
|
|
526
|
+
statusMessageTs: toTurnNullableString,
|
|
527
|
+
responseMessageTimestamps: toStringArray,
|
|
528
|
+
executionId: toTurnNullableString,
|
|
529
|
+
backupPath: toTurnNullableString,
|
|
530
|
+
status: toTurnStatus,
|
|
531
|
+
createdAt: toStringValue,
|
|
532
|
+
updatedAt: toStringValue,
|
|
533
|
+
};
|
|
534
|
+
function assertPartialTurn(raw) {
|
|
535
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
536
|
+
throw new Error('Invalid session registry turn patch');
|
|
537
|
+
const row = raw;
|
|
538
|
+
const fields = {};
|
|
539
|
+
for (const key of Object.keys(row)) {
|
|
540
|
+
const reader = TURN_FIELD_READERS[key];
|
|
541
|
+
if (!reader)
|
|
542
|
+
throw new Error(`Unknown session registry turn field: ${key}`);
|
|
543
|
+
fields[key] = reader(row[key]);
|
|
544
|
+
}
|
|
545
|
+
return fields;
|
|
546
|
+
}
|
|
547
|
+
/** Whitelist for the `conversation set` payload. A key not listed is DROPPED on replay (the same
|
|
548
|
+
* toOptionalBrowserValue trap that silently lost fields, §8.1) — keep in lock-step with the header. */
|
|
549
|
+
const CONVERSATION_FIELD_READERS = {
|
|
550
|
+
sessionId: toTurnNullableString,
|
|
551
|
+
sessionName: toTurnNullableString,
|
|
552
|
+
backend: toStringValue,
|
|
553
|
+
profileName: toTurnNullableString,
|
|
554
|
+
updatedAt: toStringValue,
|
|
555
|
+
};
|
|
556
|
+
function assertConversationFields(raw) {
|
|
557
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
558
|
+
throw new Error('Invalid session registry conversation fields');
|
|
559
|
+
const row = raw;
|
|
560
|
+
const fields = {};
|
|
561
|
+
for (const key of Object.keys(row)) {
|
|
562
|
+
const reader = CONVERSATION_FIELD_READERS[key];
|
|
563
|
+
if (!reader)
|
|
564
|
+
throw new Error(`Unknown session registry conversation field: ${key}`);
|
|
565
|
+
fields[key] = reader(row[key]);
|
|
566
|
+
}
|
|
567
|
+
return fields;
|
|
568
|
+
}
|
|
356
569
|
function assertAppendableEvent(state, event) {
|
|
357
570
|
if (event.op === 'put')
|
|
358
571
|
assertPutAllowed(state, event.record);
|
|
@@ -360,6 +573,14 @@ function assertAppendableEvent(state, event) {
|
|
|
360
573
|
assertDeleteIntentAppendable(state, event.record);
|
|
361
574
|
if (event.op === 'delete-commit')
|
|
362
575
|
assertDeleteCommitAllowed(state, event.id);
|
|
576
|
+
if (event.op === 'patch')
|
|
577
|
+
assertPatchAppendable(state, event.id);
|
|
578
|
+
// bind / turn / conversation carry no liveness guard: they are channel-scoped and may name a
|
|
579
|
+
// session that is unknown, pending, or already gone (the free-map contract, §1).
|
|
580
|
+
}
|
|
581
|
+
function assertPatchAppendable(state, sessionId) {
|
|
582
|
+
if (!state.live.has(sessionId))
|
|
583
|
+
throw new Error(`Patch for non-live session: ${sessionId}`);
|
|
363
584
|
}
|
|
364
585
|
function assertPutAllowed(state, record) {
|
|
365
586
|
if (state.pending.has(record.sessionId)) {
|
|
@@ -391,7 +612,66 @@ function applyEvent(state, event) {
|
|
|
391
612
|
return applyPut(state, event.record);
|
|
392
613
|
if (event.op === 'delete-intent')
|
|
393
614
|
return applyDeleteIntent(state, event.record, event.cleanup);
|
|
394
|
-
|
|
615
|
+
if (event.op === 'delete-commit')
|
|
616
|
+
return applyDeleteCommit(state, event.id);
|
|
617
|
+
if (event.op === 'patch')
|
|
618
|
+
return applyPatch(state, event);
|
|
619
|
+
if (event.op === 'bind')
|
|
620
|
+
return applyBind(state, event.channel, event.sessionId);
|
|
621
|
+
if (event.op === 'unbind')
|
|
622
|
+
return applyUnbind(state, event.channel);
|
|
623
|
+
if (event.op === 'conversation')
|
|
624
|
+
return applyConversation(state, event);
|
|
625
|
+
applyTurn(state, event);
|
|
626
|
+
}
|
|
627
|
+
/** Lenient replay: a patch whose session is no longer live (deleted later, or a torn compaction
|
|
628
|
+
* reorder) is skipped rather than thrown — the strict guard lives in the append path. */
|
|
629
|
+
function applyPatch(state, event) {
|
|
630
|
+
const record = state.live.get(event.id);
|
|
631
|
+
if (!record)
|
|
632
|
+
return;
|
|
633
|
+
const next = { ...record, ...event.fields };
|
|
634
|
+
for (const key of event.unset ?? [])
|
|
635
|
+
delete next[key];
|
|
636
|
+
state.live.set(event.id, next);
|
|
637
|
+
}
|
|
638
|
+
/** Free-map bind: never gated on liveness — a channel may bind to an unknown, pending, or already
|
|
639
|
+
* committed session id, and it simply stays until unbind or delete-commit. */
|
|
640
|
+
function applyBind(state, channel, sessionId) {
|
|
641
|
+
state.bindings.set(channel, sessionId);
|
|
642
|
+
}
|
|
643
|
+
function applyUnbind(state, channel) {
|
|
644
|
+
state.bindings.delete(channel);
|
|
645
|
+
}
|
|
646
|
+
/** Channel-keyed turns: independent of the session record. `clear` drops the channel's list. */
|
|
647
|
+
function applyTurn(state, event) {
|
|
648
|
+
const turns = state.turns.get(event.channel) ?? [];
|
|
649
|
+
if (event.kind === 'begin') {
|
|
650
|
+
turns.push(event.turn);
|
|
651
|
+
state.turns.set(event.channel, turns);
|
|
652
|
+
}
|
|
653
|
+
else if (event.kind === 'patch') {
|
|
654
|
+
const idx = turns.findIndex(turn => turn.turnIndex === event.turnIndex);
|
|
655
|
+
if (idx >= 0)
|
|
656
|
+
turns[idx] = { ...turns[idx], ...event.fields };
|
|
657
|
+
}
|
|
658
|
+
else if (event.kind === 'truncate') {
|
|
659
|
+
state.turns.set(event.channel, turns.filter(turn => turn.turnIndex < event.fromIndex));
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
state.turns.delete(event.channel);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
/** Channel-keyed conversation header. `set` merges the payload onto the previous header (or onto a
|
|
666
|
+
* null base if the channel had none); `clear` drops the header. */
|
|
667
|
+
function applyConversation(state, event) {
|
|
668
|
+
if (event.kind === 'clear') {
|
|
669
|
+
state.conversations.delete(event.channel);
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
const base = state.conversations.get(event.channel)
|
|
673
|
+
?? { sessionId: null, sessionName: null, backend: '', profileName: null, updatedAt: '' };
|
|
674
|
+
state.conversations.set(event.channel, { ...base, ...event.fields });
|
|
395
675
|
}
|
|
396
676
|
function applyPut(state, record) {
|
|
397
677
|
const previous = state.live.get(record.sessionId);
|
|
@@ -410,6 +690,15 @@ function applyDeleteIntent(state, record, cleanup = { claudeBackupPaths: [] }) {
|
|
|
410
690
|
function applyDeleteCommit(state, sessionId) {
|
|
411
691
|
assertDeleteCommitAllowed(state, sessionId);
|
|
412
692
|
state.pending.delete(sessionId);
|
|
693
|
+
// A committed session keeps nothing on any channel bound to it: binding, turn history and the
|
|
694
|
+
// conversation header are all dropped together (§1, §3). Unbind alone never touches turns/header.
|
|
695
|
+
for (const [channel, boundId] of state.bindings) {
|
|
696
|
+
if (boundId !== sessionId)
|
|
697
|
+
continue;
|
|
698
|
+
state.bindings.delete(channel);
|
|
699
|
+
state.turns.delete(channel);
|
|
700
|
+
state.conversations.delete(channel);
|
|
701
|
+
}
|
|
413
702
|
}
|
|
414
703
|
async function appendLineWithRollback(filePath, line, writer) {
|
|
415
704
|
const handle = await fsp.open(filePath, 'a+');
|