@shgroup/dsh-serenity-hooks 1.16.6 → 1.16.7

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/dsh.plugin.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "dsh-serenity-hooks",
3
- "version": "1.16.6",
3
+ "version": "1.16.7",
4
4
  "main": "lib/index.js",
5
5
  "description": "宁静号 ACC harness(Native Cordis 插件):真实 DSH 工具 cc_fs/session/acc_msm/eap/neat/cce/loop + 拦截缝机械约束(safe-mode/路径守卫/会话落盘)。适配 DSH 公开版(0.1.0-rc,deepseek-ai/deepseek-harness)。私有(dsh-external 组织)。",
6
6
  "engines": {
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import z from "@deepseek-ai/schemastery";
2
2
  import { defineTool } from "@deepseek-ai/dsh-tools";
3
- import { appendFileSync, chmodSync, cpSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
3
+ import { appendFileSync, cpSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
4
4
  import { basename, dirname, join, relative, resolve } from "node:path";
5
5
  import { execFile, execFileSync, spawn, spawnSync } from "node:child_process";
6
6
  import { homedir, platform } from "node:os";
@@ -301,7 +301,7 @@ function runCcFs(root, args) {
301
301
  * 进程内注册(取代 v0.1 的 bash spawn runner):zod/schemastery 参数校验、
302
302
  * 规范 JSON 输出、纯 render 投影。逻辑在 fs-ops.ts(可单测)。
303
303
  */
304
- function agentCwd$6(exec) {
304
+ function agentCwd$7(exec) {
305
305
  return exec.agent?.session?.header?.cwd ?? process.cwd();
306
306
  }
307
307
  function renderText$9(value) {
@@ -359,7 +359,7 @@ const ccFsTool = defineTool({
359
359
  render: (args, value) => renderText$9(value)
360
360
  },
361
361
  async execute(args, exec) {
362
- const root = findSerenityRoot(agentCwd$6(exec));
362
+ const root = findSerenityRoot(agentCwd$7(exec));
363
363
  if (!root) throw new Error("No CCC found: no .serenity file from agent cwd");
364
364
  return runCcFs(root, args);
365
365
  }
@@ -983,7 +983,7 @@ function appendHeartbeat(sessionMd) {
983
983
  * AGENT_SESSIONS/ 全周期管理:list/show/create/health/qa/archive/summary。
984
984
  * 逻辑在 session-ops.ts(可单测)。
985
985
  */
986
- function agentCwd$5(exec) {
986
+ function agentCwd$6(exec) {
987
987
  return exec.agent?.session?.header?.cwd ?? process.cwd();
988
988
  }
989
989
  /** 当前 dsh 会话 id(use/close 按会话隔离的 scope) */
@@ -1024,7 +1024,7 @@ const sessionTool = defineTool({
1024
1024
  render: (args, value) => renderText$8(value)
1025
1025
  },
1026
1026
  async execute(args, exec) {
1027
- const root = findSerenityRoot(agentCwd$5(exec));
1027
+ const root = findSerenityRoot(agentCwd$6(exec));
1028
1028
  if (!root) throw new Error("No CCC found: no .serenity file from agent cwd");
1029
1029
  if (findEntry(root, "session-tool")) {
1030
1030
  const r = runMsm(root, {
@@ -1346,7 +1346,7 @@ async function runKit(root, args) {
1346
1346
  /**
1347
1347
  * kit.ts — acc_kit 真实 DSH 工具定义(defineTool)
1348
1348
  */
1349
- function agentCwd$4(exec) {
1349
+ function agentCwd$5(exec) {
1350
1350
  return exec.agent?.session?.header?.cwd ?? process.cwd();
1351
1351
  }
1352
1352
  function renderText$7(value) {
@@ -1375,12 +1375,284 @@ const kitTool = defineTool({
1375
1375
  render: (args, value) => renderText$7(value)
1376
1376
  },
1377
1377
  async execute(args, exec) {
1378
- const root = findSerenityRoot(agentCwd$4(exec));
1378
+ const root = findSerenityRoot(agentCwd$5(exec));
1379
1379
  if (!root) throw new Error("No CCC found: no .serenity file from agent cwd");
1380
1380
  return await runKit(root, args);
1381
1381
  }
1382
1382
  });
1383
1383
  //#endregion
1384
+ //#region src/localstore-ops.ts
1385
+ /**
1386
+ * localstore-ops.ts — localstore 纯逻辑层(零 DSH 依赖,可独立单测)
1387
+ *
1388
+ * S134 重设计(v1.16.7):存储从 ~/.serenity/ 迁到 **CCC 根根目录 localstore.json**
1389
+ * (JSON 格式——方便 MSM 直接 read + JSON.parse 读取,零解析依赖)。
1390
+ *
1391
+ * git 提交策略(可靠机制 × 用户自由):
1392
+ * - 配置:.dsh/serenity.json `localstore.gitTrack`: "allow"(可提交)| "deny"(禁提交)
1393
+ * - **缺省 deny(没配就是不提交)**;且 deny 的保证**不依赖 dsh 运行**——
1394
+ * 写入时自动确保 .gitignore 含 localstore.json(物理保证:即使 dsh 不在、
1395
+ * 用户手动 git commit 也不会误提交),cc_git 检查为第二道防线(拒绝 + 提示)
1396
+ * - allow:放行(文件可提交,用户自行管理 .gitignore)
1397
+ *
1398
+ * 存储结构(JSON 顶层分节):
1399
+ * { "credentials": { "HOME_GITLAB_TOKEN": "xxx" }, "<config节>": { "<key>": "v" } }
1400
+ * credentials 为保留节(credential 命名空间,key 大写蛇形);其余节归 config 命名空间
1401
+ * (path = section.key,如 loop.defaultModel)。
1402
+ */
1403
+ const LOCALSTORE_SCOPES = ["credential", "config"];
1404
+ /** 存储文件名(CCC 根根目录) */
1405
+ const LOCALSTORE_FILENAME = "localstore.json";
1406
+ /** 凭据保留节名(JSON 顶层) */
1407
+ const CREDENTIALS_SECTION = "credentials";
1408
+ /** 凭据 key 规范:大写蛇形,如 HOME_GITLAB_TOKEN */
1409
+ const CREDENTIAL_KEY_RE = /^[A-Z][A-Z0-9_]*$/;
1410
+ /** 配置节名规范:小写字母数字连字符 */
1411
+ const CONFIG_SECTION_RE = /^[a-z][a-z0-9-]*$/;
1412
+ /** 配置 key 规范(节内):小驼峰,如 defaultModel */
1413
+ const CONFIG_KEY_RE = /^[a-z][a-zA-Z0-9_]*$/;
1414
+ /** 存储文件绝对路径(CCC 根根目录) */
1415
+ function localstorePath(root) {
1416
+ return join(root, LOCALSTORE_FILENAME);
1417
+ }
1418
+ /** git 策略:serenity.json localstore.gitTrack,缺省 deny(不提交) */
1419
+ function readGitTrack(root, paths = DEFAULT_SERENITY_CONFIG_PATHS) {
1420
+ return loadSerenityConfig(root, paths).localstore?.gitTrack === "allow" ? "allow" : "deny";
1421
+ }
1422
+ /** .gitignore 是否已覆盖 localstore 文件(非空非注释行含文件名即算) */
1423
+ function isLocalstoreGitignored(root) {
1424
+ const gi = join(root, ".gitignore");
1425
+ if (!existsSync(gi)) return false;
1426
+ return readFileSync(gi, "utf-8").split("\n").some((l) => {
1427
+ const t = l.trim();
1428
+ return t !== "" && !t.startsWith("#") && t.includes("localstore.json");
1429
+ });
1430
+ }
1431
+ /**
1432
+ * 确保 .gitignore 含 localstore 文件(deny 时的物理保证,不依赖 dsh 运行):
1433
+ * 写入 localstore 时调用——deny(含缺省)且 .gitignore 未覆盖 → 自动追加一行。
1434
+ * allow → 不写入(放行,文件可提交)。
1435
+ */
1436
+ function ensureLocalstoreGitignored(root) {
1437
+ if (readGitTrack(root) === "allow") return { status: "allow" };
1438
+ if (isLocalstoreGitignored(root)) return { status: "ignored" };
1439
+ const gi = join(root, ".gitignore");
1440
+ const existing = existsSync(gi) ? readFileSync(gi, "utf-8") : "";
1441
+ const line = `${LOCALSTORE_FILENAME} # ACC localstore(localstore.gitTrack 缺省 deny,禁提交)`;
1442
+ writeFileSync(gi, (existing.endsWith("\n") || existing === "" ? existing : existing + "\n") + line + "\n", "utf-8");
1443
+ return { status: "appended" };
1444
+ }
1445
+ /**
1446
+ * cc_git 联动检查(第二道防线):文件存在 && deny && .gitignore 未覆盖 → 不通过。
1447
+ * 调用方(cc_git commit)据此拒绝提交;status 可输出 warning。
1448
+ */
1449
+ function checkLocalstoreGitCompliance(root) {
1450
+ if (!existsSync(localstorePath(root))) return { ok: true };
1451
+ if (readGitTrack(root) === "allow") return { ok: true };
1452
+ if (isLocalstoreGitignored(root)) return { ok: true };
1453
+ return {
1454
+ ok: false,
1455
+ reason: `localstore.json 禁止提交(localstore.gitTrack=deny 缺省)但 .gitignore 未包含——请将 ${LOCALSTORE_FILENAME} 加入 .gitignore(或改配置 localstore.gitTrack=allow 显式放行)`
1456
+ };
1457
+ }
1458
+ /** 读取全文件(顶层分节);文件不存在/坏 JSON 返回空 */
1459
+ function readAll(root) {
1460
+ const p = localstorePath(root);
1461
+ if (!existsSync(p)) return {};
1462
+ try {
1463
+ const v = JSON.parse(readFileSync(p, "utf-8"));
1464
+ if (v && typeof v === "object" && !Array.isArray(v)) return v;
1465
+ return {};
1466
+ } catch {
1467
+ return {};
1468
+ }
1469
+ }
1470
+ /** 写回全文件(2 空格缩进 + 尾换行,方便 MSM 直接读取) */
1471
+ function writeAll(root, data) {
1472
+ writeFileSync(localstorePath(root), JSON.stringify(data, null, 2) + "\n", "utf-8");
1473
+ }
1474
+ /** 读取命名空间全部条目:credential → 扁平;config → 分节(剔除 credentials 保留节) */
1475
+ function readStore(root, scope) {
1476
+ const all = readAll(root);
1477
+ if (scope === "credential") return all["credentials"] ?? {};
1478
+ const { [CREDENTIALS_SECTION]: _cred, ...rest } = all;
1479
+ return rest;
1480
+ }
1481
+ /** 校验凭据 key 合法(大写蛇形);不合法抛错 */
1482
+ function assertCredentialKey(key) {
1483
+ if (!CREDENTIAL_KEY_RE.test(key)) throw new Error(`credential key "${key}" 必须匹配大写蛇形 ^[A-Z][A-Z0-9_]*$(如 HOME_GITLAB_TOKEN)`);
1484
+ }
1485
+ /** 校验 config 路径(节.key);不合法抛错 */
1486
+ function assertConfigPath(path) {
1487
+ const idx = path.indexOf(".");
1488
+ if (idx <= 0 || idx === path.length - 1) throw new Error(`config path "${path}" 必须为 section.key(如 loop.defaultModel)`);
1489
+ const section = path.slice(0, idx);
1490
+ const key = path.slice(idx + 1);
1491
+ if (!CONFIG_SECTION_RE.test(section)) throw new Error(`config 节 "${section}" 必须匹配 ^[a-z][a-z0-9-]*$`);
1492
+ if (!CONFIG_KEY_RE.test(key)) throw new Error(`config key "${key}" 必须匹配小驼峰 ^[a-z][a-zA-Z0-9_]*$(如 defaultModel)`);
1493
+ return {
1494
+ section,
1495
+ key
1496
+ };
1497
+ }
1498
+ /** 写入单个条目(自动建文件;deny 默认时同步确保 .gitignore 物理保证) */
1499
+ function writeEntry(root, scope, name, value) {
1500
+ const all = readAll(root);
1501
+ if (scope === "credential") {
1502
+ assertCredentialKey(name);
1503
+ all[CREDENTIALS_SECTION] ??= {};
1504
+ all[CREDENTIALS_SECTION][name] = value;
1505
+ } else {
1506
+ const { section, key } = assertConfigPath(name);
1507
+ all[section] ??= {};
1508
+ all[section][key] = value;
1509
+ }
1510
+ writeAll(root, all);
1511
+ ensureLocalstoreGitignored(root);
1512
+ }
1513
+ /** 删除单个条目(不存在返回 false);空节自动移除 */
1514
+ function unsetEntry(root, scope, name) {
1515
+ const all = readAll(root);
1516
+ if (scope === "credential") {
1517
+ assertCredentialKey(name);
1518
+ const sec = all[CREDENTIALS_SECTION];
1519
+ if (!sec || !(name in sec)) return false;
1520
+ delete sec[name];
1521
+ if (Object.keys(sec).length === 0) delete all[CREDENTIALS_SECTION];
1522
+ } else {
1523
+ const { section, key } = assertConfigPath(name);
1524
+ const sec = all[section];
1525
+ if (!sec || !(key in sec)) return false;
1526
+ delete sec[key];
1527
+ if (Object.keys(sec).length === 0) delete all[section];
1528
+ }
1529
+ writeAll(root, all);
1530
+ return true;
1531
+ }
1532
+ /** 读取单个条目值;不存在返回 null */
1533
+ function getEntry(root, scope, name) {
1534
+ const all = readAll(root);
1535
+ if (scope === "credential") {
1536
+ assertCredentialKey(name);
1537
+ return all["credentials"]?.[name] ?? null;
1538
+ }
1539
+ const { section, key } = assertConfigPath(name);
1540
+ return all[section]?.[key] ?? null;
1541
+ }
1542
+ /** 列出 key(凭据只返回 key 名,不返回值) */
1543
+ function listKeys(root, scope) {
1544
+ const data = readStore(root, scope);
1545
+ if (scope === "credential") return Object.keys(data);
1546
+ return Object.entries(data).flatMap(([section, entries]) => Object.keys(entries).map((key) => `${section}.${key}`));
1547
+ }
1548
+ /**
1549
+ * doc 说明文本:输出存储位置/格式/key 规范/git 策略/读写方法。
1550
+ * agent 据此可直接用 fs 工具(read/write)自己读写凭据/配置。
1551
+ */
1552
+ function docText(root) {
1553
+ const path = localstorePath(root);
1554
+ return [
1555
+ "# localstore — ACC 本地凭据/配置存储(标准,S134 重设计)",
1556
+ "",
1557
+ "一个工具管理两个命名空间:credential(凭据)+ config(偏好)。",
1558
+ `存储于 CCC 根根目录 ${path}(JSON 格式,MSM 可直接 read + JSON.parse)。`,
1559
+ "",
1560
+ "## git 提交策略",
1561
+ "- 配置:.dsh/serenity.json `localstore.gitTrack`:`\"allow\"`(可提交)| `\"deny\"`(禁提交)",
1562
+ "- **缺省 deny(没配就是不提交)**;deny 时写入会自动确保 .gitignore 含 localstore.json",
1563
+ " (物理保证,不依赖 dsh 运行);cc_git commit 会检查拒绝",
1564
+ "- 想提交:配置 `\"localstore\": { \"gitTrack\": \"allow\" }`,并从 .gitignore 移除 localstore.json",
1565
+ "",
1566
+ "## 格式(JSON 顶层分节)",
1567
+ "```json",
1568
+ "{",
1569
+ " \"credentials\": {",
1570
+ " \"HOME_GITLAB_TOKEN\": \"xxx\"",
1571
+ " },",
1572
+ " \"loop\": {",
1573
+ " \"defaultModel\": \"minimax-cn-coding-plan/MiniMax-M3\"",
1574
+ " }",
1575
+ "}",
1576
+ "```",
1577
+ "- credentials 为保留节(凭据,key 大写蛇形);其余节 = config(path = section.key)",
1578
+ "",
1579
+ "## key 规范",
1580
+ "- credential key: ^[A-Z][A-Z0-9_]*$(如 HOME_GITLAB_TOKEN)",
1581
+ "- config path: section.key(节 ^[a-z][a-z0-9-]*$,key 小驼峰 ^[a-z][a-zA-Z0-9_]*$,如 loop.defaultModel)",
1582
+ "",
1583
+ "## 读取方法(agent 可直接用 fs 工具)",
1584
+ `- 用 read 工具读 ${path} → JSON.parse 解析`,
1585
+ "- 或用 localstore get <name> [--scope credential|config]",
1586
+ "",
1587
+ "## 写入方法",
1588
+ "- 推荐:localstore set <name> <value> [--scope ...](自动建文件/保留其他条目/联动 .gitignore)",
1589
+ "- 或直接用 write/edit 工具修改文件,保持 JSON 合法",
1590
+ "",
1591
+ "## 安全边界",
1592
+ "- list/show 对 credential 只返回 key 名,不返回值",
1593
+ "- 凭据值应在 agent 内部使用,不要写入对话/日志",
1594
+ "- 默认 deny:文件不会提交 git(.gitignore 物理保证 + cc_git 检查兜底)",
1595
+ ""
1596
+ ].join("\n");
1597
+ }
1598
+ /** 运行 localstore 操作(纯逻辑;返回 JSON 值供工具 render) */
1599
+ function runLocalStore(root, args) {
1600
+ const scope = args.scope === "config" ? "config" : "credential";
1601
+ switch (args.action) {
1602
+ case "list": return {
1603
+ scope,
1604
+ keys: listKeys(root, scope)
1605
+ };
1606
+ case "get": {
1607
+ if (!args.name) throw new Error("get 需要 name");
1608
+ const value = getEntry(root, scope, args.name);
1609
+ if (value === null) throw new Error(`not found: ${args.name}(scope=${scope})`);
1610
+ return {
1611
+ scope,
1612
+ name: args.name,
1613
+ value,
1614
+ source: scope
1615
+ };
1616
+ }
1617
+ case "set": {
1618
+ if (!args.name) throw new Error("set 需要 name");
1619
+ if (args.value === void 0) throw new Error("set 需要 value");
1620
+ writeEntry(root, scope, args.name, args.value);
1621
+ const git = checkLocalstoreGitCompliance(root);
1622
+ return {
1623
+ scope,
1624
+ name: args.name,
1625
+ set: true,
1626
+ path: localstorePath(root),
1627
+ gitTrack: readGitTrack(root),
1628
+ gitOk: git.ok,
1629
+ git: git.reason ? { warning: git.reason } : null
1630
+ };
1631
+ }
1632
+ case "unset": {
1633
+ if (!args.name) throw new Error("unset 需要 name");
1634
+ const removed = unsetEntry(root, scope, args.name);
1635
+ return {
1636
+ scope,
1637
+ name: args.name,
1638
+ removed
1639
+ };
1640
+ }
1641
+ case "show": {
1642
+ if (!args.name) throw new Error("show 需要 name");
1643
+ const exists = getEntry(root, scope, args.name) !== null;
1644
+ return {
1645
+ scope,
1646
+ name: args.name,
1647
+ exists,
1648
+ path: localstorePath(root)
1649
+ };
1650
+ }
1651
+ case "doc": return { doc: docText(root) };
1652
+ default: throw new Error(`未知子命令: ${args.action}(可用 list/get/set/unset/show/doc)`);
1653
+ }
1654
+ }
1655
+ //#endregion
1384
1656
  //#region src/git-ops.ts
1385
1657
  /**
1386
1658
  * git-ops.ts — cc_git 纯操作层(零 DSH 依赖)
@@ -1426,12 +1698,17 @@ function runGit(root, args) {
1426
1698
  "--porcelain"
1427
1699
  ]);
1428
1700
  if (!r.ok) throw new Error(`status 失败:${r.stderr.trim()}`);
1429
- return {
1701
+ const out = {
1430
1702
  clean: r.stdout.trim() === "",
1431
1703
  entries: r.stdout.trim() ? r.stdout.trim().split("\n") : []
1432
1704
  };
1705
+ const ls = checkLocalstoreGitCompliance(root);
1706
+ if (!ls.ok) out.warning = ls.reason;
1707
+ return out;
1433
1708
  }
1434
1709
  case "commit": {
1710
+ const ls = checkLocalstoreGitCompliance(root);
1711
+ if (!ls.ok) throw new Error(ls.reason);
1435
1712
  if (!args.message) throw new Error("commit 需要 message");
1436
1713
  const add = git(root, ["add", "-A"]);
1437
1714
  if (!add.ok) throw new Error(`git add 失败:${add.stderr.trim()}`);
@@ -1489,7 +1766,7 @@ function runGit(root, args) {
1489
1766
  /**
1490
1767
  * git.ts — cc_git 真实 DSH 工具定义(defineTool)
1491
1768
  */
1492
- function agentCwd$3(exec) {
1769
+ function agentCwd$4(exec) {
1493
1770
  return exec.agent?.session?.header?.cwd ?? process.cwd();
1494
1771
  }
1495
1772
  function renderText$6(value) {
@@ -1522,7 +1799,7 @@ const gitTool = defineTool({
1522
1799
  render: (args, value) => renderText$6(value)
1523
1800
  },
1524
1801
  async execute(args, exec) {
1525
- const root = findSerenityRoot(agentCwd$3(exec));
1802
+ const root = findSerenityRoot(agentCwd$4(exec));
1526
1803
  if (!root) throw new Error("No CCC found: no .serenity file from agent cwd");
1527
1804
  return runGit(root, args);
1528
1805
  }
@@ -1532,7 +1809,7 @@ const gitTool = defineTool({
1532
1809
  /**
1533
1810
  * msm.ts — acc_msm 真实 DSH 工具定义(defineTool)
1534
1811
  */
1535
- function agentCwd$2(exec) {
1812
+ function agentCwd$3(exec) {
1536
1813
  return exec.agent?.session?.header?.cwd ?? process.cwd();
1537
1814
  }
1538
1815
  function renderText$5(value) {
@@ -1582,7 +1859,7 @@ const msmTool = defineTool({
1582
1859
  render: (args, value) => renderText$5(value)
1583
1860
  },
1584
1861
  async execute(args, exec) {
1585
- const root = findSerenityRoot(agentCwd$2(exec));
1862
+ const root = findSerenityRoot(agentCwd$3(exec));
1586
1863
  if (!root) throw new Error("No CCC found: no .serenity file from agent cwd");
1587
1864
  return runMsmAsync(root, args);
1588
1865
  }
@@ -1926,7 +2203,7 @@ ${resumeNote}
1926
2203
  * 使 loop agent 继承发起方会话的 agent preset 工具(read/write/edit 等 preset 层工具)。
1927
2204
  * agentPresets 是可选服务——无 preset 装配的环境(无 roster 部署)退化为空工具层(历史行为)。
1928
2205
  */
1929
- function agentCwd$1(exec) {
2206
+ function agentCwd$2(exec) {
1930
2207
  return (exec.agent?.session)?.header?.cwd ?? process.cwd();
1931
2208
  }
1932
2209
  function renderText$1(value) {
@@ -1993,7 +2270,7 @@ function createLoopTool(ctx) {
1993
2270
  render: (_args, value) => renderText$1(value)
1994
2271
  },
1995
2272
  async execute(args, exec) {
1996
- const root = findSerenityRoot(agentCwd$1(exec));
2273
+ const root = findSerenityRoot(agentCwd$2(exec));
1997
2274
  if (!root) throw new Error("No CCC found: no .serenity file from agent cwd");
1998
2275
  const cfg = loadSerenityConfig(root, DEFAULT_SERENITY_CONFIG_PATHS);
1999
2276
  const model = args.model ?? cfg.loop?.defaultModel;
@@ -2109,307 +2386,13 @@ function createLoopTool(ctx) {
2109
2386
  });
2110
2387
  }
2111
2388
  //#endregion
2112
- //#region src/localstore-ops.ts
2113
- /**
2114
- * localstore-ops.ts — localstore 纯逻辑层(零 DSH 依赖,可独立单测)
2115
- *
2116
- * ACC 标准本地凭据/配置存储(S133 设计):
2117
- * - 一个工具管理两个命名空间:credential(凭据,0600)+ config(偏好,0644)
2118
- * - 存储于 ~/.serenity/(平台感知:win %USERPROFILE%\.serenity\)
2119
- * - 两个 YAML 文件:credentials.yaml(扁平 REF→value)+ settings.yaml(命名空间分节)
2120
- * - 目录 0700
2121
- *
2122
- * YAML 用轻量自实现子集(零依赖):扁平 `KEY: value` 映射 + `#` 注释。
2123
- * 凭据/配置本质是给 MSM/agent 用的普通 YAML 文件——agent 可按 doc 子命令
2124
- * 说明直接用 fs 工具(read/write)操作;本工具是管理入口 + 规范文档。
2125
- */
2126
- const LOCALSTORE_SCOPES = ["credential", "config"];
2127
- /** 根目录名(~/.serenity) */
2128
- const STORE_DIR_NAME = ".serenity";
2129
- /** 凭据文件(0600) */
2130
- const CREDENTIALS_FILENAME = "credentials.yaml";
2131
- /** 配置文件(0644) */
2132
- const SETTINGS_FILENAME = "settings.yaml";
2133
- /** 凭据 key 规范:大写蛇形(命名空间前缀),如 HOME_GITLAB_TOKEN */
2134
- const CREDENTIAL_KEY_RE = /^[A-Z][A-Z0-9_]*$/;
2135
- /** 配置节名规范:小写字母数字连字符 */
2136
- const CONFIG_SECTION_RE = /^[a-z][a-z0-9-]*$/;
2137
- /** 配置 key 规范(节内):小驼峰(首字母小写,可含大写),如 defaultModel */
2138
- const CONFIG_KEY_RE = /^[a-z][a-zA-Z0-9_]*$/;
2139
- /** 解析 ~/.serenity 根(平台感知:os.homedir() 三平台统一) */
2140
- function serenityDir() {
2141
- return join(homedir(), STORE_DIR_NAME);
2142
- }
2143
- /** 各命名空间的文件绝对路径 */
2144
- function storeFilePath(scope) {
2145
- return scope === "credential" ? join(serenityDir(), CREDENTIALS_FILENAME) : join(serenityDir(), SETTINGS_FILENAME);
2146
- }
2147
- /** 确保目录存在并设 0700 */
2148
- function ensureDir(dir) {
2149
- mkdirSync(dir, { recursive: true });
2150
- try {
2151
- chmodSync(dir, 448);
2152
- } catch {}
2153
- }
2154
- /** 设置文件权限:credential 0600 / config 0644 */
2155
- function applyFileMode(scope, path) {
2156
- try {
2157
- chmodSync(path, scope === "credential" ? 384 : 420);
2158
- } catch {}
2159
- }
2160
- /** 解析扁平映射 YAML → Record<string,string>(忽略 # 注释与空行;值去引号) */
2161
- function parseFlatYaml(text) {
2162
- const out = {};
2163
- for (const rawLine of text.split("\n")) {
2164
- const line = rawLine.trim();
2165
- if (line === "" || line.startsWith("#")) continue;
2166
- const idx = line.indexOf(":");
2167
- if (idx <= 0) continue;
2168
- const key = line.slice(0, idx).trim();
2169
- let value = line.slice(idx + 1).trim();
2170
- if (value.startsWith("\"") && value.endsWith("\"") || value.startsWith("'") && value.endsWith("'")) value = value.slice(1, -1);
2171
- out[key] = value;
2172
- }
2173
- return out;
2174
- }
2175
- /** 序列化扁平映射 YAML(保持 key 顺序,值引号包裹含特殊字符的) */
2176
- function renderFlatYaml(entries) {
2177
- const lines = [];
2178
- for (const [key, value] of Object.entries(entries)) {
2179
- const rendered = /[:#\n]/.test(value) || value === "" || /^\s/.test(value) || /\s$/.test(value) ? JSON.stringify(value) : value;
2180
- lines.push(`${key}: ${rendered}`);
2181
- }
2182
- return lines.join("\n") + (lines.length > 0 ? "\n" : "");
2183
- }
2184
- /** 解析分节 YAML(config):节 → key → value */
2185
- function parseSectionedYaml(text) {
2186
- const out = {};
2187
- let section = "";
2188
- for (const rawLine of text.split("\n")) {
2189
- const line = rawLine.trim();
2190
- if (line === "" || line.startsWith("#")) continue;
2191
- if (rawLine.length - rawLine.trimStart().length === 0 && line.endsWith(":")) {
2192
- section = line.slice(0, -1).trim();
2193
- if (!out[section]) out[section] = {};
2194
- continue;
2195
- }
2196
- if (section === "") continue;
2197
- const idx = line.indexOf(":");
2198
- if (idx <= 0) continue;
2199
- const key = line.slice(0, idx).trim();
2200
- let value = line.slice(idx + 1).trim();
2201
- if (value.startsWith("\"") && value.endsWith("\"") || value.startsWith("'") && value.endsWith("'")) value = value.slice(1, -1);
2202
- const sectionMap = out[section] ??= {};
2203
- sectionMap[key] = value;
2204
- }
2205
- return out;
2206
- }
2207
- /** 序列化分节 YAML(config):节 → key → value */
2208
- function renderSectionedYaml(sections) {
2209
- const lines = [];
2210
- for (const [section, entries] of Object.entries(sections)) {
2211
- lines.push(`${section}:`);
2212
- for (const [key, value] of Object.entries(entries)) {
2213
- const rendered = /[:#\n]/.test(value) || value === "" || /^\s/.test(value) || /\s$/.test(value) ? JSON.stringify(value) : value;
2214
- lines.push(` ${key}: ${rendered}`);
2215
- }
2216
- }
2217
- return lines.join("\n") + (lines.length > 0 ? "\n" : "");
2218
- }
2219
- /** 读取命名空间全部条目(文件不存在返回空) */
2220
- function readStore(scope) {
2221
- const path = storeFilePath(scope);
2222
- if (!existsSync(path)) return scope === "credential" ? {} : {};
2223
- const text = readFileSync(path, "utf-8");
2224
- return scope === "credential" ? parseFlatYaml(text) : parseSectionedYaml(text);
2225
- }
2226
- /** 校验凭据 key 合法(大写蛇形);不合法抛错 */
2227
- function assertCredentialKey(key) {
2228
- if (!CREDENTIAL_KEY_RE.test(key)) throw new Error(`credential key "${key}" 必须匹配大写蛇形 ^[A-Z][A-Z0-9_]*$(如 HOME_GITLAB_TOKEN)`);
2229
- }
2230
- /** 校验 config 路径(节.key);不合法抛错 */
2231
- function assertConfigPath(path) {
2232
- const idx = path.indexOf(".");
2233
- if (idx <= 0 || idx === path.length - 1) throw new Error(`config path "${path}" 必须为 section.key(如 loop.defaultModel)`);
2234
- const section = path.slice(0, idx);
2235
- const key = path.slice(idx + 1);
2236
- if (!CONFIG_SECTION_RE.test(section)) throw new Error(`config 节 "${section}" 必须匹配 ^[a-z][a-z0-9-]*$`);
2237
- if (!CONFIG_KEY_RE.test(key)) throw new Error(`config key "${key}" 必须匹配小驼峰 ^[a-z][a-zA-Z0-9_]*$(如 defaultModel)`);
2238
- return {
2239
- section,
2240
- key
2241
- };
2242
- }
2243
- /** 写入单个条目(自动建目录/设权限;保留其他条目与注释行外内容) */
2244
- function writeEntry(scope, name, value) {
2245
- const path = storeFilePath(scope);
2246
- ensureDir(serenityDir());
2247
- if (scope === "credential") {
2248
- assertCredentialKey(name);
2249
- const entries = parseFlatYaml(existsSync(path) ? readFileSync(path, "utf-8") : "");
2250
- entries[name] = value;
2251
- writeFileSync(path, renderFlatYaml(entries), "utf-8");
2252
- } else {
2253
- const { section, key } = assertConfigPath(name);
2254
- const sections = parseSectionedYaml(existsSync(path) ? readFileSync(path, "utf-8") : "");
2255
- if (!sections[section]) sections[section] = {};
2256
- sections[section][key] = value;
2257
- writeFileSync(path, renderSectionedYaml(sections), "utf-8");
2258
- }
2259
- applyFileMode(scope, path);
2260
- }
2261
- /** 删除单个条目(不存在返回 false) */
2262
- function unsetEntry(scope, name) {
2263
- const path = storeFilePath(scope);
2264
- if (!existsSync(path)) return false;
2265
- if (scope === "credential") {
2266
- assertCredentialKey(name);
2267
- const entries = parseFlatYaml(readFileSync(path, "utf-8"));
2268
- if (!(name in entries)) return false;
2269
- delete entries[name];
2270
- writeFileSync(path, renderFlatYaml(entries), "utf-8");
2271
- return true;
2272
- }
2273
- const { section, key } = assertConfigPath(name);
2274
- const sections = parseSectionedYaml(readFileSync(path, "utf-8"));
2275
- const sectionMap = sections[section];
2276
- if (!sectionMap || !(key in sectionMap)) return false;
2277
- delete sectionMap[key];
2278
- if (Object.keys(sectionMap).length === 0) delete sections[section];
2279
- writeFileSync(path, renderSectionedYaml(sections), "utf-8");
2280
- return true;
2281
- }
2282
- /** 读取单个条目值;不存在返回 null */
2283
- function getEntry(scope, name) {
2284
- const path = storeFilePath(scope);
2285
- if (!existsSync(path)) return null;
2286
- if (scope === "credential") {
2287
- assertCredentialKey(name);
2288
- return parseFlatYaml(readFileSync(path, "utf-8"))[name] ?? null;
2289
- }
2290
- const { section, key } = assertConfigPath(name);
2291
- return parseSectionedYaml(readFileSync(path, "utf-8"))[section]?.[key] ?? null;
2292
- }
2293
- /** 列出 key(凭据只返回 key 名,不返回值) */
2294
- function listKeys(scope) {
2295
- const data = readStore(scope);
2296
- if (scope === "credential") return Object.keys(data);
2297
- return Object.entries(data).flatMap(([section, entries]) => Object.keys(entries).map((key) => `${section}.${key}`));
2298
- }
2299
- /**
2300
- * doc 说明文本:输出存储位置/格式/key 规范/权限/读写方法/安全边界。
2301
- * agent 据此可直接用 fs 工具(read/write)自己读写凭据/配置。
2302
- */
2303
- function docText() {
2304
- const dir = serenityDir();
2305
- const credPath = storeFilePath("credential");
2306
- const cfgPath = storeFilePath("config");
2307
- return [
2308
- "# localstore — ACC 本地凭据/配置存储(标准)",
2309
- "",
2310
- "一个工具管理两个命名空间:credential(凭据,0600)+ config(偏好,0644)。",
2311
- "存储于用户主目录,不在任何 CCC git 仓库内。",
2312
- "",
2313
- "## 存储位置(平台感知)",
2314
- `- 根目录: ${dir} (0700)`,
2315
- `- 凭据: ${credPath} (0600)`,
2316
- `- 配置: ${cfgPath} (0644)`,
2317
- "- Windows: %USERPROFILE%\\.serenity\\...(与 Linux/macOS 的 $HOME/.serenity 同构)",
2318
- "",
2319
- "## 格式",
2320
- "credentials.yaml(扁平映射,key = 大写蛇形命名空间前缀):",
2321
- "```yaml",
2322
- "HOME_GITLAB_TOKEN: xxx",
2323
- "SSH_UBUNTU_PASSWORD: xxx",
2324
- "ANYSEARCH_API_KEY: xxx",
2325
- "```",
2326
- "settings.yaml(命名空间分节):",
2327
- "```yaml",
2328
- "loop:",
2329
- " defaultModel: minimax-cn-coding-plan/MiniMax-M3",
2330
- "ui:",
2331
- " theme: dark",
2332
- "```",
2333
- "",
2334
- "## key 规范",
2335
- "- credential key: ^[A-Z][A-Z0-9_]*$(如 HOME_GITLAB_TOKEN)",
2336
- "- config path: section.key(节 ^[a-z][a-z0-9-]*$,key 小驼峰 ^[a-z][a-zA-Z0-9_]*$,如 loop.defaultModel)",
2337
- "",
2338
- "## 读取方法(agent 可直接用 fs 工具)",
2339
- "- 用 read 工具读对应文件 → 按上面格式解析 YAML",
2340
- "- 或用 localstore get <name> [--scope credential|config]",
2341
- "",
2342
- "## 写入方法",
2343
- "- 推荐:localstore set <name> <value> [--scope ...](自动建目录/设权限/保留其他条目)",
2344
- "- 或直接用 write/edit 工具修改文件,保持 YAML 合法(键值冒号分隔)",
2345
- "",
2346
- "## 安全边界",
2347
- "- list/show 对 credential 只返回 key 名,不返回值",
2348
- "- 凭据值应在 agent 内部使用,不要写入对话/日志",
2349
- "- 文件权限不符(非 0600/0644)时 get/set 会提示 chmod 修复",
2350
- "- 文件在用户主目录,天然不进入任何 git 仓库",
2351
- ""
2352
- ].join("\n");
2353
- }
2354
- /** 运行 localstore 操作(纯逻辑;返回 JSON 值供工具 render) */
2355
- function runLocalStore(args) {
2356
- const scope = args.scope === "config" ? "config" : "credential";
2357
- switch (args.action) {
2358
- case "list": return {
2359
- scope,
2360
- keys: listKeys(scope)
2361
- };
2362
- case "get": {
2363
- if (!args.name) throw new Error("get 需要 name");
2364
- const value = getEntry(scope, args.name);
2365
- if (value === null) throw new Error(`not found: ${args.name}(scope=${scope})`);
2366
- return {
2367
- scope,
2368
- name: args.name,
2369
- value,
2370
- source: scope
2371
- };
2372
- }
2373
- case "set":
2374
- if (!args.name) throw new Error("set 需要 name");
2375
- if (args.value === void 0) throw new Error("set 需要 value");
2376
- writeEntry(scope, args.name, args.value);
2377
- return {
2378
- scope,
2379
- name: args.name,
2380
- set: true
2381
- };
2382
- case "unset": {
2383
- if (!args.name) throw new Error("unset 需要 name");
2384
- const removed = unsetEntry(scope, args.name);
2385
- return {
2386
- scope,
2387
- name: args.name,
2388
- removed
2389
- };
2390
- }
2391
- case "show": {
2392
- if (!args.name) throw new Error("show 需要 name");
2393
- const exists = getEntry(scope, args.name) !== null;
2394
- return {
2395
- scope,
2396
- name: args.name,
2397
- exists,
2398
- path: storeFilePath(scope)
2399
- };
2400
- }
2401
- case "doc": return { doc: docText() };
2402
- default: throw new Error(`未知子命令: ${args.action}(可用 list/get/set/unset/show/doc)`);
2403
- }
2404
- }
2405
- //#endregion
2406
2389
  //#region src/tools/localstore.ts
2407
2390
  /**
2408
2391
  * localstore.ts — localstore 真实 DSH 工具定义(defineTool)
2409
2392
  *
2410
- * ACC 标准本地凭据/配置存储(S133 设计)。进程内注册,零 DSH 依赖逻辑在
2411
- * localstore-ops.ts(可单测)。doc 子命令输出存储规范,agent 可直接用 fs
2412
- * 工具(read/write)自己读写。
2393
+ * ACC 标准本地凭据/配置存储(S133 设计,S134 重设计:CCC 根 localstore.json)。
2394
+ * 进程内注册,零 DSH 依赖逻辑在 localstore-ops.ts(可单测)。
2395
+ * doc 子命令输出存储规范,agent 可直接用 fs 工具(read/write)自己读写。
2413
2396
  */
2414
2397
  const ACTIONS = [
2415
2398
  "list",
@@ -2419,6 +2402,9 @@ const ACTIONS = [
2419
2402
  "show",
2420
2403
  "doc"
2421
2404
  ];
2405
+ function agentCwd$1(exec) {
2406
+ return exec.agent?.session?.header?.cwd ?? process.cwd();
2407
+ }
2422
2408
  function renderText(value) {
2423
2409
  return [{
2424
2410
  type: "text",
@@ -2427,7 +2413,7 @@ function renderText(value) {
2427
2413
  }
2428
2414
  const localstoreTool = defineTool({
2429
2415
  name: "localstore",
2430
- description: "ACC 标准本地凭据/配置存储:一个工具管理两个命名空间 credential(凭据,0600)与 config(本地偏好,0644)。存储于用户主目录 ~/.serenity/(平台感知,不在任何 git 仓库内)。子命令:list(列 key,凭据不返回值)/ get <name>(读值)/ set <name> <value>(写)/ unset <name>(删)/ show <name>(元数据,凭据不打印值)/ doc(输出存储规范——路径/格式/key 规范/权限,agent 可按说明直接用 read/write 操作文件)。默认 scope=credential;config 需传 --scope config(路径为 section.key,如 loop.defaultModel)。",
2416
+ description: "ACC 标准本地凭据/配置存储:一个工具管理两个命名空间 credential(凭据)与 config(本地偏好)。存储于 CCC 根根目录 localstore.json(JSON 格式,MSM 可直接读取)。git 策略:.dsh/serenity.json localstore.gitTrack(allow 可提交 / deny 禁提交,缺省 deny)——deny 时写入自动确保 .gitignore 含该文件(物理保证),cc_git commit 会检查拒绝。子命令:list(列 key,凭据不返回值)/ get <name>(读值)/ set <name> <value>(写)/ unset <name>(删)/ show <name>(元数据,凭据不打印值)/ doc(输出存储规范——路径/格式/key 规范/git 策略,agent 可按说明直接用 read/write 操作文件)。默认 scope=credential;config 需传 --scope config(路径为 section.key,如 loop.defaultModel)。",
2431
2417
  parameters: {
2432
2418
  action: {
2433
2419
  type: "string",
@@ -2453,8 +2439,10 @@ const localstoreTool = defineTool({
2453
2439
  schema: { type: "json" },
2454
2440
  render: (args, value) => renderText(value)
2455
2441
  },
2456
- async execute(args) {
2457
- return runLocalStore(args);
2442
+ async execute(args, exec) {
2443
+ const root = findSerenityRoot(agentCwd$1(exec));
2444
+ if (!root) throw new Error("No CCC found: no .serenity file from agent cwd");
2445
+ return runLocalStore(root, args);
2458
2446
  }
2459
2447
  });
2460
2448
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shgroup/dsh-serenity-hooks",
3
- "version": "1.16.6",
3
+ "version": "1.16.7",
4
4
  "description": "宁静号 ACC harness — Native Cordis 插件(DeepSeek Harness 运行时)。真实 DSH 工具注册(cc_fs/session/acc_msm 等 9 工具)+ 拦截缝机械约束(safe-mode/路径守卫/会话落盘)+ 系统提示词注入(ACC/CCE/Constraints/SKILL/Session 五块)。适配 DSH 公开版(deepseek-ai/deepseek-harness 0.1.0-rc)。",
5
5
  "license": "MIT",
6
6
  "repository": {