@tyhld/conductor 0.12.147 → 0.12.148
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/selfcheck.js +80 -2
- package/package.json +2 -1
- package/sales-template/setup.sh +42 -3
- package/scripts/site-guard-local.py +350 -0
- package/scripts/update-guard.sh +400 -155
- package/skills/conductor-craftsman/SKILL.md +1 -1
package/dist/selfcheck.js
CHANGED
|
@@ -1486,6 +1486,82 @@ export function checkUnits(d) {
|
|
|
1486
1486
|
}
|
|
1487
1487
|
return item;
|
|
1488
1488
|
}
|
|
1489
|
+
/**
|
|
1490
|
+
* 現場の守りの置き場(便 9649ca05 / 52ae1d29)。★読むだけ・1バイトも書かない。
|
|
1491
|
+
* 配り直し(scripts/update-guard.sh ④)は守りを .claude/settings.local.json(リポに記録されない置き場)へ書き、
|
|
1492
|
+
* git が記録している .claude/settings.json には書かない。現場ごとに次の3つを見る:
|
|
1493
|
+
* ・正本(sales-template/settings.json)の禁止が settings.local.json に全部在る
|
|
1494
|
+
* ・記録されている settings.json が本流の版(HEAD)と違わない(前の版の書き込みが残っていない)
|
|
1495
|
+
* ・settings.local.json が記録されず、除外されている
|
|
1496
|
+
* ★合否には数えない(ok=null)。FAIL にすると、入れ替えの直後の自己診断が版を疑って戻すため。
|
|
1497
|
+
* そろっていない現場は人の確認として名前つきで出す(update-guard.sh の「★人の確認が要る現場」と同じ現場)。
|
|
1498
|
+
*/
|
|
1499
|
+
export function checkSiteGuard(d) {
|
|
1500
|
+
const item = { key: 'siteGuard', label: '現場の守りの置き場', ok: null, detail: [] };
|
|
1501
|
+
let canonical;
|
|
1502
|
+
try {
|
|
1503
|
+
const src = JSON.parse(readFileSync(path.join(d.distRoot, 'sales-template', 'settings.json'), 'utf8'));
|
|
1504
|
+
canonical = Array.isArray(src.permissions?.deny)
|
|
1505
|
+
? src.permissions.deny.filter((x) => typeof x === 'string')
|
|
1506
|
+
: [];
|
|
1507
|
+
}
|
|
1508
|
+
catch {
|
|
1509
|
+
note(item, '正本の禁止(sales-template/settings.json)を読めないので見ていません');
|
|
1510
|
+
return item;
|
|
1511
|
+
}
|
|
1512
|
+
const files = (siteSettingsFiles(d.home) ?? []).filter((f) => existsSync(f));
|
|
1513
|
+
if (files.length === 0) {
|
|
1514
|
+
note(item, '見る現場がありません(~/projects/*/.claude/settings.json が無い)');
|
|
1515
|
+
return item;
|
|
1516
|
+
}
|
|
1517
|
+
const git = (dir, ...args) => d.exec('git', ['-C', dir, ...args], { timeoutMs: 10_000 });
|
|
1518
|
+
const ready = [];
|
|
1519
|
+
for (const shared of files) {
|
|
1520
|
+
const dir = path.dirname(path.dirname(shared));
|
|
1521
|
+
const site = path.basename(dir);
|
|
1522
|
+
const local = path.join(path.dirname(shared), 'settings.local.json');
|
|
1523
|
+
const why = [];
|
|
1524
|
+
let deny = [];
|
|
1525
|
+
try {
|
|
1526
|
+
const j = JSON.parse(readFileSync(local, 'utf8'));
|
|
1527
|
+
if (Array.isArray(j.permissions?.deny))
|
|
1528
|
+
deny = j.permissions.deny;
|
|
1529
|
+
}
|
|
1530
|
+
catch {
|
|
1531
|
+
// 無い・読めない=禁止が1件も無いと同じ(下で数える)
|
|
1532
|
+
}
|
|
1533
|
+
const missing = canonical.filter((x) => !deny.includes(x));
|
|
1534
|
+
if (missing.length > 0)
|
|
1535
|
+
why.push(`正本の禁止が settings.local.json に ${missing.length} 件ありません`);
|
|
1536
|
+
const inRepo = git(dir, 'rev-parse', '--is-inside-work-tree');
|
|
1537
|
+
if (inRepo.status === 0 && inRepo.stdout.trim() === 'true') {
|
|
1538
|
+
if (git(dir, 'ls-files', '--error-unmatch', '--', '.claude/settings.local.json').status === 0) {
|
|
1539
|
+
why.push('settings.local.json を git が記録しています');
|
|
1540
|
+
}
|
|
1541
|
+
else if (git(dir, 'check-ignore', '-q', '--', '.claude/settings.local.json').status !== 0) {
|
|
1542
|
+
why.push('settings.local.json が除外されていません(記録に入るおそれ)');
|
|
1543
|
+
}
|
|
1544
|
+
if (git(dir, 'ls-files', '--error-unmatch', '--', '.claude/settings.json').status === 0) {
|
|
1545
|
+
const diff = git(dir, 'diff', '--quiet', 'HEAD', '--', '.claude/settings.json');
|
|
1546
|
+
if (diff.status === 1)
|
|
1547
|
+
why.push('記録されている settings.json が本流の版と違います(前の版の書き込みや人の変更が残っている)');
|
|
1548
|
+
else if (diff.status !== 0)
|
|
1549
|
+
why.push('settings.json を本流の版と比べられません');
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
else if (inRepo.status !== 0 && inRepo.status !== 128) {
|
|
1553
|
+
why.push('git の状態を読めません');
|
|
1554
|
+
}
|
|
1555
|
+
if (why.length === 0)
|
|
1556
|
+
ready.push(site);
|
|
1557
|
+
else
|
|
1558
|
+
note(item, `人の確認が要る: ${site}(${dir}): ${why.join('/')}`);
|
|
1559
|
+
}
|
|
1560
|
+
if (ready.length > 0)
|
|
1561
|
+
note(item, `守りが settings.local.json にそろっている現場: ${ready.join('・')}`);
|
|
1562
|
+
note(item, 'この項目は合否に数えません(そろっていない現場は人の確認です・APPLY=1 bash scripts/update-guard.sh の最後の一覧にも出ます)');
|
|
1563
|
+
return item;
|
|
1564
|
+
}
|
|
1489
1565
|
/** 項目ごとの合否(管制の checks)。見られなかった項目は skip。 */
|
|
1490
1566
|
export function checksOf(r) {
|
|
1491
1567
|
const out = {};
|
|
@@ -1644,6 +1720,8 @@ export async function runSelfcheck(d, trigger, opts = {}) {
|
|
|
1644
1720
|
const unsafe = checkUnsafe(d);
|
|
1645
1721
|
// +常駐の定義と部品置き場の鍵(★読むだけ・鍵の中身は読まない・便 2f984935)
|
|
1646
1722
|
const units = checkUnits(d);
|
|
1723
|
+
// +現場の守りの置き場(★読むだけ・合否に数えない・便 52ae1d29)
|
|
1724
|
+
const siteGuard = checkSiteGuard(d);
|
|
1647
1725
|
if (!probe) {
|
|
1648
1726
|
// ★Claude を使わない確かめだけ(お客さまの機の毎日の定期・測り直し・常駐の定義だけを入れ替えた回)。
|
|
1649
1727
|
// 小さな試験・MCP の確かめ・囲いの中からの書き込み・内側の通信・候補の版は、Claude を呼ぶ回にだけ見る。
|
|
@@ -1651,7 +1729,7 @@ export async function runSelfcheck(d, trigger, opts = {}) {
|
|
|
1651
1729
|
note(headless, 'お客さまの機の定期の確かめでは Claude を呼びません(版を入れ替えた直後の1回だけ流します・使用量の上限)');
|
|
1652
1730
|
note(sandbox, '囲いの中からの書き込みは、Claude を呼ぶ回(版を入れ替えた直後)にだけ確かめます');
|
|
1653
1731
|
note(network, '囲いの内側からの通信は、Claude を呼ぶ回(版を入れ替えた直後)にだけ確かめます');
|
|
1654
|
-
const items = [conductor, claude.item, central, sandbox, network, headless, guard, managed, unsafe, units];
|
|
1732
|
+
const items = [conductor, claude.item, central, sandbox, network, headless, guard, managed, unsafe, units, siteGuard];
|
|
1655
1733
|
return {
|
|
1656
1734
|
schema: 'conductor-selfcheck/1',
|
|
1657
1735
|
machine: d.machine,
|
|
@@ -1754,7 +1832,7 @@ export async function runSelfcheck(d, trigger, opts = {}) {
|
|
|
1754
1832
|
else
|
|
1755
1833
|
fail(network, 'fault', `囲いの内側から ${u} → ${c}`);
|
|
1756
1834
|
}
|
|
1757
|
-
const items = [conductor, claude.item, central, sandbox, network, headless, guard, managed, unsafe, units];
|
|
1835
|
+
const items = [conductor, claude.item, central, sandbox, network, headless, guard, managed, unsafe, units, siteGuard];
|
|
1758
1836
|
const humanActions = items.flatMap((i) => i.human ?? []);
|
|
1759
1837
|
const candidates = pickCandidates({ conductor: version, claude: claude.version }, { conductor: net.latest, claude: await fetchClaudeStable(d.fetchImpl) });
|
|
1760
1838
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tyhld/conductor",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.148",
|
|
4
4
|
"description": "采配くん管制の見守りアプリ(conductor-agent)。各PCで常駐し、中央(devlog-tracker)へ定期的に生存報告(heartbeat)を送る常駐CLI。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"scripts/update-guard.sh",
|
|
23
23
|
"scripts/deny-rule-shape.py",
|
|
24
24
|
"scripts/report-store-path.py",
|
|
25
|
+
"scripts/site-guard-local.py",
|
|
25
26
|
"scripts/managed-settings-audit.py",
|
|
26
27
|
"scripts/dist-source.sh",
|
|
27
28
|
"scripts/conductor-run.sh",
|
package/sales-template/setup.sh
CHANGED
|
@@ -1102,8 +1102,20 @@ fi
|
|
|
1102
1102
|
# 締め出す物の正本は @tyhld/conductor の src/proto-profile.ts(=dist/proto-profile.js)1本で、
|
|
1103
1103
|
# deny も deniedDomains もそこから生成される。ここに一覧を写さない(写しを2か所に持たない=
|
|
1104
1104
|
# 片方に足してもう片方を忘れる事故を構造的に起こせなくする)。
|
|
1105
|
+
#
|
|
1106
|
+
# ★置く先は .claude/settings.local.json(リポに記録されない置き場・便 52ae1d29/えふさん確定 2026-10-21)。
|
|
1107
|
+
# .claude/settings.json は多くの現場で git が記録しているので、そこへ守りを書くと本流の変更と
|
|
1108
|
+
# ぶつかって職人が着手前に止まる(2026-10-21 kenzokun・minoru)。Claude Code は settings.local.json を
|
|
1109
|
+
# settings.json より上の段で読み、deny などの一覧は足し合わせ=守りは弱まらない
|
|
1110
|
+
# (根拠と出典は scripts/site-guard-local.py の冒頭)。配り直し(update-guard.sh ④)も同じ置き場へ書く。
|
|
1111
|
+
# ・置く前に、記録されないこと(.gitignore・.git/info/exclude・全体の除外)を確かめ、外れていなければ
|
|
1112
|
+
# .git/info/exclude に1行足す(★見分けと除外の正本は site-guard-local.py の1か所)。
|
|
1113
|
+
# ・settings.local.json を git が記録している/除外を足せない現場には置かない(人の確認)。
|
|
1114
|
+
# ・.claude/settings.json が無ければ中身の無い {} だけを置く(配り直し・自己診断は settings.json を
|
|
1115
|
+
# 目印に現場を見つける)。在る settings.json には1バイトも触らない。
|
|
1105
1116
|
SETTINGS_SRC="$SETUP_DIST_ROOT/sales-template/settings.json"
|
|
1106
|
-
SETTINGS_DST="$CLAUDE_DIR/settings.json"
|
|
1117
|
+
SETTINGS_DST="$CLAUDE_DIR/settings.local.json"
|
|
1118
|
+
SITE_LOCAL_SRC="$SETUP_DIST_ROOT/scripts/site-guard-local.py"
|
|
1107
1119
|
PROTO_GEN="$SETUP_DIST_ROOT/dist/proto-profile.js"
|
|
1108
1120
|
if [[ "$PROFILE" == "proto" ]]; then
|
|
1109
1121
|
[[ -f "$PROTO_GEN" ]] || fail "試作の型の正本が見つかりません: $PROTO_GEN
|
|
@@ -1117,13 +1129,35 @@ if [[ "$PROFILE" == "proto" ]]; then
|
|
|
1117
1129
|
SETTINGS_SRC="$PROTO_SETTINGS"
|
|
1118
1130
|
echo "[setup] 型: proto(試作)— 本番の記録(DB)・鍵・送信・課金・本番の入口を締め出した設定を作りました。"
|
|
1119
1131
|
fi
|
|
1120
|
-
|
|
1132
|
+
SITE_LOCAL_OK=0
|
|
1133
|
+
SITE_LOCAL_MSG="守りの置き場の部品を読めません: $SITE_LOCAL_SRC"
|
|
1134
|
+
if [[ -f "$SETTINGS_SRC" && -f "$SITE_LOCAL_SRC" ]]; then
|
|
1135
|
+
# 記録されないことを確かめ、外れていれば .git/info/exclude に1行足す(★リポのファイルは変えない)。
|
|
1136
|
+
if SITE_LOCAL_MSG="$(PYTHONDONTWRITEBYTECODE=1 python3 - "$PROJECT_DIR" "$SITE_LOCAL_SRC" <<'PY'
|
|
1137
|
+
import importlib.util, sys
|
|
1138
|
+
proj, src = sys.argv[1], sys.argv[2]
|
|
1139
|
+
spec = importlib.util.spec_from_file_location("site_guard_local", src)
|
|
1140
|
+
sl = importlib.util.module_from_spec(spec)
|
|
1141
|
+
spec.loader.exec_module(sl)
|
|
1142
|
+
state, msg = sl.ensure_excluded(proj, sl.git_state(proj), True)
|
|
1143
|
+
print(msg)
|
|
1144
|
+
sys.exit(0 if state in ("not-repo", "ignored", "added") else 3)
|
|
1145
|
+
PY
|
|
1146
|
+
)"; then
|
|
1147
|
+
SITE_LOCAL_OK=1
|
|
1148
|
+
fi
|
|
1149
|
+
fi
|
|
1150
|
+
if [[ -f "$SETTINGS_SRC" && "$SITE_LOCAL_OK" != "1" ]]; then
|
|
1151
|
+
warn "現場の守り($SETTINGS_DST)を置きませんでした(人の確認): $SITE_LOCAL_MSG
|
|
1152
|
+
記録されている .claude/settings.json へは書きません。CC1 に伝えてください。"
|
|
1153
|
+
elif [[ -f "$SETTINGS_SRC" ]]; then
|
|
1154
|
+
[[ "$SITE_LOCAL_MSG" == "既に除外されています" ]] || echo "[setup] 記録しない置き場にしました($SITE_LOCAL_MSG)"
|
|
1121
1155
|
if [[ -f "$SETTINGS_DST" ]]; then
|
|
1122
1156
|
echo "[setup] $SETTINGS_DST は既に存在します。上書きしますか?"
|
|
1123
1157
|
setup_read overwrite " 上書き (y/N): "
|
|
1124
1158
|
# shellcheck disable=SC2154 # overwrite は setup_read で間接代入される
|
|
1125
1159
|
if [[ "$(lower "$overwrite")" != "y" ]]; then
|
|
1126
|
-
echo "[setup] 現場の settings.json をスキップしました。"
|
|
1160
|
+
echo "[setup] 現場の settings.local.json をスキップしました。"
|
|
1127
1161
|
else
|
|
1128
1162
|
cp -f "$SETTINGS_SRC" "$SETTINGS_DST"
|
|
1129
1163
|
echo "[setup] 配置: $SETTINGS_DST(上書き)"
|
|
@@ -1132,6 +1166,11 @@ if [[ -f "$SETTINGS_SRC" ]]; then
|
|
|
1132
1166
|
cp -f "$SETTINGS_SRC" "$SETTINGS_DST"
|
|
1133
1167
|
echo "[setup] 配置: $SETTINGS_DST"
|
|
1134
1168
|
fi
|
|
1169
|
+
# 配り直し・自己診断が現場を見つける目印(★在れば1バイトも触らない)。
|
|
1170
|
+
if [[ ! -e "$CLAUDE_DIR/settings.json" ]]; then
|
|
1171
|
+
printf '{}\n' > "$CLAUDE_DIR/settings.json"
|
|
1172
|
+
echo "[setup] 配置: $CLAUDE_DIR/settings.json(中身なし・現場の目印。守りは settings.local.json)"
|
|
1173
|
+
fi
|
|
1135
1174
|
|
|
1136
1175
|
# <PROJECT_DIR> をプロジェクトの絶対パスに置換
|
|
1137
1176
|
if [[ -f "$SETTINGS_DST" ]] && grep -q '<PROJECT_DIR>' "$SETTINGS_DST"; then
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""現場の守りの設定を「リポに記録されない置き場」へ書くための部品(置き場は1か所だけ・便 9649ca05)。
|
|
3
|
+
|
|
4
|
+
【なぜ要るか(実地 2026-10-21・kenzokun 便 3f92f618/minoru 便 cd42ace0)】
|
|
5
|
+
配り直し(scripts/update-guard.sh の ③④)は、各現場の .claude/settings.json に守り(禁止の一覧・
|
|
6
|
+
囲いの書いてよい場所など)を直接書いていた。このファイルは多くの現場で git が記録しているので、
|
|
7
|
+
本流でも同じファイルが変わると、職人は最新の本流から枝を切れず(git が上書きを断る)着手前に止まった。
|
|
8
|
+
|
|
9
|
+
【決め(えふさん確定 2026-10-21)】
|
|
10
|
+
守りの中身は1つも弱めずに、書き込む先を .claude/settings.local.json へ移す。
|
|
11
|
+
・Claude Code はこのファイルを .claude/settings.json より【上の段】で読む(優先の順:
|
|
12
|
+
管理者 > 起動の指定 > settings.local.json > settings.json > ホーム)。
|
|
13
|
+
・一覧(permissions.deny・sandbox の一覧)は段をまたいで【足し合わせ】になり、どの段の deny も
|
|
14
|
+
ほかの段の allow より先に効く(出典 https://code.claude.com/docs/en/settings の
|
|
15
|
+
「Lists merge instead of overriding」と /docs/en/permissions の「If a tool is denied at any
|
|
16
|
+
level, no other level can allow it」。確認日 2026-10-21)。=置き場を移しても守りは弱まらない。
|
|
17
|
+
・値1つのキー(allowUnsandboxedCommands・disableBypassPermissionsMode)は上の段が勝つので、
|
|
18
|
+
settings.local.json に書けば settings.json の値より優先される。
|
|
19
|
+
|
|
20
|
+
【ここが受け持つこと】(★書き換えの中身=何を足すかは update-guard.sh ④ が決める。ここは置き場の話だけ)
|
|
21
|
+
・git の状態を読む(記録されているか・除外されているか・本流と違うか)。
|
|
22
|
+
・settings.local.json が記録されないことを確かめ、されていなければ .git/info/exclude に1行足す
|
|
23
|
+
(★リポのファイル=.gitignore は変えない)。
|
|
24
|
+
・移行:settings.json の本流との差が「采配くんが書いた物だけ」かを見分け、守りを
|
|
25
|
+
settings.local.json へ移してから、控えを残して本流の版へ戻す。
|
|
26
|
+
★人や職人が書いた変更が混ざっている・見分けられないときは戻さない(人へ知らせる)。
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
import copy
|
|
30
|
+
import json
|
|
31
|
+
import os
|
|
32
|
+
import shutil
|
|
33
|
+
import subprocess
|
|
34
|
+
import time
|
|
35
|
+
|
|
36
|
+
__all__ = [
|
|
37
|
+
"SHARED", "LOCAL", "git_state", "ensure_excluded", "head_json", "classify",
|
|
38
|
+
"merge_carry", "restore", "is_ours_hook", "strip_our_hooks",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
SHARED = ".claude/settings.json"
|
|
42
|
+
LOCAL = ".claude/settings.local.json"
|
|
43
|
+
# ★除外の1行。控え(.bak.<日時>)と書きかけ(.tmp)も一緒に外す=git status に出さない。
|
|
44
|
+
_EXCLUDE_SUFFIX = ".claude/settings.local.json*"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _git(proj, *args):
|
|
48
|
+
"""git を読むだけ・書くだけで呼ぶ(★失敗しても例外にしない=台本を落とさない)。"""
|
|
49
|
+
try:
|
|
50
|
+
return subprocess.run(["git", "-C", proj, *args], capture_output=True, text=True, timeout=60)
|
|
51
|
+
except Exception:
|
|
52
|
+
return None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _ok(r):
|
|
56
|
+
return r is not None and r.returncode == 0
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def git_state(proj):
|
|
60
|
+
"""この現場の git の状態(★読むだけ)。
|
|
61
|
+
|
|
62
|
+
返す物:
|
|
63
|
+
repo … git のリポの中か
|
|
64
|
+
prefix … リポの根から現場までの道筋(根なら '')
|
|
65
|
+
shared_tracked… .claude/settings.json を git が記録しているか
|
|
66
|
+
local_tracked … .claude/settings.local.json を git が記録しているか(★記録されていたら書かない)
|
|
67
|
+
local_ignored … .claude/settings.local.json が除外されているか(.gitignore・info/exclude・全体の除外)
|
|
68
|
+
"""
|
|
69
|
+
st = {"repo": False, "prefix": "", "shared_tracked": False, "local_tracked": False, "local_ignored": False}
|
|
70
|
+
r = _git(proj, "rev-parse", "--is-inside-work-tree")
|
|
71
|
+
if not _ok(r) or r.stdout.strip() != "true":
|
|
72
|
+
return st
|
|
73
|
+
st["repo"] = True
|
|
74
|
+
p = _git(proj, "rev-parse", "--show-prefix")
|
|
75
|
+
st["prefix"] = p.stdout.strip() if _ok(p) else ""
|
|
76
|
+
st["shared_tracked"] = _ok(_git(proj, "ls-files", "--error-unmatch", "--", SHARED))
|
|
77
|
+
st["local_tracked"] = _ok(_git(proj, "ls-files", "--error-unmatch", "--", LOCAL))
|
|
78
|
+
st["local_ignored"] = _ok(_git(proj, "check-ignore", "-q", "--", LOCAL))
|
|
79
|
+
return st
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def ensure_excluded(proj, st, apply_mode):
|
|
83
|
+
"""settings.local.json がリポに記録されないことを確かめ、されていなければ info/exclude に足す。
|
|
84
|
+
|
|
85
|
+
返す物: (状態, 説明)。状態は
|
|
86
|
+
'not-repo' … git のリポではない(記録されることが無い)
|
|
87
|
+
'ignored' … 既に除外されている(何もしない)
|
|
88
|
+
'tracked' … ★記録されている=除外では外せない(書かずに人へ知らせる)
|
|
89
|
+
'would-add'… 見るだけの回(足す予定)
|
|
90
|
+
'added' … .git/info/exclude に1行足した
|
|
91
|
+
'failed' … 足せなかった(書かずに人へ知らせる)
|
|
92
|
+
★リポのファイル(.gitignore)は変えない。足すのは各自の手元だけに効く .git/info/exclude だけ。
|
|
93
|
+
"""
|
|
94
|
+
if not st["repo"]:
|
|
95
|
+
return "not-repo", "git のリポではないので記録されません"
|
|
96
|
+
if st["local_tracked"]:
|
|
97
|
+
return "tracked", f"{LOCAL} を git が記録しています(除外では外せません)"
|
|
98
|
+
if st["local_ignored"]:
|
|
99
|
+
return "ignored", "既に除外されています"
|
|
100
|
+
line = "/" + st["prefix"] + _EXCLUDE_SUFFIX
|
|
101
|
+
if not apply_mode:
|
|
102
|
+
return "would-add", f".git/info/exclude に {line} を足す予定です"
|
|
103
|
+
r = _git(proj, "rev-parse", "--git-path", "info/exclude")
|
|
104
|
+
if not _ok(r) or not r.stdout.strip():
|
|
105
|
+
return "failed", "除外の置き場(.git/info/exclude)を読めません"
|
|
106
|
+
path = r.stdout.strip()
|
|
107
|
+
if not os.path.isabs(path):
|
|
108
|
+
path = os.path.join(proj, path)
|
|
109
|
+
try:
|
|
110
|
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
111
|
+
body = ""
|
|
112
|
+
if os.path.exists(path):
|
|
113
|
+
with open(path, encoding="utf-8") as f:
|
|
114
|
+
body = f.read()
|
|
115
|
+
if line not in body.splitlines():
|
|
116
|
+
with open(path, "a", encoding="utf-8") as f:
|
|
117
|
+
if body and not body.endswith("\n"):
|
|
118
|
+
f.write("\n")
|
|
119
|
+
f.write("# 采配くん: 現場の守りの設定(記録しない・update-guard.sh が足した)\n")
|
|
120
|
+
f.write(line + "\n")
|
|
121
|
+
except OSError as e:
|
|
122
|
+
return "failed", f"除外の置き場に書けません({e})"
|
|
123
|
+
if not _ok(_git(proj, "check-ignore", "-q", "--", LOCAL)):
|
|
124
|
+
return "failed", "除外を足しても外れていません"
|
|
125
|
+
return "added", f".git/info/exclude に {line} を足しました"
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def head_json(proj):
|
|
129
|
+
"""本流の版(HEAD)の .claude/settings.json を読む。読めなければ None。"""
|
|
130
|
+
r = _git(proj, "show", "HEAD:./" + SHARED)
|
|
131
|
+
if not _ok(r):
|
|
132
|
+
return None
|
|
133
|
+
try:
|
|
134
|
+
return json.loads(r.stdout)
|
|
135
|
+
except Exception:
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def shared_dirty(proj):
|
|
140
|
+
"""settings.json が本流の版と違うか(作業場所・記録待ちのどちらか)。(違うか, 記録待ちに載っているか)"""
|
|
141
|
+
work = _git(proj, "diff", "--quiet", "HEAD", "--", SHARED)
|
|
142
|
+
staged = _git(proj, "diff", "--cached", "--quiet", "--", SHARED)
|
|
143
|
+
return (work is not None and work.returncode == 1), (staged is not None and staged.returncode == 1)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def is_ours_hook(entry, entrypoints):
|
|
147
|
+
"""番人の入口(cc2_guard/permission_request_hook/stop_hook…)を呼ぶ登録か(③ と同じ見分け方)。"""
|
|
148
|
+
if not isinstance(entry, dict):
|
|
149
|
+
return False
|
|
150
|
+
cmd = entry.get("command")
|
|
151
|
+
if not isinstance(cmd, str) or not cmd.strip():
|
|
152
|
+
return False
|
|
153
|
+
return os.path.basename(cmd.split()[-1]) in entrypoints
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def strip_our_hooks(hooks, entrypoints):
|
|
157
|
+
"""hooks から番人の登録だけを外した形(③ と同じ外し方・空になった節は消す)。"""
|
|
158
|
+
if not isinstance(hooks, dict):
|
|
159
|
+
return hooks
|
|
160
|
+
out = {}
|
|
161
|
+
for ev, groups in hooks.items():
|
|
162
|
+
if not isinstance(groups, list):
|
|
163
|
+
out[ev] = groups
|
|
164
|
+
continue
|
|
165
|
+
kept = []
|
|
166
|
+
for g in groups:
|
|
167
|
+
if not isinstance(g, dict):
|
|
168
|
+
kept.append(g)
|
|
169
|
+
continue
|
|
170
|
+
entries = g.get("hooks") or []
|
|
171
|
+
keep = [h for h in entries if not is_ours_hook(h, entrypoints)]
|
|
172
|
+
if keep:
|
|
173
|
+
ng = dict(g)
|
|
174
|
+
ng["hooks"] = keep
|
|
175
|
+
kept.append(ng)
|
|
176
|
+
elif not entries:
|
|
177
|
+
kept.append(g)
|
|
178
|
+
if kept:
|
|
179
|
+
out[ev] = kept
|
|
180
|
+
return out or None
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _list_diff(work, head, allowed, label, why, carry_key, carry):
|
|
184
|
+
"""一覧の差を見分ける:足された物が allowed の中だけなら説明が付く(足した物は carry へ)。"""
|
|
185
|
+
w = work if isinstance(work, list) else ([] if work is None else None)
|
|
186
|
+
h = head if isinstance(head, list) else ([] if head is None else None)
|
|
187
|
+
if w is None or h is None:
|
|
188
|
+
why.append(f"{label} が一覧ではない")
|
|
189
|
+
return
|
|
190
|
+
removed = [x for x in h if x not in w]
|
|
191
|
+
if removed:
|
|
192
|
+
why.append(f"{label} から消えた行がある: {', '.join(map(str, removed))}")
|
|
193
|
+
added = [x for x in w if x not in h]
|
|
194
|
+
odd = [x for x in added if x not in allowed]
|
|
195
|
+
if odd:
|
|
196
|
+
why.append(f"{label} に采配くんが配らない行がある: {', '.join(map(str, odd))}")
|
|
197
|
+
carry[carry_key] = [x for x in added if x in allowed]
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def classify(head, work, ctx):
|
|
201
|
+
"""settings.json の本流(head)との差が「采配くんが書いた物だけ」かを見分ける(★書き換えない)。
|
|
202
|
+
|
|
203
|
+
ctx: canonical_deny / retired / is_broken(関数) / domains / report_store / entrypoints
|
|
204
|
+
返す物:
|
|
205
|
+
ok … 差が全部、配り直し(④③)の書き方で説明が付く
|
|
206
|
+
why … 説明が付かない差(人や職人の変更・見分けられない物)
|
|
207
|
+
blockers … 戻すと守りが弱まる・二重になるので戻さない理由(本流の版そのものの事情)
|
|
208
|
+
carry … settings.local.json へ移す守り(deny / allowedDomains / allowWrite / unsandboxed / bypass)
|
|
209
|
+
"""
|
|
210
|
+
why, blockers = [], []
|
|
211
|
+
carry = {"deny": [], "domains": [], "write": [], "unsandboxed": False, "bypass": False}
|
|
212
|
+
if not isinstance(head, dict) or not isinstance(work, dict):
|
|
213
|
+
return {"ok": False, "why": ["JSON の形が読めない"], "blockers": [], "carry": carry}
|
|
214
|
+
w, h = copy.deepcopy(work), copy.deepcopy(head)
|
|
215
|
+
|
|
216
|
+
# --- permissions ---
|
|
217
|
+
wp, hp = w.pop("permissions", {}), h.pop("permissions", {})
|
|
218
|
+
if not isinstance(wp, dict) or not isinstance(hp, dict):
|
|
219
|
+
why.append("permissions の形が読めない")
|
|
220
|
+
wp, hp = {}, {}
|
|
221
|
+
# deny:足された行は「正本・退役の一覧・.npmrc の狙い撃ち」だけ/消えた行は「退役・何も止めていない形」だけ。
|
|
222
|
+
wd, hd = wp.pop("deny", []), hp.pop("deny", [])
|
|
223
|
+
if not isinstance(wd, list) or not isinstance(hd, list):
|
|
224
|
+
why.append("permissions.deny が一覧ではない")
|
|
225
|
+
else:
|
|
226
|
+
allowed = set(ctx["canonical_deny"]) | set(ctx["retired"]) | {"Read(/.npmrc)"}
|
|
227
|
+
added = [x for x in wd if x not in hd]
|
|
228
|
+
odd = [x for x in added if x not in allowed]
|
|
229
|
+
if odd:
|
|
230
|
+
why.append(f"禁止の一覧に采配くんが配らない行がある: {', '.join(map(str, odd))}")
|
|
231
|
+
removed = [x for x in hd if x not in wd]
|
|
232
|
+
bad_rm = [x for x in removed if x not in ctx["retired"] and not ctx["is_broken"](x)]
|
|
233
|
+
if bad_rm:
|
|
234
|
+
why.append(f"禁止の一覧から采配くんが外さない行が消えている: {', '.join(map(str, bad_rm))}")
|
|
235
|
+
# ★退役の行は移さない(意図して廃止した行)。それ以外の足された行は全部移す。
|
|
236
|
+
carry["deny"] = [x for x in added if x in allowed and x not in ctx["retired"]]
|
|
237
|
+
# allow:④ は全削除するだけ。作業場所の側に allow が在るのに本流と違う=人が足した。
|
|
238
|
+
wa, ha = wp.pop("allow", None), hp.pop("allow", None)
|
|
239
|
+
if wa is not None and wa != ha:
|
|
240
|
+
why.append("permissions.allow が本流と違う(采配くんは allow を足さない)")
|
|
241
|
+
if isinstance(ha, list) and ha:
|
|
242
|
+
blockers.append(f"本流の版に permissions.allow が {len(ha)} 件ある(戻すと関門の素通りが戻る)")
|
|
243
|
+
# 自動許可封じ:④ が正しいキーと値へ直す。
|
|
244
|
+
wb = (wp.pop("disableBypassPermissions", None), wp.pop("disableBypassPermissionsMode", None))
|
|
245
|
+
hb = (hp.pop("disableBypassPermissions", None), hp.pop("disableBypassPermissionsMode", None))
|
|
246
|
+
if wb != hb:
|
|
247
|
+
if wb == (None, "disable"):
|
|
248
|
+
carry["bypass"] = True
|
|
249
|
+
else:
|
|
250
|
+
why.append("自動許可封じのキーが本流と違う(采配くんの直し方ではない)")
|
|
251
|
+
if wp != hp:
|
|
252
|
+
why.append("permissions のほかのキーが本流と違う")
|
|
253
|
+
|
|
254
|
+
# --- hooks(③ が番人の登録を外すだけ) ---
|
|
255
|
+
wh, hh = w.pop("hooks", None), h.pop("hooks", None)
|
|
256
|
+
if wh != hh:
|
|
257
|
+
if strip_our_hooks(hh, ctx["entrypoints"]) == (wh or None):
|
|
258
|
+
blockers.append("本流の版に番人の登録がある(戻すとホームと二重に走る)")
|
|
259
|
+
else:
|
|
260
|
+
why.append("hooks が本流と違う(采配くんの外し方ではない)")
|
|
261
|
+
|
|
262
|
+
# --- sandbox(④ の (3c)・(3)・(3b) が足す・直すだけ。無い節は作らない) ---
|
|
263
|
+
ws, hs = w.pop("sandbox", None), h.pop("sandbox", None)
|
|
264
|
+
if ws != hs:
|
|
265
|
+
if not isinstance(ws, dict) or not isinstance(hs, dict):
|
|
266
|
+
why.append("sandbox の節が本流と違う(采配くんは節を作らない・消さない)")
|
|
267
|
+
else:
|
|
268
|
+
wn, hn = ws.pop("network", {}) or {}, hs.pop("network", {}) or {}
|
|
269
|
+
if isinstance(wn, dict) and isinstance(hn, dict):
|
|
270
|
+
_list_diff(wn.pop("allowedDomains", None), hn.pop("allowedDomains", None),
|
|
271
|
+
set(ctx["domains"]), "sandbox.network.allowedDomains", why, "domains", carry)
|
|
272
|
+
if wn != hn:
|
|
273
|
+
why.append("sandbox.network のほかのキーが本流と違う")
|
|
274
|
+
else:
|
|
275
|
+
why.append("sandbox.network の形が読めない")
|
|
276
|
+
wf, hf = ws.pop("filesystem", {}) or {}, hs.pop("filesystem", {}) or {}
|
|
277
|
+
if isinstance(wf, dict) and isinstance(hf, dict):
|
|
278
|
+
_list_diff(wf.pop("allowWrite", None), hf.pop("allowWrite", None),
|
|
279
|
+
set(ctx["report_store"]), "sandbox.filesystem.allowWrite", why, "write", carry)
|
|
280
|
+
if wf != hf:
|
|
281
|
+
why.append("sandbox.filesystem のほかのキーが本流と違う")
|
|
282
|
+
else:
|
|
283
|
+
why.append("sandbox.filesystem の形が読めない")
|
|
284
|
+
wu, hu = ws.pop("allowUnsandboxedCommands", None), hs.pop("allowUnsandboxedCommands", None)
|
|
285
|
+
if wu != hu:
|
|
286
|
+
if wu is False:
|
|
287
|
+
carry["unsandboxed"] = True
|
|
288
|
+
else:
|
|
289
|
+
why.append("sandbox.allowUnsandboxedCommands が本流と違う(采配くんは false にしかしない)")
|
|
290
|
+
if ws != hs:
|
|
291
|
+
why.append("sandbox のほかのキーが本流と違う")
|
|
292
|
+
|
|
293
|
+
if w != h:
|
|
294
|
+
keys = sorted(set(w) ^ set(h) | {k for k in set(w) & set(h) if w[k] != h[k]})
|
|
295
|
+
why.append(f"ほかのキーが本流と違う: {', '.join(keys)}")
|
|
296
|
+
return {"ok": not why, "why": why, "blockers": blockers, "carry": carry}
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def _append(lst, items):
|
|
300
|
+
added = [x for x in items if x not in lst]
|
|
301
|
+
lst.extend(added)
|
|
302
|
+
return added
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def merge_carry(local, carry):
|
|
306
|
+
"""移す守りを settings.local.json の中身(dict)へ足す(★足すだけ・消さない)。変更の説明を返す。"""
|
|
307
|
+
out = []
|
|
308
|
+
if carry["deny"]:
|
|
309
|
+
perms = local.setdefault("permissions", {})
|
|
310
|
+
deny = perms.setdefault("deny", [])
|
|
311
|
+
n = _append(deny, carry["deny"])
|
|
312
|
+
if n:
|
|
313
|
+
out.append(f"禁止ルール {len(n)} 件")
|
|
314
|
+
if carry["bypass"]:
|
|
315
|
+
perms = local.setdefault("permissions", {})
|
|
316
|
+
if perms.get("disableBypassPermissionsMode") != "disable":
|
|
317
|
+
perms["disableBypassPermissionsMode"] = "disable"
|
|
318
|
+
out.append('disableBypassPermissionsMode="disable"')
|
|
319
|
+
if carry["domains"] or carry["write"] or carry["unsandboxed"]:
|
|
320
|
+
sb = local.setdefault("sandbox", {})
|
|
321
|
+
if carry["domains"]:
|
|
322
|
+
n = _append(sb.setdefault("network", {}).setdefault("allowedDomains", []), carry["domains"])
|
|
323
|
+
if n:
|
|
324
|
+
out.append(f"接続許可 {len(n)} 件")
|
|
325
|
+
if carry["write"]:
|
|
326
|
+
n = _append(sb.setdefault("filesystem", {}).setdefault("allowWrite", []), carry["write"])
|
|
327
|
+
if n:
|
|
328
|
+
out.append(f"書いてよい場所 {len(n)} 件")
|
|
329
|
+
if carry["unsandboxed"] and sb.get("allowUnsandboxedCommands") is not False:
|
|
330
|
+
sb["allowUnsandboxedCommands"] = False
|
|
331
|
+
out.append("allowUnsandboxedCommands=false")
|
|
332
|
+
return out
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def restore(proj, shared_path):
|
|
336
|
+
"""控えを残してから、settings.json を本流の版へ戻す(git checkout -- .claude/settings.json)。
|
|
337
|
+
|
|
338
|
+
返す物: (戻せたか, 控えの道筋, 説明)。★控えが作れなければ戻さない。
|
|
339
|
+
★控えは settings.local.json* の名前で隣に置く(除外の1行に入る=git status に出さない)。
|
|
340
|
+
"""
|
|
341
|
+
backup = os.path.join(os.path.dirname(shared_path),
|
|
342
|
+
f"settings.local.json.bak-settings.json.{time.strftime('%Y%m%d-%H%M%S')}")
|
|
343
|
+
try:
|
|
344
|
+
shutil.copy2(shared_path, backup)
|
|
345
|
+
except OSError as e:
|
|
346
|
+
return False, None, f"控えを作れないので戻しません({e})"
|
|
347
|
+
r = _git(proj, "checkout", "--", SHARED)
|
|
348
|
+
if not _ok(r):
|
|
349
|
+
return False, backup, f"戻せませんでした({(r.stderr if r else '').strip() or 'git が使えない'})"
|
|
350
|
+
return True, backup, "本流の版へ戻しました"
|