counterparts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/CONSTITUTION.md +68 -0
  2. package/LICENSE +21 -0
  3. package/README.md +348 -0
  4. package/docs/ELI5.md +84 -0
  5. package/docs/QUICKSTART.md +1258 -0
  6. package/docs/module-map.md +81 -0
  7. package/package.json +63 -0
  8. package/src/adapters/claude-code/CONTRACT.md +606 -0
  9. package/src/adapters/claude-code/INTERFACE-GAPS.md +655 -0
  10. package/src/adapters/claude-code/NOTES.md +1055 -0
  11. package/src/adapters/claude-code/bin/hook.ts +820 -0
  12. package/src/adapters/claude-code/bin/runner.ts +522 -0
  13. package/src/adapters/claude-code/config.ts +778 -0
  14. package/src/adapters/claude-code/credentials.ts +199 -0
  15. package/src/adapters/claude-code/doctor.ts +2557 -0
  16. package/src/adapters/claude-code/embed-client.ts +617 -0
  17. package/src/adapters/claude-code/hooks.ts +2240 -0
  18. package/src/adapters/claude-code/index.ts +224 -0
  19. package/src/adapters/claude-code/interpret-client.ts +395 -0
  20. package/src/adapters/claude-code/page-writer.ts +462 -0
  21. package/src/adapters/claude-code/primacy.ts +162 -0
  22. package/src/adapters/claude-code/spawn.ts +241 -0
  23. package/src/adapters/claude-code/standdown.ts +536 -0
  24. package/src/adapters/claude-code/transcript.ts +661 -0
  25. package/src/adapters/claude-code/vectors.ts +415 -0
  26. package/src/adapters/cli/CONTRACT.md +255 -0
  27. package/src/adapters/cli/INTERFACE-GAPS.md +350 -0
  28. package/src/adapters/cli/NOTES.md +936 -0
  29. package/src/adapters/cli/bin/counterparts.ts +74 -0
  30. package/src/adapters/cli/commands.ts +6095 -0
  31. package/src/adapters/cli/export.ts +766 -0
  32. package/src/adapters/cli/index.ts +103 -0
  33. package/src/adapters/cli/install.ts +670 -0
  34. package/src/adapters/cli/removal.ts +1186 -0
  35. package/src/adapters/cli/repair-dates.ts +659 -0
  36. package/src/adapters/cli/self-page.ts +229 -0
  37. package/src/adapters/cli/snapshot.ts +279 -0
  38. package/src/adapters/cli/start-fresh.ts +1228 -0
  39. package/src/adapters/config-path.ts +342 -0
  40. package/src/adapters/dashboard/CONTRACT.md +67 -0
  41. package/src/adapters/dashboard/INTERFACE-GAPS.md +139 -0
  42. package/src/adapters/dashboard/NOTES.md +474 -0
  43. package/src/adapters/dashboard/activity.ts +121 -0
  44. package/src/adapters/dashboard/ansi.ts +82 -0
  45. package/src/adapters/dashboard/bin/dashboard.ts +479 -0
  46. package/src/adapters/dashboard/browse.ts +298 -0
  47. package/src/adapters/dashboard/identity.ts +182 -0
  48. package/src/adapters/dashboard/index.ts +183 -0
  49. package/src/adapters/dashboard/layout.ts +149 -0
  50. package/src/adapters/dashboard/registries.ts +291 -0
  51. package/src/adapters/dashboard/resolve.ts +206 -0
  52. package/src/adapters/dashboard/source.ts +48 -0
  53. package/src/adapters/dashboard/status.ts +327 -0
  54. package/src/adapters/dashboard/stories.ts +194 -0
  55. package/src/adapters/dashboard/web/app.html +1731 -0
  56. package/src/adapters/dashboard/web/brain.html +463 -0
  57. package/src/adapters/dashboard/web/fired.ts +31 -0
  58. package/src/adapters/dashboard/web/flow.ts +457 -0
  59. package/src/adapters/dashboard/web/narrate.ts +1042 -0
  60. package/src/adapters/dashboard/web/reveal.ts +135 -0
  61. package/src/adapters/dashboard/web/server.ts +287 -0
  62. package/src/adapters/dashboard/web/views.ts +1744 -0
  63. package/src/adapters/expansions.ts +569 -0
  64. package/src/adapters/fired.ts +1580 -0
  65. package/src/adapters/mcp/CONTRACT.md +240 -0
  66. package/src/adapters/mcp/INTERFACE-GAPS.md +271 -0
  67. package/src/adapters/mcp/NOTES.md +205 -0
  68. package/src/adapters/mcp/bin/serve.ts +349 -0
  69. package/src/adapters/mcp/deliberate.ts +619 -0
  70. package/src/adapters/mcp/index.ts +101 -0
  71. package/src/adapters/mcp/protocol.ts +170 -0
  72. package/src/adapters/mcp/server.ts +1740 -0
  73. package/src/adapters/mcp/stdio.ts +56 -0
  74. package/src/adapters/mcp/tools.ts +779 -0
  75. package/src/adapters/scopes.ts +657 -0
  76. package/src/adapters/sessions.ts +402 -0
  77. package/src/adapters/snapshots.ts +1069 -0
  78. package/src/adapters/stance-env.ts +137 -0
  79. package/src/core/associate/CONTRACT.md +118 -0
  80. package/src/core/associate/INTERFACE-GAPS.md +173 -0
  81. package/src/core/associate/NOTES.md +194 -0
  82. package/src/core/associate/buffer.ts +66 -0
  83. package/src/core/associate/edges.ts +188 -0
  84. package/src/core/associate/index.ts +570 -0
  85. package/src/core/associate/pending.ts +383 -0
  86. package/src/core/associate/spread.ts +149 -0
  87. package/src/core/associate/tunables.ts +123 -0
  88. package/src/core/bridge.ts +272 -0
  89. package/src/core/briefing.ts +67 -0
  90. package/src/core/counterpart.ts +3855 -0
  91. package/src/core/encode/CONTRACT.md +151 -0
  92. package/src/core/encode/INTERFACE-GAPS.md +135 -0
  93. package/src/core/encode/NOTES.md +146 -0
  94. package/src/core/encode/aliases.ts +71 -0
  95. package/src/core/encode/battery.ts +284 -0
  96. package/src/core/encode/chunk.ts +265 -0
  97. package/src/core/encode/emotion.ts +119 -0
  98. package/src/core/encode/floor.ts +76 -0
  99. package/src/core/encode/index.ts +100 -0
  100. package/src/core/encode/precision.ts +162 -0
  101. package/src/core/encode/preselect.ts +269 -0
  102. package/src/core/encode/salience.ts +108 -0
  103. package/src/core/encode/secrets.ts +265 -0
  104. package/src/core/encode/tunables.ts +99 -0
  105. package/src/core/encode/types.ts +286 -0
  106. package/src/core/encode/words.ts +75 -0
  107. package/src/core/handoff/CONTRACT.md +199 -0
  108. package/src/core/handoff/INTERFACE-GAPS.md +166 -0
  109. package/src/core/handoff/NOTES.md +206 -0
  110. package/src/core/handoff/index.ts +957 -0
  111. package/src/core/mint.ts +279 -0
  112. package/src/core/observer.ts +28 -0
  113. package/src/core/physics/CONTRACT.md +341 -0
  114. package/src/core/physics/NOTES.md +155 -0
  115. package/src/core/physics/clock.ts +51 -0
  116. package/src/core/physics/index.ts +1054 -0
  117. package/src/core/prospective/CONTRACT.md +137 -0
  118. package/src/core/prospective/INTERFACE-GAPS.md +112 -0
  119. package/src/core/prospective/NOTES.md +160 -0
  120. package/src/core/prospective/derive.ts +188 -0
  121. package/src/core/prospective/index.ts +1030 -0
  122. package/src/core/prospective/tunables.ts +72 -0
  123. package/src/core/prospective/windows.ts +187 -0
  124. package/src/core/recall/CONTRACT.md +326 -0
  125. package/src/core/recall/INTERFACE-GAPS.md +163 -0
  126. package/src/core/recall/NOTES.md +472 -0
  127. package/src/core/recall/activate.ts +521 -0
  128. package/src/core/recall/cues.ts +275 -0
  129. package/src/core/recall/gate.ts +441 -0
  130. package/src/core/recall/index.ts +692 -0
  131. package/src/core/recall/probe.ts +239 -0
  132. package/src/core/recall/reference.ts +201 -0
  133. package/src/core/recall/render.ts +285 -0
  134. package/src/core/recall/session.ts +403 -0
  135. package/src/core/recall/tunables.ts +329 -0
  136. package/src/core/remember/CONTRACT.md +293 -0
  137. package/src/core/remember/INTERFACE-GAPS.md +189 -0
  138. package/src/core/remember/NOTES.md +373 -0
  139. package/src/core/remember/fallback.ts +452 -0
  140. package/src/core/remember/index.ts +103 -0
  141. package/src/core/remember/owner-strike-seam.ts +479 -0
  142. package/src/core/remember/proposals.ts +609 -0
  143. package/src/core/remember/spans.ts +1310 -0
  144. package/src/core/remember/tunables.ts +111 -0
  145. package/src/core/remember/updates.ts +198 -0
  146. package/src/core/retrieval.ts +96 -0
  147. package/src/core/revision.ts +441 -0
  148. package/src/core/schemas/CONTRACT.md +190 -0
  149. package/src/core/schemas/INTERFACE-GAPS.md +172 -0
  150. package/src/core/schemas/NOTES.md +260 -0
  151. package/src/core/schemas/aliases.ts +160 -0
  152. package/src/core/schemas/index.ts +1459 -0
  153. package/src/core/schemas/tunables.ts +44 -0
  154. package/src/core/schemas/types.ts +387 -0
  155. package/src/core/self/CONTRACT.md +459 -0
  156. package/src/core/self/INTERFACE-GAPS.md +284 -0
  157. package/src/core/self/NOTES.md +1057 -0
  158. package/src/core/self/briefing.ts +980 -0
  159. package/src/core/self/episodes.ts +483 -0
  160. package/src/core/self/freeze.ts +103 -0
  161. package/src/core/self/identity.ts +604 -0
  162. package/src/core/self/index.ts +2130 -0
  163. package/src/core/self/journal-file.ts +760 -0
  164. package/src/core/self/page.ts +404 -0
  165. package/src/core/self/tunables.ts +244 -0
  166. package/src/core/self/writer.ts +714 -0
  167. package/src/core/sleep/CONTRACT.md +298 -0
  168. package/src/core/sleep/INTERFACE-GAPS.md +262 -0
  169. package/src/core/sleep/NOTES.md +770 -0
  170. package/src/core/sleep/briefing.ts +86 -0
  171. package/src/core/sleep/consolidate.ts +241 -0
  172. package/src/core/sleep/cycle.ts +598 -0
  173. package/src/core/sleep/decay.ts +237 -0
  174. package/src/core/sleep/dedup.ts +373 -0
  175. package/src/core/sleep/index.ts +112 -0
  176. package/src/core/sleep/log.ts +116 -0
  177. package/src/core/sleep/markers.ts +164 -0
  178. package/src/core/sleep/prune.ts +170 -0
  179. package/src/core/sleep/strength-cache.ts +176 -0
  180. package/src/core/sleep/tunables.ts +135 -0
  181. package/src/core/sleep/types.ts +542 -0
  182. package/src/core/store/CONTRACT.md +446 -0
  183. package/src/core/store/NOTES.md +1068 -0
  184. package/src/core/store/cache.ts +709 -0
  185. package/src/core/store/db.ts +275 -0
  186. package/src/core/store/errors.ts +134 -0
  187. package/src/core/store/index.ts +2450 -0
  188. package/src/core/store/operational.ts +610 -0
  189. package/src/core/store/owner-op-seam.ts +428 -0
  190. package/src/core/store/paths.ts +494 -0
  191. package/src/core/store/prose.ts +165 -0
  192. package/src/core/store/render.ts +87 -0
  193. package/src/core/store/walk-seam.ts +78 -0
  194. package/src/core/types.ts +68 -0
  195. package/tools/install-loop/run.sh +1063 -0
@@ -0,0 +1,101 @@
1
+ /**
2
+ * `adapters/mcp/` — the deliberate tools, over the wire.
3
+ *
4
+ * The dependency direction is one-way and asserted by a test: the core imports
5
+ * NOTHING from this directory (CONTRACT §5 G9, constitution line 5). Everything
6
+ * exported here is host-shaped; everything imported from `../../core/` is
7
+ * host-agnostic.
8
+ */
9
+ export {
10
+ ERROR_CODES,
11
+ FrameReader,
12
+ JSONRPC_VERSION,
13
+ LATEST_PROTOCOL_VERSION,
14
+ PROTOCOL_VERSIONS,
15
+ encodeMessage,
16
+ failure,
17
+ isNotification,
18
+ negotiateVersion,
19
+ parseLine,
20
+ success,
21
+ } from "./protocol.js";
22
+ export type { ErrorResponse, Id, ParsedLine, Request, Response, SuccessResponse } from "./protocol.js";
23
+
24
+ export { TOOLS, TOOL_NAMES, renderDescription, toolDefinitions, toolSpec } from "./tools.js";
25
+ export type { Privilege, ToolName, ToolSpec } from "./tools.js";
26
+
27
+ export {
28
+ DELIBERATE_DIM_CAP,
29
+ DELIBERATE_TIERS,
30
+ HARD_GATES,
31
+ RECALL_BODY_CHARS,
32
+ RECALL_EXCERPT_CHARS,
33
+ RECALL_MAX_IDS,
34
+ RECALL_RESULT_CHARS,
35
+ answerQuestion,
36
+ boundMemories,
37
+ deliberateRecall,
38
+ expandHandle,
39
+ expandIds,
40
+ tierOf,
41
+ } from "./deliberate.js";
42
+ export type {
43
+ BoundedMemory,
44
+ BoundedResult,
45
+ DeliberateInput,
46
+ DeliberateOptions,
47
+ DeliberateReason,
48
+ DeliberateResult,
49
+ Recalled,
50
+ Tier,
51
+ } from "./deliberate.js";
52
+
53
+ export { McpServer, SERVER_NAME, SERVER_VERSION, hostScope, resolveScope } from "./server.js";
54
+ export type {
55
+ McpEvent,
56
+ McpServerOptions,
57
+ QuestionEmbedder,
58
+ ScopeSource,
59
+ ToolResult,
60
+ } from "./server.js";
61
+
62
+ export { serveStdio } from "./stdio.js";
63
+ export type { StdioOptions } from "./stdio.js";
64
+
65
+ import { Counterpart } from "../../core/counterpart.js";
66
+ import { McpServer } from "./server.js";
67
+ import type { McpServerOptions } from "./server.js";
68
+
69
+ export interface OpenServerOptions extends Omit<McpServerOptions, "counterpart"> {
70
+ /** Where the memory lives. Defaults to the store's own resolution. */
71
+ dir?: string;
72
+ observer?: boolean;
73
+ }
74
+
75
+ /**
76
+ * Open a brain and wrap it in the tools. The host facts — the data dir, whose
77
+ * session this is, which session id the deposits belong to — travel as
78
+ * ARGUMENTS, never as ambient state this file reads for itself (scar §2.13).
79
+ */
80
+ export function openServer(opts: OpenServerOptions = {}): McpServer {
81
+ const counterpart = Counterpart.open({
82
+ ...(opts.dir === undefined ? {} : { dir: opts.dir }),
83
+ ...(opts.observer === undefined ? {} : { observer: opts.observer }),
84
+ ...(opts.owner === undefined ? {} : { owner: opts.owner }),
85
+ });
86
+ return new McpServer({
87
+ counterpart,
88
+ ...(opts.session === undefined ? {} : { session: opts.session }),
89
+ ...(opts.scope === undefined ? {} : { scope: opts.scope }),
90
+ ...(opts.owner === undefined ? {} : { owner: opts.owner }),
91
+ // The embedder is the ENTRY POINT's to open, because the credential is:
92
+ // this file composes, it does not read a key (server.ts's header).
93
+ ...(opts.embedder === undefined ? {} : { embedder: opts.embedder }),
94
+ ...(opts.registryDir === undefined ? {} : { registryDir: opts.registryDir }),
95
+ ...(opts.scopesFile === undefined ? {} : { scopesFile: opts.scopesFile }),
96
+ ...(opts.sessionTtlMs === undefined ? {} : { sessionTtlMs: opts.sessionTtlMs }),
97
+ ...(opts.onEvent === undefined ? {} : { onEvent: opts.onEvent }),
98
+ ...(opts.now === undefined ? {} : { now: opts.now }),
99
+ ...(opts.env === undefined ? {} : { env: opts.env }),
100
+ });
101
+ }
@@ -0,0 +1,170 @@
1
+ /**
2
+ * JSON-RPC 2.0 over stdio — the wire, and nothing else.
3
+ *
4
+ * CONTRACT §5 G8: **wire-protocol framing is host trivia.** This file is where
5
+ * that triviality is allowed to live, and it is deliberately the only file in
6
+ * this directory that knows what a `jsonrpc` field is. `server.ts` handles
7
+ * *messages*; `tools.ts` describes *tools*; neither of them can be broken by a
8
+ * transport change.
9
+ *
10
+ * Hand-rolled, no SDK (constitution line 10, and the zero-dep default). The
11
+ * stdio transport MCP specifies is newline-delimited JSON — one complete JSON
12
+ * object per line, no Content-Length headers (that is LSP, a different wire).
13
+ * A message may arrive split across chunk boundaries, so framing is a small
14
+ * state machine rather than a `split("\n")` per chunk.
15
+ *
16
+ * Two rules that are behaviour, not framing, and are therefore stated here once:
17
+ *
18
+ * 1. A **notification** (no `id`) is answered with nothing, ever. A server
19
+ * that replies to a notification desynchronizes a client that is not
20
+ * reading for one.
21
+ * 2. **JSON-RPC errors are for protocol faults** — bad JSON, unknown method,
22
+ * malformed params. A tool that ran and refused is a SUCCESSFUL response
23
+ * carrying `isError: true`, because the refusal is the tool's answer and
24
+ * the model is supposed to read it (scar §2.4: a stood-down tool must be
25
+ * distinguishable from a broken one — that only works if the stand-down
26
+ * arrives as content the model can read).
27
+ */
28
+
29
+ export const JSONRPC_VERSION = "2.0";
30
+
31
+ /** JSON-RPC's own codes. Nothing memory-shaped belongs in this list. */
32
+ export const ERROR_CODES = {
33
+ PARSE_ERROR: -32700,
34
+ INVALID_REQUEST: -32600,
35
+ METHOD_NOT_FOUND: -32601,
36
+ INVALID_PARAMS: -32602,
37
+ INTERNAL_ERROR: -32603,
38
+ } as const;
39
+
40
+ /**
41
+ * Protocol revisions this server knows how to speak, newest first. Version
42
+ * negotiation ECHOES a version the client asked for when we support it, and
43
+ * answers with our newest when we do not — never a hard failure on an unknown
44
+ * string, which would make a newer client unable to talk to an older server for
45
+ * no reason (the client decides whether our answer is acceptable).
46
+ */
47
+ export const PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"] as const;
48
+ export const LATEST_PROTOCOL_VERSION = PROTOCOL_VERSIONS[0];
49
+
50
+ export type Id = string | number;
51
+
52
+ export interface Request {
53
+ jsonrpc: string;
54
+ /** Absent ⇒ a notification: answered with nothing. */
55
+ id?: Id;
56
+ method: string;
57
+ params?: Record<string, unknown>;
58
+ }
59
+
60
+ export interface SuccessResponse {
61
+ jsonrpc: string;
62
+ id: Id;
63
+ result: Record<string, unknown>;
64
+ }
65
+
66
+ export interface ErrorResponse {
67
+ jsonrpc: string;
68
+ id: Id | null;
69
+ error: { code: number; message: string; data?: Record<string, unknown> };
70
+ }
71
+
72
+ export type Response = SuccessResponse | ErrorResponse;
73
+
74
+ export function negotiateVersion(requested: unknown): string {
75
+ if (typeof requested === "string" && (PROTOCOL_VERSIONS as readonly string[]).includes(requested)) {
76
+ return requested;
77
+ }
78
+ return LATEST_PROTOCOL_VERSION;
79
+ }
80
+
81
+ export function success(id: Id, result: Record<string, unknown>): SuccessResponse {
82
+ return { jsonrpc: JSONRPC_VERSION, id, result };
83
+ }
84
+
85
+ export function failure(
86
+ id: Id | null,
87
+ code: number,
88
+ message: string,
89
+ data?: Record<string, unknown>,
90
+ ): ErrorResponse {
91
+ return {
92
+ jsonrpc: JSONRPC_VERSION,
93
+ id,
94
+ error: { code, message, ...(data === undefined ? {} : { data }) },
95
+ };
96
+ }
97
+
98
+ /** One message, one line. The trailing newline IS the frame. */
99
+ export function encodeMessage(message: Response | Request): string {
100
+ return `${JSON.stringify(message)}\n`;
101
+ }
102
+
103
+ export type ParsedLine =
104
+ | { ok: true; request: Request }
105
+ | { ok: false; code: number; message: string; id: Id | null };
106
+
107
+ /**
108
+ * Parse one framed line into a request. Structural validation only: whether the
109
+ * METHOD exists and whether its params make sense is the dispatcher's business.
110
+ */
111
+ export function parseLine(line: string): ParsedLine {
112
+ let raw: unknown;
113
+ try {
114
+ raw = JSON.parse(line);
115
+ } catch {
116
+ return { ok: false, code: ERROR_CODES.PARSE_ERROR, message: "invalid JSON", id: null };
117
+ }
118
+ if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
119
+ return { ok: false, code: ERROR_CODES.INVALID_REQUEST, message: "not a JSON-RPC object", id: null };
120
+ }
121
+ const rec = raw as Record<string, unknown>;
122
+ const id =
123
+ typeof rec["id"] === "string" || typeof rec["id"] === "number" ? (rec["id"] as Id) : null;
124
+ if (rec["jsonrpc"] !== JSONRPC_VERSION) {
125
+ return { ok: false, code: ERROR_CODES.INVALID_REQUEST, message: "jsonrpc must be \"2.0\"", id };
126
+ }
127
+ if (typeof rec["method"] !== "string" || rec["method"].length === 0) {
128
+ return { ok: false, code: ERROR_CODES.INVALID_REQUEST, message: "method must be a string", id };
129
+ }
130
+ const params = rec["params"];
131
+ if (params !== undefined && (params === null || typeof params !== "object" || Array.isArray(params))) {
132
+ return { ok: false, code: ERROR_CODES.INVALID_PARAMS, message: "params must be an object", id };
133
+ }
134
+ const request: Request = { jsonrpc: JSONRPC_VERSION, method: rec["method"] };
135
+ if (id !== null) request.id = id;
136
+ if (params !== undefined) request.params = params as Record<string, unknown>;
137
+ return { ok: true, request };
138
+ }
139
+
140
+ /** True for a message that must be answered with nothing at all. */
141
+ export function isNotification(request: Request): boolean {
142
+ return request.id === undefined;
143
+ }
144
+
145
+ /**
146
+ * The framing state machine: bytes in, complete lines out. A partial line is
147
+ * held until its newline arrives; an empty line is not a message and is
148
+ * dropped rather than reported as a parse error.
149
+ */
150
+ export class FrameReader {
151
+ private buffer = "";
152
+
153
+ feed(chunk: string): string[] {
154
+ this.buffer += chunk;
155
+ const out: string[] = [];
156
+ for (;;) {
157
+ const nl = this.buffer.indexOf("\n");
158
+ if (nl < 0) break;
159
+ const line = this.buffer.slice(0, nl).trim();
160
+ this.buffer = this.buffer.slice(nl + 1);
161
+ if (line.length > 0) out.push(line);
162
+ }
163
+ return out;
164
+ }
165
+
166
+ /** Bytes held back awaiting a newline. Non-zero at EOF means a truncated write. */
167
+ pending(): number {
168
+ return this.buffer.length;
169
+ }
170
+ }