@giamme/forge 0.18.0 → 0.20.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/CHANGELOG.md +49 -0
- package/README.md +1 -0
- package/SKILL.md +19 -2
- package/VERSION +1 -1
- package/docs/artifacts.md +25 -1
- package/docs/commands.md +217 -18
- package/docs/decomposed-runs.md +114 -1
- package/docs/development.md +2 -1
- package/docs/forge-ui.md +138 -18
- package/docs/memory.md +25 -4
- package/docs/models.md +8 -0
- package/docs/reviews.md +122 -0
- package/forge +26 -3
- package/package.json +2 -1
- package/references/dataset.md +20 -1
- package/references/decompose.md +641 -41
- package/references/harnesses.md +18 -3
- package/references/memory.md +30 -5
- package/references/planning.md +43 -14
- package/references/ui.md +53 -6
- package/registry.tsv +8 -3
- package/scripts/forge-brief.py +88 -3
- package/scripts/forge-compare.py +4 -3
- package/scripts/forge-contract.py +202 -27
- package/scripts/forge-control.py +117 -5
- package/scripts/forge-dispatch.sh +34 -5
- package/scripts/forge-findings.py +675 -0
- package/scripts/forge-group.py +496 -0
- package/scripts/forge-groups.py +89 -0
- package/scripts/forge-handoff.py +1128 -0
- package/scripts/forge-issues.py +228 -97
- package/scripts/forge-lib-group.sh +641 -0
- package/scripts/forge-lib-plans.sh +339 -3
- package/scripts/forge-lib-task.sh +197 -4
- package/scripts/forge-memory.sh +711 -17
- package/scripts/forge-parallel.sh +721 -38
- package/scripts/forge-planner.py +104 -8
- package/scripts/forge-route.py +190 -12
- package/scripts/forge-runs.py +9 -5
- package/scripts/forge-schedule.py +417 -52
- package/scripts/forge-solo.sh +2 -0
- package/scripts/forge-sweep.py +199 -44
- package/scripts/forge-worktrees.py +243 -0
- package/scripts/forge_brief/__init__.py +294 -50
- package/scripts/forge_brief/answers.py +58 -11
- package/scripts/forge_brief/proposals.py +1630 -0
- package/scripts/forge_dataset/efforts.py +4 -3
- package/scripts/forge_dataset/export.py +24 -12
- package/scripts/forge_dataset/facts.py +5 -3
- package/scripts/forge_dataset/forecast.py +8 -18
- package/scripts/forge_dataset/harvest.py +204 -9
- package/scripts/forge_dataset/history.py +11 -6
- package/scripts/forge_dataset/importer.py +65 -1
- package/scripts/forge_dataset/names.py +66 -0
- package/scripts/forge_dataset/reviewers.py +5 -2
- package/scripts/forge_dataset/risk.py +6 -3
- package/scripts/forge_dataset/routing.py +6 -1
- package/scripts/forge_findings/__init__.py +17 -0
- package/scripts/forge_findings/apply.py +441 -0
- package/scripts/forge_findings/status.py +43 -0
- package/scripts/forge_findings/store.py +583 -0
- package/scripts/forge_findings/tags.py +53 -0
- package/scripts/forge_findings/triage.py +603 -0
- package/scripts/forge_groups/__init__.py +308 -0
- package/scripts/forge_groups/attribute.py +289 -0
- package/scripts/forge_groups/review.py +286 -0
- package/scripts/forge_identity.py +327 -0
- package/scripts/forge_memory_list.py +7 -3
- package/scripts/forge_owner.py +152 -0
- package/scripts/forge_side/__init__.py +228 -38
- package/ui/docs/api.md +303 -29
- package/ui/forge_ui/control.py +1018 -6
- package/ui/forge_ui/executions.py +8 -1
- package/ui/forge_ui/export.py +31 -4
- package/ui/forge_ui/groups.py +264 -0
- package/ui/forge_ui/models.py +70 -8
- package/ui/forge_ui/projects.py +209 -22
- package/ui/forge_ui/reader.py +262 -19
- package/ui/forge_ui/server.py +4 -0
- package/ui/forge_ui/static/app.js +93 -13
- package/ui/forge_ui/static/briefview.js +350 -47
- package/ui/forge_ui/static/diffview.js +86 -1
- package/ui/forge_ui/static/findingsactions.js +201 -0
- package/ui/forge_ui/static/groupsview.js +332 -0
- package/ui/forge_ui/static/index.html +3 -0
- package/ui/forge_ui/static/panel.js +127 -7
- package/ui/forge_ui/static/projectsview.js +764 -30
- package/ui/forge_ui/static/runmodel.js +14 -3
- package/ui/forge_ui/static/storageview.js +91 -10
- package/ui/forge_ui/static/style.css +138 -5
- package/ui/forge_ui/static/triageview.js +423 -0
- package/ui/forge_ui/static/worktreesview.js +233 -12
- package/ui/forge_ui/storage.py +14 -0
package/scripts/forge-memory.sh
CHANGED
|
@@ -26,6 +26,10 @@
|
|
|
26
26
|
# forge-memory.sh prune <repo>
|
|
27
27
|
# forge-memory.sh spend <repo> [--run <run-id>]
|
|
28
28
|
# forge-memory.sh learnings <repo> [--json] every ledger learning and its state, as JSON
|
|
29
|
+
# forge-memory.sh forget <repo> <category:key>... [--dry-run] [--json] [--actor ui|cli]
|
|
30
|
+
# forge-memory.sh add <repo> --category <verify|trap|finding> --text TEXT [--path P]...
|
|
31
|
+
# [--actor ui|cli] [--json]
|
|
32
|
+
# forge-memory.sh edit <repo> <category:key> --text TEXT [--path P]... [--actor ui|cli] [--json]
|
|
29
33
|
#
|
|
30
34
|
# Exit codes: 0 ok | 2 usage error
|
|
31
35
|
set -uo pipefail
|
|
@@ -122,22 +126,34 @@ section_title() {
|
|
|
122
126
|
# cannot leave memory half-updated — it can only leave the ledger short a row.
|
|
123
127
|
# With <out>, the memory is written there instead of the repo's .forge/memory.md, and the repo is
|
|
124
128
|
# only read: `learnings` uses that to say what a rebuild keeps without changing anything.
|
|
125
|
-
|
|
126
|
-
|
|
129
|
+
# With <ledger>, that file is read instead of the repo's ledger. `forget --dry-run` passes a
|
|
130
|
+
# scratch copy that already has the rows removed, and a scratch <out>, so the repo is not touched.
|
|
131
|
+
rebuild() { # rebuild <repo> [<out> [<ledger>]] -> prints "kept<TAB>pruned_stale<TAB>pruned_cap"
|
|
132
|
+
local repo="$1" led mem tmp cand jev_loaded=0 personf tmproles
|
|
127
133
|
if [ "${FORGE_MEMORY_DEEP_PRUNE:-0}" = 1 ] && [ -f "$SKILL_DIR/scripts/forge-jev-options.sh" ]; then
|
|
128
134
|
source "$SKILL_DIR/scripts/forge-jev-options.sh" 2>/dev/null && jev_loaded=1
|
|
129
135
|
fi
|
|
130
|
-
led="$(ledger_for "$repo")"; mem="${2:-$(memory_for "$repo")}"
|
|
136
|
+
led="${3:-$(ledger_for "$repo")}"; mem="${2:-$(memory_for "$repo")}"
|
|
131
137
|
[ -f "$led" ] || return 0
|
|
132
138
|
tmp="$mem.tmp.$$"; cand="$mem.cand.$$"
|
|
139
|
+
# Learnings a person wrote are kept on the first row and trimmed last. The cand
|
|
140
|
+
# file itself stays the old four columns: sort's tie-break is the whole line, so an
|
|
141
|
+
# extra column would reshuffle two facts that share a count and a timestamp.
|
|
142
|
+
personf="$mem.person.$$"; tmproles="$mem.roles.$$"
|
|
143
|
+
rm -f "$personf" "$tmproles"
|
|
144
|
+
: > "$tmproles"
|
|
133
145
|
|
|
134
146
|
# Group by (category, key). count = number of DISTINCT runs the key appeared in,
|
|
135
147
|
# which is what makes a recurrence threshold meaningful: one run emitting the
|
|
136
|
-
# same learning twice is still one occurrence.
|
|
137
|
-
|
|
148
|
+
# same learning twice is still one occurrence. A row with role person, injectable
|
|
149
|
+
# or not, marks the learning so the finding threshold below does not apply.
|
|
150
|
+
FORGE_MEMORY_PERSON="$personf" awk -F'\t' '
|
|
151
|
+
BEGIN { personfile = ENVIRON["FORGE_MEMORY_PERSON"] }
|
|
138
152
|
/^#/ || NF < 9 { next }
|
|
153
|
+
{ if ($4 == "person") person[$7 "\034" $8] = 1 }
|
|
139
154
|
# Column 11 is the injectable flag. Rows written before it existed have NF == 10
|
|
140
155
|
# and are treated as injectable, so an existing ledger keeps behaving as it did.
|
|
156
|
+
# Column 12 (paths) is only for edit; rebuild does not read it.
|
|
141
157
|
NF >= 11 && $11 == "0" { next }
|
|
142
158
|
{
|
|
143
159
|
ts=$1; run=$2; cat=$7; key=$8; txt=$9
|
|
@@ -146,21 +162,30 @@ rebuild() { # rebuild <repo> [<out>] -> prints "kept<TAB>pruned_stale<TAB>pruned
|
|
|
146
162
|
if (ts >= lastts[k]) { lastts[k]=ts; last[k]=txt }
|
|
147
163
|
catof[k]=cat
|
|
148
164
|
}
|
|
149
|
-
END {
|
|
165
|
+
END {
|
|
166
|
+
for (k in cnt) printf "%s\t%d\t%s\t%s\n", catof[k], cnt[k], lastts[k], last[k]
|
|
167
|
+
for (k in cnt) if (person[k]) printf "%s\t%s\t%s\n", catof[k], lastts[k], last[k] > personfile
|
|
168
|
+
}
|
|
150
169
|
' "$led" > "$cand" 2>/dev/null
|
|
151
170
|
|
|
152
171
|
local kept=0 stale=0 capped=0
|
|
153
172
|
: > "$tmp"
|
|
154
|
-
local sec cat count ts text anchor line
|
|
173
|
+
local sec cat count ts text anchor line person browns
|
|
155
174
|
for sec in verify trap finding; do
|
|
156
175
|
local body="$mem.sec.$$"; : > "$body"
|
|
176
|
+
browns="$body.roles"; : > "$browns"
|
|
157
177
|
# Highest count first, then most recent — the ordering a human scans for a
|
|
158
178
|
# systemic problem, and deterministic so tests can assert on it.
|
|
159
179
|
while IFS=$'\t' read -r cat count ts text; do
|
|
160
180
|
[ "$cat" = "$sec" ] || continue
|
|
181
|
+
person=0
|
|
182
|
+
if [ -s "$personf" ] && grep -F -x -q -- "$cat"$'\t'"$ts"$'\t'"$text" "$personf"; then
|
|
183
|
+
person=1
|
|
184
|
+
fi
|
|
161
185
|
# A finding seen once is an incident, not a pattern. Promoting it would fill
|
|
162
186
|
# memory with one-offs, which is exactly how this becomes a junk drawer.
|
|
163
|
-
|
|
187
|
+
# A person's own learning is kept on its first row, in every category.
|
|
188
|
+
if [ "$sec" = finding ] && [ "$count" -lt 2 ] && [ "$person" != 1 ]; then continue; fi
|
|
164
189
|
anchor="$(anchor_of "$text")"
|
|
165
190
|
# An entry anchored to a path that no longer exists is stale by construction:
|
|
166
191
|
# the code it describes is gone, so the fact is at best useless and at worst
|
|
@@ -188,19 +213,21 @@ rebuild() { # rebuild <repo> [<out>] -> prints "kept<TAB>pruned_stale<TAB>pruned
|
|
|
188
213
|
else
|
|
189
214
|
printf '%s\t- %s\n' "$count" "$text" >> "$body"
|
|
190
215
|
fi
|
|
216
|
+
if [ "$person" = 1 ]; then printf 'p\n' >> "$browns"; else printf 'n\n' >> "$browns"; fi
|
|
191
217
|
done < <(sort -t"$TAB" -k2,2nr -k3,3r "$cand")
|
|
192
218
|
|
|
193
219
|
if [ -s "$body" ]; then
|
|
194
220
|
{ section_title "$sec"; cut -f2- "$body"; echo; } >> "$tmp"
|
|
221
|
+
{ printf '.\n'; cat "$browns"; printf '.\n'; } >> "$tmproles"
|
|
195
222
|
kept=$((kept + $(wc -l < "$body" | tr -d ' ')))
|
|
196
223
|
fi
|
|
197
|
-
rm -f "$body"
|
|
224
|
+
rm -f "$body" "$browns"
|
|
198
225
|
done
|
|
199
|
-
rm -f "$cand"
|
|
226
|
+
rm -f "$cand" "$personf"
|
|
200
227
|
|
|
201
|
-
if [ ! -s "$tmp" ]; then rm -f "$tmp" "$mem"; printf '0\t%s\t0\n' "$stale"; return 0; fi
|
|
228
|
+
if [ ! -s "$tmp" ]; then rm -f "$tmp" "$tmproles" "$mem"; printf '0\t%s\t0\n' "$stale"; return 0; fi
|
|
202
229
|
|
|
203
|
-
local header="$mem.hdr.$$"
|
|
230
|
+
local header="$mem.hdr.$$" roles="$mem.caproles.$$"
|
|
204
231
|
{
|
|
205
232
|
echo "# forge project memory"
|
|
206
233
|
echo "<!-- Maintained by forge from what its own runs observed. Do not edit this file"
|
|
@@ -209,17 +236,37 @@ rebuild() { # rebuild <repo> [<out>] -> prints "kept<TAB>pruned_stale<TAB>pruned
|
|
|
209
236
|
echo
|
|
210
237
|
cat "$tmp"
|
|
211
238
|
} > "$header"
|
|
239
|
+
# One role per header line, in lockstep: n a run fact, p a person's fact, . the rest.
|
|
240
|
+
# The five dots are the header lines above. With no person rows every fact is n, so the
|
|
241
|
+
# trim below still deletes the last fact line and memory.md stays byte-identical.
|
|
242
|
+
{
|
|
243
|
+
printf '.\n.\n.\n.\n.\n'
|
|
244
|
+
cat "$tmproles"
|
|
245
|
+
} > "$roles"
|
|
212
246
|
|
|
213
247
|
# Cap enforcement. Everything above already dropped stale entries and unpromoted
|
|
214
|
-
# findings, so what is left is real
|
|
215
|
-
# sort above has arranged to be the lowest-count, least-recent
|
|
248
|
+
# findings, so what is left is real. A run fact is trimmed from the bottom, which the
|
|
249
|
+
# sort above has arranged to be the lowest-count, least-recent. A person's fact goes
|
|
250
|
+
# only once no run fact is left.
|
|
216
251
|
while [ "$(wc -l < "$header" | tr -d ' ')" -gt "$MAX_LINES" ] || \
|
|
217
252
|
[ "$(wc -c < "$header" | tr -d ' ')" -gt "$MAX_BYTES" ]; do
|
|
218
253
|
local last_fact
|
|
219
|
-
last_fact="$(
|
|
254
|
+
last_fact="$(awk '
|
|
255
|
+
{ role[NR] = $0 }
|
|
256
|
+
END {
|
|
257
|
+
last_n = 0; last_any = 0
|
|
258
|
+
for (i = 1; i <= NR; i++) {
|
|
259
|
+
if (role[i] == "n") last_n = i
|
|
260
|
+
if (role[i] == "n" || role[i] == "p") last_any = i
|
|
261
|
+
}
|
|
262
|
+
if (last_n) print last_n
|
|
263
|
+
else if (last_any) print last_any
|
|
264
|
+
}
|
|
265
|
+
' "$roles")"
|
|
220
266
|
[ -n "$last_fact" ] || break
|
|
221
267
|
[ -z "$MEMORY_DROPS" ] || printf 'cap\t-\t%s\n' "$(sed -n "${last_fact}p" "$header")" >> "$MEMORY_DROPS"
|
|
222
268
|
sed "${last_fact}d" "$header" > "$header.x" && mv "$header.x" "$header"
|
|
269
|
+
sed "${last_fact}d" "$roles" > "$roles.x" && mv "$roles.x" "$roles"
|
|
223
270
|
capped=$((capped+1)); kept=$((kept-1))
|
|
224
271
|
done
|
|
225
272
|
# The cap can eat a section's last fact, leaving a heading over nothing.
|
|
@@ -227,7 +274,7 @@ rebuild() { # rebuild <repo> [<out>] -> prints "kept<TAB>pruned_stale<TAB>pruned
|
|
|
227
274
|
|
|
228
275
|
[ -n "${2:-}" ] || mkdir -p "$(dir_for "$repo")"
|
|
229
276
|
mv "$header" "$mem"
|
|
230
|
-
rm -f "$tmp"
|
|
277
|
+
rm -f "$tmp" "$tmproles" "$roles"
|
|
231
278
|
printf '%s\t%s\t%s\n' "$kept" "$stale" "$capped"
|
|
232
279
|
}
|
|
233
280
|
|
|
@@ -583,6 +630,642 @@ do_spend() {
|
|
|
583
630
|
' "$led"
|
|
584
631
|
}
|
|
585
632
|
|
|
633
|
+
# --- forget -------------------------------------------------------------------
|
|
634
|
+
# Drop every ledger row of the given category:key learnings and rebuild memory.md.
|
|
635
|
+
# A learning is the (category, key) group `rebuild` and `learnings` use. Rows have no id.
|
|
636
|
+
# FORGE_MEMORY=off stops record, note and inject from writing: those run unasked. show and
|
|
637
|
+
# learnings still read the ledger (learnings only reports memory_off). forget is an explicit
|
|
638
|
+
# edit of that same ledger, so it runs when memory is off too. There is no early return.
|
|
639
|
+
#
|
|
640
|
+
# Sets FORGET_CANON to the ledger's "category:key" form (norm_key on each part), or exits 2.
|
|
641
|
+
# <cmd> is the word in the error (default forget). edit uses the same parse and must not
|
|
642
|
+
# tell the person they ran forget.
|
|
643
|
+
parse_forget_key() { # parse_forget_key <raw> [cmd]
|
|
644
|
+
local raw="$1" cmd="${2:-forget}" cat key
|
|
645
|
+
case "$raw" in
|
|
646
|
+
*:*) ;;
|
|
647
|
+
*) die "$cmd: KEY must be category:key, got '$raw'" ;;
|
|
648
|
+
esac
|
|
649
|
+
cat="$(norm_key "${raw%%:*}")"
|
|
650
|
+
key="$(norm_key "${raw#*:}")"
|
|
651
|
+
[ -n "$cat" ] && [ -n "$key" ] || die "$cmd: KEY must be category:key with neither part empty, got '$raw'"
|
|
652
|
+
FORGET_CANON="$cat:$key"
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
# Rewrite the ledger without the requested keys. Kept lines stay byte-identical, including
|
|
656
|
+
# a missing final newline. Prints how many rows matched. Counts are "<key>\t<rows>\t<1|0>".
|
|
657
|
+
forget_filter() { # forget_filter <ledger> <keys-file> <out-ledger> <counts-file>
|
|
658
|
+
python3 - "$@" <<'PY'
|
|
659
|
+
import sys
|
|
660
|
+
ledger_path, keys_path, out_path, counts_path = sys.argv[1:5]
|
|
661
|
+
keys = [line.rstrip('\n') for line in open(keys_path, encoding='utf-8') if line.rstrip('\n')]
|
|
662
|
+
seen = {}
|
|
663
|
+
for key in keys:
|
|
664
|
+
cat, sep, rest = key.partition(':')
|
|
665
|
+
if sep:
|
|
666
|
+
seen.setdefault((cat, rest), 0)
|
|
667
|
+
with open(ledger_path, 'rb') as handle:
|
|
668
|
+
data = handle.read()
|
|
669
|
+
kept = []
|
|
670
|
+
for line in data.splitlines(keepends=True):
|
|
671
|
+
raw = line.rstrip(b'\n').rstrip(b'\r')
|
|
672
|
+
if raw.startswith(b'#') or raw.count(b'\t') < 8:
|
|
673
|
+
kept.append(line)
|
|
674
|
+
continue
|
|
675
|
+
cols = raw.split(b'\t')
|
|
676
|
+
if len(cols) < 9:
|
|
677
|
+
kept.append(line)
|
|
678
|
+
continue
|
|
679
|
+
pair = (cols[6].decode('utf-8', 'surrogateescape'), cols[7].decode('utf-8', 'surrogateescape'))
|
|
680
|
+
if pair in seen:
|
|
681
|
+
seen[pair] += 1
|
|
682
|
+
continue
|
|
683
|
+
kept.append(line)
|
|
684
|
+
with open(out_path, 'wb') as handle:
|
|
685
|
+
handle.write(b''.join(kept))
|
|
686
|
+
with open(counts_path, 'w', encoding='utf-8') as handle:
|
|
687
|
+
for key in keys:
|
|
688
|
+
cat, _, rest = key.partition(':')
|
|
689
|
+
n = seen.get((cat, rest), 0)
|
|
690
|
+
handle.write('%s\t%d\t%s\n' % (key, n, '1' if n else '0'))
|
|
691
|
+
sys.stdout.write('%d\n' % sum(seen.values()))
|
|
692
|
+
PY
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
# Fact lines (`- ...`) present in <before> and absent from <after>, in the before file's
|
|
696
|
+
# order. A section heading disappears with its last fact; it is formatting, and so are the
|
|
697
|
+
# header and blank lines. Callers match on the `- ` lines. A missing file counts as empty.
|
|
698
|
+
forget_removed() { # forget_removed <before> <after> <out>
|
|
699
|
+
python3 - "$@" <<'PY'
|
|
700
|
+
import sys
|
|
701
|
+
from collections import Counter
|
|
702
|
+
before_path, after_path, out_path = sys.argv[1:4]
|
|
703
|
+
|
|
704
|
+
def lines(path):
|
|
705
|
+
try:
|
|
706
|
+
with open(path, encoding='utf-8') as handle:
|
|
707
|
+
return handle.read().splitlines()
|
|
708
|
+
except FileNotFoundError:
|
|
709
|
+
return []
|
|
710
|
+
|
|
711
|
+
have = Counter(lines(after_path))
|
|
712
|
+
removed = []
|
|
713
|
+
for line in lines(before_path):
|
|
714
|
+
if not line.startswith('- '):
|
|
715
|
+
continue
|
|
716
|
+
if have[line]:
|
|
717
|
+
have[line] -= 1
|
|
718
|
+
else:
|
|
719
|
+
removed.append(line)
|
|
720
|
+
with open(out_path, 'w', encoding='utf-8') as handle:
|
|
721
|
+
for line in removed:
|
|
722
|
+
handle.write(line + '\n')
|
|
723
|
+
PY
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
forget_report() { # forget_report <json 0|1> <dry 0|1> <counts> <removed>
|
|
727
|
+
if [ "$1" = 1 ]; then
|
|
728
|
+
python3 - "$2" "$3" "$4" <<'PY'
|
|
729
|
+
import json, sys
|
|
730
|
+
dry, counts_path, removed_path = sys.argv[1:4]
|
|
731
|
+
keys = []
|
|
732
|
+
for line in open(counts_path, encoding='utf-8'):
|
|
733
|
+
line = line.rstrip('\n')
|
|
734
|
+
if not line:
|
|
735
|
+
continue
|
|
736
|
+
key, rows, found = line.split('\t')
|
|
737
|
+
keys.append({"key": key, "rows": int(rows), "found": found == "1"})
|
|
738
|
+
removed = []
|
|
739
|
+
try:
|
|
740
|
+
for line in open(removed_path, encoding='utf-8'):
|
|
741
|
+
line = line.rstrip('\n')
|
|
742
|
+
if line:
|
|
743
|
+
removed.append(line)
|
|
744
|
+
except FileNotFoundError:
|
|
745
|
+
pass
|
|
746
|
+
json.dump({"ok": True, "dry_run": dry == "1", "keys": keys, "memory_lines_removed": removed},
|
|
747
|
+
sys.stdout)
|
|
748
|
+
sys.stdout.write('\n')
|
|
749
|
+
PY
|
|
750
|
+
else
|
|
751
|
+
local key rows found
|
|
752
|
+
while IFS=$'\t' read -r key rows found; do
|
|
753
|
+
[ -n "$key" ] || continue
|
|
754
|
+
if [ "$found" = 1 ]; then
|
|
755
|
+
printf '%s\t%s\n' "$key" "$rows"
|
|
756
|
+
else
|
|
757
|
+
printf '%s\tnot found\n' "$key"
|
|
758
|
+
fi
|
|
759
|
+
done < "$3"
|
|
760
|
+
fi
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
forget_fail() { # forget_fail <scratch> <message>
|
|
764
|
+
[ -n "$1" ] && rm -rf "$1"
|
|
765
|
+
die "$2"
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
do_forget() { # do_forget <repo> <category:key>... [--dry-run] [--json] [--actor ui|cli]
|
|
769
|
+
local repo="" dry=0 as_json=0 actor="cli" raw c keylist="" total=0 status=0 tmp="" scratch="" already=0
|
|
770
|
+
local -a rawkeys=() canon=()
|
|
771
|
+
while [ $# -gt 0 ]; do
|
|
772
|
+
case "$1" in
|
|
773
|
+
-h|--help) memory_usage; exit 0 ;;
|
|
774
|
+
--dry-run) dry=1; shift ;;
|
|
775
|
+
--json) as_json=1; shift ;;
|
|
776
|
+
--actor)
|
|
777
|
+
[ $# -ge 2 ] || die "forget: --actor needs ui or cli"
|
|
778
|
+
actor="$2"; shift 2
|
|
779
|
+
case "$actor" in
|
|
780
|
+
ui|cli) ;;
|
|
781
|
+
*) die "forget: --actor must be ui or cli" ;;
|
|
782
|
+
esac
|
|
783
|
+
;;
|
|
784
|
+
--) shift
|
|
785
|
+
while [ $# -gt 0 ]; do rawkeys+=("$1"); shift; done
|
|
786
|
+
;;
|
|
787
|
+
-*) die "forget: unknown option '$1'" ;;
|
|
788
|
+
*)
|
|
789
|
+
if [ -z "$repo" ]; then repo="$1"; else rawkeys+=("$1"); fi
|
|
790
|
+
shift
|
|
791
|
+
;;
|
|
792
|
+
esac
|
|
793
|
+
done
|
|
794
|
+
[ -n "$repo" ] || die "forget: usage: forget <repo> <category:key>... [--dry-run] [--json] [--actor ui|cli]"
|
|
795
|
+
[ "${#rawkeys[@]}" -ge 1 ] || die "forget: usage: forget <repo> <category:key>... [--dry-run] [--json] [--actor ui|cli]"
|
|
796
|
+
[ -d "$repo" ] || die "forget: '$repo' is not a directory"
|
|
797
|
+
# One learning once. A repeated KEY, or two spellings that norm_key collapses together,
|
|
798
|
+
# is a single entry in the report and in the dataset event's keys list. rows counts
|
|
799
|
+
# ledger rows, so listing the key twice must not count them twice either.
|
|
800
|
+
for raw in "${rawkeys[@]}"; do
|
|
801
|
+
parse_forget_key "$raw"
|
|
802
|
+
already=0
|
|
803
|
+
if [ "${#canon[@]}" -gt 0 ]; then
|
|
804
|
+
for c in "${canon[@]}"; do
|
|
805
|
+
[ "$c" = "$FORGET_CANON" ] && already=1 && break
|
|
806
|
+
done
|
|
807
|
+
fi
|
|
808
|
+
[ "$already" = 1 ] || canon+=("$FORGET_CANON")
|
|
809
|
+
done
|
|
810
|
+
keylist="${canon[0]}"
|
|
811
|
+
if [ "${#canon[@]}" -gt 1 ]; then
|
|
812
|
+
for c in "${canon[@]:1}"; do keylist="$keylist,$c"; done
|
|
813
|
+
fi
|
|
814
|
+
|
|
815
|
+
scratch="$(mktemp -d "${TMPDIR:-/tmp}/forge-memory-forget.XXXXXX")" || die "forget: no scratch dir"
|
|
816
|
+
printf '%s\n' "${canon[@]}" > "$scratch/keys"
|
|
817
|
+
local led mem
|
|
818
|
+
led="$(ledger_for "$repo")"
|
|
819
|
+
mem="$(memory_for "$repo")"
|
|
820
|
+
if [ -f "$led" ]; then
|
|
821
|
+
total="$(forget_filter "$led" "$scratch/keys" "$scratch/ledger.new" "$scratch/counts")" \
|
|
822
|
+
|| forget_fail "$scratch" "forget: could not read '$led'"
|
|
823
|
+
else
|
|
824
|
+
total=0
|
|
825
|
+
: > "$scratch/counts"
|
|
826
|
+
for c in "${canon[@]}"; do printf '%s\t0\t0\n' "$c" >> "$scratch/counts"; done
|
|
827
|
+
fi
|
|
828
|
+
case "$total" in
|
|
829
|
+
''|*[!0-9]*) forget_fail "$scratch" "forget: could not read '$led'" ;;
|
|
830
|
+
esac
|
|
831
|
+
|
|
832
|
+
: > "$scratch/removed"
|
|
833
|
+
if [ "$total" -gt 0 ]; then
|
|
834
|
+
# Lines that leave are the difference between two rebuilds of the ledger, not between
|
|
835
|
+
# whatever memory.md happens to be on disk. A missing file would otherwise report
|
|
836
|
+
# nothing, and a stale or hand-edited one would report edits this forget did not make.
|
|
837
|
+
# The before rebuild reads the full ledger and writes only the scratch <out>.
|
|
838
|
+
rebuild "$repo" "$scratch/before.md" "$led" >/dev/null \
|
|
839
|
+
|| forget_fail "$scratch" "forget: could not preview memory"
|
|
840
|
+
if [ "$dry" = 1 ]; then
|
|
841
|
+
# Same rebuild `record` runs, against the reduced ledger, written outside the repo.
|
|
842
|
+
rebuild "$repo" "$scratch/after.md" "$scratch/ledger.new" >/dev/null \
|
|
843
|
+
|| forget_fail "$scratch" "forget: could not preview memory"
|
|
844
|
+
forget_removed "$scratch/before.md" "$scratch/after.md" "$scratch/removed" \
|
|
845
|
+
|| forget_fail "$scratch" "forget: could not compare memory"
|
|
846
|
+
else
|
|
847
|
+
# do_record appends with >> and takes no lock, so there is none to share. The new
|
|
848
|
+
# ledger is written beside the old one and renamed over it, which is atomic on one
|
|
849
|
+
# filesystem. A concurrent append that lands between this read and that rename is
|
|
850
|
+
# the remaining race: that row is lost.
|
|
851
|
+
tmp="$(mktemp "$(dir_for "$repo")/ledger.tsv.tmp.XXXXXX")" \
|
|
852
|
+
|| forget_fail "$scratch" "forget: could not write the ledger"
|
|
853
|
+
# mktemp makes it 0600: cp -p first gives it the ledger's own mode, which the write keeps.
|
|
854
|
+
{ cp -p "$led" "$tmp" && cat "$scratch/ledger.new" > "$tmp"; } \
|
|
855
|
+
|| { rm -f "$tmp"; forget_fail "$scratch" "forget: could not write the ledger"; }
|
|
856
|
+
mv -f "$tmp" "$led" \
|
|
857
|
+
|| { rm -f "$tmp"; forget_fail "$scratch" "forget: could not replace the ledger"; }
|
|
858
|
+
rebuild "$repo" >/dev/null || forget_fail "$scratch" "forget: could not rebuild memory"
|
|
859
|
+
forget_removed "$scratch/before.md" "$mem" "$scratch/removed" \
|
|
860
|
+
|| forget_fail "$scratch" "forget: could not compare memory"
|
|
861
|
+
# Only after a rewrite and a rebuild. A miss (rows 0) changes nothing, so it records nothing.
|
|
862
|
+
memory_dataset_event memory_forget "$repo" "" --set "keys=$keylist" --set "rows=$total" --set "actor=$actor"
|
|
863
|
+
fi
|
|
864
|
+
fi
|
|
865
|
+
|
|
866
|
+
forget_report "$as_json" "$dry" "$scratch/counts" "$scratch/removed" || status=$?
|
|
867
|
+
rm -rf "$scratch"
|
|
868
|
+
return "$status"
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
# --- add / edit ---------------------------------------------------------------
|
|
872
|
+
# A person writes one ledger row (role person, run id manual-<epoch>) and rebuild runs.
|
|
873
|
+
# Column 12 is paths, comma-joined, empty when there are none. Rows of 11 columns still
|
|
874
|
+
# read as before; only edit reads column 12. Refusals die before either file is written.
|
|
875
|
+
|
|
876
|
+
# Text is one line, 1..300 characters, with no tab. Checked before sanitize, which is what
|
|
877
|
+
# do_record runs next. Dies with one forge: line.
|
|
878
|
+
refuse_memory_text() { # refuse_memory_text <cmd> <text>
|
|
879
|
+
local cmd="$1" text="$2" n cleaned
|
|
880
|
+
case "$text" in
|
|
881
|
+
*$'\n'*|*$'\t'*) die "$cmd: text must be one line without a tab" ;;
|
|
882
|
+
esac
|
|
883
|
+
n=${#text}
|
|
884
|
+
if [ "$n" -lt 1 ] || [ "$n" -gt 300 ]; then
|
|
885
|
+
die "$cmd: text must be 1 to 300 characters"
|
|
886
|
+
fi
|
|
887
|
+
cleaned="$(sanitize "$text")"
|
|
888
|
+
[ -n "$cleaned" ] || die "$cmd: text must be 1 to 300 characters"
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
# 0 when a path can sit in column 12 and be parsed back out of a trailing [anchor].
|
|
892
|
+
# anchor_of stops at the first '[' or ']', so a path like app/[id]/page.tsx would be
|
|
893
|
+
# stored and then never go stale. Same rule as check_memory_path, without the message.
|
|
894
|
+
memory_path_ok() { # memory_path_ok <path>
|
|
895
|
+
local p="$1" rest part
|
|
896
|
+
[ -n "$p" ] || return 1
|
|
897
|
+
[ "${#p}" -le 300 ] || return 1
|
|
898
|
+
case "$p" in
|
|
899
|
+
*$'\n'*|*$'\t'*|*,*|*'['*|*']'*|/*) return 1 ;;
|
|
900
|
+
esac
|
|
901
|
+
rest="$p"
|
|
902
|
+
while [ -n "$rest" ]; do
|
|
903
|
+
part="${rest%%/*}"
|
|
904
|
+
if [ "$part" = ".." ]; then return 1; fi
|
|
905
|
+
case "$rest" in
|
|
906
|
+
*/*) rest="${rest#*/}" ;;
|
|
907
|
+
*) rest="" ;;
|
|
908
|
+
esac
|
|
909
|
+
done
|
|
910
|
+
return 0
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
# A path stored in column 12 or used as an anchor: relative, no .. component, at most
|
|
914
|
+
# 300 characters, and none of the characters that would break the column, the line,
|
|
915
|
+
# or anchor_of's bracket match.
|
|
916
|
+
check_memory_path() { # check_memory_path <cmd> <path>
|
|
917
|
+
local cmd="$1" p="$2" rest part
|
|
918
|
+
[ -n "$p" ] || die "$cmd: path is empty"
|
|
919
|
+
[ "${#p}" -le 300 ] || die "$cmd: path is longer than 300 characters"
|
|
920
|
+
case "$p" in
|
|
921
|
+
*$'\n'*|*$'\t'*|*,*) die "$cmd: path must not contain a tab, newline or comma" ;;
|
|
922
|
+
*'['*|*']'*) die "$cmd: path must not contain '[' or ']'" ;;
|
|
923
|
+
/*) die "$cmd: path must be relative" ;;
|
|
924
|
+
esac
|
|
925
|
+
rest="$p"
|
|
926
|
+
while [ -n "$rest" ]; do
|
|
927
|
+
part="${rest%%/*}"
|
|
928
|
+
if [ "$part" = ".." ]; then die "$cmd: path must not contain '..'"; fi
|
|
929
|
+
case "$rest" in
|
|
930
|
+
*/*) rest="${rest#*/}" ;;
|
|
931
|
+
*) rest="" ;;
|
|
932
|
+
esac
|
|
933
|
+
done
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
# First-seen order, at most 20 distinct paths that memory_path_ok accepts. A --path
|
|
937
|
+
# argument is refused before it gets here. An old column-12 entry or anchor that fails
|
|
938
|
+
# the same rule is omitted: it was not typed as --path, and refusing the edit would
|
|
939
|
+
# leave that learning uncorrectable. Returns 1 when more than 20 remain.
|
|
940
|
+
# Sets MEM_PATHS (csv) and MEM_FIRST (the anchor to append, or empty).
|
|
941
|
+
collect_paths() {
|
|
942
|
+
local p f n=0
|
|
943
|
+
MEM_PATHS=""
|
|
944
|
+
MEM_FIRST=""
|
|
945
|
+
[ "$#" -gt 0 ] || return 0
|
|
946
|
+
f="$(mktemp "${TMPDIR:-/tmp}/forge-memory-paths.XXXXXX")" || die "memory: no scratch file"
|
|
947
|
+
: > "$f"
|
|
948
|
+
for p in "$@"; do
|
|
949
|
+
memory_path_ok "$p" || continue
|
|
950
|
+
if grep -F -x -q -- "$p" "$f"; then continue; fi
|
|
951
|
+
n=$((n + 1))
|
|
952
|
+
if [ "$n" -gt 20 ]; then
|
|
953
|
+
rm -f "$f"
|
|
954
|
+
MEM_PATHS=""
|
|
955
|
+
MEM_FIRST=""
|
|
956
|
+
return 1
|
|
957
|
+
fi
|
|
958
|
+
printf '%s\n' "$p" >> "$f"
|
|
959
|
+
[ -n "$MEM_FIRST" ] || MEM_FIRST="$p"
|
|
960
|
+
done
|
|
961
|
+
while IFS= read -r p; do
|
|
962
|
+
[ -n "$p" ] || continue
|
|
963
|
+
if [ -n "$MEM_PATHS" ]; then MEM_PATHS="$MEM_PATHS,$p"; else MEM_PATHS="$p"; fi
|
|
964
|
+
done < "$f"
|
|
965
|
+
rm -f "$f"
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
# Sanitize, append the first path as [anchor] when the text has none, then clamp and
|
|
969
|
+
# key the same way do_record does. Sets MEM_TEXT and MEM_KEY.
|
|
970
|
+
prepare_memory_text() { # prepare_memory_text <cmd> <text> <first-path>
|
|
971
|
+
local cmd="$1" text="$2" first="$3"
|
|
972
|
+
refuse_memory_text "$cmd" "$text"
|
|
973
|
+
text="$(sanitize "$text")"
|
|
974
|
+
if [ -n "$first" ] && [ -z "$(anchor_of "$text")" ]; then
|
|
975
|
+
text="$text [$first]"
|
|
976
|
+
fi
|
|
977
|
+
MEM_TEXT="$(clamp_fact "$text")"
|
|
978
|
+
MEM_KEY="$(norm_key "$(strip_anchor "$MEM_TEXT")")"
|
|
979
|
+
[ -n "$MEM_KEY" ] || die "$cmd: text has nothing to keep"
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
# One person row. The header matches do_record when the file is missing or empty;
|
|
983
|
+
# an existing header is left as it is. The data row always has the 12th column.
|
|
984
|
+
# forget_filter keeps a ledger whose last line has no newline. The append below would
|
|
985
|
+
# glue this row onto that line, so a non-empty ledger is given its missing newline first.
|
|
986
|
+
write_person_row() { # write_person_row <ledger> <category> <paths-csv>
|
|
987
|
+
local led="$1" cat="$2" paths="$3" ts run_id
|
|
988
|
+
ts="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
989
|
+
run_id="manual-$(date +%s)"
|
|
990
|
+
mkdir -p "$(dirname "$led")"
|
|
991
|
+
if [ ! -s "$led" ]; then
|
|
992
|
+
printf '# ts\trun_id\ttask\trole\tmodel\tverdict\tcategory\tkey\ttext\tduration_s\tinjectable\n' > "$led" \
|
|
993
|
+
|| return 1
|
|
994
|
+
else
|
|
995
|
+
ensure_trailing_newline "$led" || return 1
|
|
996
|
+
fi
|
|
997
|
+
printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
|
|
998
|
+
"$ts" "$run_id" "-" "person" "-" "-" "$cat" "$MEM_KEY" "$MEM_TEXT" "0" "1" "$paths" >> "$led"
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
ensure_trailing_newline() {
|
|
1002
|
+
python3 - "$1" <<'PY'
|
|
1003
|
+
import sys
|
|
1004
|
+
path = sys.argv[1]
|
|
1005
|
+
with open(path, 'rb') as handle:
|
|
1006
|
+
data = handle.read()
|
|
1007
|
+
if data and not data.endswith(b'\n'):
|
|
1008
|
+
with open(path, 'ab') as handle:
|
|
1009
|
+
handle.write(b'\n')
|
|
1010
|
+
PY
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
# Paths of one learning, in ledger order: each row's column 12, then that row's anchor.
|
|
1014
|
+
# Prints how many rows matched. The category:key is the form parse_forget_key stores.
|
|
1015
|
+
edit_old_paths() { # edit_old_paths <ledger> <canon> <out>
|
|
1016
|
+
python3 - "$@" <<'PY'
|
|
1017
|
+
import re
|
|
1018
|
+
import sys
|
|
1019
|
+
ledger_path, canon, out_path = sys.argv[1:4]
|
|
1020
|
+
cat, _, key = canon.partition(':')
|
|
1021
|
+
anchor = re.compile(r'\[([^\[\]]+)\]\s*$')
|
|
1022
|
+
found = 0
|
|
1023
|
+
paths = []
|
|
1024
|
+
with open(ledger_path, 'rb') as handle:
|
|
1025
|
+
data = handle.read()
|
|
1026
|
+
for line in data.splitlines():
|
|
1027
|
+
if line.startswith(b'#') or line.count(b'\t') < 8:
|
|
1028
|
+
continue
|
|
1029
|
+
cols = line.split(b'\t')
|
|
1030
|
+
if len(cols) < 9:
|
|
1031
|
+
continue
|
|
1032
|
+
if cols[6].decode('utf-8', 'surrogateescape') != cat:
|
|
1033
|
+
continue
|
|
1034
|
+
if cols[7].decode('utf-8', 'surrogateescape') != key:
|
|
1035
|
+
continue
|
|
1036
|
+
found += 1
|
|
1037
|
+
if len(cols) >= 12 and cols[11]:
|
|
1038
|
+
for part in cols[11].decode('utf-8', 'surrogateescape').split(','):
|
|
1039
|
+
if part:
|
|
1040
|
+
paths.append(part)
|
|
1041
|
+
text = cols[8].decode('utf-8', 'surrogateescape')
|
|
1042
|
+
match = anchor.search(text)
|
|
1043
|
+
if match:
|
|
1044
|
+
paths.append(match.group(1))
|
|
1045
|
+
with open(out_path, 'wb') as handle:
|
|
1046
|
+
for path in paths:
|
|
1047
|
+
handle.write(path.encode('utf-8', 'surrogateescape') + b'\n')
|
|
1048
|
+
sys.stdout.write('%d\n' % found)
|
|
1049
|
+
PY
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
# `- ` lines gained and lost between two rebuilds. forget_removed is the multiset
|
|
1053
|
+
# difference in the first file's order; swapping the files is the lines that were added.
|
|
1054
|
+
memory_line_changes() { # memory_line_changes <before> <after> <added-out> <removed-out>
|
|
1055
|
+
forget_removed "$1" "$2" "$4" || return 1
|
|
1056
|
+
forget_removed "$2" "$1" "$3" || return 1
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
memory_change_report() { # memory_change_report <json 0|1> <added|edited> <category:key> <added> <removed>
|
|
1060
|
+
if [ "$1" = 1 ]; then
|
|
1061
|
+
python3 - "$3" "$4" "$5" <<'PY'
|
|
1062
|
+
import json
|
|
1063
|
+
import sys
|
|
1064
|
+
key, added_path, removed_path = sys.argv[1:4]
|
|
1065
|
+
|
|
1066
|
+
def load(path):
|
|
1067
|
+
try:
|
|
1068
|
+
lines = open(path, encoding='utf-8').read().splitlines()
|
|
1069
|
+
except FileNotFoundError:
|
|
1070
|
+
return []
|
|
1071
|
+
return [line for line in lines if line]
|
|
1072
|
+
|
|
1073
|
+
json.dump({"ok": True, "key": key, "memory_lines_added": load(added_path),
|
|
1074
|
+
"memory_lines_removed": load(removed_path)}, sys.stdout)
|
|
1075
|
+
sys.stdout.write('\n')
|
|
1076
|
+
PY
|
|
1077
|
+
else
|
|
1078
|
+
printf '%s %s\n' "$2" "$3"
|
|
1079
|
+
fi
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
do_add() { # do_add <repo> --category C --text TEXT [--path P]... [--actor ui|cli] [--json]
|
|
1083
|
+
local repo="" cat="" text="" actor="cli" as_json=0 have_text=0 scratch="" led mem status=0 p
|
|
1084
|
+
local -a paths=()
|
|
1085
|
+
while [ $# -gt 0 ]; do
|
|
1086
|
+
case "$1" in
|
|
1087
|
+
-h|--help) memory_usage; exit 0 ;;
|
|
1088
|
+
--json) as_json=1; shift ;;
|
|
1089
|
+
--category)
|
|
1090
|
+
[ $# -ge 2 ] || die "add: --category needs verify, trap or finding"
|
|
1091
|
+
cat="$2"; shift 2
|
|
1092
|
+
case "$cat" in
|
|
1093
|
+
verify|trap|finding) ;;
|
|
1094
|
+
*) die "add: --category must be verify, trap or finding" ;;
|
|
1095
|
+
esac
|
|
1096
|
+
;;
|
|
1097
|
+
--text)
|
|
1098
|
+
[ $# -ge 2 ] || die "add: --text needs a value"
|
|
1099
|
+
text="$2"; have_text=1; shift 2
|
|
1100
|
+
;;
|
|
1101
|
+
--path)
|
|
1102
|
+
[ $# -ge 2 ] || die "add: --path needs a value"
|
|
1103
|
+
paths+=("$2"); shift 2
|
|
1104
|
+
;;
|
|
1105
|
+
--actor)
|
|
1106
|
+
[ $# -ge 2 ] || die "add: --actor needs ui or cli"
|
|
1107
|
+
actor="$2"; shift 2
|
|
1108
|
+
case "$actor" in
|
|
1109
|
+
ui|cli) ;;
|
|
1110
|
+
*) die "add: --actor must be ui or cli" ;;
|
|
1111
|
+
esac
|
|
1112
|
+
;;
|
|
1113
|
+
--) shift; break ;;
|
|
1114
|
+
-*) die "add: unknown option '$1'" ;;
|
|
1115
|
+
*)
|
|
1116
|
+
[ -z "$repo" ] || die "add: unexpected argument '$1'"
|
|
1117
|
+
repo="$1"; shift
|
|
1118
|
+
;;
|
|
1119
|
+
esac
|
|
1120
|
+
done
|
|
1121
|
+
while [ $# -gt 0 ]; do
|
|
1122
|
+
[ -z "$repo" ] || die "add: unexpected argument '$1'"
|
|
1123
|
+
repo="$1"; shift
|
|
1124
|
+
done
|
|
1125
|
+
[ -n "$repo" ] && [ -n "$cat" ] && [ "$have_text" = 1 ] \
|
|
1126
|
+
|| die "add: usage: add <repo> --category verify|trap|finding --text TEXT [--path P]... [--actor ui|cli] [--json]"
|
|
1127
|
+
[ -d "$repo" ] || die "add: '$repo' is not a directory"
|
|
1128
|
+
[ "${#paths[@]}" -le 20 ] || die "add: at most 20 paths"
|
|
1129
|
+
if [ "${#paths[@]}" -gt 0 ]; then
|
|
1130
|
+
for p in "${paths[@]}"; do check_memory_path add "$p"; done
|
|
1131
|
+
collect_paths "${paths[@]}" || die "add: at most 20 paths"
|
|
1132
|
+
else
|
|
1133
|
+
MEM_PATHS=""
|
|
1134
|
+
MEM_FIRST=""
|
|
1135
|
+
fi
|
|
1136
|
+
# Text, paths and the key are settled before any file in the repo is created.
|
|
1137
|
+
prepare_memory_text add "$text" "$MEM_FIRST"
|
|
1138
|
+
scratch="$(mktemp -d "${TMPDIR:-/tmp}/forge-memory-add.XXXXXX")" || die "add: no scratch dir"
|
|
1139
|
+
led="$(ledger_for "$repo")"
|
|
1140
|
+
mem="$(memory_for "$repo")"
|
|
1141
|
+
if [ -f "$led" ]; then
|
|
1142
|
+
rebuild "$repo" "$scratch/before.md" "$led" >/dev/null \
|
|
1143
|
+
|| forget_fail "$scratch" "add: could not preview memory"
|
|
1144
|
+
fi
|
|
1145
|
+
mkdir -p "$(dir_for "$repo")"
|
|
1146
|
+
write_person_row "$led" "$cat" "$MEM_PATHS" \
|
|
1147
|
+
|| forget_fail "$scratch" "add: could not write the ledger"
|
|
1148
|
+
rebuild "$repo" >/dev/null || forget_fail "$scratch" "add: could not rebuild memory"
|
|
1149
|
+
memory_line_changes "$scratch/before.md" "$mem" "$scratch/added" "$scratch/removed" \
|
|
1150
|
+
|| forget_fail "$scratch" "add: could not compare memory"
|
|
1151
|
+
memory_dataset_event memory_add "$repo" "" --set "key=$cat:$MEM_KEY" --set "actor=$actor"
|
|
1152
|
+
memory_change_report "$as_json" added "$cat:$MEM_KEY" "$scratch/added" "$scratch/removed" || status=$?
|
|
1153
|
+
rm -rf "$scratch"
|
|
1154
|
+
return "$status"
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
do_edit() { # do_edit <repo> <category:key> --text TEXT [--path P]... [--actor ui|cli] [--json]
|
|
1158
|
+
local repo="" rawkey="" text="" actor="cli" as_json=0 have_text=0
|
|
1159
|
+
local scratch="" led mem status=0 p count cat total tmp
|
|
1160
|
+
local -a paths=() union=()
|
|
1161
|
+
while [ $# -gt 0 ]; do
|
|
1162
|
+
case "$1" in
|
|
1163
|
+
-h|--help) memory_usage; exit 0 ;;
|
|
1164
|
+
--json) as_json=1; shift ;;
|
|
1165
|
+
--text)
|
|
1166
|
+
[ $# -ge 2 ] || die "edit: --text needs a value"
|
|
1167
|
+
text="$2"; have_text=1; shift 2
|
|
1168
|
+
;;
|
|
1169
|
+
--path)
|
|
1170
|
+
[ $# -ge 2 ] || die "edit: --path needs a value"
|
|
1171
|
+
paths+=("$2"); shift 2
|
|
1172
|
+
;;
|
|
1173
|
+
--actor)
|
|
1174
|
+
[ $# -ge 2 ] || die "edit: --actor needs ui or cli"
|
|
1175
|
+
actor="$2"; shift 2
|
|
1176
|
+
case "$actor" in
|
|
1177
|
+
ui|cli) ;;
|
|
1178
|
+
*) die "edit: --actor must be ui or cli" ;;
|
|
1179
|
+
esac
|
|
1180
|
+
;;
|
|
1181
|
+
--) shift; break ;;
|
|
1182
|
+
-*) die "edit: unknown option '$1'" ;;
|
|
1183
|
+
*)
|
|
1184
|
+
if [ -z "$repo" ]; then repo="$1"
|
|
1185
|
+
elif [ -z "$rawkey" ]; then rawkey="$1"
|
|
1186
|
+
else die "edit: unexpected argument '$1'"
|
|
1187
|
+
fi
|
|
1188
|
+
shift
|
|
1189
|
+
;;
|
|
1190
|
+
esac
|
|
1191
|
+
done
|
|
1192
|
+
while [ $# -gt 0 ]; do
|
|
1193
|
+
if [ -z "$repo" ]; then repo="$1"
|
|
1194
|
+
elif [ -z "$rawkey" ]; then rawkey="$1"
|
|
1195
|
+
else die "edit: unexpected argument '$1'"
|
|
1196
|
+
fi
|
|
1197
|
+
shift
|
|
1198
|
+
done
|
|
1199
|
+
[ -n "$repo" ] && [ -n "$rawkey" ] && [ "$have_text" = 1 ] \
|
|
1200
|
+
|| die "edit: usage: edit <repo> <category:key> --text TEXT [--path P]... [--actor ui|cli] [--json]"
|
|
1201
|
+
[ -d "$repo" ] || die "edit: '$repo' is not a directory"
|
|
1202
|
+
[ "${#paths[@]}" -le 20 ] || die "edit: at most 20 paths"
|
|
1203
|
+
if [ "${#paths[@]}" -gt 0 ]; then
|
|
1204
|
+
for p in "${paths[@]}"; do check_memory_path edit "$p"; done
|
|
1205
|
+
fi
|
|
1206
|
+
# Same parser forget uses, so Trap:Foo_Bar is the ledger's trap:foo bar.
|
|
1207
|
+
parse_forget_key "$rawkey" edit
|
|
1208
|
+
refuse_memory_text edit "$text"
|
|
1209
|
+
led="$(ledger_for "$repo")"
|
|
1210
|
+
[ -f "$led" ] || die "edit: no ledger row for '$FORGET_CANON'"
|
|
1211
|
+
scratch="$(mktemp -d "${TMPDIR:-/tmp}/forge-memory-edit.XXXXXX")" || die "edit: no scratch dir"
|
|
1212
|
+
count="$(edit_old_paths "$led" "$FORGET_CANON" "$scratch/oldpaths")" \
|
|
1213
|
+
|| forget_fail "$scratch" "edit: could not read '$led'"
|
|
1214
|
+
case "$count" in
|
|
1215
|
+
''|*[!0-9]*) forget_fail "$scratch" "edit: could not read '$led'" ;;
|
|
1216
|
+
0) forget_fail "$scratch" "edit: no ledger row for '$FORGET_CANON'" ;;
|
|
1217
|
+
esac
|
|
1218
|
+
if [ -s "$scratch/oldpaths" ]; then
|
|
1219
|
+
while IFS= read -r p; do
|
|
1220
|
+
[ -n "$p" ] || continue
|
|
1221
|
+
union+=("$p")
|
|
1222
|
+
done < "$scratch/oldpaths"
|
|
1223
|
+
fi
|
|
1224
|
+
if [ "${#paths[@]}" -gt 0 ]; then
|
|
1225
|
+
for p in "${paths[@]}"; do union+=("$p"); done
|
|
1226
|
+
fi
|
|
1227
|
+
# Old paths that cannot be an anchor were dropped inside collect_paths. What remains,
|
|
1228
|
+
# including every new --path, is still at most 20. Past that, write nothing.
|
|
1229
|
+
if [ "${#union[@]}" -gt 0 ]; then
|
|
1230
|
+
collect_paths "${union[@]}" || forget_fail "$scratch" "edit: at most 20 paths"
|
|
1231
|
+
else
|
|
1232
|
+
MEM_PATHS=""
|
|
1233
|
+
MEM_FIRST=""
|
|
1234
|
+
fi
|
|
1235
|
+
prepare_memory_text edit "$text" "$MEM_FIRST"
|
|
1236
|
+
# The category is the one in the key. edit has no way to file the text elsewhere.
|
|
1237
|
+
cat="${FORGET_CANON%%:*}"
|
|
1238
|
+
mem="$(memory_for "$repo")"
|
|
1239
|
+
rebuild "$repo" "$scratch/before.md" "$led" >/dev/null \
|
|
1240
|
+
|| forget_fail "$scratch" "edit: could not preview memory"
|
|
1241
|
+
printf '%s\n' "$FORGET_CANON" > "$scratch/keys"
|
|
1242
|
+
total="$(forget_filter "$led" "$scratch/keys" "$scratch/ledger.new" "$scratch/counts")" \
|
|
1243
|
+
|| forget_fail "$scratch" "edit: could not read '$led'"
|
|
1244
|
+
case "$total" in
|
|
1245
|
+
''|*[!0-9]*) forget_fail "$scratch" "edit: could not read '$led'" ;;
|
|
1246
|
+
0) forget_fail "$scratch" "edit: no ledger row for '$FORGET_CANON'" ;;
|
|
1247
|
+
esac
|
|
1248
|
+
# One rewrite of the ledger, then one rebuild of memory.md. The preview above
|
|
1249
|
+
# writes only the scratch copy used to name the lines that changed.
|
|
1250
|
+
ensure_trailing_newline "$scratch/ledger.new" \
|
|
1251
|
+
|| forget_fail "$scratch" "edit: could not write the ledger"
|
|
1252
|
+
write_person_row "$scratch/ledger.new" "$cat" "$MEM_PATHS" \
|
|
1253
|
+
|| forget_fail "$scratch" "edit: could not write the ledger"
|
|
1254
|
+
tmp="$(mktemp "$(dir_for "$repo")/ledger.tsv.tmp.XXXXXX")" \
|
|
1255
|
+
|| forget_fail "$scratch" "edit: could not write the ledger"
|
|
1256
|
+
{ cp -p "$led" "$tmp" && cat "$scratch/ledger.new" > "$tmp"; } \
|
|
1257
|
+
|| { rm -f "$tmp"; forget_fail "$scratch" "edit: could not write the ledger"; }
|
|
1258
|
+
mv -f "$tmp" "$led" \
|
|
1259
|
+
|| { rm -f "$tmp"; forget_fail "$scratch" "edit: could not replace the ledger"; }
|
|
1260
|
+
rebuild "$repo" >/dev/null || forget_fail "$scratch" "edit: could not rebuild memory"
|
|
1261
|
+
memory_line_changes "$scratch/before.md" "$mem" "$scratch/added" "$scratch/removed" \
|
|
1262
|
+
|| forget_fail "$scratch" "edit: could not compare memory"
|
|
1263
|
+
memory_dataset_event memory_edit "$repo" "" --set "key=$cat:$MEM_KEY" --set "actor=$actor"
|
|
1264
|
+
memory_change_report "$as_json" edited "$cat:$MEM_KEY" "$scratch/added" "$scratch/removed" || status=$?
|
|
1265
|
+
rm -rf "$scratch"
|
|
1266
|
+
return "$status"
|
|
1267
|
+
}
|
|
1268
|
+
|
|
586
1269
|
# --- main ---------------------------------------------------------------------
|
|
587
1270
|
memory_usage() {
|
|
588
1271
|
cat <<'USAGE'
|
|
@@ -594,9 +1277,17 @@ usage: forge memory <command> <repo> [...] (scripts/forge-memory.sh)
|
|
|
594
1277
|
prune <repo> re-apply staleness and the cap now, then print what a dwarf is told
|
|
595
1278
|
spend <repo> [--run ID] what forge has cost this repository, from the ledger
|
|
596
1279
|
inject <repo> <role> the block forge prepends to a planner, dwarf or qa prompt
|
|
1280
|
+
forget <repo> <cat:key>... remove those learnings from the ledger and rebuild memory.md
|
|
1281
|
+
(--dry-run, --json, --actor ui|cli); a missing key is not an error
|
|
1282
|
+
add <repo> --category C --text T
|
|
1283
|
+
append one learning (C is verify, trap or finding) and rebuild;
|
|
1284
|
+
a finding is kept at once (--path P, repeatable; --actor ui|cli; --json)
|
|
1285
|
+
edit <repo> <cat:key> --text T
|
|
1286
|
+
replace that learning and rebuild once, keeping its paths
|
|
1287
|
+
(--path P, --actor ui|cli, --json); an unknown key is an error
|
|
597
1288
|
USAGE
|
|
598
1289
|
}
|
|
599
|
-
[ $# -ge 1 ] || die "usage: forge-memory.sh <inject|note|record|show|prune|spend|learnings> [repo] [...]"
|
|
1290
|
+
[ $# -ge 1 ] || die "usage: forge-memory.sh <inject|note|record|show|prune|spend|learnings|forget|add|edit> [repo] [...]"
|
|
600
1291
|
CMD="$1"; shift
|
|
601
1292
|
case "$CMD" in
|
|
602
1293
|
-h|--help|help) memory_usage; exit 0 ;;
|
|
@@ -608,5 +1299,8 @@ case "$CMD" in
|
|
|
608
1299
|
learnings) [ "${2:-}" = --json ] || [ $# -eq 1 ] || die "learnings: usage: learnings <repo> [--json]"; do_learnings "$1" ;;
|
|
609
1300
|
prune) do_prune "${1:?prune needs a repo}" >/dev/null
|
|
610
1301
|
do_inject "${1}" dwarf ;;
|
|
1302
|
+
forget) do_forget "$@" ;;
|
|
1303
|
+
add) do_add "$@" ;;
|
|
1304
|
+
edit) do_edit "$@" ;;
|
|
611
1305
|
*) die "unknown subcommand '$CMD'" ;;
|
|
612
1306
|
esac
|