pantheon-opencode 2.2.0-beta.1 → 2.2.0-beta.2
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/AGENTS.global.md +2 -2
- package/AGENTS.md +1 -1
- package/CHANGELOG.md +38 -11
- package/README.md +13 -9
- package/README.pt-BR.md +13 -9
- package/bin/pantheon-init.mjs +1 -1
- package/commands/pantheon-audit.md +4 -5
- package/commands/pantheon-model.md +1 -2
- package/package.json +2 -2
- package/plugin.json +1 -1
- package/prompts/dynamic/agora-generated.txt +2 -2
- package/prompts/dynamic/orchestrate-generated.txt +4 -4
- package/prompts/sketch.prompt.md +1 -1
- package/pyproject.toml +1 -1
- package/scripts/build-agents-md.mjs +73 -12
- package/scripts/build-plugin.mjs +2 -0
- package/scripts/hooks/format-multi-language.sh +30 -29
- package/scripts/prune.mjs +124 -2
- package/scripts/setup.mjs +1 -1
- package/scripts/themis_heuristic_scan.py +3 -5
- package/skills-lock.json +1 -1
- package/src/agents/aphrodite.md +1 -1
- package/src/agents/athena.md +6 -14
- package/src/agents/demeter.md +1 -1
- package/src/agents/hermes.md +3 -2
- package/src/agents/iris.md +4 -6
- package/src/agents/prometheus.md +3 -7
- package/src/agents/roster.json +13 -8
- package/src/agents/zeus.md +3 -0
- package/src/instructions/agent-return-format.instructions.md +1 -1
- package/src/instructions/zeus-anti-stall.instructions.md +11 -35
- package/src/instructions/zeus-council-synthesis.instructions.md +17 -49
- package/src/instructions/zeus-timeout-retry.instructions.md +12 -80
- package/src/mcp/mcp_codemap_module.py +6 -5
- package/src/mcp/pantheon-mcp-server.mjs +515 -148
- package/src/pantheon/agent-forge-tool.ts +316 -0
- package/src/pantheon/auto-permissions.ts +31 -1
- package/src/pantheon/codemap-tools.ts +525 -0
- package/src/pantheon/deepwork/plan-tracker.ts +63 -2
- package/src/pantheon/execute-after.ts +21 -0
- package/src/pantheon/hook-engine.ts +2 -1
- package/src/pantheon/native-memory-tools.ts +686 -0
- package/src/pantheon/presets.mjs +15 -0
- package/src/pantheon/step-ledger.ts +259 -0
- package/src/pantheon/swe-eval-harness.ts +211 -0
- package/src/pantheon/tools.ts +62 -7
- package/src/plugin/index.js +2980 -9462
- package/src/plugin/index.ts +2 -43
- package/src/plugin.ts +116 -10
- package/src/plugins/tui/dist/tui.js +90 -26
- package/src/plugins/tui/dist/tui.js.map +1 -1
- package/src/plugins/tui/package-lock.json +2 -2
- package/src/plugins/tui/package.json +1 -1
- package/src/plugins/tui/src/index.tsx +1 -1
- package/src/plugins/tui/src/pantheon-settings.tsx +25 -12
- package/src/plugins/tui/src/quota-state.ts +71 -9
- package/src/plugins/tui/src/tui-prefs.ts +8 -0
- package/src/routing.yml +3 -0
- package/src/skills/auto-continue/references/auto-continue-details.md +2 -31
- /package/scripts/{setup-v2-config.mjs → setup-config.mjs} +0 -0
|
@@ -0,0 +1,686 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native In-Process Memory & Persistence Tools for Pantheon OpenCode Plugin.
|
|
3
|
+
*
|
|
4
|
+
* Replaces external stdio MCP servers (`pantheon-memory`, `pantheon-persistence`)
|
|
5
|
+
* with zero-overhead, in-process TypeScript tools backed by `node:sqlite` (with
|
|
6
|
+
* FTS5 BM25 lexical search and TTL expiration) and confined JSON checkpoints/goals.
|
|
7
|
+
*
|
|
8
|
+
* @module pantheon/native-memory-tools
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
existsSync,
|
|
13
|
+
mkdirSync,
|
|
14
|
+
readdirSync,
|
|
15
|
+
readFileSync,
|
|
16
|
+
realpathSync,
|
|
17
|
+
writeFileSync,
|
|
18
|
+
} from 'node:fs'
|
|
19
|
+
import { join, resolve, sep } from 'node:path'
|
|
20
|
+
import { DatabaseSync } from 'node:sqlite'
|
|
21
|
+
import { resolveToolCwd } from './tool-context.ts'
|
|
22
|
+
import type { V2ToolDef, V2ToolResult } from './tools.ts'
|
|
23
|
+
|
|
24
|
+
const SAFE_SEGMENT_RE = /^[a-zA-Z0-9._:-]{1,128}$/
|
|
25
|
+
const MAX_MEMORY_VALUE_BYTES = 65_536
|
|
26
|
+
|
|
27
|
+
function isSafeSegment(value: unknown): value is string {
|
|
28
|
+
return (
|
|
29
|
+
typeof value === 'string' &&
|
|
30
|
+
SAFE_SEGMENT_RE.test(value) &&
|
|
31
|
+
value !== '.' &&
|
|
32
|
+
value !== '..' &&
|
|
33
|
+
!value.includes('..') &&
|
|
34
|
+
!value.includes('/') &&
|
|
35
|
+
!value.includes('\\')
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function resolveConfinedPath(baseDir: string, ...segments: string[]): string {
|
|
40
|
+
mkdirSync(baseDir, { recursive: true })
|
|
41
|
+
const canonicalBase = realpathSync(baseDir)
|
|
42
|
+
const candidate = resolve(canonicalBase, ...segments)
|
|
43
|
+
const prefix = canonicalBase.endsWith(sep) ? canonicalBase : `${canonicalBase}${sep}`
|
|
44
|
+
if (candidate !== canonicalBase && !candidate.startsWith(prefix)) {
|
|
45
|
+
throw new Error('Path traversal denied: target escapes confined directory')
|
|
46
|
+
}
|
|
47
|
+
return candidate
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const dbCache = new Map<string, DatabaseSync>()
|
|
51
|
+
|
|
52
|
+
function getMemoryDb(workspaceDir: string): DatabaseSync {
|
|
53
|
+
const memoryDir = join(workspaceDir, '.pantheon', 'memory')
|
|
54
|
+
mkdirSync(memoryDir, { recursive: true })
|
|
55
|
+
const dbPath = resolveConfinedPath(memoryDir, 'memory.db')
|
|
56
|
+
const cached = dbCache.get(dbPath)
|
|
57
|
+
if (cached) return cached
|
|
58
|
+
|
|
59
|
+
const db = new DatabaseSync(dbPath)
|
|
60
|
+
db.exec(`
|
|
61
|
+
CREATE TABLE IF NOT EXISTS memories (
|
|
62
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
63
|
+
namespace TEXT NOT NULL,
|
|
64
|
+
key TEXT NOT NULL,
|
|
65
|
+
value TEXT NOT NULL,
|
|
66
|
+
metadata TEXT,
|
|
67
|
+
created_at TEXT NOT NULL,
|
|
68
|
+
expires_at TEXT,
|
|
69
|
+
UNIQUE(namespace, key)
|
|
70
|
+
);
|
|
71
|
+
CREATE INDEX IF NOT EXISTS idx_memories_ns_key ON memories(namespace, key);
|
|
72
|
+
`)
|
|
73
|
+
try {
|
|
74
|
+
db.exec(`
|
|
75
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
76
|
+
namespace,
|
|
77
|
+
key,
|
|
78
|
+
value,
|
|
79
|
+
content='memories',
|
|
80
|
+
content_rowid='id',
|
|
81
|
+
tokenize='porter unicode61'
|
|
82
|
+
);
|
|
83
|
+
CREATE TRIGGER IF NOT EXISTS memories_ai AFTER INSERT ON memories BEGIN
|
|
84
|
+
INSERT INTO memories_fts(rowid, namespace, key, value)
|
|
85
|
+
VALUES (new.id, new.namespace, new.key, new.value);
|
|
86
|
+
END;
|
|
87
|
+
CREATE TRIGGER IF NOT EXISTS memories_ad AFTER DELETE ON memories BEGIN
|
|
88
|
+
INSERT INTO memories_fts(memories_fts, rowid, namespace, key, value)
|
|
89
|
+
VALUES ('delete', old.id, old.namespace, old.key, old.value);
|
|
90
|
+
END;
|
|
91
|
+
CREATE TRIGGER IF NOT EXISTS memories_au AFTER UPDATE ON memories BEGIN
|
|
92
|
+
INSERT INTO memories_fts(memories_fts, rowid, namespace, key, value)
|
|
93
|
+
VALUES ('delete', old.id, old.namespace, old.key, old.value);
|
|
94
|
+
INSERT INTO memories_fts(rowid, namespace, key, value)
|
|
95
|
+
VALUES (new.id, new.namespace, new.key, new.value);
|
|
96
|
+
END;
|
|
97
|
+
`)
|
|
98
|
+
} catch {
|
|
99
|
+
/* FTS5 optional fallback */
|
|
100
|
+
}
|
|
101
|
+
dbCache.set(dbPath, db)
|
|
102
|
+
return db
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function jsonResult(payload: unknown): V2ToolResult {
|
|
106
|
+
return { output: JSON.stringify(payload) }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const NOT_EXPIRED_SQL = '(expires_at IS NULL OR expires_at > ?)'
|
|
110
|
+
|
|
111
|
+
export function createNativeMemoryAndPersistenceTools(fallbackDir?: string): V2ToolDef[] {
|
|
112
|
+
return [
|
|
113
|
+
{
|
|
114
|
+
name: 'memory_store',
|
|
115
|
+
description:
|
|
116
|
+
'Store a durable memory entry in .pantheon/memory/memory.db with FTS5 indexing and optional TTL.',
|
|
117
|
+
input: {
|
|
118
|
+
type: 'object',
|
|
119
|
+
additionalProperties: false,
|
|
120
|
+
required: ['namespace', 'key', 'value'],
|
|
121
|
+
properties: {
|
|
122
|
+
namespace: { type: 'string', description: 'Project or domain namespace' },
|
|
123
|
+
key: { type: 'string', description: 'Unique key within the namespace' },
|
|
124
|
+
value: { type: 'string', description: 'Durable fact, ADR, or convention text (<=64KiB)' },
|
|
125
|
+
metadata: { type: 'string', description: 'Optional JSON metadata string' },
|
|
126
|
+
expires_at: { type: 'string', description: 'Optional ISO-8601 expiration timestamp' },
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
output: {},
|
|
130
|
+
execute: async (input, context) => {
|
|
131
|
+
try {
|
|
132
|
+
const ns = input.namespace
|
|
133
|
+
const key = input.key
|
|
134
|
+
const value = input.value
|
|
135
|
+
if (typeof ns !== 'string' || !ns.trim() || typeof key !== 'string' || !key.trim()) {
|
|
136
|
+
return jsonResult({
|
|
137
|
+
ok: false,
|
|
138
|
+
error: 'namespace and key are required non-empty strings',
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
142
|
+
return jsonResult({
|
|
143
|
+
ok: false,
|
|
144
|
+
error: 'value is required and must be a non-empty string',
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
if (Buffer.byteLength(value, 'utf8') > MAX_MEMORY_VALUE_BYTES) {
|
|
148
|
+
return jsonResult({ ok: false, error: 'value exceeds maximum size of 64 KiB' })
|
|
149
|
+
}
|
|
150
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
151
|
+
const db = getMemoryDb(cwd)
|
|
152
|
+
const nowIso = new Date().toISOString()
|
|
153
|
+
db.prepare(
|
|
154
|
+
`INSERT INTO memories (namespace, key, value, metadata, created_at, expires_at)
|
|
155
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
156
|
+
ON CONFLICT(namespace, key) DO UPDATE SET
|
|
157
|
+
value = excluded.value,
|
|
158
|
+
metadata = excluded.metadata,
|
|
159
|
+
created_at = excluded.created_at,
|
|
160
|
+
expires_at = excluded.expires_at`,
|
|
161
|
+
).run(
|
|
162
|
+
ns.trim(),
|
|
163
|
+
key.trim(),
|
|
164
|
+
value,
|
|
165
|
+
typeof input.metadata === 'string' ? input.metadata : null,
|
|
166
|
+
nowIso,
|
|
167
|
+
typeof input.expires_at === 'string' ? input.expires_at : null,
|
|
168
|
+
)
|
|
169
|
+
return jsonResult({ ok: true, status: 'stored', namespace: ns.trim(), key: key.trim() })
|
|
170
|
+
} catch (err) {
|
|
171
|
+
return jsonResult({
|
|
172
|
+
ok: false,
|
|
173
|
+
error: err instanceof Error ? err.message : String(err),
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
name: 'memory_recall',
|
|
180
|
+
description: 'Recall a specific memory entry by exact namespace and key.',
|
|
181
|
+
input: {
|
|
182
|
+
type: 'object',
|
|
183
|
+
additionalProperties: false,
|
|
184
|
+
required: ['namespace', 'key'],
|
|
185
|
+
properties: {
|
|
186
|
+
namespace: { type: 'string', description: 'Memory namespace' },
|
|
187
|
+
key: { type: 'string', description: 'Memory key' },
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
output: {},
|
|
191
|
+
execute: async (input, context) => {
|
|
192
|
+
try {
|
|
193
|
+
const ns = input.namespace
|
|
194
|
+
const key = input.key
|
|
195
|
+
if (typeof ns !== 'string' || !ns.trim() || typeof key !== 'string' || !key.trim()) {
|
|
196
|
+
return jsonResult({ ok: false, error: 'namespace and key are required' })
|
|
197
|
+
}
|
|
198
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
199
|
+
const db = getMemoryDb(cwd)
|
|
200
|
+
const nowIso = new Date().toISOString()
|
|
201
|
+
const row = db
|
|
202
|
+
.prepare(
|
|
203
|
+
`SELECT namespace, key, value, metadata, created_at, expires_at
|
|
204
|
+
FROM memories
|
|
205
|
+
WHERE namespace = ? AND key = ? AND ${NOT_EXPIRED_SQL}`,
|
|
206
|
+
)
|
|
207
|
+
.get(ns.trim(), key.trim(), nowIso)
|
|
208
|
+
return jsonResult(row ?? { ok: false, error: 'not_found' })
|
|
209
|
+
} catch (err) {
|
|
210
|
+
return jsonResult({
|
|
211
|
+
ok: false,
|
|
212
|
+
error: err instanceof Error ? err.message : String(err),
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
name: 'memory_search',
|
|
219
|
+
description: 'Search durable memories using SQLite FTS5 BM25 ranking with lexical fallback.',
|
|
220
|
+
input: {
|
|
221
|
+
type: 'object',
|
|
222
|
+
additionalProperties: false,
|
|
223
|
+
required: ['query'],
|
|
224
|
+
properties: {
|
|
225
|
+
query: { type: 'string', description: 'Search query terms' },
|
|
226
|
+
namespace: { type: 'string', description: 'Optional namespace filter' },
|
|
227
|
+
top_k: { type: 'number', description: 'Maximum results to return (default 10)' },
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
output: {},
|
|
231
|
+
execute: async (input, context) => {
|
|
232
|
+
try {
|
|
233
|
+
const rawQuery = typeof input.query === 'string' ? input.query.trim() : ''
|
|
234
|
+
if (!rawQuery) {
|
|
235
|
+
return jsonResult({ ok: false, error: 'query is required', count: 0, results: [] })
|
|
236
|
+
}
|
|
237
|
+
const limit =
|
|
238
|
+
typeof input.top_k === 'number' && Number.isFinite(input.top_k) && input.top_k > 0
|
|
239
|
+
? Math.min(Math.floor(input.top_k), 100)
|
|
240
|
+
: 10
|
|
241
|
+
const ns =
|
|
242
|
+
typeof input.namespace === 'string' && input.namespace.trim()
|
|
243
|
+
? input.namespace.trim()
|
|
244
|
+
: undefined
|
|
245
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
246
|
+
const db = getMemoryDb(cwd)
|
|
247
|
+
const nowIso = new Date().toISOString()
|
|
248
|
+
|
|
249
|
+
const tokens = rawQuery
|
|
250
|
+
.replace(/[^\p{L}\p{N}_-]+/gu, ' ')
|
|
251
|
+
.trim()
|
|
252
|
+
.split(/\s+/)
|
|
253
|
+
.filter((t) => t.length >= 2)
|
|
254
|
+
|
|
255
|
+
if (tokens.length > 0) {
|
|
256
|
+
try {
|
|
257
|
+
const ftsExpr = tokens.map((t) => `"${t.replace(/"/g, '""')}"*`).join(' OR ')
|
|
258
|
+
let ftsSql = `
|
|
259
|
+
SELECT m.namespace, m.key, m.value, m.metadata, m.created_at, bm25(memories_fts) AS score
|
|
260
|
+
FROM memories_fts
|
|
261
|
+
JOIN memories m ON m.id = memories_fts.rowid
|
|
262
|
+
WHERE memories_fts MATCH ? AND (m.expires_at IS NULL OR m.expires_at > ?)
|
|
263
|
+
`
|
|
264
|
+
const ftsParams: Array<string | number> = [ftsExpr, nowIso]
|
|
265
|
+
if (ns) {
|
|
266
|
+
ftsSql += ' AND m.namespace = ?'
|
|
267
|
+
ftsParams.push(ns)
|
|
268
|
+
}
|
|
269
|
+
ftsSql += ' ORDER BY score LIMIT ?'
|
|
270
|
+
ftsParams.push(limit)
|
|
271
|
+
const rows = db.prepare(ftsSql).all(...ftsParams)
|
|
272
|
+
if (rows.length > 0) {
|
|
273
|
+
return jsonResult({ ok: true, count: rows.length, results: rows })
|
|
274
|
+
}
|
|
275
|
+
} catch {
|
|
276
|
+
/* fallback to LIKE */
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const escaped = rawQuery.replace(/[\\%_]/g, '\\$&')
|
|
281
|
+
let sql = `
|
|
282
|
+
SELECT namespace, key, value, metadata, created_at
|
|
283
|
+
FROM memories
|
|
284
|
+
WHERE (value LIKE ? ESCAPE '\\' OR key LIKE ? ESCAPE '\\')
|
|
285
|
+
AND ${NOT_EXPIRED_SQL}
|
|
286
|
+
`
|
|
287
|
+
const params: Array<string | number> = [`%${escaped}%`, `%${escaped}%`, nowIso]
|
|
288
|
+
if (ns) {
|
|
289
|
+
sql += ' AND namespace = ?'
|
|
290
|
+
params.push(ns)
|
|
291
|
+
}
|
|
292
|
+
sql += ' ORDER BY id DESC LIMIT ?'
|
|
293
|
+
params.push(limit)
|
|
294
|
+
const rows = db.prepare(sql).all(...params)
|
|
295
|
+
return jsonResult({ ok: true, count: rows.length, results: rows })
|
|
296
|
+
} catch (err) {
|
|
297
|
+
return jsonResult({
|
|
298
|
+
ok: false,
|
|
299
|
+
error: err instanceof Error ? err.message : String(err),
|
|
300
|
+
})
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
name: 'memory_forget',
|
|
306
|
+
description: 'Delete a durable memory entry by namespace and key.',
|
|
307
|
+
input: {
|
|
308
|
+
type: 'object',
|
|
309
|
+
additionalProperties: false,
|
|
310
|
+
required: ['namespace', 'key'],
|
|
311
|
+
properties: {
|
|
312
|
+
namespace: { type: 'string', description: 'Memory namespace' },
|
|
313
|
+
key: { type: 'string', description: 'Memory key to delete' },
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
output: {},
|
|
317
|
+
execute: async (input, context) => {
|
|
318
|
+
try {
|
|
319
|
+
const ns = input.namespace
|
|
320
|
+
const key = input.key
|
|
321
|
+
if (typeof ns !== 'string' || !ns.trim() || typeof key !== 'string' || !key.trim()) {
|
|
322
|
+
return jsonResult({ ok: false, error: 'namespace and key are required' })
|
|
323
|
+
}
|
|
324
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
325
|
+
const db = getMemoryDb(cwd)
|
|
326
|
+
const info = db
|
|
327
|
+
.prepare('DELETE FROM memories WHERE namespace = ? AND key = ?')
|
|
328
|
+
.run(ns.trim(), key.trim())
|
|
329
|
+
return jsonResult({
|
|
330
|
+
ok: true,
|
|
331
|
+
deleted: info.changes > 0,
|
|
332
|
+
namespace: ns.trim(),
|
|
333
|
+
key: key.trim(),
|
|
334
|
+
})
|
|
335
|
+
} catch (err) {
|
|
336
|
+
return jsonResult({
|
|
337
|
+
ok: false,
|
|
338
|
+
error: err instanceof Error ? err.message : String(err),
|
|
339
|
+
})
|
|
340
|
+
}
|
|
341
|
+
},
|
|
342
|
+
},
|
|
343
|
+
{
|
|
344
|
+
name: 'memory_list',
|
|
345
|
+
description: 'List active memory keys in a namespace.',
|
|
346
|
+
input: {
|
|
347
|
+
type: 'object',
|
|
348
|
+
additionalProperties: false,
|
|
349
|
+
properties: {
|
|
350
|
+
namespace: { type: 'string', description: 'Optional namespace filter' },
|
|
351
|
+
limit: { type: 'number', description: 'Maximum entries to list (default 50)' },
|
|
352
|
+
},
|
|
353
|
+
},
|
|
354
|
+
output: {},
|
|
355
|
+
execute: async (input, context) => {
|
|
356
|
+
try {
|
|
357
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
358
|
+
const db = getMemoryDb(cwd)
|
|
359
|
+
const nowIso = new Date().toISOString()
|
|
360
|
+
const limit =
|
|
361
|
+
typeof input.limit === 'number' && Number.isFinite(input.limit) && input.limit > 0
|
|
362
|
+
? Math.min(Math.floor(input.limit), 200)
|
|
363
|
+
: 50
|
|
364
|
+
let sql = `SELECT namespace, key, created_at FROM memories WHERE ${NOT_EXPIRED_SQL}`
|
|
365
|
+
const params: Array<string | number> = [nowIso]
|
|
366
|
+
if (typeof input.namespace === 'string' && input.namespace.trim()) {
|
|
367
|
+
sql += ' AND namespace = ?'
|
|
368
|
+
params.push(input.namespace.trim())
|
|
369
|
+
}
|
|
370
|
+
sql += ' ORDER BY id DESC LIMIT ?'
|
|
371
|
+
params.push(limit)
|
|
372
|
+
const rows = db.prepare(sql).all(...params)
|
|
373
|
+
return jsonResult({ ok: true, count: rows.length, entries: rows })
|
|
374
|
+
} catch (err) {
|
|
375
|
+
return jsonResult({
|
|
376
|
+
ok: false,
|
|
377
|
+
error: err instanceof Error ? err.message : String(err),
|
|
378
|
+
})
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
{
|
|
383
|
+
name: 'memory_stats',
|
|
384
|
+
description: 'Get counts of active durable memory entries by namespace.',
|
|
385
|
+
input: {
|
|
386
|
+
type: 'object',
|
|
387
|
+
additionalProperties: false,
|
|
388
|
+
properties: {
|
|
389
|
+
namespace: { type: 'string', description: 'Optional namespace filter' },
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
output: {},
|
|
393
|
+
execute: async (_input, context) => {
|
|
394
|
+
try {
|
|
395
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
396
|
+
const db = getMemoryDb(cwd)
|
|
397
|
+
const nowIso = new Date().toISOString()
|
|
398
|
+
const total = db
|
|
399
|
+
.prepare(`SELECT count(*) as count FROM memories WHERE ${NOT_EXPIRED_SQL}`)
|
|
400
|
+
.get(nowIso) as { count?: number } | undefined
|
|
401
|
+
const byNamespace = db
|
|
402
|
+
.prepare(
|
|
403
|
+
`SELECT namespace, count(*) as count FROM memories WHERE ${NOT_EXPIRED_SQL} GROUP BY namespace`,
|
|
404
|
+
)
|
|
405
|
+
.all(nowIso)
|
|
406
|
+
return jsonResult({ ok: true, total: total?.count ?? 0, byNamespace })
|
|
407
|
+
} catch (err) {
|
|
408
|
+
return jsonResult({
|
|
409
|
+
ok: false,
|
|
410
|
+
error: err instanceof Error ? err.message : String(err),
|
|
411
|
+
})
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
name: 'context_save',
|
|
417
|
+
description: 'Save a milestone session checkpoint under .pantheon/persistence.',
|
|
418
|
+
input: {
|
|
419
|
+
type: 'object',
|
|
420
|
+
additionalProperties: false,
|
|
421
|
+
required: ['session_id', 'content'],
|
|
422
|
+
properties: {
|
|
423
|
+
session_id: { type: 'string', description: 'Session identifier' },
|
|
424
|
+
content: { type: 'string', description: 'Serialized JSON state object' },
|
|
425
|
+
namespace: { type: 'string', description: 'Optional checkpoint namespace' },
|
|
426
|
+
},
|
|
427
|
+
},
|
|
428
|
+
output: {},
|
|
429
|
+
execute: async (input, context) => {
|
|
430
|
+
try {
|
|
431
|
+
const sessionId = input.session_id
|
|
432
|
+
const ns = input.namespace ?? 'checkpoints'
|
|
433
|
+
if (!isSafeSegment(sessionId) || !isSafeSegment(ns)) {
|
|
434
|
+
return jsonResult({
|
|
435
|
+
ok: false,
|
|
436
|
+
error: 'session_id and namespace must be safe identifiers without path traversal',
|
|
437
|
+
})
|
|
438
|
+
}
|
|
439
|
+
if (typeof input.content !== 'string' || !input.content.trim()) {
|
|
440
|
+
return jsonResult({ ok: false, error: 'content must be a non-empty string' })
|
|
441
|
+
}
|
|
442
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
443
|
+
const baseDir = join(cwd, '.pantheon', 'persistence')
|
|
444
|
+
const dir = resolveConfinedPath(baseDir, ns)
|
|
445
|
+
mkdirSync(dir, { recursive: true })
|
|
446
|
+
const filePath = resolveConfinedPath(dir, `${sessionId}.json`)
|
|
447
|
+
const nowIso = new Date().toISOString()
|
|
448
|
+
writeFileSync(
|
|
449
|
+
filePath,
|
|
450
|
+
JSON.stringify(
|
|
451
|
+
{
|
|
452
|
+
session_id: sessionId,
|
|
453
|
+
namespace: ns,
|
|
454
|
+
content: input.content,
|
|
455
|
+
updated_at: nowIso,
|
|
456
|
+
},
|
|
457
|
+
null,
|
|
458
|
+
2,
|
|
459
|
+
),
|
|
460
|
+
'utf8',
|
|
461
|
+
)
|
|
462
|
+
return jsonResult({
|
|
463
|
+
ok: true,
|
|
464
|
+
status: 'saved',
|
|
465
|
+
session_id: sessionId,
|
|
466
|
+
updated_at: nowIso,
|
|
467
|
+
})
|
|
468
|
+
} catch (err) {
|
|
469
|
+
return jsonResult({
|
|
470
|
+
ok: false,
|
|
471
|
+
error: err instanceof Error ? err.message : String(err),
|
|
472
|
+
})
|
|
473
|
+
}
|
|
474
|
+
},
|
|
475
|
+
},
|
|
476
|
+
{
|
|
477
|
+
name: 'context_get',
|
|
478
|
+
description: 'Retrieve a saved session checkpoint by session_id.',
|
|
479
|
+
input: {
|
|
480
|
+
type: 'object',
|
|
481
|
+
additionalProperties: false,
|
|
482
|
+
required: ['session_id'],
|
|
483
|
+
properties: {
|
|
484
|
+
session_id: { type: 'string', description: 'Session identifier' },
|
|
485
|
+
namespace: { type: 'string', description: 'Optional checkpoint namespace' },
|
|
486
|
+
},
|
|
487
|
+
},
|
|
488
|
+
output: {},
|
|
489
|
+
execute: async (input, context) => {
|
|
490
|
+
try {
|
|
491
|
+
const sessionId = input.session_id
|
|
492
|
+
const ns = input.namespace ?? 'checkpoints'
|
|
493
|
+
if (!isSafeSegment(sessionId) || !isSafeSegment(ns)) {
|
|
494
|
+
return jsonResult({
|
|
495
|
+
ok: false,
|
|
496
|
+
error: 'session_id and namespace must be safe identifiers without path traversal',
|
|
497
|
+
})
|
|
498
|
+
}
|
|
499
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
500
|
+
const baseDir = join(cwd, '.pantheon', 'persistence')
|
|
501
|
+
const dir = resolveConfinedPath(baseDir, ns)
|
|
502
|
+
const filePath = resolveConfinedPath(dir, `${sessionId}.json`)
|
|
503
|
+
if (!existsSync(filePath)) {
|
|
504
|
+
return jsonResult({ ok: false, error: 'not_found', session_id: sessionId })
|
|
505
|
+
}
|
|
506
|
+
return jsonResult(JSON.parse(readFileSync(filePath, 'utf8')))
|
|
507
|
+
} catch (err) {
|
|
508
|
+
return jsonResult({
|
|
509
|
+
ok: false,
|
|
510
|
+
error: err instanceof Error ? err.message : String(err),
|
|
511
|
+
})
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
name: 'goal_create',
|
|
517
|
+
description:
|
|
518
|
+
'Create a persistent session goal in .pantheon/goals (read by compaction hooks).',
|
|
519
|
+
input: {
|
|
520
|
+
type: 'object',
|
|
521
|
+
additionalProperties: false,
|
|
522
|
+
required: ['title'],
|
|
523
|
+
properties: {
|
|
524
|
+
id: { type: 'string', description: 'Optional goal identifier' },
|
|
525
|
+
title: { type: 'string', description: 'Concise goal statement' },
|
|
526
|
+
criteria: {
|
|
527
|
+
type: 'array',
|
|
528
|
+
items: { type: 'string' },
|
|
529
|
+
description: 'Completion criteria checklist',
|
|
530
|
+
},
|
|
531
|
+
},
|
|
532
|
+
},
|
|
533
|
+
output: {},
|
|
534
|
+
execute: async (input, context) => {
|
|
535
|
+
try {
|
|
536
|
+
const title = typeof input.title === 'string' ? input.title.trim() : ''
|
|
537
|
+
if (!title) return jsonResult({ ok: false, error: 'title is required' })
|
|
538
|
+
const rawId =
|
|
539
|
+
typeof input.id === 'string' && input.id.trim() ? input.id.trim() : `goal-${Date.now()}`
|
|
540
|
+
if (!isSafeSegment(rawId)) {
|
|
541
|
+
return jsonResult({ ok: false, error: 'id must be a safe identifier' })
|
|
542
|
+
}
|
|
543
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
544
|
+
const goalsDir = join(cwd, '.pantheon', 'goals')
|
|
545
|
+
const filePath = resolveConfinedPath(goalsDir, `${rawId}.json`)
|
|
546
|
+
const nowIso = new Date().toISOString()
|
|
547
|
+
const goal = {
|
|
548
|
+
id: rawId,
|
|
549
|
+
title,
|
|
550
|
+
criteria: Array.isArray(input.criteria) ? input.criteria.map(String) : [],
|
|
551
|
+
status: 'active',
|
|
552
|
+
created_at: nowIso,
|
|
553
|
+
updated_at: nowIso,
|
|
554
|
+
}
|
|
555
|
+
writeFileSync(filePath, JSON.stringify(goal, null, 2), 'utf8')
|
|
556
|
+
return jsonResult({ ok: true, goal })
|
|
557
|
+
} catch (err) {
|
|
558
|
+
return jsonResult({
|
|
559
|
+
ok: false,
|
|
560
|
+
error: err instanceof Error ? err.message : String(err),
|
|
561
|
+
})
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
},
|
|
565
|
+
{
|
|
566
|
+
name: 'goal_get',
|
|
567
|
+
description: 'Retrieve an active session goal by id or latest.',
|
|
568
|
+
input: {
|
|
569
|
+
type: 'object',
|
|
570
|
+
additionalProperties: false,
|
|
571
|
+
properties: {
|
|
572
|
+
id: { type: 'string', description: 'Optional goal id (defaults to latest)' },
|
|
573
|
+
},
|
|
574
|
+
},
|
|
575
|
+
output: {},
|
|
576
|
+
execute: async (input, context) => {
|
|
577
|
+
try {
|
|
578
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
579
|
+
const goalsDir = join(cwd, '.pantheon', 'goals')
|
|
580
|
+
mkdirSync(goalsDir, { recursive: true })
|
|
581
|
+
if (typeof input.id === 'string' && input.id.trim()) {
|
|
582
|
+
const id = input.id.trim()
|
|
583
|
+
if (!isSafeSegment(id)) {
|
|
584
|
+
return jsonResult({ ok: false, error: 'id must be a safe identifier' })
|
|
585
|
+
}
|
|
586
|
+
const filePath = resolveConfinedPath(goalsDir, `${id}.json`)
|
|
587
|
+
if (!existsSync(filePath)) return jsonResult({ ok: false, error: 'not_found', id })
|
|
588
|
+
return jsonResult(JSON.parse(readFileSync(filePath, 'utf8')))
|
|
589
|
+
}
|
|
590
|
+
const files = readdirSync(goalsDir)
|
|
591
|
+
.filter((f) => f.endsWith('.json'))
|
|
592
|
+
.sort()
|
|
593
|
+
const latest = files.at(-1)
|
|
594
|
+
if (!latest) return jsonResult({ ok: false, error: 'no_active_goals' })
|
|
595
|
+
return jsonResult(JSON.parse(readFileSync(resolveConfinedPath(goalsDir, latest), 'utf8')))
|
|
596
|
+
} catch (err) {
|
|
597
|
+
return jsonResult({
|
|
598
|
+
ok: false,
|
|
599
|
+
error: err instanceof Error ? err.message : String(err),
|
|
600
|
+
})
|
|
601
|
+
}
|
|
602
|
+
},
|
|
603
|
+
},
|
|
604
|
+
{
|
|
605
|
+
name: 'goal_update',
|
|
606
|
+
description: 'Update progress or status on an active session goal.',
|
|
607
|
+
input: {
|
|
608
|
+
type: 'object',
|
|
609
|
+
additionalProperties: false,
|
|
610
|
+
required: ['id'],
|
|
611
|
+
properties: {
|
|
612
|
+
id: { type: 'string', description: 'Goal identifier' },
|
|
613
|
+
status: { type: 'string', description: 'Updated status (active, paused, completed)' },
|
|
614
|
+
notes: { type: 'string', description: 'Progress notes' },
|
|
615
|
+
},
|
|
616
|
+
},
|
|
617
|
+
output: {},
|
|
618
|
+
execute: async (input, context) => {
|
|
619
|
+
try {
|
|
620
|
+
const id = typeof input.id === 'string' ? input.id.trim() : ''
|
|
621
|
+
if (!isSafeSegment(id)) {
|
|
622
|
+
return jsonResult({ ok: false, error: 'valid goal id is required' })
|
|
623
|
+
}
|
|
624
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
625
|
+
const goalsDir = join(cwd, '.pantheon', 'goals')
|
|
626
|
+
const filePath = resolveConfinedPath(goalsDir, `${id}.json`)
|
|
627
|
+
if (!existsSync(filePath)) return jsonResult({ ok: false, error: 'not_found', id })
|
|
628
|
+
const existing = JSON.parse(readFileSync(filePath, 'utf8')) as Record<string, unknown>
|
|
629
|
+
const updated = {
|
|
630
|
+
...existing,
|
|
631
|
+
status: typeof input.status === 'string' ? input.status : existing.status,
|
|
632
|
+
...(typeof input.notes === 'string' ? { notes: input.notes } : {}),
|
|
633
|
+
updated_at: new Date().toISOString(),
|
|
634
|
+
}
|
|
635
|
+
writeFileSync(filePath, JSON.stringify(updated, null, 2), 'utf8')
|
|
636
|
+
return jsonResult({ ok: true, goal: updated })
|
|
637
|
+
} catch (err) {
|
|
638
|
+
return jsonResult({
|
|
639
|
+
ok: false,
|
|
640
|
+
error: err instanceof Error ? err.message : String(err),
|
|
641
|
+
})
|
|
642
|
+
}
|
|
643
|
+
},
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
name: 'goal_done',
|
|
647
|
+
description: 'Mark a session goal as completed.',
|
|
648
|
+
input: {
|
|
649
|
+
type: 'object',
|
|
650
|
+
additionalProperties: false,
|
|
651
|
+
required: ['id'],
|
|
652
|
+
properties: {
|
|
653
|
+
id: { type: 'string', description: 'Goal identifier' },
|
|
654
|
+
summary: { type: 'string', description: 'Completion summary' },
|
|
655
|
+
},
|
|
656
|
+
},
|
|
657
|
+
output: {},
|
|
658
|
+
execute: async (input, context) => {
|
|
659
|
+
try {
|
|
660
|
+
const id = typeof input.id === 'string' ? input.id.trim() : ''
|
|
661
|
+
if (!isSafeSegment(id)) {
|
|
662
|
+
return jsonResult({ ok: false, error: 'valid goal id is required' })
|
|
663
|
+
}
|
|
664
|
+
const cwd = resolveToolCwd(context, fallbackDir)
|
|
665
|
+
const goalsDir = join(cwd, '.pantheon', 'goals')
|
|
666
|
+
const filePath = resolveConfinedPath(goalsDir, `${id}.json`)
|
|
667
|
+
if (!existsSync(filePath)) return jsonResult({ ok: false, error: 'not_found', id })
|
|
668
|
+
const existing = JSON.parse(readFileSync(filePath, 'utf8')) as Record<string, unknown>
|
|
669
|
+
const updated = {
|
|
670
|
+
...existing,
|
|
671
|
+
status: 'completed',
|
|
672
|
+
...(typeof input.summary === 'string' ? { summary: input.summary } : {}),
|
|
673
|
+
updated_at: new Date().toISOString(),
|
|
674
|
+
}
|
|
675
|
+
writeFileSync(filePath, JSON.stringify(updated, null, 2), 'utf8')
|
|
676
|
+
return jsonResult({ ok: true, goal: updated })
|
|
677
|
+
} catch (err) {
|
|
678
|
+
return jsonResult({
|
|
679
|
+
ok: false,
|
|
680
|
+
error: err instanceof Error ? err.message : String(err),
|
|
681
|
+
})
|
|
682
|
+
}
|
|
683
|
+
},
|
|
684
|
+
},
|
|
685
|
+
]
|
|
686
|
+
}
|