forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel-sessions.ts
More file actions
572 lines (545 loc) · 19.7 KB
/
Copy pathkernel-sessions.ts
File metadata and controls
572 lines (545 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import type {
AdapterAttemptResult,
AdapterBindingHandle,
CancelDispatchResult,
OpenedBinding,
RuntimeAdapter,
} from "../adapters/interface.js";
import type { OutboundMessage } from "../protocol.js";
import { AdapterRegistry } from "./adapter-registry.js";
import { AdapterRuntimeError, failureFromError } from "./failures.js";
import {
clearOwnerSurfaceState,
importLegacyMainChatSessions,
resolveSurfaceSession,
type LegacyMainChatSessionEntry,
type LegacyMainChatSessionImportReceipt,
type ResolveSurfaceSessionInput,
type ResolveSurfaceSessionResult,
} from "./surface-session.js";
import type {
AdapterBinding,
AgentArtifact,
AgentDelegation,
AgentRun,
AgentSession,
AgentStore,
AgentGrant,
NewAgentArtifact,
NewAgentGrant,
RunAttempt,
RunStatus,
DelegationStatus,
DesktopAttentionOverride,
NewDesktopCoordinatorDispatch,
} from "./types.js";
import { buildDesktopActionQueue } from "./desktop-action-queue.js";
import { buildDesktopContextPacket, type DesktopContextPacketBuildInput } from "./desktop-context-packet.js";
import { OmiArtifactStorage } from "./artifact-storage.js";
import { writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import {
ACTIVE_STATUSES,
TERMINAL_STATUSES,
DEFAULT_DELEGATION_MAX_DEPTH,
HARD_DELEGATION_MAX_DEPTH,
DEFAULT_DELEGATION_MAX_BUDGET_USD,
HARD_DELEGATION_MAX_BUDGET_USD,
requiresVerifiedContextDispatch,
bindingMetadata,
stableHash,
stableJsonStringify,
stableMcpServerConfig,
stableJsonHash,
parseJsonObject,
placeholders,
isStaleBindingError,
messageFrom,
boundedLimit,
sessionFromRow,
runFromRow,
delegationFromRow,
delegationValues,
buildDelegatedPrompt,
requiredChildSessionId,
attemptFromRow,
bindingFromRow,
eventFromRow,
artifactFromRow,
desktopDispatchFromRow,
desktopArtifactDeliveryFromRow,
desktopMemoryCandidateFromRow,
desktopTaskCandidateFromRow,
desktopAttentionOverrideFromRow,
dispatchToQueueInput,
deliveryToQueueInput,
memoryCandidateToQueueInput,
taskCandidateToQueueInput,
overrideToQueueInput,
intentCandidateStatus,
updateByColumns,
queueRunGoalText,
stringValue,
numberValue,
nullableString,
nullableNumber,
nullableText,
text,
} from "./kernel-support.js";
import type {
KernelSessionResolutionInput,
ExecuteAgentRunInput,
KernelRunResult,
CancelRunResult,
ListSessionsInput,
KernelSessionSummary,
GetRunInput,
KernelRunDetails,
InspectArtifactsInput,
DesktopAwarenessSnapshotInput,
DesktopAwarenessSnapshot,
DesktopActionQueueInput,
DesktopOpenLoopsInput,
DesktopContextPacketPersistInput,
ResolveDesktopDispatchInput,
ResolveDesktopDispatchResult,
UpdateArtifactLifecycleInput,
UpdateArtifactLifecycleResult,
PersistArtifactInput,
InvalidateBindingsInput,
InvalidateBindingsResult,
StaleProcessLocalBindingsInput,
StaleProcessLocalBindingsResult,
SendAgentMessageInput,
SpawnBackgroundAgentInput,
SpawnBackgroundAgentResult,
DelegateAgentInput,
DelegateAgentResult,
KernelEventSubscriber,
AgentRuntimeKernelOptions,
} from "./kernel-types.js";
import { StaleAdapterBindingError } from "./kernel-types.js";
import { KernelArtifacts } from "./kernel-artifacts.js";
import {
configureDefaultExecutionProfile,
migrateSessionExecutionProfile,
readDefaultExecutionProfilePreference,
readSessionExecutionProfile,
type MigrateSessionExecutionProfileInput,
type MigrateSessionExecutionProfileResult,
} from "./session-execution-profile.js";
import type { DefaultExecutionProfilePreference, SessionExecutionProfile } from "./types.js";
import {
buildContextSnapshot,
updateContextSource,
type ContextSourceUpdateInput,
type ContextSourceUpdateResult,
} from "./context-snapshot.js";
import type { ContextSnapshotProjection } from "../protocol.js";
import type { ChatFirstCapabilityProjection } from "./chat-first-capability.js";
import {
ensureAgentSpawnJournal,
type EnsureAgentSpawnJournalInput,
type EnsureAgentSpawnJournalResult,
} from "./agent-spawn-journal.js";
import { conversationIdForSession } from "./conversation-turns.js";
import type { AuthorizedRunToolInvocation } from "./run-tool-capability.js";
export class KernelSessions extends KernelArtifacts {
ownedSession(sessionId: string, ownerId: string): AgentSession {
const session = this.readSession(sessionId);
this.assertSessionOwner(session, ownerId);
return session;
}
/**
* T08 is not a model-tool invocation, but it still needs the same immutable
* server-derived Main Chat capability that admitted rich blocks. Keeping the
* check here avoids a second Swift or UI-side rollout gate.
*/
assertChatFirstMainCapability(sessionId: string, ownerId: string, controlGeneration: number): void {
this.ownedSession(sessionId, ownerId);
const capability = this.chatFirstCapability(sessionId, ownerId, "main_chat");
if (
capability?.chatFirstUi !== true
|| capability.controlGeneration !== controlGeneration
) {
throw new Error("Question interaction requires an enabled current main-Chat capability");
}
}
/**
* Background chat-first work is permitted only while this process has an
* immutable, enabled Main Chat projection for the active owner. It is not a
* persisted rollout flag: a fresh capability-off launch therefore leaves
* deferral rows dormant instead of delivering feature work to a legacy user.
*/
hasChatFirstMainCapability(ownerId: string): boolean {
for (const [key, capability] of this.chatFirstCapabilities) {
if (key.startsWith(`${ownerId}:`) && capability.chatFirstUi === true) return true;
}
return false;
}
defaultExecutionProfilePreference(ownerId: string): DefaultExecutionProfilePreference | undefined {
return readDefaultExecutionProfilePreference(this.store, ownerId);
}
configureDefaultExecutionProfile(input: {
ownerId: string;
adapterId: string;
modelProfile: string | null;
workingDirectory: string;
expectedPreferenceGeneration?: number;
}): DefaultExecutionProfilePreference {
return configureDefaultExecutionProfile(this.store, input, Date.now());
}
contextSnapshot(sessionId: string, ownerId: string, surfaceKind?: string): ContextSnapshotProjection {
return buildContextSnapshot(
this.store,
sessionId,
ownerId,
Date.now(),
surfaceKind,
this.chatFirstCapability(sessionId, ownerId, surfaceKind),
);
}
/**
* Local/offline E2E admission for the real `render_chat_blocks` executor.
* Session ownership and the ephemeral rollout projection both belong to this
* session layer, so the probe cannot manufacture either one from its input.
*/
beginChatFirstHarnessExecutor(input: {
ownerId: string;
sessionId: string;
producingTurnId: string;
controlGeneration: number;
clientId: string;
requestId: string;
toolInput: Record<string, unknown>;
}): AuthorizedRunToolInvocation {
const session = this.ownedSession(input.sessionId, input.ownerId);
if (session.surfaceKind !== "main_chat") {
throw new Error("Chat-first E2E executor requires an existing main Chat session");
}
const admitted = this.contextSnapshot(input.sessionId, input.ownerId, "main_chat");
if (
admitted.capabilities.chatFirstUi !== true
|| admitted.capabilities.chatFirstControlGeneration !== input.controlGeneration
|| !admitted.capabilities.allowedToolNames.includes("render_chat_blocks")
) {
throw new Error("Chat-first E2E executor requires the mounted server-derived capability");
}
const accepted = this.createAcceptedRun({
sessionId: input.sessionId,
ownerId: input.ownerId,
surfaceKind: "main_chat",
clientId: input.clientId,
requestId: input.requestId,
producingTurnId: input.producingTurnId,
prompt: "Local Chat-first executor probe",
mode: "ask",
admittedContextSnapshot: admitted,
});
const conversationId = conversationIdForSession(this.store, accepted.session.sessionId);
if (!conversationId) throw new Error("Chat-first E2E executor requires a canonical Chat conversation");
const attempt = this.createAttempt({
runId: accepted.run.runId,
attemptNo: 1,
adapterId: accepted.session.defaultAdapterId,
retryReason: null,
resumeFromAttemptId: null,
producingTurn: {
ownerId: accepted.session.ownerId,
sessionId: accepted.session.sessionId,
conversationId,
turnId: input.producingTurnId,
},
});
const capability = this.toolCapabilities.register({
ownerId: accepted.session.ownerId,
sessionId: accepted.session.sessionId,
runId: accepted.run.runId,
attemptId: attempt.attemptId,
});
const invocation = this.toolCapabilities.authorize({
capabilityRef: capability.capabilityRef,
invocationId: `chat_first_e2e_${accepted.run.runId}`,
runId: accepted.run.runId,
attemptId: attempt.attemptId,
toolName: "render_chat_blocks",
toolInput: input.toolInput,
activeOwnerId: input.ownerId,
});
if (
invocation.canonicalToolName !== "render_chat_blocks"
|| invocation.surfaceKind !== "main_chat"
|| invocation.chatFirstUi !== true
|| invocation.chatFirstControlGeneration !== input.controlGeneration
) {
throw new Error("Chat-first E2E executor did not receive the authorized main-Chat tool");
}
this.markRunToolInvocationDispatched(invocation);
return invocation;
}
completeChatFirstHarnessExecutor(input: {
ownerId: string;
sessionId: string;
runId: string;
attemptId: string;
succeeded: boolean;
}): void {
const session = this.ownedSession(input.sessionId, input.ownerId);
const run = this.readRun(input.runId);
const attempt = this.readAttempt(input.attemptId);
if (
session.surfaceKind !== "main_chat"
|| run.sessionId !== session.sessionId
|| attempt.runId !== run.runId
|| this.readLatestAttempt(run.runId).attemptId !== attempt.attemptId
) {
throw new Error("Chat-first E2E executor completion does not own the active run");
}
const pendingInvocations = Number(this.store.getRow(
`SELECT COUNT(*) AS count FROM tool_invocation_ledger
WHERE run_id = ? AND attempt_id = ? AND status IN ('prepared', 'dispatched')`,
[input.runId, input.attemptId],
).count);
if (pendingInvocations > 0) {
throw new Error("Chat-first E2E executor cannot finish while its tool invocation is pending");
}
this.withTransaction(() => {
this.finishAttemptAndRun({
sessionId: input.sessionId,
runId: input.runId,
attemptId: input.attemptId,
status: input.succeeded ? "succeeded" : "failed",
finalText: null,
errorCode: input.succeeded ? null : "chat_first_e2e_executor_failed",
errorMessage: input.succeeded ? null : "Chat-first E2E executor failed",
});
});
}
contextSnapshotForExactSurface(
ownerId: string,
surface: { surfaceKind: string; externalRefKind: string; externalRefId: string },
): ContextSnapshotProjection {
const mapping = this.store.getRow(
`SELECT agent_session_id FROM surface_conversations
WHERE owner_id = ? AND surface_kind = ? AND external_ref_kind = ? AND external_ref_id = ?`,
[ownerId, surface.surfaceKind, surface.externalRefKind, surface.externalRefId],
);
const sessionId = String(mapping.agent_session_id);
return buildContextSnapshot(
this.store,
sessionId,
ownerId,
Date.now(),
surface.surfaceKind,
this.chatFirstCapability(sessionId, ownerId, surface.surfaceKind),
);
}
updateContextSource(input: ContextSourceUpdateInput): ContextSourceUpdateResult {
return updateContextSource(this.store, {
...input,
chatFirstCapability: this.chatFirstCapability(input.sessionId, input.ownerId, input.surfaceKind),
});
}
ensureAgentSpawnJournal(input: EnsureAgentSpawnJournalInput): EnsureAgentSpawnJournalResult {
return ensureAgentSpawnJournal(this.store, input);
}
sessionExecutionProfile(sessionId: string, ownerId: string): SessionExecutionProfile {
const session = this.readSession(sessionId);
this.assertSessionOwner(session, ownerId);
return readSessionExecutionProfile(this.store, sessionId);
}
migrateSessionExecutionProfile(
input: MigrateSessionExecutionProfileInput,
): MigrateSessionExecutionProfileResult {
return this.withTransaction(() => {
const result = migrateSessionExecutionProfile(this.store, input, Date.now());
this.appendEvent({
sessionId: input.sessionId,
type: "session.execution_profile_migrated",
payload: {
previousGeneration: result.previous.generation,
profileGeneration: result.profile.generation,
adapterId: result.profile.adapterId,
executionRole: result.profile.executionRole,
staleBindingIds: result.staleBindingIds,
reason: input.reason,
},
});
return result;
});
}
executionPolicyForSession(sessionId: string): Pick<AgentSession, "executionRole" | "providerBoundary" | "defaultAdapterId"> {
const session = this.readSession(sessionId);
return {
executionRole: session.executionRole,
providerBoundary: session.providerBoundary,
defaultAdapterId: session.defaultAdapterId,
};
}
executionPolicyForOwnedSession(
sessionId: string,
ownerId: string,
): Pick<AgentSession, "executionRole" | "providerBoundary" | "defaultAdapterId"> {
const session = this.readSession(sessionId);
this.assertSessionOwner(session, ownerId);
return {
executionRole: session.executionRole,
providerBoundary: session.providerBoundary,
defaultAdapterId: session.defaultAdapterId,
};
}
listSessions(input: ListSessionsInput = {}): KernelSessionSummary[] {
const where: string[] = [];
const values: unknown[] = [];
if (input.ownerId) {
where.push("owner_id = ?");
values.push(input.ownerId);
}
if (input.status) {
where.push("status = ?");
values.push(input.status);
}
if (input.surfaceKind) {
where.push("surface_kind = ?");
values.push(input.surfaceKind);
}
if (input.executionRole) {
where.push("execution_role = ?");
values.push(input.executionRole);
}
if (input.beforeUpdatedAtMs !== undefined) {
where.push("updated_at_ms < ?");
values.push(input.beforeUpdatedAtMs);
}
const limit = boundedLimit(input.limit, 50, 200);
const sessions = this.store
.allRows(
`SELECT * FROM sessions
${where.length ? `WHERE ${where.join(" AND ")}` : ""}
ORDER BY last_activity_at_ms DESC, created_at_ms DESC
LIMIT ?`,
[...values, limit],
)
.map((row) => this.readSession(String(row.session_id)));
return sessions.map((session) => ({
session,
latestRun: this.readLatestRunForSession(session.sessionId),
activeRun: this.readActiveRunForSession(session.sessionId),
adapterBindings: this.readBindingsForSession(session.sessionId),
}));
}
resolveSurfaceSession(input: ResolveSurfaceSessionInput & { chatFirstCapability?: ChatFirstCapabilityProjection }): ResolveSurfaceSessionResult {
const capability = input.chatFirstCapability;
const { chatFirstCapability: _ignored, ...sessionInput } = input;
const resolved = resolveSurfaceSession(this.store, sessionInput, () => Date.now());
if (input.surfaceRef.surfaceKind !== "main_chat") return resolved;
if (capability && (!Number.isSafeInteger(capability.controlGeneration) || capability.controlGeneration < 0)) {
throw new Error("chat-first capability requires a non-negative control generation");
}
// Capability-less lookups can happen while auth and root-shell control are
// still converging (for example, a journal projection read during startup).
// They remain capability-off for that read, but must not consume the one
// immutable server-derived sample for the runtime session.
if (capability === undefined) return resolved;
const key = `${input.ownerId}:${resolved.agentSessionId}`;
const previous = this.chatFirstCapabilities.get(key);
const sampled: ChatFirstCapabilityProjection = capability;
if (
previous
&& (previous.chatFirstUi !== sampled.chatFirstUi || previous.controlGeneration !== sampled.controlGeneration)
) {
throw new Error("chat-first capability is immutable for the runtime session");
}
if (!previous) this.chatFirstCapabilities.set(key, Object.freeze({ ...sampled }));
return resolved;
}
importLegacyMainChatSessions(
input: { ownerId: string; entries: LegacyMainChatSessionEntry[] },
): LegacyMainChatSessionImportReceipt {
return importLegacyMainChatSessions(this.store, input, () => Date.now());
}
clearOwnerState(ownerId: string): { invalidatedBindingIds: string[] } {
for (const key of this.chatFirstCapabilities.keys()) {
if (key.startsWith(`${ownerId}:`)) this.chatFirstCapabilities.delete(key);
}
return clearOwnerSurfaceState(this.store, ownerId, () => Date.now());
}
invalidateBindings(input: InvalidateBindingsInput): InvalidateBindingsResult {
const session = this.findExistingSession(input);
const sessionIds = session ? [session.sessionId] : this.findInvalidationSessionIds(input);
if (sessionIds.length === 0) {
return { invalidatedBindingIds: [] };
}
const rows = this.store.allRows(
`SELECT binding_id, session_id
FROM adapter_bindings
WHERE session_id IN (${placeholders(sessionIds.length)})
AND status = ?
${input.adapterId ? "AND adapter_id = ?" : ""}`,
input.adapterId ? [...sessionIds, "active", input.adapterId] : [...sessionIds, "active"],
);
const invalidatedBindingIds = rows.map((row) => String(row.binding_id));
if (invalidatedBindingIds.length === 0) {
return { sessionId: session?.sessionId, invalidatedBindingIds };
}
const now = Date.now();
this.withTransaction(() => {
for (const bindingId of invalidatedBindingIds) {
this.updateBinding(bindingId, {
status: "invalid",
invalidatedAtMs: now,
updatedAtMs: now,
});
this.appendEvent({
sessionId: String(rows.find((row) => String(row.binding_id) === bindingId)?.session_id),
runId: null,
attemptId: null,
type: "binding.stale",
payload: {
bindingId,
adapterId: input.adapterId,
reason: input.reason ?? "invalidate_session",
},
});
}
});
return { sessionId: session?.sessionId, invalidatedBindingIds };
}
staleProcessLocalBindings(input: StaleProcessLocalBindingsInput): StaleProcessLocalBindingsResult {
const rows = this.store.allRows(
`SELECT binding_id, session_id
FROM adapter_bindings
WHERE adapter_id = ?
AND resume_fidelity = ?
AND status = ?`,
[input.adapterId, "none", "active"],
);
const staleBindingIds = rows.map((row) => String(row.binding_id));
if (staleBindingIds.length === 0) {
return { staleBindingIds };
}
const now = Date.now();
this.withTransaction(() => {
for (const row of rows) {
const bindingId = String(row.binding_id);
this.updateBinding(bindingId, {
status: "stale",
invalidatedAtMs: now,
updatedAtMs: now,
});
this.appendEvent({
sessionId: String(row.session_id),
runId: null,
attemptId: null,
type: "binding.stale",
payload: {
bindingId,
adapterId: input.adapterId,
reason: input.reason,
},
});
}
});
return { staleBindingIds };
}
}