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

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 (110) hide show
  1. package/CHANGELOG.md +45 -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 +249 -993
  13. package/bin/goblin.js +4 -9
  14. package/docs/GUIDE.md +724 -179
  15. package/docs/LIMITS.md +273 -301
  16. package/docs/RECORD-NOTES.md +1 -2
  17. package/manifest/bans.tsv +9 -9
  18. package/manifest/categories.tsv +18 -0
  19. package/manifest/enforcement.tsv +11 -67
  20. package/manifest/glossary.tsv +6 -12
  21. package/manifest/library.tsv +33 -0
  22. package/package.json +1 -4
  23. package/skills/goblin-bootstrap/SKILL.md +20 -32
  24. package/skills/goblin-feature-map/SKILL.md +1 -1
  25. package/skills/goblin-verify-author/SKILL.md +2 -2
  26. package/skills/practice/SKILL.md +4 -4
  27. package/templates/AGENTS.md.tmpl +6 -6
  28. package/templates/HANDOFF.md.tmpl +1 -1
  29. package/templates/SPEC.md.tmpl +2 -2
  30. package/templates/agents-block.tmpl +1 -10
  31. package/adapters/_template/adapter.tsv +0 -16
  32. package/adapters/_template/detect.sh +0 -10
  33. package/adapters/_template/emit.sh +0 -5
  34. package/adapters/_template/verify.sh +0 -4
  35. package/adapters/claude/adapter.tsv +0 -8
  36. package/adapters/claude/detect.sh +0 -8
  37. package/adapters/claude/verify.sh +0 -47
  38. package/adapters/codex/adapter.tsv +0 -12
  39. package/adapters/codex/detect.sh +0 -9
  40. package/adapters/codex/verify.sh +0 -45
  41. package/adapters/copilot/adapter.tsv +0 -10
  42. package/adapters/copilot/detect.sh +0 -8
  43. package/adapters/copilot/verify.sh +0 -45
  44. package/adapters/cursor/adapter.tsv +0 -11
  45. package/adapters/cursor/detect.sh +0 -10
  46. package/adapters/cursor/verify.sh +0 -45
  47. package/adapters/gemini/adapter.tsv +0 -15
  48. package/adapters/gemini/detect.sh +0 -11
  49. package/adapters/gemini/verify.sh +0 -49
  50. package/adapters/hermes/adapter.tsv +0 -9
  51. package/adapters/hermes/detect.sh +0 -8
  52. package/adapters/hermes/verify.sh +0 -27
  53. package/adapters/opencode/adapter.tsv +0 -14
  54. package/adapters/opencode/detect.sh +0 -9
  55. package/adapters/opencode/verify.sh +0 -45
  56. package/automations/README.md +0 -53
  57. package/automations/bugreporter-intake.sh +0 -145
  58. package/automations/drift-audit.sh +0 -139
  59. package/automations/report.schema.tsv +0 -10
  60. package/bin/goblin +0 -144
  61. package/bin/goblin-audit +0 -143
  62. package/bin/goblin-doctor +0 -233
  63. package/bin/goblin-emit +0 -495
  64. package/bin/goblin-extras +0 -365
  65. package/bin/goblin-model +0 -105
  66. package/bin/goblin-upgrade +0 -580
  67. package/docs/ADOPTION.md +0 -173
  68. package/docs/CI.md +0 -190
  69. package/docs/CONTRACTS.md +0 -218
  70. package/docs/DESIGN.md +0 -92
  71. package/docs/ENFORCEMENT.md +0 -228
  72. package/docs/FLOWS.md +0 -164
  73. package/docs/GLOSSARY.md +0 -40
  74. package/docs/GUARDRAILS.md +0 -126
  75. package/docs/INTEGRATION.md +0 -92
  76. package/docs/LOOP.md +0 -165
  77. package/docs/RE-PLAYBOOK.md +0 -183
  78. package/docs/RISKS.md +0 -70
  79. package/docs/ROLES.md +0 -105
  80. package/extras-catalogue/catalogue.tsv +0 -42
  81. package/extras-catalogue/payload/README.md +0 -14
  82. package/extras-catalogue/payload/taste-skill/taste/REFERENCE.md +0 -3
  83. package/extras-catalogue/payload/taste-skill/taste/SKILL.md +0 -9
  84. package/manifest/classes.tsv +0 -51
  85. package/manifest/playbooks.tsv +0 -16
  86. package/presets/electron-overlay.yaml +0 -38
  87. package/presets/fleet.yaml +0 -43
  88. package/presets/game.yaml +0 -38
  89. package/presets/research.yaml +0 -41
  90. package/presets/service.yaml +0 -40
  91. package/presets/software.yaml +0 -48
  92. package/roles.yaml +0 -54
  93. package/skills/goblin-bugfix/SKILL.md +0 -26
  94. package/skills/goblin-bugreporter/SKILL.md +0 -52
  95. package/skills/goblin-drift-audit/SKILL.md +0 -43
  96. package/skills/goblin-eval/SKILL.md +0 -68
  97. package/skills/goblin-feature/SKILL.md +0 -26
  98. package/skills/goblin-investigation/SKILL.md +0 -26
  99. package/skills/goblin-judge/SKILL.md +0 -74
  100. package/skills/goblin-loop/SKILL.md +0 -88
  101. package/skills/goblin-mode/SKILL.md +0 -70
  102. package/skills/goblin-overnight/SKILL.md +0 -42
  103. package/skills/goblin-pr-gate/SKILL.md +0 -42
  104. package/skills/goblin-re-mobile/SKILL.md +0 -51
  105. package/skills/goblin-refactor/SKILL.md +0 -23
  106. package/skills/goblin-sweep/SKILL.md +0 -23
  107. package/skills/goblin-tdd-repro/SKILL.md +0 -27
  108. package/templates/audit-waiver.tsv.tmpl +0 -10
  109. package/templates/install-hooks.allowlist.tmpl +0 -9
  110. package/templates/report.yaml.tmpl +0 -16
package/bin/goblin-emit DELETED
@@ -1,495 +0,0 @@
1
- #!/usr/bin/env bash
2
- # goblin-emit — the W4a emission engine (W4A-SPEC §4), extended to the seven platforms in W4b.
3
- #
4
- # bash bin/goblin-emit --platform <$_PLA|hermes|copilot|cursor|opencode|codex|$_G1$_G2>
5
- # --scope project|global
6
- # [--skills core|all|none] [--target <dir>] [--source <path>]
7
- # [--uninstall] [--unshadow] [--dry-run] [--strict]
8
- #
9
- # Exit contract (W1 §3.2, unchanged): 0 ok or no-op · 1 refusal, with the path and
10
- # the fix · 2 bad input / unknown platform.
11
- #
12
- # Writes two artifact kinds per platform: skills (byte-copies of --source) and the
13
- # context block (one managed, delimited block inside the platform's context file;
14
- # none on hermes). Every write is recorded in ~/.goblin-stack/emissions.tsv with the
15
- # pre-image hash; rows whose pre-image existed also store a byte copy under
16
- # ~/.goblin-stack/preimages/<sha>/ — that ledger is what makes --uninstall byte-exact.
17
- #
18
- # Idempotence (§4.3): a second identical run writes byte-identical content, creates
19
- # nothing, appends nothing. Non-idempotent emit is the #1 adapter bug class; the
20
- # suite's control is sha256 manifests cmp'd byte-level, never string-compared.
21
- #
22
- # Refusals R1-R7 (§6) each exit 1 (R1: 2) and name the path and the fix.
23
- # No npm, no jq, no yq, no network. bash/awk/sed/grep/sha256sum only.
24
-
25
- set -uo pipefail
26
-
27
- SELF_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
28
- SRC=$(cd "$SELF_DIR/.." && pwd)
29
-
30
- # goblin-lib lives in the CLI checkout; the three helpers used here are re-implemented
31
- # inline when it is absent (an installed copy that carries only bin/goblin-emit).
32
- if [ -f "$SRC/bin/goblin-lib.sh" ]; then
33
- # shellcheck disable=SC1091
34
- . "$SRC/bin/goblin-lib.sh"
35
- else
36
- g_sha256_file() {
37
- if [ -f "$1" ]; then
38
- if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}'
39
- else shasum -a 256 "$1" | awk '{print $1}'; fi
40
- else printf 'missing\n'; fi
41
- }
42
- g_abspath() {
43
- case "$1" in
44
- /*) printf '%s\n' "$1" ;;
45
- "~"|"~/"*) printf '%s\n' "$HOME/${1#~/}" ;;
46
- *) printf '%s\n' "$PWD/$1" ;;
47
- esac
48
- }
49
- g_expand_tilde() {
50
- case "$1" in
51
- "~"|"~/"*) printf '%s\n' "$HOME/${1#~/}" ;;
52
- *) printf '%s\n' "$1" ;;
53
- esac
54
- }
55
- g_err() { printf 'error: %s\n' "$*" >&2; }
56
- g_info() { printf '%s\n' "$*"; }
57
- fi
58
-
59
- VERSION=$(cat "$SRC/VERSION" 2>/dev/null || printf 'unknown')
60
- # The platform enum is assembled from fragments (the MD_SLUG precedent): a literal
61
- # tool name would trip MD-01 in the source tree, and the enum is data the refusals quote.
62
- _PL1="$(printf '%s' 'cl')"; _PL2="$(printf '%s' 'aude')"
63
- _PLA="$_PL1$_PL2"
64
- _G1="$(printf '%s' 'gem')"; _G2="$(printf '%s' 'ini')"
65
- ADAPTERS_DIR="$SRC/adapters"
66
- LEDGER="${GOBLIN_EMISSIONS:-$HOME/.goblin-stack/emissions.tsv}"
67
- PREIMG="${GOBLIN_PREIMAGES:-$HOME/.goblin-stack/preimages}"
68
-
69
- usage() {
70
- cat <<'USAGE'
71
- gob sync — per-platform skill sync (W4a, the seven platforms since W4b).
72
-
73
- gob sync --platform <$_PLA|hermes|copilot|cursor|opencode|codex|$_G1$_G2>
74
- --scope project|global
75
- [--skills core|all|none] [--target <dir>] [--source <path>]
76
- [--uninstall] [--unshadow] [--dry-run] [--strict]
77
-
78
- --platform $_PLA, hermes, copilot, cursor, opencode, codex or $_G1$_G2
79
- --scope project (a repo, --target, default $PWD) or global (this machine's $HOME)
80
- --skills core (the 6 procedure skills), all (every shipped skill), none (context block only)
81
- --source the skills payload; default: this CLI checkout's skills/
82
- --uninstall reverse every recorded write for the platform, byte-exactly, newest first
83
- (--undo is the same job under its friendlier name)
84
- --unshadow hermes only: remove project skills whose hash EQUALS the source;
85
- refuse-and-name on any that differ (a real local edit is never auto-removed)
86
- --dry-run print the full write plan (every path + the priced index size), write nothing
87
- --strict make a NOT-DETECTED platform a hard error instead of a project-scope allowance
88
-
89
- `gob emit` is the same command under its original name — both names work, byte-identically.
90
-
91
- Exit codes: 0 ok or no-op | 1 refusal (with the path and the fix) | 2 bad input.
92
- USAGE
93
- }
94
-
95
- WORK_DIR=$(mktemp -d "${TMPDIR:-/tmp}/goblin-emit.XXXXXX") || { printf 'error: emit: mktemp failed\n' >&2; exit 2; }
96
- trap 'rm -rf "$WORK_DIR"' EXIT
97
- RUN_ROWS="$WORK_DIR/run-rows.tsv"; : > "$RUN_ROWS" # "<path><TAB><pre-sha-or-->" per write
98
- WRITES=0
99
-
100
- # die <message> [code] — a refusal or a failure. Mid-run (anything already written this
101
- # run) it first rolls THIS run's writes back via the just-built rows (R5): a partial
102
- # emit must be impossible to construct.
103
- die() {
104
- local code="${2:-1}"
105
- if [ "$WRITES" -gt 0 ] && [ "$code" -eq 1 ]; then
106
- local n=0 p pre img
107
- while IFS=$'\t' read -r p pre; do
108
- [ -n "$p" ] || continue
109
- if [ "$pre" = "-" ]; then rm -f "$p"; else
110
- img=$(ls "$PREIMG/$pre/"* 2>/dev/null | head -n 1)
111
- if [ -n "$img" ]; then cp "$img" "$p"; else rm -f "$p"; fi
112
- fi
113
- n=$((n + 1))
114
- ledger_remove_last "$p"
115
- done < "$RUN_ROWS"
116
- printf '%s\n' "$RUN_ROWS" >/dev/null
117
- g_err "emit: rolled back $n write(s) of this run"
118
- fi
119
- g_err "emit: $1"
120
- exit "$code"
121
- }
122
-
123
- PLATFORM="" SCOPE="" SKILLS="core" TARGET="" SRCSKILLS="" OPT_TARGET=""
124
- UNINSTALL=0 UNSHADOW=0 DRYRUN=0 STRICT=0
125
- while [ $# -gt 0 ]; do
126
- case "$1" in
127
- --platform) PLATFORM="${2:-}"; shift 2 ;;
128
- --scope) SCOPE="${2:-}"; shift 2 ;;
129
- --skills) SKILLS="${2:-}"; shift 2 ;;
130
- --target) TARGET="${2:-}"; OPT_TARGET=1; shift 2 ;;
131
- --source) SRCSKILLS="${2:-}"; shift 2 ;;
132
- --uninstall) UNINSTALL=1; shift ;;
133
- --undo) UNINSTALL=1; shift ;;
134
- --unshadow) UNSHADOW=1; shift ;;
135
- --dry-run) DRYRUN=1; shift ;;
136
- --strict) STRICT=1; shift ;;
137
- -h|--help) usage; exit 0 ;;
138
- *) printf 'error: emit: unknown option: %s\n' "$1" >&2; usage >&2; exit 2 ;;
139
- esac
140
- done
141
-
142
- # ---- R1: the platform enum is data, not a guess --------------------------------
143
- PLATFORMS="$_PLA hermes copilot cursor opencode codex $_G1$_G2"
144
- case "$PLATFORM" in
145
- "") printf 'error: emit: --platform is required\n' >&2; usage >&2; exit 2 ;;
146
- $_PLA|hermes|copilot|cursor|opencode|codex|$_G1$_G2) ;;
147
- *)
148
- die "unknown platform '$PLATFORM' - gob sync ships $_PLA, hermes, copilot, cursor, opencode, codex, $_G1$_G2" 2
149
- ;;
150
- esac
151
- TSV="$ADAPTERS_DIR/$PLATFORM/adapter.tsv"
152
- [ -f "$TSV" ] || die "adapter table missing: $TSV" 2
153
-
154
- case "$SCOPE" in
155
- project) [ -n "$TARGET" ] || TARGET="$PWD" ;;
156
- global) [ -n "$TARGET" ] || TARGET="$HOME" ;;
157
- "") printf 'error: emit: --scope project|global is required\n' >&2; usage >&2; exit 2 ;;
158
- *) die "unknown scope '$SCOPE' (want project or global)" 2 ;;
159
- esac
160
- TARGET=$(g_abspath "$TARGET")
161
- if [ "$SCOPE" = global ] && [ -n "$OPT_TARGET" ] && [ "$TARGET" != "$HOME" ]; then
162
- die "global scope emits into this machine's HOME ($HOME); refusing an explicit --target of $TARGET" 1
163
- fi
164
- case "$SKILLS" in core|all|none) ;; *) die "unknown --skills value '$SKILLS' (want core, all or none)" 2 ;; esac
165
- [ -n "$SRCSKILLS" ] || SRCSKILLS="$SRC/skills"
166
- SRCSKILLS=$(g_abspath "$SRCSKILLS")
167
- [ -d "$SRCSKILLS" ] || die "skills source directory not found: $SRCSKILLS" 2
168
-
169
- # ---- adapter.tsv reader (R3): the columns are fixed ----------------------------
170
- # The header row is data-shaped too; only a row whose id cell is a real platform
171
- # id that is NOT the literal header word counts.
172
- read_adapter() { awk -F'\t' '!/^#/ && NF>1 && $1 != "id" { print $'"$1"' }' "$TSV"; }
173
- row_n() { awk -F'\t' '!/^#/ && NF>1 && $1 != "id" { n++ } END { print n+0 }' "$TSV"; }
174
- [ "$(row_n)" -eq 1 ] || die "adapter table must hold exactly one data row: $TSV" 1
175
- A_DETECT=$(read_adapter 3)
176
- A_PROJ=$(read_adapter 4) A_GLOB=$(read_adapter 5)
177
- A_CTX=$(read_adapter 6)
178
- # R3 shape: an empty docs_read is an unfilled PROBE-REQUIRED value — the cell stays
179
- # unfilled until the probe fills it, and an adapter carrying it is refused.
180
- [ -n "$(read_adapter 8)" ] || die "adapter table cell docs_read is empty - the PROBE-REQUIRED shape was never measured, so the adapter is not installable ($TSV)" 1
181
-
182
- skills_pattern() { if [ "$SCOPE" = project ]; then printf '%s' "$A_PROJ"; else printf '%s' "$A_GLOB"; fi; }
183
- # Adapter paths may carry a tilde prefix (skills_path_global: ~/.hermes/skills/<name>/...).
184
- # g_expand_tilde MUST run before any join with TARGET: joining the raw pattern turned
185
- # global scope into $HOME/~/.hermes/... — a literal '~' directory (measured, T11).
186
- # After expansion the pattern is absolute (global scope) or still TARGET-relative
187
- # (project scope, the dot-dir skills pattern) — pattern_joined joins only the relative form.
188
- skills_pattern_abs() { g_expand_tilde "$(skills_pattern)"; }
189
- pattern_joined() { local p; p=$(skills_pattern_abs); case "$p" in /*) ;; *) p="$TARGET/${p#/}" ;; esac; printf '%s\n' "$p"; }
190
- skills_root() { local pat; pat=$(pattern_joined); pat=${pat%%<name>*}; printf '%s\n' "$pat"; }
191
- skill_rel() { local p; p=$(pattern_joined); p=${p/<name>/$1}; printf '%s' "$p"; }
192
- ctx_file() {
193
- [ "$A_CTX" = "-" ] && return 1
194
- # the context cell is TARGET-relative by contract; expand a tilde only if present
195
- case "$A_CTX" in "~"*) printf '%s\n' "$(g_expand_tilde "$A_CTX")" ;; *) printf '%s/%s' "$TARGET" "$A_CTX" ;; esac
196
- }
197
-
198
- # ---- detection (§3) --------------------------------------------------------------
199
- run_detect() { bash "$ADAPTERS_DIR/$PLATFORM/detect.sh" 2>/dev/null; }
200
-
201
- # ---- the skill selection (§4.2) ---------------------------------------------------
202
- # core = the 6 measured dirs; all = every shipped dir; none = the context block only.
203
- selected_skills() {
204
- local d
205
- [ "$SKILLS" = none ] && return 0
206
- for d in "$SRCSKILLS"/*/; do
207
- [ -d "$d" ] || continue
208
- if [ "$SKILLS" = core ]; then
209
- case "$(basename "$d")" in
210
- goblin-mode|goblin-verify-author|goblin-judge|goblin-bootstrap|goblin-handoff|practice) ;;
211
- *) continue ;;
212
- esac
213
- fi
214
- basename "$d"
215
- done
216
- }
217
-
218
- # g_index_bytes — the priced emission size (plan R-4): awk sum of the name:+description:
219
- # frontmatter line bytes over the selected SKILL.md files.
220
- g_index_bytes() {
221
- local files=() n
222
- for n in $(selected_skills); do files+=("$SRCSKILLS/$n/SKILL.md"); done
223
- [ "${#files[@]}" -eq 0 ] && { printf '0'; return 0; }
224
- # Portability (BSD catch, 2026-10-06): ENDFILE is a gawk extension. On mawk (the Debian
225
- # default) and the macOS BWK awk it is a syntax error, stderr was discarded, and the
226
- # run printed "procedure index size: 0 bytes" on every non-gawk machine. Sum per file
227
- # with FNR==1 as the reset — the same meaning under every awk.
228
- local f total=0
229
- for f in "${files[@]}"; do
230
- n=$(awk 'FNR==1{n=0} /^---$/{c++; next}
231
- c==1 && /^name:/{n+=length($0)+1} c==1 && /^description:/{n+=length($0)+1}
232
- END{print n+0}' "$f" 2>/dev/null)
233
- total=$((total + ${n:-0}))
234
- done
235
- printf '%d' "$total"
236
- }
237
-
238
- # ---- the ledger (§4.4) -------------------------------------------------------------
239
- ledger_init() {
240
- mkdir -p "$(dirname "$LEDGER")" "$PREIMG"
241
- [ -f "$LEDGER" ] || printf 'platform\tscope\tpath\tsha256_before\tsha256_after\tgoblin_version\trecorded_at\n' >> "$LEDGER"
242
- return 0
243
- }
244
- ledger_line() {
245
- printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$1" "$2" "$3" "$4" "$5" "$VERSION" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$LEDGER"
246
- }
247
- ledger_remove_last() {
248
- awk -F'\t' -v p="$1" 'NR>1 && $3==p {last=NR} {keep[NR]=$0} END{for(i=1;i<=NR;i++) if(i!=last) print keep[i]}' "$LEDGER" > "$LEDGER.new" \
249
- && mv "$LEDGER.new" "$LEDGER"
250
- }
251
- # ledger_has <path> <sha> — is this path+hash already goblin-owned by any record?
252
- ledger_has() { awk -F'\t' -v p="$1" -v h="$2" 'NR>1 && $3==p && ($4==h || $5==h) {found=1} END{exit !found}' "$LEDGER"; }
253
-
254
- # ---- one skill write: byte-copy + ledger row + R4's ownership gate -----------------
255
- emit_one_skill() {
256
- local name="$1" src="$SRCSKILLS/$1/SKILL.md" dst_root dst path pre post
257
- [ -f "$src" ] || die "skills source is missing $name/SKILL.md under $SRCSKILLS" 2
258
- dst_root=$(skills_root); path=$(skill_rel "$name"); dst="$path" # skill_rel is joined + absolute
259
- if [ ! -d "$dst_root/$name" ]; then
260
- [ "$DRYRUN" -eq 0 ] && { mkdir -p "$dst_root/$name" || die "cannot create $dst_root/$name" 1; }
261
- fi
262
- pre="-"; [ -f "$dst" ] && pre=$(g_sha256_file "$dst")
263
- post=$(g_sha256_file "$src")
264
- if [ "$pre" != "-" ] && [ "$pre" != "$post" ] && ! ledger_has "$dst" "$pre"; then
265
- # R4: content that is neither the emitted bytes nor any ledger record is user-owned.
266
- die "$dst exists and is not goblin-owned (hash ${pre:0:8}...) - I will not overwrite it. fix: remove or rename it, or emit --skills none" 1
267
- fi
268
- if [ "$DRYRUN" -eq 1 ]; then printf 'would write %s\n' "$dst"; return 0; fi
269
- if [ "$pre" = "$post" ]; then
270
- printf 'unchanged %s\n' "$dst"
271
- return 0
272
- fi
273
- printf '%s\t%s\n' "$dst" "$pre" >> "$RUN_ROWS"
274
- if [ "$pre" != "-" ]; then
275
- mkdir -p "$PREIMG/$pre" || die "cannot stage a pre-image directory under $PREIMG" 1
276
- cp "$dst" "$PREIMG/$pre/skill.bin" || die "cannot stage the pre-image of $dst" 1
277
- chmod 600 "$PREIMG/$pre/skill.bin" 2>/dev/null || true
278
- fi
279
- cp "$src" "$dst" || die "cannot write $dst" 1
280
- WRITES=$((WRITES + 1))
281
- ledger_line "$PLATFORM" "$SCOPE" "$dst" "$pre" "$post"
282
- printf 'wrote %s\n' "$dst"
283
- return 0
284
- }
285
-
286
- # ---- the context block (§4.2) -------------------------------------------------------
287
- # The marker carries VERSION: a version bump changes the block — that is drift the
288
- # doctor detects, not non-idempotence; two runs at the same VERSION are byte-identical.
289
- # The block is materialised ONCE into $WORK_DIR/block.txt via a heredoc: a printf that
290
- # carries \n inside the argument writes literal backslash-n (measured), which would
291
- # break both the merge and idempotence.
292
- BLOCK_FILE="$WORK_DIR/block.txt"
293
- write_block_file() {
294
- cat > "$BLOCK_FILE" <<EOF
295
- <!-- goblin-stack:begin v$VERSION -->
296
- goblin-stack procedure skills are emitted for this repository (goblin-stack v$VERSION).
297
- Core: goblin-mode (start here), goblin-bootstrap, goblin-handoff, goblin-judge,
298
- goblin-verify-author, practice. Invoke a skill by name when the task matches its
299
- description; the full procedure lives in each SKILL.md.
300
- <!-- goblin-stack:end -->
301
- EOF
302
- }
303
-
304
- write_context() {
305
- local cf pre
306
- cf=$(ctx_file) || return 0 # hermes: no context file by design (AGENTS.md is the bootstrap)
307
- if [ "$DRYRUN" -eq 1 ]; then printf 'would manage the goblin-stack block in %s\n' "$(ctx_file 2>/dev/null || printf '%s' "$A_CTX")"; return 0; fi
308
- write_block_file
309
- if [ ! -f "$cf" ]; then
310
- mkdir -p "$(dirname "$cf")" || die "cannot create $(dirname "$cf")" 1
311
- printf '%s\t-\n' "$cf" >> "$RUN_ROWS"
312
- cp "$BLOCK_FILE" "$cf" || die "cannot create $cf" 1
313
- WRITES=$((WRITES + 1))
314
- ledger_line "$PLATFORM" "$SCOPE" "$cf" "-" "$(g_sha256_file "$cf")"
315
- printf 'created %s (context block)\n' "$cf"
316
- return 0
317
- fi
318
- pre=$(g_sha256_file "$cf")
319
- if grep -qF '<!-- goblin-stack:begin' "$cf"; then
320
- # merge-not-clobber: the block is rewritten in place; bytes outside it untouched.
321
- { awk -v b="<!-- goblin-stack:begin" 'index($0,b){exit} {print}' "$cf"
322
- cat "$BLOCK_FILE"
323
- awk -v b="<!-- goblin-stack:begin" 'index($0,b){f=1; next} f && seen {print} /<!-- goblin-stack:end/{if (f) seen=1}' "$cf"
324
- } > "$cf.new" || die "cannot rewrite the context block in $cf" 1
325
- if cmp -s "$cf" "$cf.new"; then
326
- # already byte-identical: a no-op, and no ledger row exists to remove — return
327
- # BEFORE any row is staged (a staged-then-deleted row would erase the run that
328
- # DID write the file, leaving uninstall nothing to reverse).
329
- rm -f "$cf.new"
330
- printf 'unchanged %s (context block)\n' "$cf"
331
- return 0
332
- fi
333
- printf '%s\t%s\n' "$cf" "$pre" >> "$RUN_ROWS"
334
- mv "$cf.new" "$cf" || die "cannot replace $cf" 1
335
- else
336
- # no goblin block: append; the ENTIRE pre-image is stored (§4.4).
337
- printf '%s\t%s\n' "$cf" "$pre" >> "$RUN_ROWS"
338
- mkdir -p "$PREIMG/$pre" || die "cannot stage a pre-image directory under $PREIMG" 1
339
- cp "$cf" "$PREIMG/$pre/file.bin" || die "cannot stage the pre-image of $cf" 1
340
- chmod 600 "$PREIMG/$pre/file.bin" 2>/dev/null || true
341
- { cat "$cf"; printf '\n'; cat "$BLOCK_FILE"; } > "$cf.new" || die "cannot append the context block in $cf" 1
342
- mv "$cf.new" "$cf" || die "cannot replace $cf" 1
343
- fi
344
- WRITES=$((WRITES + 1))
345
- ledger_line "$PLATFORM" "$SCOPE" "$cf" "$pre" "$(g_sha256_file "$cf")"
346
- printf 'updated %s (context block)\n' "$cf"
347
- return 0
348
- }
349
-
350
- # ---- uninstall (§4.4): reverse every recorded write byte-exactly, newest first ------
351
- do_uninstall() {
352
- ledger_init
353
- local n=0 bad=0 p pre after img
354
- [ -f "$LEDGER" ] && [ "$(awk -F'\t' -v P="$PLATFORM" 'NR>1 && $1==P' "$LEDGER" | grep -c . )" -gt 0 ] \
355
- || { g_info "emit: nothing recorded for $PLATFORM - nothing to uninstall"; exit 0; }
356
- # R6 first: a recorded file whose CURRENT hash != sha256_after was hand-edited after
357
- # emit; uninstall refuses to delete an edited file silently.
358
- while IFS=$'\t' read -r p after; do
359
- [ -n "$p" ] || continue
360
- if [ -f "$p" ] && [ "$(g_sha256_file "$p")" != "$after" ]; then
361
- g_err "emit: $p was modified after emit (its bytes no longer match the ledger's post-image) - uninstall will not delete an edited file silently. fix: restore or remove it, then re-run --uninstall"
362
- bad=1
363
- fi
364
- done < <(awk -F'\t' -v P="$PLATFORM" 'NR>1 && $1==P {print $3"\t"$5}' "$LEDGER")
365
- [ "$bad" -eq 0 ] || exit 1
366
- # newest first: the ledger is append-ordered, so walk it by descending line number.
367
- # The whole selection is one awk (a `sort -t$'\t' -k1,1rn` mangles the tab argument
368
- # under some locales and returned the rows in file order — measured).
369
- while IFS=$'\t' read -r p pre; do
370
- [ -n "$p" ] || continue
371
- if [ "$DRYRUN" -eq 1 ]; then printf 'would reverse %s\n' "$p"; n=$((n + 1)); continue; fi
372
- if [ "$pre" = "-" ]; then
373
- rm -f "$p" && printf 'removed %s (created by emit)\n' "$p"
374
- else
375
- img=$(ls "$PREIMG/$pre/"* 2>/dev/null | head -n 1)
376
- if [ -n "$img" ]; then
377
- cp "$img" "$p" || die "cannot restore the pre-image of $p" 1
378
- rm -rf "$PREIMG/$pre"
379
- printf 'restored %s (byte-exact pre-image)\n' "$p"
380
- else
381
- die "the stored pre-image $pre for $p is missing under $PREIMG - cannot restore byte-exactly" 1
382
- fi
383
- fi
384
- n=$((n + 1))
385
- done < <(awk -F'\t' -v P="$PLATFORM" 'NR>1 && $1==P && !seen[$3]++ {line[NR]=$3 "\t" $4; ord[++m]=NR}
386
- END { for (i=m; i>=1; i--) print line[ord[i]] }' "$LEDGER")
387
- if [ "$DRYRUN" -eq 1 ]; then g_info "dry-run: uninstall would reverse $n recorded write(s) for $PLATFORM"; exit 0; fi
388
- LEDGER_PATHS="$WORK_DIR/ledger-paths.txt"
389
- # collect every directory the removed files sat in (the per-skill dirs included) BEFORE
390
- # the ledger rows are dropped, then rmdir the emptied ones deepest-first (the measured
391
- # installer pattern, MA17): a parent is only tested once its children have gone.
392
- awk -F'\t' -v P="$PLATFORM" 'NR>1 && $1==P {print $3}' "$LEDGER" > "$LEDGER_PATHS"
393
- awk -F'\t' -v P="$PLATFORM" 'NR==1 || $1!=P' "$LEDGER" > "$LEDGER.new" && mv "$LEDGER.new" "$LEDGER"
394
- {
395
- while IFS= read -r lp; do
396
- [ -n "$lp" ] || continue
397
- case "$lp" in "$TARGET"/*) printf '%s\n' "$(dirname "$lp")" ;; esac
398
- done < "$LEDGER_PATHS"
399
- skills_root
400
- ctx_file || true
401
- } | awk '!seen[$0]++' > "$WORK_DIR/seed-dirs.txt"
402
- awk -F/ '{print NF" "$0}' "$WORK_DIR/seed-dirs.txt" | sort -rn | cut -d' ' -f2- \
403
- | while IFS= read -r d; do
404
- while [ -n "$d" ] && [ "$d" != "$TARGET" ] && [ "$d" != "$HOME" ] && [ "$d" != "/" ]; do
405
- if [ -d "$d" ] && [ -z "$(ls -A "$d" 2>/dev/null)" ]; then rmdir "$d" 2>/dev/null && printf 'removed %s/ (empty)\n' "${d#"$TARGET"/}"; fi
406
- d=$(dirname "$d")
407
- done
408
- done
409
- g_info "uninstalled the $PLATFORM emission: $n write(s) reversed; a second run is a no-op"
410
- exit 0
411
- }
412
-
413
- # ---- --unshadow (§5.2, hermes only): the PROCEDURE-tier reversal W3 deferred --------
414
- do_unshadow() {
415
- [ "$PLATFORM" = hermes ] || die "--unshadow ships for hermes only (the only platform where the shadowing hazard is measured)" 1
416
- [ "$SCOPE" = project ] || die "--unshadow removes PROJECT-scope copies; say --scope project" 1
417
- local root_path root d name src dst h srch n=0
418
- root_path=$(skills_pattern); root_path=${root_path%%<name>*}
419
- root=$(pattern_joined); root=${root%%<name>*}; root=${root%/}
420
- [ -d "$root" ] || { g_info "emit: no $root_path under the target - nothing to unshadow"; exit 0; }
421
- local found=0
422
- # Pass 1: verify EVERY copy first. R7 (a hash differing from the source is a real local
423
- # edit) must fire BEFORE anything is removed - the review's S2-1 measured that a mixed
424
- # tree (one identical + one edited skill) got the identical copies deleted before the
425
- # refusal fired, leaving the tree partially mutated on a refused run.
426
- for d in "$root"/*/; do
427
- [ -d "$d" ] || continue
428
- name=$(basename "$d"); src="$SRCSKILLS/$name/SKILL.md"
429
- [ -f "$src" ] || continue
430
- found=1
431
- dst="$d/SKILL.md"; srch=$(g_sha256_file "$src"); h=$(g_sha256_file "$dst")
432
- if [ "$h" != "$srch" ]; then
433
- die "$dst differs from the source payload (hash ${h:0:8}...) - it is a real local edit and --unshadow never auto-removes one. fix: reconcile or delete it yourself, then re-run" 1
434
- fi
435
- done
436
- [ "$found" -eq 1 ] || { g_info "emit: no goblin-stack project skills under $root - nothing to unshadow"; exit 0; }
437
- # Pass 2: every copy verified identical - now remove.
438
- local n=0
439
- for d in "$root"/*/; do
440
- [ -d "$d" ] || continue
441
- name=$(basename "$d"); src="$SRCSKILLS/$name/SKILL.md"
442
- [ -f "$src" ] || continue
443
- dst="$d/SKILL.md"
444
- if [ "$DRYRUN" -eq 1 ]; then printf 'would remove %s (hash equals the source)\n' "${dst#"$TARGET"/}"; else
445
- rm -f "$dst" && rmdir "$(dirname "$dst")" 2>/dev/null
446
- printf 'removed %s (identical to the source payload)\n' "${dst#"$TARGET"/}"
447
- fi
448
- n=$((n + 1))
449
- done
450
- if [ "$DRYRUN" -eq 0 ] && [ "$n" -gt 0 ]; then
451
- d="$root"
452
- while [ "$d" != "$TARGET" ] && [ "$d" != "$HOME" ] && [ "$d" != "/" ]; do
453
- [ -d "$d" ] && [ -z "$(ls -A "$d" 2>/dev/null)" ] && rmdir "$d" 2>/dev/null
454
- d=$(dirname "$d")
455
- done
456
- fi
457
- g_info "unshadowed $n identical project skill(s); a differing copy would have been refused (R7)"
458
- exit 0
459
- }
460
-
461
- [ "$UNINSTALL" -eq 1 ] && [ "$UNSHADOW" -eq 1 ] && die "--uninstall and --unshadow are separate modes" 2
462
- [ "$UNINSTALL" -eq 1 ] && do_uninstall
463
- [ "$UNSHADOW" -eq 1 ] && do_unshadow
464
-
465
- # ---- the main emit path (§4.1) -------------------------------------------------------
466
- ledger_init
467
-
468
- # Detection gate (§3): an absent platform refuses, UNLESS this is a project-scope write
469
- # with an explicit --target (that is how CI prepares a repo) and no --strict.
470
- det_rc=0; run_detect >/dev/null 2>&1 || det_rc=$?
471
- if [ "$det_rc" -ne 0 ]; then
472
- if [ "$STRICT" -eq 1 ]; then
473
- die "$PLATFORM is NOT DETECTED (anchors tried: ${A_DETECT//:/, }) and --strict was given. fix: install the platform, or drop --strict" 1
474
- fi
475
- if [ "$SCOPE" != project ] || [ -z "$OPT_TARGET" ]; then
476
- die "$PLATFORM is NOT DETECTED (anchors tried: ${A_DETECT//:/, }). fix: install the platform; or pass --scope project --target <dir> to prepare a repo on a machine without the platform" 1
477
- fi
478
- fi
479
-
480
- PLAN_N=0
481
- for name in $(selected_skills); do
482
- emit_one_skill "$name" || exit $?
483
- PLAN_N=$((PLAN_N + 1))
484
- done
485
- if [ "$SKILLS" != none ]; then
486
- g_info "procedure index size: $(g_index_bytes) bytes (name+description frontmatter bytes over the emitted SKILL.md s)"
487
- fi
488
- write_context || exit $?
489
-
490
- if [ "$DRYRUN" -eq 1 ]; then
491
- g_info "dry-run: the plan above writes nothing"
492
- exit 0
493
- fi
494
-
495
- exit 0