@opengeni/db 0.28.1 → 0.28.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/{chunk-HT5T62CC.js → chunk-P6CUHXQZ.js} +651 -206
  2. package/dist/chunk-P6CUHXQZ.js.map +1 -0
  3. package/dist/{chunk-DBS5HPEW.js → chunk-VHBFHMPC.js} +7 -1
  4. package/dist/chunk-VHBFHMPC.js.map +1 -0
  5. package/dist/environment-crypto.d.ts +8 -4
  6. package/dist/index.d.ts +265 -20
  7. package/dist/index.js +3966 -2185
  8. package/dist/index.js.map +1 -1
  9. package/dist/lossless-columns.d.ts +58 -0
  10. package/dist/lossless-json.d.ts +39 -0
  11. package/dist/memory-domain.d.ts +3 -6
  12. package/dist/persistence-errors.d.ts +7 -14
  13. package/dist/provision-roles.js +1 -1
  14. package/dist/runtime-posture.d.ts +2 -2
  15. package/dist/schema.d.ts +1419 -271
  16. package/dist/schema.js +9 -1
  17. package/dist/session-realtime-terminal.d.ts +7 -0
  18. package/dist/transcription-recordings-schema.d.ts +44 -6
  19. package/dist/transcription-recordings.d.ts +1 -1
  20. package/drizzle/0065_enrollment_credential_generation.sql +10 -0
  21. package/drizzle/0140_retained_screenshot_artifacts.sql +192 -0
  22. package/drizzle/0176_lossless_canonical_json.sql +975 -0
  23. package/drizzle/0177_session_events_workspace_turn_type_index.sql +5 -0
  24. package/drizzle/0178_permissioned_secret_reads.sql +14 -0
  25. package/drizzle/0179_slack_private_shortcut_delivery_gate.sql +85 -0
  26. package/drizzle/0180_retained_screenshot_lifecycle_fences.sql +273 -0
  27. package/drizzle/0181_connected_machine_removal.sql +52 -0
  28. package/drizzle/0182_connected_machine_remove_session_default.sql +77 -0
  29. package/package.json +4 -4
  30. package/src/database.ts +7 -1
  31. package/src/environment-crypto.ts +22 -9
  32. package/src/index.ts +4253 -1601
  33. package/src/lossless-columns.ts +35 -0
  34. package/src/lossless-json.ts +322 -0
  35. package/src/memory-domain.ts +5 -44
  36. package/src/persistence-errors.ts +29 -34
  37. package/src/runtime-posture.ts +6 -0
  38. package/src/schema.ts +260 -38
  39. package/src/session-control.ts +125 -76
  40. package/src/session-queue-commands.ts +325 -234
  41. package/src/session-realtime-context.ts +18 -5
  42. package/src/session-realtime-ledger.ts +29 -7
  43. package/src/session-realtime-mirror.ts +4 -2
  44. package/src/session-realtime-terminal.ts +89 -21
  45. package/src/session-realtime.ts +3 -2
  46. package/src/session-tool-call-settlement.ts +29 -15
  47. package/src/transcription-recordings-schema.ts +5 -2
  48. package/src/transcription-recordings.ts +70 -40
  49. package/dist/chunk-DBS5HPEW.js.map +0 -1
  50. package/dist/chunk-HT5T62CC.js.map +0 -1
  51. package/dist/event-payload-sanitizer.d.ts +0 -32
  52. package/src/event-payload-sanitizer.ts +0 -377
@@ -8,6 +8,7 @@ import {
8
8
  } from "@opengeni/contracts";
9
9
  import { and, eq, inArray, sql } from "drizzle-orm";
10
10
  import type { Database } from "./database";
11
+ import { withLosslessContentWriteVersion } from "./lossless-json";
11
12
  import * as schema from "./schema";
12
13
  import { closePendingSessionToolCallsInTransaction } from "./session-tool-call-settlement";
13
14
  import {
@@ -15,6 +16,10 @@ import {
15
16
  renderRealtimeHumanInputResponseContext,
16
17
  } from "./session-realtime-mirror";
17
18
 
19
+ type SessionEventInsertWithPayload = typeof schema.sessionEvents.$inferInsert & {
20
+ payload: unknown;
21
+ };
22
+
18
23
  export const SESSION_ANCESTRY_LIMIT = 10_000;
19
24
 
20
25
  export type WorkspaceControlLockMode = "none" | "share" | "update";
@@ -1940,7 +1945,9 @@ async function enqueueCancelledChildOutboxInTransaction(
1940
1945
  [];
1941
1946
  if (input.rootSession.parentTurnId) {
1942
1947
  const [parentTurn] = await db
1943
- .select({ delegations: schema.sessionTurns.personalConnectionDelegations })
1948
+ .select({
1949
+ delegations: schema.sessionTurns.personalConnectionDelegations,
1950
+ })
1944
1951
  .from(schema.sessionTurns)
1945
1952
  .where(
1946
1953
  and(
@@ -1957,33 +1964,47 @@ async function enqueueCancelledChildOutboxInTransaction(
1957
1964
  `Invalid personal MCP delegation snapshot at session_turns:${input.workspaceId}:${parentSessionId}:${input.rootSession.parentTurnId}`,
1958
1965
  );
1959
1966
  }
1960
- personalConnectionDelegations = parsed.data.map((delegation) => ({ ...delegation }));
1967
+ personalConnectionDelegations = parsed.data.map((delegation) => ({
1968
+ ...delegation,
1969
+ }));
1961
1970
  }
1962
1971
  }
1963
1972
  await db
1964
1973
  .insert(schema.sessionSystemUpdateOutbox)
1965
- .values({
1966
- accountId: input.rootSession.accountId,
1967
- workspaceId: input.workspaceId,
1968
- sourceSessionId: input.rootSession.id,
1969
- targetSessionId: parentSessionId,
1970
- dedupeKey: `child-completion:${input.rootSession.id}:cancelled`,
1971
- kind: "child_terminal_result",
1972
- classification: "info",
1973
- sourceId: input.rootSession.id,
1974
- summary: "Child session was terminally cancelled.",
1975
- payload: {
1976
- type: "child_terminal_result",
1977
- childSessionId: input.rootSession.id,
1978
- status: "cancelled",
1979
- },
1980
- lineage: {
1981
- childSessionId: input.rootSession.id,
1982
- parentSessionId,
1983
- ...(input.rootSession.parentTurnId ? { parentTurnId: input.rootSession.parentTurnId } : {}),
1984
- },
1985
- personalConnectionDelegations,
1986
- })
1974
+ .values(
1975
+ withLosslessContentWriteVersion(
1976
+ withLosslessContentWriteVersion(
1977
+ {
1978
+ accountId: input.rootSession.accountId,
1979
+ workspaceId: input.workspaceId,
1980
+ sourceSessionId: input.rootSession.id,
1981
+ targetSessionId: parentSessionId,
1982
+ dedupeKey: `child-completion:${input.rootSession.id}:cancelled`,
1983
+ kind: "child_terminal_result",
1984
+ classification: "info",
1985
+ sourceId: input.rootSession.id,
1986
+ summary: "Child session was terminally cancelled.",
1987
+ payload: {
1988
+ type: "child_terminal_result",
1989
+ childSessionId: input.rootSession.id,
1990
+ status: "cancelled",
1991
+ },
1992
+ lineage: {
1993
+ childSessionId: input.rootSession.id,
1994
+ parentSessionId,
1995
+ ...(input.rootSession.parentTurnId
1996
+ ? { parentTurnId: input.rootSession.parentTurnId }
1997
+ : {}),
1998
+ },
1999
+ personalConnectionDelegations,
2000
+ },
2001
+ "summary",
2002
+ "summaryCodecVersion",
2003
+ ),
2004
+ "payload",
2005
+ "payloadCodecVersion",
2006
+ ),
2007
+ )
1987
2008
  .onConflictDoNothing({
1988
2009
  target: [
1989
2010
  schema.sessionSystemUpdateOutbox.workspaceId,
@@ -2142,7 +2163,11 @@ async function cancelSessionSubtreeInTransaction(
2142
2163
  });
2143
2164
  await db
2144
2165
  .update(schema.codexCapacityWaiters)
2145
- .set({ status: "superseded", lastWakeReason: "session_cancelled", updatedAt: now })
2166
+ .set({
2167
+ status: "superseded",
2168
+ lastWakeReason: "session_cancelled",
2169
+ updatedAt: now,
2170
+ })
2146
2171
  .where(
2147
2172
  and(
2148
2173
  eq(schema.codexCapacityWaiters.workspaceId, input.workspaceId),
@@ -2178,13 +2203,16 @@ async function cancelSessionSubtreeInTransaction(
2178
2203
  else systemUpdatesBySession.set(update.sessionId, [update]);
2179
2204
  }
2180
2205
 
2181
- const affectedSessionEvents: Array<{ sessionId: string; eventIds: string[] }> = [];
2206
+ const affectedSessionEvents: Array<{
2207
+ sessionId: string;
2208
+ eventIds: string[];
2209
+ }> = [];
2182
2210
  for (const session of input.lockedSessions) {
2183
2211
  if (!sessionIdSet.has(session.id)) continue;
2184
2212
  let sequence = session.lastSequence + (session.id === input.rootSessionId ? 1 : 0);
2185
2213
  const cancelledTurns = cancelledTurnsBySession.get(session.id) ?? [];
2186
2214
  const preinsertedEvents: Array<{ id: string; sequence: number }> = [];
2187
- const eventValues: Array<typeof schema.sessionEvents.$inferInsert> = [];
2215
+ const eventValues: SessionEventInsertWithPayload[] = [];
2188
2216
  for (const update of (systemUpdatesBySession.get(session.id) ?? []).sort((left, right) =>
2189
2217
  left.id.localeCompare(right.id),
2190
2218
  )) {
@@ -2210,7 +2238,10 @@ async function cancelSessionSubtreeInTransaction(
2210
2238
  });
2211
2239
  sequence = closedTools.sequence;
2212
2240
  preinsertedEvents.push(
2213
- ...closedTools.events.map((event) => ({ id: event.id, sequence: event.sequence })),
2241
+ ...closedTools.events.map((event) => ({
2242
+ id: event.id,
2243
+ sequence: event.sequence,
2244
+ })),
2214
2245
  );
2215
2246
  for (const humanInput of (humanInputsByTurn.get(turn.id) ?? []).sort((left, right) =>
2216
2247
  left.id.localeCompare(right.id),
@@ -2224,7 +2255,10 @@ async function cancelSessionSubtreeInTransaction(
2224
2255
  turnId: turn.id,
2225
2256
  turnGeneration: turn.executionGeneration,
2226
2257
  ...(turn.id === session.activeTurnId ? { turnAssociation: "current" as const } : {}),
2227
- payload: { requestId: humanInput.id, response: { outcome: "cancelled" } },
2258
+ payload: {
2259
+ requestId: humanInput.id,
2260
+ response: { outcome: "cancelled" },
2261
+ },
2228
2262
  occurredAt: now,
2229
2263
  });
2230
2264
  }
@@ -2261,13 +2295,16 @@ async function cancelSessionSubtreeInTransaction(
2261
2295
  }
2262
2296
  const inserted =
2263
2297
  eventValues.length > 0
2264
- ? await db.insert(schema.sessionEvents).values(eventValues).returning({
2265
- id: schema.sessionEvents.id,
2266
- sequence: schema.sessionEvents.sequence,
2267
- type: schema.sessionEvents.type,
2268
- turnId: schema.sessionEvents.turnId,
2269
- payload: schema.sessionEvents.payload,
2270
- })
2298
+ ? await db
2299
+ .insert(schema.sessionEvents)
2300
+ .values(withLosslessContentWriteVersion(eventValues, "payload", "payloadCodecVersion"))
2301
+ .returning({
2302
+ id: schema.sessionEvents.id,
2303
+ sequence: schema.sessionEvents.sequence,
2304
+ type: schema.sessionEvents.type,
2305
+ turnId: schema.sessionEvents.turnId,
2306
+ payload: schema.sessionEvents.payload,
2307
+ })
2271
2308
  : [];
2272
2309
  const cancelledHumanInputsById = new Map(
2273
2310
  cancelledHumanInputs
@@ -2548,25 +2585,31 @@ export async function mutateSessionControlInTransaction(
2548
2585
  });
2549
2586
  const [controlEvent] = await db
2550
2587
  .insert(schema.sessionEvents)
2551
- .values({
2552
- accountId: input.accountId,
2553
- workspaceId: input.workspaceId,
2554
- sessionId: input.sessionId,
2555
- sequence: updated.lastSequence + 1,
2556
- type:
2557
- input.action === "pause" || input.action === "cancel"
2558
- ? "session.control.paused"
2559
- : "session.control.resumed",
2560
- payload: {
2561
- operationId: reserved.receipt.id,
2562
- revision,
2563
- actor,
2564
- ...(input.reason ? { reason: input.reason } : {}),
2565
- ...(input.action === "cancel" ? { terminal: true } : {}),
2566
- interruptionCount,
2567
- },
2568
- occurredAt: new Date(),
2569
- })
2588
+ .values(
2589
+ withLosslessContentWriteVersion(
2590
+ {
2591
+ accountId: input.accountId,
2592
+ workspaceId: input.workspaceId,
2593
+ sessionId: input.sessionId,
2594
+ sequence: updated.lastSequence + 1,
2595
+ type:
2596
+ input.action === "pause" || input.action === "cancel"
2597
+ ? "session.control.paused"
2598
+ : "session.control.resumed",
2599
+ payload: {
2600
+ operationId: reserved.receipt.id,
2601
+ revision,
2602
+ actor,
2603
+ ...(input.reason ? { reason: input.reason } : {}),
2604
+ ...(input.action === "cancel" ? { terminal: true } : {}),
2605
+ interruptionCount,
2606
+ },
2607
+ occurredAt: new Date(),
2608
+ },
2609
+ "payload",
2610
+ "payloadCodecVersion",
2611
+ ),
2612
+ )
2570
2613
  .returning({ id: schema.sessionEvents.id });
2571
2614
  if (!controlEvent)
2572
2615
  throw new SessionControlInvariantError("Session control event was not inserted");
@@ -2574,27 +2617,33 @@ export async function mutateSessionControlInTransaction(
2574
2617
  .update(schema.sessions)
2575
2618
  .set({ lastSequence: updated.lastSequence + 1, updatedAt: new Date() })
2576
2619
  .where(eq(schema.sessions.id, input.sessionId));
2577
- await db.insert(schema.auditEvents).values({
2578
- accountId: input.accountId,
2579
- workspaceId: input.workspaceId,
2580
- subjectId: actor,
2581
- action: `session.control.${input.action}`,
2582
- targetType: "session",
2583
- targetId: input.sessionId,
2584
- metadata: {
2585
- operationId: reserved.receipt.id,
2586
- revision,
2587
- interruptionCount,
2588
- ...(input.actor.type === "agent_attempt"
2589
- ? {
2590
- callerSessionId: input.actor.sessionId,
2591
- callerTurnId: input.actor.turnId,
2592
- callerAttemptId: input.actor.attemptId,
2593
- callerExecutionGeneration: input.actor.executionGeneration,
2594
- }
2595
- : {}),
2596
- },
2597
- });
2620
+ await db.insert(schema.auditEvents).values(
2621
+ withLosslessContentWriteVersion(
2622
+ {
2623
+ accountId: input.accountId,
2624
+ workspaceId: input.workspaceId,
2625
+ subjectId: actor,
2626
+ action: `session.control.${input.action}`,
2627
+ targetType: "session",
2628
+ targetId: input.sessionId,
2629
+ metadata: {
2630
+ operationId: reserved.receipt.id,
2631
+ revision,
2632
+ interruptionCount,
2633
+ ...(input.actor.type === "agent_attempt"
2634
+ ? {
2635
+ callerSessionId: input.actor.sessionId,
2636
+ callerTurnId: input.actor.turnId,
2637
+ callerAttemptId: input.actor.attemptId,
2638
+ callerExecutionGeneration: input.actor.executionGeneration,
2639
+ }
2640
+ : {}),
2641
+ },
2642
+ },
2643
+ "metadata",
2644
+ "metadataCodecVersion",
2645
+ ),
2646
+ );
2598
2647
  const cancellation =
2599
2648
  input.action === "cancel"
2600
2649
  ? await cancelSessionSubtreeInTransaction(db, {