@picassohq/cli 1.0.4 → 1.0.5
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/dist/build-info.json +2 -1
- package/dist/index.js +1557 -1353
- package/package.json +1 -1
- package/runtime/dsh/picasso-ecosystem.cordis.patch.yml +4 -7
- package/runtime/dsh/picasso-sdk.patch.yml +47 -0
- package/runtime/engine-manifest.json +20 -10
- package/runtime/packages/picasso-compaction/index.js +257 -5
- package/runtime/packages/picasso-hooks/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-hooks/index.js +302 -0
- package/runtime/packages/picasso-hooks/lifecycle-events.js +651 -0
- package/runtime/packages/picasso-hooks/package.json +29 -0
- package/runtime/packages/picasso-lean-context/glob-walk.js +518 -0
- package/runtime/packages/picasso-lean-context/index.js +136 -9
- package/runtime/packages/picasso-memory/index.js +66 -24
- package/runtime/packages/picasso-model-router/connections.js +164 -7
- package/runtime/packages/picasso-model-router/index.js +97 -1
- package/runtime/packages/picasso-model-router/thinking-default.js +29 -5
- package/runtime/packages/picasso-progress-reports/index.js +128 -18
- package/runtime/packages/picasso-sdk-server/command-runs.js +37 -0
- package/runtime/packages/picasso-sdk-server/ignore-file.js +5 -1
- package/runtime/packages/picasso-sdk-server/index.js +11 -3
- package/runtime/packages/picasso-sdk-server/package.json +1 -0
- package/runtime/packages/picasso-sdk-server/wave2-hooks.js +26 -6
- package/runtime/packages/picasso-x-reader/settings.js +4 -1
- package/runtime/plugins/picasso-auto-approve/index.js +21 -8
- package/runtime/plugins/picasso-compaction/index.js +257 -5
- package/runtime/plugins/picasso-hooks/index.js +302 -0
- package/runtime/plugins/picasso-hooks/lifecycle-events.js +651 -0
- package/runtime/plugins/picasso-hooks/package.json +29 -0
- package/runtime/plugins/picasso-lean-context/glob-walk.js +518 -0
- package/runtime/plugins/picasso-lean-context/index.js +136 -9
- package/runtime/plugins/picasso-memory/index.js +66 -24
- package/runtime/plugins/picasso-model-router/connections.js +164 -7
- package/runtime/plugins/picasso-model-router/index.js +97 -1
- package/runtime/plugins/picasso-model-router/thinking-default.js +29 -5
- package/runtime/plugins/picasso-progress-reports/index.js +128 -18
- package/runtime/plugins/picasso-trackers/index.js +12 -2
- package/runtime/plugins/picasso-x-reader/settings.js +4 -1
- package/runtime/relay/main.mjs +2254 -1648
- package/runtime/relay/versions-routes.mjs +24 -20
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
// @picasso/hooks — picasso→dsh hooks bridge.
|
|
2
|
+
// Reads the picasso hooks store (`<picassoHome>/hooks.json`, flat hook list with
|
|
3
|
+
// { id, event, command, timeoutMs, enabled }) and runs each enabled shell hook
|
|
4
|
+
// at the mapped dsh interception point, fail-open, via node:child_process
|
|
5
|
+
// (host-side execution — the dsh ctx.shell service is for model tools, not
|
|
6
|
+
// host hooks; picasso's own createHookRunner uses execFileAsync the same way).
|
|
7
|
+
//
|
|
8
|
+
// Two families of events:
|
|
9
|
+
//
|
|
10
|
+
// - engine events (`pre-tool`, `turn/end`, `tools/pre-execute`, …;
|
|
11
|
+
// lifecycle-events.js) fire wherever this plugin is mounted — both the
|
|
12
|
+
// CLI's runtime (ECOSYSTEM_PLUGIN_DIRS) and the Mac relay (PICASSO_PLUGINS);
|
|
13
|
+
// - loop events (`pre-plan`, `pre-act`, `post-act`, `on-error`, `*`) are fired
|
|
14
|
+
// on CLI turns by the CLI's run loop (packages/core/src/dsh/dsh-agent-loop.ts
|
|
15
|
+
// + createHookRunner), so the CLI mounts this plugin without loopEvents and
|
|
16
|
+
// it leaves those names alone (no double firing). With
|
|
17
|
+
// `config.loopEvents: true` (the Mac relay's row, apps/desktop-helper/src/dsh-host.ts)
|
|
18
|
+
// this plugin fires them itself with the same semantics, so a hooks.json
|
|
19
|
+
// entry behaves the same on a Mac turn as on a CLI turn: pre-plan once per
|
|
20
|
+
// top-level turn before its first model step (awaited), pre-act / post-act
|
|
21
|
+
// per tool call and result of that turn, on-error when the turn ends in an
|
|
22
|
+
// error; subagent sessions fire nothing (the CLI projects their tools as
|
|
23
|
+
// subagent progress, not as tool calls). Like the CLI, a hook observes only:
|
|
24
|
+
// it never blocks a tool call or changes a decision.
|
|
25
|
+
//
|
|
26
|
+
// The store is re-read when it changes (checked at each turn start), so a hook
|
|
27
|
+
// added with `picasso hooks add` reaches a long-lived engine on its next turn —
|
|
28
|
+
// the CLI reads the file once per run.
|
|
29
|
+
import { execFile } from 'node:child_process'
|
|
30
|
+
import { readFileSync, statSync } from 'node:fs'
|
|
31
|
+
import { userInfo } from 'node:os'
|
|
32
|
+
import { join } from 'node:path'
|
|
33
|
+
import { isLifecycleEvent, lifecycleEvent, resolveHookPoint } from './lifecycle-events.js'
|
|
34
|
+
|
|
35
|
+
export const name = 'picasso-hooks'
|
|
36
|
+
|
|
37
|
+
/** The CLI run loop's hook events (apps/cli/src/hooks-commands.ts PICASSO_LOOP_HOOK_EVENTS). `*` matches every one. */
|
|
38
|
+
export const LOOP_HOOK_EVENTS = ['pre-plan', 'pre-act', 'post-act', 'on-error', '*']
|
|
39
|
+
|
|
40
|
+
/** The hooks.json schema version this bridge reads (packages/shared-home HOOKS_SPEC.current). */
|
|
41
|
+
export const HOOKS_STORE_VERSION = 1
|
|
42
|
+
|
|
43
|
+
const LOOP_EVENT_SET = new Set(LOOP_HOOK_EVENTS)
|
|
44
|
+
const DEFAULT_TIMEOUT_MS = 15_000
|
|
45
|
+
const MAX_BUFFER = 512 * 1024
|
|
46
|
+
|
|
47
|
+
const nonEmpty = (value) => typeof value === 'string' && value.trim() !== ''
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The store this bridge reads: `config.file`, else `PICASSO_HOOKS_ROOT` (a file path), else
|
|
51
|
+
* `<PICASSO_HOME or the OS account's home/.picasso>/hooks.json` — the file `picasso hooks` edits
|
|
52
|
+
* (packages/shared-home `hooksFile`, which resolves the home the same way).
|
|
53
|
+
*/
|
|
54
|
+
export function hooksFilePath(env = process.env, config = {}) {
|
|
55
|
+
if (nonEmpty(config?.file)) return config.file
|
|
56
|
+
if (nonEmpty(env.PICASSO_HOOKS_ROOT)) return env.PICASSO_HOOKS_ROOT
|
|
57
|
+
const home = nonEmpty(env.PICASSO_HOME) ? env.PICASSO_HOME : join(userInfo().homedir, '.picasso')
|
|
58
|
+
return join(home, 'hooks.json')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Enabled, well-formed hooks from the store (id, event and command strings; `enabled !== false`), the same entries
|
|
63
|
+
* the CLI loader accepts. A missing or unreadable file is no hooks; a file written by a newer Picasso is no hooks
|
|
64
|
+
* plus a warning (the CLI refuses it the same way).
|
|
65
|
+
*/
|
|
66
|
+
export function readPicassoHooks(file, warn = () => undefined) {
|
|
67
|
+
let parsed
|
|
68
|
+
try {
|
|
69
|
+
parsed = JSON.parse(readFileSync(file, 'utf8'))
|
|
70
|
+
} catch {
|
|
71
|
+
return []
|
|
72
|
+
}
|
|
73
|
+
if (parsed === null || typeof parsed !== 'object') return []
|
|
74
|
+
if (typeof parsed.version === 'number' && parsed.version > HOOKS_STORE_VERSION) {
|
|
75
|
+
warn(`[picasso-hooks] ${file} is hooks schema ${parsed.version}; this build reads ${HOOKS_STORE_VERSION} — hooks skipped until Picasso is updated`)
|
|
76
|
+
return []
|
|
77
|
+
}
|
|
78
|
+
if (!Array.isArray(parsed.hooks)) return []
|
|
79
|
+
return parsed.hooks.filter(
|
|
80
|
+
(h) =>
|
|
81
|
+
h !== null &&
|
|
82
|
+
typeof h === 'object' &&
|
|
83
|
+
typeof h.id === 'string' &&
|
|
84
|
+
typeof h.command === 'string' &&
|
|
85
|
+
typeof h.event === 'string' &&
|
|
86
|
+
h.enabled !== false,
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function hookShell() {
|
|
91
|
+
return nonEmpty(process.env.SHELL) ? process.env.SHELL : '/bin/sh'
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Run one hook's command; always resolves (fail-open) with whether it exited 0 and what it printed. */
|
|
95
|
+
export function runHookCommand(hook, { event, cwd, env = {} }) {
|
|
96
|
+
return new Promise((resolve) => {
|
|
97
|
+
try {
|
|
98
|
+
execFile(
|
|
99
|
+
hookShell(),
|
|
100
|
+
['-lc', hook.command],
|
|
101
|
+
{
|
|
102
|
+
cwd,
|
|
103
|
+
timeout: typeof hook.timeoutMs === 'number' && hook.timeoutMs > 0 ? hook.timeoutMs : DEFAULT_TIMEOUT_MS,
|
|
104
|
+
maxBuffer: MAX_BUFFER,
|
|
105
|
+
env: { ...process.env, PICASSO_HOOK_EVENT: event, PICASSO_HOOK_ID: hook.id, PICASSO_HOOK_TOOL: '', ...env },
|
|
106
|
+
},
|
|
107
|
+
(error, stdout, stderr) => {
|
|
108
|
+
const output = `${stdout ?? ''}${stderr ?? ''}`.trim()
|
|
109
|
+
resolve(error ? { ok: false, output: output || (error instanceof Error ? error.message : String(error)) } : { ok: true, output })
|
|
110
|
+
},
|
|
111
|
+
)
|
|
112
|
+
} catch (error) {
|
|
113
|
+
// A cwd that no longer exists throws synchronously on some platforms: still fail-open.
|
|
114
|
+
resolve({ ok: false, output: error instanceof Error ? error.message : String(error) })
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A subagent child session (delegated work). Forks are ordinary sessions, so `parentSession` alone does not count. */
|
|
120
|
+
function isSubagentSession(session) {
|
|
121
|
+
const header = session?.header
|
|
122
|
+
if (header === null || typeof header !== 'object') return false
|
|
123
|
+
return header.origin === 'subagent' || (Number.isSafeInteger(header.delegationDepth) && header.delegationDepth > 0)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The session a dispatch is about: a Session itself, or the session of the agent an execution or step carries. */
|
|
127
|
+
function sessionOf(args) {
|
|
128
|
+
const first = args[0]
|
|
129
|
+
if (first?.header !== undefined && typeof first?.id === 'string') return first
|
|
130
|
+
return first?.agent?.session ?? first?.session
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The tool a dispatch names: an execution's `name`, or a session-log record's `data.name`. */
|
|
134
|
+
function toolNameOf(args) {
|
|
135
|
+
const [first, second] = args
|
|
136
|
+
if (typeof first?.name === 'string') return first.name
|
|
137
|
+
if (typeof second?.data?.name === 'string') return second.data.name
|
|
138
|
+
return ''
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function apply(ctx, config = {}) {
|
|
142
|
+
const loopEvents = config?.loopEvents === true
|
|
143
|
+
const file = hooksFilePath(process.env, config)
|
|
144
|
+
const warn = (message) => (typeof ctx.logger?.warn === 'function' ? ctx.logger.warn(message) : console.warn(message))
|
|
145
|
+
const report = (hook, event, result) => {
|
|
146
|
+
const line = `[picasso-hooks] hook ${hook.id} (${event}) ${result.ok ? 'ran' : 'failed'}${result.output ? `: ${result.output.slice(0, 160)}` : ''}`
|
|
147
|
+
if (typeof ctx.logger?.info === 'function') ctx.logger.info(line)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Last-seen working folder per session (`working-directory/change`), else the session header's. */
|
|
151
|
+
const cwdBySession = new WeakMap()
|
|
152
|
+
const cwdOf = (session) => cwdBySession.get(session) ?? (nonEmpty(session?.header?.cwd) ? session.header.cwd : process.cwd())
|
|
153
|
+
|
|
154
|
+
let stamp
|
|
155
|
+
let loopHooks = []
|
|
156
|
+
let engineByPoint = new Map()
|
|
157
|
+
|
|
158
|
+
const run = (hook, event, session, env) =>
|
|
159
|
+
runHookCommand(hook, { event, cwd: cwdOf(session), env }).then((result) => report(hook, event, result))
|
|
160
|
+
|
|
161
|
+
// ── engine events ──────────────────────────────────────────────────────────
|
|
162
|
+
const subscriptions = new Map()
|
|
163
|
+
const engineListener = (point) => (...args) => {
|
|
164
|
+
// Waterfall points hand `next` last: the pipeline decides first and its decision is returned unchanged.
|
|
165
|
+
const next = typeof args[args.length - 1] === 'function' ? args[args.length - 1] : undefined
|
|
166
|
+
const decision = next === undefined ? undefined : next()
|
|
167
|
+
const hooks = engineByPoint.get(point)
|
|
168
|
+
if (hooks !== undefined && hooks.length > 0) {
|
|
169
|
+
const session = sessionOf(args)
|
|
170
|
+
const tool = toolNameOf(args)
|
|
171
|
+
// Off the hot path: the hooks start after the dispatch returns.
|
|
172
|
+
queueMicrotask(() => {
|
|
173
|
+
for (const hook of hooks) void run(hook, hook.event, session, { PICASSO_HOOK_TOOL: tool })
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
return decision
|
|
177
|
+
}
|
|
178
|
+
const syncSubscriptions = () => {
|
|
179
|
+
const wanted = new Set([...engineByPoint.keys()].filter((point) => point !== 'session/event'))
|
|
180
|
+
if (loopEvents && loopHooks.some((hook) => hook.event === 'pre-plan' || hook.event === '*')) wanted.add('loop:pre-plan')
|
|
181
|
+
for (const [key, dispose] of subscriptions) {
|
|
182
|
+
if (!wanted.has(key)) {
|
|
183
|
+
dispose?.()
|
|
184
|
+
subscriptions.delete(key)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
for (const key of wanted) {
|
|
188
|
+
if (subscriptions.has(key)) continue
|
|
189
|
+
subscriptions.set(key, key === 'loop:pre-plan' ? ctx.on('agent/pre-step', prePlanListener) : ctx.on(key, engineListener(key)))
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const refresh = () => {
|
|
194
|
+
let next
|
|
195
|
+
try {
|
|
196
|
+
const stat = statSync(file)
|
|
197
|
+
next = `${stat.mtimeMs}:${stat.size}`
|
|
198
|
+
} catch {
|
|
199
|
+
next = 'missing'
|
|
200
|
+
}
|
|
201
|
+
if (next === stamp) return
|
|
202
|
+
stamp = next
|
|
203
|
+
const loop = []
|
|
204
|
+
const byPoint = new Map()
|
|
205
|
+
for (const hook of readPicassoHooks(file, warn)) {
|
|
206
|
+
if (loopEvents && LOOP_EVENT_SET.has(hook.event)) {
|
|
207
|
+
loop.push(hook)
|
|
208
|
+
continue
|
|
209
|
+
}
|
|
210
|
+
const resolved = resolveHookPoint(hook.event)
|
|
211
|
+
if (resolved === undefined) {
|
|
212
|
+
// Loop names belong to the CLI run loop where this bridge runs without loopEvents.
|
|
213
|
+
if (!LOOP_EVENT_SET.has(hook.event)) {
|
|
214
|
+
warn('[picasso-hooks] no dsh interception point for picasso event ' + hook.event + ' (hook ' + hook.id + '); skipped')
|
|
215
|
+
}
|
|
216
|
+
continue
|
|
217
|
+
}
|
|
218
|
+
const entry = { ...hook, sessionType: resolved.sessionType }
|
|
219
|
+
if (!byPoint.has(resolved.point)) byPoint.set(resolved.point, [])
|
|
220
|
+
byPoint.get(resolved.point).push(entry)
|
|
221
|
+
}
|
|
222
|
+
loopHooks = loop
|
|
223
|
+
engineByPoint = byPoint
|
|
224
|
+
syncSubscriptions()
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// ── loop events (config.loopEvents) ────────────────────────────────────────
|
|
228
|
+
/** Per top-level session: the open turn's tool-call count (PICASSO_HOOK_STEP) and its call ids → tool names. */
|
|
229
|
+
const turns = new WeakMap()
|
|
230
|
+
const prePlanPending = new WeakSet()
|
|
231
|
+
const fireLoop = async (event, session, { tool = '', step = 0 } = {}) => {
|
|
232
|
+
const matching = loopHooks.filter((hook) => hook.event === event || hook.event === '*')
|
|
233
|
+
// Sequential within one event, like createHookRunner; every hook settles (fail-open).
|
|
234
|
+
for (const hook of matching) {
|
|
235
|
+
await run(hook, event, session, {
|
|
236
|
+
PICASSO_HOOK_WORKSPACE: cwdOf(session),
|
|
237
|
+
PICASSO_HOOK_TOOL: tool,
|
|
238
|
+
PICASSO_HOOK_STEP: String(step),
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
async function prePlanListener(payload, next) {
|
|
243
|
+
const session = payload?.agent?.session
|
|
244
|
+
if (session !== undefined && prePlanPending.has(session)) {
|
|
245
|
+
prePlanPending.delete(session)
|
|
246
|
+
// Awaited before the turn's first model step, as the CLI awaits pre-plan before it dispatches the prompt.
|
|
247
|
+
await fireLoop('pre-plan', session)
|
|
248
|
+
}
|
|
249
|
+
return next()
|
|
250
|
+
}
|
|
251
|
+
const observeLoop = (session, event) => {
|
|
252
|
+
const type = event?.type
|
|
253
|
+
const data = event?.data ?? {}
|
|
254
|
+
if (type === 'turn/start') {
|
|
255
|
+
turns.set(session, { steps: 0, calls: new Map() })
|
|
256
|
+
if (loopHooks.some((hook) => hook.event === 'pre-plan' || hook.event === '*')) prePlanPending.add(session)
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
const turn = turns.get(session)
|
|
260
|
+
if (turn === undefined) return
|
|
261
|
+
if (type === 'tool/call') {
|
|
262
|
+
const tool = typeof data.name === 'string' ? data.name : 'unknown'
|
|
263
|
+
turn.steps += 1
|
|
264
|
+
if (typeof data.callId === 'string') turn.calls.set(data.callId, tool)
|
|
265
|
+
void fireLoop('pre-act', session, { tool, step: turn.steps })
|
|
266
|
+
} else if (type === 'tool/result') {
|
|
267
|
+
const message = data.message ?? {}
|
|
268
|
+
const callId = typeof message.toolCallId === 'string' ? message.toolCallId : message.source?.callId
|
|
269
|
+
// Only results of this turn's calls: a prune's trimmed copy of an old output is not a tool call.
|
|
270
|
+
if (typeof callId !== 'string' || !turn.calls.has(callId)) return
|
|
271
|
+
void fireLoop('post-act', session, { tool: turn.calls.get(callId), step: turn.steps })
|
|
272
|
+
} else if (type === 'turn/end') {
|
|
273
|
+
prePlanPending.delete(session)
|
|
274
|
+
turns.delete(session)
|
|
275
|
+
if (data.reason?.kind === 'error') void fireLoop('on-error', session, { step: turn.steps })
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const disposeCore = ctx.on('session/event', (session, event) => {
|
|
280
|
+
const type = event?.type
|
|
281
|
+
if (type === 'turn/start') refresh()
|
|
282
|
+
if (type === 'working-directory/change' && nonEmpty(event?.data?.cwd)) cwdBySession.set(session, event.data.cwd)
|
|
283
|
+
if (loopEvents && loopHooks.length > 0 && !isSubagentSession(session)) observeLoop(session, event)
|
|
284
|
+
// Session-log type hooks: the bound record type only.
|
|
285
|
+
const typed = (engineByPoint.get('session/event') ?? []).filter((hook) => hook.sessionType === type)
|
|
286
|
+
if (typed.length > 0) {
|
|
287
|
+
const tool = typeof event?.data?.name === 'string' ? event.data.name : ''
|
|
288
|
+
queueMicrotask(() => {
|
|
289
|
+
for (const hook of typed) void run(hook, hook.event, session, { PICASSO_HOOK_TOOL: tool })
|
|
290
|
+
})
|
|
291
|
+
}
|
|
292
|
+
})
|
|
293
|
+
refresh()
|
|
294
|
+
|
|
295
|
+
return () => {
|
|
296
|
+
disposeCore?.()
|
|
297
|
+
for (const dispose of subscriptions.values()) dispose?.()
|
|
298
|
+
subscriptions.clear()
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export { isLifecycleEvent, lifecycleEvent }
|