talon-agent 4.6.1 → 5.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 (310) hide show
  1. package/README.md +3 -3
  2. package/package.json +2 -1
  3. package/prompts/README.md +2 -2
  4. package/prompts/system/memory-recall.md +16 -0
  5. package/src/app.ts +22 -7
  6. package/src/backend/claude-sdk/factory.ts +21 -41
  7. package/src/backend/claude-sdk/handler.ts +28 -7
  8. package/src/backend/claude-sdk/host/in-process.ts +147 -0
  9. package/src/backend/claude-sdk/one-shot.ts +1 -1
  10. package/src/backend/claude-sdk/options.ts +7 -6
  11. package/src/backend/claude-sdk/stream.ts +30 -3
  12. package/src/backend/claude-sdk/warm.ts +1 -1
  13. package/src/backend/codex/constants.ts +1 -1
  14. package/src/backend/codex/factory.ts +2 -2
  15. package/src/backend/codex/handler/events.ts +1 -1
  16. package/src/backend/codex/handler/message.ts +26 -12
  17. package/src/backend/codex/handler/rollout-accounting.ts +1 -1
  18. package/src/backend/codex/init.ts +1 -1
  19. package/src/backend/codex/mcp-config.ts +2 -2
  20. package/src/backend/codex/one-shot.ts +1 -1
  21. package/src/backend/kilo/handler/message.ts +4 -1
  22. package/src/backend/openai-agents/constants.ts +1 -1
  23. package/src/backend/openai-agents/factory.ts +2 -2
  24. package/src/backend/openai-agents/handler/events.ts +1 -1
  25. package/src/backend/openai-agents/handler/message.ts +8 -4
  26. package/src/backend/openai-agents/init.ts +1 -1
  27. package/src/backend/openai-agents/mcp-pool.ts +2 -2
  28. package/src/backend/opencode/handler/message.ts +4 -1
  29. package/src/backend/remote-server/chat-turn.ts +31 -23
  30. package/src/backend/remote-server/events.ts +3 -3
  31. package/src/backend/remote-server/factory.ts +6 -3
  32. package/src/backend/remote-server/index.ts +1 -1
  33. package/src/backend/remote-server/mcp.ts +1 -1
  34. package/src/backend/remote-server/one-shot.ts +1 -1
  35. package/src/backend/remote-server/server-bindings.ts +1 -1
  36. package/src/backend/remote-server/turn.ts +1 -1
  37. package/src/backend/runtime/cache/cache-metrics.ts +126 -0
  38. package/src/backend/{shared → runtime/cache}/cache-telemetry.ts +22 -2
  39. package/src/backend/{shared → runtime}/index.ts +34 -25
  40. package/src/backend/{shared → runtime/prompt}/delivery-contract.ts +1 -1
  41. package/src/backend/{shared → runtime/prompt}/prompt-format.ts +46 -2
  42. package/src/backend/{shared → runtime/prompt}/system-prompt.ts +4 -4
  43. package/src/backend/{shared → runtime/turn}/delivered-text.ts +1 -1
  44. package/src/backend/{shared → runtime/turn}/delivery.ts +3 -3
  45. package/src/backend/{shared → runtime/turn}/handle-retry.ts +5 -5
  46. package/src/backend/{shared → runtime/turn}/handler-to-events.ts +24 -10
  47. package/src/backend/{shared → runtime/turn}/handler-types.ts +7 -1
  48. package/src/backend/{shared → runtime/turn}/model-retry.ts +2 -2
  49. package/src/backend/{shared → runtime/turn}/result-events.ts +2 -2
  50. package/src/backend/{shared → runtime/turn}/stream-state.ts +2 -2
  51. package/src/backend/{shared → runtime/turn}/turn-interrupt.ts +2 -2
  52. package/src/backend/{shared → runtime/turn}/turn-phases.ts +6 -6
  53. package/src/bootstrap.ts +6 -21
  54. package/src/cli.ts +6 -3
  55. package/src/core/agent-runtime/README.md +15 -0
  56. package/src/core/agent-runtime/agent-host.ts +387 -0
  57. package/src/core/agent-runtime/capabilities.ts +11 -0
  58. package/src/core/agent-runtime/contract-tests.ts +92 -1
  59. package/src/core/agent-runtime/events.ts +1 -1
  60. package/src/core/background/{isolated-agent.ts → cron/isolated-agent.ts} +3 -3
  61. package/src/core/background/{job-health.ts → cron/job-health.ts} +1 -1
  62. package/src/core/background/{job-oneshot.ts → cron/job-oneshot.ts} +5 -5
  63. package/src/core/background/{job-prompt.ts → cron/job-prompt.ts} +1 -1
  64. package/src/core/background/{cron.ts → cron/scheduler.ts} +6 -6
  65. package/src/core/background/{cron-spec.ts → cron/spec.ts} +1 -1
  66. package/src/core/background/{dream.ts → dream/index.ts} +10 -15
  67. package/src/core/background/{plan-alerts.ts → pulse/plan-alerts.ts} +3 -3
  68. package/src/core/background/{pulse.ts → pulse/pulse.ts} +8 -5
  69. package/src/core/background/triggers/command.ts +2 -2
  70. package/src/{util → core/config}/harden.ts +1 -1
  71. package/src/core/config/index.ts +16 -12
  72. package/src/{util → core/daemon}/boot-timer.ts +15 -1
  73. package/src/core/daemon/pidfile.ts +1 -1
  74. package/src/core/daemon/resource-sampler.ts +121 -0
  75. package/src/{util → core/daemon}/respawn.ts +1 -1
  76. package/src/core/doctor/index.ts +1 -1
  77. package/src/core/engine/dispatcher.ts +16 -0
  78. package/src/core/engine/gateway-actions/cron.ts +2 -2
  79. package/src/core/engine/gateway-actions/fetch-url.ts +1 -1
  80. package/src/core/engine/gateway-actions/index.ts +4 -1
  81. package/src/core/engine/gateway-actions/memory.ts +335 -0
  82. package/src/core/engine/gateway-actions/native/exec-background.ts +129 -0
  83. package/src/core/engine/gateway-actions/native/exec-remote.ts +85 -0
  84. package/src/core/engine/gateway-actions/native/exec.ts +182 -0
  85. package/src/core/engine/gateway-actions/native/index.ts +46 -0
  86. package/src/core/engine/gateway-actions/native/params.ts +46 -0
  87. package/src/core/engine/gateway-actions/native/read.ts +182 -0
  88. package/src/core/engine/gateway-actions/native/results.ts +34 -0
  89. package/src/core/engine/gateway-actions/native/search.ts +212 -0
  90. package/src/core/engine/gateway-actions/native/shell.ts +39 -0
  91. package/src/core/engine/gateway-actions/native/teleport.ts +55 -0
  92. package/src/core/engine/gateway-actions/native/write.ts +170 -0
  93. package/src/core/engine/gateway.ts +0 -6
  94. package/src/core/frontend-runtime/builtins.ts +2 -2
  95. package/src/{util → core/frontend-runtime}/chat-id.ts +30 -0
  96. package/src/core/mcp-hub/index.ts +1 -1
  97. package/src/core/mcp-hub/talon-server.ts +1 -1
  98. package/src/core/memory/import.ts +3 -2
  99. package/src/core/memory/taps.ts +199 -0
  100. package/src/core/memory/turn-retrieval.ts +222 -0
  101. package/src/core/plugin/mcp.ts +1 -1
  102. package/src/core/prompt/assemble.ts +2 -12
  103. package/src/core/prompt/index.ts +2 -2
  104. package/src/core/prompt/invalidation.ts +1 -1
  105. package/src/core/{scripting/lua-runner.ts → scripts/lua.ts} +1 -1
  106. package/src/core/tasks/index.ts +1 -1
  107. package/src/core/tools/{chat.ts → chat/chat.ts} +1 -1
  108. package/src/core/tools/{cross-send.ts → chat/cross-send.ts} +1 -1
  109. package/src/core/tools/{history.ts → chat/history.ts} +2 -2
  110. package/src/core/tools/{media.ts → chat/media.ts} +1 -1
  111. package/src/core/tools/{members.ts → chat/members.ts} +2 -2
  112. package/src/core/tools/{messaging.ts → chat/messaging.ts} +2 -2
  113. package/src/core/tools/{moderation.ts → chat/moderation.ts} +2 -2
  114. package/src/core/tools/{stickers.ts → chat/stickers.ts} +2 -2
  115. package/src/core/tools/{whatsapp.ts → chat/whatsapp.ts} +1 -1
  116. package/src/core/tools/content/memory.ts +128 -0
  117. package/src/core/tools/{web.ts → content/web.ts} +1 -1
  118. package/src/core/tools/index.ts +21 -19
  119. package/src/core/tools/{admin.ts → ops/admin.ts} +1 -1
  120. package/src/core/tools/{bridge.ts → ops/bridge.ts} +2 -2
  121. package/src/core/tools/{goals.ts → ops/goals.ts} +2 -2
  122. package/src/core/tools/{mesh.ts → ops/mesh.ts} +1 -1
  123. package/src/core/tools/{models.ts → ops/models.ts} +1 -1
  124. package/src/core/tools/{native.ts → ops/native.ts} +1 -1
  125. package/src/core/tools/{scheduling.ts → ops/scheduling.ts} +1 -1
  126. package/src/core/tools/{scripts.ts → ops/scripts.ts} +1 -1
  127. package/src/core/tools/{skills.ts → ops/skills.ts} +1 -1
  128. package/src/core/tools/{triggers.ts → ops/triggers.ts} +1 -1
  129. package/src/core/tools/types.ts +1 -0
  130. package/src/{util → core/vfs}/workspace.ts +2 -2
  131. package/src/core/weaver/turn-cpu.ts +32 -0
  132. package/src/core/weaver/weaver.ts +36 -0
  133. package/src/frontend/discord/admin.ts +1 -1
  134. package/src/frontend/discord/callbacks/components/backend-select.ts +1 -1
  135. package/src/frontend/discord/callbacks/components/effort.ts +1 -1
  136. package/src/frontend/discord/callbacks/components/index.ts +1 -1
  137. package/src/frontend/discord/callbacks/components/pulse.ts +1 -1
  138. package/src/frontend/discord/callbacks/components/settings.ts +2 -2
  139. package/src/frontend/discord/callbacks/modals.ts +4 -1
  140. package/src/frontend/discord/commands/admin.ts +2 -36
  141. package/src/frontend/discord/commands/definitions.ts +0 -11
  142. package/src/frontend/discord/commands/info.ts +1 -1
  143. package/src/frontend/discord/commands/interaction.ts +1 -1
  144. package/src/frontend/discord/commands/router.ts +0 -3
  145. package/src/frontend/discord/commands/session.ts +8 -2
  146. package/src/frontend/discord/commands/settings.ts +2 -2
  147. package/src/frontend/discord/handlers/messages.ts +1 -1
  148. package/src/frontend/discord/handlers/state.ts +1 -1
  149. package/src/frontend/discord/middleware.ts +2 -26
  150. package/src/frontend/discord/ready.ts +1 -1
  151. package/src/frontend/discord/render.ts +41 -259
  152. package/src/frontend/discord/runtime.ts +1 -2
  153. package/src/frontend/native/{auth.ts → bridge/auth.ts} +2 -2
  154. package/src/frontend/native/{discovery.ts → bridge/discovery.ts} +3 -3
  155. package/src/frontend/native/{routes → bridge/routes}/chats.ts +1 -1
  156. package/src/frontend/native/{routes → bridge/routes}/daemon.ts +1 -1
  157. package/src/frontend/native/{routes → bridge/routes}/host.ts +6 -3
  158. package/src/frontend/native/{routes → bridge/routes}/pre-auth.ts +1 -1
  159. package/src/frontend/native/{server.ts → bridge/server.ts} +3 -3
  160. package/src/frontend/native/{tls.ts → bridge/tls.ts} +2 -2
  161. package/src/frontend/native/{chat-lifecycle.ts → chats/chat-lifecycle.ts} +3 -3
  162. package/src/frontend/native/{chat-wire.ts → chats/chat-wire.ts} +4 -4
  163. package/src/frontend/native/{chats.ts → chats/chats.ts} +4 -4
  164. package/src/frontend/native/{empty-chat-sweep.ts → chats/empty-chat-sweep.ts} +4 -4
  165. package/src/frontend/native/{history.ts → chats/history.ts} +7 -7
  166. package/src/frontend/native/{reset.ts → chats/reset.ts} +7 -7
  167. package/src/frontend/native/index.ts +13 -10
  168. package/src/frontend/native/{media.ts → media/media.ts} +3 -3
  169. package/src/frontend/native/runtime.ts +1 -1
  170. package/src/frontend/native/{control.ts → surface/control.ts} +4 -4
  171. package/src/frontend/native/{extensions.ts → surface/extensions.ts} +12 -9
  172. package/src/frontend/native/{handlers.ts → surface/handlers.ts} +19 -14
  173. package/src/frontend/native/{logs.ts → surface/logs.ts} +2 -2
  174. package/src/frontend/native/{memory.ts → surface/memory.ts} +2 -2
  175. package/src/frontend/native/{models.ts → surface/models.ts} +17 -10
  176. package/src/frontend/native/{settings.ts → surface/settings.ts} +9 -9
  177. package/src/frontend/native/{status.ts → surface/status.ts} +3 -3
  178. package/src/frontend/native/{actions.ts → turn/actions.ts} +7 -4
  179. package/src/frontend/native/{context.ts → turn/context.ts} +8 -8
  180. package/src/frontend/native/{emit.ts → turn/emit.ts} +7 -7
  181. package/src/frontend/native/{queue.ts → turn/queue.ts} +3 -3
  182. package/src/frontend/native/{turn-meta.ts → turn/turn-meta.ts} +2 -2
  183. package/src/frontend/native/{turn.ts → turn/turn.ts} +9 -9
  184. package/src/frontend/{shared → presentation}/format.ts +4 -3
  185. package/src/frontend/{shared → presentation}/model-commands.ts +1 -1
  186. package/src/frontend/{shared → presentation}/reasoning-levels.ts +12 -0
  187. package/src/frontend/presentation/reports.ts +493 -0
  188. package/src/frontend/{shared → presentation}/session-status.ts +36 -9
  189. package/src/frontend/{shared → presentation}/status-context.ts +101 -2
  190. package/src/frontend/teams/commands.ts +2 -2
  191. package/src/frontend/teams/turn.ts +1 -1
  192. package/src/frontend/telegram/admin/background.ts +1 -1
  193. package/src/frontend/telegram/admin/health.ts +1 -1
  194. package/src/frontend/telegram/admin/sessions.ts +1 -1
  195. package/src/frontend/telegram/callbacks/effort.ts +2 -2
  196. package/src/frontend/telegram/callbacks/metrics.ts +1 -1
  197. package/src/frontend/telegram/callbacks/model/backend.ts +1 -1
  198. package/src/frontend/telegram/callbacks/model/views.ts +1 -1
  199. package/src/frontend/telegram/callbacks/pulse.ts +1 -1
  200. package/src/frontend/telegram/callbacks/query.ts +1 -1
  201. package/src/frontend/telegram/callbacks/settings.ts +3 -3
  202. package/src/frontend/telegram/commands/admin.ts +6 -41
  203. package/src/frontend/telegram/commands/index.ts +1 -1
  204. package/src/frontend/telegram/commands/info.ts +2 -2
  205. package/src/frontend/telegram/commands/session.ts +9 -3
  206. package/src/frontend/telegram/commands/settings.ts +5 -3
  207. package/src/frontend/telegram/handlers/messages.ts +0 -10
  208. package/src/frontend/telegram/handlers/state.ts +1 -1
  209. package/src/frontend/telegram/index.ts +1 -5
  210. package/src/frontend/telegram/middleware.ts +1 -15
  211. package/src/frontend/telegram/model-menu.ts +1 -1
  212. package/src/frontend/telegram/render/html.ts +30 -0
  213. package/src/frontend/telegram/{helpers → render}/menu.ts +46 -19
  214. package/src/frontend/telegram/render/reports.ts +182 -0
  215. package/src/frontend/terminal/builtins/context.ts +2 -2
  216. package/src/frontend/terminal/builtins/session.ts +1 -1
  217. package/src/frontend/terminal/builtins/status.ts +2 -2
  218. package/src/frontend/terminal/index.ts +2 -2
  219. package/src/frontend/terminal/renderer.ts +1 -1
  220. package/src/frontend/whatsapp/access.ts +2 -2
  221. package/src/frontend/whatsapp/actions/history.ts +2 -2
  222. package/src/frontend/whatsapp/actions/messaging.ts +2 -2
  223. package/src/frontend/whatsapp/actions/moderation.ts +1 -1
  224. package/src/frontend/whatsapp/actions/send.ts +1 -1
  225. package/src/frontend/whatsapp/commands.ts +12 -6
  226. package/src/frontend/whatsapp/{connection.ts → connection/connection.ts} +5 -5
  227. package/src/frontend/whatsapp/{pairing-service.ts → connection/pairing-service.ts} +3 -3
  228. package/src/frontend/whatsapp/{wa-logger.ts → connection/wa-logger.ts} +2 -2
  229. package/src/frontend/whatsapp/index.ts +4 -4
  230. package/src/frontend/whatsapp/{inbound.ts → messages/inbound.ts} +15 -15
  231. package/src/frontend/whatsapp/{media-store.ts → messages/media-store.ts} +4 -4
  232. package/src/frontend/whatsapp/{message-store.ts → messages/message-store.ts} +1 -1
  233. package/src/frontend/whatsapp/{turn-recovery.ts → messages/turn-recovery.ts} +4 -4
  234. package/src/frontend/whatsapp/registry.ts +2 -2
  235. package/src/frontend/whatsapp/runtime.ts +1 -1
  236. package/src/index.ts +6 -3
  237. package/src/storage/db.ts +6 -1
  238. package/src/storage/memory.ts +59 -8
  239. package/src/storage/metrics.ts +38 -0
  240. package/src/storage/repositories/sessions-repo.ts +6 -0
  241. package/src/storage/session-record.ts +13 -2
  242. package/src/storage/sessions.ts +12 -0
  243. package/src/storage/sql/db.sql +5 -0
  244. package/src/storage/sql/schema.sql +4 -0
  245. package/src/storage/sql/sessions.sql +3 -3
  246. package/src/storage/sql/statements.generated.ts +10 -3
  247. package/src/storage/sql/turn-meta.sql +1 -1
  248. package/src/util/log.ts +1 -1
  249. package/src/util/paths.ts +0 -2
  250. package/src/util/runtime.ts +1 -1
  251. package/src/core/engine/gateway-actions/native.ts +0 -1035
  252. package/src/core/soul/README.md +0 -110
  253. package/src/core/soul/RESEARCH.md +0 -98
  254. package/src/core/soul/associative.ts +0 -98
  255. package/src/core/soul/centrality.ts +0 -98
  256. package/src/core/soul/cluster.ts +0 -83
  257. package/src/core/soul/compiler.ts +0 -207
  258. package/src/core/soul/consolidate.ts +0 -179
  259. package/src/core/soul/critic.ts +0 -162
  260. package/src/core/soul/dag.ts +0 -265
  261. package/src/core/soul/delta.ts +0 -123
  262. package/src/core/soul/drift.ts +0 -99
  263. package/src/core/soul/embedder.ts +0 -129
  264. package/src/core/soul/emergent-critic.ts +0 -96
  265. package/src/core/soul/forgetting.ts +0 -131
  266. package/src/core/soul/governance.ts +0 -93
  267. package/src/core/soul/hash.ts +0 -97
  268. package/src/core/soul/hdc.ts +0 -154
  269. package/src/core/soul/kernel.ts +0 -540
  270. package/src/core/soul/lattice.ts +0 -103
  271. package/src/core/soul/lens.ts +0 -110
  272. package/src/core/soul/projector.ts +0 -240
  273. package/src/core/soul/reflect.ts +0 -170
  274. package/src/core/soul/reflex.ts +0 -164
  275. package/src/core/soul/retrieve.ts +0 -146
  276. package/src/core/soul/salience.ts +0 -146
  277. package/src/core/soul/service.ts +0 -204
  278. package/src/core/soul/settings.ts +0 -47
  279. package/src/core/soul/signals.ts +0 -117
  280. package/src/core/soul/talon-embedder.ts +0 -80
  281. package/src/core/soul/taps.ts +0 -199
  282. package/src/core/soul/types.ts +0 -298
  283. package/src/core/soul/valence.ts +0 -83
  284. package/src/frontend/telegram/helpers/diagnostics.ts +0 -428
  285. package/src/frontend/telegram/helpers/format.ts +0 -42
  286. package/src/frontend/telegram/helpers/index.ts +0 -13
  287. package/src/util/cleanup-registry.ts +0 -36
  288. /package/src/backend/{shared → runtime}/frontends.ts +0 -0
  289. /package/src/backend/{shared → runtime}/metrics.ts +0 -0
  290. /package/src/backend/{shared → runtime}/sleep.ts +0 -0
  291. /package/src/backend/{shared → runtime/turn}/flow-violation.ts +0 -0
  292. /package/src/backend/{shared → runtime}/usage.ts +0 -0
  293. /package/src/{util/mcp-launcher.ts → core/mcp-hub/launcher.ts} +0 -0
  294. /package/src/{util → core/tools/content}/web-content.ts +0 -0
  295. /package/src/core/tools/{mcp-env.ts → ops/mcp-env.ts} +0 -0
  296. /package/src/{util → core/weaver}/session-name.ts +0 -0
  297. /package/src/frontend/native/{routes → bridge/routes}/index.ts +0 -0
  298. /package/src/frontend/native/{routes → bridge/routes}/memory.ts +0 -0
  299. /package/src/frontend/native/{routes → bridge/routes}/mesh.ts +0 -0
  300. /package/src/frontend/native/{routes → bridge/routes}/models.ts +0 -0
  301. /package/src/frontend/native/{routes → bridge/routes}/params.ts +0 -0
  302. /package/src/frontend/native/{routes → bridge/routes}/table.ts +0 -0
  303. /package/src/frontend/native/{tool-result.ts → turn/tool-result.ts} +0 -0
  304. /package/src/frontend/{shared → presentation}/access.ts +0 -0
  305. /package/src/frontend/{shared → presentation}/plan-usage-report.ts +0 -0
  306. /package/src/frontend/whatsapp/{auth-state.ts → connection/auth-state.ts} +0 -0
  307. /package/src/frontend/whatsapp/{identity.ts → connection/identity.ts} +0 -0
  308. /package/src/frontend/whatsapp/{pairing-lock.ts → connection/pairing-lock.ts} +0 -0
  309. /package/src/frontend/whatsapp/{pairing.ts → connection/pairing.ts} +0 -0
  310. /package/src/frontend/whatsapp/{pins.ts → messages/pins.ts} +0 -0
@@ -1,97 +0,0 @@
1
- /**
2
- * Content addressing for the Soul kernel.
3
- *
4
- * A node's identity is the sha-256 of its *canonical* serialization. Canonical
5
- * means: deterministic key ordering, no whitespace, explicit rejection of
6
- * non-data values. Two nodes with semantically identical content therefore hash
7
- * identically — which is what makes dedup, provenance, and partial recompilation
8
- * structural rather than bolted-on.
9
- *
10
- * Only CONTENT is ever hashed here. Mutable state (salience, edge weights) is
11
- * deliberately excluded from node identity (see types.ts).
12
- */
13
-
14
- import { createHash } from "node:crypto";
15
- import type { Hash, NodePayload } from "./types.js";
16
-
17
- /**
18
- * Canonical JSON: object keys sorted lexicographically, arrays preserved in
19
- * order, no insignificant whitespace. Rejects `undefined`, functions, symbols,
20
- * NaN and ±Infinity so a hash can never depend on an unserializable value.
21
- *
22
- * This is intentionally stricter than JSON.stringify: we want a single
23
- * canonical byte string per logical value, and we want loud failure on anything
24
- * that could silently vary.
25
- */
26
- export function canonicalize(value: unknown): string {
27
- return encode(value);
28
- }
29
-
30
- function encode(value: unknown): string {
31
- if (value === null) return "null";
32
-
33
- const t = typeof value;
34
-
35
- if (t === "string") return JSON.stringify(value);
36
-
37
- if (t === "number") {
38
- if (!Number.isFinite(value as number)) {
39
- throw new TypeError(`canonicalize: non-finite number ${String(value)}`);
40
- }
41
- return JSON.stringify(value);
42
- }
43
-
44
- if (t === "boolean") return value ? "true" : "false";
45
-
46
- if (t === "bigint") {
47
- throw new TypeError("canonicalize: bigint is not serializable");
48
- }
49
-
50
- if (Array.isArray(value)) {
51
- return `[${value.map(encode).join(",")}]`;
52
- }
53
-
54
- if (t === "object") {
55
- const obj = value as Record<string, unknown>;
56
- const keys = Object.keys(obj).sort();
57
- const parts: string[] = [];
58
- for (const key of keys) {
59
- const v = obj[key];
60
- // Drop keys whose value is undefined, mirroring JSON semantics, but only
61
- // for objects — undefined inside arrays is rejected below.
62
- if (v === undefined) continue;
63
- parts.push(`${JSON.stringify(key)}:${encode(v)}`);
64
- }
65
- return `{${parts.join(",")}}`;
66
- }
67
-
68
- // undefined, function, symbol
69
- throw new TypeError(`canonicalize: cannot serialize ${t}`);
70
- }
71
-
72
- /** sha-256 of an arbitrary canonicalized value, prefixed "sha256:". */
73
- export function hashContent(value: unknown): Hash {
74
- const canonical = canonicalize(value);
75
- const digest = createHash("sha256").update(canonical, "utf8").digest("hex");
76
- return `sha256:${digest}` as Hash;
77
- }
78
-
79
- /** The content address of a node payload. This is the node's identity. */
80
- export function hashPayload(payload: NodePayload): Hash {
81
- return hashContent(payload);
82
- }
83
-
84
- /**
85
- * A Merkle root over a set of child hashes. Order-independent: the children are
86
- * sorted before hashing, so the root depends only on the *set* of content
87
- * present, not the order it was inserted. This is the structural version id.
88
- */
89
- export function merkleRoot(childHashes: Iterable<Hash>): Hash {
90
- const sorted = [...childHashes].sort();
91
- return hashContent({ merkle: sorted });
92
- }
93
-
94
- /** True when a string is a well-formed kernel content address. */
95
- export function isHash(value: unknown): value is Hash {
96
- return typeof value === "string" && /^sha256:[0-9a-f]{64}$/.test(value);
97
- }
@@ -1,154 +0,0 @@
1
- /**
2
- * Soul Kernel — hyperdimensional computing (Vector-Symbolic Architecture).
3
- *
4
- * Kanerva, "Hyperdimensional Computing" (2009). High-dimensional bipolar vectors
5
- * (±1, here a few thousand dims) support a small algebra that is the basis of a
6
- * compositional, model-free associative memory:
7
- *
8
- * - bind (⊗) elementwise product. Combines two hypervectors into one
9
- * DISSIMILAR to both, and is its own inverse (bind twice by the
10
- * same key recovers the other operand). Used to pair a role with a
11
- * filler: context ⊗ value.
12
- * - bundle (+) elementwise majority. Superposes many hypervectors into one
13
- * SIMILAR to all of them — a set, or a memory of many episodes.
14
- * - permute (ρ) cyclic shift. Encodes order/role without collision.
15
- * - cleanup nearest stored item under cosine — denoises a recalled vector
16
- * back to a known symbol.
17
- *
18
- * The soul uses this for episodic recall: bind the current context to the value
19
- * that worked, bundle those bindings into one memory hypervector, and later query
20
- * "in a context like this, what do I do?" by unbinding and cleaning up. It is a
21
- * genuinely compositional memory, and it is pure integer arithmetic — no model.
22
- */
23
-
24
- import { createHash } from "node:crypto";
25
-
26
- export type Hypervector = Int8Array;
27
-
28
- const DEFAULT_HD_DIM = 4096;
29
-
30
- /** Deterministic ±1 hypervector for a symbol — the same token always maps here. */
31
- export function symbolVector(token: string, dim = DEFAULT_HD_DIM): Hypervector {
32
- const v = new Int8Array(dim);
33
- // Seed a tiny xorshift PRNG from the token hash; fill with ±1.
34
- const digest = createHash("sha256").update(token).digest();
35
- let s0 = digest.readUInt32LE(0) || 1;
36
- let s1 = digest.readUInt32LE(4) || 2;
37
- for (let i = 0; i < dim; i++) {
38
- // xorshift128-ish
39
- let x = s0;
40
- const y = s1;
41
- s0 = y;
42
- x ^= x << 23;
43
- x ^= x >>> 17;
44
- x ^= y ^ (y >>> 26);
45
- s1 = x >>> 0;
46
- v[i] = ((s1 >>> (i % 31)) & 1) === 0 ? 1 : -1;
47
- }
48
- return v;
49
- }
50
-
51
- /** Elementwise bind (product). Self-inverse for bipolar vectors. */
52
- export function bind(a: Hypervector, b: Hypervector): Hypervector {
53
- const out = new Int8Array(a.length);
54
- for (let i = 0; i < a.length; i++) out[i] = (a[i]! * b[i]!) as number;
55
- return out;
56
- }
57
-
58
- /** Majority-rule bundle of several hypervectors; empty ⇒ zero vector. */
59
- export function bundle(vectors: readonly Hypervector[]): Hypervector {
60
- const dim = vectors[0]?.length ?? DEFAULT_HD_DIM;
61
- const acc = new Int32Array(dim);
62
- for (const v of vectors) for (let i = 0; i < dim; i++) acc[i]! += v[i]!;
63
- const out = new Int8Array(dim);
64
- for (let i = 0; i < dim; i++) out[i] = acc[i]! >= 0 ? 1 : -1; // ties → +1
65
- return out;
66
- }
67
-
68
- /** Cyclic shift by k (encodes role/order). */
69
- export function permute(v: Hypervector, k = 1): Hypervector {
70
- const dim = v.length;
71
- const out = new Int8Array(dim);
72
- const shift = ((k % dim) + dim) % dim;
73
- for (let i = 0; i < dim; i++) out[(i + shift) % dim] = v[i]!;
74
- return out;
75
- }
76
-
77
- /** Cosine similarity of two bipolar vectors (= normalized dot). */
78
- export function hdCosine(a: Hypervector, b: Hypervector): number {
79
- let dot = 0;
80
- for (let i = 0; i < a.length; i++) dot += a[i]! * b[i]!;
81
- return dot / a.length;
82
- }
83
-
84
- /** Nearest stored item to a (noisy) query, by cosine. */
85
- export function cleanup(
86
- query: Hypervector,
87
- items: ReadonlyMap<string, Hypervector>,
88
- ): { token: string; score: number } | undefined {
89
- let best: { token: string; score: number } | undefined;
90
- for (const [token, v] of items) {
91
- const score = hdCosine(query, v);
92
- if (!best || score > best.score) best = { token, score };
93
- }
94
- return best;
95
- }
96
-
97
- /**
98
- * A compositional associative memory: a running superposition of role⊗filler
99
- * bindings. Querying with a role unbinds and returns a noisy filler to be cleaned
100
- * up against an item memory. Stored as an integer accumulator so many episodes
101
- * can be bundled without saturating.
102
- */
103
- export class CompositionalMemory {
104
- private readonly acc: Int32Array;
105
- private count = 0;
106
-
107
- constructor(
108
- readonly dim = DEFAULT_HD_DIM,
109
- acc?: Int32Array,
110
- count = 0,
111
- ) {
112
- this.acc = acc ?? new Int32Array(dim);
113
- this.count = count;
114
- }
115
-
116
- /** Superpose one role⊗filler binding into the memory. */
117
- add(role: Hypervector, filler: Hypervector): void {
118
- const bound = bind(role, filler);
119
- for (let i = 0; i < this.dim; i++) this.acc[i]! += bound[i]!;
120
- this.count += 1;
121
- }
122
-
123
- /** The signed memory hypervector. */
124
- vector(): Hypervector {
125
- const out = new Int8Array(this.dim);
126
- for (let i = 0; i < this.dim; i++) out[i] = this.acc[i]! >= 0 ? 1 : -1;
127
- return out;
128
- }
129
-
130
- /** Unbind a role to recover the (noisy) filler it was paired with. */
131
- query(role: Hypervector): Hypervector {
132
- return bind(this.vector(), role);
133
- }
134
-
135
- get episodes(): number {
136
- return this.count;
137
- }
138
-
139
- snapshot(): { dim: number; acc: number[]; count: number } {
140
- return { dim: this.dim, acc: Array.from(this.acc), count: this.count };
141
- }
142
-
143
- static restore(snap: {
144
- dim: number;
145
- acc: number[];
146
- count: number;
147
- }): CompositionalMemory {
148
- return new CompositionalMemory(
149
- snap.dim,
150
- Int32Array.from(snap.acc),
151
- snap.count,
152
- );
153
- }
154
- }