@theronap/agnoclast-mcp 0.9.296 → 0.9.298
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/lib/closeout.mjs +25 -7
- package/lib/closeout_wiki.mjs +52 -12
- package/package.json +1 -1
package/lib/closeout.mjs
CHANGED
|
@@ -43,8 +43,9 @@ const TAIL_CHARS = 30_000
|
|
|
43
43
|
const CHECK_RAW_CHARS = 110_000
|
|
44
44
|
const MAX_PAGES_PER_SEGMENT = 6
|
|
45
45
|
const MIN_SUMMARY_CHARS = 200
|
|
46
|
-
//
|
|
47
|
-
|
|
46
|
+
// On since the second scored test (ADR-0107): 25 edits on 8 unseen sessions, all true, none damaging. Off, accepted
|
|
47
|
+
// proposals go on the record under "Pages to update" instead of the page.
|
|
48
|
+
const WIKI_WRITES = true
|
|
48
49
|
|
|
49
50
|
const WORK_TOOLS = new Set(['Edit', 'Write', 'NotebookEdit', 'mcp__agnoclast__author', 'mcp__agnoclast__edit_page'])
|
|
50
51
|
const COMMITTED = /\bgit\b[^\n]*\b(commit|push)\b|\bgh pr (create|merge)\b/
|
|
@@ -384,6 +385,9 @@ export function prStates(prs, lastAt, env = process.env) {
|
|
|
384
385
|
})
|
|
385
386
|
}
|
|
386
387
|
|
|
388
|
+
/** PURE. The session's last day in this machine's timezone, which is the person's: a UTC date put an evening merge on the next day. */
|
|
389
|
+
export const localDay = (iso) => (iso ? new Date(iso).toLocaleDateString('en-CA') : undefined)
|
|
390
|
+
|
|
387
391
|
/** The whole digest. Writes nothing; the caller decides. */
|
|
388
392
|
export async function digestSession(text, { brains, brainNames, privateBrain, env = process.env, stopAtLog = false, onCost = () => {} }) {
|
|
389
393
|
let cost = 0
|
|
@@ -414,7 +418,7 @@ ${facts}`, 'sonnet')
|
|
|
414
418
|
if (fixed.includes('SEGMENT') && fixed.length >= 0.9 * record.length) record = fixed
|
|
415
419
|
}
|
|
416
420
|
const checks = checkSegments(parseRecord(record), { brainNames, privateBrain, prs })
|
|
417
|
-
return { ...checks, cost, facts, raw: rawForCheck(read.log), read: { ...read, prs, log: undefined }, chars: read.log.length, chunks: parts.length }
|
|
421
|
+
return { ...checks, cost, facts, raw: rawForCheck(read.log), log: read.log, read: { ...read, prs, log: undefined }, chars: read.log.length, chunks: parts.length }
|
|
418
422
|
}
|
|
419
423
|
|
|
420
424
|
// ⚠ THE WHOLE OF WHAT AN UNATTENDED CLOSE-OUT CAN DO. Nothing here sends, deletes or changes who can see anything.
|
|
@@ -512,21 +516,35 @@ async function writeSegments(tools, sessionId, segments) {
|
|
|
512
516
|
return { ok: !r.isError && r.text.startsWith('Logged') && records.length === segments.length, records, text: r.text }
|
|
513
517
|
}
|
|
514
518
|
|
|
519
|
+
// A run on 2026-11-03 04:39Z found no brains and spent one of the session's two attempts, with the reply thrown away.
|
|
520
|
+
// A server that starts slowly can answer before it lists its tools, so ask again once, and keep what it said.
|
|
521
|
+
async function listBrains(tools, wait = 5_000) {
|
|
522
|
+
let reply = ''
|
|
523
|
+
for (let n = 0; n < 2; n++) {
|
|
524
|
+
if (n) await new Promise((r) => setTimeout(r, wait))
|
|
525
|
+
const r = await tools.call('my_brains', {}).catch((e) => ({ text: String(e?.message ?? e) }))
|
|
526
|
+
reply = r.text ?? ''
|
|
527
|
+
const list = parseBrains(reply)
|
|
528
|
+
if (list.length) return list
|
|
529
|
+
}
|
|
530
|
+
throw new Error(`no brains listed: ${reply.replace(/\s+/g, ' ').slice(0, 200)}`)
|
|
531
|
+
}
|
|
532
|
+
|
|
515
533
|
async function closeSession(session, { env, home, stopAtLog = false, write = true, noWiki = false }) {
|
|
516
534
|
const tools = await openTools(session.id, env, home)
|
|
517
535
|
try {
|
|
518
|
-
const list =
|
|
519
|
-
if (!list.length) throw new Error('no brains listed')
|
|
536
|
+
const list = await listBrains(tools)
|
|
520
537
|
const { guide, names, privateBrain } = brainGuide(list, home)
|
|
521
538
|
const digest = await digestSession(readFileSync(session.path, 'utf8'), { brains: guide, brainNames: names, privateBrain, env, stopAtLog })
|
|
522
539
|
if (digest.blocking.length) return { ok: false, why: digest.blocking.join('; '), cost: digest.cost, digest }
|
|
523
540
|
let cost = digest.cost
|
|
524
541
|
const call = async (prompt, model) => { const r = await llm(prompt, model, undefined, env); cost += r.cost; return r.text }
|
|
525
|
-
const wiki = noWiki ? null : await proposeWikiEdits({ segments: digest.segments, read: digest.read, facts: digest.facts, raw: digest.raw, call, readPage: currentPage(tools), search: pageSearch(tools) })
|
|
542
|
+
const wiki = noWiki ? null : await proposeWikiEdits({ segments: digest.segments, read: digest.read, facts: digest.facts, raw: digest.raw, log: digest.log, call, readPage: currentPage(tools), search: pageSearch(tools), asOf: localDay(digest.read.lastAt) })
|
|
526
543
|
if (!write) return { ok: true, cost, digest, wiki }
|
|
527
544
|
const accepted = wiki?.accepted ?? []
|
|
545
|
+
// Pages first, so the record that follows has them attached: the server attaches what this session wrote.
|
|
546
|
+
const edits = WIKI_WRITES && accepted.length ? await applyWikiEdits(tools, accepted) : []
|
|
528
547
|
const written = await writeSegments(tools, session.id, WIKI_WRITES ? digest.segments : withPageProposals(digest.segments, accepted))
|
|
529
|
-
const edits = written.ok && WIKI_WRITES && accepted.length ? await applyWikiEdits(tools, accepted) : []
|
|
530
548
|
return { ok: written.ok, records: written.records, why: written.ok ? null : written.text.slice(0, 600), cost, digest, wiki, edits }
|
|
531
549
|
} finally {
|
|
532
550
|
await tools.close().catch(() => {})
|
package/lib/closeout_wiki.mjs
CHANGED
|
@@ -6,9 +6,11 @@
|
|
|
6
6
|
// passage can only be replaced by text at least as supported as the session's own log.
|
|
7
7
|
import { scrub } from './closeout.mjs'
|
|
8
8
|
|
|
9
|
-
const MAX_PAGES =
|
|
10
|
-
const SEARCH_HITS =
|
|
11
|
-
const MAX_EDITS =
|
|
9
|
+
const MAX_PAGES = 12
|
|
10
|
+
const SEARCH_HITS = 8
|
|
11
|
+
const MAX_EDITS = 10
|
|
12
|
+
const EVIDENCE_WINDOW = 1_500
|
|
13
|
+
const MAX_EVIDENCE = 60_000
|
|
12
14
|
const MAX_PAGE_CHARS = 24_000
|
|
13
15
|
const MIN_KEPT_RATIO = 0.6
|
|
14
16
|
|
|
@@ -19,6 +21,12 @@ export function wikiCandidates(segments, read, found = []) {
|
|
|
19
21
|
return [...new Set([...named, ...read.pages, ...reads.slice(0, 3), ...found])].slice(0, MAX_PAGES)
|
|
20
22
|
}
|
|
21
23
|
|
|
24
|
+
/** PURE. What to search for pages the session never opened: each segment's title and each workstream heading in it. */
|
|
25
|
+
export function searchQueries(segments) {
|
|
26
|
+
const headings = segments.flatMap((s) => [...s.summary.matchAll(/^\*\*([^*\n]{4,80})\*\*\s*$/gm)].map((m) => m[1].trim()))
|
|
27
|
+
return [...new Set([...segments.map((s) => s.title ?? s.summary.slice(0, 80)), ...headings])]
|
|
28
|
+
}
|
|
29
|
+
|
|
22
30
|
/** PURE. Page names in a `grep` reply, best first. */
|
|
23
31
|
export function parseGrepPages(text) {
|
|
24
32
|
return [...new Set([...text.matchAll(/^- \[page\] (.+?)(?: > .*)? \[\w+\]$/gm)].map((m) => m[1]))]
|
|
@@ -27,7 +35,8 @@ export function parseGrepPages(text) {
|
|
|
27
35
|
/** PURE. The proposer's prompt. */
|
|
28
36
|
export function proposePrompt({ segments, pages, facts, asOf }) {
|
|
29
37
|
return `A work session has just closed. Its record is below, then the current text of wiki pages it touched. Propose the passage edits a careful colleague would make so these pages stop being wrong or behind.
|
|
30
|
-
|
|
38
|
+
Go through every page and ask: after this session, what on it is now false, out of date, or missing a status or decision its reader needs? A page the session wrote, or whose subject the session's work changed, usually needs its status brought current. A page it only read may need nothing. Propose an edit only for something this session established first-hand.
|
|
39
|
+
State a status only as it stood when the session ended: the fact notes run in order and later ones win, so a thing first described as not done and later done is done.
|
|
31
40
|
Prefer correcting the sentence that went stale over adding a new paragraph. Copy the page's style. Date what you state, in the form ${asOf ?? 'YYYY-MM-DD'}. Keep every [[link]] in the passage you replace. No process remarks, no mention of this session or record.
|
|
32
41
|
A page in a shared brain must not gain family, friends, classes, health, private messages or phone numbers.
|
|
33
42
|
Reply with JSON only: an array of at most ${MAX_EDITS} objects {"page": exact page name, "heading": exact section heading without any "· as of" date, "old_string": text copied exactly from that section, appearing once, "new_string": the replacement, "reason": one short phrase}.
|
|
@@ -42,12 +51,42 @@ PAGES:
|
|
|
42
51
|
${pages.map((p) => `=== PAGE: ${p.name} (brain: ${p.brain}) ===\n${p.text.slice(0, MAX_PAGE_CHARS)}`).join('\n\n')}`
|
|
43
52
|
}
|
|
44
53
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
const IDENTIFIER = /#\d{3,5}\b|\b\d{8,14}_\w+|\bv?\d+\.\d+\.\d+(?:-[\w.]+)?\b|`[^`\n]{3,80}`|\b[0-9a-f]{7,40}\b/g
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* PURE. The parts of the WHOLE log that bear on an edit: the last mentions of each identifier its new text names. The
|
|
58
|
+
* check once saw only the start and end of a long log, and passed a migration as unapplied that the middle applied.
|
|
59
|
+
*/
|
|
60
|
+
export function evidenceFor(edit, log) {
|
|
61
|
+
const terms = [...new Set((edit.new_string.match(IDENTIFIER) ?? []).map((t) => t.replace(/`/g, '')))]
|
|
62
|
+
const spans = []
|
|
63
|
+
for (const term of terms) {
|
|
64
|
+
let at = log.length
|
|
65
|
+
for (let n = 0; n < 3; n++) {
|
|
66
|
+
at = log.lastIndexOf(term, at - 1)
|
|
67
|
+
if (at < 0) break
|
|
68
|
+
spans.push([Math.max(0, at - EVIDENCE_WINDOW), Math.min(log.length, at + term.length + EVIDENCE_WINDOW)])
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
spans.sort((a, b) => a[0] - b[0])
|
|
72
|
+
const merged = []
|
|
73
|
+
for (const s of spans) {
|
|
74
|
+
const last = merged[merged.length - 1]
|
|
75
|
+
if (last && s[0] <= last[1]) last[1] = Math.max(last[1], s[1])
|
|
76
|
+
else merged.push([...s])
|
|
77
|
+
}
|
|
78
|
+
return merged.map(([a, b]) => log.slice(a, b)).join('\n[...]\n').slice(-MAX_EVIDENCE)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** PURE. The check of each edit against the session: the fact notes in order, the log's end, and each edit's own evidence. */
|
|
82
|
+
export function verifyPrompt(edits, { raw, facts = '', log = '' }) {
|
|
83
|
+
return `Each edit below changes a wiki page because of a work session. Decide each one from the session's own material below. Keep an edit only if it shows the new text is true as of the END of the session: not later corrected, not rehearsal, not merely advised, and its dates are right. The fact notes run in order and later ones win. Reply with JSON only: an array of the zero-based indexes to keep.
|
|
48
84
|
|
|
49
85
|
EDITS:
|
|
50
|
-
${JSON.stringify(edits.map((e, i) => ({ i, page: e.page, old: e.old_string, new: e.new_string })), null, 1)}
|
|
86
|
+
${JSON.stringify(edits.map((e, i) => ({ i, page: e.page, old: e.old_string, new: e.new_string, evidence: log ? evidenceFor(e, log) : undefined })), null, 1)}
|
|
87
|
+
|
|
88
|
+
FACT NOTES (in order):
|
|
89
|
+
${facts.slice(-60_000)}
|
|
51
90
|
|
|
52
91
|
RAW SESSION LOG:
|
|
53
92
|
${raw}`
|
|
@@ -86,11 +125,12 @@ export function gateEdit(edit, { page, allowedBrains, evidence }) {
|
|
|
86
125
|
}
|
|
87
126
|
|
|
88
127
|
/** Propose, gate, verify. Writes nothing. `readPage(name, brain)` gives {name, brain, tier, version, text} or null. */
|
|
89
|
-
export async function proposeWikiEdits({ segments, read, facts, raw, call, readPage, search = async () => [], asOf }) {
|
|
128
|
+
export async function proposeWikiEdits({ segments, read, facts, raw, log = '', call, readPage, search = async () => [], asOf }) {
|
|
90
129
|
const allowedBrains = segments.map((s) => s.brain)
|
|
91
130
|
const pages = []
|
|
92
|
-
// Manual close-outs also fix pages the session never opened, like a package's status page
|
|
93
|
-
|
|
131
|
+
// Manual close-outs also fix pages the session never opened, like a package's status page: in the two longest
|
|
132
|
+
// sessions of the 2026-11-02 test, 17 of 22 such pages. Searching each workstream finds those.
|
|
133
|
+
const found = [...new Set((await Promise.all(searchQueries(segments).map(search))).flat())].slice(0, SEARCH_HITS)
|
|
94
134
|
for (const name of wikiCandidates(segments, read, found)) {
|
|
95
135
|
const page = await readPage(name, allowedBrains)
|
|
96
136
|
if (page) pages.push(page)
|
|
@@ -106,7 +146,7 @@ export async function proposeWikiEdits({ segments, read, facts, raw, call, readP
|
|
|
106
146
|
else passed.push(edit)
|
|
107
147
|
}
|
|
108
148
|
if (!passed.length) return { proposed, accepted: [], refused }
|
|
109
|
-
const keep = new Set(parseJsonArray(await call(verifyPrompt(passed, raw), 'sonnet')))
|
|
149
|
+
const keep = new Set(parseJsonArray(await call(verifyPrompt(passed, { raw, facts, log }), 'sonnet')))
|
|
110
150
|
passed.forEach((e, i) => { if (!keep.has(i)) refused.push({ ...e, why: 'not supported by the log' }) })
|
|
111
151
|
return { proposed, accepted: passed.filter((_, i) => keep.has(i)).map((e) => { const p = pages.find((x) => x.name === e.page); return { ...e, version: p.version, brain: p.brain } }), refused }
|
|
112
152
|
}
|
package/package.json
CHANGED