@techgoblin/gobstack 0.6.0-alpha.4 → 0.6.0-beta.2

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 (112) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/README.md +249 -116
  3. package/VERSION +1 -1
  4. package/bans/README.md +5 -5
  5. package/bans/grep-ban.sh +1 -1
  6. package/bin/goblin-bans +103 -27
  7. package/bin/goblin-init +345 -259
  8. package/bin/goblin-install +269 -213
  9. package/bin/goblin-lib.sh +63 -66
  10. package/bin/goblin-map +130 -579
  11. package/bin/goblin-mcp.js +9 -14
  12. package/bin/goblin-verify +411 -993
  13. package/bin/goblin.js +4 -9
  14. package/docs/CURATION-2026-11-07-gobigrams.md +223 -0
  15. package/docs/GUIDE.md +727 -179
  16. package/docs/LIMITS.md +297 -301
  17. package/docs/RECORD-NOTES.md +1 -2
  18. package/manifest/bans.tsv +9 -9
  19. package/manifest/categories.tsv +18 -0
  20. package/manifest/enforcement.tsv +14 -67
  21. package/manifest/glossary.tsv +6 -12
  22. package/manifest/library.tsv +33 -0
  23. package/package.json +1 -4
  24. package/skills/goblin-bootstrap/SKILL.md +20 -32
  25. package/skills/goblin-feature-map/SKILL.md +1 -1
  26. package/skills/goblin-handoff/SKILL.md +5 -0
  27. package/skills/goblin-verify-author/SKILL.md +14 -2
  28. package/skills/practice/SKILL.md +4 -4
  29. package/templates/AGENTS.md.tmpl +6 -6
  30. package/templates/HANDOFF.md.tmpl +8 -6
  31. package/templates/SPEC.md.tmpl +2 -2
  32. package/templates/agents-block.tmpl +1 -10
  33. package/adapters/_template/adapter.tsv +0 -16
  34. package/adapters/_template/detect.sh +0 -10
  35. package/adapters/_template/emit.sh +0 -5
  36. package/adapters/_template/verify.sh +0 -4
  37. package/adapters/claude/adapter.tsv +0 -8
  38. package/adapters/claude/detect.sh +0 -8
  39. package/adapters/claude/verify.sh +0 -47
  40. package/adapters/codex/adapter.tsv +0 -12
  41. package/adapters/codex/detect.sh +0 -9
  42. package/adapters/codex/verify.sh +0 -45
  43. package/adapters/copilot/adapter.tsv +0 -10
  44. package/adapters/copilot/detect.sh +0 -8
  45. package/adapters/copilot/verify.sh +0 -45
  46. package/adapters/cursor/adapter.tsv +0 -11
  47. package/adapters/cursor/detect.sh +0 -10
  48. package/adapters/cursor/verify.sh +0 -45
  49. package/adapters/gemini/adapter.tsv +0 -15
  50. package/adapters/gemini/detect.sh +0 -11
  51. package/adapters/gemini/verify.sh +0 -49
  52. package/adapters/hermes/adapter.tsv +0 -9
  53. package/adapters/hermes/detect.sh +0 -8
  54. package/adapters/hermes/verify.sh +0 -27
  55. package/adapters/opencode/adapter.tsv +0 -14
  56. package/adapters/opencode/detect.sh +0 -9
  57. package/adapters/opencode/verify.sh +0 -45
  58. package/automations/README.md +0 -53
  59. package/automations/bugreporter-intake.sh +0 -145
  60. package/automations/drift-audit.sh +0 -139
  61. package/automations/report.schema.tsv +0 -10
  62. package/bin/goblin +0 -144
  63. package/bin/goblin-audit +0 -143
  64. package/bin/goblin-doctor +0 -233
  65. package/bin/goblin-emit +0 -495
  66. package/bin/goblin-extras +0 -365
  67. package/bin/goblin-model +0 -105
  68. package/bin/goblin-upgrade +0 -580
  69. package/docs/ADOPTION.md +0 -173
  70. package/docs/CI.md +0 -190
  71. package/docs/CONTRACTS.md +0 -218
  72. package/docs/DESIGN.md +0 -92
  73. package/docs/ENFORCEMENT.md +0 -228
  74. package/docs/FLOWS.md +0 -164
  75. package/docs/GLOSSARY.md +0 -40
  76. package/docs/GUARDRAILS.md +0 -126
  77. package/docs/INTEGRATION.md +0 -92
  78. package/docs/LOOP.md +0 -165
  79. package/docs/RE-PLAYBOOK.md +0 -183
  80. package/docs/RISKS.md +0 -70
  81. package/docs/ROLES.md +0 -105
  82. package/extras-catalogue/catalogue.tsv +0 -42
  83. package/extras-catalogue/payload/README.md +0 -14
  84. package/extras-catalogue/payload/taste-skill/taste/REFERENCE.md +0 -3
  85. package/extras-catalogue/payload/taste-skill/taste/SKILL.md +0 -9
  86. package/manifest/classes.tsv +0 -51
  87. package/manifest/playbooks.tsv +0 -16
  88. package/presets/electron-overlay.yaml +0 -38
  89. package/presets/fleet.yaml +0 -43
  90. package/presets/game.yaml +0 -38
  91. package/presets/research.yaml +0 -41
  92. package/presets/service.yaml +0 -40
  93. package/presets/software.yaml +0 -48
  94. package/roles.yaml +0 -54
  95. package/skills/goblin-bugfix/SKILL.md +0 -26
  96. package/skills/goblin-bugreporter/SKILL.md +0 -52
  97. package/skills/goblin-drift-audit/SKILL.md +0 -43
  98. package/skills/goblin-eval/SKILL.md +0 -68
  99. package/skills/goblin-feature/SKILL.md +0 -26
  100. package/skills/goblin-investigation/SKILL.md +0 -26
  101. package/skills/goblin-judge/SKILL.md +0 -74
  102. package/skills/goblin-loop/SKILL.md +0 -88
  103. package/skills/goblin-mode/SKILL.md +0 -70
  104. package/skills/goblin-overnight/SKILL.md +0 -42
  105. package/skills/goblin-pr-gate/SKILL.md +0 -42
  106. package/skills/goblin-re-mobile/SKILL.md +0 -51
  107. package/skills/goblin-refactor/SKILL.md +0 -23
  108. package/skills/goblin-sweep/SKILL.md +0 -23
  109. package/skills/goblin-tdd-repro/SKILL.md +0 -27
  110. package/templates/audit-waiver.tsv.tmpl +0 -10
  111. package/templates/install-hooks.allowlist.tmpl +0 -9
  112. package/templates/report.yaml.tmpl +0 -16
@@ -1,580 +0,0 @@
1
- #!/usr/bin/env bash
2
- # goblin-upgrade — the 0.4.4 per-repo → global engine migration (W3).
3
- #
4
- # gob upgrade [--target <dir>] [--dry-run] [--yes] [--engine-dir <path>]
5
- #
6
- # What it does (the corrected 7-step sequence, W3-SPEC §2 — the record is rewritten
7
- # BEFORE the deletion, and every verify runs against a committed tree, CM-03):
8
- # 0. preflight: resolve the input state (I1/I2/I3/I4), refuse R1–R10
9
- # 1. hash-verify every recorded file (IN-02 clause-1 logic) — drift exits 1
10
- # 2. verify #1 — the untouched repo must already be green
11
- # 3. land the global engine at --engine-dir (default ~/.goblin/engine), 0700,
12
- # by copying this repo's own 18 verified bytes (same-hash reuse; different
13
- # hash → R6 refusal — never overwrite a global engine other repos declare)
14
- # 4. commit A: engine_dir: active in goblin.yaml + engine: block in
15
- # installed.json (18 engine entries dropped, skills retained) + gate.sh and
16
- # the CI run line re-pointed
17
- # 5. verify #2 — green with BOTH engines present (the vendored one judges; the
18
- # point is that the rewritten record and gate.sh parse mid-sequence)
19
- # 6. commit B: git rm exactly the 18 engine paths (the only irreversible step)
20
- # 7. verify #3 — expected, measured: 39 passed, 0 failed, 11 advisory,
21
- # 32 skipped, rc 0 (W3-SPEC MA5)
22
- # 8. report: counts, engine identity, both commit SHAs, the rollback command
23
- #
24
- # Exit contract (PLAN-V1 §2.3): 0 ok or no-op · 1 refusal, with the path and the
25
- # fix · 2 bad input. Refusals R1–R10 are W3-SPEC §4 verbatim in intent.
26
- #
27
- # Non-negotiables inherited from the rest of bin/: no npm, no jq, no yq, no
28
- # network (bash/awk/sed/git only); the engine stays bash; nothing outside the
29
- # repo is written except the engine lift, which is additive; rollback is pure
30
- # `git revert` of this command's own two commits (W3-SPEC §5).
31
- set -uo pipefail
32
-
33
- GOBLIN_UPGRADE_VERSION="0.6.0-alpha.4"
34
-
35
- usage() { sed -n '2,/^$/p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; }
36
-
37
- g_err() { printf 'error: %s\n' "$*" >&2; }
38
-
39
- # g_sed_i <sed-script> <file...> — in-place sed on BOTH sed families (goblin-lib's
40
- # helper; duplicated here because this command is deliberately self-contained and
41
- # does not source goblin-lib.sh — the BSD sed bomb, client Mac 2026-10-06).
42
- g_sed_i() {
43
- local script="$1"; shift
44
- if sed --version >/dev/null 2>&1; then sed -i "$script" "$@"; else sed -i '' "$script" "$@"; fi
45
- }
46
-
47
- # g_expand_tilde <path> — ~ and ~/... to $HOME (the goblin-install:81 idiom).
48
- g_expand_tilde() {
49
- case "$1" in
50
- "~") printf '%s\n' "$HOME" ;;
51
- "~"/*) printf '%s\n' "$HOME/${1#\~/}" ;;
52
- *) printf '%s\n' "$1" ;;
53
- esac
54
- }
55
-
56
- # sha256 <file> — the one hash spelling, used everywhere.
57
- sha256() { sha256sum "$1" 2>/dev/null | awk '{print $1}'; }
58
-
59
- # ---- options ------------------------------------------------------------------
60
- TARGET="."
61
- DRY_RUN=0 # --dry-run is the DEFAULT: the CLI never changes a repo
62
- # without an explicit --yes (PLAN-V1 §8.1 item 8).
63
- YES=0
64
- ENGINE_DIR_OPT=""
65
- while [ $# -gt 0 ]; do
66
- case "$1" in
67
- --target) TARGET="${2:-}"; shift 2 ;;
68
- --dry-run) DRY_RUN=1; shift ;;
69
- --yes) YES=1; shift ;;
70
- --engine-dir) ENGINE_DIR_OPT="${2:-}"; shift 2 ;;
71
- --help|-h) usage; exit 0 ;;
72
- *) g_err "unknown option: $1"; usage >&2; exit 2 ;;
73
- esac
74
- done
75
- if [ "$DRY_RUN" -eq 1 ] && [ "$YES" -eq 1 ]; then
76
- g_err "--dry-run and --yes are mutually exclusive"
77
- exit 2
78
- fi
79
- [ -n "$TARGET" ] || { g_err "--target needs a directory"; exit 2; }
80
- [ -z "$ENGINE_DIR_OPT" ] || [ -n "${ENGINE_DIR_OPT#-}" ] || { g_err "--engine-dir needs a path"; exit 2; }
81
-
82
- for dep in git awk sed grep sha256sum; do
83
- command -v "$dep" >/dev/null 2>&1 || { g_err "missing dependency: $dep"; exit 2; }
84
- done
85
-
86
- [ -d "$TARGET" ] || { g_err "--target is not a directory: $TARGET"; exit 2; }
87
- REPO=$(cd "$TARGET" && pwd)
88
- [ -n "$ENGINE_DIR_OPT" ] && ENGINE_DIR_OPT=$(g_expand_tilde "$ENGINE_DIR_OPT")
89
- ED="${ENGINE_DIR_OPT:-$HOME/.goblin/engine}"
90
-
91
- CONFIG="$REPO/.goblin/goblin.yaml"
92
- INSTALLED="$REPO/.goblin/installed.json"
93
-
94
- # The 18 engine paths (W3-SPEC MA1): .goblin/bin/* (the 4 installable) +
95
- # .goblin/manifest/* (5) + .goblin/bans/* (3) + .goblin/automations/* (5) +
96
- # .goblin/roles.yaml. The checkout's bin/ holds 8 files; only 4 are installable
97
- # (goblin-install:509-522), so the vendored payload is exactly these four.
98
- ENGINE_BIN_FILES="goblin-verify goblin-lib.sh goblin-audit goblin-bans"
99
-
100
- # vendored_list — the repo's engine payload, one path per line, .goblin-prefixed.
101
- # Empty when there is no payload (the engine is already gone).
102
- vendored_list() {
103
- local f
104
- [ -d "$REPO/.goblin/bin" ] && for f in $ENGINE_BIN_FILES; do
105
- [ -f "$REPO/.goblin/bin/$f" ] && printf '%s\n' ".goblin/bin/$f"
106
- done
107
- for d in manifest bans automations; do
108
- [ -d "$REPO/.goblin/$d" ] || continue
109
- for f in "$REPO/.goblin/$d/"*; do
110
- [ -f "$f" ] && printf '%s\n' ".goblin/$d/$(basename "$f")"
111
- done
112
- done
113
- [ -f "$REPO/.goblin/roles.yaml" ] && printf '%s\n' ".goblin/roles.yaml"
114
- return 0
115
- }
116
-
117
- # g_installed_files_strict <record> — only the `files:` map's entries (the same
118
- # reader goblin-verify's IN-04 uses: the `engine:` block's key/value pairs must
119
- # never read as files).
120
- g_installed_files_strict() {
121
- awk '
122
- /^ "files": \{[[:space:]]*\},?$/ { next }
123
- /^ "files": \{/ { f = 1; next }
124
- f && /^ \},$/ { f = 0 }
125
- f && /^ "/ {
126
- line = $0
127
- sub(/^ "/, "", line)
128
- p = line; sub(/".*/, "", p)
129
- h = line; sub(/[^"]*"[[:space:]]*:[[:space:]]*"/, "", h); sub(/".*/, "", h)
130
- if (p != "") print p "\t" h
131
- }
132
- ' "$1"
133
- }
134
-
135
- # engine_block_field <key> — one field of the record's engine: block (empty if absent).
136
- engine_block_field() {
137
- [ -f "$INSTALLED" ] || return 0
138
- awk -v k="$1" '
139
- index($0, "\"engine\"") && /:[[:space:]]*\{/ { inf = 1; next }
140
- inf && /^[[:space:]]*\}/ { inf = 0 }
141
- inf && $0 ~ ("\"" k "\"") {
142
- v = $0; sub(/^[^:]*:[[:space:]]*/, "", v); sub(/,?[[:space:]]*$/, "", v)
143
- gsub(/"/, "", v); print v; exit
144
- }
145
- ' "$INSTALLED"
146
- }
147
-
148
- record_scalar() { # <key>
149
- sed -n "s/^[[:space:]]*\"$1\"[[:space:]]*:[[:space:]]*\"\{0,1\}\([^\",]*\)\"\{0,1\},\{0,1\}$/\1/p" "$INSTALLED" | head -n 1
150
- }
151
-
152
- # record_files_count — entries in the record's files map.
153
- record_files_count() { g_installed_files_strict "$INSTALLED" | grep -c .; }
154
-
155
- record_engine_entries_count() { # engine-path entries still in the record
156
- g_installed_files_strict "$INSTALLED" | cut -f1 | grep -cE '^\.goblin/(bin/|manifest/|bans/|automations/|roles\.yaml$)' || true
157
- }
158
-
159
- # ---- verify #N runner ----------------------------------------------------------
160
- # The verify INSIDE the sequence must see the target repo's own state: HOME and
161
- # GOBLIN_ENGINE_DIR are re-pointed for the child only, so the operator's shell
162
- # environment can never override the migration's own declaration (the engine-dir
163
- # escape hatch, --engine-dir, must win over an inherited GOBLIN_ENGINE_DIR).
164
- VERIFY_SCRIPT="" # resolved per step: vendored while it exists, else the engine's
165
- run_verify() {
166
- local ed="$ED"
167
- ( cd "$REPO" && env -u GOBLIN_ENGINE_DIR \
168
- ${ed:+GOBLIN_ENGINE_DIR="$ed"} HOME="${GOBLIN_UPGRADE_HOME:-$HOME}" \
169
- bash "$VERIFY_SCRIPT" "$@" )
170
- }
171
-
172
- step_line() { printf 'step %s: %s\n' "$1" "$2"; }
173
-
174
- # ---- preflight: resolve the input state (W3-SPEC §1.2) -------------------------
175
- # I1 fresh 0.4.x per-repo install → migrate
176
- # I2 already migrated → idempotent no-op, exit 0
177
- # I3 absent/foreign/unparseable → refusal (2 / 1)
178
- # I4 partial crash shapes → refusal naming resume/rollback (R8/R9)
179
-
180
- MODE=""
181
-
182
- # ---- R3: not a git repo --------------------------------------------------------
183
- if ! git -C "$REPO" rev-parse --git-dir >/dev/null 2>&1; then
184
- MODE="refuse-R3"
185
- R3_MSG="upgrade: $REPO is not a git repository - upgrade's safety (three verifies, two commits, revert) requires git. fix: git init && git add -A && git commit"
186
- else
187
- # ---- R2: detached HEAD -------------------------------------------------------
188
- if [ -z "$(git -C "$REPO" symbolic-ref -q HEAD)" ]; then
189
- MODE="refuse-R2"
190
- R2_SHA=$(git -C "$REPO" rev-parse --short HEAD)
191
- R2_MSG="upgrade: $REPO is in detached-HEAD state at $R2_SHA - upgrade's rollback is \`git revert\` of its commits, which needs a branch. fix: git switch -c <name>"
192
- else
193
- # ---- R1: dirty tree --------------------------------------------------------
194
- R1_COUNT=$(git -C "$REPO" status --porcelain | grep -c . || true)
195
- if [ "${R1_COUNT:-0}" -gt 0 ]; then
196
- MODE="refuse-R1"
197
- R1_MSG="upgrade: $R1_COUNT uncommitted entries in $REPO - commit or stash first (upgrade writes its own two commits and cannot mix them with yours)"
198
- fi
199
- fi
200
- fi
201
-
202
- # ---- the record (I3) -----------------------------------------------------------
203
- RECORD_PRESENT=0
204
- [ -f "$INSTALLED" ] && RECORD_PRESENT=1
205
- RECORD_OK=0
206
- if [ "$RECORD_PRESENT" -eq 1 ]; then
207
- # It must parse, carry a version, and carry a files map (the strict reader must
208
- # see at least the shape a 0.4.x install writes).
209
- REC_VERSION=$(record_scalar version)
210
- REC_FILES_N=$(record_files_count)
211
- if grep -q '"version"' "$INSTALLED" 2>/dev/null && [ -n "$REC_VERSION" ] && grep -q '"files"' "$INSTALLED" 2>/dev/null; then
212
- RECORD_OK=1
213
- fi
214
- fi
215
-
216
- case "$RECORD_PRESENT:$RECORD_OK" in
217
- 0:*) MODE="${MODE:-refuse-I3-absent}" ;;
218
- *:0) MODE="${MODE:-refuse-I3-parse}" ;;
219
- esac
220
-
221
- if [ "$RECORD_OK" -eq 1 ]; then
222
- case "$REC_VERSION" in
223
- 0.4.*) : ;;
224
- *) MODE="${MODE:-refuse-I3-version}" ;;
225
- esac
226
- fi
227
-
228
- # ---- the state discriminator (I1/I2/I4), only on a well-formed 0.4.x record ----
229
- if [ "${RECORD_OK:-0}" -eq 1 ] && [ -z "${MODE:-}" ]; then
230
- BLOCK_MODE=$(engine_block_field mode)
231
- BLOCK_DIR=$(engine_block_field engine_dir)
232
- PAYLOAD_N=$(vendored_list | grep -c . || true)
233
- RECORD_ENGINE_N=$(record_engine_entries_count)
234
- if [ "$BLOCK_MODE" = "global" ] && [ "$PAYLOAD_N" -eq 0 ]; then
235
- MODE="noop-R4" # I2: already migrated → idempotent no-op
236
- elif [ "$BLOCK_MODE" = "global" ] && [ "$PAYLOAD_N" -gt 0 ]; then
237
- MODE="refuse-R8" # crash between commits A and B
238
- R8_MSG="upgrade: partial migration detected - the record says global but $PAYLOAD_N engine files are still here. fix: resume by removing them (the engine lives at ${BLOCK_DIR:-$ED}), or roll back: git revert <commit A>"
239
- elif [ "$BLOCK_MODE" != "global" ] && [ "$RECORD_ENGINE_N" -lt 18 ]; then
240
- MODE="refuse-R9" # hand-migration: entries PARTIALLY or fully dropped, no block
241
- R9_MSG="upgrade: $REPO's record is missing its engine: block and carries only $RECORD_ENGINE_N of the 18 engine entries - dropped $((18 - RECORD_ENGINE_N)) by hand, a state gob upgrade did not produce. fix: restore the record (git checkout .goblin/installed.json) and re-run"
242
- elif [ "$PAYLOAD_N" -eq 0 ]; then
243
- MODE="refuse-R5" # fresh-shape record but no payload to lift
244
- R5_MSG="upgrade: no engine at $ED and no vendored payload to lift it from - install the engine first (goblin-install a fresh repo, or set --engine-dir)"
245
- elif [ "$RECORD_ENGINE_N" -eq 18 ]; then
246
- MODE="migrate" # I1: the migration runs (record intact, payload intact)
247
- else
248
- MODE="refuse-R9" # record count off in any unnamed combination: refuse, do not guess
249
- R9_MSG="upgrade: $REPO's record carries $RECORD_ENGINE_N engine entries where a 0.4.4 install records 18 - an unnamed state. fix: restore the record (git checkout .goblin/installed.json) and re-run"
250
- fi
251
- fi
252
-
253
- # ---- dry-run report -------------------------------------------------------------
254
- if [ "$DRY_RUN" -eq 1 ]; then
255
- printf 'gob upgrade --dry-run — target %s\n' "$REPO"
256
- printf ' engine-dir: %s\n' "$ED"
257
- case "$MODE" in
258
- migrate)
259
- printf ' state: fresh 0.4.x per-repo install (record %s entries, %s engine payload files on disk)\n' \
260
- "$(record_files_count)" "$PAYLOAD_N"
261
- step_line 1 "hash-verify every recorded file (IN-02 clause-1 logic)"
262
- step_line 2 "verify #1 — the untouched repo must be green"
263
- step_line 3 "land the global engine at $ED (copy this repo's own $PAYLOAD_N verified bytes; reuse if the hash pair matches)"
264
- step_line 4 "commit A — engine_dir: active in goblin.yaml + engine: block in installed.json (record $(record_files_count) → $(( $(record_files_count) - RECORD_ENGINE_N )) entries) + gate.sh and the CI run line re-pointed"
265
- step_line 5 "verify #2 — green with both engines present (vendored judges; proves the rewritten record parses)"
266
- step_line 6 "commit B — git rm exactly the $PAYLOAD_N engine paths (the only irreversible step)"
267
- step_line 7 "verify #3 — expected: 39 passed, 0 failed, 11 advisory, 32 skipped, rc 0"
268
- step_line 8 "report — counts, engine identity, both commit SHAs, the rollback command"
269
- ;;
270
- noop-R4)
271
- printf ' state: already migrated — the real run would print no-op and exit 0\n'
272
- ;;
273
- refuse-R1) printf ' would refuse (exit 1): %s\n' "$R1_MSG" ;;
274
- refuse-R2) printf ' would refuse (exit 1): %s\n' "$R2_MSG" ;;
275
- refuse-R3) printf ' would refuse (exit 1): %s\n' "$R3_MSG" ;;
276
- refuse-I3-absent) printf ' would refuse: nothing to upgrade: %s/.goblin/installed.json — this is not a per-repo goblin-stack install\n' "$REPO" ;;
277
- refuse-I3-parse) printf ' would refuse: %s does not parse — this is not a per-repo goblin-stack install (exit 2)\n' "$INSTALLED" ;;
278
- refuse-I3-version) printf ' would refuse: record says %s; this upgrade migrates 0.4.x installs — do not hand-edit the record\n' "$REC_VERSION" ;;
279
- refuse-R8) printf ' would refuse (exit 1): %s\n' "$R8_MSG" ;;
280
- refuse-R9) printf ' would refuse (exit 1): %s\n' "$R9_MSG" ;;
281
- refuse-R5) printf ' would refuse (exit 1): %s\n' "$R5_MSG" ;;
282
- *) printf ' state: unknown\n' ;;
283
- esac
284
- printf ' dry-run wrote nothing.\n'
285
- exit 0
286
- fi
287
-
288
- # ---- the refusals, for a real run ----------------------------------------------
289
- case "$MODE" in
290
- refuse-R1) g_err "$R1_MSG"; exit 1 ;;
291
- refuse-R2) g_err "$R2_MSG"; exit 1 ;;
292
- refuse-R3) g_err "$R3_MSG"; exit 1 ;;
293
- refuse-I3-absent)
294
- g_err "nothing to upgrade: $INSTALLED — this is not a per-repo goblin-stack install"
295
- exit 2 ;;
296
- refuse-I3-parse)
297
- g_err "$INSTALLED does not parse — this is not a per-repo goblin-stack install"
298
- exit 2 ;;
299
- refuse-I3-version)
300
- g_err "record says $REC_VERSION; this upgrade migrates 0.4.x installs — do not hand-edit the record"
301
- exit 1 ;;
302
- refuse-R8) g_err "$R8_MSG"; exit 1 ;;
303
- refuse-R9) g_err "$R9_MSG"; exit 1 ;;
304
- refuse-R5) g_err "$R5_MSG"; exit 1 ;;
305
- noop-R4)
306
- printf 'no-op: already migrated to %s (cli %s)\n' \
307
- "${BLOCK_DIR:-$ED}" "$(engine_block_field cli_version)"
308
- exit 0 ;;
309
- esac
310
-
311
- # ================================================================================ step 1
312
- # Hash-verify EVERY recorded file before writing anything. A hand-edited harness
313
- # is not migrated silently: any drift exits 1 naming the file.
314
- step_line 1 "hash-verify the record ($(record_files_count) files)"
315
- VERIFY_SCRIPT="$REPO/.goblin/bin/goblin-verify"
316
- while IFS=$'\t' read -r p h; do
317
- [ -n "$p" ] || continue
318
- if [ ! -e "$REPO/$p" ]; then
319
- g_err "upgrade: recorded file is missing: $REPO/$p - restore it (git checkout -- $p) before upgrading"
320
- exit 1
321
- fi
322
- cur=$(sha256 "$REPO/$p")
323
- if [ "$cur" != "$h" ]; then
324
- g_err "upgrade: hash drift: $p (recorded $h, now $cur) - restore it (git checkout -- $p) before upgrading; a hand-edited harness is not migrated silently"
325
- exit 1
326
- fi
327
- done < <(g_installed_files_strict "$INSTALLED")
328
-
329
- # ================================================================================ step 2
330
- # Verify #1 — the untouched repo must already be green. Do not migrate a repo
331
- # that is already red: the migration would be blamed for it.
332
- step_line 2 "verify #1 — the untouched repo must be green"
333
- VERIFY_SCRIPT="$REPO/.goblin/bin/goblin-verify"
334
- V1=$(run_verify 2>&1); V1_RC=$?
335
- if [ "$V1_RC" -ne 0 ]; then
336
- printf '%s\n' "$V1"
337
- g_err "upgrade: verify #1 is red (exit $V1_RC) - the repo is not green before the migration; fix it first (the migration would be blamed for a red it did not cause)"
338
- exit 1
339
- fi
340
- printf 'verify #1: %s\n' "$(printf '%s' "$V1" | grep -E 'passed,' | head -n 1 | sed 's/^[[:space:]]*//')"
341
-
342
- # ================================================================================ step 3
343
- # Land the global engine. Copy THIS repo's own just-verified bytes — no trust
344
- # question arises. Same hash pair → reuse; different → R6 refusal.
345
- step_line 3 "land the global engine at $ED"
346
- RH=$(sha256 "$REPO/.goblin/bin/goblin-verify")
347
- RT=$(sha256 "$REPO/.goblin/manifest/enforcement.tsv")
348
- CLI_VERSION=$(record_scalar version)
349
- if [ -d "$ED" ]; then
350
- if [ ! -f "$ED/manifest/enforcement.tsv" ]; then
351
- g_err "upgrade: $ED exists but holds no manifest/enforcement.tsv - this is not an engine directory (a partial copy is a crashed step 3). fix: pass --engine-dir <path>, or remove $ED and re-run"
352
- exit 1
353
- fi
354
- EH=$(sha256 "$ED/bin/goblin-verify")
355
- if [ -z "$EH" ]; then
356
- g_err "upgrade: $ED holds a manifest but no bin/goblin-verify - a partial engine copy. fix: pass --engine-dir <path>, or remove $ED and re-run"
357
- exit 1
358
- fi
359
- ET=$(sha256 "$ED/manifest/enforcement.tsv")
360
- if [ "$EH" = "$RH" ] && [ "$ET" = "$RT" ]; then
361
- printf ' reusing existing engine (identical hash pair)\n'
362
- else
363
- g_err "upgrade: $ED holds a different engine (cli_sha256 ${EH:-absent}, enforcement ${ET:-absent}) than this repo's ($RH, $RT). I will not overwrite a global engine other repos may declare. fix: pass --engine-dir <path> for a second engine, or upgrade $ED first"
364
- exit 1
365
- fi
366
- else
367
- lift_rc=0
368
- while IFS= read -r rel; do
369
- [ -n "$rel" ] || continue
370
- dst="$ED/${rel#.goblin/}"
371
- mkdir -p "$(dirname "$dst")" || { lift_rc=1; break; }
372
- cp -p "$REPO/$rel" "$dst" || { lift_rc=1; break; }
373
- done < <(vendored_list)
374
- if [ "$lift_rc" -ne 0 ]; then
375
- g_err "upgrade: copying the engine payload to $ED failed - aborting before anything in the repo changed"
376
- exit 1
377
- fi
378
- chmod 700 "$ED"
379
- # Validate AFTER the copy: the landed engine must carry this repo's exact bytes.
380
- EH=$(sha256 "$ED/bin/goblin-verify"); ET=$(sha256 "$ED/manifest/enforcement.tsv")
381
- if [ "$EH" != "$RH" ] || [ "$ET" != "$RT" ]; then
382
- g_err "upgrade: the engine landed at $ED does not match this repo's bytes (cli $EH/$RH, enforcement $ET/$RT) - a partial copy; remove $ED and re-run"
383
- exit 1
384
- fi
385
- printf ' landed %s engine files at %s (mode 0700)\n' "$(vendored_list | grep -c .)" "$ED"
386
- fi
387
-
388
- # ================================================================================ step 4
389
- # Commit A — the declaration-and-record commit. Written in ONE commit:
390
- # goblin.yaml the commented `# engine_dir:` placeholder becomes the active line
391
- # installed.json gains the engine: block; exactly the 18 engine entries drop
392
- # (skills stay — MA5 shows SK-01/02/04/AU-04 SKIP by mode)
393
- # checks/gate.sh the source line re-pointed to the engine-resolution idiom
394
- # .github/workflows/goblin-gate.yml run line re-pointed to npx @techgoblin/gobstack@<v>
395
- # The record rewrite happens BEFORE any deletion (the plan's order is measured
396
- # wrong: deleting with the record untouched is IN-02 FAIL, W3-SPEC MA3).
397
- step_line 4 "commit A — the declaration-and-record commit"
398
-
399
- engine_dir_line="$ED"
400
- active_engine_line() { printf 'engine_dir: %s\n' "$engine_dir_line"; }
401
-
402
- # ---- goblin.yaml: the commented placeholder becomes the active line ----
403
- if grep -q '^engine_dir:' "$CONFIG"; then
404
- cur_line=$(sed -n 's/^engine_dir:[[:space:]]*//p' "$CONFIG" | head -n 1)
405
- if [ "$cur_line" != "$engine_dir_line" ]; then
406
- g_err "upgrade: $CONFIG already declares engine_dir: $cur_line (wanted $engine_dir_line) - unparseable or hand-edited config; aborting"
407
- exit 2
408
- fi
409
- else
410
- if ! grep -q '^# engine_dir:' "$CONFIG"; then
411
- g_err "upgrade: $CONFIG has neither an active nor a commented engine_dir: line - not a 0.4.4 template; aborting"
412
- exit 2
413
- fi
414
- g_sed_i "s|^# engine_dir:.*|$(active_engine_line | sed 's/[&|]/\\&/g')|" "$CONFIG"
415
- fi
416
-
417
- # ---- installed.json: engine block + drop exactly the 18 engine entries ----
418
- REC_BEFORE=$(record_files_count)
419
- PYJ=$(mktemp -d)
420
- cat > "$PYJ/rewrite_record.py" <<'PYEOF'
421
- import json, sys
422
- path, ed, ver, cli_sha, tsv_sha = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]
423
- rec = json.load(open(path))
424
- rec["engine"] = {"mode": "global", "engine_dir": ed, "cli_version": ver,
425
- "cli_sha256": cli_sha, "enforcement_tsv_sha256": tsv_sha}
426
- def is_engine(p):
427
- return p.startswith(".goblin/bin/") or p.startswith(".goblin/manifest/") \
428
- or p.startswith(".goblin/bans/") or p.startswith(".goblin/automations/") \
429
- or p == ".goblin/roles.yaml"
430
- rec["files"] = {p: h for p, h in rec["files"].items() if not is_engine(p)}
431
- json.dump(rec, open(path, "w"), indent=2)
432
- PYEOF
433
- cat > "$PYJ/rehash_record.py" <<'PYEOF'
434
- import json, sys
435
- path, key, new_sha = sys.argv[1], sys.argv[2], sys.argv[3]
436
- rec = json.load(open(path))
437
- if key in rec.get("files", {}):
438
- rec["files"][key] = new_sha
439
- json.dump(rec, open(path, "w"), indent=2)
440
- PYEOF
441
- if ! python3 "$PYJ/rewrite_record.py" "$INSTALLED" "$ED" "$CLI_VERSION" "$RH" "$RT"; then
442
- g_err "upgrade: rewriting $INSTALLED failed (python3 unavailable or the record unparseable) - the tree is uncommitted; fix: git checkout .goblin/goblin.yaml .goblin/installed.json checks/gate.sh"
443
- exit 2
444
- fi
445
- REC_AFTER=$(record_files_count)
446
-
447
- # ---- checks/gate.sh: source line re-pointed to the engine-resolution idiom ----
448
- GATE="$REPO/checks/gate.sh"
449
- if [ -f "$GATE" ] && grep -q '\.goblin/bin/goblin-lib.sh' "$GATE"; then
450
- # No machine path is committed: the idiom resolves engine_dir: → the env
451
- # default → ~/.goblin/engine, then sources <engine>/bin/goblin-lib.sh.
452
- # The CONFIG line moves UP beside ROOT so the resolver can read it.
453
- # gate.sh is a files-RECORDED artifact: its record entry is re-hashed below,
454
- # in the same commit (IN-02 would otherwise read the rewrite as drift).
455
- awk '
456
- /^# shellcheck source=\/dev\/null$/ && !done {
457
- print "# shellcheck source=/dev/null"
458
- print "# The engine went global (gob upgrade): resolve it exactly as"
459
- print "# goblin-verify does - engine_dir:, then the machine default."
460
- print "CONFIG=\"$ROOT/.goblin/goblin.yaml\""
461
- print "ENGINE_DIR=$(sed -n \"s/^engine_dir:[[:space:]]*//p\" \"$CONFIG\" | head -n 1)"
462
- print "case \"$ENGINE_DIR\" in \"~\") ENGINE_DIR=\"$HOME\" ;; \"~\"/*) ENGINE_DIR=\"$HOME/${ENGINE_DIR#\\~/}\" ;; esac"
463
- print "if [ -z \"$ENGINE_DIR\" ] && [ -n \"${GOBLIN_ENGINE_DIR:-}\" ]; then ENGINE_DIR=$GOBLIN_ENGINE_DIR; fi"
464
- print "if [ -z \"$ENGINE_DIR\" ] && [ -d \"$HOME/.goblin/engine\" ]; then ENGINE_DIR=$HOME/.goblin/engine; fi"
465
- print ". \"$ENGINE_DIR/bin/goblin-lib.sh\""
466
- done=1; next
467
- }
468
- /^CONFIG="/ { next }
469
- /^\. \"\$ROOT\/\.goblin\/bin\/goblin-lib\.sh\"$/ { next }
470
- { print }
471
- ' "$GATE" > "$GATE.new" && mv "$GATE.new" "$GATE"
472
- GATE_NEW=$(sha256 "$GATE")
473
- python3 "$PYJ/rehash_record.py" "$INSTALLED" "checks/gate.sh" "$GATE_NEW" || {
474
- g_err "upgrade: re-hashing the rewritten gate.sh in $INSTALLED failed - the tree is uncommitted; fix: git checkout ."
475
- exit 2
476
- }
477
- fi
478
-
479
- # ---- the CI workflow: run line re-pointed to the pinned global form ----
480
- WF="$REPO/.github/workflows/goblin-gate.yml"
481
- if [ -f "$WF" ] && grep -q 'run: bash .goblin/bin/goblin-verify' "$WF"; then
482
- # Names a package W5 publishes; until then CI on a migrated repo is expected
483
- # red-by-absence — recorded in the commit message, not hidden. The workflow is
484
- # a files-RECORDED artifact, so this rewrite re-hashes its record entry here,
485
- # in the same commit (IN-02 hashes every recorded file; a rewritten artifact
486
- # with a stale recorded hash would fail verify #2 — measured this session).
487
- g_sed_i "s|run: bash .goblin/bin/goblin-verify|run: npx @techgoblin/gobstack@$CLI_VERSION run goblin-verify|" "$WF"
488
- WF_NEW=$(sha256 "$WF")
489
- python3 "$PYJ/rehash_record.py" "$INSTALLED" ".github/workflows/goblin-gate.yml" "$WF_NEW" || {
490
- g_err "upgrade: re-hashing the rewritten workflow in $INSTALLED failed - the tree is uncommitted; fix: git checkout ."
491
- exit 2
492
- }
493
- rm -rf "$PYJ"
494
- fi
495
-
496
- git -C "$REPO" add -A
497
- git -C "$REPO" -c commit.gpgsign=false commit -q -m "feat: migrate goblin-stack engine to global $ED (W3 step A)
498
-
499
- The record is rewritten BEFORE the deletion (the plan's order is measured
500
- wrong: deleting with the record untouched is IN-02 FAIL, W3-SPEC MA3).
501
- goblin.yaml gains the active engine_dir line; installed.json gains the
502
- engine block and drops exactly the engine entries (skills stay); gate.sh
503
- and the CI run line are re-pointed. The CI run line names
504
- npx @techgoblin/gobstack@$CLI_VERSION verify — a package W5 publishes; until then
505
- CI on a migrated repo is expected red-by-absence."
506
- COMMIT_A=$(git -C "$REPO" rev-parse --short HEAD)
507
- printf ' commit A: %s (record %s → %s entries)\n' "$COMMIT_A" "$REC_BEFORE" "$REC_AFTER"
508
-
509
- # ================================================================================ step 5
510
- # Verify #2 — green with both engines present. The vendored engine judges this
511
- # run (W1's A7 ordering); the point is that the rewritten record, declaration
512
- # and gate.sh parse and the tree is green MID-sequence. Red → no deletion; the
513
- # user reverts commit A.
514
- step_line 5 "verify #2 — green with both engines present"
515
- VERIFY_SCRIPT="$REPO/.goblin/bin/goblin-verify"
516
- V2=$(run_verify 2>&1); V2_RC=$?
517
- if [ "$V2_RC" -ne 0 ]; then
518
- printf '%s\n' "$V2"
519
- g_err "upgrade: verify #2 is red (exit $V2_RC) - nothing was deleted. fix: git revert $COMMIT_A"
520
- exit 1
521
- fi
522
- printf 'verify #2: %s\n' "$(printf '%s' "$V2" | grep -E 'passed,' | head -n 1 | sed 's/^[[:space:]]*//')"
523
-
524
- # ================================================================================ step 6
525
- # Commit B — the deletion (the only irreversible step). Exactly the payload list
526
- # the record just dropped; never skills/, installed.json, or any owned file.
527
- step_line 6 "commit B — git rm the engine payload (the only irreversible step)"
528
- EXPECT_N=$(vendored_list | grep -c .)
529
- DELETE_N=0
530
- while IFS= read -r rel; do
531
- [ -n "$rel" ] || continue
532
- case "$rel" in
533
- .goblin/bin/goblin-verify|.goblin/bin/goblin-lib.sh|.goblin/bin/goblin-audit|.goblin/bin/goblin-bans|\
534
- .goblin/manifest/*|.goblin/bans/*|.goblin/automations/*|.goblin/roles.yaml) : ;;
535
- *) g_err "upgrade: refusing to delete anything outside the engine-path list: $rel - aborting, nothing deleted"
536
- exit 1 ;;
537
- esac
538
- DELETE_N=$((DELETE_N + 1))
539
- done < <(vendored_list)
540
- if [ "$DELETE_N" -ne "$EXPECT_N" ]; then
541
- g_err "upgrade: payload list changed mid-run ($EXPECT_N → $DELETE_N) - aborting, nothing deleted"
542
- exit 1
543
- fi
544
- while IFS= read -r rel; do
545
- [ -n "$rel" ] || continue
546
- git -C "$REPO" rm -q "$rel"
547
- done < <(vendored_list)
548
- git -C "$REPO" -c commit.gpgsign=false commit -q -m "feat: remove the vendored engine payload (W3 step B; engine lives at $ED)
549
-
550
- The one-way door, split from commit A so the rollback for a red verify #3
551
- never has to resurrect a deleted file: \`git revert <A> <B>\` restores the
552
- pre-upgrade tree byte-for-byte (W3-SPEC §5.2)."
553
- COMMIT_B=$(git -C "$REPO" rev-parse --short HEAD)
554
- printf ' commit B: %s (%s files deleted)\n' "$COMMIT_B" "$DELETE_N"
555
-
556
- # ================================================================================ step 7
557
- # Verify #3 — the post-deletion gate, run against a committed tree (CM-03).
558
- # Expected, measured: 39 passed, 0 failed, 11 advisory, 32 skipped, rc 0 (MA5).
559
- step_line 7 "verify #3 — the post-deletion gate"
560
- if [ -f "$REPO/.goblin/bin/goblin-verify" ]; then
561
- VERIFY_SCRIPT="$REPO/.goblin/bin/goblin-verify"
562
- else
563
- VERIFY_SCRIPT="$ED/bin/goblin-verify"
564
- fi
565
- V3=$(run_verify 2>&1); V3_RC=$?
566
- if [ "$V3_RC" -ne 0 ]; then
567
- printf '%s\n' "$V3"
568
- g_err "upgrade: verify #3 is red (exit $V3_RC) - rollback: git revert $COMMIT_A $COMMIT_B"
569
- exit 1
570
- fi
571
- printf 'verify #3: %s\n' "$(printf '%s' "$V3" | grep -E 'passed,' | head -n 1 | sed 's/^[[:space:]]*//')"
572
-
573
- # ================================================================================ step 8
574
- step_line 8 "report"
575
- printf ' files moved: %s (now at %s)\n' "$DELETE_N" "$ED"
576
- printf ' record entries: %s -> %s\n' "$REC_BEFORE" "$REC_AFTER"
577
- printf ' engine identity: cli_sha256=%s enforcement_tsv_sha256=%s\n' "$RH" "$RT"
578
- printf ' commits: %s (A, declaration+record), %s (B, deletion)\n' "$COMMIT_A" "$COMMIT_B"
579
- printf ' rollback: git revert %s %s\n' "$COMMIT_A" "$COMMIT_B"
580
- exit 0