@codewizard-dt/bootstrap 2.13.0 → 2.15.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 (38) hide show
  1. package/lib/hooks/README.md +592 -12
  2. package/lib/hooks/absolute-path-guard.js +142 -0
  3. package/lib/hooks/claude-settings-guard.js +235 -0
  4. package/lib/hooks/env-content-read-guard.js +367 -0
  5. package/lib/hooks/git-protected-ops-block.js +12 -7
  6. package/lib/hooks/interpreter-indirection-guard.js +576 -0
  7. package/lib/hooks/lib/command-parse.js +63 -0
  8. package/lib/hooks/package-install-consent.js +151 -0
  9. package/lib/hooks/protected-write-guard.js +237 -0
  10. package/lib/scripts/README.md +62 -5
  11. package/lib/scripts/build-mcp-guide.sh +9 -1
  12. package/lib/scripts/install-global.sh +25 -2
  13. package/lib/scripts/install-mcps.sh +140 -36
  14. package/lib/scripts/lib.sh +11 -7
  15. package/lib/scripts/merge-gitignore.sh +242 -33
  16. package/lib/scripts/merge-settings-deny.js +112 -39
  17. package/lib/scripts/templates/file-suggestion.sh +142 -0
  18. package/lib/scripts/templates/gitignore +8 -0
  19. package/lib/scripts/templates/guides/stubs/playwright.md +2 -0
  20. package/lib/scripts/templates/settings-deny.json +81 -1
  21. package/lib/skills/debug-logs/SKILL.md +1 -1
  22. package/lib/skills/git-commit/SKILL.md +8 -0
  23. package/package.json +5 -2
  24. package/raw/research/brave-mcp-single-docker-container/index.md +0 -94
  25. package/raw/research/brave-mcp-single-docker-container/sources.md +0 -70
  26. package/raw/research/dark-mode-theme/index.md +0 -85
  27. package/raw/research/dark-mode-theme/sources.md +0 -49
  28. package/raw/research/mcp-add-scope-writes/index.md +0 -46
  29. package/raw/research/mcp-add-scope-writes/sources.md +0 -38
  30. package/raw/research/mcp-one-process-per-user/index.md +0 -75
  31. package/raw/research/mcp-one-process-per-user/sources.md +0 -66
  32. package/raw/research/mcp-scope-conflict-handling/index.md +0 -61
  33. package/raw/research/mcp-scope-conflict-handling/sources.md +0 -32
  34. package/raw/research/serena-mcp-disconnect/index.md +0 -81
  35. package/raw/research/serena-mcp-disconnect/sources.md +0 -87
  36. package/raw/research/wiki-tooling-improvements/index.md +0 -126
  37. package/raw/research/wiki-tooling-improvements/sources.md +0 -73
  38. package/raw/software-factory-case-study.pdf +0 -0
@@ -34,10 +34,12 @@ BRAVE_MCP_URL="http://127.0.0.1:${BRAVE_MCP_PORT}/mcp"
34
34
  # server is started with --host 127.0.0.1) — 127.0.0.1 URLs get 403 "Access is
35
35
  # only allowed at localhost:<port>". Found by TASK-025 runtime UAT.
36
36
  PLAYWRIGHT_MCP_URL="http://localhost:${PLAYWRIGHT_MCP_PORT}/mcp"
37
- # The bootstrap-managed server registers under its own name so it can coexist
38
- # with a project-scoped `playwright` entry in a team's committed .mcp.json —
39
- # we never edit that file; a distinct name avoids [Conflicting scopes] entirely.
40
- PLAYWRIGHT_MCP_NAME="${PLAYWRIGHT_MCP_NAME:-playwright-shared}"
37
+ # Default server name. When a project already ships its own `playwright`
38
+ # registration, the interactive conflict flow (see _install_playwright_flow)
39
+ # may register ours under the alternate name below instead — a team's
40
+ # committed .mcp.json is never edited.
41
+ PLAYWRIGHT_MCP_NAME="${PLAYWRIGHT_MCP_NAME:-playwright}"
42
+ PLAYWRIGHT_MCP_ALT_NAME="${PLAYWRIGHT_MCP_ALT_NAME:-playwright-shared}"
41
43
 
42
44
  # mcp_installed, serena_installed, prompt_yn, prompt_scope live in lib.sh.
43
45
 
@@ -178,9 +180,11 @@ _playwright_bootstrap_agent() {
178
180
  }
179
181
 
180
182
  _add_playwright() {
183
+ # $1 = scope, $2 = server name (defaults to the canonical name)
184
+ local reg_name="${2:-$PLAYWRIGHT_MCP_NAME}"
181
185
  if [ "$(uname -s)" != "Darwin" ]; then
182
186
  # Non-macOS: per-session stdio server (shared-server wiring is macOS-only for now).
183
- mcp_add_scoped "$1" "$PLAYWRIGHT_MCP_NAME" -- npx @playwright/mcp@latest
187
+ mcp_add_scoped "$1" "$reg_name" -- npx @playwright/mcp@latest
184
188
  return 0
185
189
  fi
186
190
  # macOS: one shared HTTP server per machine, run as a launchd LaunchAgent in
@@ -257,11 +261,10 @@ PLIST
257
261
  if [ "$rc" -eq 2 ]; then
258
262
  return 0
259
263
  fi
260
- # Always user scope, under our own name — coexists with any project-scoped
261
- # `playwright` a team ships in .mcp.json.
262
- mcp_add_scoped user "$PLAYWRIGHT_MCP_NAME" --transport http "$PLAYWRIGHT_MCP_URL"
263
- wait_http_up "$PLAYWRIGHT_MCP_URL" && echo " $PLAYWRIGHT_MCP_NAME: listening on $PLAYWRIGHT_MCP_URL" \
264
- || { echo " WARNING: $PLAYWRIGHT_MCP_NAME endpoint not answering — diagnostics:"; \
264
+ # Always user scope for the shared HTTP server.
265
+ mcp_add_scoped user "$reg_name" --transport http "$PLAYWRIGHT_MCP_URL"
266
+ wait_http_up "$PLAYWRIGHT_MCP_URL" && echo " $reg_name: listening on $PLAYWRIGHT_MCP_URL" \
267
+ || { echo " WARNING: $reg_name endpoint not answering — diagnostics:"; \
265
268
  launchctl print "gui/${uid}/${label}" 2>/dev/null || true; \
266
269
  echo " Log: ${log_file}"; }
267
270
  }
@@ -310,30 +313,131 @@ register_optional_mcp brave-search \
310
313
  register_optional_mcp context7 \
311
314
  "Install Context7 MCP (library documentation lookups)? [y/N]: " _add_context7
312
315
 
313
- # Migrate the legacy bootstrap-managed `playwright` USER-scope entry to the new
314
- # name. Only shapes bootstrap itself created are touched (the shared-http URL
315
- # or the old stdio `npx @playwright/mcp` form), and only at user scope — a
316
- # project-scoped `playwright` is repository config and coexists with ours by
317
- # name. Detection reads ~/.claude.json top-level mcpServers directly because
318
- # `claude mcp get playwright` resolves to the project entry when one exists.
319
- if node -e '
320
- const p = (require(require("os").homedir() + "/.claude.json").mcpServers || {}).playwright;
321
- if (!p) process.exit(1);
322
- const j = JSON.stringify(p);
323
- process.exit(j.includes(process.argv[1]) || j.includes("@playwright/mcp") ? 0 : 1);
324
- ' "$PLAYWRIGHT_MCP_URL" 2>/dev/null; then
325
- echo " playwright: migrating legacy bootstrap user-scope entry to '$PLAYWRIGHT_MCP_NAME'."
326
- claude mcp remove playwright -s user 2>/dev/null || true
327
- fi
316
+ # _disable_project_playwright_locally
317
+ # Rejects the project's .mcp.json `playwright` on THIS machine only, via
318
+ # disabledMcpjsonServers in $PROJECT_DIR/.claude/settings.local.json — the
319
+ # team's .mcp.json is never touched. Idempotent; warns + skips on unparseable
320
+ # or unexpected file shapes (same fail-safe posture as merge-settings-deny.js).
321
+ _disable_project_playwright_locally() {
322
+ node -e '
323
+ const fs = require("fs"), path = require("path");
324
+ const file = path.join(process.argv[1], ".claude", "settings.local.json");
325
+ let s = {};
326
+ if (fs.existsSync(file)) {
327
+ try { s = JSON.parse(fs.readFileSync(file, "utf8")); } catch (e) {
328
+ console.error(" WARNING: could not parse " + file + " — skipping local disable."); process.exit(0);
329
+ }
330
+ if (typeof s !== "object" || s === null || Array.isArray(s)) {
331
+ console.error(" WARNING: " + file + " is not a JSON object — skipping local disable."); process.exit(0);
332
+ }
333
+ }
334
+ if ("disabledMcpjsonServers" in s && !Array.isArray(s.disabledMcpjsonServers)) {
335
+ console.error(" WARNING: disabledMcpjsonServers is not an array — skipping local disable."); process.exit(0);
336
+ }
337
+ const list = s.disabledMcpjsonServers || (s.disabledMcpjsonServers = []);
338
+ if (!list.includes("playwright")) list.push("playwright");
339
+ fs.mkdirSync(path.dirname(file), { recursive: true });
340
+ const tmp = file + ".tmp-" + process.pid;
341
+ fs.writeFileSync(tmp, JSON.stringify(s, null, 2) + "\n");
342
+ fs.renameSync(tmp, file);
343
+ console.log(" playwright: project entry disabled on this machine (.claude/settings.local.json disabledMcpjsonServers).");
344
+ ' "$PROJECT_DIR" || true
345
+ }
328
346
 
329
- # Shared-http upgrade detection is darwin-only: on Linux the stdio entry is the
330
- # desired end state, so pass an empty expected arg there (empty = never upgrade).
331
- # The scope prompt is skipped on darwin (the shared server is user-scope only).
332
- playwright_expected=""
333
- playwright_fixed_scope=""
334
- if [ "$(uname -s)" = "Darwin" ]; then
335
- playwright_expected="$PLAYWRIGHT_MCP_URL"
336
- playwright_fixed_scope="user"
337
- fi
338
- register_optional_mcp "$PLAYWRIGHT_MCP_NAME" \
339
- "Install Playwright MCP (browser automation & UI testing)? [y/N]: " _add_playwright "$playwright_expected" "$playwright_fixed_scope"
347
+ # _install_playwright_flow
348
+ # Dedicated install/conflict logic for playwright (the generic wrapper cannot
349
+ # express this). Two variables decide the path: WHERE an existing `playwright`
350
+ # registration lives (user / project / local), and — for project scope —
351
+ # whether .mcp.json is CHECKED IN (git-tracked ⇒ team-owned, never modified).
352
+ _install_playwright_flow() {
353
+ local pw_scope choice expected=""
354
+ [ "$(uname -s)" = "Darwin" ] && expected="$PLAYWRIGHT_MCP_URL"
355
+
356
+ # Rename-back: a user-scope `playwright-shared` pointing at our URL is the
357
+ # never-published 2.11.3 naming — fold it back into the canonical name when
358
+ # no conflicting `playwright` blocks that (the conflict branches below keep
359
+ # it as-is, since there it IS the desired registration).
360
+ if ! mcp_installed playwright && mcp_installed "$PLAYWRIGHT_MCP_ALT_NAME" \
361
+ && mcp_matches "$PLAYWRIGHT_MCP_ALT_NAME" "$PLAYWRIGHT_MCP_URL"; then
362
+ echo " playwright: reverting '$PLAYWRIGHT_MCP_ALT_NAME' back to the canonical name 'playwright'."
363
+ claude mcp remove "$PLAYWRIGHT_MCP_ALT_NAME" -s user 2>/dev/null || true
364
+ _add_playwright user "$PLAYWRIGHT_MCP_NAME"
365
+ return 0
366
+ fi
367
+
368
+ if ! mcp_installed playwright; then
369
+ # Fresh install (same gating as register_optional_mcp).
370
+ if [ "$INTERACTIVE" = true ]; then
371
+ if prompt_yn "Install Playwright MCP (browser automation & UI testing)? [y/N]: "; then
372
+ _add_playwright user "$PLAYWRIGHT_MCP_NAME"
373
+ echo " playwright MCP installed."
374
+ fi
375
+ else
376
+ echo " Installing playwright MCP..."
377
+ _add_playwright user "$PLAYWRIGHT_MCP_NAME"
378
+ echo " playwright MCP installed."
379
+ fi
380
+ return 0
381
+ fi
382
+
383
+ pw_scope="$(mcp_scope_of playwright)"
384
+
385
+ if [ "$pw_scope" = "user" ]; then
386
+ if [ -z "$expected" ] || mcp_matches playwright "$expected"; then
387
+ echo " playwright: already installed, skipping."
388
+ else
389
+ echo " playwright: upgrading registration (stdio → shared http)."
390
+ claude mcp remove playwright -s user 2>/dev/null || true
391
+ _add_playwright user "$PLAYWRIGHT_MCP_NAME"
392
+ fi
393
+ # Cleanup a leftover alternate-name duplicate of ours at user scope.
394
+ if mcp_installed "$PLAYWRIGHT_MCP_ALT_NAME" && mcp_matches "$PLAYWRIGHT_MCP_ALT_NAME" "$PLAYWRIGHT_MCP_URL"; then
395
+ echo " playwright: removing duplicate '$PLAYWRIGHT_MCP_ALT_NAME' user-scope entry."
396
+ claude mcp remove "$PLAYWRIGHT_MCP_ALT_NAME" -s user 2>/dev/null || true
397
+ fi
398
+ return 0
399
+ fi
400
+
401
+ # Conflict: playwright registered at project/local scope (or undetectable).
402
+ if [ "$INTERACTIVE" != true ] || [ ! -t 0 ] || [ -z "$PROJECT_DIR" ] || [ "$pw_scope" = "unknown" ]; then
403
+ echo " playwright: an existing ${pw_scope}-scope registration was found — leaving everything untouched."
404
+ echo " Resolve interactively: run 'npx @codewizard-dt/bootstrap update' in a terminal to choose how to proceed."
405
+ return 0
406
+ fi
407
+
408
+ if [ "$pw_scope" = "project" ] \
409
+ && git -C "$PROJECT_DIR" ls-files --error-unmatch .mcp.json >/dev/null 2>&1; then
410
+ # Team-owned: .mcp.json is checked into the repo. NEVER modified.
411
+ echo " playwright: this project's committed .mcp.json registers its own playwright server."
412
+ echo " [1] Register the bootstrap shared server as '$PLAYWRIGHT_MCP_ALT_NAME' and disable the project one on this machine only"
413
+ echo " [2] Register '$PLAYWRIGHT_MCP_ALT_NAME' alongside it (both active — browser tools will appear twice)"
414
+ echo " [3] Don't touch anything (default)"
415
+ read -r -p " How should we proceed? [1/2/3]: " choice || choice=3
416
+ case "$choice" in
417
+ 1)
418
+ _add_playwright user "$PLAYWRIGHT_MCP_ALT_NAME"
419
+ _disable_project_playwright_locally
420
+ ;;
421
+ 2)
422
+ _add_playwright user "$PLAYWRIGHT_MCP_ALT_NAME"
423
+ ;;
424
+ *)
425
+ echo " playwright: left untouched."
426
+ ;;
427
+ esac
428
+ return 0
429
+ fi
430
+
431
+ # Machine-local registration: untracked .mcp.json (project scope) or the
432
+ # ~/.claude.json project entry (local scope). Safe to modify with consent.
433
+ echo " playwright: an existing ${pw_scope}-scope registration was found (machine-local, not checked in)."
434
+ if prompt_yn " Replace it with the bootstrap shared server (removes the ${pw_scope}-scope entry)? [y/N] (no = keep it and register ours as '$PLAYWRIGHT_MCP_ALT_NAME'): "; then
435
+ ( cd "$PROJECT_DIR" && claude mcp remove playwright -s "$pw_scope" ) || true
436
+ _add_playwright user "$PLAYWRIGHT_MCP_NAME"
437
+ else
438
+ _add_playwright user "$PLAYWRIGHT_MCP_ALT_NAME"
439
+ echo " playwright: existing entry kept; bootstrap server registered as '$PLAYWRIGHT_MCP_ALT_NAME'."
440
+ fi
441
+ }
442
+
443
+ _install_playwright_flow
@@ -59,11 +59,14 @@ mcp_matches() { claude mcp get "$1" 2>/dev/null | grep -qF "$2"; }
59
59
  mcp_scope_of() {
60
60
  local line
61
61
  line="$(claude mcp get "$1" 2>/dev/null | grep -m1 'Scope:')" || { echo unknown; return 0; }
62
+ # Match the scope word immediately after "Scope: " — descriptive parentheticals
63
+ # like "Local config (private to you in this project)" contain other scope
64
+ # words, so loose substring matching misclassifies.
62
65
  case "$line" in
63
- *User*) echo user ;;
64
- *[Pp]roject*) echo project ;;
65
- *[Ll]ocal*) echo local ;;
66
- *) echo unknown ;;
66
+ *"Scope: User"*|*"Scope: user"*) echo user ;;
67
+ *"Scope: Project"*|*"Scope: project"*) echo project ;;
68
+ *"Scope: Local"*|*"Scope: local"*) echo local ;;
69
+ *) echo unknown ;;
67
70
  esac
68
71
  }
69
72
 
@@ -107,8 +110,9 @@ detect_installed_mcps() {
107
110
  result="${result:+$result }$mcp"
108
111
  fi
109
112
  done
110
- # The bootstrap-managed playwright server registers as "playwright-shared" so
111
- # it can coexist with a project-scoped "playwright"; either name enables the
113
+ # The bootstrap-managed playwright server is normally named "playwright";
114
+ # "playwright-shared" is the conflict-resolution alternate (used when a
115
+ # project ships its own playwright entry). Either name enables the
112
116
  # playwright guide section (guide key stays "playwright").
113
117
  if mcp_installed "playwright-shared" || mcp_installed "playwright"; then
114
118
  result="${result:+$result }playwright"
@@ -141,7 +145,7 @@ run_project_sync() {
141
145
 
142
146
  echo "Syncing wiki scaffold..."
143
147
  "$script_dir/sync-wiki-scaffold.sh" --interactive "$project_dir"
144
- "$script_dir/merge-gitignore.sh" "$project_dir"
148
+ "$script_dir/merge-gitignore.sh" --interactive "$project_dir"
145
149
  echo ""
146
150
 
147
151
  echo "Building MCP tools guide..."
@@ -6,8 +6,28 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6
6
  # Packed copy of the repo .gitignore (dotfiles are never included in npm packages)
7
7
  TEMPLATE_GITIGNORE="$SCRIPT_DIR/templates/gitignore"
8
8
 
9
+ # merge-gitignore.sh [--interactive] <path-to-project>
10
+ #
11
+ # NOTHING is ever added to a project's .gitignore without asking. The template
12
+ # is split into titled sections (delimited by `# ---` banner comments); every
13
+ # section with missing lines is offered by title ("Add/update .gitignore
14
+ # section '<title>' (N new line(s))? [y/N]") — even sections already partially
15
+ # present. Sections with nothing new are skipped silently. Non-interactive
16
+ # runs (or no tty) change nothing at all, including when no .gitignore exists.
17
+
18
+ INTERACTIVE=false
19
+ POSITIONAL=()
20
+ for arg in "$@"; do
21
+ case "$arg" in
22
+ --interactive) INTERACTIVE=true ;;
23
+ *) POSITIONAL+=("$arg") ;;
24
+ esac
25
+ done
26
+ # bash 3.2 + set -u: expanding an empty array is an unbound-variable error
27
+ set -- ${POSITIONAL[@]+"${POSITIONAL[@]}"}
28
+
9
29
  if [ $# -ne 1 ]; then
10
- echo "Usage: $0 <path-to-project>" >&2
30
+ echo "Usage: $0 [--interactive] <path-to-project>" >&2
11
31
  exit 1
12
32
  fi
13
33
 
@@ -15,51 +35,240 @@ PROJECT_DIR="$(resolve_project_dir "$1")" || exit 1
15
35
 
16
36
  TARGET="$PROJECT_DIR/.gitignore"
17
37
 
18
- # No target → copy wholesale
19
- if [ ! -f "$TARGET" ]; then
20
- cp "$TEMPLATE_GITIGNORE" "$TARGET"
21
- echo ".gitignore: copied to $PROJECT_DIR"
38
+ # Interactive-only: without a tty there is nobody to ask, and we never add
39
+ # .gitignore entries without asking.
40
+ if [ "$INTERACTIVE" = false ] || [ ! -t 0 ]; then
41
+ echo ".gitignore: skipped (interactive only — run 'npx @codewizard-dt/bootstrap update' in a terminal to be offered the sections)."
22
42
  exit 0
23
43
  fi
24
44
 
25
- # Snapshot existing lines for exact-match lookups during the merge loop
26
- EXISTING=$(mktemp)
27
- cp "$TARGET" "$EXISTING"
28
- trap 'rm -f "$EXISTING"' EXIT
45
+ WORK_DIR=$(mktemp -d)
46
+ trap 'rm -rf "$WORK_DIR"' EXIT
29
47
 
30
- added=0
31
- pending="" # comment/blank lines buffered until we know the next content line is new
48
+ # Split the template into per-section files. Banner delimiters come in pairs
49
+ # (open dash line, title comment, close dash line); every odd dash line starts
50
+ # a new section. Titles land in $WORK_DIR/title.N, bodies in $WORK_DIR/sec.N.
51
+ SECTION_COUNT=$(awk -v dir="$WORK_DIR" '
52
+ /^# -----/ {
53
+ dash++
54
+ if (dash % 2 == 1) n++
55
+ print >> (dir "/sec." n)
56
+ next
57
+ }
58
+ n > 0 {
59
+ print >> (dir "/sec." n)
60
+ if (dash % 2 == 1 && $0 ~ /^# / && !(n in ttl)) {
61
+ t = $0; sub(/^# +/, "", t); ttl[n] = t
62
+ print t > (dir "/title." n)
63
+ }
64
+ }
65
+ END { print n }
66
+ ' "$TEMPLATE_GITIGNORE")
32
67
 
33
- while IFS= read -r line || [ -n "$line" ]; do
34
- trimmed="${line#"${line%%[![:space:]]*}"}" # leading-whitespace stripped
68
+ # Snapshot existing lines for exact-match lookups during the merge loops.
69
+ # The target itself is NOT pre-created — it comes into existence only when a
70
+ # section is accepted (merge_section appends).
71
+ EXISTING="$WORK_DIR/existing"
72
+ if [ -f "$TARGET" ]; then cp "$TARGET" "$EXISTING"; else : > "$EXISTING"; fi
35
73
 
36
- if [ -z "$trimmed" ] || [[ "$trimmed" == \#* ]]; then
37
- # Blank or comment line — hold in buffer; only flush if a new content line follows
38
- pending+="${line}"$'\n'
39
- else
40
- # Content line
41
- if grep -qFx -- "$line" "$EXISTING"; then
42
- # Already present — discard buffered headers (their section is already represented)
43
- pending=""
74
+ # section_missing_count <section-file>: how many non-comment entries are not
75
+ # yet in the target (exact line match)?
76
+ section_missing_count() {
77
+ local line trimmed n=0
78
+ while IFS= read -r line || [ -n "$line" ]; do
79
+ trimmed="${line#"${line%%[![:space:]]*}"}"
80
+ [ -z "$trimmed" ] && continue
81
+ case "$trimmed" in \#*) continue ;; esac
82
+ grep -qFx -- "$line" "$EXISTING" || n=$((n + 1))
83
+ done < "$1"
84
+ echo "$n"
85
+ }
86
+
87
+ added=0
88
+
89
+ # merge_section <section-file>: append the section's missing lines, keeping
90
+ # comment/blank lines buffered so headers only land when new entries follow.
91
+ merge_section() {
92
+ local line trimmed pending=""
93
+ while IFS= read -r line || [ -n "$line" ]; do
94
+ trimmed="${line#"${line%%[![:space:]]*}"}"
95
+ if [ -z "$trimmed" ] || [[ "$trimmed" == \#* ]]; then
96
+ pending+="${line}"$'\n'
44
97
  else
45
- # New line — pre-flight the file on first append, then flush buffer + add line
46
- if [ "$added" -eq 0 ] && [ -s "$TARGET" ]; then
47
- # Ensure target ends with a newline before we start appending
48
- [[ $(tail -c1 "$TARGET") ]] && printf '\n' >> "$TARGET"
49
- fi
50
- if [ -n "$pending" ]; then
51
- printf '%s' "$pending" >> "$TARGET"
98
+ if grep -qFx -- "$line" "$EXISTING"; then
52
99
  pending=""
100
+ else
101
+ if [ "$added" -eq 0 ] && [ -s "$TARGET" ]; then
102
+ # Ensure target ends with a newline before we start appending
103
+ [[ $(tail -c1 "$TARGET") ]] && printf '\n' >> "$TARGET"
104
+ fi
105
+ if [ -n "$pending" ]; then
106
+ printf '%s' "$pending" >> "$TARGET"
107
+ pending=""
108
+ fi
109
+ printf '%s\n' "$line" >> "$TARGET"
110
+ echo " + $line"
111
+ added=$((added + 1))
53
112
  fi
54
- printf '%s\n' "$line" >> "$TARGET"
55
- echo " + $line"
56
- added=$((added + 1))
57
113
  fi
114
+ done < "$1"
115
+ }
116
+
117
+ i=1
118
+ while [ "$i" -le "$SECTION_COUNT" ]; do
119
+ SEC="$WORK_DIR/sec.$i"
120
+ TITLE="section $i"
121
+ [ -f "$WORK_DIR/title.$i" ] && TITLE="$(cat "$WORK_DIR/title.$i")"
122
+ missing="$(section_missing_count "$SEC")"
123
+ if [ "$missing" -eq 0 ]; then
124
+ : # nothing new to offer for this section
125
+ elif prompt_yn " Add/update .gitignore section '$TITLE' ($missing new line(s))? [y/N]: "; then
126
+ merge_section "$SEC"
127
+ else
128
+ echo " .gitignore section '$TITLE': skipped."
58
129
  fi
59
- done < "$TEMPLATE_GITIGNORE"
130
+ i=$((i + 1))
131
+ done
60
132
 
61
133
  if [ "$added" -eq 0 ]; then
62
- echo ".gitignore: already up to date in $PROJECT_DIR"
134
+ echo ".gitignore: no changes made in $PROJECT_DIR"
63
135
  else
64
136
  echo ".gitignore: $added line(s) merged into $PROJECT_DIR"
65
137
  fi
138
+
139
+ # Machine-local git exclusion for bootstrap agent state. These dirs must NEVER
140
+ # go into .gitignore: Serena (ignore_all_files_in_gitignore: true) and Claude
141
+ # Code's Grep mirror .gitignore and would go blind on the wiki. .git/info/exclude
142
+ # has identical semantics for git, is never committed (a per-machine choice,
143
+ # offered per-machine), and is invisible to Serena — its GitignoreParser reads
144
+ # only files named ".gitignore", so the dirs stay navigable. It is NOT invisible
145
+ # to every tool: ripgrep-class walkers and Claude Code's @ file picker do honor
146
+ # info/exclude. That is why install-global.sh installs ~/.claude/file-suggestion.sh
147
+ # and registers it as the fileSuggestion picker — it re-includes exactly the paths
148
+ # listed under the sentinel written below.
149
+ GIT_EXCLUDE="$PROJECT_DIR/.git/info/exclude"
150
+ EXCLUDE_SENTINEL="# bootstrap wiki & agent state (machine-local)"
151
+
152
+ # Canonical form is what file-suggestion.sh can actually parse: the sentinel
153
+ # line, immediately followed by .serena/, raw/, wiki/ in that order, each of the
154
+ # four appearing exactly once, and the block terminated by a "#" comment or EOF.
155
+ # A path sitting ABOVE the sentinel is still excluded by git but invisible to the
156
+ # picker, so appending only the absent paths is not enough — we normalize.
157
+ exclude_is_canonical() {
158
+ [ -f "$1" ] || return 1
159
+ awk -v sentinel="$EXCLUDE_SENTINEL" '
160
+ { line[NR] = $0 }
161
+ $0 == sentinel { sent++; at = NR }
162
+ $0 == ".serena/" { a++ }
163
+ $0 == "raw/" { b++ }
164
+ $0 == "wiki/" { c++ }
165
+ END {
166
+ if (sent != 1 || a != 1 || b != 1 || c != 1) exit 1
167
+ if (line[at + 1] != ".serena/" || line[at + 2] != "raw/" || line[at + 3] != "wiki/") exit 1
168
+ if (at + 3 < NR && line[at + 4] !~ /^[[:space:]]*#/) exit 1
169
+ exit 0
170
+ }
171
+ ' "$1"
172
+ }
173
+
174
+ # A user's own entry sitting INSIDE the sentinel block. Normalizing re-appends
175
+ # our block at the bottom and leaves such an entry above it: git still excludes
176
+ # it, but the picker stops re-including it, so say so rather than change the
177
+ # user's view of their own file silently.
178
+ exclude_stranded() {
179
+ [ -f "$1" ] || return 0
180
+ awk -v sentinel="$EXCLUDE_SENTINEL" '
181
+ $0 == sentinel { in_block = 1; next }
182
+ in_block && /^[[:space:]]*#/ { in_block = 0 }
183
+ in_block && $0 !~ /^[[:space:]]*$/ \
184
+ && $0 != ".serena/" && $0 != "raw/" && $0 != "wiki/" { print " " $0 }
185
+ ' "$1"
186
+ }
187
+
188
+ # Scrub every occurrence of the sentinel and of the three exact whole-line paths
189
+ # from anywhere in the file, then re-append the canonical block at the bottom.
190
+ # Every other line survives verbatim and in its original order (this file may
191
+ # hold the user's own exclusions); only a blank run the scrub itself created is
192
+ # collapsed, and the result ends with exactly one newline.
193
+ exclude_normalize() {
194
+ local file="$1" src="$1" tmp mode=""
195
+ [ -f "$src" ] || src=/dev/null
196
+ if [ -f "$file" ]; then
197
+ mode=$(stat -f '%Lp' "$file" 2>/dev/null || stat -c '%a' "$file" 2>/dev/null || true)
198
+ fi
199
+ mkdir -p "$(dirname "$file")" || return 1
200
+ tmp=$(mktemp "$(dirname "$file")/.bootstrap-exclude.XXXXXX") || return 1
201
+ awk -v sentinel="$EXCLUDE_SENTINEL" '
202
+ BEGIN { n = 0; prev_blank = 1; dropped = 0 }
203
+ $0 == sentinel || $0 == ".serena/" || $0 == "raw/" || $0 == "wiki/" { dropped = 1; next }
204
+ {
205
+ if ($0 ~ /^[[:space:]]*$/) {
206
+ if (prev_blank && dropped) next
207
+ prev_blank = 1
208
+ } else {
209
+ prev_blank = 0
210
+ }
211
+ keep[++n] = $0
212
+ dropped = 0
213
+ }
214
+ END {
215
+ while (n > 1 && keep[n] ~ /^[[:space:]]*$/ && keep[n - 1] ~ /^[[:space:]]*$/) n--
216
+ if (n == 1 && keep[1] ~ /^[[:space:]]*$/) n = 0
217
+ for (i = 1; i <= n; i++) print keep[i]
218
+ print sentinel
219
+ print ".serena/"
220
+ print "raw/"
221
+ print "wiki/"
222
+ }
223
+ ' "$src" > "$tmp" || { rm -f "$tmp"; return 1; }
224
+ mv -f "$tmp" "$file" || { rm -f "$tmp"; return 1; }
225
+ if [ -n "$mode" ]; then chmod "$mode" "$file" 2>/dev/null || true; fi
226
+ return 0
227
+ }
228
+
229
+ # Normalize and report. Never fatal: this runs under `set -euo pipefail` from
230
+ # run_project_sync and must not abort the user's setup.
231
+ exclude_apply() {
232
+ local stranded
233
+ stranded=$(exclude_stranded "$GIT_EXCLUDE")
234
+ if ! exclude_normalize "$GIT_EXCLUDE"; then
235
+ echo " Warning: could not update $GIT_EXCLUDE — left unchanged."
236
+ return 1
237
+ fi
238
+ if [ -n "$stranded" ]; then
239
+ echo " Note: these entries were inside the bootstrap block and now sit above it."
240
+ echo " git still excludes them; @-autocomplete will no longer list them:"
241
+ printf '%s\n' "$stranded"
242
+ fi
243
+ return 0
244
+ }
245
+
246
+ if [ -d "$PROJECT_DIR/.git" ]; then
247
+ exclude_missing=0
248
+ exclude_added=""
249
+ for p in ".serena/" "raw/" "wiki/"; do
250
+ if [ -f "$GIT_EXCLUDE" ] && grep -qFx -- "$p" "$GIT_EXCLUDE"; then
251
+ continue
252
+ fi
253
+ exclude_missing=$((exclude_missing + 1))
254
+ exclude_added="${exclude_added} + ${p} (.git/info/exclude)"$'\n'
255
+ done
256
+
257
+ if exclude_is_canonical "$GIT_EXCLUDE"; then
258
+ : # already canonical — leave the file byte-identical
259
+ elif [ "$exclude_missing" -gt 0 ]; then
260
+ # Accepting newly hides paths from git, so this stays a consent prompt.
261
+ if prompt_yn " Keep .serena/, raw/, wiki/ out of git on THIS machine (.git/info/exclude — not shared with the team; visible to Serena; @-autocomplete restored via the installed fileSuggestion script)? [y/N]: "; then
262
+ if exclude_apply; then
263
+ printf '%s' "$exclude_added"
264
+ echo " Note: this protects only this clone — teammates opt in by running 'npx @codewizard-dt/bootstrap update' themselves."
265
+ fi
266
+ fi
267
+ else
268
+ # All three are already excluded somewhere in the file: git's behaviour does
269
+ # not change, only the picker's view of it. A pure repair, so no prompt.
270
+ if exclude_apply; then
271
+ echo " .git/info/exclude: reordered .serena/, raw/, wiki/ under the bootstrap sentinel (git unchanged; restores @-autocomplete)"
272
+ fi
273
+ fi
274
+ fi