counterparts 0.2.0 → 0.3.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 (96) hide show
  1. package/README.md +87 -367
  2. package/docs/QUICKSTART.md +83 -1584
  3. package/docs/README.md +86 -0
  4. package/docs/module-map.md +1 -0
  5. package/package.json +5 -2
  6. package/src/adapters/claude-code/CONTRACT.md +151 -18
  7. package/src/adapters/claude-code/INTERFACE-GAPS.md +138 -2
  8. package/src/adapters/claude-code/NOTES.md +452 -0
  9. package/src/adapters/claude-code/bin/hook.ts +221 -49
  10. package/src/adapters/claude-code/bin/runner.ts +220 -101
  11. package/src/adapters/claude-code/config.ts +202 -279
  12. package/src/adapters/claude-code/doctor.ts +713 -421
  13. package/src/adapters/claude-code/embed-client.ts +165 -557
  14. package/src/adapters/claude-code/hooks.ts +409 -116
  15. package/src/adapters/claude-code/index.ts +7 -59
  16. package/src/adapters/claude-code/page-writer.ts +24 -4
  17. package/src/adapters/claude-code/spawn.ts +6 -7
  18. package/src/adapters/claude-code/transcript.ts +204 -10
  19. package/src/adapters/claude-code/vectors.ts +108 -24
  20. package/src/adapters/cli/CONTRACT.md +123 -3
  21. package/src/adapters/cli/INTERFACE-GAPS.md +45 -2
  22. package/src/adapters/cli/NOTES.md +265 -1
  23. package/src/adapters/cli/bin/counterparts.ts +9 -9
  24. package/src/adapters/cli/commands.ts +494 -436
  25. package/src/adapters/cli/help.ts +22 -27
  26. package/src/adapters/cli/index.ts +0 -2
  27. package/src/adapters/cli/install.ts +102 -89
  28. package/src/adapters/cli/removal.ts +1 -1
  29. package/src/adapters/cli/report.ts +25 -2
  30. package/src/adapters/cli/uninstall.ts +182 -34
  31. package/src/adapters/cli/wire.ts +232 -35
  32. package/src/adapters/dashboard/INTERFACE-GAPS.md +14 -2
  33. package/src/adapters/dashboard/activity.ts +4 -1
  34. package/src/adapters/dashboard/bin/dashboard.ts +1 -1
  35. package/src/adapters/dashboard/registries.ts +16 -1
  36. package/src/adapters/dashboard/resolve.ts +4 -4
  37. package/src/adapters/dashboard/source.ts +8 -2
  38. package/src/adapters/dashboard/web/flow.ts +6 -0
  39. package/src/adapters/dashboard/web/narrate.ts +66 -14
  40. package/src/adapters/dashboard/web/reveal.ts +6 -6
  41. package/src/adapters/dashboard/web/views.ts +12 -4
  42. package/src/adapters/fired.ts +20 -5
  43. package/src/adapters/mcp/CONTRACT.md +73 -1
  44. package/src/adapters/mcp/INTERFACE-GAPS.md +51 -1
  45. package/src/adapters/mcp/NOTES.md +389 -0
  46. package/src/adapters/mcp/bin/serve.ts +24 -35
  47. package/src/adapters/mcp/deliberate.ts +22 -0
  48. package/src/adapters/mcp/index.ts +26 -2
  49. package/src/adapters/mcp/server.ts +425 -49
  50. package/src/adapters/mcp/tools.ts +32 -7
  51. package/src/adapters/mcp/write-up.ts +454 -0
  52. package/src/adapters/sessions.ts +1353 -0
  53. package/src/core/counterpart.ts +32 -8
  54. package/src/core/embed/CONTRACT.md +85 -0
  55. package/src/core/embed/NOTES.md +93 -0
  56. package/src/core/embed/static.ts +622 -0
  57. package/src/core/encode/INTERFACE-GAPS.md +17 -0
  58. package/src/core/encode/NOTES.md +78 -0
  59. package/src/core/encode/secrets.ts +309 -9
  60. package/src/core/handoff/INTERFACE-GAPS.md +13 -1
  61. package/src/core/recall/CONTRACT.md +15 -3
  62. package/src/core/recall/INTERFACE-GAPS.md +94 -0
  63. package/src/core/recall/NOTES.md +49 -0
  64. package/src/core/recall/activate.ts +37 -3
  65. package/src/core/recall/index.ts +22 -1
  66. package/src/core/recall/session.ts +32 -0
  67. package/src/core/recall/tunables.ts +99 -2
  68. package/src/core/remember/CONTRACT.md +51 -7
  69. package/src/core/remember/INTERFACE-GAPS.md +111 -1
  70. package/src/core/remember/NOTES.md +221 -1
  71. package/src/core/remember/index.ts +28 -0
  72. package/src/core/remember/owes.ts +532 -0
  73. package/src/core/remember/owner-strike-seam.ts +34 -2
  74. package/src/core/remember/proposals.ts +25 -6
  75. package/src/core/remember/retention.ts +154 -0
  76. package/src/core/remember/spans.ts +53 -12
  77. package/src/core/remember/tunables.ts +8 -0
  78. package/src/core/remember/write-up-seam.ts +104 -0
  79. package/src/core/self/CONTRACT.md +16 -13
  80. package/src/core/self/INTERFACE-GAPS.md +8 -1
  81. package/src/core/self/NOTES.md +123 -1
  82. package/src/core/self/calendar.ts +81 -0
  83. package/src/core/self/episodes.ts +158 -36
  84. package/src/core/self/identity.ts +2 -2
  85. package/src/core/self/index.ts +88 -20
  86. package/src/core/self/tunables.ts +18 -46
  87. package/src/core/self/writer.ts +58 -9
  88. package/src/core/store/CONTRACT.md +16 -1
  89. package/src/core/store/NOTES.md +293 -0
  90. package/src/core/store/cache.ts +585 -27
  91. package/src/core/store/db.ts +97 -0
  92. package/src/core/store/index.ts +570 -13
  93. package/tools/install-loop/run.sh +52 -71
  94. package/src/adapters/claude-code/credentials.ts +0 -199
  95. package/src/adapters/claude-code/interpret-client.ts +0 -395
  96. package/src/adapters/cli/keys.ts +0 -503
@@ -199,6 +199,103 @@ export function journalModeOf(path: string): string {
199
199
  }
200
200
  }
201
201
 
202
+ /**
203
+ * What `foldWal` did. A fold succeeded only when `busy` is false AND `error` is
204
+ * unset; `busy: false` with `error` set means the pragma threw for some other
205
+ * reason (a closed handle, a statement still running) and nothing was folded.
206
+ */
207
+ export interface WalFold {
208
+ /** True when SQLite could not finish: a reader held an older snapshot, or
209
+ * another connection held a lock. What it could copy, it copied. */
210
+ readonly busy: boolean;
211
+ /** Frames in the log as the pragma reports them: 0 after a successful
212
+ * TRUNCATE (the log is emptied), the remaining frames when `busy`, and -1
213
+ * when the file is not in WAL or the pragma could not run. */
214
+ readonly log: number;
215
+ /** Frames copied into the database file, read the same way as `log`: 0
216
+ * after a successful TRUNCATE, -1 when not in WAL or the pragma could not run. */
217
+ readonly checkpointed: number;
218
+ /** Set only when the pragma THREW; a code or a message, never row text. */
219
+ readonly error?: string;
220
+ }
221
+
222
+ /**
223
+ * FOLD THE WRITE-AHEAD LOG into the database file and truncate it to zero —
224
+ * `PRAGMA wal_checkpoint(TRUNCATE)` — without ever waiting and without ever
225
+ * throwing. Meant for the last thing a WRITER does before it closes its handle
226
+ * (`Store.close`, cli INTERFACE-GAPS §13).
227
+ *
228
+ * WHY IT IS NEEDED: SQLite folds the log itself when the LAST connection closes,
229
+ * but only if that close really happens. `openDb` prepares a statement per call
230
+ * and never finalizes one, so bun's `close()` defers the real close and the
231
+ * `-wal` stays at its high-water size until some later process folds it —
232
+ * finding #26, a store whose size on disk depended on who touched it last. An
233
+ * explicit checkpoint does not care about statement lifecycles or drivers.
234
+ *
235
+ * WHY IT NEVER WAITS: RESTART and TRUNCATE run the busy handler while they wait
236
+ * for readers to leave the log, and this connection's handler waits
237
+ * `BUSY_TIMEOUT_MS`. A hook closing while the MCP server or the worker held a
238
+ * read snapshot would stall for five seconds. So the timeout goes to ZERO first
239
+ * for the fold only (it is put back afterwards) — and a
240
+ * contended fold degrades to what SQLite calls PASSIVE: it copies every frame it
241
+ * can without blocking, reports `busy`, and the next writer's close (or
242
+ * SQLite's own auto-checkpoint) finishes the job. Nothing is lost either way;
243
+ * the log is part of the database until it is folded.
244
+ *
245
+ * A file not in WAL answers `log: -1` and nothing happens. Any throw — a
246
+ * statement still running on this connection, a closed handle — is caught and
247
+ * named in `error`: a close must never fail because a checkpoint could not run.
248
+ *
249
+ * Declared BEFORE `openDb` on purpose: the I39 test reads the pragmas in
250
+ * `openDb`'s body in order, and this function's two are not part of an open.
251
+ */
252
+ export function foldWal(db: Db): WalFold {
253
+ try {
254
+ db.exec("PRAGMA busy_timeout = 0");
255
+ const row = db.get<Record<string, number>>("PRAGMA wal_checkpoint(TRUNCATE)");
256
+ if (row === undefined) return { busy: true, log: -1, checkpointed: -1, error: "no row" };
257
+ // SQLite names the three columns `busy`, `log`, `checkpointed`; by position
258
+ // as the fallback, which is how the removal reclaim has always read the first.
259
+ const at = Object.values(row);
260
+ const busy = row["busy"] ?? at[0];
261
+ const log = row["log"] ?? at[1];
262
+ const checkpointed = row["checkpointed"] ?? at[2];
263
+ return { busy: busy !== 0, log: Number(log ?? -1), checkpointed: Number(checkpointed ?? -1) };
264
+ } catch (err) {
265
+ const code = (err as { code?: unknown } | null | undefined)?.code;
266
+ return {
267
+ busy: isLocked(err),
268
+ log: -1,
269
+ checkpointed: -1,
270
+ error: typeof code === "string" ? code : String((err as Error)?.message ?? err),
271
+ };
272
+ } finally {
273
+ // Put the wait back, so a caller that runs another statement on this handle
274
+ // after the fold has not silently lost I39's timeout.
275
+ try {
276
+ db.exec(`PRAGMA busy_timeout = ${BUSY_TIMEOUT_MS}`);
277
+ } catch {
278
+ /* a closed handle has no timeout to restore */
279
+ }
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Did THIS connection change any row since it opened? SQLite's own
285
+ * `total_changes()` — row changes (INSERT/UPDATE/DELETE) by this connection,
286
+ * nobody else's. A schema-only write (CREATE, ALTER, a pragma) counts no rows,
287
+ * so it does not trigger a fold; its frames wait for the next writer's fold.
288
+ * A handle that only read answers false, and so does a closed one (the
289
+ * question throws there, and "I cannot tell" is not "I wrote").
290
+ */
291
+ export function wroteOn(db: Db): boolean {
292
+ try {
293
+ return (db.get<{ n: number }>("SELECT total_changes() AS n")?.n ?? 0) > 0;
294
+ } catch {
295
+ return false;
296
+ }
297
+ }
298
+
202
299
  export function openDb(path: string, opts: OpenDbOptions = {}): Db {
203
300
  const { raw, driver } = openRaw(path);
204
301
  let depth = 0;