@alterset/alterspec-client 0.4.1 → 0.4.4

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.
package/dist/cli.js CHANGED
@@ -964,8 +964,8 @@ function renderContext(c) {
964
964
 
965
965
  // src/git.ts
966
966
  import { execFileSync as execFileSync3 } from "child_process";
967
- import { existsSync as existsSync4, realpathSync } from "fs";
968
- import { join as join4, resolve as resolve2 } from "path";
967
+ import { existsSync as existsSync4, lstatSync, readdirSync as readdirSync2, readlinkSync, realpathSync, symlinkSync } from "fs";
968
+ import { dirname as dirname2, isAbsolute as isAbsolute2, join as join4, relative, resolve as resolve2 } from "path";
969
969
 
970
970
  // src/task.ts
971
971
  var TRAILER = "Alterspec-Task";
@@ -1041,27 +1041,94 @@ function taskWorktree(repo, task, opts) {
1041
1041
  tryGit(repo, ["fetch", "--quiet", "origin", opts.main]);
1042
1042
  const base = `origin/${opts.main}`;
1043
1043
  if (opts.here) {
1044
- const top = git2(repo, ["rev-parse", "--show-toplevel"]);
1044
+ const top2 = git2(repo, ["rev-parse", "--show-toplevel"]);
1045
1045
  if (tryGit(repo, ["symbolic-ref", "--short", "HEAD"]) === branch)
1046
- return { path: top, branch, created: false };
1046
+ return { path: top2, branch, created: false };
1047
1047
  if (git2(repo, ["status", "--porcelain", "--untracked-files=no"]))
1048
1048
  throw new GitError(
1049
1049
  `This checkout has uncommitted work: commit or stash it before switching to ${branch}.`
1050
1050
  );
1051
1051
  if (branchExists(repo, branch)) {
1052
1052
  git2(repo, ["switch", "--quiet", branch]);
1053
- return { path: top, branch, created: false };
1053
+ return { path: top2, branch, created: false };
1054
1054
  }
1055
1055
  git2(repo, ["switch", "--quiet", "--no-track", "-c", branch, base]);
1056
- return { path: top, branch, created: true };
1056
+ return { path: top2, branch, created: true };
1057
1057
  }
1058
1058
  const path = resolve2(opts.worktreesDir, folderOf(task));
1059
1059
  const listed = git2(repo, ["worktree", "list", "--porcelain"]).split("\n").filter((l) => l.startsWith("worktree ")).map((l) => real(l.slice("worktree ".length)));
1060
- if (listed.includes(real(path))) return { path, branch, created: false };
1060
+ const top = git2(repo, ["rev-parse", "--show-toplevel"]);
1061
+ if (listed.includes(real(path))) {
1062
+ const bridges = bridgeTrackedLinks(top, path);
1063
+ return { path, branch, created: false, bridges, dangling: danglingLinks(path) };
1064
+ }
1061
1065
  if (branchExists(repo, branch)) git2(repo, ["worktree", "add", "--quiet", path, branch]);
1062
1066
  else git2(repo, ["worktree", "add", "--quiet", "--no-track", "-b", branch, path, base]);
1063
- return { path, branch, created: true };
1067
+ return {
1068
+ path,
1069
+ branch,
1070
+ created: true,
1071
+ links: mirrorIgnoredLinks(top, path),
1072
+ bridges: bridgeTrackedLinks(top, path),
1073
+ dangling: danglingLinks(path)
1074
+ };
1075
+ }
1076
+ var danglingLinks = (worktree) => readdirSync2(worktree, { withFileTypes: true }).filter((e) => e.isSymbolicLink() && !existsSync4(join4(worktree, e.name))).map((e) => e.name);
1077
+ function mirrorIgnoredLinks(top, worktree) {
1078
+ const links = readdirSync2(top, { withFileTypes: true }).filter((e) => e.isSymbolicLink()).map((e) => e.name);
1079
+ if (!links.length) return [];
1080
+ const ignored = new Set((tryGit(top, ["check-ignore", "--", ...links]) ?? "").split("\n").filter(Boolean));
1081
+ const at = real(worktree);
1082
+ return links.filter((name) => {
1083
+ if (!ignored.has(name) || present(join4(at, name))) return false;
1084
+ const target = readlinkSync(join4(top, name));
1085
+ symlinkSync(isAbsolute2(target) ? target : relative(at, resolve2(top, target)), join4(at, name));
1086
+ return true;
1087
+ });
1064
1088
  }
1089
+ function bridgeTrackedLinks(top, worktree) {
1090
+ const at = real(worktree);
1091
+ const bridges = [];
1092
+ for (const entry of readdirSync2(at, { withFileTypes: true })) {
1093
+ const link = join4(at, entry.name);
1094
+ if (!entry.isSymbolicLink() || existsSync4(link)) continue;
1095
+ const target = readlinkSync(link);
1096
+ const source = resolve2(top, target);
1097
+ if (isAbsolute2(target) || !existsSync4(source)) continue;
1098
+ let missing = resolve2(at, target);
1099
+ let from = source;
1100
+ while (!present(dirname2(missing))) {
1101
+ missing = dirname2(missing);
1102
+ from = dirname2(from);
1103
+ }
1104
+ const inside = (dir) => !relative(dir, missing).startsWith("..");
1105
+ const refuse = (why, fix) => new GitError(
1106
+ `${entry.name} in ${at} points to ${target}, which resolves from the checkout but not from the worktree, and ${why}: ${fix}.`
1107
+ );
1108
+ const untrack = "keep the link out of git (ignored, not committed), so the client links it into each worktree";
1109
+ if (inside(at) || inside(top))
1110
+ throw refuse(`the folder it misses, ${missing}, lies inside a checkout`, untrack);
1111
+ const parent = dirname2(missing);
1112
+ if (present(missing) || !existsSync4(parent) || realpathSync(parent) !== parent) {
1113
+ const blocking = present(missing) ? missing : parent;
1114
+ throw refuse(
1115
+ `${blocking} is in the way`,
1116
+ `remove it, or make it lead to ${present(missing) ? from : parent}`
1117
+ );
1118
+ }
1119
+ symlinkSync(relative(parent, from), missing);
1120
+ bridges.push(missing);
1121
+ }
1122
+ return bridges;
1123
+ }
1124
+ var present = (path) => {
1125
+ try {
1126
+ lstatSync(path);
1127
+ return true;
1128
+ } catch {
1129
+ return false;
1130
+ }
1131
+ };
1065
1132
  var real = (path) => existsSync4(path) ? realpathSync(path) : resolve2(path);
1066
1133
  var escape = (s) => s.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
1067
1134
  function mergedCommit(repo, task, main) {
@@ -1090,13 +1157,14 @@ function mergedCommit(repo, task, main) {
1090
1157
  }
1091
1158
 
1092
1159
  // src/install.ts
1093
- import { existsSync as existsSync6, mkdirSync as mkdirSync2, readFileSync as readFileSync5, readdirSync as readdirSync3, statSync, writeFileSync as writeFileSync4 } from "fs";
1094
- import { join as join6, relative } from "path";
1160
+ import { spawnSync } from "child_process";
1161
+ import { existsSync as existsSync6, mkdirSync as mkdirSync2, readFileSync as readFileSync5, readdirSync as readdirSync4, statSync, writeFileSync as writeFileSync4 } from "fs";
1162
+ import { join as join6, relative as relative2 } from "path";
1095
1163
  import { fileURLToPath } from "url";
1096
1164
 
1097
1165
  // ../src/install/installer.ts
1098
- import { existsSync as existsSync5, mkdirSync, readFileSync as readFileSync4, readdirSync as readdirSync2, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "fs";
1099
- import { dirname as dirname2, join as join5 } from "path";
1166
+ import { existsSync as existsSync5, mkdirSync, readFileSync as readFileSync4, readdirSync as readdirSync3, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "fs";
1167
+ import { dirname as dirname3, join as join5 } from "path";
1100
1168
 
1101
1169
  // ../src/install/hash.ts
1102
1170
  import { createHash as createHash2 } from "crypto";
@@ -1115,7 +1183,7 @@ function readManifest(root, path = MANIFEST_PATH) {
1115
1183
  }
1116
1184
  function write(root, path, content) {
1117
1185
  const full = join5(root, path);
1118
- mkdirSync(dirname2(full), { recursive: true });
1186
+ mkdirSync(dirname3(full), { recursive: true });
1119
1187
  writeFileSync3(full, content);
1120
1188
  }
1121
1189
  function install(root, plan, opts) {
@@ -1175,8 +1243,8 @@ function install(root, plan, opts) {
1175
1243
  if (sha256(readFileSync4(full, "utf8")) === hash) {
1176
1244
  rmSync2(full);
1177
1245
  result.removed.push(path);
1178
- const folder = dirname2(full);
1179
- if (emptiedFolder.test(folder) && readdirSync2(folder).length === 0)
1246
+ const folder = dirname3(full);
1247
+ if (emptiedFolder.test(folder) && readdirSync3(folder).length === 0)
1180
1248
  rmSync2(folder, { recursive: true });
1181
1249
  } else {
1182
1250
  result.conflicts.push(path);
@@ -1196,10 +1264,10 @@ var MCP_JSON = ".mcp.json";
1196
1264
  var MCP_SERVER = "dispatcher";
1197
1265
  var MCP_ENTRY = { command: "npx", args: ["@alterset/alterspec-client", "mcp"] };
1198
1266
  function walk(dir) {
1199
- return readdirSync3(dir).sort().flatMap((n) => statSync(join6(dir, n)).isDirectory() ? walk(join6(dir, n)) : [join6(dir, n)]);
1267
+ return readdirSync4(dir).sort().flatMap((n) => statSync(join6(dir, n)).isDirectory() ? walk(join6(dir, n)) : [join6(dir, n)]);
1200
1268
  }
1201
1269
  var assetFiles = (sub) => walk(join6(ASSETS_DIR, sub)).map((f) => ({
1202
- rel: relative(join6(ASSETS_DIR, sub), f).split("\\").join("/"),
1270
+ rel: relative2(join6(ASSETS_DIR, sub), f).split("\\").join("/"),
1203
1271
  content: readFileSync5(f, "utf8")
1204
1272
  }));
1205
1273
  function buildClientPlan(version) {
@@ -1271,6 +1339,36 @@ function writeClaudeSettings(root) {
1271
1339
  writeFileSync4(path, after);
1272
1340
  return before === void 0 ? "created" : "updated";
1273
1341
  }
1342
+ var CLAUDE_LOCAL_SETTINGS = ".claude/settings.local.json";
1343
+ function writeLocalSettings(root) {
1344
+ const path = join6(root, CLAUDE_LOCAL_SETTINGS);
1345
+ const before = existsSync6(path) ? readFileSync5(path, "utf8") : void 0;
1346
+ let data = {};
1347
+ if (before !== void 0)
1348
+ try {
1349
+ data = JSON.parse(before);
1350
+ } catch {
1351
+ throw new ConfigError(`${path} is not valid JSON: fix it, then run init again.`);
1352
+ }
1353
+ const enabled = Array.isArray(data.enabledMcpjsonServers) ? data.enabledMcpjsonServers : [];
1354
+ if (before !== void 0 && enabled.includes(MCP_SERVER)) return "unchanged";
1355
+ data.enabledMcpjsonServers = [...enabled, MCP_SERVER];
1356
+ mkdirSync2(join6(root, ".claude"), { recursive: true });
1357
+ writeFileSync4(path, JSON.stringify(data, null, 2) + "\n");
1358
+ return before === void 0 ? "created" : "updated";
1359
+ }
1360
+ function ignoreLocalSettings(root) {
1361
+ const git4 = (...args) => spawnSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).status;
1362
+ if (git4("ls-files", "--error-unmatch", CLAUDE_LOCAL_SETTINGS) === 0) return "tracked";
1363
+ const status = git4("check-ignore", "-q", CLAUDE_LOCAL_SETTINGS);
1364
+ if (status === 0) return "ignored";
1365
+ if (status !== 1) return "unknown";
1366
+ const path = join6(root, ".gitignore");
1367
+ const before = existsSync6(path) ? readFileSync5(path, "utf8") : "";
1368
+ writeFileSync4(path, `${before}${before && !before.endsWith("\n") ? "\n" : ""}${CLAUDE_LOCAL_SETTINGS}
1369
+ `);
1370
+ return "added";
1371
+ }
1274
1372
  function initClient(root, opts, version) {
1275
1373
  const { force, ...given } = opts;
1276
1374
  const existing = readConfigAt(root);
@@ -1287,6 +1385,8 @@ function initClient(root, opts, version) {
1287
1385
  if (config !== "unchanged") writeConfig(root, parsed.data);
1288
1386
  const mcp = writeMcpJson(root);
1289
1387
  const hook = writeClaudeSettings(root);
1388
+ const ignore = ignoreLocalSettings(root);
1389
+ const approval = writeLocalSettings(root);
1290
1390
  const files = install(root, buildClientPlan(version), {
1291
1391
  mode: "update",
1292
1392
  force,
@@ -1294,7 +1394,7 @@ function initClient(root, opts, version) {
1294
1394
  manifestPath: MANIFEST_PATH2,
1295
1395
  emptiedFolder: /\.claude\/skills\/alterspec-[^/]+$/
1296
1396
  });
1297
- return { config, mcp, hook, files };
1397
+ return { config, mcp, hook, approval, ignore, files };
1298
1398
  }
1299
1399
 
1300
1400
  // src/mcp.ts
@@ -1593,6 +1693,11 @@ var run = (fn) => async (...args) => {
1593
1693
  process.exit(1);
1594
1694
  }
1595
1695
  };
1696
+ var ignoreLines = (o) => o === "added" ? [`Added ${CLAUDE_LOCAL_SETTINGS} to .gitignore; commit that too.`] : o === "tracked" ? [
1697
+ `${CLAUDE_LOCAL_SETTINGS} is committed, so Claude Code ignores its approval: \`git rm --cached\` it and ignore it.`
1698
+ ] : o === "unknown" ? [
1699
+ `Make sure git ignores ${CLAUDE_LOCAL_SETTINGS} (where .claude is a symlink, in the repository it points to).`
1700
+ ] : [];
1596
1701
  var program = new Command();
1597
1702
  program.name("alterspec-client").description("alterspec client \u2014 takes tasks of an alterspec export from the dispatcher.").version(packageVersion());
1598
1703
  program.command("init").description(
@@ -1609,10 +1714,13 @@ program.command("init").description(
1609
1714
  `${CONFIG_FILE}: ${r.config}`,
1610
1715
  `${MCP_JSON} (server "dispatcher"): ${r.mcp}`,
1611
1716
  `${CLAUDE_SETTINGS} (progress hook after git commit): ${r.hook}`,
1717
+ `${CLAUDE_LOCAL_SETTINGS} (the "dispatcher" server approved for you): ${r.approval}`,
1612
1718
  `Skills and ${PROMPTS_DIR}/: ${f.created.length} created, ${f.updated.length} updated, ${f.unchanged.length} unchanged${f.removed.length ? `, ${f.removed.length} removed` : ""}.`,
1613
1719
  ...f.conflicts.map((p) => `Kept your modified ${p}; --force replaces it.`),
1614
1720
  "",
1615
- "Commit .mcp.json, .claude/settings.json, .claude/skills/alterspec-* and .alterspec-client/, so task worktrees have them too."
1721
+ "Commit .mcp.json, .claude/settings.json, .claude/skills/alterspec-* and .alterspec-client/, so task worktrees have them too.",
1722
+ ...ignoreLines(r.ignore),
1723
+ "The dispatcher's tools appear in the next Claude Code session."
1616
1724
  ];
1617
1725
  try {
1618
1726
  const key = keyOf({ root, config: readConfigAt(root) });
@@ -1695,6 +1803,12 @@ program.command("worktree").description("create the task\u2019s branch task/<chg
1695
1803
  worktreesDir: worktreesDir(loaded),
1696
1804
  here: opts.here
1697
1805
  });
1806
+ if (wt.dangling?.length)
1807
+ process.stderr.write(
1808
+ `In ${wt.path}, ${wt.dangling.join(", ")} ${wt.dangling.length === 1 ? "points" : "point"} nowhere. A symlink git tracks is checked out as it is, so a relative target made for the checkout doesn't resolve from the worktree: keep such links out of git (ignored, not committed) and the client links them into each worktree, or fix them there.
1809
+ `
1810
+ );
1811
+ if (!opts.here && !wt.dangling?.includes(".claude")) writeLocalSettings(wt.path);
1698
1812
  process.stdout.write(JSON.stringify(wt, null, 2) + "\n");
1699
1813
  })
1700
1814
  );