@tangle-network/braid 0.3.6 → 0.3.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 (306) hide show
  1. package/README.md +2 -0
  2. package/THIRD_PARTY_LICENSES.json +512 -777
  3. package/THIRD_PARTY_NOTICES.md +8 -6
  4. package/dist/adapters/analysis/managed-analysis-runtime.js +1 -1
  5. package/dist/adapters/analysis/runtime-retained-model-call.d.ts.map +1 -1
  6. package/dist/adapters/analysis/runtime-retained-model-call.js +4 -0
  7. package/dist/adapters/analysis/runtime-retained-model-call.js.map +1 -1
  8. package/dist/adapters/connections/production-connection-health.js +2 -2
  9. package/dist/adapters/connections/production-connection-health.js.map +1 -1
  10. package/dist/adapters/connections/production-connection-providers.d.ts +1 -1
  11. package/dist/adapters/connections/production-connection-providers.d.ts.map +1 -1
  12. package/dist/adapters/connections/production-connection-providers.js +2 -2
  13. package/dist/adapters/connections/production-connection-providers.js.map +1 -1
  14. package/dist/adapters/connections/production-connections.d.ts +1 -1
  15. package/dist/adapters/connections/production-connections.d.ts.map +1 -1
  16. package/dist/adapters/connections/production-connections.js +1 -1
  17. package/dist/adapters/connections/production-connections.js.map +1 -1
  18. package/dist/adapters/connections/stored-model-grant.d.ts +6 -0
  19. package/dist/adapters/connections/stored-model-grant.d.ts.map +1 -0
  20. package/dist/adapters/connections/stored-model-grant.js +63 -0
  21. package/dist/adapters/connections/stored-model-grant.js.map +1 -0
  22. package/dist/adapters/connections/tangle-model-credentials.d.ts.map +1 -1
  23. package/dist/adapters/connections/tangle-model-credentials.js +8 -0
  24. package/dist/adapters/connections/tangle-model-credentials.js.map +1 -1
  25. package/dist/adapters/connections/tangle-router-client.d.ts +1 -1
  26. package/dist/adapters/connections/tangle-router-client.js +1 -1
  27. package/dist/adapters/runtime/production-cli-bridge-backend.d.ts.map +1 -1
  28. package/dist/adapters/runtime/production-cli-bridge-backend.js +2 -0
  29. package/dist/adapters/runtime/production-cli-bridge-backend.js.map +1 -1
  30. package/dist/adapters/runtime/production-tangle-sandbox-backend.js +4 -4
  31. package/dist/adapters/runtime/production-tangle-sandbox-backend.js.map +1 -1
  32. package/dist/adapters/runtime/sandbox-instance-observation.js.map +1 -1
  33. package/dist/adapters/runtime/sandbox-observation-types.d.ts +1 -2
  34. package/dist/adapters/runtime/sandbox-observation-types.d.ts.map +1 -1
  35. package/dist/adapters/runtime/sandbox-observation-types.js.map +1 -1
  36. package/dist/adapters/tui/application-ui-controller.d.ts.map +1 -1
  37. package/dist/adapters/tui/application-ui-controller.js +15 -0
  38. package/dist/adapters/tui/application-ui-controller.js.map +1 -1
  39. package/dist/adapters/tui/learning-dispatch.d.ts +21 -0
  40. package/dist/adapters/tui/learning-dispatch.d.ts.map +1 -0
  41. package/dist/adapters/tui/learning-dispatch.js +474 -0
  42. package/dist/adapters/tui/learning-dispatch.js.map +1 -0
  43. package/dist/adapters/tui/native-terminal-transport.js +14 -2
  44. package/dist/adapters/tui/native-terminal-transport.js.map +1 -1
  45. package/dist/adapters/tui/profile-connection-dispatch.d.ts +4 -0
  46. package/dist/adapters/tui/profile-connection-dispatch.d.ts.map +1 -1
  47. package/dist/adapters/tui/profile-connection-dispatch.js +1 -0
  48. package/dist/adapters/tui/profile-connection-dispatch.js.map +1 -1
  49. package/dist/adapters/tui/ui-command-dispatch.d.ts.map +1 -1
  50. package/dist/adapters/tui/ui-command-dispatch.js +8 -0
  51. package/dist/adapters/tui/ui-command-dispatch.js.map +1 -1
  52. package/dist/adapters/tui/ui-fixtures.d.ts +1 -1
  53. package/dist/adapters/tui/ui-fixtures.d.ts.map +1 -1
  54. package/dist/adapters/tui/ui-fixtures.js.map +1 -1
  55. package/dist/adapters/tui/ui-headless-dispatch.d.ts.map +1 -1
  56. package/dist/adapters/tui/ui-headless-dispatch.js +8 -0
  57. package/dist/adapters/tui/ui-headless-dispatch.js.map +1 -1
  58. package/dist/app/active-reconnects.d.ts +10 -0
  59. package/dist/app/active-reconnects.d.ts.map +1 -0
  60. package/dist/app/active-reconnects.js +60 -0
  61. package/dist/app/active-reconnects.js.map +1 -0
  62. package/dist/app/application-guards.d.ts +5 -0
  63. package/dist/app/application-guards.d.ts.map +1 -1
  64. package/dist/app/application-guards.js +19 -0
  65. package/dist/app/application-guards.js.map +1 -1
  66. package/dist/app/application-options.d.ts +5 -0
  67. package/dist/app/application-options.d.ts.map +1 -1
  68. package/dist/app/application-runtime-wiring.d.ts +1 -0
  69. package/dist/app/application-runtime-wiring.d.ts.map +1 -1
  70. package/dist/app/application-runtime-wiring.js +1 -0
  71. package/dist/app/application-runtime-wiring.js.map +1 -1
  72. package/dist/app/application-support.d.ts +2 -1
  73. package/dist/app/application-support.d.ts.map +1 -1
  74. package/dist/app/application-support.js +5 -0
  75. package/dist/app/application-support.js.map +1 -1
  76. package/dist/app/application-transition.d.ts +2 -0
  77. package/dist/app/application-transition.d.ts.map +1 -1
  78. package/dist/app/application-transition.js +5 -1
  79. package/dist/app/application-transition.js.map +1 -1
  80. package/dist/app/application.d.ts +11 -1
  81. package/dist/app/application.d.ts.map +1 -1
  82. package/dist/app/application.js +84 -54
  83. package/dist/app/application.js.map +1 -1
  84. package/dist/app/canary/canary-ledger.d.ts +72 -0
  85. package/dist/app/canary/canary-ledger.d.ts.map +1 -0
  86. package/dist/app/canary/canary-ledger.js +161 -0
  87. package/dist/app/canary/canary-ledger.js.map +1 -0
  88. package/dist/app/canary/canary-observe.d.ts +58 -0
  89. package/dist/app/canary/canary-observe.d.ts.map +1 -0
  90. package/dist/app/canary/canary-observe.js +115 -0
  91. package/dist/app/canary/canary-observe.js.map +1 -0
  92. package/dist/app/canary/canary-pack.d.ts +53 -0
  93. package/dist/app/canary/canary-pack.d.ts.map +1 -0
  94. package/dist/app/canary/canary-pack.js +100 -0
  95. package/dist/app/canary/canary-pack.js.map +1 -0
  96. package/dist/app/canary/canary-protocol.d.ts +111 -0
  97. package/dist/app/canary/canary-protocol.d.ts.map +1 -0
  98. package/dist/app/canary/canary-protocol.js +260 -0
  99. package/dist/app/canary/canary-protocol.js.map +1 -0
  100. package/dist/app/connections.js +2 -0
  101. package/dist/app/connections.js.map +1 -1
  102. package/dist/app/journal-follower.d.ts +20 -0
  103. package/dist/app/journal-follower.d.ts.map +1 -0
  104. package/dist/app/journal-follower.js +61 -0
  105. package/dist/app/journal-follower.js.map +1 -0
  106. package/dist/app/learning/instruction-blocks.d.ts +100 -0
  107. package/dist/app/learning/instruction-blocks.d.ts.map +1 -0
  108. package/dist/app/learning/instruction-blocks.js +373 -0
  109. package/dist/app/learning/instruction-blocks.js.map +1 -0
  110. package/dist/app/learning/learner-measure.d.ts +44 -0
  111. package/dist/app/learning/learner-measure.d.ts.map +1 -0
  112. package/dist/app/learning/learner-measure.js +134 -0
  113. package/dist/app/learning/learner-measure.js.map +1 -0
  114. package/dist/app/learning/learner-profile.d.ts +35 -0
  115. package/dist/app/learning/learner-profile.d.ts.map +1 -0
  116. package/dist/app/learning/learner-profile.js +145 -0
  117. package/dist/app/learning/learner-profile.js.map +1 -0
  118. package/dist/app/learning/learning-actions.d.ts +44 -0
  119. package/dist/app/learning/learning-actions.d.ts.map +1 -0
  120. package/dist/app/learning/learning-actions.js +458 -0
  121. package/dist/app/learning/learning-actions.js.map +1 -0
  122. package/dist/app/learning/learning-candidates.d.ts +102 -0
  123. package/dist/app/learning/learning-candidates.d.ts.map +1 -0
  124. package/dist/app/learning/learning-candidates.js +628 -0
  125. package/dist/app/learning/learning-candidates.js.map +1 -0
  126. package/dist/app/learning/learning-ledger.d.ts +52 -0
  127. package/dist/app/learning/learning-ledger.d.ts.map +1 -0
  128. package/dist/app/learning/learning-ledger.js +353 -0
  129. package/dist/app/learning/learning-ledger.js.map +1 -0
  130. package/dist/app/learning/learning-measure.d.ts +44 -0
  131. package/dist/app/learning/learning-measure.d.ts.map +1 -0
  132. package/dist/app/learning/learning-measure.js +189 -0
  133. package/dist/app/learning/learning-measure.js.map +1 -0
  134. package/dist/app/learning/learning-profile.d.ts +53 -0
  135. package/dist/app/learning/learning-profile.d.ts.map +1 -0
  136. package/dist/app/learning/learning-profile.js +137 -0
  137. package/dist/app/learning/learning-profile.js.map +1 -0
  138. package/dist/app/learning/learning-replay.d.ts +45 -0
  139. package/dist/app/learning/learning-replay.d.ts.map +1 -0
  140. package/dist/app/learning/learning-replay.js +170 -0
  141. package/dist/app/learning/learning-replay.js.map +1 -0
  142. package/dist/app/learning/learning-sources.d.ts +81 -0
  143. package/dist/app/learning/learning-sources.d.ts.map +1 -0
  144. package/dist/app/learning/learning-sources.js +212 -0
  145. package/dist/app/learning/learning-sources.js.map +1 -0
  146. package/dist/app/learning/learning-store.d.ts +41 -0
  147. package/dist/app/learning/learning-store.d.ts.map +1 -0
  148. package/dist/app/learning/learning-store.js +107 -0
  149. package/dist/app/learning/learning-store.js.map +1 -0
  150. package/dist/app/learning/learning-types.d.ts +287 -0
  151. package/dist/app/learning/learning-types.d.ts.map +1 -0
  152. package/dist/app/learning/learning-types.js +2 -0
  153. package/dist/app/learning/learning-types.js.map +1 -0
  154. package/dist/app/provider-values.d.ts +2 -0
  155. package/dist/app/provider-values.d.ts.map +1 -1
  156. package/dist/app/provider-values.js +27 -0
  157. package/dist/app/provider-values.js.map +1 -1
  158. package/dist/app/run-continuation.d.ts +2 -1
  159. package/dist/app/run-continuation.d.ts.map +1 -1
  160. package/dist/app/run-continuation.js +11 -4
  161. package/dist/app/run-continuation.js.map +1 -1
  162. package/dist/app/run-event-mapper.d.ts.map +1 -1
  163. package/dist/app/run-event-mapper.js +6 -0
  164. package/dist/app/run-event-mapper.js.map +1 -1
  165. package/dist/app/run-execution.d.ts.map +1 -1
  166. package/dist/app/run-execution.js +4 -2
  167. package/dist/app/run-execution.js.map +1 -1
  168. package/dist/app/run-handoff-context.d.ts +21 -16
  169. package/dist/app/run-handoff-context.d.ts.map +1 -1
  170. package/dist/app/run-ingestion.d.ts.map +1 -1
  171. package/dist/app/run-ingestion.js +27 -3
  172. package/dist/app/run-ingestion.js.map +1 -1
  173. package/dist/app/run-replay.d.ts.map +1 -1
  174. package/dist/app/run-replay.js +120 -27
  175. package/dist/app/run-replay.js.map +1 -1
  176. package/dist/app/run-stream-coalescing.d.ts +16 -0
  177. package/dist/app/run-stream-coalescing.d.ts.map +1 -0
  178. package/dist/app/run-stream-coalescing.js +185 -0
  179. package/dist/app/run-stream-coalescing.js.map +1 -0
  180. package/dist/bin/args.d.ts +3 -2
  181. package/dist/bin/args.d.ts.map +1 -1
  182. package/dist/bin/args.js +13 -0
  183. package/dist/bin/args.js.map +1 -1
  184. package/dist/bin/braid-runtime.js +7 -3
  185. package/dist/bin/braid-runtime.js.map +1 -1
  186. package/dist/bin/braid.js +5 -0
  187. package/dist/bin/braid.js.map +1 -1
  188. package/dist/bin/canary-command.d.ts +16 -0
  189. package/dist/bin/canary-command.d.ts.map +1 -0
  190. package/dist/bin/canary-command.js +192 -0
  191. package/dist/bin/canary-command.js.map +1 -0
  192. package/dist/bin/fixture-application.d.ts +9 -0
  193. package/dist/bin/fixture-application.d.ts.map +1 -1
  194. package/dist/bin/fixture-application.js +22 -1
  195. package/dist/bin/fixture-application.js.map +1 -1
  196. package/dist/bin/interface-runner.d.ts.map +1 -1
  197. package/dist/bin/interface-runner.js +7 -2
  198. package/dist/bin/interface-runner.js.map +1 -1
  199. package/dist/bin/native-interactive-actions.d.ts +3 -2
  200. package/dist/bin/native-interactive-actions.d.ts.map +1 -1
  201. package/dist/bin/native-interactive-actions.js +12 -2
  202. package/dist/bin/native-interactive-actions.js.map +1 -1
  203. package/dist/bin/production-application.d.ts.map +1 -1
  204. package/dist/bin/production-application.js +1 -0
  205. package/dist/bin/production-application.js.map +1 -1
  206. package/dist/bin/production-config-mutation-lock.d.ts.map +1 -1
  207. package/dist/bin/production-config-mutation-lock.js +10 -2
  208. package/dist/bin/production-config-mutation-lock.js.map +1 -1
  209. package/dist/bin/production-setup-application.d.ts +6 -0
  210. package/dist/bin/production-setup-application.d.ts.map +1 -1
  211. package/dist/bin/production-setup-application.js +4 -0
  212. package/dist/bin/production-setup-application.js.map +1 -1
  213. package/dist/bin/production-setup-persistence.d.ts.map +1 -1
  214. package/dist/bin/production-setup-persistence.js +2 -8
  215. package/dist/bin/production-setup-persistence.js.map +1 -1
  216. package/dist/bin/production-setup-types.d.ts +2 -0
  217. package/dist/bin/production-setup-types.d.ts.map +1 -1
  218. package/dist/bin/runtime-startup-options.d.ts.map +1 -1
  219. package/dist/bin/runtime-startup-options.js +1 -0
  220. package/dist/bin/runtime-startup-options.js.map +1 -1
  221. package/dist/bin/state-path.d.ts +25 -0
  222. package/dist/bin/state-path.d.ts.map +1 -1
  223. package/dist/bin/state-path.js +36 -0
  224. package/dist/bin/state-path.js.map +1 -1
  225. package/dist/domain/bounded-structured.d.ts +12 -0
  226. package/dist/domain/bounded-structured.d.ts.map +1 -1
  227. package/dist/domain/bounded-structured.js +32 -4
  228. package/dist/domain/bounded-structured.js.map +1 -1
  229. package/dist/domain/connection-credential-references.d.ts +4 -1
  230. package/dist/domain/connection-credential-references.d.ts.map +1 -1
  231. package/dist/domain/connection-credential-references.js +43 -1
  232. package/dist/domain/connection-credential-references.js.map +1 -1
  233. package/dist/domain/entities-core.d.ts +7 -1
  234. package/dist/domain/entities-core.d.ts.map +1 -1
  235. package/dist/domain/events-legacy.d.ts +2 -0
  236. package/dist/domain/events-legacy.d.ts.map +1 -1
  237. package/dist/domain/invariants-base.d.ts +5 -5
  238. package/dist/domain/invariants-base.d.ts.map +1 -1
  239. package/dist/domain/invariants-base.js.map +1 -1
  240. package/dist/domain/redaction.d.ts +6 -0
  241. package/dist/domain/redaction.d.ts.map +1 -1
  242. package/dist/domain/redaction.js +25 -2
  243. package/dist/domain/redaction.js.map +1 -1
  244. package/dist/domain/reducer-conversation-events.d.ts +2 -0
  245. package/dist/domain/reducer-conversation-events.d.ts.map +1 -1
  246. package/dist/domain/reducer-conversation-events.js +13 -0
  247. package/dist/domain/reducer-conversation-events.js.map +1 -1
  248. package/dist/domain/reducer-support.d.ts.map +1 -1
  249. package/dist/domain/reducer-support.js +5 -3
  250. package/dist/domain/reducer-support.js.map +1 -1
  251. package/dist/domain/runtime-events.d.ts +5 -0
  252. package/dist/domain/runtime-events.d.ts.map +1 -1
  253. package/dist/domain/runtime-events.js.map +1 -1
  254. package/dist/ports/native-terminal-transport.d.ts +6 -0
  255. package/dist/ports/native-terminal-transport.d.ts.map +1 -1
  256. package/dist/profiles/discovery-operator.d.ts +2 -2
  257. package/dist/profiles/discovery-operator.js +21 -21
  258. package/dist/profiles/discovery-operator.js.map +1 -1
  259. package/dist/startup/durable-runtime.d.ts +2 -0
  260. package/dist/startup/durable-runtime.d.ts.map +1 -1
  261. package/dist/startup/durable-runtime.js +1 -0
  262. package/dist/startup/durable-runtime.js.map +1 -1
  263. package/dist/testing/deterministic-backend.d.ts +3 -0
  264. package/dist/testing/deterministic-backend.d.ts.map +1 -1
  265. package/dist/testing/deterministic-backend.js +1 -1
  266. package/dist/testing/deterministic-backend.js.map +1 -1
  267. package/dist/testing/learning-fixture.d.ts +20 -0
  268. package/dist/testing/learning-fixture.d.ts.map +1 -0
  269. package/dist/testing/learning-fixture.js +35 -0
  270. package/dist/testing/learning-fixture.js.map +1 -0
  271. package/dist/views/headless/protocol.d.ts +1 -1
  272. package/dist/views/headless/protocol.d.ts.map +1 -1
  273. package/dist/views/headless/rpc-parser.d.ts.map +1 -1
  274. package/dist/views/headless/rpc-parser.js +13 -0
  275. package/dist/views/headless/rpc-parser.js.map +1 -1
  276. package/dist/views/shared/command-registry.d.ts +1 -1
  277. package/dist/views/shared/command-registry.d.ts.map +1 -1
  278. package/dist/views/shared/command-registry.js +9 -0
  279. package/dist/views/shared/command-registry.js.map +1 -1
  280. package/dist/views/shared/command-table.d.ts +5 -0
  281. package/dist/views/shared/command-table.d.ts.map +1 -1
  282. package/dist/views/shared/command-table.js +6 -0
  283. package/dist/views/shared/command-table.js.map +1 -1
  284. package/dist/views/shared/headless-commands.d.ts +1 -1
  285. package/dist/views/shared/headless-commands.d.ts.map +1 -1
  286. package/dist/views/shared/headless-commands.js +2 -0
  287. package/dist/views/shared/headless-commands.js.map +1 -1
  288. package/dist/views/tui/terminal-app.d.ts +6 -2
  289. package/dist/views/tui/terminal-app.d.ts.map +1 -1
  290. package/dist/views/tui/terminal-app.js +8 -2
  291. package/dist/views/tui/terminal-app.js.map +1 -1
  292. package/dist/views/tui/terminal-command-controller.d.ts.map +1 -1
  293. package/dist/views/tui/terminal-command-controller.js +2 -0
  294. package/dist/views/tui/terminal-command-controller.js.map +1 -1
  295. package/dist/views/tui/terminal-overlays.d.ts +1 -0
  296. package/dist/views/tui/terminal-overlays.d.ts.map +1 -1
  297. package/dist/views/tui/terminal-overlays.js +3 -0
  298. package/dist/views/tui/terminal-overlays.js.map +1 -1
  299. package/dist/views/tui/terminal-surface-overlays.d.ts +2 -0
  300. package/dist/views/tui/terminal-surface-overlays.d.ts.map +1 -1
  301. package/dist/views/tui/terminal-surface-overlays.js +5 -1
  302. package/dist/views/tui/terminal-surface-overlays.js.map +1 -1
  303. package/dist/views/tui/transcript.d.ts.map +1 -1
  304. package/dist/views/tui/transcript.js +15 -3
  305. package/dist/views/tui/transcript.js.map +1 -1
  306. package/package.json +9 -10
@@ -0,0 +1,72 @@
1
+ /**
2
+ * The canary's assignment ledger: an append-only JSONL file whose entries form
3
+ * a hash chain. Each entry names the digest of the one before it and its own
4
+ * digest over everything else it holds, so an edit, a removal or a reorder of
5
+ * any earlier entry breaks every later one.
6
+ *
7
+ * The first entry is the sealed protocol. After it come assignments, one per
8
+ * task, and dispositions: arrivals the eligibility rule turned away, kept so
9
+ * the roster of arrivals is complete.
10
+ *
11
+ * When the ledger file sits in a git checkout with an `origin`, every append
12
+ * is a commit pushed to `origin`'s main branch. The push is the ledger's
13
+ * order across hosts: a rejected push means another host appended first, so
14
+ * the append is redone on top of what it pushed. The remote's history is the
15
+ * independent witness of when each entry existed.
16
+ */
17
+ export type CanaryArm = 'control' | 'candidate';
18
+ interface EntryBase {
19
+ readonly index: number;
20
+ readonly at: string;
21
+ /** The previous entry's digest; null for the protocol entry. */
22
+ readonly prev: string | null;
23
+ /** sha256 of the entry's canonical text without this field. */
24
+ readonly digest: string;
25
+ }
26
+ export interface ProtocolEntry extends EntryBase {
27
+ readonly kind: 'protocol';
28
+ readonly experimentId: string;
29
+ /** The digest agent-eval's seal computed over the registered analysis rule. */
30
+ readonly protocolDigest: string;
31
+ readonly protocol: Readonly<Record<string, unknown>>;
32
+ }
33
+ export interface AssignmentEntry extends EntryBase {
34
+ readonly kind: 'assignment';
35
+ readonly experimentId: string;
36
+ readonly assignmentId: string;
37
+ readonly taskId: string;
38
+ readonly skill: string;
39
+ readonly repository: string;
40
+ readonly arm: CanaryArm;
41
+ /** The sealed pack the candidate arm is served for this skill, named in both arms. */
42
+ readonly packDigest: string;
43
+ }
44
+ export interface DispositionEntry extends EntryBase {
45
+ readonly kind: 'disposition';
46
+ readonly experimentId: string;
47
+ readonly taskId: string;
48
+ readonly skill: string;
49
+ readonly repository: string;
50
+ readonly reason: string;
51
+ }
52
+ export type LedgerEntry = ProtocolEntry | AssignmentEntry | DispositionEntry;
53
+ type Unchained<T> = T extends LedgerEntry ? Omit<T, 'index' | 'at' | 'prev' | 'digest'> : never;
54
+ export type LedgerAppend = Unchained<LedgerEntry>;
55
+ /** The `sha256:`-prefixed digest of a text, as the ledger, the seal and the packs name it. */
56
+ export declare function canaryDigest(text: string): string;
57
+ /** Read and verify the chain. A missing file is an empty ledger. */
58
+ export declare function readLedger(path: string): LedgerEntry[];
59
+ /** Bring a git-backed ledger up to its remote. The local copy is never ahead of a completed append. */
60
+ export declare function syncLedger(path: string): void;
61
+ /**
62
+ * Decide and append one entry. `decide` sees the verified ledger as it stands
63
+ * after the remote's latest appends and returns the entry to append, or null
64
+ * when there is nothing to append (the task already holds an assignment).
65
+ * A rejected push redoes the decision on top of the remote's new entries.
66
+ */
67
+ export declare function appendLedger<T>(path: string, decide: (entries: readonly LedgerEntry[]) => {
68
+ readonly append: LedgerAppend | null;
69
+ readonly result: T;
70
+ }, now?: () => Date): T;
71
+ export {};
72
+ //# sourceMappingURL=canary-ledger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canary-ledger.d.ts","sourceRoot":"","sources":["../../../src/app/canary/canary-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAaH,MAAM,MAAM,SAAS,GAAG,SAAS,GAAG,WAAW,CAAA;AAE/C,UAAU,SAAS;IACjB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,gEAAgE;IAChE,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,+DAA+D;IAC/D,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CACxB;AAED,MAAM,WAAW,aAAc,SAAQ,SAAS;IAC9C,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;IACzB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,+EAA+E;IAC/E,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;IAC/B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;CACrD;AAED,MAAM,WAAW,eAAgB,SAAQ,SAAS;IAChD,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAA;IAC3B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAA;IACvB,sFAAsF;IACtF,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;CAC5B;AAED,MAAM,WAAW,gBAAiB,SAAQ,SAAS;IACjD,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAA;IAC5B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CACxB;AAED,MAAM,MAAM,WAAW,GAAG,aAAa,GAAG,eAAe,GAAG,gBAAgB,CAAA;AAE5E,KAAK,SAAS,CAAC,CAAC,IAAI,CAAC,SAAS,WAAW,GAAG,IAAI,CAAC,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,GAAG,QAAQ,CAAC,GAAG,KAAK,CAAA;AAC/F,MAAM,MAAM,YAAY,GAAG,SAAS,CAAC,WAAW,CAAC,CAAA;AAEjD,8FAA8F;AAC9F,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEjD;AAMD,oEAAoE;AACpE,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,EAAE,CA8BtD;AAoBD,uGAAuG;AACvG,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAO7C;AA2BD;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC5B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,CAAC,OAAO,EAAE,SAAS,WAAW,EAAE,KAAK;IAC3C,QAAQ,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA;IACpC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAA;CACnB,EACD,GAAG,GAAE,MAAM,IAAuB,GACjC,CAAC,CAuCH"}
@@ -0,0 +1,161 @@
1
+ /**
2
+ * The canary's assignment ledger: an append-only JSONL file whose entries form
3
+ * a hash chain. Each entry names the digest of the one before it and its own
4
+ * digest over everything else it holds, so an edit, a removal or a reorder of
5
+ * any earlier entry breaks every later one.
6
+ *
7
+ * The first entry is the sealed protocol. After it come assignments, one per
8
+ * task, and dispositions: arrivals the eligibility rule turned away, kept so
9
+ * the roster of arrivals is complete.
10
+ *
11
+ * When the ledger file sits in a git checkout with an `origin`, every append
12
+ * is a commit pushed to `origin`'s main branch. The push is the ledger's
13
+ * order across hosts: a rejected push means another host appended first, so
14
+ * the append is redone on top of what it pushed. The remote's history is the
15
+ * independent witness of when each entry existed.
16
+ */
17
+ import { execFileSync } from 'node:child_process';
18
+ import { appendFileSync, closeSync, existsSync, fsyncSync, openSync, readFileSync } from 'node:fs';
19
+ import { basename, dirname, join } from 'node:path';
20
+ import { acquirePrivateFileLock, releasePrivateFileLock, } from '../../adapters/persistence/safe-file.js';
21
+ import { canonicalJson } from '../../domain/canonical.js';
22
+ import { AppError } from '../errors.js';
23
+ import { sha256Hex } from '../learning/learning-profile.js';
24
+ /** The `sha256:`-prefixed digest of a text, as the ledger, the seal and the packs name it. */
25
+ export function canaryDigest(text) {
26
+ return `sha256:${sha256Hex(text)}`;
27
+ }
28
+ function entryDigest(entry) {
29
+ return canaryDigest(canonicalJson(entry));
30
+ }
31
+ /** Read and verify the chain. A missing file is an empty ledger. */
32
+ export function readLedger(path) {
33
+ if (!existsSync(path))
34
+ return [];
35
+ const entries = [];
36
+ const lines = readFileSync(path, 'utf8').split('\n');
37
+ for (const [number, line] of lines.entries()) {
38
+ if (line === '') {
39
+ if (number !== lines.length - 1)
40
+ throw new AppError('CANARY_LEDGER_INVALID', `ledger line ${number + 1} is empty`);
41
+ continue;
42
+ }
43
+ let entry;
44
+ try {
45
+ entry = JSON.parse(line);
46
+ }
47
+ catch {
48
+ throw new AppError('CANARY_LEDGER_INVALID', `ledger line ${number + 1} is not JSON`);
49
+ }
50
+ const previous = entries.at(-1);
51
+ const { digest, ...rest } = entry;
52
+ if (entry.index !== entries.length || entry.prev !== (previous?.digest ?? null))
53
+ throw new AppError('CANARY_LEDGER_INVALID', `ledger entry ${number + 1} breaks the chain`);
54
+ if (digest !== entryDigest(rest))
55
+ throw new AppError('CANARY_LEDGER_INVALID', `ledger entry ${number + 1} was edited`);
56
+ if ((entry.index === 0) !== (entry.kind === 'protocol'))
57
+ throw new AppError('CANARY_LEDGER_INVALID', 'the protocol is the first entry and only the first');
58
+ entries.push(entry);
59
+ }
60
+ return entries;
61
+ }
62
+ function git(directory, args) {
63
+ return execFileSync('git', ['-C', directory, ...args], {
64
+ encoding: 'utf8',
65
+ stdio: ['ignore', 'pipe', 'pipe'],
66
+ }).trim();
67
+ }
68
+ function gitRemote(directory) {
69
+ try {
70
+ return (git(directory, ['rev-parse', '--is-inside-work-tree']) === 'true' &&
71
+ git(directory, ['remote']).split('\n').includes('origin'));
72
+ }
73
+ catch {
74
+ return false;
75
+ }
76
+ }
77
+ /** Bring a git-backed ledger up to its remote. The local copy is never ahead of a completed append. */
78
+ export function syncLedger(path) {
79
+ const directory = dirname(path);
80
+ if (!gitRemote(directory))
81
+ return;
82
+ // A new remote has no main until its first append.
83
+ if (git(directory, ['ls-remote', '--heads', 'origin', 'main']) === '')
84
+ return;
85
+ git(directory, ['fetch', '--quiet', 'origin', 'main']);
86
+ git(directory, ['checkout', '--quiet', '--force', '-B', 'main', 'origin/main']);
87
+ }
88
+ function sleep(ms) {
89
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
90
+ }
91
+ function withLock(path, work) {
92
+ const lock = join(dirname(path), `.${basename(path)}.lock`);
93
+ for (let attempt = 0;; attempt++) {
94
+ let handle;
95
+ try {
96
+ handle = acquirePrivateFileLock(lock, 'The canary ledger');
97
+ }
98
+ catch (error) {
99
+ if (attempt < 100 && error instanceof Error && error.message.includes('busy')) {
100
+ sleep(50 + Math.floor(Math.random() * 100));
101
+ continue;
102
+ }
103
+ throw error;
104
+ }
105
+ try {
106
+ return work();
107
+ }
108
+ finally {
109
+ releasePrivateFileLock(lock, handle);
110
+ }
111
+ }
112
+ }
113
+ /**
114
+ * Decide and append one entry. `decide` sees the verified ledger as it stands
115
+ * after the remote's latest appends and returns the entry to append, or null
116
+ * when there is nothing to append (the task already holds an assignment).
117
+ * A rejected push redoes the decision on top of the remote's new entries.
118
+ */
119
+ export function appendLedger(path, decide, now = () => new Date()) {
120
+ const directory = dirname(path);
121
+ const remote = gitRemote(directory);
122
+ return withLock(path, () => {
123
+ for (let attempt = 0; attempt < 8; attempt++) {
124
+ if (remote)
125
+ syncLedger(path);
126
+ const entries = readLedger(path);
127
+ const { append, result } = decide(entries);
128
+ if (append === null)
129
+ return result;
130
+ const previous = entries.at(-1);
131
+ const unsigned = {
132
+ ...append,
133
+ index: entries.length,
134
+ at: now().toISOString(),
135
+ prev: previous?.digest ?? null,
136
+ };
137
+ const entry = { ...unsigned, digest: entryDigest(unsigned) };
138
+ appendFileSync(path, `${JSON.stringify(entry)}\n`, { mode: 0o600 });
139
+ const handle = openSync(path, 'r');
140
+ try {
141
+ fsyncSync(handle);
142
+ }
143
+ finally {
144
+ closeSync(handle);
145
+ }
146
+ if (!remote)
147
+ return result;
148
+ git(directory, ['add', basename(path)]);
149
+ git(directory, ['commit', '--quiet', '-m', `${entry.kind} ${entry.index}`]);
150
+ try {
151
+ git(directory, ['push', '--quiet', 'origin', 'HEAD:main']);
152
+ return result;
153
+ }
154
+ catch {
155
+ sleep(100 + Math.floor(Math.random() * 400));
156
+ }
157
+ }
158
+ throw new AppError('CANARY_LEDGER_CONTENDED', 'The ledger remote kept moving; nothing was assigned. Try again.');
159
+ });
160
+ }
161
+ //# sourceMappingURL=canary-ledger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canary-ledger.js","sourceRoot":"","sources":["../../../src/app/canary/canary-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE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B,EACzB,iEAAiE,CAClE,CAAA;IACH,CAAC,CAAC,CAAA;AACJ,CAAC"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * The canary's secondary measures, read from this host's own sessions.
3
+ *
4
+ * Every brief in either arm carries its assignment line, so the sessions that
5
+ * did a task are those whose human turns hold `Braid-Assignment: <id>`. For
6
+ * each assignment Braid reads those sessions with the sealed procedure
7
+ * contract, and reports the procedure mistakes they made per rule, the tokens
8
+ * their model calls used, and the span from their first to their last event.
9
+ * Missing evidence stays null: no session found, a check that could not run,
10
+ * or a model call with no recorded usage is never a zero.
11
+ *
12
+ * Sessions live on the host that ran them, so each host reports its own rows;
13
+ * a readout joins them by assignment and session.
14
+ */
15
+ import type { TraceContractSpec } from '@tangle-network/agent-eval';
16
+ import { type HarnessSessionPaths } from '../learning/learning-sources.js';
17
+ import { type CanaryConfig } from './canary-protocol.js';
18
+ export interface ObservedSession {
19
+ readonly harness: string;
20
+ readonly sessionId: string;
21
+ readonly firstAt: string | null;
22
+ readonly lastAt: string | null;
23
+ /** Violations per contract rule; null when the check could not run. */
24
+ readonly mistakes: Readonly<Record<string, number>> | null;
25
+ readonly modelCalls: number;
26
+ /** Null when a model call recorded no usage. */
27
+ readonly inputTokens: number | null;
28
+ readonly outputTokens: number | null;
29
+ }
30
+ export interface ObservedAssignment {
31
+ readonly assignmentId: string;
32
+ readonly taskId: string;
33
+ readonly skill: string;
34
+ readonly repository: string;
35
+ readonly arm: string;
36
+ readonly assignedAt: string;
37
+ readonly sessions: readonly ObservedSession[];
38
+ readonly procedureMistakes: number | null;
39
+ readonly mistakesByRule: Readonly<Record<string, number>> | null;
40
+ readonly inputTokens: number | null;
41
+ readonly outputTokens: number | null;
42
+ readonly firstAt: string | null;
43
+ readonly lastAt: string | null;
44
+ readonly wallMs: number | null;
45
+ }
46
+ export interface CanaryObservations {
47
+ readonly experimentId: string;
48
+ readonly protocolDigest: string;
49
+ readonly contractDigest: string;
50
+ readonly observedAt: string;
51
+ readonly sessionsRead: number;
52
+ readonly rows: readonly ObservedAssignment[];
53
+ }
54
+ export declare function observeCanary(config: CanaryConfig, options: {
55
+ readonly contract: TraceContractSpec;
56
+ readonly paths: HarnessSessionPaths;
57
+ }): Promise<CanaryObservations>;
58
+ //# sourceMappingURL=canary-observe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canary-observe.d.ts","sourceRoot":"","sources":["../../../src/app/canary/canary-observe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AAInE,OAAO,EAAE,KAAK,mBAAmB,EAAuB,MAAM,iCAAiC,CAAA;AAE/F,OAAO,EAA0B,KAAK,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAEhF,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;IAC9B,uEAAuE;IACvE,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,IAAI,CAAA;IAC1D,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,gDAAgD;IAChD,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;IACnC,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAA;CACrC;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,QAAQ,EAAE,SAAS,eAAe,EAAE,CAAA;IAC7C,QAAQ,CAAC,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAA;IACzC,QAAQ,CAAC,cAAc,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,IAAI,CAAA;IAChE,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;IACnC,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAA;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;IAC9B,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CAC/B;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;IAC/B,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;IAC/B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,QAAQ,CAAC,IAAI,EAAE,SAAS,kBAAkB,EAAE,CAAA;CAC7C;AAqCD,wBAAsB,aAAa,CACjC,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE;IAAE,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAA;CAAE,GACrF,OAAO,CAAC,kBAAkB,CAAC,CAoE7B"}
@@ -0,0 +1,115 @@
1
+ /**
2
+ * The canary's secondary measures, read from this host's own sessions.
3
+ *
4
+ * Every brief in either arm carries its assignment line, so the sessions that
5
+ * did a task are those whose human turns hold `Braid-Assignment: <id>`. For
6
+ * each assignment Braid reads those sessions with the sealed procedure
7
+ * contract, and reports the procedure mistakes they made per rule, the tokens
8
+ * their model calls used, and the span from their first to their last event.
9
+ * Missing evidence stays null: no session found, a check that could not run,
10
+ * or a model call with no recorded usage is never a zero.
11
+ *
12
+ * Sessions live on the host that ran them, so each host reports its own rows;
13
+ * a readout joins them by assignment and session.
14
+ */
15
+ import { canonicalJson } from '../../domain/canonical.js';
16
+ import { AppError } from '../errors.js';
17
+ import { readHarnessSessions } from '../learning/learning-sources.js';
18
+ import { canaryDigest, readLedger, syncLedger } from './canary-ledger.js';
19
+ import { ASSIGNMENT_LINE_PREFIX } from './canary-protocol.js';
20
+ function lineOf(text) {
21
+ const found = new Set();
22
+ const pattern = new RegExp(`^[ \\t>*-]*${ASSIGNMENT_LINE_PREFIX}[ \\t]*(ba-[0-9a-f]{16})[ \\t]*$`, 'gmu');
23
+ for (const match of text.matchAll(pattern))
24
+ found.add(match[1]);
25
+ return found;
26
+ }
27
+ function sum(values) {
28
+ return values.some((value) => value === null)
29
+ ? null
30
+ : values.reduce((total, value) => total + value, 0);
31
+ }
32
+ function observeSession(session) {
33
+ const times = session.events
34
+ .map((event) => event.at)
35
+ .filter((at) => at !== null)
36
+ .sort();
37
+ const check = session.contractChecks?.[0];
38
+ const usages = session.events.flatMap((event) => (event.kind === 'model' ? [event.usage] : []));
39
+ return {
40
+ harness: session.harness,
41
+ sessionId: session.sessionId,
42
+ firstAt: times[0] ?? null,
43
+ lastAt: times.at(-1) ?? null,
44
+ mistakes: check === undefined || check.status === 'error' ? null : { ...check.violations },
45
+ modelCalls: usages.length,
46
+ inputTokens: sum(usages.map((usage) => usage?.inputTokens ?? null)),
47
+ outputTokens: sum(usages.map((usage) => usage?.outputTokens ?? null)),
48
+ };
49
+ }
50
+ export async function observeCanary(config, options) {
51
+ syncLedger(config.ledger);
52
+ const entries = readLedger(config.ledger);
53
+ const sealed = entries[0];
54
+ if (sealed === undefined || sealed.kind !== 'protocol')
55
+ throw new AppError('CANARY_NOT_SEALED', 'The ledger holds no sealed protocol');
56
+ const contractDigest = canaryDigest(canonicalJson(options.contract));
57
+ const secondary = sealed.protocol.secondary;
58
+ if (secondary?.contractDigest !== contractDigest)
59
+ throw new AppError('CANARY_CONTRACT_NOT_SEALED', `The protocol sealed procedure contract ${String(secondary?.contractDigest ?? 'none')}; this one is ${contractDigest}`);
60
+ const { compileTraceContractSpec } = await import('@tangle-network/agent-eval');
61
+ const read = await readHarnessSessions(options.paths, Date.parse(sealed.at), {
62
+ contracts: [compileTraceContractSpec(options.contract)],
63
+ });
64
+ const byAssignment = new Map();
65
+ for (const session of read.sessions)
66
+ for (const id of lineOf((session.turns ?? []).map((turn) => turn.text).join('\n')))
67
+ byAssignment.set(id, [...(byAssignment.get(id) ?? []), session]);
68
+ const rows = entries
69
+ .filter((entry) => entry.kind === 'assignment')
70
+ .map((entry) => {
71
+ const sessions = (byAssignment.get(entry.assignmentId) ?? []).map(observeSession);
72
+ const found = sessions.length > 0;
73
+ const byRule = {};
74
+ const checked = found && sessions.every((session) => session.mistakes !== null);
75
+ if (checked)
76
+ for (const session of sessions)
77
+ for (const [rule, count] of Object.entries(session.mistakes ?? {}))
78
+ byRule[rule] = (byRule[rule] ?? 0) + count;
79
+ const firsts = sessions
80
+ .map((session) => session.firstAt)
81
+ .filter((at) => at !== null)
82
+ .sort();
83
+ const lasts = sessions
84
+ .map((session) => session.lastAt)
85
+ .filter((at) => at !== null)
86
+ .sort();
87
+ const firstAt = firsts[0] ?? null;
88
+ const lastAt = lasts.at(-1) ?? null;
89
+ return {
90
+ assignmentId: entry.assignmentId,
91
+ taskId: entry.taskId,
92
+ skill: entry.skill,
93
+ repository: entry.repository,
94
+ arm: entry.arm,
95
+ assignedAt: entry.at,
96
+ sessions,
97
+ procedureMistakes: checked ? Object.values(byRule).reduce((a, b) => a + b, 0) : null,
98
+ mistakesByRule: checked ? byRule : null,
99
+ inputTokens: found ? sum(sessions.map((session) => session.inputTokens)) : null,
100
+ outputTokens: found ? sum(sessions.map((session) => session.outputTokens)) : null,
101
+ firstAt,
102
+ lastAt,
103
+ wallMs: firstAt !== null && lastAt !== null ? Date.parse(lastAt) - Date.parse(firstAt) : null,
104
+ };
105
+ });
106
+ return {
107
+ experimentId: sealed.experimentId,
108
+ protocolDigest: sealed.protocolDigest,
109
+ contractDigest,
110
+ observedAt: new Date().toISOString(),
111
+ sessionsRead: read.sessions.length,
112
+ rows,
113
+ };
114
+ }
115
+ //# sourceMappingURL=canary-observe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canary-observe.js","sourceRoot":"","sources":["../../../src/app/canary/canary-observe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AACvC,OAAO,EAA4B,mBAAmB,EAAE,MAAM,iCAAiC,CAAA;AAC/F,OAAO,EAAwB,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAC/F,OAAO,EAAE,sBAAsB,EAAqB,MAAM,sBAAsB,CAAA;AAyChF,SAAS,MAAM,CAAC,IAAY;IAC1B,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAA;IAC/B,MAAM,OAAO,GAAG,IAAI,MAAM,CACxB,cAAc,sBAAsB,kCAAkC,EACtE,KAAK,CACN,CAAA;IACD,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAW,CAAC,CAAA;IACzE,OAAO,KAAK,CAAA;AACd,CAAC;AAED,SAAS,GAAG,CAAC,MAAkC;IAC7C,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC;QAC3C,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,MA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,IAAI;YACjF,OAAO;YACP,MAAM;YACN,MAAM,EACJ,OAAO,KAAK,IAAI,IAAI,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI;SACxF,CAAA;IACH,CAAC,CAAC,CAAA;IACJ,OAAO;QACL,YAAY,EAAE,MAAM,CAAC,YAAY;QACjC,cAAc,EAAE,MAAM,CAAC,cAAc;QACrC,cAAc;QACd,UAAU,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QACpC,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM;QAClC,IAAI;KACL,CAAA;AACH,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Learn a procedure pack per skill from the user's past sessions.
3
+ *
4
+ * A procedure contract states procedures as trace-contract rules over what an
5
+ * agent ran and said, for example "a check's output is never piped into
6
+ * `tail`". Braid reads every Claude Code and Codex session in the window,
7
+ * checks each against the contract while reading it, and for each skill
8
+ * counts the sessions that used the skill and broke each rule. A skill's pack
9
+ * is the procedures its own sessions broke most, each with how often, in the
10
+ * words the procedure catalog gives the rule. A skill whose sessions broke no
11
+ * rule often enough gets no pack, so a brief naming it is not eligible.
12
+ */
13
+ import type { TraceContractSpec } from '@tangle-network/agent-eval';
14
+ import { type HarnessSessionPaths } from '../learning/learning-sources.js';
15
+ import type { PackManifest } from './canary-protocol.js';
16
+ export interface LearnPacksOptions {
17
+ readonly contract: TraceContractSpec;
18
+ /** Each rule label of the contract, as the procedure to follow. */
19
+ readonly procedures: Readonly<Record<string, string>>;
20
+ readonly out: string;
21
+ readonly sinceMs: number;
22
+ readonly untilMs: number;
23
+ readonly paths: HarnessSessionPaths;
24
+ /** Only these skills; default every skill the sessions invoked. */
25
+ readonly skills?: readonly string[];
26
+ /** A skill needs this many checked sessions to get a pack. Default 5. */
27
+ readonly minSessions?: number;
28
+ /** A procedure enters a pack when this many of the skill's sessions broke it. Default 2. */
29
+ readonly minRuleSessions?: number;
30
+ /** Procedures per pack. Default 6. */
31
+ readonly maxProcedures?: number;
32
+ }
33
+ export interface LearnedSkill {
34
+ readonly sessions: number;
35
+ /** Sessions whose check could not evaluate a rule; they are left out of `sessions`. */
36
+ readonly unchecked: number;
37
+ readonly rules: readonly {
38
+ readonly rule: string;
39
+ readonly sessions: number;
40
+ }[];
41
+ readonly file: string | null;
42
+ readonly digest: string | null;
43
+ }
44
+ export interface LearnedPacks extends PackManifest {
45
+ readonly since: string;
46
+ readonly until: string;
47
+ readonly contract: string;
48
+ readonly contractDigest: string;
49
+ readonly sessionsRead: number;
50
+ readonly learned: Readonly<Record<string, LearnedSkill>>;
51
+ }
52
+ export declare function learnPacks(options: LearnPacksOptions): Promise<LearnedPacks>;
53
+ //# sourceMappingURL=canary-pack.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canary-pack.d.ts","sourceRoot":"","sources":["../../../src/app/canary/canary-pack.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AAGnE,OAAO,EACL,KAAK,mBAAmB,EAGzB,MAAM,iCAAiC,CAAA;AAExC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAExD,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAA;IACpC,mEAAmE;IACnE,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IACrD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAA;IACnC,mEAAmE;IACnE,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IACnC,yEAAyE;IACzE,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;IAC7B,4FAA4F;IAC5F,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAA;IACjC,sCAAsC;IACtC,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAA;CAChC;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,uFAAuF;IACvF,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,KAAK,EAAE,SAAS;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;IAC/E,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CAC/B;AAED,MAAM,WAAW,YAAa,SAAQ,YAAY;IAChD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;IAC/B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAA;CACzD;AAsBD,wBAAsB,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,YAAY,CAAC,CA+ElF"}
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Learn a procedure pack per skill from the user's past sessions.
3
+ *
4
+ * A procedure contract states procedures as trace-contract rules over what an
5
+ * agent ran and said, for example "a check's output is never piped into
6
+ * `tail`". Braid reads every Claude Code and Codex session in the window,
7
+ * checks each against the contract while reading it, and for each skill
8
+ * counts the sessions that used the skill and broke each rule. A skill's pack
9
+ * is the procedures its own sessions broke most, each with how often, in the
10
+ * words the procedure catalog gives the rule. A skill whose sessions broke no
11
+ * rule often enough gets no pack, so a brief naming it is not eligible.
12
+ */
13
+ import { chmodSync, mkdirSync, writeFileSync } from 'node:fs';
14
+ import { join } from 'node:path';
15
+ import { canonicalJson } from '../../domain/canonical.js';
16
+ import { AppError } from '../errors.js';
17
+ import { readHarnessSessions, sessionsInvoking, } from '../learning/learning-sources.js';
18
+ import { canaryDigest } from './canary-ledger.js';
19
+ function packText(skill, sessions, since, until, lines) {
20
+ return [
21
+ `## Procedures learned from past ${skill} sessions`,
22
+ '',
23
+ `Braid read ${sessions} sessions from ${since.slice(0, 10)} to ${until.slice(0, 10)} that used ${skill}.`,
24
+ 'Agents in them broke these procedures. Follow each one in this task.',
25
+ '',
26
+ ...lines.map((line) => `- ${line.procedure} (broken in ${line.sessions} of ${sessions} sessions)`),
27
+ '',
28
+ ].join('\n');
29
+ }
30
+ export async function learnPacks(options) {
31
+ const { compileTraceContractSpec } = await import('@tangle-network/agent-eval');
32
+ const contract = compileTraceContractSpec(options.contract);
33
+ const labels = (options.contract.rules ?? []).map((rule) => rule.label);
34
+ if (labels.length === 0 || labels.some((label) => typeof label !== 'string'))
35
+ throw new AppError('CANARY_CONTRACT_INVALID', 'Every procedure rule needs a label');
36
+ for (const label of labels)
37
+ if (typeof options.procedures[label] !== 'string' || options.procedures[label] === '')
38
+ throw new AppError('CANARY_PROCEDURE_MISSING', `The catalog gives no procedure for ${label}`);
39
+ const minSessions = options.minSessions ?? 5;
40
+ const minRuleSessions = options.minRuleSessions ?? 2;
41
+ const maxProcedures = options.maxProcedures ?? 6;
42
+ const since = new Date(options.sinceMs).toISOString();
43
+ const until = new Date(options.untilMs).toISOString();
44
+ const read = await readHarnessSessions(options.paths, options.sinceMs, { contracts: [contract] });
45
+ const window = read.sessions.filter((session) => session.endedAt === null || Date.parse(session.endedAt) < options.untilMs);
46
+ const skills = options.skills ?? [...new Set((read.usage?.rows ?? []).map((row) => row.skill))].sort();
47
+ mkdirSync(options.out, { recursive: true, mode: 0o700 });
48
+ chmodSync(options.out, 0o700);
49
+ const manifest = {};
50
+ const learned = {};
51
+ for (const skill of skills) {
52
+ const invoking = sessionsInvoking(window, skill, options.sinceMs);
53
+ const checks = invoking.map((session) => session.contractChecks?.[0]);
54
+ const checked = checks.filter((check) => check !== undefined && check.status !== 'error');
55
+ const counts = new Map();
56
+ for (const check of checked)
57
+ for (const [rule, violations] of Object.entries(check?.violations ?? {}))
58
+ if (violations > 0)
59
+ counts.set(rule, (counts.get(rule) ?? 0) + 1);
60
+ const rules = [...counts]
61
+ .map(([rule, sessions]) => ({ rule, sessions }))
62
+ .sort((a, b) => b.sessions - a.sessions || a.rule.localeCompare(b.rule));
63
+ const chosen = checked.length < minSessions
64
+ ? []
65
+ : rules.filter((rule) => rule.sessions >= minRuleSessions).slice(0, maxProcedures);
66
+ let file = null;
67
+ let digest = null;
68
+ if (chosen.length > 0) {
69
+ const text = packText(skill, checked.length, since, until, chosen.map((rule) => ({
70
+ procedure: options.procedures[rule.rule],
71
+ sessions: rule.sessions,
72
+ })));
73
+ file = `${skill.replace(/[^A-Za-z0-9_.-]/gu, '_')}.md`;
74
+ digest = canaryDigest(text);
75
+ writeFileSync(join(options.out, file), text, { mode: 0o600 });
76
+ manifest[skill] = { file, digest };
77
+ }
78
+ learned[skill] = {
79
+ sessions: checked.length,
80
+ unchecked: invoking.length - checked.length,
81
+ rules,
82
+ file,
83
+ digest,
84
+ };
85
+ }
86
+ const result = {
87
+ since,
88
+ until,
89
+ contract: options.contract.name,
90
+ contractDigest: canaryDigest(canonicalJson(options.contract)),
91
+ sessionsRead: window.length,
92
+ skills: manifest,
93
+ learned,
94
+ };
95
+ writeFileSync(join(options.out, 'manifest.json'), `${JSON.stringify(result, null, 2)}\n`, {
96
+ mode: 0o600,
97
+ });
98
+ return result;
99
+ }
100
+ //# sourceMappingURL=canary-pack.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canary-pack.js","sourceRoot":"","sources":["../../../src/app/canary/canary-pack.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAC7D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEhC,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AACvC,OAAO,EAEL,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,iCAAiC,CAAA;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAuCjD,SAAS,QAAQ,CACf,KAAa,EACb,QAAgB,EAChB,KAAa,EACb,KAAa,EACb,KAA2E;IAE3E,OAAO;QACL,mCAAmC,KAAK,WAAW;QACnD,EAAE;QACF,cAAc,QAAQ,kBAAkB,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,cAAc,KAAK,GAAG;QACzG,sEAAsE;QACtE,EAAE;QACF,GAAG,KAAK,CAAC,GAAG,CACV,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,IAAI,CAAC,SAAS,eAAe,IAAI,CAAC,QAAQ,OAAO,QAAQ,YAAY,CACrF;QACD,EAAE;KACH,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACd,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,OAA0B;IACzD,MAAM,EAAE,wBAAwB,EAAE,GAAG,MAAM,MAAM,CAAC,4BAA4B,CAAC,CAAA;IAC/E,MAAM,QAAQ,GAAG,wBAAwB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;IAC3D,MAAM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