counterparts 0.3.13 → 0.3.15

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 (106) hide show
  1. package/CHANGELOG.md +264 -0
  2. package/docs/QUICKSTART.md +22 -0
  3. package/docs/README.md +1 -0
  4. package/package.json +1 -1
  5. package/src/adapters/claude-code/CONTRACT.md +48 -0
  6. package/src/adapters/claude-code/NOTES.md +148 -0
  7. package/src/adapters/claude-code/bin/hook.mjs +4 -2
  8. package/src/adapters/claude-code/bin/hook.ts +61 -7
  9. package/src/adapters/claude-code/bin/nightly.ts +5 -1
  10. package/src/adapters/claude-code/bin/runner.ts +11 -3
  11. package/src/adapters/claude-code/claim.ts +319 -0
  12. package/src/adapters/claude-code/doctor.ts +170 -16
  13. package/src/adapters/claude-code/embed-client.ts +3 -0
  14. package/src/adapters/cli/CONTRACT.md +5 -0
  15. package/src/adapters/cli/NOTES.md +25 -0
  16. package/src/adapters/cli/bin/counterparts.mjs +4 -2
  17. package/src/adapters/cli/bin/counterparts.ts +49 -3
  18. package/src/adapters/cli/commands.ts +169 -32
  19. package/src/adapters/cli/desktop.ts +112 -7
  20. package/src/adapters/cli/help.ts +10 -0
  21. package/src/adapters/cli/index.ts +1 -0
  22. package/src/adapters/cli/install.ts +94 -14
  23. package/src/adapters/cli/self-page.ts +5 -8
  24. package/src/adapters/cli/settle.ts +39 -2
  25. package/src/adapters/cli/wire.ts +18 -1
  26. package/src/adapters/config.ts +29 -1
  27. package/src/adapters/dashboard/NOTES.md +33 -0
  28. package/src/adapters/dashboard/bin/dashboard.mjs +4 -2
  29. package/src/adapters/dashboard/bin/dashboard.ts +11 -3
  30. package/src/adapters/dashboard/index.ts +14 -0
  31. package/src/adapters/dashboard/registries.ts +3 -0
  32. package/src/adapters/dashboard/source.ts +0 -8
  33. package/src/adapters/dashboard/web/fired.ts +8 -0
  34. package/src/adapters/dashboard/web/flow.ts +1 -0
  35. package/src/adapters/dashboard/web/lanes.ts +1 -0
  36. package/src/adapters/dashboard/web/narrate.ts +11 -0
  37. package/src/adapters/dashboard/web/pages/flow/sections/fired.js +1 -1
  38. package/src/adapters/dashboard/web/pages/health/sections/cycle.js +7 -10
  39. package/src/adapters/dashboard/web/pages/memories/memories.css +3 -0
  40. package/src/adapters/dashboard/web/pages/memories/row.js +5 -2
  41. package/src/adapters/dashboard/web/pages/memories/sections/hold.js +2 -1
  42. package/src/adapters/dashboard/web/pages/memories/sections/search.js +19 -3
  43. package/src/adapters/dashboard/web/server.ts +16 -4
  44. package/src/adapters/dashboard/web/shared/dates.js +6 -16
  45. package/src/adapters/dashboard/web/shared/memory-modal.js +21 -8
  46. package/src/adapters/dashboard/web/views/health.ts +19 -5
  47. package/src/adapters/dashboard/web/views/mechanism-panel.ts +8 -5
  48. package/src/adapters/dashboard/web/views/memories.ts +26 -7
  49. package/src/adapters/dashboard/web/views/memory-words.ts +16 -0
  50. package/src/adapters/dashboard/web/views/memory.ts +51 -2
  51. package/src/adapters/dashboard/web/views/mind.ts +38 -6
  52. package/src/adapters/empty-bunfig.toml +5 -0
  53. package/src/adapters/fired.ts +16 -2
  54. package/src/adapters/host-wiring.ts +112 -17
  55. package/src/adapters/log/index.ts +2 -2
  56. package/src/adapters/mcp/CONTRACT.md +17 -2
  57. package/src/adapters/mcp/INTERFACE-GAPS.md +15 -1
  58. package/src/adapters/mcp/NOTES.md +269 -0
  59. package/src/adapters/mcp/bin/serve.mjs +4 -2
  60. package/src/adapters/mcp/bin/serve.ts +17 -6
  61. package/src/adapters/mcp/facts.ts +72 -4
  62. package/src/adapters/mcp/meaning.ts +218 -15
  63. package/src/adapters/mcp/server.ts +21 -6
  64. package/src/adapters/mcp/tools.ts +86 -61
  65. package/src/adapters/plugin-run.sh +413 -34
  66. package/src/adapters/plugin.ts +141 -17
  67. package/src/adapters/runtime.ts +192 -17
  68. package/src/adapters/sessions.ts +2 -2
  69. package/src/core/associate/NOTES.md +10 -0
  70. package/src/core/bridge.ts +1 -1
  71. package/src/core/briefing.ts +20 -3
  72. package/src/core/contradictions.ts +56 -20
  73. package/src/core/counterpart.ts +72 -23
  74. package/src/core/dream/CONTRACT.md +6 -14
  75. package/src/core/dream/INTERFACE-GAPS.md +3 -2
  76. package/src/core/dream/NOTES.md +10 -0
  77. package/src/core/dream/index.ts +11 -53
  78. package/src/core/dream/reflect.ts +11 -2
  79. package/src/core/embed/static.ts +5 -1
  80. package/src/core/encode/INTERFACE-GAPS.md +2 -1
  81. package/src/core/encode/NOTES.md +12 -0
  82. package/src/core/encode/chunk.ts +6 -30
  83. package/src/core/encode/index.ts +0 -1
  84. package/src/core/encode/types.ts +1 -13
  85. package/src/core/handoff/INTERFACE-GAPS.md +1 -1
  86. package/src/core/handoff/index.ts +2 -1
  87. package/src/core/handoff/last-here.ts +18 -2
  88. package/src/core/physics/CONTRACT.md +6 -0
  89. package/src/core/physics/NOTES.md +33 -2
  90. package/src/core/physics/index.ts +22 -7
  91. package/src/core/prospective/CONTRACT.md +3 -1
  92. package/src/core/prospective/NOTES.md +15 -0
  93. package/src/core/recall/activate.ts +1 -1
  94. package/src/core/recall/time-ask.ts +173 -19
  95. package/src/core/self/CONTRACT.md +56 -19
  96. package/src/core/self/NOTES.md +172 -0
  97. package/src/core/self/behind.ts +6 -3
  98. package/src/core/self/briefing.ts +384 -28
  99. package/src/core/self/index.ts +105 -44
  100. package/src/core/self/page.ts +12 -85
  101. package/src/core/self/tunables.ts +16 -19
  102. package/src/core/self/writer.ts +31 -4
  103. package/src/core/sleep/NOTES.md +32 -0
  104. package/src/core/store/NOTES.md +21 -2
  105. package/src/core/store/index.ts +2 -1
  106. package/src/core/time.ts +15 -0
@@ -31,7 +31,6 @@ import { isDay, localDate, localTime, resolveZone, todayIn } from "../../core/ti
31
31
  import { homedir, tmpdir } from "node:os";
32
32
  import { basename, delimiter, dirname, isAbsolute, join, resolve } from "node:path";
33
33
  import { spawn } from "node:child_process";
34
- import { fileURLToPath } from "node:url";
35
34
  import { parseArgs } from "node:util";
36
35
 
37
36
  import {
@@ -47,7 +46,7 @@ import { CLAIMED_DEFAULT_META_KEY } from "../../core/mint.js";
47
46
  // band with this exact function, and two implementations of "which band is this
48
47
  // row in today" is how the two surfaces disagreed in the first place.
49
48
  import { TUNABLES, band } from "../../core/physics/index.js";
50
- import { LANE_ORDER, PREFACE_RESERVE_BYTES, readChapterLead, readableDate } from "../../core/self/index.js";
49
+ import { LANE_ORDER, PAGE_HOST_BUDGET_BYTES, PREFACE_RESERVE_BYTES, readChapterLead, readableDate } from "../../core/self/index.js";
51
50
  // The ONE predicate for "this row is the journal, not a memory" — the same one
52
51
  // the sleep phases and the dashboard's census use. A second copy of that test
53
52
  // living here is how the console drifted away from them in the first place.
@@ -155,7 +154,7 @@ import {
155
154
  SPAWN_START_COUNT_KEY,
156
155
  SPAWN_START_DATE_KEY,
157
156
  } from "../claude-code/hooks.js";
158
- import { loadConfig, withEmbedderDefault } from "../config.js";
157
+ import { loadConfig, withEmbedderDefault, zoneBeside } from "../config.js";
159
158
  // The one answer to "is there an embedder", shared with the hook, the worker and
160
159
  // the MCP server's entry point — `ask` embeds its question with it.
161
160
  import { openEmbedder } from "../claude-code/embed-client.js";
@@ -237,9 +236,11 @@ import {
237
236
  import type { ParkStep, StartFreshPlan, UndoPlan } from "./start-fresh.js";
238
237
  // A's two modules: the host's files, and leaving. They import nothing from here
239
238
  // but the `Io` type, so this direction is one-way.
240
- import { realProcessLister, realSpawner, sessionsNote, tilde, unwire, wire } from "./wire.js";
241
- import { connectDesktop, desktopUnavailable, readDesktop } from "./desktop.js";
242
- import { runtimeLabel } from "../runtime.js";
239
+ import { look, realProcessLister, realSpawner, sessionsNote, tilde, unwire, wire } from "./wire.js";
240
+ import { connectDesktop, desktopUnavailable, readDesktop, repairDesktop } from "./desktop.js";
241
+ import type { DesktopRepair } from "./desktop.js";
242
+ import { BINARY, bundledModelDir, packagePath, runtimeLabel } from "../runtime.js";
243
+ import type { Binary } from "../runtime.js";
243
244
  import { DESKTOP_ALWAYS_ALLOW, DESKTOP_HOST, upgradeWords } from "../hosts.js";
244
245
  import { hostsSeen, installedVersion } from "../sessions.js";
245
246
  import type {
@@ -504,8 +505,29 @@ export interface RunOptions {
504
505
  * falls back to.)
505
506
  */
506
507
  scope?: string;
508
+ /**
509
+ * THE SINGLE BINARY (`runtime.ts#Binary`), when this console is it — the
510
+ * program the Claude Code plugin downloads when a machine has no Bun or Node.
511
+ * Real runs never pass it and get `BINARY`; tests pass one to drive the
512
+ * binary's refusals from source.
513
+ */
514
+ binary?: Binary | null;
507
515
  }
508
516
 
517
+ /**
518
+ * WHAT THE SINGLE BINARY WILL NOT DO TO A HOST. The binary lives in the
519
+ * plugin's data directory, which Claude Code deletes with the plugin, and the
520
+ * plugin already runs the hooks and the memory server — so a hooks block, an
521
+ * MCP registration or a Claude Desktop entry naming it would be a second
522
+ * install that dies silently on uninstall. Wiring a host by hand is the npm
523
+ * install's job.
524
+ */
525
+ export const BINARY_CONNECT_REFUSAL =
526
+ "This is Counterparts' single program, the one the Claude Code plugin downloads when a computer has no Bun or Node.js. " +
527
+ "It doesn't connect hosts itself: the plugin already runs the hooks and the memory server, and anything wired to this file " +
528
+ "would stop working when the plugin is uninstalled. To wire Claude Code or Claude Desktop by hand, install Counterparts with " +
529
+ "npm (`npm i -g counterparts`) and run `counterparts connect` from that install.";
530
+
509
531
  /**
510
532
  * THE CONSOLE'S MAP — about forty lines, grouped, one line per command.
511
533
  *
@@ -544,7 +566,7 @@ let readVersion: string | null = null;
544
566
  export function packageVersion(): string {
545
567
  if (readVersion !== null) return readVersion;
546
568
  try {
547
- const raw = readFileSync(fileURLToPath(new URL("../../../package.json", import.meta.url)), "utf8");
569
+ const raw = readFileSync(packagePath("package.json"), "utf8");
548
570
  const said = (JSON.parse(raw) as Record<string, unknown>)["version"];
549
571
  readVersion = typeof said === "string" && said.length > 0 ? said : VERSION_UNKNOWN;
550
572
  } catch {
@@ -817,7 +839,7 @@ export const COMMAND_BLURB: Record<Command, string> = {
817
839
  core:
818
840
  "The core — the memories about me, about us and about the owner that do not fade. With no flags (or --list), what it holds and which lane carried each one there, what dreams have nominated, and what you sent back; --demote <id> --reason \"...\" sends one back to ordinary fading from today, records why, and keeps the lanes from promoting it again; --reflected-feeling on|off opens or closes the core to reflection alone. On (the default): a feeling a reflection records later and a reflection citing a memory both count toward the fast lane, and a reflection may re-label what a memory is about either way — each re-label is recorded with its reason, and one into me, us or the owner is told in the next morning share. Off: nothing reaches the core on a reflection alone — the fast lane needs a feeling felt at the time (not one a reflection recorded later) and an ordinary use after a gap (not a reflection citing it), and a reflection may only move what a memory is about toward work or world. Reading works under observer; the two changes refuse there.",
819
841
  settle:
820
- "Two memories that disagree. With no flags (or --list), the pairs nobody has settled — a dream flags them — and the ones settled lately: how, which holds, who settled it and why. --pair <id> --holds <memory id> --how changed|corrected|open --why \"...\" settles one as you (or --holds <id> --against <id> for two memories no dream flagged): changed — both were true at their time, the older fades once and is shown as earlier; corrected — the older was wrong, it leaves recall and stays readable by its id; open — a real disagreement, both kept and shown together. No memory is rewritten. --undo <pair> reverses a settle: the strength comes back, a corrected memory comes back into recall, and the pair is unsettled again. Reading works under observer; the two changes refuse there.",
842
+ "Two memories that disagree. With no flags (or --list), the pairs nobody has settled — a dream flags them — and the ones settled lately: how, which holds, who settled it and why; and the corrections held in the last 7 lived days (a memory said it changes or corrects one that didn't look related, so nothing was settled) that nobody has settled since, each with the command that settles it. --pair <id> --holds <memory id> --how changed|corrected|open --why \"...\" settles one as you (or --holds <id> --against <id> for two memories no dream flagged): changed — both were true at their time, the older fades once and is shown as earlier; corrected — the older was wrong, it leaves recall and stays readable by its id; open — a real disagreement, both kept and shown together. No memory is rewritten. --undo <pair> reverses a settle: the strength comes back, a corrected memory comes back into recall, and the pair is unsettled again. Reading works under observer; the two changes refuse there.",
821
843
  coverage:
822
844
  "What is written up, and what is not yet, for one date (default today): each session that captured something that day, how much of it is written up, and what is not yet written up — how many pieces, over how long, since when — and whether that is owed (the next session in its project is asked to write it up), lapsed (nobody did in two days of use; nothing is deleted), under the floor, or still at work. Read-only.",
823
845
  dashboard:
@@ -1634,6 +1656,29 @@ export async function run(argv: readonly string[], opts: RunOptions): Promise<nu
1634
1656
  io.err(implicit);
1635
1657
  return EXIT.refused;
1636
1658
  }
1659
+ // THE SINGLE BINARY makes the store and the configuration — the plugin's
1660
+ // first run is exactly `install --no-connect` — and wires nothing: no
1661
+ // conversation that ends in `connect`, no Desktop entry, no printed hooks
1662
+ // block naming a file the plugin's uninstall will delete.
1663
+ const binary = opts.binary === undefined ? BINARY : opts.binary;
1664
+ if (binary !== null) {
1665
+ const hostAsked = parsed.flags["host"];
1666
+ if (hostAsked !== undefined && hostAsked !== "claude-code") {
1667
+ io.err(BINARY_CONNECT_REFUSAL);
1668
+ return EXIT.refused;
1669
+ }
1670
+ try {
1671
+ const code = installCommand(parsed, io, env, opts.home, named, { hostSteps: false });
1672
+ if (code === EXIT.ok) {
1673
+ io.out("");
1674
+ io.out("Claude Code runs this memory through the Counterparts plugin, so there is nothing to connect.");
1675
+ }
1676
+ return code;
1677
+ } catch (err) {
1678
+ io.err(`install failed: ${describeDirRefusal(err)}`);
1679
+ return EXIT.failed;
1680
+ }
1681
+ }
1637
1682
  // `--host claude-desktop` (2026-09-30): the store and the configuration as
1638
1683
  // any install makes them, then Claude Desktop's config entry instead of
1639
1684
  // Claude Code's hooks. No `--host`, or `--host claude-code`, is the install
@@ -1676,6 +1721,10 @@ export async function run(argv: readonly string[], opts: RunOptions): Promise<nu
1676
1721
  // The explicit-dir guard applies by hand, exactly as it does to `install`:
1677
1722
  // the default configuration names the live store and the live base, and
1678
1723
  // `uninstall --park` renames the directory that file sits in.
1724
+ if (command === "connect" && (opts.binary === undefined ? BINARY : opts.binary) !== null) {
1725
+ io.err(BINARY_CONNECT_REFUSAL);
1726
+ return EXIT.refused;
1727
+ }
1679
1728
  if (command === "connect" || command === "disconnect" || command === "uninstall") {
1680
1729
  const implicit = named === undefined ? null : implicitConfigRefusal(named, env);
1681
1730
  if (implicit !== null) {
@@ -2598,7 +2647,7 @@ async function selfPageCommand(
2598
2647
  return EXIT.failed;
2599
2648
  }
2600
2649
  try {
2601
- const cap = counterpart.self.tunables.PAGE_WAKE_BYTES;
2650
+ const cap = counterpart.self.tunables.PAGE_MAX_BYTES;
2602
2651
  const say = (out: ReturnType<typeof writeLines>): number => {
2603
2652
  for (const line of out.lines) (out.ok ? io.out : io.err)(line);
2604
2653
  return out.ok ? EXIT.ok : EXIT.refused;
@@ -3824,6 +3873,19 @@ async function installConversation(
3824
3873
  u.hint(`You can do it later with: ${BIN.cli} connect`);
3825
3874
  }
3826
3875
  }
3876
+ // CLAUDE DESKTOP'S ENTRY, REPAIRED AS `connect` REPAIRS IT (2026-10-10, left
3877
+ // open by #354): an entry `install --host claude-desktop` wrote in an older
3878
+ // shape gets today's command, by the same path and with the same refusal
3879
+ // while Desktop runs (`repairDesktopEntry`). Outside the Claude Code question
3880
+ // on purpose: Desktop's file is there or not whether Claude Code is. Only
3881
+ // this arm: the scripted install reads no host file at all.
3882
+ const desktop = repairDesktopEntry(io, env, home_, lister, false, now());
3883
+ // Left in the old shape, Desktop's entry keeps doctor's Runtime line amber.
3884
+ const desktopPending = desktop.outcome === "desktop-running" || desktop.outcome === "failed";
3885
+ // A Desktop file that does not parse was left alone too, but doctor cannot
3886
+ // read an entry in it either, so its Runtime line is green: the ending can
3887
+ // promise neither green nor amber for Desktop (review of #356).
3888
+ const desktopUnread = desktop.outcome === "refused";
3827
3889
  u.blank();
3828
3890
 
3829
3891
  // ── no keys ───────────────────────────────────────────────────────────────
@@ -3848,7 +3910,8 @@ async function installConversation(
3848
3910
  // Looked for the way the hooks will look (`resolveStaticWeights`: the
3849
3911
  // environment variable, then the installed package), and then for the table
3850
3912
  // FILE in what that found — a variable naming an empty folder is not a table.
3851
- const table = resolveStaticWeights({ env });
3913
+ const bundled = bundledModelDir();
3914
+ const table = resolveStaticWeights({ env, ...(bundled === undefined ? {} : { packageDir: bundled }) });
3852
3915
  const tableOn = config.embedder?.enabled === true;
3853
3916
  const tableMissing = tableOn && (table === null || !existsSync(join(table.dir, MODEL_FILE)));
3854
3917
  if (tableOn && !tableMissing) {
@@ -3883,14 +3946,27 @@ async function installConversation(
3883
3946
  const toolsIn =
3884
3947
  wired !== null &&
3885
3948
  (wired.mcp === "added" || wired.mcp === "re-added" || wired.mcp === "already");
3886
- if (wired !== null && wired.outcome === "ok" && hooksIn && toolsIn && !tableMissing) {
3949
+ const connected = wired !== null && wired.outcome === "ok" && hooksIn && toolsIn;
3950
+ if (connected && !tableMissing && !desktopPending && !desktopUnread) {
3887
3951
  io.out(`Restart Claude Code, then run \`${BIN.cli} doctor\` — it should be all green.`);
3888
3952
  } else if (hooksIn && !toolsIn) {
3889
3953
  io.out("The hooks are in; the memory tools are NOT registered — the line above does that.");
3890
3954
  io.out(`Then restart Claude Code and run \`${BIN.cli} doctor\`.`);
3891
- } else if (wired !== null && wired.outcome === "ok" && hooksIn && toolsIn) {
3955
+ } else if (connected && desktopUnread) {
3956
+ // Desktop's file is unreadable — said above. Doctor cannot see into it, so
3957
+ // "all green" would vouch for a Desktop nobody checked.
3958
+ io.out(
3959
+ `Restart Claude Code, then run \`${BIN.cli} doctor\`. Claude Desktop's config was left as it was, as said above; once it is fixed, \`${BIN.cli} connect\` brings its entry up to date.`,
3960
+ );
3961
+ } else if (connected && !desktopPending) {
3892
3962
  // Connected, and the one amber is the table named above.
3893
3963
  io.out(`Restart Claude Code, then run \`${BIN.cli} doctor\` — everything but recall by meaning should be green.`);
3964
+ } else if (connected) {
3965
+ // Connected, and Claude Desktop's entry is still in the old shape — said
3966
+ // above, with what to do. "All green" here would be false (review M1).
3967
+ io.out(
3968
+ `Restart Claude Code, then run \`${BIN.cli} doctor\` — its Runtime line stays amber until Claude Desktop's entry is rewritten, as said above.`,
3969
+ );
3894
3970
  } else {
3895
3971
  io.out(`Run \`${BIN.cli} doctor\` to see where this got to.`);
3896
3972
  }
@@ -4067,9 +4143,12 @@ function blankBeside(u: Ui, usable: readonly ParkedSighting[], home: string): "b
4067
4143
 
4068
4144
  /**
4069
4145
  * The interactive install's ceiling, and the ONLY default this package has for
4070
- * one (scar §2.18 holds everywhere else, the scripted arm included).
4146
+ * one (scar §2.18 holds everywhere else, the scripted arm included). It is the
4147
+ * ceiling the self page's write limit is sized against
4148
+ * (`self/briefing.ts#PAGE_HOST_BUDGET_BYTES`, 2026-10-09), read from there so
4149
+ * the two cannot drift: 9,000.
4071
4150
  */
4072
- const DEFAULT_BUDGET_BYTES = 9000;
4151
+ const DEFAULT_BUDGET_BYTES = PAGE_HOST_BUDGET_BYTES;
4073
4152
 
4074
4153
  /** A configuration is "custom" by its PATH, not by how it was named — the same
4075
4154
  * test `installCommand` and `startFreshCommand` each make. Hoisted here so
@@ -4225,6 +4304,12 @@ async function hostWiringCommand(
4225
4304
  lister,
4226
4305
  };
4227
4306
  const result = command === "connect" ? await wire(input) : await unwire(input);
4307
+ // CLAUDE DESKTOP'S ENTRY TOO (2026-10-10): `install --host claude-desktop`
4308
+ // wrote a server command into Desktop's own file, and it needs the same
4309
+ // repair the hooks just got — but never while Desktop is open
4310
+ // (`repairDesktopEntry`).
4311
+ const desktopFailed =
4312
+ command === "connect" && repairDesktopEntry(io, env, home_, lister, input.dryRun === true, now()).outcome === "failed";
4228
4313
  // WHAT AN OPEN SESSION DOES NOW, printed by the CALLER (2026-09-22). `wire()`
4229
4314
  // used to say it itself, which put it in the middle of `install`'s screen two
4230
4315
  // lines above install's own "restart Claude Code, then run doctor". A
@@ -4243,7 +4328,66 @@ async function hostWiringCommand(
4243
4328
  ) {
4244
4329
  sessionsNote(ui(io, env), lister, { dataDir: store, env });
4245
4330
  }
4246
- return exitFor(result.outcome);
4331
+ return desktopFailed && result.outcome === "ok" ? EXIT.failed : exitFor(result.outcome);
4332
+ }
4333
+
4334
+ /**
4335
+ * CLAUDE DESKTOP'S ENTRY IN TODAY'S SHAPE — `connect`'s repair, and since
4336
+ * 2026-10-10 `install`'s at a terminal too (the other caller of `wire()`).
4337
+ * One path for both: the process look, `desktop.ts#repairDesktop` (an entry of
4338
+ * ours only, its `env` kept, backed up first, written atomically, never while
4339
+ * Desktop runs or could not be ruled out), and one line saying what happened.
4340
+ * Returns the repair; what it does to the exit is the caller's call.
4341
+ */
4342
+ function repairDesktopEntry(
4343
+ io: Io,
4344
+ env: Record<string, string | undefined>,
4345
+ home_: string,
4346
+ lister: ProcessLister,
4347
+ dryRun: boolean,
4348
+ at: number,
4349
+ ): DesktopRepair {
4350
+ const sighting = look(lister);
4351
+ const repair = repairDesktop({
4352
+ home: home_,
4353
+ exe: process.execPath,
4354
+ desktopRunning: sighting.looked ? (sighting.desktopRunning ?? null) : null,
4355
+ dryRun,
4356
+ now: at,
4357
+ });
4358
+ sayDesktopRepair(ui(io, env), repair, home_);
4359
+ return repair;
4360
+ }
4361
+
4362
+ /** One line about Claude Desktop's entry, or none. True when the write failed. */
4363
+ function sayDesktopRepair(u: ReturnType<typeof ui>, repair: DesktopRepair, home_: string): boolean {
4364
+ const where = tilde(repair.path, home_);
4365
+ switch (repair.outcome) {
4366
+ case "none":
4367
+ case "current":
4368
+ return false;
4369
+ case "repaired":
4370
+ u.ok(
4371
+ `Claude Desktop: rewrote its counterparts entry to start the memory server the way Claude Code's now does${repair.backup === null ? "" : ` (backup: ${tilde(repair.backup, home_)})`}. It takes effect when Desktop next opens.`,
4372
+ );
4373
+ return false;
4374
+ case "would-repair":
4375
+ u.hint(`Claude Desktop: would rewrite its counterparts entry in ${where} to start the memory server the way Claude Code's does. Nothing was written.`);
4376
+ return false;
4377
+ case "desktop-running":
4378
+ u.warn(
4379
+ repair.unknownRunning === true
4380
+ ? `Claude Desktop's counterparts entry was left as it was: whether Desktop is open could not be checked, and Desktop rewrites ${where} while it runs. Quit Claude Desktop, then run \`${BIN.cli} connect\` again.`
4381
+ : `Claude Desktop is open, so its counterparts entry was left in the old shape: Desktop rewrites ${where} while it runs. Quit Claude Desktop, run \`${BIN.cli} connect\` again, then reopen it.`,
4382
+ );
4383
+ return false;
4384
+ case "refused":
4385
+ u.warn(`Claude Desktop: ${repair.detail ?? `${where} could not be read, and was left as it was.`}`);
4386
+ return false;
4387
+ case "failed":
4388
+ u.fail(`Claude Desktop's entry could not be rewritten: ${repair.detail ?? "unknown error"}`);
4389
+ return true;
4390
+ }
4247
4391
  }
4248
4392
 
4249
4393
  /** `wire.ts` returns words, not numbers, so that it never has to import this
@@ -8736,27 +8880,14 @@ export function openCounterpart(
8736
8880
  });
8737
8881
  }
8738
8882
 
8739
- /**
8740
- * `snapshots.dir` from the host configuration beside the store
8741
- * (`<base>/claude-code.json`, the file `install` writes), so a migration the
8742
- * console runs puts its copy where the worker's rotation and doctor look.
8743
- * Absent, unreadable or unset: undefined, and the store's default applies.
8744
- */
8745
8883
  /**
8746
8884
  * `timeZone` from the host configuration beside the store — the same file
8747
8885
  * `snapshotsDirBeside` reads — so a console command names the person's day in
8748
- * the zone the hooks use (docs/time.md, 2026-09-25). Absent or unreadable:
8749
- * undefined, and the store follows the machine's zone.
8886
+ * the zone the hooks use (docs/time.md, 2026-09-25). It lives in `config.ts`
8887
+ * since 2026-10-09, where the dashboard reads it too; re-exported here for the
8888
+ * console's callers.
8750
8889
  */
8751
- export function zoneBeside(dir: string): string | undefined {
8752
- const beside = join(dir, "..", "claude-code.json");
8753
- if (!existsSync(beside)) return undefined;
8754
- try {
8755
- return loadConfig(JSON.parse(readFileSync(beside, "utf8"))).config.timeZone;
8756
- } catch {
8757
- return undefined;
8758
- }
8759
- }
8890
+ export { zoneBeside };
8760
8891
 
8761
8892
  /** `Store.open` for the console: the config's zone beside the store rides along. */
8762
8893
  function openStoreAt(opts: { dir: string; observer?: boolean }): Store {
@@ -8764,6 +8895,12 @@ function openStoreAt(opts: { dir: string; observer?: boolean }): Store {
8764
8895
  return Store.open({ ...opts, ...(timeZone === undefined ? {} : { timeZone }) });
8765
8896
  }
8766
8897
 
8898
+ /**
8899
+ * `snapshots.dir` from the host configuration beside the store
8900
+ * (`<base>/claude-code.json`, the file `install` writes), so a migration the
8901
+ * console runs puts its copy where the worker's rotation and doctor look.
8902
+ * Absent, unreadable or unset: undefined, and the store's default applies.
8903
+ */
8767
8904
  export function snapshotsDirBeside(dir: string): string | undefined {
8768
8905
  const beside = join(dir, "..", "claude-code.json");
8769
8906
  if (!existsSync(beside)) return undefined;
@@ -36,20 +36,18 @@
36
36
  * one): nothing here reads `os.homedir()`.
37
37
  */
38
38
  import { existsSync, readFileSync } from "node:fs";
39
- import { dirname, join } from "node:path";
39
+ import { dirname } from "node:path";
40
40
 
41
41
  import { CONFIG_ENV } from "../config-path.js";
42
42
  import { scriptArgs } from "../runtime.js";
43
- import { hostConfigBase, hostMcpFile, MCP_SCRIPT, MCP_SERVER_NAME } from "./install.js";
44
- import { sightSettings, writeSettings } from "./wire.js";
43
+ import { BIN, desktopConfigPath, hostConfigBase, hostMcpFile, isOurDesktopEntry, MCP_SCRIPT, MCP_SERVER_NAME } from "./install.js";
44
+ import { sightSettings, tilde, unparsedSettingsWhy, writeSettings } from "./wire.js";
45
45
 
46
46
  /** The variable the server reads its store from (`mcp/bin/serve.ts#ENV.dir`). */
47
47
  const DATA_DIR_VAR = "COUNTERPARTS_DATA_DIR";
48
48
 
49
- /** Where Claude Desktop keeps its MCP servers, under a home directory (macOS). */
50
- export function desktopConfigPath(home: string): string {
51
- return join(home, "Library", "Application Support", "Claude", "claude_desktop_config.json");
52
- }
49
+ /** Where Claude Desktop keeps its MCP servers (macOS) — `install.ts` holds it. */
50
+ export { desktopConfigPath };
53
51
 
54
52
  /**
55
53
  * Why Claude Desktop cannot be connected on this platform, or null. The config
@@ -149,6 +147,113 @@ export function connectDesktop(input: {
149
147
  }
150
148
  }
151
149
 
150
+ /** Is a `counterparts` entry in Desktop's file one this package wrote —
151
+ * `install.ts` holds it, because doctor's `readHost` asks it too. */
152
+ export { isOurDesktopEntry };
153
+
154
+ /** What `connect` did about Claude Desktop's entry (`repairDesktop`). */
155
+ export interface DesktopRepair {
156
+ /**
157
+ * `none`: no file, no entry, or an entry that is not ours; `current`: ours,
158
+ * already in the shape this runtime writes; `repaired`: rewritten;
159
+ * `would-repair`: the dry run's answer; `desktop-running`: Desktop is open
160
+ * (or could not be ruled out), so the file was left as it was; `refused` /
161
+ * `failed`: the file could not be read or written.
162
+ */
163
+ readonly outcome: "none" | "current" | "repaired" | "would-repair" | "desktop-running" | "refused" | "failed";
164
+ readonly path: string;
165
+ readonly backup: string | null;
166
+ readonly detail: string | null;
167
+ /** True when "running" means "could not look", not "seen running". */
168
+ readonly unknownRunning?: boolean;
169
+ }
170
+
171
+ /**
172
+ * REWRITE OUR DESKTOP ENTRY IN TODAY'S SHAPE (2026-10-10) — what `connect`
173
+ * does for Claude Desktop after the hooks and Claude Code's registration.
174
+ *
175
+ * Only the command and its arguments change: the runtime `connect` runs under
176
+ * and `scriptArgs`' shape for it (`--no-env-file --config=<empty bunfig> run
177
+ * <serve.ts>` under Bun), the same repair the hooks get. Its `env` — the store
178
+ * and the configuration it names — and every other key stay as they were:
179
+ * a Desktop pointed at a different store is doctor's question
180
+ * (`desktopFindings`) and `install --host claude-desktop`'s answer, never a
181
+ * side effect of this.
182
+ *
183
+ * **NEVER WHILE DESKTOP RUNS.** Desktop rewrites this file while it is open,
184
+ * so a write under it can be overwritten (measured as advice since
185
+ * 2026-09-30: "Quit Claude Desktop BEFORE you run it"), and the server it
186
+ * already started keeps its old command until Desktop restarts anyway. Seen
187
+ * running — or not known, because the process list could not be read — is
188
+ * `desktop-running`: nothing written, and the caller says to quit Desktop and
189
+ * run `connect` again. Never throws.
190
+ */
191
+ export function repairDesktop(input: {
192
+ home: string;
193
+ exe: string;
194
+ /** From the process look (`wire.ts#ProcessSighting.desktopRunning`); null: could not look. */
195
+ desktopRunning: boolean | null;
196
+ dryRun: boolean;
197
+ now: number;
198
+ }): DesktopRepair {
199
+ const path = desktopConfigPath(input.home);
200
+ try {
201
+ const read = readObject(path);
202
+ if (read.state === "absent") return { outcome: "none", path, backup: null, detail: null };
203
+ // A FILE THAT DOES NOT PARSE IS SAID, NOT PASSED OVER (review of #354):
204
+ // whether it holds an entry of ours cannot be told, so it is never written,
205
+ // and the person hears why the Desktop half did nothing. An empty file is
206
+ // not a broken one (`sightSettings`): nothing in it, nothing of ours.
207
+ if (read.state === "unreadable") {
208
+ const sight = sightSettings(path, input.home);
209
+ if (sight.refusal === null) return { outcome: "none", path, backup: null, detail: null };
210
+ return { outcome: "refused", path, backup: null, detail: unreadableWhy(path, input.home) };
211
+ }
212
+ const servers = read.value["mcpServers"];
213
+ const prior = isRecord(servers) ? servers[MCP_SERVER_NAME] : undefined;
214
+ if (!isOurDesktopEntry(prior)) return { outcome: "none", path, backup: null, detail: null };
215
+ const want = { ...prior, command: input.exe, args: scriptArgs(MCP_SCRIPT, input.exe) };
216
+ if (JSON.stringify(want) === JSON.stringify(prior)) return { outcome: "current", path, backup: null, detail: null };
217
+ if (input.desktopRunning !== false) {
218
+ return { outcome: "desktop-running", path, backup: null, detail: null, unknownRunning: input.desktopRunning === null };
219
+ }
220
+ if (input.dryRun) return { outcome: "would-repair", path, backup: null, detail: null };
221
+ const sight = sightSettings(path, input.home);
222
+ if (sight.refusal !== null) return { outcome: "refused", path, backup: null, detail: sight.refusal };
223
+ const value = { ...sight.value, mcpServers: { ...(servers as Record<string, unknown>), [MCP_SERVER_NAME]: want } };
224
+ const wrote = writeSettings(sight, value, input.now);
225
+ if (wrote.error !== null) return { outcome: "failed", path, backup: wrote.backup, detail: wrote.error };
226
+ return { outcome: "repaired", path, backup: wrote.backup, detail: null };
227
+ } catch (err) {
228
+ return { outcome: "failed", path, backup: null, detail: String((err as Error).message ?? err) };
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Why `connect` left an unreadable Desktop file alone, in words a person can
234
+ * act on: `wire.ts#unparsedSettingsWhy`'s for the shapes a hand-edited file
235
+ * takes (comments, a trailing comma, a byte-order mark), else the parser's.
236
+ */
237
+ function unreadableWhy(path: string, home: string): string {
238
+ const where = tilde(path, home);
239
+ const unsure = "connect cannot tell whether it holds a counterparts entry, so nothing in it was changed";
240
+ let raw: string;
241
+ try {
242
+ raw = readFileSync(path, "utf8");
243
+ } catch (err) {
244
+ return `${where} could not be read (${String((err as Error).message ?? err)}): ${unsure}.`;
245
+ }
246
+ let parsed: unknown;
247
+ try {
248
+ parsed = JSON.parse(raw);
249
+ } catch (err) {
250
+ const why = unparsedSettingsWhy(raw);
251
+ if (why !== null) return `${where} ${why}. Until then ${unsure}.`;
252
+ return `${where} does not parse as JSON (${String((err as Error).message ?? err)}): ${unsure}. If you connected Claude Desktop, fix the file and run \`${BIN.cli} connect\` again.`;
253
+ }
254
+ return `${where} parses, but ${Array.isArray(parsed) ? "as an array" : `as a ${typeof parsed}`} rather than an object: ${unsure}.`;
255
+ }
256
+
152
257
  /** What doctor reads about Claude Desktop (`claude-code/doctor.ts#DesktopReading`). */
153
258
  export interface DesktopConfigReading {
154
259
  readonly path: string;
@@ -331,6 +331,11 @@ export const COMMAND_DETAIL: Record<string, readonly string[]> = {
331
331
  "start a chat with the \"Start with Counterparts\" prompt, or ask it to",
332
332
  "call the counterparts wake tool. In Claude Desktop, set the counterparts",
333
333
  "tools to Always allow, or a chat cannot wake on its own.",
334
+ "",
335
+ "Once Desktop is connected, `install` at a terminal brings that entry's",
336
+ "command up to date the way `counterparts connect` does, keeping the store",
337
+ "it names. Not while Desktop is open: it asks you to quit Desktop and run",
338
+ "connect again.",
334
339
  ],
335
340
  init: [
336
341
  "No host config, nothing under ~/.counterparts/ — that",
@@ -464,6 +469,11 @@ export const COMMAND_DETAIL: Record<string, readonly string[]> = {
464
469
  "",
465
470
  "Hooks start with your next turn in any open session; the memory tools",
466
471
  "appear after you restart Claude Code.",
472
+ "",
473
+ "If you connected Claude Desktop (see `counterparts help install`), connect",
474
+ "brings its entry's command up to date too, keeping the store it names. Not",
475
+ "while Desktop is open: Desktop rewrites that file while it runs, so",
476
+ "connect leaves it alone and asks you to quit Desktop and connect again.",
467
477
  ],
468
478
  disconnect: [
469
479
  "It removes only the entries it recognises as its own — another tool's hooks",
@@ -8,6 +8,7 @@
8
8
  * caller imports. The console reaches it directly; nobody else may.
9
9
  */
10
10
  export {
11
+ BINARY_CONNECT_REFUSAL,
11
12
  COMMANDS,
12
13
  COMMAND_BLURB,
13
14
  COMMAND_FLAGS,