@astrosheep/keiyaku 4.0.0 → 4.0.1

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 (241) hide show
  1. package/README.md +2 -9
  2. package/build/integrations/marketplace/plugins/keiyaku/.claude-plugin/plugin.json +2 -2
  3. package/build/integrations/marketplace/plugins/keiyaku/.codex-plugin/plugin.json +4 -4
  4. package/build/integrations/marketplace/plugins/keiyaku/opencode.js +2 -2
  5. package/build/integrations/marketplace/plugins/keiyaku/package.json +2 -2
  6. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku/SKILL.md +11 -3
  7. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-akuma/SKILL.md +9 -6
  8. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-bind/SKILL.md +94 -0
  9. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-bind/agents/openai.yaml +4 -0
  10. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-task/SKILL.md +41 -6
  11. package/build/integrations/marketplace/plugins/keiyaku/skills/keiyaku-workflow/SKILL.md +162 -45
  12. package/build/src/akuma/akuma.d.ts +33 -25
  13. package/build/src/akuma/akuma.js +93 -61
  14. package/build/src/akuma/archetype.d.ts +8 -0
  15. package/build/src/akuma/archetype.js +28 -9
  16. package/build/src/akuma/body.js +196 -84
  17. package/build/src/akuma/coordinate.d.ts +9 -0
  18. package/build/src/akuma/coordinate.js +22 -0
  19. package/build/src/akuma/heart/facts.d.ts +67 -15
  20. package/build/src/akuma/heart/facts.js +3 -3
  21. package/build/src/akuma/heart/index.d.ts +41 -29
  22. package/build/src/akuma/heart/index.js +72 -193
  23. package/build/src/akuma/heart/rows.d.ts +30 -53
  24. package/build/src/akuma/heart/rows.js +91 -111
  25. package/build/src/akuma/heart/schema.d.ts +1 -1
  26. package/build/src/akuma/heart/schema.js +55 -8
  27. package/build/src/akuma/heart/storage.d.ts +33 -0
  28. package/build/src/akuma/heart/storage.js +164 -0
  29. package/build/src/akuma/heart/tells.d.ts +12 -0
  30. package/build/src/akuma/heart/tells.js +79 -0
  31. package/build/src/akuma/heart/timeline.d.ts +15 -0
  32. package/build/src/akuma/heart/timeline.js +86 -0
  33. package/build/src/akuma/index.d.ts +4 -2
  34. package/build/src/akuma/index.js +1 -1
  35. package/build/src/akuma/projection.d.ts +97 -0
  36. package/build/src/akuma/projection.js +145 -0
  37. package/build/src/akuma/provider.d.ts +57 -11
  38. package/build/src/akuma/provider.js +57 -37
  39. package/build/src/akuma/providers/{claude.d.ts → claude/events.d.ts} +9 -41
  40. package/build/src/akuma/providers/claude/events.js +185 -0
  41. package/build/src/akuma/providers/claude/index.d.ts +46 -0
  42. package/build/src/akuma/providers/claude/index.js +265 -0
  43. package/build/src/akuma/providers/claude/input.d.ts +13 -0
  44. package/build/src/akuma/providers/claude/input.js +109 -0
  45. package/build/src/akuma/providers/codex-app-server/events.d.ts +1 -1
  46. package/build/src/akuma/providers/codex-app-server/events.js +12 -12
  47. package/build/src/akuma/providers/codex-app-server/index.d.ts +13 -11
  48. package/build/src/akuma/providers/codex-app-server/index.js +73 -22
  49. package/build/src/akuma/providers/index.js +22 -8
  50. package/build/src/akuma/providers/opencode-sdk/events.d.ts +48 -0
  51. package/build/src/akuma/providers/opencode-sdk/events.js +116 -0
  52. package/build/src/akuma/providers/opencode-sdk/index.d.ts +32 -0
  53. package/build/src/akuma/providers/opencode-sdk/index.js +203 -0
  54. package/build/src/akuma/providers/opencode-sdk/session.d.ts +41 -0
  55. package/build/src/akuma/providers/opencode-sdk/session.js +62 -0
  56. package/build/src/akuma/providers/pi/events.d.ts +33 -0
  57. package/build/src/akuma/providers/pi/events.js +128 -0
  58. package/build/src/akuma/providers/pi/index.d.ts +11 -0
  59. package/build/src/akuma/providers/pi/index.js +162 -0
  60. package/build/src/akuma/requests.d.ts +1 -0
  61. package/build/src/akuma/requests.js +5 -2
  62. package/build/src/alias/index.d.ts +4 -3
  63. package/build/src/alias/index.js +2 -3
  64. package/build/src/body/render.js +2 -1
  65. package/build/src/body/shape.js +7 -1
  66. package/build/src/body/verification.js +19 -4
  67. package/build/src/cli/accepted.d.ts +2 -0
  68. package/build/src/cli/accepted.js +2 -1
  69. package/build/src/cli/commands/akuma-invoke.d.ts +42 -18
  70. package/build/src/cli/commands/akuma-invoke.js +94 -50
  71. package/build/src/cli/commands/akuma.d.ts +15 -7
  72. package/build/src/cli/commands/akuma.js +94 -56
  73. package/build/src/cli/commands/contract.d.ts +142 -0
  74. package/build/src/cli/commands/contract.js +14 -0
  75. package/build/src/cli/commands/install.js +2 -0
  76. package/build/src/cli/commands/task-invoke.d.ts +3 -1
  77. package/build/src/cli/commands/task-invoke.js +27 -2
  78. package/build/src/cli/commands/task.js +2 -2
  79. package/build/src/cli/invoke.d.ts +6 -1
  80. package/build/src/cli/invoke.js +241 -43
  81. package/build/src/cli/main.js +20 -1
  82. package/build/src/cli/parse.d.ts +13 -116
  83. package/build/src/cli/parse.js +51 -78
  84. package/build/src/cli/render/akuma-tool.d.ts +2 -0
  85. package/build/src/cli/render/akuma-tool.js +6 -2
  86. package/build/src/cli/render/akuma.d.ts +2 -1
  87. package/build/src/cli/render/akuma.js +157 -217
  88. package/build/src/cli/render/catalog.d.ts +2 -0
  89. package/build/src/cli/render/catalog.js +16 -0
  90. package/build/src/cli/render/contract.d.ts +1 -1
  91. package/build/src/cli/render/contract.js +38 -5
  92. package/build/src/cli/render/kanshi.js +53 -5
  93. package/build/src/cli/render/refusal.js +17 -0
  94. package/build/src/cli/render/terminal.d.ts +13 -0
  95. package/build/src/cli/render/terminal.js +75 -0
  96. package/build/src/cli/render/text.js +5 -0
  97. package/build/src/cli/result.d.ts +15 -3
  98. package/build/src/contract-worktree.d.ts +43 -0
  99. package/build/src/contract-worktree.js +213 -0
  100. package/build/src/coordination/durable-file.d.ts +3 -0
  101. package/build/src/coordination/durable-file.js +44 -1
  102. package/build/src/core/facts/codec.js +23 -4
  103. package/build/src/core/facts/eligibility.d.ts +1 -10
  104. package/build/src/core/facts/eligibility.js +0 -49
  105. package/build/src/core/facts/observation.d.ts +3 -0
  106. package/build/src/core/facts/observation.js +12 -0
  107. package/build/src/core/facts/types.d.ts +10 -3
  108. package/build/src/core/subject.js +2 -2
  109. package/build/src/core/verbs/amend.js +12 -16
  110. package/build/src/core/verbs/bind.js +2 -16
  111. package/build/src/core/verbs/deliver.js +20 -4
  112. package/build/src/core/verbs/placement.js +3 -8
  113. package/build/src/dispatch/index.d.ts +3 -1
  114. package/build/src/dispatch/index.js +14 -5
  115. package/build/src/duration.d.ts +10 -0
  116. package/build/src/duration.js +22 -0
  117. package/build/src/git/admission.d.ts +2 -2
  118. package/build/src/git/admission.js +26 -16
  119. package/build/src/git/hooks.d.ts +15 -0
  120. package/build/src/git/hooks.js +57 -5
  121. package/build/src/git/identity.d.ts +5 -1
  122. package/build/src/git/identity.js +23 -4
  123. package/build/src/git/integration.d.ts +49 -0
  124. package/build/src/git/integration.js +214 -0
  125. package/build/src/git/observe.d.ts +25 -13
  126. package/build/src/git/observe.js +175 -72
  127. package/build/src/git/read-observation.d.ts +41 -0
  128. package/build/src/git/read-observation.js +361 -0
  129. package/build/src/git/reconcile.d.ts +15 -5
  130. package/build/src/git/reconcile.js +238 -85
  131. package/build/src/git/repository.d.ts +19 -4
  132. package/build/src/git/repository.js +77 -43
  133. package/build/src/git/scratch.d.ts +13 -0
  134. package/build/src/git/scratch.js +66 -0
  135. package/build/src/git/target-placement.d.ts +56 -0
  136. package/build/src/git/target-placement.js +255 -0
  137. package/build/src/git/tender.d.ts +48 -0
  138. package/build/src/git/tender.js +82 -0
  139. package/build/src/git/terminal-seal.d.ts +24 -0
  140. package/build/src/git/terminal-seal.js +87 -0
  141. package/build/src/git/tree.d.ts +2 -2
  142. package/build/src/git/tree.js +4 -0
  143. package/build/src/git/workspace.d.ts +21 -0
  144. package/build/src/git/workspace.js +104 -0
  145. package/build/src/identity/selector.d.ts +6 -0
  146. package/build/src/identity/selector.js +19 -0
  147. package/build/src/index.d.ts +4 -2
  148. package/build/src/index.js +2 -1
  149. package/build/src/kanshi/index.d.ts +1 -1
  150. package/build/src/kanshi/read.d.ts +5 -2
  151. package/build/src/kanshi/read.js +154 -45
  152. package/build/src/kanshi/report.d.ts +31 -6
  153. package/build/src/kanshi/select.js +1 -1
  154. package/build/src/library/address.d.ts +48 -0
  155. package/build/src/library/address.js +134 -0
  156. package/build/src/library/akuma-creation.d.ts +10 -6
  157. package/build/src/library/akuma-creation.js +12 -11
  158. package/build/src/library/bind.d.ts +21 -0
  159. package/build/src/library/bind.js +56 -0
  160. package/build/src/library/catalog.d.ts +58 -0
  161. package/build/src/library/catalog.js +72 -0
  162. package/build/src/library/configuration.d.ts +6 -15
  163. package/build/src/library/configuration.js +14 -17
  164. package/build/src/library/contract.d.ts +21 -46
  165. package/build/src/library/contract.js +193 -183
  166. package/build/src/library/delivery.d.ts +23 -0
  167. package/build/src/library/delivery.js +21 -0
  168. package/build/src/library/fleet.d.ts +65 -0
  169. package/build/src/library/fleet.js +189 -0
  170. package/build/src/library/input.d.ts +3 -0
  171. package/build/src/library/input.js +19 -0
  172. package/build/src/library/keiyaku.d.ts +16 -2
  173. package/build/src/library/keiyaku.js +11 -1
  174. package/build/src/library/mutation.d.ts +38 -0
  175. package/build/src/library/mutation.js +39 -0
  176. package/build/src/library/refusal.d.ts +21 -0
  177. package/build/src/library/refusal.js +29 -0
  178. package/build/src/library/region.d.ts +2 -1
  179. package/build/src/library/region.js +3 -3
  180. package/build/src/library/repo.d.ts +4 -1
  181. package/build/src/library/repo.js +31 -16
  182. package/build/src/protocol/attempt.d.ts +16 -3
  183. package/build/src/protocol/attempt.js +39 -8
  184. package/build/src/protocol/bind.d.ts +9 -2
  185. package/build/src/protocol/bind.js +47 -39
  186. package/build/src/protocol/intent.d.ts +42 -18
  187. package/build/src/protocol/intent.js +36 -75
  188. package/build/src/protocol/operations.d.ts +93 -35
  189. package/build/src/protocol/operations.js +235 -123
  190. package/build/src/protocol/outcome.d.ts +7 -2
  191. package/build/src/protocol/outcome.js +9 -3
  192. package/build/src/protocol/placement.d.ts +30 -0
  193. package/build/src/protocol/placement.js +114 -0
  194. package/build/src/protocol/read/audit.d.ts +9 -3
  195. package/build/src/protocol/read/audit.js +25 -9
  196. package/build/src/protocol/read/documents.d.ts +2 -2
  197. package/build/src/protocol/read/documents.js +4 -4
  198. package/build/src/protocol/read/status.d.ts +11 -5
  199. package/build/src/protocol/read/status.js +28 -15
  200. package/build/src/protocol/run.d.ts +49 -7
  201. package/build/src/protocol/run.js +53 -25
  202. package/build/src/runtime/proc/line-rpc.d.ts +1 -0
  203. package/build/src/runtime/proc/line-rpc.js +21 -0
  204. package/build/src/runtime/proc/run.d.ts +18 -2
  205. package/build/src/runtime/proc/run.js +34 -8
  206. package/build/src/settings.d.ts +6 -0
  207. package/build/src/settings.js +29 -14
  208. package/build/src/settlement/fence.d.ts +9 -0
  209. package/build/src/settlement/fence.js +23 -0
  210. package/build/src/settlement/holder.d.ts +25 -4
  211. package/build/src/settlement/holder.js +102 -28
  212. package/build/src/settlement/settle.d.ts +8 -0
  213. package/build/src/settlement/settle.js +102 -28
  214. package/build/src/task/board.d.ts +20 -2
  215. package/build/src/task/board.js +11 -1
  216. package/build/src/task/compose.d.ts +2 -2
  217. package/build/src/task/compose.js +2 -2
  218. package/build/src/task/context.js +2 -7
  219. package/build/src/task/index.d.ts +9 -9
  220. package/build/src/task/index.js +15 -12
  221. package/build/src/task/operations.d.ts +31 -12
  222. package/build/src/task/operations.js +8 -4
  223. package/build/src/task/store.d.ts +4 -6
  224. package/build/src/task/store.js +3 -3
  225. package/build/src/verification/declaration.d.ts +1 -0
  226. package/build/src/verification/execution.d.ts +49 -0
  227. package/build/src/verification/execution.js +104 -0
  228. package/build/src/world.d.ts +13 -0
  229. package/build/src/world.js +103 -0
  230. package/package.json +7 -4
  231. package/build/src/akuma/activity.d.ts +0 -66
  232. package/build/src/akuma/activity.js +0 -139
  233. package/build/src/akuma/providers/claude.js +0 -305
  234. package/build/src/context-root.d.ts +0 -4
  235. package/build/src/context-root.js +0 -15
  236. package/build/src/git/delivery.d.ts +0 -20
  237. package/build/src/git/delivery.js +0 -109
  238. package/build/src/git/verification.d.ts +0 -14
  239. package/build/src/git/verification.js +0 -56
  240. package/build/src/verification/producer.d.ts +0 -23
  241. package/build/src/verification/producer.js +0 -54
@@ -0,0 +1,79 @@
1
+ export const tellStateSql = `CASE
2
+ WHEN EXISTS (
3
+ SELECT 1 FROM tell_deliveries d
4
+ WHERE d.tell_id = tells.id AND (d.route = 'launch' OR d.receipt = 'unavailable')
5
+ ) THEN 'told'
6
+ WHEN EXISTS (SELECT 1 FROM tell_receipts r WHERE r.evidence = 'exact' AND r.tell_id = tells.id) THEN 'told'
7
+ WHEN EXISTS (
8
+ SELECT 1 FROM tell_receipts r
9
+ JOIN tell_deliveries d ON d.turn_sequence = r.turn_sequence AND d.fence = r.fence
10
+ WHERE r.evidence = 'fence' AND d.tell_id = tells.id
11
+ ) THEN 'told'
12
+ ELSE 'pending'
13
+ END`;
14
+ export const pendingTellSequencesSql = `SELECT tells.sequence FROM tells WHERE ${tellStateSql} = 'pending'`;
15
+ export const pendingTellProtectionSql = `SELECT d.turn_sequence FROM tell_deliveries d
16
+ JOIN tells ON tells.id = d.tell_id WHERE ${tellStateSql} = 'pending'`;
17
+ function tellDeliveries(database, id) {
18
+ const rows = database.prepare(`SELECT turn_sequence, route, receipt, delivered_at FROM tell_deliveries
19
+ WHERE tell_id = ? ORDER BY sequence`).all(id);
20
+ return rows.map((row) => row.receipt == null
21
+ ? { turnSequence: row.turn_sequence, route: row.route, deliveredAt: row.delivered_at }
22
+ : { turnSequence: row.turn_sequence, route: row.route, receipt: row.receipt, deliveredAt: row.delivered_at });
23
+ }
24
+ function decodeTellRow(database, row) {
25
+ return {
26
+ kind: "tell",
27
+ sequence: row.sequence,
28
+ id: row.id,
29
+ body: row.body,
30
+ state: row.state,
31
+ recordedAt: row.recorded_at,
32
+ deliveries: tellDeliveries(database, row.id),
33
+ };
34
+ }
35
+ export function decodeTellAtSequence(database, sequence) {
36
+ const row = database.prepare(`SELECT sequence, id, body, recorded_at, ${tellStateSql} AS state
37
+ FROM tells WHERE sequence = ?`).get(sequence);
38
+ if (row === undefined)
39
+ throw new Error(`Akuma timeline references missing tell ${sequence}`);
40
+ return decodeTellRow(database, row);
41
+ }
42
+ export function insertTellFact(database, tell) {
43
+ const sequence = Number(database.prepare("INSERT INTO timeline(kind) VALUES ('tell')").run().lastInsertRowid);
44
+ database.prepare("INSERT INTO tells(id, sequence, body, recorded_at) VALUES (?, ?, ?, ?)")
45
+ .run(tell.id, sequence, tell.body, tell.recordedAt);
46
+ return sequence;
47
+ }
48
+ export function tellFact(database, id) {
49
+ const row = database.prepare(`SELECT sequence, id, body, recorded_at, ${tellStateSql} AS state
50
+ FROM tells WHERE id = ?`).get(id);
51
+ return row === undefined ? null : decodeTellRow(database, row);
52
+ }
53
+ export function insertTellDeliveryFact(database, input) {
54
+ const result = database.prepare(`INSERT OR IGNORE INTO tell_deliveries(
55
+ tell_id, route, turn_sequence, fence, receipt, delivered_at
56
+ ) VALUES (?, ?, ?, ?, ?, ?)`).run(input.tellId, input.route, input.turnSequence, input.fence, input.route === "live" ? input.receipt : null, input.deliveredAt);
57
+ const row = database.prepare(`SELECT route, receipt FROM tell_deliveries
58
+ WHERE tell_id = ? AND turn_sequence = ? AND fence = ?`)
59
+ .get(input.tellId, input.turnSequence, input.fence);
60
+ if (row === undefined || (result.changes === 0
61
+ && (row.route !== input.route || row.receipt !== (input.route === "live" ? input.receipt : null)))) {
62
+ throw new Error(`tell delivery ${input.tellId} has conflicting evidence`);
63
+ }
64
+ }
65
+ export function insertTellReceiptFact(database, input) {
66
+ database.prepare(`INSERT OR IGNORE INTO tell_receipts(
67
+ evidence, tell_id, turn_sequence, fence, kind, received_at
68
+ ) VALUES (?, ?, ?, ?, ?, ?)`).run(input.evidence, input.evidence === "exact" ? input.tellId : null, input.evidence === "fence" ? input.turnSequence : null, input.evidence === "fence" ? input.fence : null, input.kind, input.receivedAt);
69
+ }
70
+ export function tellIdsForFence(database, turnSequence, fence) {
71
+ return database.prepare(`SELECT tell_id FROM tell_deliveries
72
+ WHERE turn_sequence = ? AND fence = ? ORDER BY sequence`)
73
+ .all(turnSequence, fence).map((row) => row.tell_id);
74
+ }
75
+ export function pendingTellFacts(database) {
76
+ const rows = database.prepare(`SELECT sequence, id, body, recorded_at, ${tellStateSql} AS state FROM tells
77
+ WHERE ${tellStateSql} = 'pending' ORDER BY sequence`).all();
78
+ return rows.map((row) => decodeTellRow(database, row));
79
+ }
@@ -0,0 +1,15 @@
1
+ import type { DatabaseSync } from "node:sqlite";
2
+ import type { CallFact, TellFact, TurnEndFact, TurnStartFact } from "./facts.js";
3
+ import { type ActivityFact } from "./rows.js";
4
+ export type TimelineFact = TurnStartFact | CallFact | ActivityFact | TellFact | TurnEndFact;
5
+ export type ActivityFactSlice = Readonly<{
6
+ rows: readonly TimelineFact[];
7
+ lowestRetained: number | null;
8
+ highest: number | null;
9
+ }>;
10
+ export declare function pruneActivityFacts(database: DatabaseSync, limit: number): void;
11
+ export declare function activityFactSlice(database: DatabaseSync, input: Readonly<{
12
+ before?: number;
13
+ since?: number;
14
+ limit: number;
15
+ }>): ActivityFactSlice;
@@ -0,0 +1,86 @@
1
+ import { decodeActivityRow, decodeCallRow, decodeTurnRow, } from "./rows.js";
2
+ import { decodeTellAtSequence, pendingTellProtectionSql, pendingTellSequencesSql } from "./tells.js";
3
+ export function pruneActivityFacts(database, limit) {
4
+ const count = database.prepare("SELECT COUNT(*) AS count FROM timeline").get();
5
+ if (count.count <= limit + 500)
6
+ return;
7
+ const cutoff = database.prepare(`SELECT sequence FROM timeline ORDER BY sequence DESC LIMIT 1 OFFSET ?`)
8
+ .get(limit - 1);
9
+ if (cutoff === undefined)
10
+ return;
11
+ database.prepare(`WITH protected_turns(sequence) AS (
12
+ SELECT sequence FROM turns WHERE end_sequence IS NULL
13
+ UNION SELECT turn_sequence FROM calls WHERE sequence >= ?
14
+ UNION SELECT turn_sequence FROM activity WHERE sequence >= ?
15
+ UNION SELECT sequence FROM turns WHERE end_sequence >= ?
16
+ UNION ${pendingTellProtectionSql}
17
+ ), protected(sequence) AS (
18
+ SELECT sequence FROM protected_turns
19
+ UNION SELECT sequence FROM calls WHERE turn_sequence IN (SELECT sequence FROM protected_turns)
20
+ UNION SELECT end_sequence FROM turns WHERE sequence IN (SELECT sequence FROM protected_turns) AND end_sequence IS NOT NULL
21
+ UNION ${pendingTellSequencesSql}
22
+ )
23
+ DELETE FROM timeline WHERE sequence < ? AND sequence NOT IN protected`)
24
+ .run(cutoff.sequence, cutoff.sequence, cutoff.sequence, cutoff.sequence);
25
+ }
26
+ function turn(database, sequence) {
27
+ const row = database.prepare(`SELECT sequence, body_sequence, started_at, end_sequence, outcome,
28
+ history_id, session_json, answer, diagnostic, completed_at FROM turns WHERE sequence = ?`)
29
+ .get(sequence);
30
+ if (row === undefined)
31
+ throw new Error(`Akuma timeline references missing Turn ${sequence}`);
32
+ return decodeTurnRow(row);
33
+ }
34
+ function turnStart(database, sequence) {
35
+ const row = database.prepare("SELECT sequence, body_sequence, started_at FROM turns WHERE sequence = ?")
36
+ .get(sequence);
37
+ if (row === undefined)
38
+ throw new Error(`Akuma timeline references missing Turn ${sequence}`);
39
+ return { kind: "turn-start", sequence: row.sequence, bodySequence: row.body_sequence, startedAt: row.started_at };
40
+ }
41
+ function decodeTimelineRow(database, row) {
42
+ if (row.kind === "turn-start")
43
+ return turnStart(database, row.sequence);
44
+ if (row.kind === "turn-end") {
45
+ const source = database.prepare("SELECT sequence FROM turns WHERE end_sequence = ?").get(row.sequence);
46
+ const fact = source === undefined ? undefined : turn(database, source.sequence).end;
47
+ if (fact === undefined)
48
+ throw new Error(`Akuma timeline references missing Turn end ${row.sequence}`);
49
+ return fact;
50
+ }
51
+ if (row.kind === "call") {
52
+ const value = database.prepare("SELECT sequence, turn_sequence, body, at FROM calls WHERE sequence = ?")
53
+ .get(row.sequence);
54
+ if (value === undefined)
55
+ throw new Error(`Akuma timeline references missing call ${row.sequence}`);
56
+ return decodeCallRow(value);
57
+ }
58
+ if (row.kind === "activity") {
59
+ const value = database.prepare("SELECT sequence, turn_sequence, event_json, at FROM activity WHERE sequence = ?")
60
+ .get(row.sequence);
61
+ if (value === undefined)
62
+ throw new Error(`Akuma timeline references missing activity ${row.sequence}`);
63
+ return decodeActivityRow(value);
64
+ }
65
+ return decodeTellAtSequence(database, row.sequence);
66
+ }
67
+ export function activityFactSlice(database, input) {
68
+ const bounds = database.prepare("SELECT MIN(sequence) AS lowest, MAX(sequence) AS highest FROM timeline")
69
+ .get();
70
+ let rows;
71
+ if (input.before !== undefined) {
72
+ rows = database.prepare(`SELECT * FROM (
73
+ SELECT sequence, kind FROM timeline WHERE sequence < ? ORDER BY sequence DESC LIMIT ?
74
+ ) ORDER BY sequence`).all(input.before, input.limit);
75
+ }
76
+ else if (input.since !== undefined) {
77
+ rows = database.prepare(`SELECT sequence, kind FROM timeline WHERE sequence > ? ORDER BY sequence LIMIT ?`)
78
+ .all(input.since, input.limit);
79
+ }
80
+ else {
81
+ rows = database.prepare(`SELECT * FROM (
82
+ SELECT sequence, kind FROM timeline ORDER BY sequence DESC LIMIT ?
83
+ ) ORDER BY sequence`).all(input.limit);
84
+ }
85
+ return { rows: rows.map((row) => decodeTimelineRow(database, row)), lowestRetained: bounds.lowest, highest: bounds.highest };
86
+ }
@@ -1,7 +1,9 @@
1
1
  export { Akuma, AkumaHandle, AkumaNotBornError } from "./akuma.js";
2
- export { AkumaArchetypeError } from "./archetype.js";
2
+ export type { WorldRoot } from "../world.js";
3
+ export { AkumaArchetypeError, listArchetypeDefinitions, listArchetypes } from "./archetype.js";
4
+ export type { ArchetypeCatalogRow } from "./archetype.js";
3
5
  export { AkumaBodyRequestError } from "./requests.js";
4
- export type { AkumaList, AkumaListRow, AkumaStatus, ActivityHistory, ActivityRow, ActivitySnapshot, ForkReceipt, InterruptReceipt, TellReceipt, UnbornAkumaListRow, } from "./akuma.js";
6
+ export type { AkumaList, AkumaListInput, AkumaListRow, AkumaStatus, ActivityHistory, ActivityRow, ActivitySnapshot, ActivitySnapshotEntry, ForkReceipt, InterruptReceipt, TellResult, UnbornAkumaListRow, } from "./akuma.js";
5
7
  export type { AgentEvent, ToolCall, ToolEvent, ToolResult } from "./provider.js";
6
8
  export type { AkuId } from "./identity.js";
7
9
  export type { Confinement, KillEvidence, ProviderOptions, ResumeCoordinate, TurnFact, } from "./heart/index.js";
@@ -1,3 +1,3 @@
1
1
  export { Akuma, AkumaHandle, AkumaNotBornError } from "./akuma.js";
2
- export { AkumaArchetypeError } from "./archetype.js";
2
+ export { AkumaArchetypeError, listArchetypeDefinitions, listArchetypes } from "./archetype.js";
3
3
  export { AkumaBodyRequestError } from "./requests.js";
@@ -0,0 +1,97 @@
1
+ import { type ToolCall, type ToolResult } from "./provider.js";
2
+ import type { ActivitySlice, TellDelivery, TellFact, TimelineFact, TurnEndFact } from "./heart/index.js";
3
+ export type ActivityRow = Readonly<{
4
+ kind: "turn";
5
+ sequence: number;
6
+ turnSequence: number;
7
+ bodySequence: number;
8
+ at: string;
9
+ }> | Readonly<{
10
+ kind: "call";
11
+ sequence: number;
12
+ turnSequence: number;
13
+ at: string;
14
+ text: string;
15
+ }> | Readonly<{
16
+ kind: "said";
17
+ sequence: number;
18
+ turnSequence: number;
19
+ at: string;
20
+ text: string;
21
+ truncated?: true;
22
+ }> | Readonly<{
23
+ kind: "thought";
24
+ sequence: number;
25
+ turnSequence: number;
26
+ at: string;
27
+ text: string;
28
+ truncated?: true;
29
+ }> | Readonly<{
30
+ kind: "tool";
31
+ sequence: number;
32
+ turnSequence: number;
33
+ at: string;
34
+ completedAt?: string;
35
+ durationMs?: number;
36
+ name: string;
37
+ call: ToolCall;
38
+ state: "running" | ToolResult;
39
+ truncated?: true;
40
+ }> | Readonly<{
41
+ kind: "note";
42
+ sequence: number;
43
+ turnSequence: number;
44
+ at: string;
45
+ text: string;
46
+ truncated?: true;
47
+ }> | Readonly<{
48
+ kind: "tell";
49
+ sequence: number;
50
+ at: string;
51
+ tellId: string;
52
+ text: string;
53
+ state: TellFact["state"];
54
+ deliveries: readonly TellDelivery[];
55
+ }> | Readonly<{
56
+ kind: "outcome";
57
+ sequence: number;
58
+ turnSequence: number;
59
+ at: string;
60
+ outcome: TurnEndFact["outcome"];
61
+ }>;
62
+ export type ActivitySnapshot = Readonly<{
63
+ entries: readonly ActivitySnapshotEntry[];
64
+ lowestRetained: number | null;
65
+ highest: number | null;
66
+ }>;
67
+ export type ActivitySnapshotEntry = Readonly<{
68
+ kind: "row";
69
+ row: ActivityRow;
70
+ }> | Readonly<{
71
+ kind: "gap";
72
+ count: number;
73
+ }>;
74
+ export type ActivitySnapshotProfile = "status" | "feedback";
75
+ export type ActivityHistory = Readonly<{
76
+ rows: readonly ActivityRow[];
77
+ omitted: number;
78
+ hasEarlier: boolean;
79
+ hasLater: boolean;
80
+ historyLost: boolean;
81
+ lowestRetained: number | null;
82
+ highest: number | null;
83
+ }>;
84
+ /** Project the complete retained fact window in persisted timeline order. */
85
+ export declare function projectTimeline(facts: readonly TimelineFact[]): readonly ActivityRow[];
86
+ /** Apply the bounded snapshot policy after semantic projection. */
87
+ export declare function selectActivitySnapshot(facts: readonly TimelineFact[], input?: Readonly<{
88
+ lowestRetained?: number | null;
89
+ highest?: number | null;
90
+ profile?: ActivitySnapshotProfile;
91
+ }>): ActivitySnapshot;
92
+ /** Apply cursor and limit to the one semantic timeline projection. */
93
+ export declare function projectActivityHistory(slice: ActivitySlice, input: Readonly<{
94
+ before?: number;
95
+ since?: number;
96
+ limit: number;
97
+ }>): ActivityHistory;
@@ -0,0 +1,145 @@
1
+ import { decodeAgentEvent } from "./provider.js";
2
+ const TAIL_LIMIT = 3;
3
+ const VOICE_LIMIT = 5;
4
+ function duration(startedAt, completedAt) {
5
+ const start = Date.parse(startedAt);
6
+ const end = Date.parse(completedAt);
7
+ if (!Number.isFinite(start) || !Number.isFinite(end) || end < start)
8
+ return undefined;
9
+ return end - start;
10
+ }
11
+ function toolKey(turnSequence, id) {
12
+ return `${turnSequence}:${id}`;
13
+ }
14
+ function narrationRow(fact, event) {
15
+ if (event.type === "assistant") {
16
+ return { kind: "said", sequence: fact.sequence, turnSequence: fact.turnSequence, at: fact.at, text: event.text,
17
+ ...(event.truncated === true ? { truncated: true } : {}) };
18
+ }
19
+ if (event.type === "thought" || event.type === "note") {
20
+ return { kind: event.type, sequence: fact.sequence, turnSequence: fact.turnSequence, at: fact.at, text: event.text,
21
+ ...(event.truncated === true ? { truncated: true } : {}) };
22
+ }
23
+ return null;
24
+ }
25
+ /** Project the complete retained fact window in persisted timeline order. */
26
+ export function projectTimeline(facts) {
27
+ const state = { rows: [], running: new Map() };
28
+ for (const fact of facts) {
29
+ if (fact.kind === "turn-start")
30
+ state.rows.push({ settled: false, row: { kind: "turn", sequence: fact.sequence, turnSequence: fact.sequence, bodySequence: fact.bodySequence, at: fact.startedAt } });
31
+ else if (fact.kind === "turn-end")
32
+ state.rows.push({ settled: true, row: { kind: "outcome", sequence: fact.sequence, turnSequence: fact.turnSequence, at: fact.completedAt, outcome: fact.outcome } });
33
+ else if (fact.kind === "call")
34
+ state.rows.push({ settled: true, row: { kind: "call", sequence: fact.sequence, turnSequence: fact.turnSequence, at: fact.at, text: fact.body } });
35
+ else if (fact.kind === "tell")
36
+ state.rows.push({ settled: fact.state !== "pending", row: { kind: "tell", sequence: fact.sequence, at: fact.recordedAt, tellId: fact.id, text: fact.body, state: fact.state, deliveries: fact.deliveries } });
37
+ else
38
+ projectActivityEvent(state, fact);
39
+ }
40
+ return state.rows.map((entry) => entry.row);
41
+ }
42
+ function projectActivityEvent(state, fact) {
43
+ const event = decodeAgentEvent(fact.event);
44
+ const narration = narrationRow(fact, event);
45
+ if (narration !== null) {
46
+ state.rows.push({ settled: true, row: narration });
47
+ return;
48
+ }
49
+ if (event.type !== "tool")
50
+ return;
51
+ const key = toolKey(fact.turnSequence, event.id);
52
+ if (event.phase === "started") {
53
+ state.running.set(key, state.rows.length);
54
+ state.rows.push({ settled: false, row: { kind: "tool", sequence: fact.sequence, turnSequence: fact.turnSequence, at: fact.at, name: event.name, call: event.call, state: "running", ...(event.truncated === true ? { truncated: true } : {}) } });
55
+ return;
56
+ }
57
+ const index = state.running.get(key);
58
+ const started = index === undefined ? undefined : state.rows[index]?.row;
59
+ if (started?.kind !== "tool") {
60
+ state.rows.push({ settled: true, row: {
61
+ kind: "tool", sequence: fact.sequence, turnSequence: fact.turnSequence, at: fact.at,
62
+ name: event.name, call: event.call, state: event.result,
63
+ ...(event.truncated === true ? { truncated: true } : {}),
64
+ } });
65
+ return;
66
+ }
67
+ const startedAt = started.at;
68
+ const durationMs = duration(startedAt, fact.at);
69
+ const completed = {
70
+ kind: "tool", sequence: started.sequence, turnSequence: fact.turnSequence, at: startedAt,
71
+ completedAt: fact.at, ...(durationMs === undefined ? {} : { durationMs }),
72
+ name: event.name, call: event.call, state: event.result,
73
+ ...(started?.kind === "tool" && started.truncated === true || event.truncated === true ? { truncated: true } : {}),
74
+ };
75
+ state.rows[index] = { settled: true, row: completed };
76
+ state.running.delete(key);
77
+ }
78
+ function pinned(row, latestOutcome, openTurn) {
79
+ return row === latestOutcome
80
+ || (row.kind === "turn" && row.turnSequence === openTurn)
81
+ || (row.kind === "call" && row.turnSequence === openTurn)
82
+ || (row.kind === "tool" && row.state === "running")
83
+ || (row.kind === "tell" && row.state === "pending");
84
+ }
85
+ /** Apply the bounded snapshot policy after semantic projection. */
86
+ export function selectActivitySnapshot(facts, input = {}) {
87
+ const projected = projectTimeline(facts);
88
+ const ended = new Set(projected.filter((row) => row.kind === "outcome").map((row) => row.turnSequence));
89
+ const openTurn = projected.findLast((row) => row.kind === "turn" && !ended.has(row.turnSequence));
90
+ const latestOutcome = projected.findLast((row) => row.kind === "outcome");
91
+ const ordinary = projected.filter((row) => !pinned(row, latestOutcome, openTurn?.turnSequence));
92
+ const selected = new Set(ordinary.slice(-TAIL_LIMIT));
93
+ if ((input.profile ?? "status") === "status") {
94
+ for (const row of ordinary.filter((value) => value.kind === "said" || value.kind === "thought").slice(-VOICE_LIMIT)) {
95
+ selected.add(row);
96
+ }
97
+ }
98
+ for (const row of projected)
99
+ if (pinned(row, latestOutcome, openTurn?.turnSequence))
100
+ selected.add(row);
101
+ const entries = [];
102
+ let hidden = [];
103
+ const flush = () => {
104
+ if (hidden.length === 1)
105
+ entries.push({ kind: "row", row: hidden[0] });
106
+ else if (hidden.length > 1)
107
+ entries.push({ kind: "gap", count: hidden.length });
108
+ hidden = [];
109
+ };
110
+ for (const row of projected) {
111
+ if (!selected.has(row))
112
+ hidden.push(row);
113
+ else {
114
+ flush();
115
+ entries.push({ kind: "row", row });
116
+ }
117
+ }
118
+ flush();
119
+ return {
120
+ entries,
121
+ lowestRetained: input.lowestRetained ?? (facts[0]?.sequence ?? null),
122
+ highest: input.highest ?? (facts.at(-1)?.sequence ?? null),
123
+ };
124
+ }
125
+ /** Apply cursor and limit to the one semantic timeline projection. */
126
+ export function projectActivityHistory(slice, input) {
127
+ const projected = projectTimeline(slice.rows);
128
+ const eligible = projected.filter((row) => input.before !== undefined
129
+ ? row.sequence < input.before
130
+ : input.since !== undefined ? row.sequence > input.since : true);
131
+ const rows = input.since !== undefined ? eligible.slice(0, input.limit) : eligible.slice(-input.limit);
132
+ const forwardHistoryLost = input.since !== undefined
133
+ && slice.rows[0] !== undefined
134
+ && slice.rows[0].sequence > input.since + 1;
135
+ return {
136
+ rows,
137
+ omitted: Math.max(0, eligible.length - rows.length),
138
+ hasEarlier: input.since === undefined && eligible.length > rows.length,
139
+ hasLater: input.since !== undefined && eligible.length > rows.length,
140
+ historyLost: forwardHistoryLost || (input.since === undefined && eligible.length === rows.length
141
+ && slice.lowestRetained !== null && slice.lowestRetained > 1),
142
+ lowestRetained: slice.lowestRetained,
143
+ highest: slice.highest,
144
+ };
145
+ }
@@ -1,5 +1,7 @@
1
- import type { Confinement, ProviderOptions, ResumeCoordinate } from "./heart/index.js";
2
- export type { ProviderOptions, ResumeCoordinate } from "./heart/index.js";
1
+ import type { Confinement, ProviderOptions } from "./heart/index.js";
2
+ import { decodeResumeCoordinate, encodeResumeCoordinate, type ResumeCoordinate } from "./coordinate.js";
3
+ export type { ResumeCoordinate } from "./coordinate.js";
4
+ export type { ProviderOptions } from "./heart/index.js";
3
5
  export declare const AKUMA_REQUESTS_ENV = "AKUMA_REQUESTS";
4
6
  export declare const AGENT_EVENT_TEXT_LIMIT = 16384;
5
7
  export declare const AGENT_THOUGHT_TEXT_LIMIT = 4000;
@@ -36,6 +38,7 @@ export type ToolEvent = Readonly<{
36
38
  id: string;
37
39
  name: string;
38
40
  call: ToolCall;
41
+ truncated?: true;
39
42
  }> & (Readonly<{
40
43
  phase: "started";
41
44
  result?: never;
@@ -49,16 +52,21 @@ export type AgentEvent = Readonly<{
49
52
  }> | Readonly<{
50
53
  type: "assistant";
51
54
  text: string;
55
+ truncated?: true;
52
56
  }> | Readonly<{
53
57
  type: "thought";
54
58
  text: string;
59
+ truncated?: true;
55
60
  }> | ToolEvent | Readonly<{
56
61
  type: "note";
57
62
  text: string;
63
+ truncated?: true;
58
64
  }> | Readonly<{
59
65
  type: "unknown";
60
66
  kind: string;
67
+ truncated?: true;
61
68
  }>;
69
+ export { decodeResumeCoordinate, encodeResumeCoordinate };
62
70
  export declare function decodeProviderOptions(value: unknown): ProviderOptions;
63
71
  export declare function decodeAgentEvent(value: unknown): AgentEvent;
64
72
  export declare function encodeAgentEvent(event: AgentEvent): unknown;
@@ -86,10 +94,46 @@ export type TurnResult = Readonly<{
86
94
  kind: "failed";
87
95
  diagnostic: string;
88
96
  }>;
89
- export type Drive = Readonly<{
97
+ export type ProviderFence = string;
98
+ export type TellReceipt = Readonly<{
99
+ evidence: "exact";
100
+ tellId: string;
101
+ kind: string;
102
+ }> | Readonly<{
103
+ evidence: "fence";
104
+ fence: ProviderFence;
105
+ kind: string;
106
+ }>;
107
+ export type TellSubmission = Readonly<{
108
+ kind: "accepted";
109
+ fence: ProviderFence;
110
+ }> | Readonly<{
111
+ kind: "turn-ended";
112
+ }>;
113
+ export type Session = Readonly<{
114
+ admission: Readonly<{
115
+ fence: ProviderFence;
116
+ }>;
90
117
  events: AsyncIterable<AgentEvent>;
118
+ receipts?: AsyncIterable<TellReceipt>;
91
119
  completion: Promise<TurnResult>;
92
120
  abort(): Promise<void>;
121
+ tell?(tell: Readonly<{
122
+ id: string;
123
+ text: string;
124
+ }>): Promise<TellSubmission>;
125
+ }>;
126
+ export type DriveInput = Readonly<{
127
+ body: string;
128
+ launchTells: readonly Readonly<{
129
+ id: string;
130
+ text: string;
131
+ }>[];
132
+ cwd: string;
133
+ options: ProviderOptions;
134
+ requests?: Readonly<{
135
+ dir: string;
136
+ }>;
93
137
  }>;
94
138
  export type ProviderOptionAdmission = Readonly<{
95
139
  kind: "admitted";
@@ -111,13 +155,15 @@ export type ProviderAdapter = Readonly<{
111
155
  }>): Promise<Readonly<{
112
156
  session: ResumeCoordinate;
113
157
  }>>;
114
- start(input: Readonly<{
115
- prompt: string;
116
- cwd: string;
117
- options: ProviderOptions;
118
- session?: ResumeCoordinate;
119
- requests?: Readonly<{
120
- dir: string;
158
+ start(input: DriveInput & Readonly<{
159
+ session: Readonly<{
160
+ kind: "fresh";
161
+ }>;
162
+ }>): Promise<Session>;
163
+ resume?(input: DriveInput & Readonly<{
164
+ session: Readonly<{
165
+ kind: "resume";
166
+ coordinate: ResumeCoordinate;
121
167
  }>;
122
- }>): Promise<Drive>;
168
+ }>): Promise<Session>;
123
169
  }>;