@yemi33/minions 0.1.2154 → 0.1.2155

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.
@@ -0,0 +1,31 @@
1
+ // engine/db/migrations/013-backfill-broken-note-links.js
2
+ //
3
+ // One-time data repair (not a schema change). Before the work-item rewriter
4
+ // covered completion-report artifact links (W-mq1j85cj00055a8f, extended to
5
+ // _artifacts.notes[] / artifacts[]), any inbox note the engine auto-archived
6
+ // left the work item's note link pointing at the gone `notes/inbox/<name>` path
7
+ // — a dangling pill. This migration runs the backfill exactly once on the next
8
+ // engine start: it scans existing work_items, finds links whose inbox file is
9
+ // missing, discovers where the note went (notes/archive/ or knowledge/<cat>/),
10
+ // and repoints them. Best-effort and self-isolating — wrapped so a failure can
11
+ // never wedge boot or roll back the migration batch.
12
+
13
+ module.exports = {
14
+ version: 13,
15
+ description: 'one-time backfill: repoint broken inbox note links in existing work items',
16
+ up(db) {
17
+ try {
18
+ const { backfillBrokenNoteLinks } = require('../../note-link-backfill');
19
+ const res = backfillBrokenNoteLinks(db);
20
+ if (res && res.fixedLinks > 0) {
21
+ // eslint-disable-next-line no-console -- migrate.js logs progress to stdout
22
+ console.log(`[db-migrate] v13: repointed ${res.fixedLinks} broken note link(s) across ${res.fixedItems} work item(s) (${res.scanned} scanned)`);
23
+ }
24
+ } catch (e) {
25
+ // One-time cosmetic repair — never block boot. Record the version anyway
26
+ // so it doesn't retry forever against the same un-repairable state.
27
+ // eslint-disable-next-line no-console -- surface the skip without throwing
28
+ console.warn(`[db-migrate] v13: note-link backfill skipped: ${e && e.message ? e.message : e}`);
29
+ }
30
+ },
31
+ };
@@ -0,0 +1,202 @@
1
+ // engine/note-link-backfill.js
2
+ //
3
+ // One-time repair for work-item note links that broke BEFORE the live
4
+ // rewrite landed (W-mq1j85cj00055a8f extended to _artifacts.notes / artifacts).
5
+ //
6
+ // Before the rewriter covered completion-report artifact links, any inbox note
7
+ // that the engine auto-archived (or that was promoted to KB) left the work
8
+ // item's link pointing at the now-gone `notes/inbox/<name>` path — a dangling
9
+ // pill in the dashboard. This module scans existing work items, finds links
10
+ // whose inbox file is GONE, discovers where the note actually went (the flat
11
+ // `notes/archive/` dir or a `knowledge/<cat>/` dir), and repoints the link —
12
+ // re-encoding the `_artifacts.notes[].file` token to the renderer's
13
+ // `archive:<base>` / `kb:<cat>/<file>` form so the pill resolves again.
14
+ //
15
+ // It only ever touches links that are PROVABLY broken (inbox file missing) AND
16
+ // resolvable (a matching archive/KB file exists). Valid links, already-relocated
17
+ // tokens, and notes with no discoverable destination (e.g. merged into the flat
18
+ // notes.md) are left untouched. Idempotent: a second pass finds nothing because
19
+ // the repaired links no longer reference `notes/inbox/`.
20
+ //
21
+ // Wired as a one-time SQL migration (db/migrations/013-*) so it runs exactly
22
+ // once on the next engine start after an update. Updates the canonical SQL
23
+ // `work_items` rows only; the JSON mirror self-heals on the next mutateWorkItems
24
+ // write and SQL is what readers serve (work-items-store), so rewriting the
25
+ // passive mirror here would only risk dropping rows for no benefit.
26
+
27
+ 'use strict';
28
+
29
+ const fs = require('fs');
30
+ const path = require('path');
31
+
32
+ const INBOX_REF_RE = /(?:^|\/)notes\/inbox\/([^/?#]+)/;
33
+
34
+ // Extract the inbox basename from a `…/notes/inbox/<name>` string (tolerating a
35
+ // trailing ?query / #fragment), or null if the string isn't an inbox ref.
36
+ function inboxNameFromRef(str) {
37
+ if (typeof str !== 'string') return null;
38
+ const m = str.match(INBOX_REF_RE);
39
+ return m ? m[1] : null;
40
+ }
41
+
42
+ // Build a resolver: inbox basename → { newLocation, fileToken } | null.
43
+ // - null when the note is STILL in the inbox (link isn't broken), or when no
44
+ // destination can be discovered (note merged into notes.md, or deleted).
45
+ // - newLocation is the MINIONS_DIR-relative path; fileToken is the prefixed
46
+ // basename the work-item renderer resolves for note pills.
47
+ // The archive dir is flat: consolidation writes `${YYYY-MM-DD}-${name}` (with an
48
+ // optional uniquePath `-N` collision suffix). We index each archived file by the
49
+ // basename it derived from (date prefix stripped) and by its literal name.
50
+ function buildNoteResolver(dirs) {
51
+ const inboxDir = dirs.inboxDir;
52
+ const archiveDir = dirs.archiveDir;
53
+ const knowledgeDir = dirs.knowledgeDir;
54
+ const kbCategories = Array.isArray(dirs.kbCategories) ? dirs.kbCategories : [];
55
+ // Reuse the live rewriter's encoder so the backfill and the live path emit
56
+ // byte-identical pill tokens (one source for the archive:/kb: grammar).
57
+ const tokenFor = require('./shared')._artifactNoteFileToken;
58
+
59
+ const archiveByBase = new Map();
60
+ let archiveFiles = [];
61
+ try { archiveFiles = fs.readdirSync(archiveDir); } catch { archiveFiles = []; }
62
+ for (const f of archiveFiles) {
63
+ const origin = f.replace(/^\d{4}-\d{2}-\d{2}-/, '');
64
+ for (const key of new Set([origin, f])) {
65
+ const prev = archiveByBase.get(key);
66
+ // Lexicographic max keeps the newest date-prefixed file for a given note.
67
+ if (!prev || f > prev) archiveByBase.set(key, f);
68
+ }
69
+ }
70
+
71
+ const kbByBase = new Map();
72
+ for (const cat of kbCategories) {
73
+ let files = [];
74
+ try { files = fs.readdirSync(path.join(knowledgeDir, cat)); } catch { files = []; }
75
+ for (const f of files) if (!kbByBase.has(f)) kbByBase.set(f, cat + '/' + f);
76
+ }
77
+
78
+ return function resolve(baseName) {
79
+ if (!baseName) return null;
80
+ try { if (fs.existsSync(path.join(inboxDir, baseName))) return null; } catch { /* treat as gone */ }
81
+ const arch = archiveByBase.get(baseName);
82
+ if (arch) { const nl = 'notes/archive/' + arch; return { newLocation: nl, fileToken: tokenFor(nl) }; }
83
+ const kb = kbByBase.get(baseName);
84
+ if (kb) { const nl = 'knowledge/' + kb; return { newLocation: nl, fileToken: tokenFor(nl) }; }
85
+ return null;
86
+ };
87
+ }
88
+
89
+ // Repoint every broken inbox note link on one work item in place. `resolve` is a
90
+ // buildNoteResolver() result (returns null for non-broken / unresolvable). Mirrors
91
+ // the link shapes shared.rewriteInboxRefsAcrossProjects handles: references[],
92
+ // _artifacts.notes[] (string + object), and artifacts[].path. Returns the count
93
+ // of links repointed.
94
+ function repointItemNoteLinks(item, resolve) {
95
+ if (!item || typeof item !== 'object') return 0;
96
+ let fixed = 0;
97
+
98
+ if (Array.isArray(item.references)) {
99
+ for (let i = 0; i < item.references.length; i++) {
100
+ const ref = item.references[i];
101
+ if (typeof ref === 'string') {
102
+ const name = inboxNameFromRef(ref);
103
+ if (name) { const d = resolve(name); if (d) { item.references[i] = d.newLocation; fixed++; } }
104
+ } else if (ref && typeof ref === 'object') {
105
+ for (const key of ['url', 'path', 'href']) {
106
+ const name = inboxNameFromRef(ref[key]);
107
+ if (name) { const d = resolve(name); if (d) { ref[key] = d.newLocation; fixed++; break; } }
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ const notes = item._artifacts && item._artifacts.notes;
114
+ if (Array.isArray(notes)) {
115
+ for (let i = 0; i < notes.length; i++) {
116
+ const n = notes[i];
117
+ if (typeof n === 'string') {
118
+ // Bare basename = an inbox pill. Skip tokens already relocated.
119
+ if (n.startsWith('archive:') || n.startsWith('kb:')) continue;
120
+ const d = resolve(n);
121
+ if (d && d.fileToken) { notes[i] = d.fileToken; fixed++; }
122
+ } else if (n && typeof n === 'object') {
123
+ let name = inboxNameFromRef(n.path);
124
+ if (!name && typeof n.file === 'string'
125
+ && !n.file.startsWith('archive:') && !n.file.startsWith('kb:')) {
126
+ name = n.file;
127
+ }
128
+ if (name) {
129
+ const d = resolve(name);
130
+ if (d) {
131
+ if (typeof n.path === 'string') n.path = d.newLocation;
132
+ if (d.fileToken && typeof n.file === 'string') n.file = d.fileToken;
133
+ fixed++;
134
+ }
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ if (Array.isArray(item.artifacts)) {
141
+ for (const a of item.artifacts) {
142
+ if (a && typeof a === 'object') {
143
+ const name = inboxNameFromRef(a.path);
144
+ if (name) { const d = resolve(name); if (d) { a.path = d.newLocation; fixed++; } }
145
+ }
146
+ }
147
+ }
148
+
149
+ return fixed;
150
+ }
151
+
152
+ // Scan the canonical SQL `work_items` table and repair broken note links in
153
+ // place. db is a node:sqlite DatabaseSync (the migration's connection). Per-row
154
+ // try/catch so one corrupt row can't abort the pass. Returns a summary.
155
+ function backfillBrokenNoteLinks(db, opts = {}) {
156
+ const shared = require('./shared');
157
+ const minionsDir = opts.minionsDir || shared.MINIONS_DIR;
158
+ const resolve = buildNoteResolver({
159
+ inboxDir: path.join(minionsDir, 'notes', 'inbox'),
160
+ archiveDir: path.join(minionsDir, 'notes', 'archive'),
161
+ knowledgeDir: path.join(minionsDir, 'knowledge'),
162
+ kbCategories: shared.KB_CATEGORIES,
163
+ });
164
+
165
+ let scopes;
166
+ try {
167
+ scopes = db.prepare('SELECT DISTINCT scope FROM work_items').all().map(r => r.scope);
168
+ } catch {
169
+ return { scanned: 0, fixedItems: 0, fixedLinks: 0 };
170
+ }
171
+
172
+ const now = opts.now || Date.now();
173
+ const upd = db.prepare('UPDATE work_items SET data = ?, updated_at = ? WHERE scope = ? AND id = ?');
174
+ let scanned = 0, fixedItems = 0, fixedLinks = 0;
175
+
176
+ for (const scope of scopes) {
177
+ let rows;
178
+ try { rows = db.prepare('SELECT id, data FROM work_items WHERE scope = ?').all(scope); } catch { continue; }
179
+ for (const row of rows) {
180
+ let item;
181
+ try { item = JSON.parse(row.data); } catch { continue; }
182
+ scanned++;
183
+ try {
184
+ const n = repointItemNoteLinks(item, resolve);
185
+ if (n > 0) {
186
+ upd.run(JSON.stringify(item), now, scope, item.id);
187
+ fixedItems++;
188
+ fixedLinks += n;
189
+ }
190
+ } catch { /* skip unfixable row */ }
191
+ }
192
+ }
193
+
194
+ return { scanned, fixedItems, fixedLinks };
195
+ }
196
+
197
+ module.exports = {
198
+ inboxNameFromRef,
199
+ buildNoteResolver,
200
+ repointItemNoteLinks,
201
+ backfillBrokenNoteLinks,
202
+ };
package/engine/shared.js CHANGED
@@ -7503,6 +7503,7 @@ module.exports = {
7503
7503
  extractWorkItemPrRef,
7504
7504
  extractStructuredWorkItemPrRef,
7505
7505
  rewriteInboxRefsAcrossProjects,
7506
+ _artifactNoteFileToken, // exported so the one-time note-link backfill shares the token grammar
7506
7507
  getProjectPrScope,
7507
7508
  getPrNumber,
7508
7509
  getPrDisplayId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yemi33/minions",
3
- "version": "0.1.2154",
3
+ "version": "0.1.2155",
4
4
  "description": "Multi-agent AI dev team that runs from ~/.minions/ — five autonomous agents share a single engine, dashboard, and knowledge base",
5
5
  "bin": {
6
6
  "minions": "bin/minions.js"