@unieai/rabi-api-session-controller 0.0.0-stage → 0.2.2-rc.5
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/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/client.js +4 -0
- package/lib/index.js +4300 -0
- package/lib/typert.host.d.ts +3 -0
- package/lib/typert.host.js +5013 -0
- package/lib/typert.remote-client.d.ts +115 -0
- package/lib/typert.remote-client.js +2714 -0
- package/lib/types/agent.d.ts +164 -0
- package/lib/types/agent.js +484 -0
- package/lib/types/archived-session-gate.d.ts +33 -0
- package/lib/types/archived-session-gate.js +54 -0
- package/lib/types/assistant-stream.d.ts +26 -0
- package/lib/types/assistant-stream.js +82 -0
- package/lib/types/catalog.d.ts +24 -0
- package/lib/types/catalog.js +107 -0
- package/lib/types/client/contract/events.d.ts +102 -0
- package/lib/types/client/contract/events.js +111 -0
- package/lib/types/client/contract/session.d.ts +178 -0
- package/lib/types/client/contract/session.js +1 -0
- package/lib/types/client/contract/sessions.d.ts +162 -0
- package/lib/types/client/contract/sessions.js +1 -0
- package/lib/types/client/contract/snapshot.d.ts +84 -0
- package/lib/types/client/contract/snapshot.js +1 -0
- package/lib/types/client/index.d.ts +39 -0
- package/lib/types/client/index.js +60 -0
- package/lib/types/client/ordered-baseline.d.ts +12 -0
- package/lib/types/client/ordered-baseline.js +40 -0
- package/lib/types/client/scope.d.ts +46 -0
- package/lib/types/client/scope.js +46 -0
- package/lib/types/client/session-wire-event.d.ts +11 -0
- package/lib/types/client/session-wire-event.js +42 -0
- package/lib/types/client/sessions/assistant-stream.d.ts +56 -0
- package/lib/types/client/sessions/assistant-stream.js +182 -0
- package/lib/types/client/sessions/history-records.d.ts +22 -0
- package/lib/types/client/sessions/history-records.js +25 -0
- package/lib/types/client/sessions/lineage.d.ts +48 -0
- package/lib/types/client/sessions/lineage.js +55 -0
- package/lib/types/client/sessions/manager.d.ts +272 -0
- package/lib/types/client/sessions/manager.js +774 -0
- package/lib/types/client/sessions/notifier.d.ts +39 -0
- package/lib/types/client/sessions/notifier.js +97 -0
- package/lib/types/client/sessions/projection-store.d.ts +128 -0
- package/lib/types/client/sessions/projection-store.js +161 -0
- package/lib/types/client/sessions/remotes.d.ts +29 -0
- package/lib/types/client/sessions/remotes.js +7 -0
- package/lib/types/client/sessions/service.d.ts +263 -0
- package/lib/types/client/sessions/service.js +597 -0
- package/lib/types/client/sessions/session.d.ts +303 -0
- package/lib/types/client/sessions/session.js +991 -0
- package/lib/types/client/time-zone.d.ts +8 -0
- package/lib/types/client/time-zone.js +13 -0
- package/lib/types/client/transport.d.ts +77 -0
- package/lib/types/client/transport.js +130 -0
- package/lib/types/commands.d.ts +92 -0
- package/lib/types/commands.js +575 -0
- package/lib/types/continuation.d.ts +35 -0
- package/lib/types/continuation.js +47 -0
- package/lib/types/control.d.ts +20 -0
- package/lib/types/control.js +106 -0
- package/lib/types/file-references.d.ts +27 -0
- package/lib/types/file-references.js +68 -0
- package/lib/types/history/assistant-stream.d.ts +43 -0
- package/lib/types/history/assistant-stream.js +79 -0
- package/lib/types/history/controller.d.ts +104 -0
- package/lib/types/history/controller.js +293 -0
- package/lib/types/history/listener.d.ts +105 -0
- package/lib/types/history/listener.js +296 -0
- package/lib/types/history.d.ts +58 -0
- package/lib/types/history.js +152 -0
- package/lib/types/index.d.ts +262 -0
- package/lib/types/index.js +693 -0
- package/lib/types/list.d.ts +62 -0
- package/lib/types/list.js +343 -0
- package/lib/types/media-references.d.ts +18 -0
- package/lib/types/media-references.js +84 -0
- package/lib/types/remote-events.d.ts +8 -0
- package/lib/types/remote-events.js +1 -0
- package/lib/types/skill-catalog.d.ts +28 -0
- package/lib/types/skill-catalog.js +203 -0
- package/lib/types/state/controller.d.ts +45 -0
- package/lib/types/state/controller.js +288 -0
- package/lib/types/state/listener.d.ts +40 -0
- package/lib/types/state/listener.js +111 -0
- package/lib/types/types.d.ts +687 -0
- package/lib/types/types.js +5 -0
- package/lib/types/web/controller.d.ts +25 -0
- package/lib/types/web/controller.js +138 -0
- package/locale/de.json +6 -0
- package/locale/en.json +5 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +119 -4
- package/src/agent.ts +474 -0
- package/src/archived-session-gate.ts +59 -0
- package/src/assistant-stream.ts +102 -0
- package/src/catalog.ts +117 -0
- package/src/client/contract/events.ts +205 -0
- package/src/client/contract/session.ts +174 -0
- package/src/client/contract/sessions.ts +169 -0
- package/src/client/contract/snapshot.ts +94 -0
- package/src/client/index.ts +149 -0
- package/src/client/ordered-baseline.ts +43 -0
- package/src/client/scope.ts +76 -0
- package/src/client/session-wire-event.ts +46 -0
- package/src/client/sessions/assistant-stream.ts +229 -0
- package/src/client/sessions/history-records.ts +35 -0
- package/src/client/sessions/lineage.ts +96 -0
- package/src/client/sessions/manager.ts +857 -0
- package/src/client/sessions/notifier.ts +97 -0
- package/src/client/sessions/projection-store.ts +244 -0
- package/src/client/sessions/remotes.ts +44 -0
- package/src/client/sessions/service.ts +741 -0
- package/src/client/sessions/session.ts +1055 -0
- package/src/client/time-zone.ts +14 -0
- package/src/client/transport.ts +247 -0
- package/src/commands.ts +602 -0
- package/src/continuation.ts +58 -0
- package/src/control.ts +121 -0
- package/src/file-references.ts +42 -0
- package/src/history/assistant-stream.ts +102 -0
- package/src/history/controller.ts +256 -0
- package/src/history/listener.ts +302 -0
- package/src/history.ts +189 -0
- package/src/index.ts +651 -0
- package/src/list.ts +382 -0
- package/src/media-references.ts +84 -0
- package/src/remote-events.ts +14 -0
- package/src/skill-catalog.ts +108 -0
- package/src/state/controller.ts +221 -0
- package/src/state/listener.ts +116 -0
- package/src/types.ts +720 -0
- package/src/web/controller.ts +78 -0
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+
/** Host catalog, durable projection caches, and explicitly retained Client instances. */
|
|
2
|
+
import { SessionSeq } from '@unieai/rabi-session/types';
|
|
3
|
+
import { mergeOrderedBaseline } from "../ordered-baseline.js";
|
|
4
|
+
import { isRemoteFailure } from '@unieai/rabi-api-gateway/client';
|
|
5
|
+
import { assertNever } from '@unieai/rabi-util-values';
|
|
6
|
+
import { flattenLineage } from "./lineage.js";
|
|
7
|
+
import { Notifier } from "./notifier.js";
|
|
8
|
+
import { ProjectionValueStore } from "./projection-store.js";
|
|
9
|
+
import { Session } from "./session.js";
|
|
10
|
+
function sessionSeqCursor(value) {
|
|
11
|
+
return value === -1 ? -1 : SessionSeq(value);
|
|
12
|
+
}
|
|
13
|
+
/** Instance cluster + frame entry + the session list. */
|
|
14
|
+
export class SessionManager {
|
|
15
|
+
remote;
|
|
16
|
+
sessions = new Map();
|
|
17
|
+
/** In-flight Session disposals remain here after instances leave `sessions`, so manager disposal can await quiescence. */
|
|
18
|
+
sessionDisposals = new Set();
|
|
19
|
+
/**
|
|
20
|
+
* Accepted/running presentation must survive a later empty-history list
|
|
21
|
+
* response. Host-asserted running is recorded even before a row, instance, or
|
|
22
|
+
* address holds the identity — the listing that would hold it may not have
|
|
23
|
+
* landed yet — while the client-local acceptance callback requires a current
|
|
24
|
+
* holder, because it can arrive from a replaced or already-dropped Session.
|
|
25
|
+
*/
|
|
26
|
+
engagedSessions = new Set();
|
|
27
|
+
disposed = false;
|
|
28
|
+
/** Per-session projection value stores, retained independently of instance arrival (the
|
|
29
|
+
* title-snapshot precedent, generalized): push frames land here whether or not the Session
|
|
30
|
+
* is instantiated (list rows read the 'title' key), and an instantiated Session adopts the
|
|
31
|
+
* same store so history-baseline seeding and frames converge on one row set. */
|
|
32
|
+
projectionStores = new Map();
|
|
33
|
+
summaries = [];
|
|
34
|
+
listState = 'idle';
|
|
35
|
+
/** Arrival phase; the pending → ready edge fires on the first successful pull (see SessionListPhase). */
|
|
36
|
+
listPhase = 'pending';
|
|
37
|
+
listError = null;
|
|
38
|
+
listInflight = null;
|
|
39
|
+
/** Active list request's mutation log; its identity also fences completion after reconnect. */
|
|
40
|
+
listMutations = null;
|
|
41
|
+
addresses = new Map();
|
|
42
|
+
projectionLoads = new Map();
|
|
43
|
+
projectionInflight = new Map();
|
|
44
|
+
listSnapshotCache;
|
|
45
|
+
/** Entry-identity cache (reference stability): list rebuilds reuse the previous entry
|
|
46
|
+
* object when every field matches — wire refreshes mint all-new summary objects, so identity
|
|
47
|
+
* must be recovered by value or every SessionListItem memo misses on every refresh. */
|
|
48
|
+
entryCache = new Map();
|
|
49
|
+
itemsCache = [];
|
|
50
|
+
notifier = new Notifier(() => {
|
|
51
|
+
this.listSnapshotCache = this.buildListSnapshot();
|
|
52
|
+
});
|
|
53
|
+
/** @param remote - generated Remote namespaces used by catalog and history readers. */
|
|
54
|
+
constructor(remote) {
|
|
55
|
+
this.remote = remote;
|
|
56
|
+
this.listSnapshotCache = this.buildListSnapshot();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Resolve an acquisition target without materializing a Session.
|
|
60
|
+
* @param target - known identity or durable direct-parent address.
|
|
61
|
+
* @returns the resolved identity with its explicit or catalog-derived history route installed.
|
|
62
|
+
*/
|
|
63
|
+
resolveTarget(target) {
|
|
64
|
+
const id = typeof target === 'string' ? target : target.childSessionId;
|
|
65
|
+
const address = typeof target === 'string' ? this.subagentAddress(id) : target;
|
|
66
|
+
if (typeof target === 'string'
|
|
67
|
+
&& !this.sessions.has(id)
|
|
68
|
+
&& !this.summaries.some(summary => summary.sessionId === id)
|
|
69
|
+
&& address === undefined) {
|
|
70
|
+
throw new Error(`sessions.retain: unknown session ${id}`);
|
|
71
|
+
}
|
|
72
|
+
if (address !== undefined)
|
|
73
|
+
this.addresses.set(id, address);
|
|
74
|
+
this.sessions.get(id)?.configureSubagent(address);
|
|
75
|
+
return id;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Resolve an address for breadcrumb navigation without retaining transport authority.
|
|
79
|
+
* @param sessionId - possible child id in received Session metadata.
|
|
80
|
+
* @returns A retained address, then a catalog address, then an unknown-mode address
|
|
81
|
+
* from a listed subagent header; undefined without that evidence.
|
|
82
|
+
*/
|
|
83
|
+
subagentAddress(sessionId) {
|
|
84
|
+
const retained = this.addresses.get(sessionId);
|
|
85
|
+
if (retained !== undefined)
|
|
86
|
+
return retained;
|
|
87
|
+
for (const parentSessionId of this.projectionStores.keys()) {
|
|
88
|
+
const child = this.projectionStores.get(parentSessionId)?.values().subagentCatalog
|
|
89
|
+
?.find(entry => entry.id === sessionId);
|
|
90
|
+
if (child !== undefined && child.mode !== 'external') {
|
|
91
|
+
return {
|
|
92
|
+
parentSessionId,
|
|
93
|
+
childSessionId: sessionId,
|
|
94
|
+
mode: child.mode,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const summary = this.summaries.find(summary => summary.sessionId === sessionId);
|
|
99
|
+
if (summary?.origin !== 'subagent' || summary.parentSessionId === undefined)
|
|
100
|
+
return undefined;
|
|
101
|
+
return {
|
|
102
|
+
parentSessionId: summary.parentSessionId,
|
|
103
|
+
childSessionId: sessionId,
|
|
104
|
+
mode: 'unknown',
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
// ---- Instance management ----
|
|
108
|
+
/**
|
|
109
|
+
* Withdraw an exact Client instance before running its teardown callbacks.
|
|
110
|
+
* @param sessionId - identity to withdraw.
|
|
111
|
+
* @param expected - instance being released; a replacement is left untouched.
|
|
112
|
+
* @returns completion of the detached instance's stream teardown.
|
|
113
|
+
*/
|
|
114
|
+
drop(sessionId, expected) {
|
|
115
|
+
const session = this.sessions.get(sessionId);
|
|
116
|
+
if (session !== expected)
|
|
117
|
+
return Promise.resolve();
|
|
118
|
+
this.sessions.delete(sessionId);
|
|
119
|
+
this.addresses.delete(sessionId);
|
|
120
|
+
this.pruneEngagement(sessionId, this.retainedIds(this.summaries));
|
|
121
|
+
return this.startSessionDisposal(session);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Stop catalog requests and dispose every resident Session.
|
|
125
|
+
* @returns once catalog requests and every Session stream have stopped.
|
|
126
|
+
*/
|
|
127
|
+
async dispose() {
|
|
128
|
+
this.disposed = true;
|
|
129
|
+
this.listMutations = null;
|
|
130
|
+
this.listInflight = null;
|
|
131
|
+
this.engagedSessions.clear();
|
|
132
|
+
const reads = [...this.projectionInflight.values()];
|
|
133
|
+
for (const { controller } of reads)
|
|
134
|
+
controller.abort();
|
|
135
|
+
this.projectionInflight.clear();
|
|
136
|
+
await Promise.all(reads.map(read => read.promise));
|
|
137
|
+
const sessions = [...this.sessions.values()];
|
|
138
|
+
this.sessions.clear();
|
|
139
|
+
this.addresses.clear();
|
|
140
|
+
for (const session of sessions)
|
|
141
|
+
void this.startSessionDisposal(session);
|
|
142
|
+
await this.drainSessionDisposals();
|
|
143
|
+
}
|
|
144
|
+
startSessionDisposal(session) {
|
|
145
|
+
const disposal = session.dispose();
|
|
146
|
+
this.sessionDisposals.add(disposal);
|
|
147
|
+
void disposal.then(() => { this.sessionDisposals.delete(disposal); }, () => { this.sessionDisposals.delete(disposal); });
|
|
148
|
+
return disposal;
|
|
149
|
+
}
|
|
150
|
+
async drainSessionDisposals() {
|
|
151
|
+
while (this.sessionDisposals.size > 0) {
|
|
152
|
+
await Promise.allSettled([...this.sessionDisposals]);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Lazy build: return the existing instance or construct one (no auto-open —
|
|
157
|
+
* the reference allocator opens history after binding the scope).
|
|
158
|
+
* New instances reconcile retained metadata before returning.
|
|
159
|
+
* @param sessionId - the session to get.
|
|
160
|
+
* @returns the resident instance.
|
|
161
|
+
*/
|
|
162
|
+
get(sessionId) {
|
|
163
|
+
let session = this.sessions.get(sessionId);
|
|
164
|
+
if (session === undefined) {
|
|
165
|
+
session = this.createSession(sessionId);
|
|
166
|
+
this.sessions.set(sessionId, session);
|
|
167
|
+
// Sync the running and blank bits from the list snapshot into the new
|
|
168
|
+
// instance (consistency when the list precedes open).
|
|
169
|
+
const summary = this.summaries.find(s => s.sessionId === sessionId);
|
|
170
|
+
if (summary !== undefined) {
|
|
171
|
+
session.handleBlank(this.effectiveBlank(summary));
|
|
172
|
+
session.handleRunning(summary.running);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
const address = this.addresses.get(sessionId);
|
|
176
|
+
const child = address === undefined ? undefined : this.projectionStores.get(address.parentSessionId)?.values().subagentCatalog
|
|
177
|
+
?.find(entry => entry.id === sessionId);
|
|
178
|
+
if (child !== undefined && child.mode !== 'external') {
|
|
179
|
+
// A catalogued child exists only after its delegated session has
|
|
180
|
+
// durable history, even though child rows do not carry `blank`.
|
|
181
|
+
session.handleBlank(false);
|
|
182
|
+
session.handleRunning(false);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
// Retained metadata may have notified before this Session existed.
|
|
186
|
+
session.handleBlank(true);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return session;
|
|
191
|
+
}
|
|
192
|
+
createSession(sessionId) {
|
|
193
|
+
const address = this.addresses.get(sessionId);
|
|
194
|
+
return new Session(sessionId, this.remote, {
|
|
195
|
+
...(address === undefined ? {} : { address }),
|
|
196
|
+
// The sender's local first-send flip mirrors into the list row so the
|
|
197
|
+
// session surfaces (lists filter on blank) before any host frame lands.
|
|
198
|
+
onEngaged: (engaged) => {
|
|
199
|
+
if (this.disposed || !this.retainedIds(this.summaries).has(engaged.sessionId))
|
|
200
|
+
return;
|
|
201
|
+
// An identity already on the ledger needs no second row mutation.
|
|
202
|
+
if (!this.engagedSessions.has(engaged.sessionId)) {
|
|
203
|
+
this.engagedSessions.add(engaged.sessionId);
|
|
204
|
+
this.recordMutation({ kind: 'engaged', sessionId: engaged.sessionId });
|
|
205
|
+
}
|
|
206
|
+
this.sessions.get(engaged.sessionId)?.handleBlank(false);
|
|
207
|
+
},
|
|
208
|
+
projections: this.projectionStore(sessionId),
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
effectiveBlank(summary) {
|
|
212
|
+
return summary.blank && !this.engagedSessions.has(summary.sessionId);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Identities an engagement may still belong to: the given list rows, resident
|
|
216
|
+
* Session instances, and retained child addresses.
|
|
217
|
+
* @param summaries - list rows of the caller's snapshot.
|
|
218
|
+
* @returns the retained identity set.
|
|
219
|
+
*/
|
|
220
|
+
retainedIds(summaries) {
|
|
221
|
+
const retained = new Set(summaries.map(summary => summary.sessionId));
|
|
222
|
+
for (const sessionId of this.sessions.keys())
|
|
223
|
+
retained.add(sessionId);
|
|
224
|
+
for (const sessionId of this.addresses.keys())
|
|
225
|
+
retained.add(sessionId);
|
|
226
|
+
return retained;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Forget one engagement that no retained identity holds.
|
|
230
|
+
* @param sessionId - identity whose engagement may be dropped.
|
|
231
|
+
* @param retained - identities from {@link retainedIds} for the caller's snapshot.
|
|
232
|
+
*/
|
|
233
|
+
pruneEngagement(sessionId, retained) {
|
|
234
|
+
if (!retained.has(sessionId))
|
|
235
|
+
this.engagedSessions.delete(sessionId);
|
|
236
|
+
}
|
|
237
|
+
/** Resident per-session projection store (create-on-demand; outlives instantiation). */
|
|
238
|
+
projectionStore(sessionId) {
|
|
239
|
+
let store = this.projectionStores.get(sessionId);
|
|
240
|
+
if (store === undefined) {
|
|
241
|
+
store = new ProjectionValueStore();
|
|
242
|
+
const projections = store;
|
|
243
|
+
store.subscribeAny(() => {
|
|
244
|
+
// Newer history or control metadata corrects a resident Session's stale list hint.
|
|
245
|
+
if (projections.values().sessionListMetadata?.blank === false) {
|
|
246
|
+
this.sessions.get(sessionId)?.handleBlank(false);
|
|
247
|
+
}
|
|
248
|
+
this.notifier.markDirty();
|
|
249
|
+
});
|
|
250
|
+
this.projectionStores.set(sessionId, store);
|
|
251
|
+
}
|
|
252
|
+
return store;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Read projections without starting migration; retry failed or migration-deferred reads.
|
|
256
|
+
* @param sessionId - Session to inspect without opening its conversation.
|
|
257
|
+
* @returns completion of the current or newly started read.
|
|
258
|
+
*/
|
|
259
|
+
refreshProjections(sessionId) {
|
|
260
|
+
const existing = this.projectionInflight.get(sessionId);
|
|
261
|
+
if (existing !== undefined)
|
|
262
|
+
return existing.promise;
|
|
263
|
+
if (this.projectionLoads.get(sessionId)?.state === 'ready')
|
|
264
|
+
return Promise.resolve();
|
|
265
|
+
const controller = new AbortController();
|
|
266
|
+
const store = this.projectionStore(sessionId);
|
|
267
|
+
const initialValues = store.values();
|
|
268
|
+
this.projectionLoads.set(sessionId, { state: 'loading', error: null });
|
|
269
|
+
this.notifier.markDirty();
|
|
270
|
+
const operation = (async () => {
|
|
271
|
+
try {
|
|
272
|
+
const result = await this.remote.session.projections({ sessionId }, controller.signal);
|
|
273
|
+
if (controller.signal.aborted)
|
|
274
|
+
return;
|
|
275
|
+
if (result.ok) {
|
|
276
|
+
const value = result.value;
|
|
277
|
+
if (value !== null) {
|
|
278
|
+
switch (value.kind) {
|
|
279
|
+
case 'sequenced':
|
|
280
|
+
store.seed({ values: value.values, asOfSeq: sessionSeqCursor(value.asOfSeq) });
|
|
281
|
+
break;
|
|
282
|
+
case 'migration-required':
|
|
283
|
+
store.applyCached(value.values);
|
|
284
|
+
break;
|
|
285
|
+
default:
|
|
286
|
+
assertNever(value, 'session projections result');
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
else if (store.values() === initialValues) {
|
|
290
|
+
// A later frame proves existence independently of an earlier missing read.
|
|
291
|
+
store.clear();
|
|
292
|
+
}
|
|
293
|
+
this.projectionLoads.set(sessionId, {
|
|
294
|
+
state: value?.kind === 'migration-required' ? 'migration-required' : 'ready',
|
|
295
|
+
error: null,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
this.projectionLoads.set(sessionId, { state: 'error', error: result.error });
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
if (controller.signal.aborted)
|
|
304
|
+
return;
|
|
305
|
+
if (!isRemoteFailure(error))
|
|
306
|
+
throw error;
|
|
307
|
+
this.projectionLoads.set(sessionId, { state: 'error', error });
|
|
308
|
+
}
|
|
309
|
+
finally {
|
|
310
|
+
if (!controller.signal.aborted) {
|
|
311
|
+
this.projectionInflight.delete(sessionId);
|
|
312
|
+
this.notifier.markDirty();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
})();
|
|
316
|
+
this.projectionInflight.set(sessionId, { promise: operation, controller });
|
|
317
|
+
return operation;
|
|
318
|
+
}
|
|
319
|
+
// ---- List API ----
|
|
320
|
+
/** Full refresh via session.list (single-flight within one Host generation). */
|
|
321
|
+
refreshList() {
|
|
322
|
+
if (this.listInflight !== null)
|
|
323
|
+
return this.listInflight;
|
|
324
|
+
this.listState = 'loading';
|
|
325
|
+
this.listError = null;
|
|
326
|
+
const established = this.summaries;
|
|
327
|
+
const mutations = [];
|
|
328
|
+
this.listMutations = mutations;
|
|
329
|
+
this.notifier.markDirty();
|
|
330
|
+
this.listInflight = (async () => {
|
|
331
|
+
try {
|
|
332
|
+
const result = await this.remote.session.list({});
|
|
333
|
+
if (this.listMutations !== mutations)
|
|
334
|
+
return;
|
|
335
|
+
if (result.ok) {
|
|
336
|
+
const baseline = this.listPhase === 'pending'
|
|
337
|
+
? [...result.value.items]
|
|
338
|
+
: mergeOrderedBaseline(established, result.value.items, summary => summary.sessionId);
|
|
339
|
+
// A removal supersedes running observed in the pull, including after re-addition.
|
|
340
|
+
const removedSincePull = new Set();
|
|
341
|
+
for (const mutation of mutations) {
|
|
342
|
+
if (mutation.kind === 'remove')
|
|
343
|
+
removedSincePull.add(mutation.sessionId);
|
|
344
|
+
}
|
|
345
|
+
for (const s of baseline) {
|
|
346
|
+
if (s.running && !removedSincePull.has(s.sessionId))
|
|
347
|
+
this.engagedSessions.add(s.sessionId);
|
|
348
|
+
}
|
|
349
|
+
const summaries = mutations.reduce(applyMutation, baseline);
|
|
350
|
+
this.summaries = summaries;
|
|
351
|
+
// A full list can remove identities without a removal frame.
|
|
352
|
+
const retained = this.retainedIds(summaries);
|
|
353
|
+
for (const sessionId of this.engagedSessions)
|
|
354
|
+
this.pruneEngagement(sessionId, retained);
|
|
355
|
+
this.listState = 'idle';
|
|
356
|
+
this.listPhase = 'ready';
|
|
357
|
+
// Resident Sessions and list rows share one display blank, reconciled with the metadata projection.
|
|
358
|
+
for (const s of this.summaries) {
|
|
359
|
+
const session = this.sessions.get(s.sessionId);
|
|
360
|
+
if (session === undefined)
|
|
361
|
+
continue;
|
|
362
|
+
session.handleBlank(this.effectiveBlank(s));
|
|
363
|
+
session.handleRunning(s.running);
|
|
364
|
+
}
|
|
365
|
+
// Land each row's projection block in the per-session value store
|
|
366
|
+
// (cold titles surface without opening the session). A list block is
|
|
367
|
+
// partial — only the keys its source holds — so an absent key never
|
|
368
|
+
// clears; how the present keys merge depends on the block's kind.
|
|
369
|
+
for (const s of result.value.items) {
|
|
370
|
+
if (s.projections !== undefined)
|
|
371
|
+
this.applyListBlock(s.sessionId, s.projections);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
this.listState = 'error';
|
|
376
|
+
this.listError = result.error;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
if (!isRemoteFailure(error))
|
|
381
|
+
throw error;
|
|
382
|
+
if (this.listMutations !== mutations)
|
|
383
|
+
return;
|
|
384
|
+
this.listState = 'error';
|
|
385
|
+
this.listError = error;
|
|
386
|
+
}
|
|
387
|
+
finally {
|
|
388
|
+
if (this.listMutations === mutations) {
|
|
389
|
+
this.listMutations = null;
|
|
390
|
+
this.listInflight = null;
|
|
391
|
+
this.notifier.markDirty();
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
})();
|
|
395
|
+
return this.listInflight;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Search visible session message content without adding transient query
|
|
399
|
+
* state to the list snapshot.
|
|
400
|
+
* @param query - non-blank literal phrase.
|
|
401
|
+
* @param signal - cancellation for superseded UI queries.
|
|
402
|
+
* @returns the Host result or a folded transport error.
|
|
403
|
+
*/
|
|
404
|
+
async search(query, signal) {
|
|
405
|
+
const result = await this.remote.session.search({ query }, signal);
|
|
406
|
+
if (!result.ok)
|
|
407
|
+
return result;
|
|
408
|
+
return {
|
|
409
|
+
ok: true,
|
|
410
|
+
value: {
|
|
411
|
+
items: [...result.value.items],
|
|
412
|
+
hasMore: result.value.hasMore,
|
|
413
|
+
},
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Contract session.create; on success merge into summaries immediately (no
|
|
418
|
+
* wait for the next refresh). A created session is blank by definition
|
|
419
|
+
* (entity birth precedes the first message).
|
|
420
|
+
* @param opts - target workspace or working directory, an optional blank Session to reuse, and an optional Agent preset.
|
|
421
|
+
* @returns the create result, naming the Session the Host created or reused.
|
|
422
|
+
*/
|
|
423
|
+
async create(opts = {}) {
|
|
424
|
+
const shared = {
|
|
425
|
+
...(opts.sessionId === undefined ? {} : { sessionId: opts.sessionId }),
|
|
426
|
+
...(opts.agentPreset === undefined ? {} : { agentPreset: opts.agentPreset }),
|
|
427
|
+
};
|
|
428
|
+
const payload = opts.workspaceId !== undefined
|
|
429
|
+
? { workspaceId: opts.workspaceId, ...shared }
|
|
430
|
+
: { ...(opts.cwd === undefined ? {} : { cwd: opts.cwd }), ...shared };
|
|
431
|
+
const result = await this.remote.session.create(payload);
|
|
432
|
+
if (result.ok) {
|
|
433
|
+
this.recordMutation({ kind: 'placeholder', summary: { agentAvailable: true,
|
|
434
|
+
sessionId: result.value.sessionId, updatedAt: Date.now(), running: false, blank: true,
|
|
435
|
+
...(opts.cwd !== undefined ? { cwd: opts.cwd } : {}),
|
|
436
|
+
} });
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
const publishedSessionId = workspaceAttachSessionId(result.error);
|
|
440
|
+
// Publication precedes attachment. The error's id is a real Session,
|
|
441
|
+
// so expose it immediately as Ungrouped while the caller keeps the
|
|
442
|
+
// prompt buffer and decides whether to retry attachment.
|
|
443
|
+
if (publishedSessionId !== undefined) {
|
|
444
|
+
this.recordMutation({ kind: 'placeholder', summary: { agentAvailable: true,
|
|
445
|
+
sessionId: publishedSessionId,
|
|
446
|
+
updatedAt: Date.now(),
|
|
447
|
+
running: false,
|
|
448
|
+
blank: true,
|
|
449
|
+
} });
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Contract session.fork; on success merge the child into summaries
|
|
456
|
+
* immediately (same synchronous-addressability guarantee as create).
|
|
457
|
+
* Blankness starts provisionally true so the authoritative Host summary can
|
|
458
|
+
* preserve it or lower it after an exact cut before the first `turn/start`;
|
|
459
|
+
* the fork has no durable source relationship. A child published before Workspace
|
|
460
|
+
* attachment fails is also reconciled into the list.
|
|
461
|
+
* @param opts - source session, optional exact inclusive boundary seq, and permission to start migration.
|
|
462
|
+
* @returns the fork result (the child session id).
|
|
463
|
+
*/
|
|
464
|
+
async fork(opts) {
|
|
465
|
+
const source = this.summaries.find(s => s.sessionId === opts.sessionId);
|
|
466
|
+
const result = await this.remote.session.fork({
|
|
467
|
+
sessionId: opts.sessionId,
|
|
468
|
+
...opts.atSeq === undefined ? {} : { atSeq: opts.atSeq },
|
|
469
|
+
...opts.allowMigration === undefined ? {} : { allowMigration: opts.allowMigration },
|
|
470
|
+
});
|
|
471
|
+
const childId = result.ok
|
|
472
|
+
? result.value.sessionId
|
|
473
|
+
: workspaceAttachSessionId(result.error);
|
|
474
|
+
if (childId !== undefined) {
|
|
475
|
+
this.recordMutation({ kind: 'placeholder', summary: { agentAvailable: true,
|
|
476
|
+
sessionId: childId, updatedAt: Date.now(), running: false, blank: true,
|
|
477
|
+
...(source?.cwd !== undefined ? { cwd: source.cwd } : {}),
|
|
478
|
+
} });
|
|
479
|
+
}
|
|
480
|
+
return result;
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Rename a Session and update its title projection without opening its history.
|
|
484
|
+
* @param sessionId - Session to rename.
|
|
485
|
+
* @param title - raw title text for Host normalization.
|
|
486
|
+
* @returns the accepted title and event position, or the Remote failure.
|
|
487
|
+
*/
|
|
488
|
+
async rename(sessionId, title) {
|
|
489
|
+
const result = await this.remote.session.rename({ sessionId, title });
|
|
490
|
+
if (result.ok) {
|
|
491
|
+
this.projectionStore(sessionId).apply('title', result.value.title, SessionSeq(result.value.seq));
|
|
492
|
+
}
|
|
493
|
+
return result;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Merge a Host summary, replacing live state and filling missing metadata.
|
|
497
|
+
* Local create/fork placeholders only fill metadata on an existing row.
|
|
498
|
+
*/
|
|
499
|
+
mergeSummary(summary) {
|
|
500
|
+
this.recordMutation({ kind: 'upsert', summary });
|
|
501
|
+
}
|
|
502
|
+
/** Apply immediately and retain for replay when a list response is in flight. */
|
|
503
|
+
recordMutation(mutation) {
|
|
504
|
+
if (this.disposed)
|
|
505
|
+
return;
|
|
506
|
+
this.listMutations?.push(mutation);
|
|
507
|
+
this.summaries = applyMutation(this.summaries, mutation);
|
|
508
|
+
this.notifier.markDirty();
|
|
509
|
+
}
|
|
510
|
+
// ---- Subscription API (for useSessionList) ----
|
|
511
|
+
/**
|
|
512
|
+
* uSES subscription entry for useSessionList.
|
|
513
|
+
* @param listener - change callback.
|
|
514
|
+
* @returns the unsubscribe function.
|
|
515
|
+
*/
|
|
516
|
+
subscribe(listener) {
|
|
517
|
+
return this.notifier.subscribe(listener);
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Cached list snapshot (rebuilt lazily when dirty with no listeners).
|
|
521
|
+
* @returns the cached reference (stable until the next flush).
|
|
522
|
+
*/
|
|
523
|
+
getListSnapshot() {
|
|
524
|
+
this.notifier.ensureFresh();
|
|
525
|
+
return this.listSnapshotCache;
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Read cached projection values for a Session that may exist only in a loaded subagent catalog.
|
|
529
|
+
* @param sessionId - Session whose control or history baseline supplied projections.
|
|
530
|
+
* @returns current values, or undefined before any projection store exists.
|
|
531
|
+
*/
|
|
532
|
+
projectionValues(sessionId) {
|
|
533
|
+
return this.projectionStores.get(sessionId)?.values();
|
|
534
|
+
}
|
|
535
|
+
// ---- Live control and Host-event sinks ----
|
|
536
|
+
/**
|
|
537
|
+
* Apply a complete control baseline or one later per-event projection frame.
|
|
538
|
+
* @param frame - baseline or live per-event projection change from Session Controller.
|
|
539
|
+
*/
|
|
540
|
+
handleControlFrame(frame) {
|
|
541
|
+
if (frame.type === 'baseline') {
|
|
542
|
+
this.replaceControlBaseline(frame.value);
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
const store = this.projectionStore(frame.sessionId);
|
|
546
|
+
const seq = SessionSeq(frame.seq);
|
|
547
|
+
for (const change of frame.changes)
|
|
548
|
+
store.apply(change.key, change.val, seq);
|
|
549
|
+
this.notifier.markDirty();
|
|
550
|
+
}
|
|
551
|
+
replaceControlBaseline(baseline) {
|
|
552
|
+
for (const [sessionId, block] of Object.entries(baseline.projections)) {
|
|
553
|
+
const store = this.projectionStore(sessionId);
|
|
554
|
+
const asOfSeq = sessionSeqCursor(block.asOfSeq);
|
|
555
|
+
store.seed({ ...block, asOfSeq });
|
|
556
|
+
}
|
|
557
|
+
this.notifier.markDirty();
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Apply one Session-list addition forwarded through `ctx.remote.$on`.
|
|
561
|
+
* @param summary - current Host summary for the added Session.
|
|
562
|
+
*/
|
|
563
|
+
handleSessionAdded(summary) {
|
|
564
|
+
this.mergeSummary(summary);
|
|
565
|
+
if (!this.disposed && summary.running)
|
|
566
|
+
this.engagedSessions.add(summary.sessionId);
|
|
567
|
+
this.sessions.get(summary.sessionId)?.handleBlank(this.effectiveBlank(summary));
|
|
568
|
+
if (summary.projections !== undefined)
|
|
569
|
+
this.applyListBlock(summary.sessionId, summary.projections);
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Merge one list-surface projection block by the sequence space it declares.
|
|
573
|
+
* A `sequenced` block came from the Host's live registry for an attached
|
|
574
|
+
* Session, so each key lands under higher-seq-wins against that Session's
|
|
575
|
+
* baselines and frames. A `cached` block was viewed from the persisted
|
|
576
|
+
* checkpoint by a header-only listing: its watermark is not comparable with
|
|
577
|
+
* this connection's seqs, so it only fills keys no sequenced row holds.
|
|
578
|
+
*/
|
|
579
|
+
applyListBlock(sessionId, block) {
|
|
580
|
+
const store = this.projectionStore(sessionId);
|
|
581
|
+
switch (block.kind) {
|
|
582
|
+
case 'sequenced': {
|
|
583
|
+
const seq = sessionSeqCursor(block.asOfSeq);
|
|
584
|
+
for (const [key, value] of Object.entries(block.values))
|
|
585
|
+
store.apply(key, value, seq);
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
case 'cached':
|
|
589
|
+
store.applyCached(block.values);
|
|
590
|
+
return;
|
|
591
|
+
default:
|
|
592
|
+
assertNever(block.kind, 'session list projection block kind');
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Apply one Session removal forwarded through `ctx.remote.$on`.
|
|
597
|
+
* @param sessionId - removed Session identity.
|
|
598
|
+
*/
|
|
599
|
+
handleSessionRemoved(sessionId) {
|
|
600
|
+
const durableSubagent = this.subagentAddress(sessionId) !== undefined
|
|
601
|
+
|| this.summaries.some(summary => summary.sessionId === sessionId && summary.origin === 'subagent');
|
|
602
|
+
this.recordMutation(durableSubagent
|
|
603
|
+
? { kind: 'status', sessionId, running: false, agentAvailable: false }
|
|
604
|
+
: { kind: 'remove', sessionId });
|
|
605
|
+
if (durableSubagent)
|
|
606
|
+
this.sessions.get(sessionId)?.handleRunning(false);
|
|
607
|
+
else
|
|
608
|
+
this.sessions.get(sessionId)?.handleRemoved();
|
|
609
|
+
const catalog = this.projectionStores.get(sessionId)?.values().subagentCatalog;
|
|
610
|
+
if (!durableSubagent && (catalog === undefined || catalog.length === 0)) {
|
|
611
|
+
this.projectionStores.delete(sessionId);
|
|
612
|
+
}
|
|
613
|
+
this.pruneEngagement(sessionId, this.retainedIds(this.summaries));
|
|
614
|
+
this.projectionInflight.get(sessionId)?.controller.abort();
|
|
615
|
+
this.projectionInflight.delete(sessionId);
|
|
616
|
+
this.projectionLoads.delete(sessionId);
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Apply one live Agent running-state change.
|
|
620
|
+
* @param sessionId - Session whose Agent state changed.
|
|
621
|
+
* @param running - current Agent running state.
|
|
622
|
+
*/
|
|
623
|
+
handleSessionStatus(sessionId, running) {
|
|
624
|
+
if (!this.disposed && running)
|
|
625
|
+
this.engagedSessions.add(sessionId);
|
|
626
|
+
this.recordMutation({ kind: 'status', sessionId, running, agentAvailable: true });
|
|
627
|
+
this.sessions.get(sessionId)?.handleRunning(running);
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Advance Session-list activity from one user-authored durable message.
|
|
631
|
+
* @param sessionId - Session whose activity changed.
|
|
632
|
+
* @param updatedAt - durable message timestamp.
|
|
633
|
+
*/
|
|
634
|
+
handleSessionActivity(sessionId, updatedAt) {
|
|
635
|
+
this.recordMutation({ kind: 'activity', sessionId, updatedAt });
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Surface one live Agent failure on an already-materialized Session.
|
|
639
|
+
* @param sessionId - Session whose Agent failed.
|
|
640
|
+
* @param message - caller-visible failure description.
|
|
641
|
+
*/
|
|
642
|
+
handleSessionError(sessionId, message) {
|
|
643
|
+
this.sessions.get(sessionId)?.handleAgentError(message);
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Repair one re-established Host-event generation with queryable baselines.
|
|
647
|
+
* Discard old projection cuts before new queries, including cold Sessions
|
|
648
|
+
* absent from the process-local control baseline.
|
|
649
|
+
* Opened Session follow streams resume independently through API Gateway.
|
|
650
|
+
*/
|
|
651
|
+
handleConnected() {
|
|
652
|
+
for (const store of this.projectionStores.values())
|
|
653
|
+
store.clear();
|
|
654
|
+
this.listMutations = null;
|
|
655
|
+
this.listInflight = null;
|
|
656
|
+
void this.refreshList();
|
|
657
|
+
const parents = new Set(this.projectionLoads.keys());
|
|
658
|
+
for (const id of this.sessions.keys()) {
|
|
659
|
+
const address = this.addresses.get(id);
|
|
660
|
+
if (address !== undefined)
|
|
661
|
+
parents.add(address.parentSessionId);
|
|
662
|
+
}
|
|
663
|
+
for (const { controller } of this.projectionInflight.values())
|
|
664
|
+
controller.abort();
|
|
665
|
+
this.projectionInflight.clear();
|
|
666
|
+
this.projectionLoads.clear();
|
|
667
|
+
for (const parentSessionId of parents)
|
|
668
|
+
void this.refreshProjections(parentSessionId);
|
|
669
|
+
}
|
|
670
|
+
buildListSnapshot() {
|
|
671
|
+
const merged = this.summaries.map((summary) => {
|
|
672
|
+
// List rows read the generic 'title' projection key (host-computed unit
|
|
673
|
+
// value; there is no dedicated title frame).
|
|
674
|
+
const projectionStore = this.projectionStores.get(summary.sessionId);
|
|
675
|
+
const title = projectionStore?.get('title');
|
|
676
|
+
const projectionValues = projectionStore?.values();
|
|
677
|
+
const metadata = projectionValues?.sessionListMetadata;
|
|
678
|
+
return {
|
|
679
|
+
...summary,
|
|
680
|
+
// Cached list hints can precede a history opening or control update.
|
|
681
|
+
blank: this.effectiveBlank(summary) && metadata?.blank !== false,
|
|
682
|
+
updatedAt: Math.max(summary.updatedAt, metadata?.lastPromptAt ?? 0),
|
|
683
|
+
...(typeof title === 'string' && title !== '' ? { title } : {}),
|
|
684
|
+
...(projectionValues === undefined ? {} : { projectionValues }),
|
|
685
|
+
};
|
|
686
|
+
});
|
|
687
|
+
const fresh = flattenLineage(merged);
|
|
688
|
+
const items = fresh.map((entry) => {
|
|
689
|
+
const prev = this.entryCache.get(entry.sessionId);
|
|
690
|
+
if (prev !== undefined && prev.updatedAt === entry.updatedAt && prev.running === entry.running
|
|
691
|
+
&& prev.blank === entry.blank
|
|
692
|
+
&& prev.parentSessionId === entry.parentSessionId && prev.cwd === entry.cwd
|
|
693
|
+
&& prev.origin === entry.origin && prev.title === entry.title && prev.depth === entry.depth
|
|
694
|
+
&& prev.formatStatus === entry.formatStatus
|
|
695
|
+
&& prev.projectionValues === entry.projectionValues)
|
|
696
|
+
return prev;
|
|
697
|
+
this.entryCache.set(entry.sessionId, entry);
|
|
698
|
+
return entry;
|
|
699
|
+
});
|
|
700
|
+
const itemIds = new Set(items.map(entry => entry.sessionId));
|
|
701
|
+
for (const id of this.entryCache.keys()) {
|
|
702
|
+
if (!itemIds.has(id))
|
|
703
|
+
this.entryCache.delete(id);
|
|
704
|
+
}
|
|
705
|
+
const sameOrder = items.length === this.itemsCache.length && items.every((e, i) => e === this.itemsCache[i]);
|
|
706
|
+
if (!sameOrder)
|
|
707
|
+
this.itemsCache = items;
|
|
708
|
+
return {
|
|
709
|
+
items: this.itemsCache,
|
|
710
|
+
state: this.listState,
|
|
711
|
+
phase: this.listPhase,
|
|
712
|
+
error: this.listError,
|
|
713
|
+
projectionsBySession: Object.fromEntries([...this.projectionStores].map(([sessionId, store]) => [
|
|
714
|
+
sessionId,
|
|
715
|
+
{ values: store.values(), state: 'idle', error: null, ...this.projectionLoads.get(sessionId) },
|
|
716
|
+
])),
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
/** Apply one list mutation without deriving display order. */
|
|
721
|
+
function applyMutation(summaries, mutation) {
|
|
722
|
+
switch (mutation.kind) {
|
|
723
|
+
case 'upsert':
|
|
724
|
+
case 'placeholder': {
|
|
725
|
+
const existing = summaries.find(summary => summary.sessionId === mutation.summary.sessionId);
|
|
726
|
+
if (existing === undefined)
|
|
727
|
+
return [mutation.summary, ...summaries];
|
|
728
|
+
const filled = {
|
|
729
|
+
...existing,
|
|
730
|
+
// Blank only lowers: a stale true (session-added racing the local
|
|
731
|
+
// first send) never re-hides an already-surfaced session.
|
|
732
|
+
blank: existing.blank && mutation.summary.blank,
|
|
733
|
+
...(mutation.kind === 'upsert' ? {
|
|
734
|
+
agentAvailable: mutation.summary.agentAvailable,
|
|
735
|
+
running: mutation.summary.running,
|
|
736
|
+
...(mutation.summary.formatStatus === undefined ? {} : { formatStatus: mutation.summary.formatStatus }),
|
|
737
|
+
} : {}),
|
|
738
|
+
...(existing.cwd === undefined && mutation.summary.cwd !== undefined ? { cwd: mutation.summary.cwd } : {}),
|
|
739
|
+
...(existing.parentSessionId === undefined && mutation.summary.parentSessionId !== undefined
|
|
740
|
+
? { parentSessionId: mutation.summary.parentSessionId } : {}),
|
|
741
|
+
...(existing.origin === undefined && mutation.summary.origin !== undefined
|
|
742
|
+
? { origin: mutation.summary.origin } : {}),
|
|
743
|
+
};
|
|
744
|
+
if (filled.cwd === existing.cwd && filled.parentSessionId === existing.parentSessionId
|
|
745
|
+
&& filled.origin === existing.origin && filled.blank === existing.blank
|
|
746
|
+
&& filled.agentAvailable === existing.agentAvailable && filled.running === existing.running
|
|
747
|
+
&& filled.formatStatus === existing.formatStatus)
|
|
748
|
+
return [...summaries];
|
|
749
|
+
return summaries.map(summary => summary.sessionId === mutation.summary.sessionId ? filled : summary);
|
|
750
|
+
}
|
|
751
|
+
case 'remove':
|
|
752
|
+
return summaries.filter(summary => summary.sessionId !== mutation.sessionId);
|
|
753
|
+
case 'status':
|
|
754
|
+
// Running converts display state; it does not establish durable history.
|
|
755
|
+
return summaries.map(summary => summary.sessionId === mutation.sessionId
|
|
756
|
+
&& (summary.running !== mutation.running
|
|
757
|
+
|| summary.agentAvailable !== mutation.agentAvailable || (mutation.running && summary.blank))
|
|
758
|
+
? { ...summary, running: mutation.running, agentAvailable: mutation.agentAvailable, blank: summary.blank && !mutation.running }
|
|
759
|
+
: summary);
|
|
760
|
+
case 'activity':
|
|
761
|
+
return summaries.map(summary => summary.sessionId === mutation.sessionId
|
|
762
|
+
&& mutation.updatedAt > summary.updatedAt
|
|
763
|
+
? { ...summary, updatedAt: mutation.updatedAt }
|
|
764
|
+
: summary);
|
|
765
|
+
case 'engaged':
|
|
766
|
+
return summaries.map(summary => summary.sessionId === mutation.sessionId && summary.blank
|
|
767
|
+
? { ...summary, blank: false }
|
|
768
|
+
: summary);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
/** Temporary source-plane bridge while the Host contract and client project build independently. */
|
|
772
|
+
function workspaceAttachSessionId(error) {
|
|
773
|
+
return error.code === 'session/workspace-attach-failed' ? error.details.sessionId : undefined;
|
|
774
|
+
}
|