@theronap/agnoclast-mcp 0.9.308 → 0.9.309
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/page_nudge.mjs +64 -11
- package/package.json +1 -1
package/lib/page_nudge.mjs
CHANGED
|
@@ -8,20 +8,35 @@
|
|
|
8
8
|
//
|
|
9
9
|
// Self-contained (Node built-ins only): setup copies it to ~/.cortex/hooks/page-nudge.mjs and the hook runs it with
|
|
10
10
|
// plain `node`, since it fires on every Bash and edit call.
|
|
11
|
-
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'
|
|
11
|
+
import { readFileSync, writeFileSync, mkdirSync, openSync, readSync, fstatSync, closeSync } from 'node:fs'
|
|
12
12
|
import { homedir } from 'node:os'
|
|
13
13
|
import { join, dirname, basename } from 'node:path'
|
|
14
14
|
import { createHash } from 'node:crypto'
|
|
15
15
|
|
|
16
16
|
export const STRETCH = 40
|
|
17
|
+
const TAIL_BYTES = 2_000_000
|
|
18
|
+
const NAMED = 3
|
|
17
19
|
|
|
20
|
+
// Each must start a command (line start, or after ; & | ( { or $( ), so a name inside an echo or a test does not count.
|
|
21
|
+
const AT = String.raw`(?:^|[;&|({\n]|\$\()\s*(?:cd\s+\S+\s*&&\s*)?`
|
|
18
22
|
const SHIP = [
|
|
19
|
-
[
|
|
20
|
-
[
|
|
21
|
-
[
|
|
22
|
-
[
|
|
23
|
-
[
|
|
23
|
+
[new RegExp(`${AT}gh\\s+pr\\s+merge\\b`), 'a pull request merged'],
|
|
24
|
+
[new RegExp(`${AT}npm\\s+publish\\b`), 'a package was published'],
|
|
25
|
+
[new RegExp(`${AT}vercel\\b[^\\n;&|]*\\s--prod\\b`), 'a production deploy ran'],
|
|
26
|
+
[new RegExp(`${AT}(?:npx\\s+)?wrangler\\s+(?:deploy|publish)\\b`), 'a deploy ran'],
|
|
27
|
+
[new RegExp(`${AT}fly(?:ctl)?\\s+deploy\\b`), 'a deploy ran'],
|
|
24
28
|
]
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* PURE. A shell command with heredoc bodies and quoted strings blanked, so only what it runs is left. On 2026-11-04
|
|
32
|
+
* a heredoc writing a test that mentioned `gh pr merge` was taken for a merge.
|
|
33
|
+
*/
|
|
34
|
+
export function commandText(cmd) {
|
|
35
|
+
return String(cmd)
|
|
36
|
+
.replace(/<<-?\s*(['"]?)(\w+)\1[^\n]*\n[\s\S]*?\n\s*\2(?=\s|$)/g, ' ')
|
|
37
|
+
.replace(/'[^']*'/g, "''")
|
|
38
|
+
.replace(/"(?:[^"\\]|\\.)*"/g, '""')
|
|
39
|
+
}
|
|
25
40
|
const SPEC_DIR = /\/(?:docs|specs?|plans?|designs?|rfcs?)\/(?:[^/]+\/)*[^/]+\.md$/i
|
|
26
41
|
const NOT_SPEC = /^(?:readme|changelog|claude|agents|license|contributing|security|code_of_conduct)\.md$/i
|
|
27
42
|
const PAGE_WRITE = /__(?:edit_page|author)$/
|
|
@@ -33,7 +48,8 @@ export function nudgeDecision({ tool, input = {}, state = {} }) {
|
|
|
33
48
|
if (!WORK.has(tool)) return null
|
|
34
49
|
if (tool === 'Bash') {
|
|
35
50
|
const cmd = String(input.command ?? '')
|
|
36
|
-
|
|
51
|
+
const run = commandText(cmd)
|
|
52
|
+
if (!/--dry-run\b/.test(run)) for (const [re, what] of SHIP) if (re.test(run)) return { kind: 'ship', key: `ship:${cmd.slice(0, 200)}`, what, cmd }
|
|
37
53
|
} else {
|
|
38
54
|
const path = String(input.file_path ?? '')
|
|
39
55
|
if (SPEC_DIR.test(path) && !NOT_SPEC.test(basename(path)) && !/\/node_modules\//.test(path)) return { kind: 'spec', key: `spec:${path}`, path }
|
|
@@ -42,11 +58,47 @@ export function nudgeDecision({ tool, input = {}, state = {} }) {
|
|
|
42
58
|
return { count: true }
|
|
43
59
|
}
|
|
44
60
|
|
|
61
|
+
/**
|
|
62
|
+
* PURE. The pages a session read or wrote, most recent first, from the tail of its transcript. A nudge that names
|
|
63
|
+
* no page was ignored the first time it fired (2026-11-04); naming the pages makes acting on it one call.
|
|
64
|
+
*/
|
|
65
|
+
export function recentPages(text, limit = NAMED) {
|
|
66
|
+
const seen = []
|
|
67
|
+
for (const line of text.split('\n').reverse()) {
|
|
68
|
+
if (!/__(?:read_page|edit_page|author)"/.test(line)) continue
|
|
69
|
+
let d
|
|
70
|
+
try { d = JSON.parse(line) } catch { continue }
|
|
71
|
+
const blocks = Array.isArray(d.message?.content) ? d.message.content : []
|
|
72
|
+
for (const b of [...blocks].reverse()) {
|
|
73
|
+
const name = b.type === 'tool_use' && /__(?:read_page|edit_page|author)$/.test(b.name) ? b.input?.name : null
|
|
74
|
+
if (typeof name === 'string' && name && !seen.includes(name)) seen.push(name)
|
|
75
|
+
if (seen.length >= limit) return seen
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return seen
|
|
79
|
+
}
|
|
80
|
+
|
|
45
81
|
/** PURE. The line the session reads: facts and options, never orders. */
|
|
46
|
-
export function nudgeLine(d) {
|
|
47
|
-
|
|
82
|
+
export function nudgeLine(d, pages = []) {
|
|
83
|
+
const named = pages.length
|
|
84
|
+
? `Pages this session read or edited: ${pages.map((p) => `"${p}"`).join(', ')}. If the work made one of them stale, edit_page it now, while the details are in front of you.`
|
|
85
|
+
: 'This session has not opened an Agnoclast page; if the work belongs on one, grep for it and bring it current with edit_page.'
|
|
86
|
+
if (d.kind === 'ship') return `Agnoclast note: ${d.what} (\`${d.cmd.trim().slice(0, 80)}\`). ${named}`
|
|
48
87
|
if (d.kind === 'spec') return `Agnoclast note: ${basename(d.path)} looks like a plan or spec, and on disk only this repo can see it. If it is one, it can live on a page (author), or the page it belongs to can link it, so teammates and later sessions find it.`
|
|
49
|
-
return `Agnoclast note: ${d.count} edits and commands since this session last wrote a page.
|
|
88
|
+
return `Agnoclast note: ${d.count} edits and commands since this session last wrote a page. ${named}`
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The last TAIL_BYTES of a file, or '' if it cannot be read. */
|
|
92
|
+
function tail(path) {
|
|
93
|
+
let fd
|
|
94
|
+
try {
|
|
95
|
+
fd = openSync(path, 'r')
|
|
96
|
+
const size = fstatSync(fd).size
|
|
97
|
+
const len = Math.min(size, TAIL_BYTES)
|
|
98
|
+
const buf = Buffer.alloc(len)
|
|
99
|
+
readSync(fd, buf, 0, len, size - len)
|
|
100
|
+
return buf.toString('utf8')
|
|
101
|
+
} catch { return '' } finally { if (fd !== undefined) try { closeSync(fd) } catch {} }
|
|
50
102
|
}
|
|
51
103
|
|
|
52
104
|
const statePath = (home, sessionId) =>
|
|
@@ -69,7 +121,8 @@ export function runPageNudge({ input, home = homedir(), env = process.env }) {
|
|
|
69
121
|
const next = { ...state, sinceWrite: d.kind === 'stretch' ? 0 : sinceWrite, nudged: { ...state.nudged, [d.key]: true } }
|
|
70
122
|
if (d.kind === 'stretch') next.stretches = (state.stretches ?? 0) + 1
|
|
71
123
|
save(file, next)
|
|
72
|
-
|
|
124
|
+
const pages = d.kind === 'spec' || !p.transcript_path ? [] : recentPages(tail(p.transcript_path))
|
|
125
|
+
return { hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: nudgeLine(d, pages) } }
|
|
73
126
|
}
|
|
74
127
|
|
|
75
128
|
function save(file, state) {
|
package/package.json
CHANGED