@unhuman/wsync-cli 0.1.2 → 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 (3) hide show
  1. package/README.md +24 -1
  2. package/dist/wsync.mjs +150 -18
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -60,7 +60,7 @@ wsync link <key> [--server URL] link this directory to a project
60
60
  wsync backfill [--limit N] [--key wsp_…] [--verbose]
61
61
  import existing ~/.claude/projects transcripts
62
62
  wsync flush [--verbose] drain the offline spool to the server
63
- wsync status show config and pending spool
63
+ wsync status show config, links (keys by prefix) and pending spool
64
64
  wsync install-hooks [--dry-run] register hooks in ~/.claude/settings.json
65
65
  wsync uninstall-hooks remove them again
66
66
  wsync refresh-context --cwd PATH refresh the local context cache
@@ -72,6 +72,29 @@ haven't been capturing since the start — entirely over HTTP, using whichever l
72
72
  directory owns each transcript's working directory, or `--key` as a fallback for anything with no
73
73
  linked ancestor. Sessions that match neither are skipped and counted, never silently dropped.
74
74
 
75
+ **Where a session is filed** depends on which command captured it:
76
+
77
+ - **Live capture (the hooks)** files every session under the project the directory's key names.
78
+ Always. The checkout's git remote is recorded as evidence and shown in the app when it disagrees,
79
+ but it never overrules the key and never creates a project.
80
+ - **`backfill`** sends `mode: backfill`, and there the remote may choose the project — among the
81
+ projects that already exist in your workspace (by slug, by a remote the workspace has seen before,
82
+ or by an unambiguous bare name). A transcript whose remote matches nothing lands in the app's
83
+ **Needs filing** queue with the remote named, for a person to file. Neither mode invents a project;
84
+ one key can import a whole machine without funnelling twenty repositories into one project.
85
+
86
+ **Re-linking a directory moves it.** `link` on a directory that already has a key verifies the new
87
+ key, writes it, and then retires the previous one — with that key, on the server — so a directory
88
+ has one live key at a time and `status` never lists ghosts. A retire the server refuses is reported
89
+ and does not undo the link; revoke the old key from the app's Keys list in that case.
90
+
91
+ **`.wsync.json`** in a repository root is an optional marker (`{ "project": "<slug>", "ticket":
92
+ "<KEY>" }`) the identity layer reads ahead of the git remote. It does not change where live capture
93
+ files a session — the key does that — but it names the project `backfill` should look for and the
94
+ ticket to prefer. It lives in the repository, so committing it shares the choice with everyone who
95
+ clones; whether a project name belongs in a public repository is the owner's call. `link` never
96
+ writes it.
97
+
75
98
  ## Production server
76
99
 
77
100
  The default server (used when you pass no `--server` and have none configured) is
package/dist/wsync.mjs CHANGED
@@ -5515,6 +5515,14 @@ var require_schemas = __commonJS({
5515
5515
  exports.ingestSchema = zod_1.z.object({
5516
5516
  distillation: exports.distillationSchema,
5517
5517
  identity: exports.identitySchema,
5518
+ /**
5519
+ * Which rule files the session. `live` (the hooks): the capture key names the project, always, and
5520
+ * the identity's remote is recorded as evidence. `backfill` (`wsync backfill`, one key for a whole
5521
+ * machine of history): the remote may choose the project — among projects that exist — and an
5522
+ * unmatched one goes to triage. Neither mode ever creates a project. Absent from older clients,
5523
+ * and absent means live, the strict rule.
5524
+ */
5525
+ mode: zod_1.z.enum(["live", "backfill"]).default("live"),
5518
5526
  transcriptPath: zod_1.z.string().max(4096),
5519
5527
  transcriptShaPrefix: zod_1.z.string().max(128),
5520
5528
  machineHostname: zod_1.z.string().max(300).optional(),
@@ -6095,10 +6103,13 @@ function saveLink(absPath, key, opts = {}) {
6095
6103
  }
6096
6104
  }
6097
6105
  const links = current["links"] ?? {};
6098
- links[resolve(absPath)] = key;
6106
+ const path = resolve(absPath);
6107
+ const previous = links[path];
6108
+ links[path] = key;
6099
6109
  current["links"] = links;
6100
6110
  if (opts.server) current["server"] = opts.server;
6101
6111
  writeFileSync(file, JSON.stringify(current, null, 2) + "\n", { mode: 384 });
6112
+ return previous && previous !== key ? previous : null;
6102
6113
  }
6103
6114
  var configFile;
6104
6115
  var init_links = __esm({
@@ -6225,7 +6236,7 @@ var init_package = __esm({
6225
6236
  "package.json"() {
6226
6237
  package_default = {
6227
6238
  name: "@unhuman/wsync-cli",
6228
- version: "0.1.2",
6239
+ version: "0.3.0",
6229
6240
  description: "Capture Claude Code sessions to an Unhuman Work Context project: link a checkout, install the hooks, drain the spool.",
6230
6241
  license: "UNLICENSED",
6231
6242
  repository: {
@@ -6430,8 +6441,15 @@ async function link(key, opts = {}) {
6430
6441
  process.exitCode = 1;
6431
6442
  return;
6432
6443
  }
6433
- saveLink(cwd, key, { server });
6444
+ const previous = saveLink(cwd, key, { server });
6434
6445
  console.log(` Linked ${cwd} \u2192 ${who.projectName ?? who.project} (${who.project})`);
6446
+ if (previous) await retirePrevious(server, previous);
6447
+ if (opts.capture) await installCapture();
6448
+ else console.log(" \xB7 session capture is off \u2014 memories go through MCP. Add --capture to also record transcripts.");
6449
+ registerMcp(cwd, server, key);
6450
+ console.log("\n Start Claude Code. Nothing else to do.");
6451
+ }
6452
+ async function installCapture() {
6435
6453
  const hooks = await installHooks({ quiet: true });
6436
6454
  console.log(hooks.added > 0 ? " \u2713 capture hooks installed" : " \u2713 capture hooks already present");
6437
6455
  const health = hookHealth();
@@ -6446,6 +6464,8 @@ async function link(key, opts = {}) {
6446
6464
  if (pending > 0) {
6447
6465
  console.log(` ! ${pending} spooled event(s) still pending \u2014 run \`wsync flush --verbose\` to see why`);
6448
6466
  }
6467
+ }
6468
+ function registerMcp(cwd, server, key) {
6449
6469
  spawnSync("claude", ["mcp", "remove", "wsync", "--scope", "local"], { cwd, stdio: "ignore" });
6450
6470
  const mcp = spawnSync("claude", [
6451
6471
  "mcp",
@@ -6466,7 +6486,26 @@ async function link(key, opts = {}) {
6466
6486
  console.log(` claude mcp add --scope local --transport http wsync ${server}/mcp \\`);
6467
6487
  console.log(` --header "Authorization: Bearer ${key}"`);
6468
6488
  }
6469
- console.log("\n Start Claude Code. Nothing else to do.");
6489
+ }
6490
+ async function retirePrevious(server, previous) {
6491
+ let res;
6492
+ try {
6493
+ res = await fetch(`${server}/wsync/keys/retire`, {
6494
+ method: "POST",
6495
+ headers: { authorization: `Bearer ${previous}` },
6496
+ signal: AbortSignal.timeout(1e4)
6497
+ });
6498
+ } catch (e) {
6499
+ console.log(` ! could not retire the key this directory used before (${String(e)}) \u2014 revoke it in the app's Keys list`);
6500
+ return;
6501
+ }
6502
+ if (res.ok) {
6503
+ console.log(" \u2713 the key this directory used before is retired");
6504
+ } else if (res.status === 401) {
6505
+ console.log(" \xB7 the key this directory used before was already revoked");
6506
+ } else {
6507
+ console.log(` ! could not retire the key this directory used before (${res.status}) \u2014 revoke it in the app's Keys list`);
6508
+ }
6470
6509
  }
6471
6510
  var init_link = __esm({
6472
6511
  "src/commands/link.ts"() {
@@ -6478,6 +6517,61 @@ var init_link = __esm({
6478
6517
  }
6479
6518
  });
6480
6519
 
6520
+ // src/commands/connect.ts
6521
+ var connect_exports = {};
6522
+ __export(connect_exports, {
6523
+ connect: () => connect
6524
+ });
6525
+ import { hostname as osHostname, release, platform as platform2 } from "node:os";
6526
+ async function connect(code, opts = {}) {
6527
+ const cfg = loadConfig();
6528
+ const server = (opts.server ?? cfg.server).replace(/\/+$/, "");
6529
+ let res;
6530
+ try {
6531
+ res = await fetch(`${server}/wsync/connect`, {
6532
+ method: "POST",
6533
+ headers: { "content-type": "application/json" },
6534
+ body: JSON.stringify({ code, hostname: osHostname().trim() || "device", os: `${platform2()} ${release()}`, cliVersion: AGENT_VERSION }),
6535
+ signal: AbortSignal.timeout(15e3)
6536
+ });
6537
+ } catch (e) {
6538
+ console.error(`Could not reach ${server}: ${String(e)}`);
6539
+ process.exitCode = 1;
6540
+ return;
6541
+ }
6542
+ if (res.status === 401) {
6543
+ console.error("That code is not valid any more \u2014 codes work once and for 15 minutes. Make a new one on the workstream.");
6544
+ process.exitCode = 1;
6545
+ return;
6546
+ }
6547
+ if (res.status === 429) {
6548
+ console.error("Too many tries from this address \u2014 wait a minute and run it again.");
6549
+ process.exitCode = 1;
6550
+ return;
6551
+ }
6552
+ if (!res.ok) {
6553
+ console.error(`${server} answered ${res.status}.`);
6554
+ process.exitCode = 1;
6555
+ return;
6556
+ }
6557
+ const body = await res.json().catch(() => ({}));
6558
+ if (!body.key) {
6559
+ console.error(`${server} answered, but without a key. Check --server.`);
6560
+ process.exitCode = 1;
6561
+ return;
6562
+ }
6563
+ console.log(` Connected this device to ${body.workstream?.name ?? "the workstream"}.`);
6564
+ await link(body.key, { server, capture: opts.capture });
6565
+ }
6566
+ var init_connect = __esm({
6567
+ "src/commands/connect.ts"() {
6568
+ "use strict";
6569
+ init_config();
6570
+ init_version();
6571
+ init_link();
6572
+ }
6573
+ });
6574
+
6481
6575
  // src/discover.ts
6482
6576
  import { readdirSync as readdirSync2, existsSync as existsSync8, statSync as statSync3 } from "node:fs";
6483
6577
  import { join as join8, relative } from "node:path";
@@ -6567,7 +6661,7 @@ var backfill_exports = {};
6567
6661
  __export(backfill_exports, {
6568
6662
  backfill: () => backfill
6569
6663
  });
6570
- import { hostname as hostname2, platform as platform2 } from "node:os";
6664
+ import { hostname as hostname2, platform as platform3 } from "node:os";
6571
6665
  async function backfill(opts = {}) {
6572
6666
  const cfg = loadConfig();
6573
6667
  const found = discoverTranscripts();
@@ -6575,6 +6669,7 @@ async function backfill(opts = {}) {
6575
6669
  console.log(`discovered ${all.length} transcript(s) (${mb(all)}MB)`);
6576
6670
  let ingested = 0;
6577
6671
  let skipped = 0;
6672
+ let unfiled = 0;
6578
6673
  for (const f of all) {
6579
6674
  try {
6580
6675
  const d = await (0, import_wsync_core4.distillMechanically)(f.path, f.bytes);
@@ -6601,12 +6696,16 @@ async function backfill(opts = {}) {
6601
6696
  distillation: (0, import_wsync_core4.clampDistillation)(d),
6602
6697
  identity,
6603
6698
  machineHostname: hostname2(),
6604
- machineOs: platform2(),
6699
+ machineOs: platform3(),
6605
6700
  agentVersion: AGENT_VERSION,
6606
6701
  transcriptPath: f.path,
6607
6702
  transcriptShaPrefix: await (0, import_wsync_core4.transcriptPrefixSha)(f.path),
6608
6703
  // No `segment`: an absent segment tells the server this span is the
6609
- // whole file, not one tile of it.
6704
+ // whole file, not one tile of it. `mode` is what lets the remote choose
6705
+ // the project (among those that exist) instead of the key -- one key
6706
+ // imports a whole machine's history without funnelling it into one
6707
+ // project. Anything the remote does not match lands in triage.
6708
+ mode: "backfill",
6610
6709
  segmentKind: "backfill",
6611
6710
  isSidechain: f.isSidechain,
6612
6711
  parentSessionId: f.parentSessionId
@@ -6627,8 +6726,10 @@ async function backfill(opts = {}) {
6627
6726
  }
6628
6727
  }
6629
6728
  ingested++;
6729
+ if (body.projectId === null) unfiled++;
6630
6730
  if (opts.verbose) {
6631
- console.log(` [ok] ${d.sessionId.slice(0, 8)} ${identity.projectKey ?? "_unfiled"} (${identity.confidence.toFixed(2)})`);
6731
+ const where = body.projectId === null ? "-> triage (no project matches)" : "";
6732
+ console.log(` [ok] ${d.sessionId.slice(0, 8)} ${identity.projectKey ?? "_unfiled"} (${identity.confidence.toFixed(2)}) ${where}`.trimEnd());
6632
6733
  }
6633
6734
  } catch (e) {
6634
6735
  skipped++;
@@ -6636,7 +6737,10 @@ async function backfill(opts = {}) {
6636
6737
  }
6637
6738
  }
6638
6739
  console.log(`backfill: ${ingested} ingested, ${skipped} skipped, ${all.length} discovered`);
6639
- return { discovered: all.length, ingested, skipped };
6740
+ if (unfiled > 0) {
6741
+ console.log(` ${unfiled} session(s) matched no project and are waiting in triage. Create the project in the app, then file them from "Needs filing".`);
6742
+ }
6743
+ return { discovered: all.length, ingested, skipped, unfiled };
6640
6744
  }
6641
6745
  var import_wsync_core4, mb;
6642
6746
  var init_backfill = __esm({
@@ -6763,7 +6867,7 @@ var setup_exports = {};
6763
6867
  __export(setup_exports, {
6764
6868
  setup: () => setup
6765
6869
  });
6766
- import { hostname as osHostname } from "node:os";
6870
+ import { hostname as osHostname2 } from "node:os";
6767
6871
  import { resolve as resolve4 } from "node:path";
6768
6872
  async function setup(token, opts = {}) {
6769
6873
  const cfg = loadConfig();
@@ -6790,7 +6894,12 @@ async function setup(token, opts = {}) {
6790
6894
  }
6791
6895
  const result = await callTool(server, token, {
6792
6896
  name: "wsync_setup",
6793
- arguments: { projectKey, ...opts.name ? { name: opts.name } : {}, machine: opts.machine ?? hostname3() }
6897
+ arguments: {
6898
+ projectKey,
6899
+ ...opts.name ? { name: opts.name } : {},
6900
+ ...opts.create ? { create: true } : {},
6901
+ machine: opts.machine ?? hostname3()
6902
+ }
6794
6903
  });
6795
6904
  if (!result.ok) {
6796
6905
  console.error(result.message);
@@ -6806,7 +6915,7 @@ async function setup(token, opts = {}) {
6806
6915
  console.log(
6807
6916
  ` ${result.created ? "Created" : "Found"} project ${result.projectName ?? projectKey} (${result.projectKey ?? projectKey})`
6808
6917
  );
6809
- await link(key, { server });
6918
+ await link(key, { server, capture: opts.capture });
6810
6919
  }
6811
6920
  async function remoteProject(cwd) {
6812
6921
  const root = (0, import_wsync_core6.nearestGitRoot)(cwd);
@@ -6815,7 +6924,7 @@ async function remoteProject(cwd) {
6815
6924
  return remote ? (0, import_wsync_core6.normaliseRemote)(remote) : null;
6816
6925
  }
6817
6926
  function hostname3() {
6818
- return osHostname().trim() || "this machine";
6927
+ return osHostname2().trim() || "this machine";
6819
6928
  }
6820
6929
  async function callTool(server, token, params) {
6821
6930
  let res;
@@ -7272,6 +7381,8 @@ async function sendTile(cfg, key, entry, byteStart, byteEnd, proposedSeq, fileSi
7272
7381
  spoolDepth: listSpool().length,
7273
7382
  transcriptPath: entry.transcriptPath,
7274
7383
  transcriptShaPrefix: await (0, import_wsync_core3.transcriptPrefixSha)(entry.transcriptPath),
7384
+ // Live: the key names the project. Said explicitly, though absent means the same.
7385
+ mode: "live",
7275
7386
  segment: { seq: proposedSeq, kind: entry.trigger, byteStart, byteEnd }
7276
7387
  });
7277
7388
  const res = await fetch(apiUrl(cfg, "/wsync/ingest"), {
@@ -7342,9 +7453,17 @@ var val = (n) => {
7342
7453
  };
7343
7454
  var USAGE = `wsync \u2014 work context across machines
7344
7455
 
7345
- wsync setup <api-token> [--server URL] [--project KEY] [--machine NAME]
7456
+ wsync connect <code> [--server URL] [--capture]
7457
+ connect this directory to a workstream with the
7458
+ one-time code from "Connect a device" in the app
7459
+ wsync setup <api-token> [--server URL] [--project KEY] [--machine NAME] [--create] [--capture]
7346
7460
  one paste: mint this machine's key and link
7347
- wsync link <key> [--server URL] link this directory to a project
7461
+ (connects Claude Code to the project's memories
7462
+ over MCP; --capture also records transcripts)
7463
+ (--create opens a new project; without it an
7464
+ unknown project is refused, not invented)
7465
+ wsync link <key> [--server URL] [--capture]
7466
+ link this directory to a project
7348
7467
  wsync backfill [--limit N] [--key wsp_\u2026] [--verbose]
7349
7468
  import existing transcripts over HTTP
7350
7469
  wsync flush [--verbose] drain the offline spool to the server
@@ -7365,6 +7484,17 @@ var USAGE = `wsync \u2014 work context across machines
7365
7484
  `;
7366
7485
  try {
7367
7486
  switch (cmd) {
7487
+ case "connect": {
7488
+ const code = args[0];
7489
+ if (!code || code.startsWith("--")) {
7490
+ console.error('usage: wsync connect <code> [--server URL] [--capture] (the code comes from "Connect a device" on a workstream)');
7491
+ process.exitCode = 1;
7492
+ break;
7493
+ }
7494
+ const { connect: connect2 } = await Promise.resolve().then(() => (init_connect(), connect_exports));
7495
+ await connect2(code, { server: val("server"), capture: has("capture") });
7496
+ break;
7497
+ }
7368
7498
  case "link": {
7369
7499
  const key = args[0];
7370
7500
  if (!key || key.startsWith("--")) {
@@ -7373,7 +7503,7 @@ try {
7373
7503
  break;
7374
7504
  }
7375
7505
  const { link: link2 } = await Promise.resolve().then(() => (init_link(), link_exports));
7376
- await link2(key, { server: val("server") });
7506
+ await link2(key, { server: val("server"), capture: has("capture") });
7377
7507
  break;
7378
7508
  }
7379
7509
  case "backfill": {
@@ -7420,7 +7550,7 @@ try {
7420
7550
  console.log(`hooks ${health.installed} installed -> ${health.target}`);
7421
7551
  }
7422
7552
  console.log(`links ${links.length}`);
7423
- for (const [path, key] of links) console.log(` - ${path} -> ${key}`);
7553
+ for (const [path, key] of links) console.log(` - ${path} -> ${key.slice(0, 12)}\u2026`);
7424
7554
  console.log(`spool ${spool.length} pending`);
7425
7555
  for (const s of spool.slice(0, 10)) {
7426
7556
  const e = s.entry;
@@ -7442,7 +7572,9 @@ try {
7442
7572
  project: val("project"),
7443
7573
  name: val("name"),
7444
7574
  machine: val("machine"),
7445
- force: has("force")
7575
+ force: has("force"),
7576
+ create: has("create"),
7577
+ capture: has("capture")
7446
7578
  });
7447
7579
  break;
7448
7580
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unhuman/wsync-cli",
3
- "version": "0.1.2",
3
+ "version": "0.3.0",
4
4
  "description": "Capture Claude Code sessions to an Unhuman Work Context project: link a checkout, install the hooks, drain the spool.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {