stitchkit 0.88.0 → 0.89.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/CHANGELOG.md +126 -0
  2. package/dist/agent-runtime/child-blocking.d.ts +17 -0
  3. package/dist/agent-runtime/child-blocking.d.ts.map +1 -0
  4. package/dist/agent-runtime/child-budget-policy.d.ts +23 -0
  5. package/dist/agent-runtime/child-budget-policy.d.ts.map +1 -0
  6. package/dist/agent-runtime/children-contract.d.ts +155 -0
  7. package/dist/agent-runtime/children-contract.d.ts.map +1 -0
  8. package/dist/agent-runtime/children.d.ts +4 -99
  9. package/dist/agent-runtime/children.d.ts.map +1 -1
  10. package/dist/agent-runtime/coding-shell-process.d.ts +33 -0
  11. package/dist/agent-runtime/coding-shell-process.d.ts.map +1 -0
  12. package/dist/agent-runtime/coding-tool-contract.d.ts +18 -0
  13. package/dist/agent-runtime/coding-tool-contract.d.ts.map +1 -1
  14. package/dist/agent-runtime/coding-tool-files.d.ts.map +1 -1
  15. package/dist/agent-runtime/coding-tool-listing.d.ts.map +1 -1
  16. package/dist/agent-runtime/coding-tool-search-patch.d.ts.map +1 -1
  17. package/dist/agent-runtime/coding-tool-shell.d.ts.map +1 -1
  18. package/dist/agent-runtime/coding-tool-utf8.d.ts +28 -0
  19. package/dist/agent-runtime/coding-tool-utf8.d.ts.map +1 -0
  20. package/dist/agent-runtime/contained-files.d.ts +20 -0
  21. package/dist/agent-runtime/contained-files.d.ts.map +1 -1
  22. package/dist/agent-runtime/durability-contract.d.ts +176 -0
  23. package/dist/agent-runtime/durability-contract.d.ts.map +1 -0
  24. package/dist/agent-runtime/durability-json.d.ts +3 -0
  25. package/dist/agent-runtime/durability-json.d.ts.map +1 -0
  26. package/dist/agent-runtime/durability-ledger.d.ts +16 -0
  27. package/dist/agent-runtime/durability-ledger.d.ts.map +1 -0
  28. package/dist/agent-runtime/durability-scheduler.d.ts +29 -0
  29. package/dist/agent-runtime/durability-scheduler.d.ts.map +1 -0
  30. package/dist/agent-runtime/durability.d.ts +14 -0
  31. package/dist/agent-runtime/durability.d.ts.map +1 -0
  32. package/dist/agent-runtime/event-search.d.ts.map +1 -1
  33. package/dist/agent-runtime/harness-contract.d.ts +37 -0
  34. package/dist/agent-runtime/harness-contract.d.ts.map +1 -1
  35. package/dist/agent-runtime/harness.d.ts +8 -0
  36. package/dist/agent-runtime/harness.d.ts.map +1 -1
  37. package/dist/agent-runtime/prompt.d.ts +23 -0
  38. package/dist/agent-runtime/prompt.d.ts.map +1 -1
  39. package/dist/agent-runtime/run-execution.d.ts.map +1 -1
  40. package/dist/agent-runtime/runtime-internals.d.ts +4 -0
  41. package/dist/agent-runtime/runtime-internals.d.ts.map +1 -1
  42. package/dist/agent-runtime/runtime.d.ts +13 -0
  43. package/dist/agent-runtime/runtime.d.ts.map +1 -1
  44. package/dist/agent-runtime/sandbox-broker.d.ts +7 -0
  45. package/dist/agent-runtime/sandbox-broker.d.ts.map +1 -0
  46. package/dist/agent-runtime/sandbox-bubblewrap-args.d.ts +9 -0
  47. package/dist/agent-runtime/sandbox-bubblewrap-args.d.ts.map +1 -0
  48. package/dist/agent-runtime/sandbox-bubblewrap-storage.d.ts +22 -0
  49. package/dist/agent-runtime/sandbox-bubblewrap-storage.d.ts.map +1 -0
  50. package/dist/agent-runtime/sandbox-bubblewrap.d.ts +14 -0
  51. package/dist/agent-runtime/sandbox-bubblewrap.d.ts.map +1 -0
  52. package/dist/agent-runtime/sandbox-coding-adapter.d.ts +15 -0
  53. package/dist/agent-runtime/sandbox-coding-adapter.d.ts.map +1 -0
  54. package/dist/agent-runtime/sandbox-coding.d.ts +15 -0
  55. package/dist/agent-runtime/sandbox-coding.d.ts.map +1 -0
  56. package/dist/agent-runtime/sandbox-contract.d.ts +93 -0
  57. package/dist/agent-runtime/sandbox-contract.d.ts.map +1 -0
  58. package/dist/agent-runtime/sandbox-process-owner.d.ts +11 -0
  59. package/dist/agent-runtime/sandbox-process-owner.d.ts.map +1 -0
  60. package/dist/agent-runtime/sandbox-process.d.ts +8 -0
  61. package/dist/agent-runtime/sandbox-process.d.ts.map +1 -0
  62. package/dist/agent-runtime/sandbox-session.d.ts +5 -0
  63. package/dist/agent-runtime/sandbox-session.d.ts.map +1 -0
  64. package/dist/agent-runtime/sandbox.d.ts +19 -0
  65. package/dist/agent-runtime/sandbox.d.ts.map +1 -1
  66. package/dist/agent-runtime/sqlite-purge.d.ts.map +1 -1
  67. package/dist/agent-runtime/sqlite.d.ts.map +1 -1
  68. package/dist/agent-runtime/store-driver.d.ts +127 -0
  69. package/dist/agent-runtime/store-driver.d.ts.map +1 -1
  70. package/dist/agent-runtime/store-events.d.ts +104 -0
  71. package/dist/agent-runtime/store-events.d.ts.map +1 -1
  72. package/dist/agent-runtime/store-migrations/v2-to-v3.d.ts +14 -0
  73. package/dist/agent-runtime/store-migrations/v2-to-v3.d.ts.map +1 -0
  74. package/dist/agent-runtime/store.d.ts +115 -0
  75. package/dist/agent-runtime/store.d.ts.map +1 -1
  76. package/dist/agent-runtime-coding-tools.js +11 -1247
  77. package/dist/agent-runtime-harness.d.ts +1 -1
  78. package/dist/agent-runtime-harness.d.ts.map +1 -1
  79. package/dist/agent-runtime-harness.js +65 -15
  80. package/dist/agent-runtime-openrouter.d.ts +3 -12
  81. package/dist/agent-runtime-openrouter.d.ts.map +1 -1
  82. package/dist/agent-runtime-openrouter.js +10 -7
  83. package/dist/agent-runtime-sandbox.d.ts +5 -0
  84. package/dist/agent-runtime-sandbox.d.ts.map +1 -0
  85. package/dist/agent-runtime-sandbox.js +797 -0
  86. package/dist/agent-runtime-sqlite-bun.js +3 -3
  87. package/dist/agent-runtime-sqlite-node.js +3 -3
  88. package/dist/agent-runtime.d.ts +5 -4
  89. package/dist/agent-runtime.d.ts.map +1 -1
  90. package/dist/agent-runtime.js +259 -65
  91. package/dist/application.js +26 -26
  92. package/dist/cli.js +8 -8
  93. package/dist/{index-rcgsnrr6.js → index-31jw8fjq.js} +3 -3
  94. package/dist/{index-9hw9eq48.js → index-7601qfg1.js} +14 -14
  95. package/dist/{index-3xnq72rz.js → index-7rkhw9ec.js} +14 -1
  96. package/dist/{index-0vfdh685.js → index-8c3a7m4g.js} +1 -1
  97. package/dist/{index-t7gh5w3v.js → index-bn2cjajt.js} +27 -6
  98. package/dist/index-cp20z0n6.js +1351 -0
  99. package/dist/{index-qp3vshvc.js → index-fadnxwna.js} +87 -5
  100. package/dist/{index-v1qk37y9.js → index-hrbajay7.js} +106 -4
  101. package/dist/{index-p86mxmpp.js → index-hw2chdvy.js} +646 -43
  102. package/dist/{index-0ydchz76.js → index-mnm4qp0p.js} +5 -5
  103. package/dist/{index-1szetq6f.js → index-q3ve2d1k.js} +30 -8
  104. package/dist/{index-6krwcdcf.js → index-zwkgfq1s.js} +95 -85
  105. package/dist/{index-wn9cb82y.js → index-zyvby92n.js} +12 -9
  106. package/dist/index.js +13 -13
  107. package/dist/server/index.js +9 -9
  108. package/dist/testing/agent-store-conformance.d.ts.map +1 -1
  109. package/dist/testing.js +57 -23
  110. package/dist/tool-invoker.js +6 -6
  111. package/dist/tools/agent.d.ts +6 -0
  112. package/dist/tools/agent.d.ts.map +1 -1
  113. package/dist/tools/connections/define.d.ts +6 -0
  114. package/dist/tools/connections/define.d.ts.map +1 -0
  115. package/dist/tools/connections/errors.d.ts +50 -0
  116. package/dist/tools/connections/errors.d.ts.map +1 -0
  117. package/dist/tools/connections/http.d.ts +3 -0
  118. package/dist/tools/connections/http.d.ts.map +1 -0
  119. package/dist/tools/connections/index.d.ts +19 -0
  120. package/dist/tools/connections/index.d.ts.map +1 -0
  121. package/dist/tools/connections/index.js +1047 -0
  122. package/dist/tools/connections/instance.d.ts +3 -0
  123. package/dist/tools/connections/instance.d.ts.map +1 -0
  124. package/dist/tools/connections/json-rpc.d.ts +8 -0
  125. package/dist/tools/connections/json-rpc.d.ts.map +1 -0
  126. package/dist/tools/connections/limits.d.ts +36 -0
  127. package/dist/tools/connections/limits.d.ts.map +1 -0
  128. package/dist/tools/connections/mcp-client.d.ts +44 -0
  129. package/dist/tools/connections/mcp-client.d.ts.map +1 -0
  130. package/dist/tools/connections/mcp.d.ts +5 -0
  131. package/dist/tools/connections/mcp.d.ts.map +1 -0
  132. package/dist/tools/connections/mount.d.ts +12 -0
  133. package/dist/tools/connections/mount.d.ts.map +1 -0
  134. package/dist/tools/connections/openapi-document.d.ts +4 -0
  135. package/dist/tools/connections/openapi-document.d.ts.map +1 -0
  136. package/dist/tools/connections/openapi.d.ts +5 -0
  137. package/dist/tools/connections/openapi.d.ts.map +1 -0
  138. package/dist/tools/connections/runtime.d.ts +29 -0
  139. package/dist/tools/connections/runtime.d.ts.map +1 -0
  140. package/dist/tools/connections/schema-budget.d.ts +8 -0
  141. package/dist/tools/connections/schema-budget.d.ts.map +1 -0
  142. package/dist/tools/connections/sse-frames.d.ts +7 -0
  143. package/dist/tools/connections/sse-frames.d.ts.map +1 -0
  144. package/dist/tools/connections/sse-session.d.ts +26 -0
  145. package/dist/tools/connections/sse-session.d.ts.map +1 -0
  146. package/dist/tools/connections/ssrf.d.ts +15 -0
  147. package/dist/tools/connections/ssrf.d.ts.map +1 -0
  148. package/dist/tools/connections/types.d.ts +60 -0
  149. package/dist/tools/connections/types.d.ts.map +1 -0
  150. package/dist/tools/durability-context.d.ts +7 -0
  151. package/dist/tools/durability-context.d.ts.map +1 -0
  152. package/dist/tools/execute.d.ts +2 -0
  153. package/dist/tools/execute.d.ts.map +1 -1
  154. package/dist/tools/registry.d.ts +38 -0
  155. package/dist/tools/registry.d.ts.map +1 -0
  156. package/dist/tools/runtime-tool.d.ts +4 -0
  157. package/dist/tools/runtime-tool.d.ts.map +1 -1
  158. package/dist/tools.d.ts +1 -0
  159. package/dist/tools.d.ts.map +1 -1
  160. package/dist/tools.js +83 -26
  161. package/llms-full.txt +246 -7
  162. package/llms.txt +1 -0
  163. package/package.json +9 -1
  164. package/dist/{index-evcnmsc3.js → index-t7fkyxcr.js} +3 -3
@@ -7,9 +7,10 @@ import {
7
7
  AgentConversationPurgedError,
8
8
  AgentHistoryMutationSchema,
9
9
  AgentRuntimeHeadSchema,
10
+ AgentSeedReceiptSchema,
10
11
  AgentStoredRunSchema,
11
12
  createAgentRuntimeStore
12
- } from "./index-v1qk37y9.js";
13
+ } from "./index-hrbajay7.js";
13
14
  import {
14
15
  AgentRecoverableDescriptorSchema,
15
16
  AgentRecoverablePageSchema,
@@ -18,7 +19,7 @@ import {
18
19
  AppendAgentStoreEventSchema,
19
20
  agentStoreEventDraft,
20
21
  canonicalAgentJson
21
- } from "./index-6krwcdcf.js";
22
+ } from "./index-zwkgfq1s.js";
22
23
  import {
23
24
  AgentMessageSchema,
24
25
  AgentRunSchema
@@ -36,6 +37,7 @@ var OWNED_TABLES = [
36
37
  "stitchkit_agent_runtime_projections",
37
38
  "stitchkit_agent_runtime_events",
38
39
  "stitchkit_agent_runtime_messages",
40
+ "stitchkit_agent_runtime_seeds",
39
41
  "stitchkit_agent_runtime_admissions",
40
42
  "stitchkit_agent_runtime_runs",
41
43
  "stitchkit_agent_runtime_heads"
@@ -263,6 +265,23 @@ function createAgentRuntimeSqliteV2Tables(database) {
263
265
  createDurableTables(database);
264
266
  }
265
267
 
268
+ // src/agent-runtime/store-migrations/v2-to-v3.ts
269
+ function createAgentRuntimeSqliteV3Tables(database) {
270
+ database.exec(`
271
+ CREATE TABLE IF NOT EXISTS stitchkit_agent_runtime_seeds (
272
+ conversation_id TEXT NOT NULL,
273
+ seed_key TEXT NOT NULL,
274
+ message_ids TEXT NOT NULL,
275
+ created_at TEXT NOT NULL,
276
+ PRIMARY KEY (conversation_id, seed_key)
277
+ );
278
+ `);
279
+ }
280
+ function migrateAgentRuntimeSqliteV2ToV3(database) {
281
+ createAgentRuntimeSqliteV3Tables(database);
282
+ database.prepare("UPDATE stitchkit_agent_runtime_meta SET value = '3' WHERE key = 'schema_version'").run();
283
+ }
284
+
266
285
  // src/agent-runtime/sqlite.ts
267
286
  var HeadRowSchema = z2.object({ version: z2.number().int().nonnegative() });
268
287
  var RunRowSchema = z2.object({
@@ -276,6 +295,11 @@ var AdmissionRowSchema2 = z2.object({
276
295
  run_id: z2.string(),
277
296
  assistant_message_id: z2.string()
278
297
  });
298
+ var SeedRowSchema = z2.object({
299
+ conversation_id: z2.string(),
300
+ seed_key: z2.string(),
301
+ message_ids: z2.string()
302
+ });
279
303
  var MessageRowSchema = z2.object({ payload: z2.string() });
280
304
  var PositionedMessageRowSchema = z2.object({ position: z2.number().int().nonnegative() });
281
305
  var RecoverableRowSchema = z2.object({
@@ -325,12 +349,13 @@ var ArchiveSpillRowSchema = z2.object({
325
349
  authorization_payload: z2.string().nullable(),
326
350
  payload: z2.instanceof(Uint8Array)
327
351
  });
328
- var SCHEMA_VERSION = 2;
352
+ var SCHEMA_VERSION = 3;
329
353
  var TABLES = [
330
354
  "stitchkit_agent_runtime_heads",
331
355
  "stitchkit_agent_runtime_runs",
332
356
  "stitchkit_agent_runtime_admissions",
333
357
  "stitchkit_agent_runtime_messages",
358
+ "stitchkit_agent_runtime_seeds",
334
359
  "stitchkit_agent_runtime_events",
335
360
  "stitchkit_agent_runtime_projections",
336
361
  "stitchkit_agent_runtime_spills",
@@ -418,6 +443,13 @@ function initializeAgentRuntimeSqlite(database) {
418
443
  const version = Number(MetaRowSchema.parse(versionRow).value);
419
444
  if (version === 1) {
420
445
  migrateAgentRuntimeSqliteV1ToV2(database);
446
+ migrateAgentRuntimeSqliteV2ToV3(database);
447
+ initializeSqliteConversationPurge(database);
448
+ database.exec("COMMIT");
449
+ return;
450
+ }
451
+ if (version === 2) {
452
+ migrateAgentRuntimeSqliteV2ToV3(database);
421
453
  initializeSqliteConversationPurge(database);
422
454
  database.exec("COMMIT");
423
455
  return;
@@ -474,7 +506,8 @@ function initializeAgentRuntimeSqlite(database) {
474
506
  );
475
507
  `);
476
508
  createAgentRuntimeSqliteV2Tables(database);
477
- database.prepare("INSERT INTO stitchkit_agent_runtime_meta (key, value) VALUES ('schema_version', '2')").run();
509
+ createAgentRuntimeSqliteV3Tables(database);
510
+ database.prepare("INSERT INTO stitchkit_agent_runtime_meta (key, value) VALUES ('schema_version', '3')").run();
478
511
  initializeSqliteConversationPurge(database);
479
512
  database.exec("COMMIT");
480
513
  } catch (error) {
@@ -646,6 +679,32 @@ function createSqliteAgentRuntimeStore(config) {
646
679
  `).run(receipt.conversationId, receipt.idempotencyKey, receipt.input.id, receipt.runId, receipt.assistantMessageId, encodeJson(receipt.input));
647
680
  }
648
681
  },
682
+ seeds: {
683
+ async load(transaction, input) {
684
+ const value = transaction.prepare(`
685
+ SELECT conversation_id, seed_key, message_ids
686
+ FROM stitchkit_agent_runtime_seeds
687
+ WHERE conversation_id = ? AND seed_key = ?
688
+ `).get(input.conversationId, input.seedKey);
689
+ if (missing(value))
690
+ return;
691
+ const row = SeedRowSchema.parse(value);
692
+ return AgentSeedReceiptSchema.parse({
693
+ schemaVersion: 1,
694
+ conversationId: row.conversation_id,
695
+ seedKey: row.seed_key,
696
+ messageIds: z2.array(z2.string().min(1)).parse(parseJson2(row.message_ids))
697
+ });
698
+ },
699
+ async create(transaction, rawReceipt) {
700
+ const receipt = AgentSeedReceiptSchema.parse(rawReceipt);
701
+ transaction.prepare(`
702
+ INSERT INTO stitchkit_agent_runtime_seeds (
703
+ conversation_id, seed_key, message_ids, created_at
704
+ ) VALUES (?, ?, ?, ?)
705
+ `).run(receipt.conversationId, receipt.seedKey, encodeJson(receipt.messageIds), new Date().toISOString());
706
+ }
707
+ },
649
708
  history: {
650
709
  async load(transaction, conversationId) {
651
710
  return transaction.prepare(`
@@ -653,9 +712,32 @@ function createSqliteAgentRuntimeStore(config) {
653
712
  WHERE conversation_id = ? AND active = 1 ORDER BY position ASC
654
713
  `).all(conversationId).map((value) => AgentMessageSchema.parse(parseJson2(MessageRowSchema.parse(value).payload)));
655
714
  },
715
+ async hasMessage(transaction, input) {
716
+ const row = transaction.prepare(`
717
+ SELECT count(*) AS count FROM stitchkit_agent_runtime_messages
718
+ WHERE conversation_id = ? AND id = ?
719
+ `).get(input.conversationId, input.messageId);
720
+ return CountRowSchema.parse(row).count > 0;
721
+ },
656
722
  async apply(transaction, rawMutation) {
657
723
  const mutation = AgentHistoryMutationSchema.parse(rawMutation);
658
- const message = mutation.type === "admit" ? mutation.input : mutation.type === "upsert-assistant" ? mutation.message : mutation.summary;
724
+ const message = mutation.type === "admit" ? mutation.input : mutation.type === "upsert-assistant" ? mutation.message : mutation.type === "seed" ? mutation.message : mutation.summary;
725
+ if (mutation.type === "seed") {
726
+ transaction.prepare(`
727
+ UPDATE stitchkit_agent_runtime_messages SET position = position + 1
728
+ WHERE conversation_id = ?
729
+ `).run(mutation.message.conversationId);
730
+ transaction.prepare(`
731
+ INSERT INTO stitchkit_agent_runtime_messages
732
+ (conversation_id, id, position, active, payload)
733
+ VALUES (?, ?, 0, 1, ?)
734
+ ON CONFLICT (conversation_id, id) DO UPDATE SET
735
+ position = excluded.position,
736
+ active = excluded.active,
737
+ payload = excluded.payload
738
+ `).run(mutation.message.conversationId, mutation.message.id, encodeJson(mutation.message));
739
+ return;
740
+ }
659
741
  if (mutation.type === "replace-compacted-range") {
660
742
  const parameters = mutation.replacedMessageIds;
661
743
  const rows = transaction.prepare(`
@@ -14,12 +14,13 @@ import {
14
14
  RecoverAgentRunSchema,
15
15
  ReplaceCompactedRangeSchema,
16
16
  RequestRunInterruptSchema,
17
+ SeedConversationInputSchema,
17
18
  agentStoreEventDraft,
18
19
  assistantStatus,
19
20
  canonicalAgentJson,
20
21
  decodeAgentConversationArchive,
21
22
  encodeAgentConversationArchive
22
- } from "./index-6krwcdcf.js";
23
+ } from "./index-zwkgfq1s.js";
23
24
  import {
24
25
  AgentMessageSchema,
25
26
  AgentRecordIdSchema,
@@ -105,6 +106,12 @@ var AgentAdmissionReceiptSchema = z2.object({
105
106
  runId: AgentRecordIdSchema,
106
107
  assistantMessageId: AgentRecordIdSchema
107
108
  });
109
+ var AgentSeedReceiptSchema = z2.object({
110
+ schemaVersion: z2.literal(1),
111
+ conversationId: AgentRecordIdSchema,
112
+ seedKey: z2.string().min(1),
113
+ messageIds: z2.array(AgentRecordIdSchema).min(1)
114
+ });
108
115
  var AgentHistoryMutationSchema = z2.discriminatedUnion("type", [
109
116
  z2.object({ type: z2.literal("admit"), input: AgentMessageSchema }),
110
117
  z2.object({
@@ -115,6 +122,10 @@ var AgentHistoryMutationSchema = z2.discriminatedUnion("type", [
115
122
  type: z2.literal("replace-compacted-range"),
116
123
  replacedMessageIds: z2.array(AgentRecordIdSchema).min(1),
117
124
  summary: AgentMessageSchema
125
+ }),
126
+ z2.object({
127
+ type: z2.literal("seed"),
128
+ message: AgentMessageSchema
118
129
  })
119
130
  ]);
120
131
  var AgentRecoverableScanInputSchema = z2.object({
@@ -279,6 +290,7 @@ function applied(current, input, effects) {
279
290
  }),
280
291
  ...effects?.runRecords?.length && { runRecords: effects.runRecords },
281
292
  ...effects?.admissionReceipt && { admissionReceipt: effects.admissionReceipt },
293
+ ...effects?.seedReceipt && { seedReceipt: effects.seedReceipt },
282
294
  ...effects?.historyMutations?.length && { historyMutations: effects.historyMutations }
283
295
  };
284
296
  }
@@ -318,6 +330,28 @@ function reduceStore(current, operation) {
318
330
  historyMutations: [{ type: "admit", input: input.input }]
319
331
  });
320
332
  }
333
+ if (operation.type === "seed") {
334
+ const conversationId2 = operation.input.conversationId;
335
+ if (current.conversationId !== conversationId2)
336
+ return { outcome: "not_found" };
337
+ for (const message of operation.input.inputs) {
338
+ if (message.conversationId !== conversationId2 || message.role !== "user" || message.status !== "committed" || message.runId !== undefined) {
339
+ throw new TypeError("Seeded conversation input must be committed unowned user messages");
340
+ }
341
+ }
342
+ return applied(current, { messages: [...operation.input.inputs, ...current.messages] }, {
343
+ seedReceipt: AgentSeedReceiptSchema.parse({
344
+ schemaVersion: 1,
345
+ conversationId: conversationId2,
346
+ seedKey: operation.input.seedKey,
347
+ messageIds: operation.input.inputs.map((message) => message.id)
348
+ }),
349
+ historyMutations: [...operation.input.inputs].reverse().map((message) => ({
350
+ type: "seed",
351
+ message
352
+ }))
353
+ });
354
+ }
321
355
  const conversationId = operation.input.conversationId;
322
356
  const run = operation.type === "compact" ? undefined : current.runs.find((candidate) => candidate.id === operation.input.runId);
323
357
  if (operation.type !== "compact" && !run)
@@ -619,7 +653,7 @@ function createAgentRuntimeStore(driver) {
619
653
  if (await driver.conversations?.isPurged(transaction, conversationId)) {
620
654
  throw new AgentConversationPurgedError;
621
655
  }
622
- const operationRunId = operation.type === "accept" ? operation.input.coalesceIntoRunId : operation.type === "compact" ? undefined : operation.input.runId;
656
+ const operationRunId = operation.type === "accept" ? operation.input.coalesceIntoRunId : operation.type === "compact" || operation.type === "seed" ? undefined : operation.input.runId;
623
657
  const [stored, messages, activeRecords, operationRecord, duplicateReceipt] = await Promise.all([
624
658
  driver.head.load(transaction, conversationId),
625
659
  driver.history.load(transaction, conversationId),
@@ -637,6 +671,34 @@ function createAgentRuntimeStore(driver) {
637
671
  });
638
672
  const records = mergeRunRecords(activeRecords, referencedRecords, operationRecord ? [operationRecord] : []);
639
673
  const current = snapshotOf(head, messages, records);
674
+ if (operation.type === "seed") {
675
+ const existing = await driver.seeds.load(transaction, {
676
+ conversationId,
677
+ seedKey: operation.input.seedKey
678
+ });
679
+ if (existing)
680
+ return { outcome: "applied", snapshot: current };
681
+ let collisions = 0;
682
+ for (const message of operation.input.inputs) {
683
+ if (current.messages.some((candidate) => candidate.id === message.id) || driver.history.hasMessage && await driver.history.hasMessage(transaction, {
684
+ conversationId,
685
+ messageId: message.id
686
+ }))
687
+ collisions++;
688
+ }
689
+ if (collisions > 0 && collisions < operation.input.inputs.length) {
690
+ throw new TypeError("Partial instruction seed collision: restore the complete seed before retrying");
691
+ }
692
+ if (collisions > 0) {
693
+ await driver.seeds.create(transaction, AgentSeedReceiptSchema.parse({
694
+ schemaVersion: 1,
695
+ conversationId,
696
+ seedKey: operation.input.seedKey,
697
+ messageIds: operation.input.inputs.map((message) => message.id)
698
+ }));
699
+ return { outcome: "applied", snapshot: current };
700
+ }
701
+ }
640
702
  if (duplicateReceipt) {
641
703
  const duplicateRecord = await driver.runs.load(transaction, {
642
704
  conversationId,
@@ -712,6 +774,9 @@ function createAgentRuntimeStore(driver) {
712
774
  if (reduced.admissionReceipt) {
713
775
  await driver.admissions.create(transaction, reduced.admissionReceipt);
714
776
  }
777
+ if (reduced.seedReceipt) {
778
+ await driver.seeds.create(transaction, reduced.seedReceipt);
779
+ }
715
780
  for (const mutation of reduced.historyMutations ?? []) {
716
781
  await driver.history.apply(transaction, mutation);
717
782
  }
@@ -877,6 +942,10 @@ function createAgentRuntimeStore(driver) {
877
942
  type: "compact",
878
943
  input: ReplaceCompactedRangeSchema.parse(input)
879
944
  }),
945
+ seedConversationInput: (input) => mutate({
946
+ type: "seed",
947
+ input: SeedConversationInputSchema.parse(input)
948
+ }),
880
949
  async scanRecoverable(input) {
881
950
  const parsed = AgentRecoverableScanInputSchema.parse(input);
882
951
  return AgentRecoverablePageSchema.parse(await driver.scanRecoverable(parsed));
@@ -906,6 +975,7 @@ function createMemoryAgentRuntimeStore() {
906
975
  let heads = new Map;
907
976
  let runs = new Map;
908
977
  let admissions = new Map;
978
+ let seeds = new Map;
909
979
  let histories = new Map;
910
980
  let events = new Map;
911
981
  let transactionTail = Promise.resolve();
@@ -924,6 +994,7 @@ function createMemoryAgentRuntimeStore() {
924
994
  heads: cloneHeadMap(heads),
925
995
  runs: cloneNestedMap(runs, (record) => AgentStoredRunSchema.parse(structuredClone(record))),
926
996
  admissions: cloneNestedMap(admissions, (receipt) => AgentAdmissionReceiptSchema.parse(structuredClone(receipt))),
997
+ seeds: cloneNestedMap(seeds, (receipt) => AgentSeedReceiptSchema.parse(structuredClone(receipt))),
927
998
  histories: cloneHistoryMap(histories),
928
999
  events: new Map([...events].map(([conversationId, stored]) => [
929
1000
  conversationId,
@@ -936,6 +1007,7 @@ function createMemoryAgentRuntimeStore() {
936
1007
  heads = transaction.heads;
937
1008
  runs = transaction.runs;
938
1009
  admissions = transaction.admissions;
1010
+ seeds = transaction.seeds;
939
1011
  histories = transaction.histories;
940
1012
  events = transaction.events;
941
1013
  return result;
@@ -952,6 +1024,7 @@ function createMemoryAgentRuntimeStore() {
952
1024
  transaction.heads.delete(conversationId);
953
1025
  transaction.runs.delete(conversationId);
954
1026
  transaction.admissions.delete(conversationId);
1027
+ transaction.seeds.delete(conversationId);
955
1028
  transaction.histories.delete(conversationId);
956
1029
  transaction.events.delete(conversationId);
957
1030
  }
@@ -1021,13 +1094,31 @@ function createMemoryAgentRuntimeStore() {
1021
1094
  transaction.admissions.set(receipt.conversationId, conversationAdmissions);
1022
1095
  }
1023
1096
  },
1097
+ seeds: {
1098
+ async load(transaction, input) {
1099
+ const receipt = transaction.seeds.get(input.conversationId)?.get(input.seedKey);
1100
+ return receipt ? AgentSeedReceiptSchema.parse(structuredClone(receipt)) : undefined;
1101
+ },
1102
+ async create(transaction, rawReceipt) {
1103
+ const receipt = AgentSeedReceiptSchema.parse(structuredClone(rawReceipt));
1104
+ const conversationSeeds = transaction.seeds.get(receipt.conversationId) ?? new Map;
1105
+ if (conversationSeeds.has(receipt.seedKey)) {
1106
+ throw new TypeError("Conversation seed identity is already reserved");
1107
+ }
1108
+ conversationSeeds.set(receipt.seedKey, receipt);
1109
+ transaction.seeds.set(receipt.conversationId, conversationSeeds);
1110
+ }
1111
+ },
1024
1112
  history: {
1025
1113
  async load(transaction, conversationId) {
1026
1114
  return (transaction.histories.get(conversationId) ?? []).map((message) => AgentMessageSchema.parse(structuredClone(message)));
1027
1115
  },
1116
+ async hasMessage(transaction, input) {
1117
+ return (transaction.histories.get(input.conversationId) ?? []).some((message) => message.id === input.messageId);
1118
+ },
1028
1119
  async apply(transaction, rawMutation) {
1029
1120
  const mutation = AgentHistoryMutationSchema.parse(rawMutation);
1030
- const conversationId = mutation.type === "admit" ? mutation.input.conversationId : mutation.type === "upsert-assistant" ? mutation.message.conversationId : mutation.summary.conversationId;
1121
+ const conversationId = mutation.type === "admit" ? mutation.input.conversationId : mutation.type === "upsert-assistant" ? mutation.message.conversationId : mutation.type === "seed" ? mutation.message.conversationId : mutation.summary.conversationId;
1031
1122
  const current = transaction.histories.get(conversationId) ?? [];
1032
1123
  if (mutation.type === "admit") {
1033
1124
  transaction.histories.set(conversationId, [...current, mutation.input]);
@@ -1037,6 +1128,17 @@ function createMemoryAgentRuntimeStore() {
1037
1128
  transaction.histories.set(conversationId, replaceMessage(current, mutation.message));
1038
1129
  return;
1039
1130
  }
1131
+ if (mutation.type === "seed") {
1132
+ const existingIndex = current.findIndex((message) => message.id === mutation.message.id);
1133
+ if (existingIndex === -1) {
1134
+ transaction.histories.set(conversationId, [mutation.message, ...current]);
1135
+ } else {
1136
+ const next = [...current];
1137
+ next[existingIndex] = mutation.message;
1138
+ transaction.histories.set(conversationId, next);
1139
+ }
1140
+ return;
1141
+ }
1040
1142
  const replaced = new Set(mutation.replacedMessageIds);
1041
1143
  const positions = current.map((message, index) => replaced.has(message.id) ? index : undefined).filter((index) => index !== undefined);
1042
1144
  const first = positions[0];
@@ -1091,4 +1193,4 @@ function createMemoryAgentRuntimeStore() {
1091
1193
  return createAgentRuntimeStore(driver);
1092
1194
  }
1093
1195
 
1094
- export { AgentConversationPurgeInputSchema, AgentConversationPurgeResultSchema, AgentConversationPurgedError, purgeAgentConversation, AgentRuntimeHeadSchema, AgentStoredRunSchema, AgentAdmissionReceiptSchema, AgentHistoryMutationSchema, ACTIVE_AGENT_RUN_STATES, createAgentRuntimeStore, createMemoryAgentRuntimeStore };
1196
+ export { AgentConversationPurgeInputSchema, AgentConversationPurgeResultSchema, AgentConversationPurgedError, purgeAgentConversation, AgentRuntimeHeadSchema, AgentStoredRunSchema, AgentAdmissionReceiptSchema, AgentSeedReceiptSchema, AgentHistoryMutationSchema, ACTIVE_AGENT_RUN_STATES, createAgentRuntimeStore, createMemoryAgentRuntimeStore };