ai-first-os 1.9.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 (90) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +226 -0
  3. package/bin/ai-first-os.js +72 -0
  4. package/core/CLAUDE.md +148 -0
  5. package/core/VERSION +1 -0
  6. package/core/agents/ambiguous-spec.md +39 -0
  7. package/core/agents/complete-spec.md +36 -0
  8. package/core/agents/scout.md +25 -0
  9. package/core/install.sh +243 -0
  10. package/core/lib/archive.sh +347 -0
  11. package/core/lib/bootstrap.sh +276 -0
  12. package/core/lib/capture.sh +188 -0
  13. package/core/lib/check-resolvable.sh +172 -0
  14. package/core/lib/close-session.sh +741 -0
  15. package/core/lib/common.sh +1773 -0
  16. package/core/lib/focus-read.sh +282 -0
  17. package/core/lib/manual-link.sh +111 -0
  18. package/core/lib/markdown.py +304 -0
  19. package/core/lib/migrate-backlogs.sh +207 -0
  20. package/core/lib/migrate-canonicals.sh +523 -0
  21. package/core/lib/mount-repo.sh +543 -0
  22. package/core/lib/my-wiki-gates.sh +137 -0
  23. package/core/lib/my-wiki.sh +202 -0
  24. package/core/lib/my_wiki_build.py +548 -0
  25. package/core/lib/my_wiki_graph.py +137 -0
  26. package/core/lib/my_wiki_overview.py +392 -0
  27. package/core/lib/new-memory.sh +160 -0
  28. package/core/lib/new-product.sh +122 -0
  29. package/core/lib/new-workspace.sh +105 -0
  30. package/core/lib/pack-install.sh +114 -0
  31. package/core/lib/recall.sh +235 -0
  32. package/core/lib/recall_index.py +295 -0
  33. package/core/lib/remote-backup.sh +84 -0
  34. package/core/lib/rename-heads.sh +354 -0
  35. package/core/lib/rename-workspaces.sh +271 -0
  36. package/core/lib/session-read.sh +203 -0
  37. package/core/lib/session-start.sh +854 -0
  38. package/core/lib/supersede.sh +436 -0
  39. package/core/lib/sweep.sh +473 -0
  40. package/core/manual/COMO-FUNCIONA.md +251 -0
  41. package/core/manual/HOW-IT-WORKS.md +250 -0
  42. package/core/resolver.md +30 -0
  43. package/core/skills/archive.md +95 -0
  44. package/core/skills/bootstrap.md +226 -0
  45. package/core/skills/capture.md +65 -0
  46. package/core/skills/check-resolvable.md +38 -0
  47. package/core/skills/close-session.md +108 -0
  48. package/core/skills/grill.md +74 -0
  49. package/core/skills/mount-repo.md +97 -0
  50. package/core/skills/my-wiki.md +103 -0
  51. package/core/skills/new-memory.md +76 -0
  52. package/core/skills/new-product.md +55 -0
  53. package/core/skills/new-spec.md +114 -0
  54. package/core/skills/new-workspace.md +53 -0
  55. package/core/skills/recall.md +64 -0
  56. package/core/skills/rename-heads.md +72 -0
  57. package/core/skills/rename-workspaces.md +43 -0
  58. package/core/skills/supersede.md +83 -0
  59. package/core/skills/sweep.md +40 -0
  60. package/core/templates/my-wiki-graph.js +431 -0
  61. package/core/templates/my-wiki-overview.js +174 -0
  62. package/core/templates/my-wiki.html +1017 -0
  63. package/core/templates/node-folder/README.md +11 -0
  64. package/core/templates/node-folder/resolver.md +9 -0
  65. package/core/templates/node-light.md +22 -0
  66. package/core/templates/node-product/README.md +8 -0
  67. package/core/templates/node-repo/README.md +14 -0
  68. package/core/templates/node-repo/context/README.md +22 -0
  69. package/core/templates/node-repo/context/competitors.md +8 -0
  70. package/core/templates/node-repo/context/glossary.md +8 -0
  71. package/core/templates/node-repo/context/open-questions.md +9 -0
  72. package/core/templates/node-repo/context/opportunities.md +9 -0
  73. package/core/templates/node-repo/context/problem.md +8 -0
  74. package/core/templates/node-repo/context/scope.md +8 -0
  75. package/core/templates/node-repo/context/users.md +8 -0
  76. package/core/templates/node-repo/context/vision.md +8 -0
  77. package/core/templates/node-repo/resolver.md +14 -0
  78. package/core/templates/operator.md +17 -0
  79. package/core/templates/person.md +10 -0
  80. package/core/templates/repo-scaffold/ARCHITECTURE.md +3 -0
  81. package/core/templates/repo-scaffold/CLAUDE.md +23 -0
  82. package/core/templates/repo-scaffold/decisions.md +3 -0
  83. package/core/templates/repo-scaffold/evals-run.sh +14 -0
  84. package/core/templates/repo-scaffold/gitignore +3 -0
  85. package/core/templates/repo-scaffold/settings.json +8 -0
  86. package/core/templates/root-resolver.md +5 -0
  87. package/core/templates/strings.md +1361 -0
  88. package/core/templates/tree.md +59 -0
  89. package/core/templates/voice.md +5 -0
  90. package/package.json +23 -0
@@ -0,0 +1,436 @@
1
+ #!/usr/bin/env bash
2
+ # A file declares what replaced it (spec 046, hardened by spec 047). Internal: not invocable — the
3
+ # invocable is `skills/supersede.md`.
4
+ #
5
+ # Usage: supersede.sh --brain DIR --file PATH --by PATH
6
+ # supersede.sh --brain DIR --check
7
+ #
8
+ # Git does not answer "is this document still current?": the agent reads the working tree, never the
9
+ # history. A research file from March and one from July that contradicts it are two equally valid
10
+ # files unless the older one says so itself. The mark is one frontmatter key on the file that was
11
+ # replaced:
12
+ #
13
+ # superseded_by: <path relative to the brain>
14
+ #
15
+ # WHY THE MARK POINTS FORWARD, FROM THE OLD FILE. The reader at risk is whoever opened the old one;
16
+ # a `supersedes:` key on the new file never reaches them. The reverse direction stays answerable
17
+ # with one `grep -rl 'superseded_by: <path>'`, and two keys naming each other desync the first time
18
+ # a file is renamed. The rule itself is stated once, in `core/CLAUDE.md`.
19
+ #
20
+ # WHY THE STARTUP SCAN IS NOT TOUCHED. `session-start.sh` reads the frontmatter of heads and never
21
+ # opens a content file — decision 1 of spec 045 (Gate 1, 2026-08-29), because the scan has to
22
+ # keep fitting on screen. The documents this mark exists for —`research/*.md`, the body of an
23
+ # initiative— are all reached by `content:` lines, so a startup line would count the heads and miss
24
+ # them: half a check presented as a check. Everything else is audited on demand, with `--check`,
25
+ # over both classes of tree line.
26
+ #
27
+ # WHY A FILE WITH NO FRONTMATTER GETS ONE. `os_fm_set` refuses whatever `fm_read` could not read,
28
+ # and `fm_read` answers `sin frontmatter` both for a file that has none and for one that does not
29
+ # exist. The files that most need this mark are precisely the headerless ones (measured on a real
30
+ # brain: 99 content files, 18 with frontmatter), so the two cases are separated here by an
31
+ # `[ -f ]` and only the first one gets a block created above its untouched body. A frontmatter that
32
+ # exists and cannot be read —unclosed, over the bound— is refused: writing over it is corrupting the
33
+ # file in silence.
34
+ #
35
+ # WHAT SPEC 047 ADDED, AND WHY (each of these was reproduced on a test brain before being fixed):
36
+ #
37
+ # - **No write reports success without having happened.** Every `mv`, every redirection and every
38
+ # `os_fm_set` is checked. Before, a file in a directory with no write permission printed that it
39
+ # had been marked and exited 0 with the file untouched.
40
+ # - **What only writes documents of the brain.** `--file` has to end in `.md` and be reached by a
41
+ # line of `tree.md`. Before, `[ -f ]` was the whole check, and pointing the command at a git
42
+ # config file prepended a frontmatter block to it and broke the repository.
43
+ # - **The spelling of a path is not a way around the checks.** `./d/a.md`, `d//a.md` and `d/a.md`
44
+ # are the same path here, and the value written is always the normalised one — otherwise a file
45
+ # declares that it replaced itself and the audit says the brain is clean.
46
+ # - **Nothing follows a symbolic link.** A link is refused instead of being replaced by a regular
47
+ # file, and a path whose directories resolve outside the brain is refused too.
48
+ # - **A chain longer than the bound is undecided, never clean.** It is refused when writing and
49
+ # reported when auditing.
50
+ # - **`--check` fails loudly over a brain it could not read**, and reports a mark that hides inside
51
+ # a frontmatter it could not parse instead of walking past it.
52
+ # - **Closing a head is never silent, and never automatic beyond `active`.** See below.
53
+ #
54
+ # WHY ONLY AN `active` HEAD WITH NOTHING DEPENDING ON IT IS CLOSED. The active lists of the scan are
55
+ # driven by `status:` and `os_cerrado` is their single reader, so a head that reads `active` while
56
+ # carrying the mark gives the operator two answers about the same file — that one is closed here,
57
+ # and the output says so. The other three states are not that case:
58
+ # · `ongoing` is permanent work. Replacing a document does not end standing work.
59
+ # · `blocked` carries what it is waiting for. Closing it drops that without anyone reading it.
60
+ # · a head another head declares in `depends_on` reads as satisfied once it is `closed`
61
+ # (`sweep.sh`, `deps_abiertas`), so closing it silently unblocks work that nobody unblocked.
62
+ # In those three the mark is written, the head is left as it was, the output names the reason, and
63
+ # the exit code is 3 so that a script never reads the pending decision as a plain success.
64
+ #
65
+ # Exit: 0 wrote, or the file already said the same thing · 1 invalid arguments, a refusal, or
66
+ # `--check` with findings · 2 a named file does not exist · 3 the mark was written and the head
67
+ # needs a decision from the operator.
68
+ #
69
+ # No `set -e`: a refusal is reported, never a stack trace.
70
+
71
+ set -u
72
+
73
+ here=$(cd "$(dirname "$0")" && pwd)
74
+ . "$here/common.sh"
75
+
76
+ nl=$(printf '\nx')
77
+ nl="${nl%x}"
78
+
79
+ # How many links of a `superseded_by:` chain are followed before the answer is called undecided. A
80
+ # chain is legitimate —a document replaced twice— and a cycle is always an error; the walk already
81
+ # terminates on its own because it remembers where it has been, so this bound only guards against a
82
+ # pathological brain. Reaching it is reported, never taken for "no cycle here": a ring longer than
83
+ # the bound used to be written whole and audited clean.
84
+ CAP_CHAIN=100
85
+
86
+ brain=""
87
+ file=""
88
+ by=""
89
+ check=0
90
+ # A flag that takes a value checks that the value is there before consuming it: with the flag as the
91
+ # last argument, `shift 2` fails without consuming anything and the loop repeats forever.
92
+ while [ $# -gt 0 ]; do
93
+ case "$1" in
94
+ --brain) [ $# -ge 2 ] || os_die "--brain needs a value"; brain="$2"; shift 2 ;;
95
+ --file) [ $# -ge 2 ] || os_die "--file needs a value"; file="$2"; shift 2 ;;
96
+ --by) [ $# -ge 2 ] || os_die "--by needs a value"; by="$2"; shift 2 ;;
97
+ --check) check=1; shift ;;
98
+ *) os_die "unknown argument: $1" ;;
99
+ esac
100
+ done
101
+
102
+ [ -n "$brain" ] || os_die "missing --brain"
103
+ [ -d "$brain" ] || os_die "no such brain: $brain"
104
+ brain=$(cd "$brain" && pwd)
105
+
106
+ if [ "$check" = "1" ]; then
107
+ { [ -z "$file" ] && [ -z "$by" ]; } || os_die "--check is exclusive with --file and --by"
108
+ else
109
+ [ -n "$file" ] || os_die "missing --file or --check"
110
+ [ -n "$by" ] || os_die "missing --by"
111
+ fi
112
+
113
+ lang=$(os_language "$brain")
114
+ os_lang_load "$lang"
115
+
116
+ # ---------------------------------------------------------------- the paths are the brain's
117
+ # Every path of the system is relative to the brain (`core/CLAUDE.md`), and that is what makes the
118
+ # mark portable and the audit possible with a plain `[ -f ]`. An absolute path works on one machine;
119
+ # a `..` walks out of the brain into a tree nothing here governs. Both are refused before anything
120
+ # is read.
121
+ # The three of them live in `common.sh` since spec 049, where the second command that validates a
122
+ # path written by hand into a frontmatter reads them too. A local copy would be the desync this repo
123
+ # already forbids; the wrappers keep this file's calls reading the same as before.
124
+ rel_ok() { os_rel_ok "$1"; }
125
+ rel_norm() { os_rel_norm "$1"; }
126
+ inside_brain() { os_inside_brain "$brain" "$1"; }
127
+
128
+ # fm_of RELPATH -> reads the frontmatter of a brain-relative path into the fm_* variables.
129
+ fm_of() {
130
+ fm_read "$brain/$1"
131
+ }
132
+
133
+ # tiene_marca_textual RELPATH -> 0 if the head of the file names the key even though `fm_read` could
134
+ # not parse it. A mark inside a frontmatter that never closes is invisible to every reader of the
135
+ # system, which is exactly why the audit has to name it instead of walking past it.
136
+ tiene_marca_textual() {
137
+ head -n "$CAP_FRONTMATTER" "$brain/$1" 2>/dev/null | grep -q '^[[:space:]]*superseded_by:'
138
+ }
139
+
140
+ # chain_reaches START TARGET -> 0 reaches it · 1 does not · 2 undecided, the bound was exhausted.
141
+ # It stops at the first file that does not exist —a broken chain is a finding of `--check`, not a
142
+ # reason to loop— and it remembers where it has been, so a ring is recognised whatever its length.
143
+ chain_reaches() {
144
+ local cur="$1" target="$2" n=0 seen="$nl"
145
+ while [ "$n" -lt "$CAP_CHAIN" ]; do
146
+ [ "$cur" = "$target" ] && return 0
147
+ case "$seen" in *"$nl$cur$nl"*) return 1 ;; esac
148
+ seen="$seen$cur$nl"
149
+ [ -f "$brain/$cur" ] || return 1
150
+ fm_of "$cur"
151
+ [ -n "$fm_superseded_by" ] || return 1
152
+ cur=$(rel_norm "$fm_superseded_by")
153
+ rel_ok "$cur" || return 1
154
+ n=$(( n + 1 ))
155
+ done
156
+ return 2
157
+ }
158
+
159
+ # ---------------------------------------------------------------- the two classes of tree line
160
+ # Read once, from the one reader of the tree format (`common.sh`). A path that a `glob:` reaches is
161
+ # a head and its `status` governs the active lists; a path that only a `content:` reaches is content
162
+ # and has no status to govern. A brain whose `tree.md` cannot be read is not a clean brain: both
163
+ # readers exit 1 there, and swallowing that turned `--check` into a command that reports "all good"
164
+ # about a brain it never read.
165
+ # The `archive:` class (spec 048) joins `contents` here and nowhere else in this script: an
166
+ # archived document is still a document of the brain, so a mark written on it — or a mark on
167
+ # another file that names it — has to be auditable. What archiving changes is what a read loads,
168
+ # never what an audit sees.
169
+ heads=$(os_tree_files "$brain"); rc_heads=$?
170
+ contents=$(os_tree_content_files "$brain"); rc_contents=$?
171
+ archived=$(os_tree_archive_files "$brain"); rc_archived=$?
172
+ if [ "$rc_archived" = "0" ] && [ -n "$archived" ]; then
173
+ if [ -n "$contents" ]; then contents="$contents$nl$archived"; else contents="$archived"; fi
174
+ fi
175
+ if [ "$rc_heads" != "0" ] || [ "$rc_contents" != "0" ]; then
176
+ printf "$S_SUPERSEDE_NO_TREE\n" "$brain" >&2
177
+ exit 1
178
+ fi
179
+
180
+ es_cabeza() {
181
+ case "$nl$heads$nl" in *"$nl$1$nl"*) return 0 ;; esac
182
+ return 1
183
+ }
184
+
185
+ alcanzado() {
186
+ case "$nl$heads$nl" in *"$nl$1$nl"*) return 0 ;; esac
187
+ case "$nl$contents$nl" in *"$nl$1$nl"*) return 0 ;; esac
188
+ return 1
189
+ }
190
+
191
+ # ================================================================ --check
192
+ if [ "$check" = "1" ]; then
193
+ hallazgos=""
194
+ n=0
195
+ todos="$heads$nl$contents"
196
+ vistos="$nl"
197
+ while IFS= read -r f || [ -n "$f" ]; do
198
+ [ -n "$f" ] || continue
199
+ case "$vistos" in *"$nl$f$nl"*) continue ;; esac
200
+ vistos="$vistos$f$nl"
201
+ fm_of "$f"
202
+ if [ -n "$fm_roto" ]; then
203
+ case "$fm_roto" in
204
+ 'sin frontmatter') continue ;;
205
+ esac
206
+ if tiene_marca_textual "$f"; then
207
+ hallazgos="$hallazgos$f: $(printf "$S_SUPERSEDE_UNREADABLE_MARK" "$fm_roto")$nl"
208
+ n=$(( n + 1 ))
209
+ fi
210
+ continue
211
+ fi
212
+ [ -n "$fm_superseded_by" ] || continue
213
+ # Copied out before anything else reads frontmatter: `chain_reaches` walks the chain with the
214
+ # same `fm_read`, and it would otherwise leave this file's `status` overwritten by the last
215
+ # link's.
216
+ target=$(rel_norm "$fm_superseded_by")
217
+ estado="$fm_status"
218
+ if [ "$target" = "$f" ]; then
219
+ hallazgos="$hallazgos$f: $S_SUPERSEDE_SELF$nl"
220
+ n=$(( n + 1 ))
221
+ continue
222
+ fi
223
+ if ! rel_ok "$target" || ! inside_brain "$target" || [ ! -f "$brain/$target" ]; then
224
+ hallazgos="$hallazgos$f: $(printf "$S_SUPERSEDE_DANGLING" "$target")$nl"
225
+ n=$(( n + 1 ))
226
+ continue
227
+ fi
228
+ chain_reaches "$target" "$f"; rc_chain=$?
229
+ if [ "$rc_chain" = "0" ]; then
230
+ hallazgos="$hallazgos$f: $(printf "$S_SUPERSEDE_CYCLE" "$target")$nl"
231
+ n=$(( n + 1 ))
232
+ continue
233
+ fi
234
+ if [ "$rc_chain" = "2" ]; then
235
+ hallazgos="$hallazgos$f: $(printf "$S_SUPERSEDE_CHAIN_UNDECIDED" "$target" "$CAP_CHAIN")$nl"
236
+ n=$(( n + 1 ))
237
+ continue
238
+ fi
239
+ # An open head is reported when its status is `active`: that is the one state this command
240
+ # closes on its own, so finding it means the file was edited by hand afterwards. `ongoing`,
241
+ # `blocked` and a head something depends on are left open on purpose (see the header), and an
242
+ # audit that reported them would never go green on a brain that uses them.
243
+ if es_cabeza "$f" && [ "$estado" = "active" ]; then
244
+ hallazgos="$hallazgos$f: $(printf "$S_SUPERSEDE_OPEN_HEAD" "$estado")$nl"
245
+ n=$(( n + 1 ))
246
+ fi
247
+ done <<TODOS
248
+ $todos
249
+ TODOS
250
+ if [ "$n" = "0" ]; then
251
+ printf '%s\n' "$S_SUPERSEDE_CHECK_CLEAN"
252
+ exit 0
253
+ fi
254
+ printf '%s' "$hallazgos"
255
+ exit 1
256
+ fi
257
+
258
+ # ================================================================ writing the mark
259
+ file=$(rel_norm "$file")
260
+ by=$(rel_norm "$by")
261
+
262
+ rel_ok "$file" || os_die "--file is not a path relative to the brain: $file"
263
+ rel_ok "$by" || os_die "--by is not a path relative to the brain: $by"
264
+
265
+ # A document of the brain, and nothing else. `[ -f ]` alone accepts `.git/config` and the block this
266
+ # script writes over it is not a frontmatter there: it is a broken repository with no way back.
267
+ for p in "$file" "$by"; do
268
+ case "$p" in
269
+ *.md) ;;
270
+ *) printf "$S_SUPERSEDE_NOT_MD\n" "$p" >&2; exit 1 ;;
271
+ esac
272
+ inside_brain "$p" || { printf "$S_SUPERSEDE_OUT_OF_BRAIN\n" "$p" >&2; exit 1; }
273
+ done
274
+
275
+ if [ -L "$brain/$file" ] || [ -L "$brain/$by" ]; then
276
+ [ -L "$brain/$file" ] && p="$file" || p="$by"
277
+ printf "$S_SUPERSEDE_IS_LINK\n" "$p" >&2
278
+ exit 1
279
+ fi
280
+
281
+ if [ ! -f "$brain/$file" ]; then
282
+ printf "$S_SUPERSEDE_NO_FILE\n" "$file" >&2
283
+ exit 2
284
+ fi
285
+ if [ ! -f "$brain/$by" ]; then
286
+ printf "$S_SUPERSEDE_NO_FILE\n" "$by" >&2
287
+ exit 2
288
+ fi
289
+
290
+ # The mark goes on a file the tree governs. A file no line of `tree.md` reaches is invisible to
291
+ # `--check`, so the mark on it is a promise nothing keeps — and it is how a file that is not a
292
+ # document of the brain gets here in the first place.
293
+ alcanzado "$file" || { printf "$S_SUPERSEDE_NOT_IN_TREE\n" "$file" >&2; exit 1; }
294
+
295
+ if [ "$file" = "$by" ]; then
296
+ printf '%s\n' "$S_SUPERSEDE_SELF" >&2
297
+ exit 1
298
+ fi
299
+
300
+ # A chain is written without complaint —a document replaced twice reaches the last one by following
301
+ # it— and a cycle is refused: it is always an error, and it hangs whoever follows it. A chain the
302
+ # walk could not finish is refused too: undecided is not the same as clean.
303
+ chain_reaches "$by" "$file"; rc_chain=$?
304
+ if [ "$rc_chain" = "0" ]; then
305
+ printf "$S_SUPERSEDE_WOULD_CYCLE\n" "$by" "$file" >&2
306
+ exit 1
307
+ fi
308
+ if [ "$rc_chain" = "2" ]; then
309
+ printf "$S_SUPERSEDE_CHAIN_TOO_LONG\n" "$by" "$CAP_CHAIN" >&2
310
+ exit 1
311
+ fi
312
+
313
+ fm_of "$file"
314
+ case "$fm_roto" in
315
+ "")
316
+ if [ "$fm_superseded_by" = "$by" ]; then
317
+ printf "$S_SUPERSEDE_ALREADY\n" "$file" "$by"
318
+ exit 0
319
+ fi
320
+ # A body that opens with a `---` rule looks like a frontmatter to a reader that only sees the
321
+ # delimiters, and the key would land in the middle of the prose.
322
+ os_fm_shape_ok "$brain/$file" || {
323
+ printf "$S_SUPERSEDE_NOT_FRONTMATTER\n" "$file" >&2
324
+ exit 1
325
+ }
326
+ os_fm_set "$brain/$file" superseded_by "$by" || {
327
+ printf "$S_SUPERSEDE_WRITE_FAILED\n" "$file" >&2
328
+ exit 1
329
+ }
330
+ ;;
331
+ 'sin frontmatter')
332
+ # The file exists (checked above) and has no frontmatter at all: the block is created above the
333
+ # body, which is copied byte for byte. This is the only case where this script writes a
334
+ # frontmatter instead of asking `os_fm_set` for it.
335
+ tmp="$brain/$file.os-tmp"
336
+ rm -f "$tmp"
337
+ if ! {
338
+ printf -- '---\n'
339
+ printf 'superseded_by: %s\n' "$by"
340
+ printf -- '---\n'
341
+ printf '\n'
342
+ cat "$brain/$file"
343
+ } 2>/dev/null > "$tmp"; then
344
+ rm -f "$tmp"
345
+ printf "$S_SUPERSEDE_WRITE_FAILED\n" "$file" >&2
346
+ exit 1
347
+ fi
348
+ os_replace_file "$tmp" "$brain/$file" || {
349
+ printf "$S_SUPERSEDE_WRITE_FAILED\n" "$file" >&2
350
+ exit 1
351
+ }
352
+ ;;
353
+ *)
354
+ # Unclosed, or over `CAP_FRONTMATTER`. Writing over a frontmatter that cannot be read is
355
+ # corrupting the file in silence.
356
+ printf "$S_SUPERSEDE_UNREADABLE\n" "$file" "$fm_roto" >&2
357
+ exit 1
358
+ ;;
359
+ esac
360
+
361
+ # ---------------------------------------------------------------- the head, out loud
362
+ # ref_of RELPATH -> the `<scope>#<name>` reference by which another head names this one in its
363
+ # `depends_on:`. Same convention `sweep.sh` resolves, read from the same two helpers.
364
+ ref_of() {
365
+ local f="$1" wsdir resto org
366
+ wsdir=$(os_ws_dir "$brain")
367
+ case "$f" in
368
+ "$wsdir"/*)
369
+ resto="${f#$wsdir/}"
370
+ org="${resto%%/*}"
371
+ ;;
372
+ *) org="$OS_ROOT_LABEL" ;;
373
+ esac
374
+ printf '%s#%s' "$org" "$(os_node_name "$f")"
375
+ }
376
+
377
+ # depende_de_mi REF -> the heads that declare REF in `depends_on:`, one per line.
378
+ depende_de_mi() {
379
+ local ref="$1" h
380
+ while IFS= read -r h || [ -n "$h" ]; do
381
+ [ -n "$h" ] || continue
382
+ [ "$h" = "$file" ] && continue
383
+ fm_of "$h"
384
+ [ -n "$fm_depends_on" ] || continue
385
+ case ",${fm_depends_on//[[:space:]\[\]]/}," in
386
+ *",$ref,"*) printf '%s\n' "$h" ;;
387
+ esac
388
+ done <<HEADS
389
+ $heads
390
+ HEADS
391
+ }
392
+
393
+ pendiente=0
394
+ motivo=""
395
+ if es_cabeza "$file"; then
396
+ fm_of "$file"
397
+ estado="$fm_status"
398
+ esperando="$fm_waiting_on"
399
+ [ -n "$esperando" ] || esperando="$fm_blocked"
400
+ [ -n "$esperando" ] || esperando="$S_SUPERSEDE_NO_REASON"
401
+ dependientes=$(depende_de_mi "$(ref_of "$file")" | tr '\n' ' ')
402
+ dependientes="${dependientes% }"
403
+ if os_cerrado "$estado"; then
404
+ :
405
+ elif [ -z "$estado" ]; then
406
+ pendiente=1
407
+ motivo=$(printf "$S_SUPERSEDE_HEAD_NO_STATUS" "$file")
408
+ elif [ "$estado" = "ongoing" ]; then
409
+ pendiente=1
410
+ motivo=$(printf "$S_SUPERSEDE_HEAD_ONGOING" "$file")
411
+ elif [ "$estado" = "blocked" ]; then
412
+ pendiente=1
413
+ motivo=$(printf "$S_SUPERSEDE_HEAD_BLOCKED" "$file" "$esperando")
414
+ elif [ -n "$dependientes" ]; then
415
+ pendiente=1
416
+ motivo=$(printf "$S_SUPERSEDE_HEAD_DEPENDED_ON" "$file" "$dependientes" "$by")
417
+ elif [ "$estado" = "active" ]; then
418
+ os_fm_set "$brain/$file" status closed || {
419
+ printf "$S_SUPERSEDE_WRITE_FAILED\n" "$file" >&2
420
+ exit 1
421
+ }
422
+ motivo=$(printf "$S_SUPERSEDE_HEAD_CLOSED" "$file")
423
+ else
424
+ # A status outside the vocabulary is declared, never interpreted (`os_estado_valido`).
425
+ pendiente=1
426
+ motivo=$(printf "$S_SUPERSEDE_HEAD_UNKNOWN_STATUS" "$file" "$estado")
427
+ fi
428
+ fi
429
+
430
+ printf "$S_SUPERSEDE_WROTE\n" "$file" "$by"
431
+ [ -n "$motivo" ] && printf '%s\n' "$motivo"
432
+ if [ "$pendiente" = "1" ]; then
433
+ printf '%s\n' "$S_SUPERSEDE_HEAD_PENDING"
434
+ exit 3
435
+ fi
436
+ exit 0