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.
- package/LICENSE +21 -0
- package/README.md +226 -0
- package/bin/ai-first-os.js +72 -0
- package/core/CLAUDE.md +148 -0
- package/core/VERSION +1 -0
- package/core/agents/ambiguous-spec.md +39 -0
- package/core/agents/complete-spec.md +36 -0
- package/core/agents/scout.md +25 -0
- package/core/install.sh +243 -0
- package/core/lib/archive.sh +347 -0
- package/core/lib/bootstrap.sh +276 -0
- package/core/lib/capture.sh +188 -0
- package/core/lib/check-resolvable.sh +172 -0
- package/core/lib/close-session.sh +741 -0
- package/core/lib/common.sh +1773 -0
- package/core/lib/focus-read.sh +282 -0
- package/core/lib/manual-link.sh +111 -0
- package/core/lib/markdown.py +304 -0
- package/core/lib/migrate-backlogs.sh +207 -0
- package/core/lib/migrate-canonicals.sh +523 -0
- package/core/lib/mount-repo.sh +543 -0
- package/core/lib/my-wiki-gates.sh +137 -0
- package/core/lib/my-wiki.sh +202 -0
- package/core/lib/my_wiki_build.py +548 -0
- package/core/lib/my_wiki_graph.py +137 -0
- package/core/lib/my_wiki_overview.py +392 -0
- package/core/lib/new-memory.sh +160 -0
- package/core/lib/new-product.sh +122 -0
- package/core/lib/new-workspace.sh +105 -0
- package/core/lib/pack-install.sh +114 -0
- package/core/lib/recall.sh +235 -0
- package/core/lib/recall_index.py +295 -0
- package/core/lib/remote-backup.sh +84 -0
- package/core/lib/rename-heads.sh +354 -0
- package/core/lib/rename-workspaces.sh +271 -0
- package/core/lib/session-read.sh +203 -0
- package/core/lib/session-start.sh +854 -0
- package/core/lib/supersede.sh +436 -0
- package/core/lib/sweep.sh +473 -0
- package/core/manual/COMO-FUNCIONA.md +251 -0
- package/core/manual/HOW-IT-WORKS.md +250 -0
- package/core/resolver.md +30 -0
- package/core/skills/archive.md +95 -0
- package/core/skills/bootstrap.md +226 -0
- package/core/skills/capture.md +65 -0
- package/core/skills/check-resolvable.md +38 -0
- package/core/skills/close-session.md +108 -0
- package/core/skills/grill.md +74 -0
- package/core/skills/mount-repo.md +97 -0
- package/core/skills/my-wiki.md +103 -0
- package/core/skills/new-memory.md +76 -0
- package/core/skills/new-product.md +55 -0
- package/core/skills/new-spec.md +114 -0
- package/core/skills/new-workspace.md +53 -0
- package/core/skills/recall.md +64 -0
- package/core/skills/rename-heads.md +72 -0
- package/core/skills/rename-workspaces.md +43 -0
- package/core/skills/supersede.md +83 -0
- package/core/skills/sweep.md +40 -0
- package/core/templates/my-wiki-graph.js +431 -0
- package/core/templates/my-wiki-overview.js +174 -0
- package/core/templates/my-wiki.html +1017 -0
- package/core/templates/node-folder/README.md +11 -0
- package/core/templates/node-folder/resolver.md +9 -0
- package/core/templates/node-light.md +22 -0
- package/core/templates/node-product/README.md +8 -0
- package/core/templates/node-repo/README.md +14 -0
- package/core/templates/node-repo/context/README.md +22 -0
- package/core/templates/node-repo/context/competitors.md +8 -0
- package/core/templates/node-repo/context/glossary.md +8 -0
- package/core/templates/node-repo/context/open-questions.md +9 -0
- package/core/templates/node-repo/context/opportunities.md +9 -0
- package/core/templates/node-repo/context/problem.md +8 -0
- package/core/templates/node-repo/context/scope.md +8 -0
- package/core/templates/node-repo/context/users.md +8 -0
- package/core/templates/node-repo/context/vision.md +8 -0
- package/core/templates/node-repo/resolver.md +14 -0
- package/core/templates/operator.md +17 -0
- package/core/templates/person.md +10 -0
- package/core/templates/repo-scaffold/ARCHITECTURE.md +3 -0
- package/core/templates/repo-scaffold/CLAUDE.md +23 -0
- package/core/templates/repo-scaffold/decisions.md +3 -0
- package/core/templates/repo-scaffold/evals-run.sh +14 -0
- package/core/templates/repo-scaffold/gitignore +3 -0
- package/core/templates/repo-scaffold/settings.json +8 -0
- package/core/templates/root-resolver.md +5 -0
- package/core/templates/strings.md +1361 -0
- package/core/templates/tree.md +59 -0
- package/core/templates/voice.md +5 -0
- package/package.json +23 -0
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# One-time split of what `decisions.md`/`learnings.md` already hold into `decisions/<date>-<slug>.md`
|
|
3
|
+
# and `learnings/<slug>.md`, leaving the canonical file as the index (spec 043; spec 053 closed its
|
|
4
|
+
# residuals — see below). Internal: not invocable — run on demand, over any brain; no skill wraps it
|
|
5
|
+
# yet (out of scope of the spec that built it).
|
|
6
|
+
#
|
|
7
|
+
# Usage: migrate-canonicals.sh --brain DIR
|
|
8
|
+
#
|
|
9
|
+
# Idempotent: a canonical file that no longer has any `## ` entry block (already an index, from a
|
|
10
|
+
# previous run of this same script or from `close-session.sh` writing since) is left untouched, and
|
|
11
|
+
# a second run over an already-migrated brain leaves `git status --porcelain` empty.
|
|
12
|
+
#
|
|
13
|
+
# Two entry shapes are recognized inside a `---`-separated block, and a block with no `---` at all
|
|
14
|
+
# (spec 053 C2 — everything from the file's first `## ` line onward is treated as one block):
|
|
15
|
+
# `## <date> · <title>` a dated entry, same shape `close-session.sh` already writes — the date
|
|
16
|
+
# and title come straight from the heading. A block can hold more than one
|
|
17
|
+
# of these back to back with no `---` between them (spec 053 C1): each
|
|
18
|
+
# heading opens a new entry, its body running to the next one.
|
|
19
|
+
# `## <heading>` a themed heading with `- <text>` bullets under it (the shape this
|
|
20
|
+
# repo's own pre-043 `decisions.md`/`learnings.md` mixed in) — each bullet
|
|
21
|
+
# becomes its own undated learning. Its date is the one declared in the
|
|
22
|
+
# file's own header (a `Desde: YYYY-MM-DD` line before the first `---` or
|
|
23
|
+
# `## `), or — with no such line — the date of the file's first commit
|
|
24
|
+
# (`git log --diff-filter=A`). Nothing here ever stamps today's date on a
|
|
25
|
+
# fact the source did not date, which is exactly what would happen if the
|
|
26
|
+
# write date were used instead.
|
|
27
|
+
# Prose before a block's first `## ` (spec 053 C3) is not lost: it is appended to the index's own
|
|
28
|
+
# header, once, right before the block's entries are written.
|
|
29
|
+
#
|
|
30
|
+
# Every entry body is staged in a scratch directory first and the whole canonical is moved into
|
|
31
|
+
# place with one `mkdir -p` + `mv` only once it split clean end to end (spec 053 C5) — a stop midway
|
|
32
|
+
# leaves the brain's `decisions/`/`learnings/` folder and the canonical file exactly as they were.
|
|
33
|
+
#
|
|
34
|
+
# Stops without changing anything, and reports why (the spec's own stopping conditions):
|
|
35
|
+
# - a block with no `## ` heading in it — an entry the format cannot name
|
|
36
|
+
# - two entries with the same title and the same date — the migration does not invent a `-2`
|
|
37
|
+
# the way the live writer does for a same-day collision; a genuine duplicate is a data problem,
|
|
38
|
+
# not a naming one
|
|
39
|
+
# - two different entries whose name collides after the slug is cut to its cap, or an entry whose
|
|
40
|
+
# target file already exists on disk with different content (spec 053 C4) — same principle: a
|
|
41
|
+
# collision is a data problem the migration reports, never one it resolves by guessing
|
|
42
|
+
# - the entry cannot be written (spec 053 C4) — permissions, a read-only target, disk full
|
|
43
|
+
# - the target canonical file has uncommitted changes — another session's work in progress, left
|
|
44
|
+
# alone
|
|
45
|
+
|
|
46
|
+
set -u
|
|
47
|
+
|
|
48
|
+
here=$(cd "$(dirname "$0")" && pwd)
|
|
49
|
+
. "$here/common.sh"
|
|
50
|
+
|
|
51
|
+
brain=""
|
|
52
|
+
while [ $# -gt 0 ]; do
|
|
53
|
+
case "$1" in
|
|
54
|
+
--brain) brain="${2:-}"; shift 2 ;;
|
|
55
|
+
*) os_die "argumento desconocido: $1" ;;
|
|
56
|
+
esac
|
|
57
|
+
done
|
|
58
|
+
[ -n "$brain" ] || os_die "falta --brain"
|
|
59
|
+
[ -d "$brain" ] || os_die "no existe el brain: $brain"
|
|
60
|
+
brain=$(cd "$brain" && pwd)
|
|
61
|
+
|
|
62
|
+
nl=$(printf '\nx'); nl="${nl%x}"
|
|
63
|
+
|
|
64
|
+
wsdir=$(os_ws_dir "$brain")
|
|
65
|
+
memory_types_org=$(os_memory_types "$brain" org) || memory_types_org=""
|
|
66
|
+
memory_types_root=$(os_memory_types "$brain" root) || memory_types_root=""
|
|
67
|
+
|
|
68
|
+
reporte=""
|
|
69
|
+
push() { reporte="$reporte$1$nl"; }
|
|
70
|
+
|
|
71
|
+
es_git=0
|
|
72
|
+
[ -d "$brain/.git" ] && es_git=1
|
|
73
|
+
|
|
74
|
+
sucio() {
|
|
75
|
+
# sucio RUTA_RELATIVA -> 0 si tiene cambios sin commitear, o no está trackeada.
|
|
76
|
+
[ "$es_git" = "1" ] || return 1
|
|
77
|
+
local out
|
|
78
|
+
out=$(git -C "$brain" status --porcelain -- "$1" 2>/dev/null)
|
|
79
|
+
[ -n "$out" ]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
fecha_primer_commit() {
|
|
83
|
+
# fecha_primer_commit RUTA_RELATIVA -> YYYY-MM-DD de cuándo se agregó el archivo, o vacío.
|
|
84
|
+
[ "$es_git" = "1" ] || { printf ''; return 0; }
|
|
85
|
+
git -C "$brain" log --diff-filter=A --format=%ad --date=format:%Y-%m-%d -- "$1" 2>/dev/null | tail -1
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
primera_linea_no_vacia() {
|
|
89
|
+
# primera_linea_no_vacia CHUNK -> "NUM:CONTENIDO" de la primera línea con algo más que espacios,
|
|
90
|
+
# o vacío si el trozo entero está en blanco. Un trozo trae una línea en blanco justo después del
|
|
91
|
+
# `---` que lo abre; sin saltarla, "la primera línea" sería esa, no el encabezado.
|
|
92
|
+
grep -n -m1 '[^[:space:]]' "$1" 2>/dev/null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
os_migrate_header_end_linea() {
|
|
96
|
+
# os_migrate_header_end_linea ARCHIVO -> número de línea (1-based) del primer "## " del archivo,
|
|
97
|
+
# o vacío si no tiene ninguno. El único corte que dice dónde termina la cabecera de un canónico
|
|
98
|
+
# (spec 055 C1): nunca el primer "---", esté donde esté. Lo usan `fecha_declarada_en_header` y
|
|
99
|
+
# `migrar_archivo` — el mismo corte, escrito una sola vez.
|
|
100
|
+
local l
|
|
101
|
+
l=$(grep -n '^## ' "$1" | head -1)
|
|
102
|
+
printf '%s' "${l%%:*}"
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
fecha_declarada_en_header() {
|
|
106
|
+
# fecha_declarada_en_header ARCHIVO -> la fecha de una línea "Desde: YYYY-MM-DD" en la cabecera
|
|
107
|
+
# real del archivo —todo antes de su primer "## " (spec 055 C1), sin importar si hay un "---"
|
|
108
|
+
# antes—, o vacío. Convención de este migrador — el escritor en vivo no la usa, así que un
|
|
109
|
+
# archivo que nunca la tuvo cae al commit.
|
|
110
|
+
local archivo="$1" cut line n=0
|
|
111
|
+
cut=$(os_migrate_header_end_linea "$archivo")
|
|
112
|
+
while IFS= read -r line || [ -n "$line" ]; do
|
|
113
|
+
n=$((n + 1))
|
|
114
|
+
if [ -n "$cut" ] && [ "$n" -ge "$cut" ]; then break; fi
|
|
115
|
+
case "$line" in
|
|
116
|
+
'Desde: '*) printf '%s' "${line#Desde: }"; return 0 ;;
|
|
117
|
+
esac
|
|
118
|
+
done < "$archivo"
|
|
119
|
+
return 1
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
# creados — los cuerpos que este archivo fue escribiendo mientras se procesaba (rutas de la carpeta
|
|
123
|
+
# de trabajo, spec 053 C5: nada de esto es todavía el brain real). Si el archivo entero se frena
|
|
124
|
+
# antes de terminar, `detener` borra la carpeta de trabajo entera; esta lista es una limpieza
|
|
125
|
+
# adicional, no la única.
|
|
126
|
+
creados=""
|
|
127
|
+
marcar_creado() { creados="$creados$1$nl"; }
|
|
128
|
+
|
|
129
|
+
# titulos_archivo — "rel → título" de cada cuerpo ya escrito en la carpeta de trabajo del canónico
|
|
130
|
+
# en curso, para poder nombrar qué se estaba escribiendo si el `mv` final (spec 053 C5) falla.
|
|
131
|
+
titulos_archivo=""
|
|
132
|
+
marcar_titulo() { titulos_archivo="$titulos_archivo$1 → $2$nl"; }
|
|
133
|
+
|
|
134
|
+
detener() {
|
|
135
|
+
local f
|
|
136
|
+
if [ -n "$creados" ]; then
|
|
137
|
+
while IFS= read -r f || [ -n "$f" ]; do
|
|
138
|
+
[ -n "$f" ] || continue
|
|
139
|
+
rm -f "$f"
|
|
140
|
+
done <<CREADOS
|
|
141
|
+
$creados
|
|
142
|
+
CREADOS
|
|
143
|
+
fi
|
|
144
|
+
# chunk_dir es local de migrar_archivo, pero sigue en el stack dinámico de bash mientras se llama
|
|
145
|
+
# a detener desde cualquier función que ese archivo esté procesando — spec 053 C5: nada de lo que
|
|
146
|
+
# se estaba armando en la carpeta de trabajo sobrevive a una parada.
|
|
147
|
+
[ -n "${chunk_dir:-}" ] && rm -rf "$chunk_dir" 2>/dev/null
|
|
148
|
+
printf 'migración detenida: %s\n' "$1" >&2
|
|
149
|
+
[ -n "$reporte" ] && printf '%s' "$reporte" >&2
|
|
150
|
+
exit 1
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
# visto — títulos+fechas ya emitidos en el archivo que se está migrando ahora mismo; se reinicia por
|
|
154
|
+
# archivo (migrar_archivo lo hace). Un duplicado exacto es la condición de parada de la spec.
|
|
155
|
+
visto=""
|
|
156
|
+
dup_visto() {
|
|
157
|
+
case "$nl$visto$nl" in
|
|
158
|
+
*"$nl$1$OS_SEP$2$nl"*) return 0 ;;
|
|
159
|
+
esac
|
|
160
|
+
return 1
|
|
161
|
+
}
|
|
162
|
+
marcar_visto() { visto="$visto$1$OS_SEP$2$nl"; }
|
|
163
|
+
|
|
164
|
+
# bloque_headings_mixtos CHUNK -> 0 si el bloque trae encabezados fechados y temáticos mezclados.
|
|
165
|
+
# La spec no dice cuál regla gana (spec 053, condición de parada): esto no elige, para la corrida.
|
|
166
|
+
# Solo bash + grep/head/tail — nada de sed/cut/awk (E7, PATH restringido): el número de línea de
|
|
167
|
+
# `grep -n` se separa con expansión de parámetros, no con `cut`.
|
|
168
|
+
bloque_headings_mixtos() {
|
|
169
|
+
local chunk="$1" linea texto tipo tipo_primero=""
|
|
170
|
+
while IFS= read -r linea; do
|
|
171
|
+
[ -n "$linea" ] || continue
|
|
172
|
+
texto="${linea#*:}"
|
|
173
|
+
case "$texto" in
|
|
174
|
+
'## '*' · '*) tipo=fechado ;;
|
|
175
|
+
*) tipo=tematico ;;
|
|
176
|
+
esac
|
|
177
|
+
if [ -z "$tipo_primero" ]; then
|
|
178
|
+
tipo_primero="$tipo"
|
|
179
|
+
elif [ "$tipo" != "$tipo_primero" ]; then
|
|
180
|
+
return 0
|
|
181
|
+
fi
|
|
182
|
+
done <<HEADS
|
|
183
|
+
$(grep -n '^## ' "$chunk")
|
|
184
|
+
HEADS
|
|
185
|
+
return 1
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
# escribir_cuerpo WD PREFIJO NOMBRE NUEVO_INDICE FECHA TITULO CUERPO
|
|
189
|
+
# El único lugar que escribe un cuerpo: a la carpeta de trabajo WD/out/<rel> (nunca al brain
|
|
190
|
+
# directamente — spec 053 C5), verificando la escritura (spec 053 C4) y deteniendo la corrida ante
|
|
191
|
+
# cualquiera de las dos colisiones que la spec nombra y no resuelve por sí sola: dos entradas de esta
|
|
192
|
+
# misma corrida que caen en el mismo nombre, o un archivo que ya existe en el brain con otro
|
|
193
|
+
# contenido. Un contenido idéntico en cualquiera de los dos casos no es colisión: se admite sin
|
|
194
|
+
# volver a escribir nada.
|
|
195
|
+
escribir_cuerpo() {
|
|
196
|
+
local wd="$1" prefijo="$2" nombre="$3" nuevo="$4" fecha="$5" titulo="$6" cuerpo="$7"
|
|
197
|
+
local rel destino real tmp_entry
|
|
198
|
+
if [ "$nombre" = "decisions" ]; then
|
|
199
|
+
rel=$(os_decision_path "$brain" "$prefijo" "$fecha" "$titulo")
|
|
200
|
+
else
|
|
201
|
+
rel=$(os_learning_path "$brain" "$prefijo" "$titulo") || true
|
|
202
|
+
fi
|
|
203
|
+
destino="$wd/out/$rel"
|
|
204
|
+
real="$brain/${prefijo}${rel}"
|
|
205
|
+
|
|
206
|
+
tmp_entry=$(mktemp "$wd/entry.XXXXXX")
|
|
207
|
+
if ! {
|
|
208
|
+
printf '# %s\n\n' "$titulo"
|
|
209
|
+
printf '> **Cuándo se lee**: %s\n\n' "$OS_CUANDO_FALTA"
|
|
210
|
+
printf '%s\n' "$cuerpo"
|
|
211
|
+
} > "$tmp_entry" 2>/dev/null
|
|
212
|
+
then
|
|
213
|
+
rm -f "$tmp_entry"
|
|
214
|
+
detener "$rel — no se pudo escribir el cuerpo de \"$titulo\""
|
|
215
|
+
fi
|
|
216
|
+
|
|
217
|
+
if [ -e "$destino" ]; then
|
|
218
|
+
if diff -q "$tmp_entry" "$destino" > /dev/null 2>&1; then
|
|
219
|
+
rm -f "$tmp_entry"
|
|
220
|
+
os_index_line "$fecha" "$titulo" "$rel" "$OS_CUANDO_FALTA" >> "$nuevo"
|
|
221
|
+
push "$nombre → ${prefijo}${rel}: $titulo"
|
|
222
|
+
return 0
|
|
223
|
+
fi
|
|
224
|
+
rm -f "$tmp_entry"
|
|
225
|
+
detener "$rel — dos entradas de esta migración producen el mismo nombre de archivo: \"$titulo\""
|
|
226
|
+
fi
|
|
227
|
+
|
|
228
|
+
if [ -e "$real" ]; then
|
|
229
|
+
if diff -q "$tmp_entry" "$real" > /dev/null 2>&1; then
|
|
230
|
+
rm -f "$tmp_entry"
|
|
231
|
+
os_index_line "$fecha" "$titulo" "$rel" "$OS_CUANDO_FALTA" >> "$nuevo"
|
|
232
|
+
push "$nombre → ${prefijo}${rel}: $titulo (ya existía, mismo contenido)"
|
|
233
|
+
return 0
|
|
234
|
+
fi
|
|
235
|
+
rm -f "$tmp_entry"
|
|
236
|
+
detener "$rel — ya existe en el brain con otro contenido: \"$titulo\""
|
|
237
|
+
fi
|
|
238
|
+
|
|
239
|
+
if ! mkdir -p "$(dirname "$destino")" 2>/dev/null; then
|
|
240
|
+
rm -f "$tmp_entry"
|
|
241
|
+
detener "$rel — no se pudo escribir el cuerpo de \"$titulo\""
|
|
242
|
+
fi
|
|
243
|
+
if ! mv "$tmp_entry" "$destino" 2>/dev/null; then
|
|
244
|
+
rm -f "$tmp_entry"
|
|
245
|
+
detener "$rel — no se pudo escribir el cuerpo de \"$titulo\""
|
|
246
|
+
fi
|
|
247
|
+
marcar_creado "$destino"
|
|
248
|
+
marcar_titulo "$rel" "$titulo"
|
|
249
|
+
os_index_line "$fecha" "$titulo" "$rel" "$OS_CUANDO_FALTA" >> "$nuevo"
|
|
250
|
+
push "$nombre → ${prefijo}${rel}: $titulo"
|
|
251
|
+
return 0
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
escribir_desde_bloque_fechado() {
|
|
255
|
+
# escribir_desde_bloque_fechado CHUNK PREFIJO NOMBRE NUEVO_INDICE WD
|
|
256
|
+
# CHUNK empieza siempre en su primer "## " (procesar_bloque ya lo recortó ahí). Spec 053 C1: puede
|
|
257
|
+
# traer más de un encabezado fechado seguido, sin "---" entre ellos — cada uno abre una entrada
|
|
258
|
+
# nueva, y su cuerpo corre hasta el próximo encabezado o el fin del bloque.
|
|
259
|
+
local chunk="$1" prefijo="$2" nombre="$3" nuevo="$4" wd="$5"
|
|
260
|
+
local -a heads=()
|
|
261
|
+
local -a textos=()
|
|
262
|
+
local linea total_lineas
|
|
263
|
+
# Solo bash + grep/head/tail — nada de sed/cut/awk (E7, PATH restringido): `grep -n` ya trae el
|
|
264
|
+
# número y el texto en la misma línea ("N:texto"), separados acá con expansión de parámetros.
|
|
265
|
+
while IFS= read -r linea; do
|
|
266
|
+
[ -n "$linea" ] || continue
|
|
267
|
+
heads+=("${linea%%:*}")
|
|
268
|
+
textos+=("${linea#*:}")
|
|
269
|
+
done <<HEADS
|
|
270
|
+
$(grep -n '^## ' "$chunk")
|
|
271
|
+
HEADS
|
|
272
|
+
total_lineas=$(wc -l < "$chunk")
|
|
273
|
+
local total=${#heads[@]} i
|
|
274
|
+
i=0
|
|
275
|
+
while [ "$i" -lt "$total" ]; do
|
|
276
|
+
local hl="${heads[$i]}" hl_fin texto resto fecha titulo cuerpo sig=$((i + 1))
|
|
277
|
+
texto="${textos[$i]}"
|
|
278
|
+
resto="${texto#\#\# }"
|
|
279
|
+
fecha="${resto%% · *}"
|
|
280
|
+
titulo="${resto#* · }"
|
|
281
|
+
if [ "$sig" -lt "$total" ]; then
|
|
282
|
+
hl_fin=$(( ${heads[$sig]} - 1 ))
|
|
283
|
+
else
|
|
284
|
+
hl_fin="$total_lineas"
|
|
285
|
+
fi
|
|
286
|
+
cuerpo=$(head -n "$hl_fin" "$chunk" | tail -n +"$((hl + 1))")
|
|
287
|
+
if dup_visto "$fecha" "$titulo"; then
|
|
288
|
+
detener "entrada duplicada — $fecha · $titulo"
|
|
289
|
+
fi
|
|
290
|
+
marcar_visto "$fecha" "$titulo"
|
|
291
|
+
escribir_cuerpo "$wd" "$prefijo" "$nombre" "$nuevo" "$fecha" "$titulo" "$cuerpo"
|
|
292
|
+
i=$((i + 1))
|
|
293
|
+
done
|
|
294
|
+
return 0
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
# os_migrate_trim_izq LINEA -> LINEA sin los espacios que la abren. Sin sed/cut/awk (E7): el clásico
|
|
298
|
+
# recorte por expansión de parámetros, quitando el prefijo que solo trae espacios.
|
|
299
|
+
os_migrate_trim_izq() {
|
|
300
|
+
local s="$1"
|
|
301
|
+
printf '%s' "${s#"${s%%[![:space:]]*}"}"
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
# os_migrate_bullet_sin_soporte LINEA -> 0 si LINEA (ya recortada) es un bloque de código o una fila
|
|
305
|
+
# de tabla — las dos formas que la spec 054 deja como condición de parada: el agente no decide cómo
|
|
306
|
+
# migrar eso, frena.
|
|
307
|
+
os_migrate_bullet_sin_soporte() {
|
|
308
|
+
case "$1" in
|
|
309
|
+
'```'*) return 0 ;;
|
|
310
|
+
'|'*) return 0 ;;
|
|
311
|
+
esac
|
|
312
|
+
return 1
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
escribir_desde_tematico() {
|
|
316
|
+
# escribir_desde_tematico CHUNK PREFIJO NOMBRE NUEVO_INDICE FECHA_HEADER WD REL_CANONICO
|
|
317
|
+
# Spec 054 C1: un bullet temático puede venir cortado en varias líneas (el brain escribe a cien
|
|
318
|
+
# columnas). Toda línea que arranca con dos o más espacios y sigue a un "- " es su continuación,
|
|
319
|
+
# hasta el próximo "- ", un encabezado, texto sin indentar, o el fin del bloque — se juntan en un
|
|
320
|
+
# párrafo (un espacio donde había salto) y el título sale de la primera oración del bullet entero,
|
|
321
|
+
# igual que hoy cuando el bullet era de una sola línea. Un sub-bullet (" - ") es continuación del
|
|
322
|
+
# padre, no un aprendizaje aparte (decisión cerrada de la spec).
|
|
323
|
+
local chunk="$1" prefijo="$2" nombre="$3" nuevo="$4" fecha_header="$5" wd="$6" rel="$7"
|
|
324
|
+
local -a lineas=()
|
|
325
|
+
local -a partes=()
|
|
326
|
+
local line en_bullet=0 trimmed cuerpo titulo
|
|
327
|
+
|
|
328
|
+
flush_bullet() {
|
|
329
|
+
[ "$en_bullet" = "1" ] || return 0
|
|
330
|
+
cuerpo="${partes[*]}"
|
|
331
|
+
titulo="${cuerpo%%. *}"
|
|
332
|
+
[ -n "$titulo" ] || titulo="$cuerpo"
|
|
333
|
+
if dup_visto "$fecha_header" "$titulo"; then
|
|
334
|
+
detener "entrada duplicada — $fecha_header · $titulo"
|
|
335
|
+
fi
|
|
336
|
+
marcar_visto "$fecha_header" "$titulo"
|
|
337
|
+
escribir_cuerpo "$wd" "$prefijo" "$nombre" "$nuevo" "$fecha_header" "$titulo" "$cuerpo"
|
|
338
|
+
partes=()
|
|
339
|
+
en_bullet=0
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
while IFS= read -r line || [ -n "$line" ]; do
|
|
343
|
+
case "$line" in
|
|
344
|
+
'- '*)
|
|
345
|
+
flush_bullet
|
|
346
|
+
trimmed="${line#- }"
|
|
347
|
+
if os_migrate_bullet_sin_soporte "$trimmed"; then
|
|
348
|
+
detener "$rel — un bullet cortado trae un bloque de código o una tabla: \"$trimmed\""
|
|
349
|
+
fi
|
|
350
|
+
partes=("$trimmed")
|
|
351
|
+
en_bullet=1
|
|
352
|
+
;;
|
|
353
|
+
' '*)
|
|
354
|
+
[ -n "$line" ] || continue
|
|
355
|
+
trimmed=$(os_migrate_trim_izq "$line")
|
|
356
|
+
[ -n "$trimmed" ] || continue
|
|
357
|
+
if [ "$en_bullet" = "1" ]; then
|
|
358
|
+
if os_migrate_bullet_sin_soporte "$trimmed"; then
|
|
359
|
+
detener "$rel — un bullet cortado trae un bloque de código o una tabla: \"$trimmed\""
|
|
360
|
+
fi
|
|
361
|
+
partes+=("$trimmed")
|
|
362
|
+
fi
|
|
363
|
+
;;
|
|
364
|
+
'## '*) flush_bullet ;;
|
|
365
|
+
'') : ;;
|
|
366
|
+
*) flush_bullet ;;
|
|
367
|
+
esac
|
|
368
|
+
done < "$chunk"
|
|
369
|
+
flush_bullet
|
|
370
|
+
return 0
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
procesar_bloque() {
|
|
374
|
+
# procesar_bloque CHUNK PREFIJO NOMBRE NUEVO_INDICE FECHA_HEADER WD REL_CANONICO
|
|
375
|
+
# Encuentra el primer "## " del bloque (spec 053 C3: puede no ser su primera línea — hay prosa
|
|
376
|
+
# antes), conserva esa prosa en la cabecera del índice nuevo una sola vez, y recorta el bloque
|
|
377
|
+
# para que arranque justo en el encabezado antes de decidir cómo partirlo.
|
|
378
|
+
local chunk="$1" prefijo="$2" nombre="$3" nuevo="$4" fecha_header="$5" wd="$6" rel="$7"
|
|
379
|
+
local primer_heading ln0 num0 prosa recortado primera_linea_heading
|
|
380
|
+
|
|
381
|
+
primera_linea_heading=$(grep -n '^## ' "$chunk" | head -1)
|
|
382
|
+
primer_heading="${primera_linea_heading%%:*}"
|
|
383
|
+
if [ -z "$primer_heading" ]; then
|
|
384
|
+
detener "$rel — un bloque sin encabezado \"## \""
|
|
385
|
+
fi
|
|
386
|
+
|
|
387
|
+
ln0=$(primera_linea_no_vacia "$chunk")
|
|
388
|
+
num0="${ln0%%:*}"
|
|
389
|
+
if [ "$num0" != "$primer_heading" ]; then
|
|
390
|
+
prosa=$(head -n "$((primer_heading - 1))" "$chunk")
|
|
391
|
+
{ printf '%s\n' "$prosa"; printf '\n'; } >> "$nuevo"
|
|
392
|
+
fi
|
|
393
|
+
recortado=$(mktemp "$wd/blk.XXXXXX")
|
|
394
|
+
tail -n +"$primer_heading" "$chunk" > "$recortado"
|
|
395
|
+
chunk="$recortado"
|
|
396
|
+
|
|
397
|
+
if bloque_headings_mixtos "$chunk"; then
|
|
398
|
+
detener "$rel — bloque con entradas fechadas y temáticas mezcladas"
|
|
399
|
+
fi
|
|
400
|
+
|
|
401
|
+
local primera
|
|
402
|
+
primera=$(head -n 1 "$chunk")
|
|
403
|
+
case "$primera" in
|
|
404
|
+
'## '*' · '*)
|
|
405
|
+
escribir_desde_bloque_fechado "$chunk" "$prefijo" "$nombre" "$nuevo" "$wd"
|
|
406
|
+
;;
|
|
407
|
+
'## '*)
|
|
408
|
+
if [ -z "$fecha_header" ]; then
|
|
409
|
+
detener "$rel — bullet sin fecha de header ni commit para fecharlo"
|
|
410
|
+
fi
|
|
411
|
+
escribir_desde_tematico "$chunk" "$prefijo" "$nombre" "$nuevo" "$fecha_header" "$wd" "$rel"
|
|
412
|
+
;;
|
|
413
|
+
esac
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
migrar_archivo() {
|
|
417
|
+
# migrar_archivo PREFIJO NOMBRE CONTENT_GLOB
|
|
418
|
+
local prefijo="$1" nombre="$2" content_glob="$3"
|
|
419
|
+
local archivo rel
|
|
420
|
+
archivo="$brain/${prefijo}${nombre}.md"
|
|
421
|
+
rel="${prefijo}${nombre}.md"
|
|
422
|
+
[ -f "$archivo" ] || return 0
|
|
423
|
+
|
|
424
|
+
os_tree_ensure "$brain" "$content_glob" content > /dev/null 2>&1 || true
|
|
425
|
+
|
|
426
|
+
grep -q '^## ' "$archivo" || return 0
|
|
427
|
+
|
|
428
|
+
if sucio "$rel"; then
|
|
429
|
+
detener "$rel tiene cambios sin commitear — otra sesión puede estar trabajando en él"
|
|
430
|
+
fi
|
|
431
|
+
|
|
432
|
+
creados=""
|
|
433
|
+
titulos_archivo=""
|
|
434
|
+
|
|
435
|
+
local fecha_header=""
|
|
436
|
+
fecha_header=$(fecha_declarada_en_header "$archivo") || true
|
|
437
|
+
[ -n "$fecha_header" ] || fecha_header=$(fecha_primer_commit "$rel")
|
|
438
|
+
|
|
439
|
+
# Spec 055 C1 · la cabecera es siempre lo de antes del primer "## " del archivo entero — nunca lo
|
|
440
|
+
# de antes del primer "---" — así que ese corte (os_migrate_header_end_linea) se calcula primero
|
|
441
|
+
# y el "---" solo cuenta para partir en bloques lo que queda desde ahí. El `grep -q '^## '` de
|
|
442
|
+
# arriba ya garantiza que el corte no es vacío antes de llegar acá.
|
|
443
|
+
local chunk_dir i=1 n lineno=0 heading_ln
|
|
444
|
+
heading_ln=$(os_migrate_header_end_linea "$archivo")
|
|
445
|
+
chunk_dir=$(mktemp -d)
|
|
446
|
+
mkdir -p "$chunk_dir/out"
|
|
447
|
+
: > "$chunk_dir/header"
|
|
448
|
+
: > "$chunk_dir/c1"
|
|
449
|
+
while IFS= read -r line || [ -n "$line" ]; do
|
|
450
|
+
lineno=$((lineno + 1))
|
|
451
|
+
if [ "$lineno" -lt "$heading_ln" ]; then
|
|
452
|
+
printf '%s\n' "$line" >> "$chunk_dir/header"
|
|
453
|
+
continue
|
|
454
|
+
fi
|
|
455
|
+
if [ "$lineno" -gt "$heading_ln" ] && [ "$line" = "---" ]; then
|
|
456
|
+
i=$((i + 1))
|
|
457
|
+
: > "$chunk_dir/c$i"
|
|
458
|
+
continue
|
|
459
|
+
fi
|
|
460
|
+
printf '%s\n' "$line" >> "$chunk_dir/c$i"
|
|
461
|
+
done < "$archivo"
|
|
462
|
+
|
|
463
|
+
visto=""
|
|
464
|
+
local nuevo="$chunk_dir/nuevo-indice"
|
|
465
|
+
cp "$chunk_dir/header" "$nuevo"
|
|
466
|
+
|
|
467
|
+
n=1
|
|
468
|
+
while [ "$n" -le "$i" ]; do
|
|
469
|
+
if [ -s "$chunk_dir/c$n" ]; then
|
|
470
|
+
procesar_bloque "$chunk_dir/c$n" "$prefijo" "$nombre" "$nuevo" "$fecha_header" "$chunk_dir" "$rel"
|
|
471
|
+
fi
|
|
472
|
+
n=$((n + 1))
|
|
473
|
+
done
|
|
474
|
+
|
|
475
|
+
# Todo el canónico se pudo partir: recién ahora se toca el disco real del brain — un `mkdir -p`
|
|
476
|
+
# más un solo `mv` (spec 053 C5), nunca antes de saber que terminó entero.
|
|
477
|
+
if [ -n "$(ls -A "$chunk_dir/out/$nombre" 2>/dev/null)" ]; then
|
|
478
|
+
if ! mkdir -p "$brain/${prefijo}${nombre}" 2>/dev/null; then
|
|
479
|
+
detener "$rel — no se pudo crear ${prefijo}${nombre}/"
|
|
480
|
+
fi
|
|
481
|
+
if ! mv "$chunk_dir/out/$nombre"/* "$brain/${prefijo}${nombre}/" 2>"$chunk_dir/mv-err"; then
|
|
482
|
+
local detalle
|
|
483
|
+
detalle=$(cat "$chunk_dir/mv-err" 2>/dev/null)
|
|
484
|
+
detener "$rel — no se pudo escribir en ${prefijo}${nombre}/ ($detalle) — quedaban: $titulos_archivo"
|
|
485
|
+
fi
|
|
486
|
+
fi
|
|
487
|
+
|
|
488
|
+
mv "$nuevo" "$archivo"
|
|
489
|
+
rm -rf "$chunk_dir"
|
|
490
|
+
push "índice reescrito: $rel"
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
migrar_todo() {
|
|
494
|
+
local o d t
|
|
495
|
+
migrar_archivo "" decisions "decisions/*.md"
|
|
496
|
+
migrar_archivo "" learnings "learnings/*.md"
|
|
497
|
+
for o in $(os_org_slugs "$brain"); do
|
|
498
|
+
migrar_archivo "$wsdir/$o/" decisions "$wsdir/*/decisions/*.md"
|
|
499
|
+
migrar_archivo "$wsdir/$o/" learnings "$wsdir/*/learnings/*.md"
|
|
500
|
+
for t in $memory_types_org; do
|
|
501
|
+
[ -d "$brain/$wsdir/$o/$t" ] || continue
|
|
502
|
+
for d in "$brain/$wsdir/$o/$t"/*; do
|
|
503
|
+
[ -d "$d" ] || continue
|
|
504
|
+
migrar_archivo "$wsdir/$o/$t/$(basename "$d")/" decisions "$wsdir/*/$t/*/decisions/*.md"
|
|
505
|
+
done
|
|
506
|
+
done
|
|
507
|
+
done
|
|
508
|
+
for t in $memory_types_root; do
|
|
509
|
+
[ -d "$brain/$t" ] || continue
|
|
510
|
+
for d in "$brain/$t"/*; do
|
|
511
|
+
[ -d "$d" ] || continue
|
|
512
|
+
migrar_archivo "$t/$(basename "$d")/" decisions "$t/*/decisions/*.md"
|
|
513
|
+
done
|
|
514
|
+
done
|
|
515
|
+
}
|
|
516
|
+
migrar_todo
|
|
517
|
+
|
|
518
|
+
if [ -z "$reporte" ]; then
|
|
519
|
+
printf 'nada para migrar: todo canónico ya era índice\n'
|
|
520
|
+
else
|
|
521
|
+
printf '%s' "$reporte"
|
|
522
|
+
fi
|
|
523
|
+
exit 0
|