@picassohq/cli 0.0.0-stage → 1.0.1
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/browser_agent.yaml +53 -0
- package/agents/code_reviewer.yaml +49 -0
- package/agents/codebase_investigator.yaml +70 -0
- package/agents/design_auditor.yaml +50 -0
- package/agents/devex_auditor.yaml +50 -0
- package/agents/doc_writer.yaml +54 -0
- package/agents/engineering_architect.yaml +51 -0
- package/agents/generalist.yaml +55 -0
- package/agents/migration_planner.yaml +51 -0
- package/agents/product_reviewer.yaml +48 -0
- package/agents/qa_operator.yaml +52 -0
- package/agents/release_engineer.yaml +50 -0
- package/agents/security_auditor.yaml +62 -0
- package/agents/test_writer.yaml +52 -0
- package/assets/first-light-lockup.png +0 -0
- package/assets/first-light-tagline.png +0 -0
- package/assets/house/README.md +26 -0
- package/assets/house/auraone-banner.png +0 -0
- package/assets/house/auraone-logo.png +0 -0
- package/assets/house/kiro-prism-banner.png +0 -0
- package/assets/house/kiro-prism-logo.png +0 -0
- package/assets/house/picasso-banner.png +0 -0
- package/assets/house/picasso-logo.png +0 -0
- package/assets/wordmark-app.png +0 -0
- package/assets/wordmark-tight.png +0 -0
- package/assets/wordmark.png +0 -0
- package/design/styles.css +1042 -0
- package/dist/build-info.json +37 -0
- package/dist/index.js +3028 -0
- package/goals/cli-hardening.yaml +40 -0
- package/goals/repo-maintenance.yaml +40 -0
- package/package.json +49 -4
- package/runtime/dsh/picasso-ecosystem.community.amendments.yml +24 -0
- package/runtime/dsh/picasso-ecosystem.community.json +185 -0
- package/runtime/dsh/picasso-ecosystem.community.patch.yml +40 -0
- package/runtime/dsh/picasso-ecosystem.cordis.patch.yml +143 -0
- package/runtime/dsh/picasso-isolation.patch.yml +25 -0
- package/runtime/dsh/picasso-providers.patch.yml +373 -0
- package/runtime/dsh/picasso-runtime.cordis.yml +738 -0
- package/runtime/dsh/picasso-sdk.patch.yml +204 -0
- package/runtime/engine-manifest.json +214 -0
- package/runtime/package.json +6 -0
- package/runtime/packages/picasso-cache-probe/cordis.patch.yml +5 -0
- package/runtime/packages/picasso-cache-probe/index.js +477 -0
- package/runtime/packages/picasso-cache-probe/package.json +38 -0
- package/runtime/packages/picasso-commands/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-commands/fresh-review.js +573 -0
- package/runtime/packages/picasso-commands/index.js +497 -0
- package/runtime/packages/picasso-commands/package.json +28 -0
- package/runtime/packages/picasso-instructions/conflicts.js +115 -0
- package/runtime/packages/picasso-instructions/formats.js +145 -0
- package/runtime/packages/picasso-instructions/frontmatter.js +175 -0
- package/runtime/packages/picasso-instructions/glob.js +114 -0
- package/runtime/packages/picasso-instructions/globals.js +90 -0
- package/runtime/packages/picasso-instructions/imports.js +123 -0
- package/runtime/packages/picasso-instructions/index.js +326 -0
- package/runtime/packages/picasso-instructions/modes.js +64 -0
- package/runtime/packages/picasso-instructions/package.json +30 -0
- package/runtime/packages/picasso-instructions/parse.js +134 -0
- package/runtime/packages/picasso-instructions/report.js +161 -0
- package/runtime/packages/picasso-instructions/resolve.js +939 -0
- package/runtime/packages/picasso-instructions/scan.js +181 -0
- package/runtime/packages/picasso-instructions/settings.js +150 -0
- package/runtime/packages/picasso-instructions/skills.js +345 -0
- package/runtime/packages/picasso-lean-context/cordis.patch.yml +13 -0
- package/runtime/packages/picasso-lean-context/index.js +6049 -0
- package/runtime/packages/picasso-lean-context/package.json +26 -0
- package/runtime/packages/picasso-lean-context/timeline.js +417 -0
- package/runtime/packages/picasso-librarian/classify.js +146 -0
- package/runtime/packages/picasso-librarian/cordis.patch.yml +10 -0
- package/runtime/packages/picasso-librarian/index.js +878 -0
- package/runtime/packages/picasso-librarian/notes.js +159 -0
- package/runtime/packages/picasso-librarian/package.json +34 -0
- package/runtime/packages/picasso-librarian/state.js +478 -0
- package/runtime/packages/picasso-memory/index.js +1184 -0
- package/runtime/packages/picasso-memory/package.json +21 -0
- package/runtime/packages/picasso-model-router/connections.js +895 -0
- package/runtime/packages/picasso-model-router/cordis.patch.yml +17 -0
- package/runtime/packages/picasso-model-router/index.js +1081 -0
- package/runtime/packages/picasso-model-router/package.json +30 -0
- package/runtime/packages/picasso-options/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-options/index.js +263 -0
- package/runtime/packages/picasso-options/package.json +28 -0
- package/runtime/packages/picasso-progress-reports/cordis.patch.yml +10 -0
- package/runtime/packages/picasso-progress-reports/index.js +267 -0
- package/runtime/packages/picasso-progress-reports/package.json +30 -0
- package/runtime/packages/picasso-progress-reports/report.js +235 -0
- package/runtime/packages/picasso-sdk-server/aside.js +128 -0
- package/runtime/packages/picasso-sdk-server/connections.js +38 -0
- package/runtime/packages/picasso-sdk-server/context-timeline.js +27 -0
- package/runtime/packages/picasso-sdk-server/egress-gate.js +520 -0
- package/runtime/packages/picasso-sdk-server/failure-policy.js +246 -0
- package/runtime/packages/picasso-sdk-server/file-input.js +169 -0
- package/runtime/packages/picasso-sdk-server/goal.js +131 -0
- package/runtime/packages/picasso-sdk-server/ignore-file.js +458 -0
- package/runtime/packages/picasso-sdk-server/ignore-plugin.js +12 -0
- package/runtime/packages/picasso-sdk-server/ignore-sandbox.js +122 -0
- package/runtime/packages/picasso-sdk-server/ignore-snapshot.js +131 -0
- package/runtime/packages/picasso-sdk-server/image-input.js +44 -0
- package/runtime/packages/picasso-sdk-server/index.js +886 -0
- package/runtime/packages/picasso-sdk-server/instructions.js +34 -0
- package/runtime/packages/picasso-sdk-server/jobs.js +113 -0
- package/runtime/packages/picasso-sdk-server/mcp-gate.js +51 -0
- package/runtime/packages/picasso-sdk-server/model-selection.js +162 -0
- package/runtime/packages/picasso-sdk-server/package.json +50 -0
- package/runtime/packages/picasso-sdk-server/picasso-records.js +50 -0
- package/runtime/packages/picasso-sdk-server/provider-reload.js +84 -0
- package/runtime/packages/picasso-sdk-server/queue.js +322 -0
- package/runtime/packages/picasso-sdk-server/requester.js +43 -0
- package/runtime/packages/picasso-sdk-server/rewind.js +108 -0
- package/runtime/packages/picasso-sdk-server/schedule.js +187 -0
- package/runtime/packages/picasso-sdk-server/session-grants.js +35 -0
- package/runtime/packages/picasso-sdk-server/session-state.js +118 -0
- package/runtime/packages/picasso-sdk-server/shell-paths.js +869 -0
- package/runtime/packages/picasso-sdk-server/stream-deltas.js +211 -0
- package/runtime/packages/picasso-sdk-server/team-tasks.js +451 -0
- package/runtime/packages/picasso-sdk-server/tool-output.js +128 -0
- package/runtime/packages/picasso-sdk-server/tools-list.js +51 -0
- package/runtime/packages/picasso-sdk-server/wave2-hooks.js +612 -0
- package/runtime/packages/picasso-sdk-server/workspace-archive.js +170 -0
- package/runtime/packages/picasso-sdk-server/workspace-restore.js +180 -0
- package/runtime/packages/picasso-web-search/backends.js +350 -0
- package/runtime/packages/picasso-web-search/config.js +121 -0
- package/runtime/packages/picasso-web-search/cordis.patch.yml +7 -0
- package/runtime/packages/picasso-web-search/failure.js +111 -0
- package/runtime/packages/picasso-web-search/format.js +35 -0
- package/runtime/packages/picasso-web-search/index.js +66 -0
- package/runtime/packages/picasso-web-search/package.json +38 -0
- package/runtime/packages/picasso-web-search/router.js +155 -0
- package/runtime/packages/picasso-x-reader/circle.js +324 -0
- package/runtime/packages/picasso-x-reader/command.js +169 -0
- package/runtime/packages/picasso-x-reader/cordis.patch.yml +8 -0
- package/runtime/packages/picasso-x-reader/index.js +170 -0
- package/runtime/packages/picasso-x-reader/package.json +39 -0
- package/runtime/packages/picasso-x-reader/read.js +370 -0
- package/runtime/packages/picasso-x-reader/rewrite.js +38 -0
- package/runtime/packages/picasso-x-reader/settings.js +131 -0
- package/runtime/packages/picasso-x-reader/urls.js +154 -0
- package/runtime/packages/picasso-x-reader/xmd.js +512 -0
- package/runtime/plugins/picasso-auth-bridge/index.js +380 -0
- package/runtime/plugins/picasso-auth-bridge/package.json +28 -0
- package/runtime/plugins/picasso-auto-approve/index.js +1003 -0
- package/runtime/plugins/picasso-auto-approve/package.json +28 -0
- package/runtime/plugins/picasso-browser/index.js +424 -0
- package/runtime/plugins/picasso-browser/package.json +28 -0
- package/runtime/plugins/picasso-checkpoints/evidence.js +210 -0
- package/runtime/plugins/picasso-checkpoints/index.js +885 -0
- package/runtime/plugins/picasso-checkpoints/package.json +29 -0
- package/runtime/plugins/picasso-commands/fresh-review.js +573 -0
- package/runtime/plugins/picasso-commands/index.js +497 -0
- package/runtime/plugins/picasso-commands/package.json +28 -0
- package/runtime/plugins/picasso-instructions/conflicts.js +115 -0
- package/runtime/plugins/picasso-instructions/formats.js +145 -0
- package/runtime/plugins/picasso-instructions/frontmatter.js +175 -0
- package/runtime/plugins/picasso-instructions/glob.js +114 -0
- package/runtime/plugins/picasso-instructions/globals.js +90 -0
- package/runtime/plugins/picasso-instructions/imports.js +123 -0
- package/runtime/plugins/picasso-instructions/index.js +326 -0
- package/runtime/plugins/picasso-instructions/modes.js +64 -0
- package/runtime/plugins/picasso-instructions/package.json +30 -0
- package/runtime/plugins/picasso-instructions/parse.js +134 -0
- package/runtime/plugins/picasso-instructions/report.js +161 -0
- package/runtime/plugins/picasso-instructions/resolve.js +939 -0
- package/runtime/plugins/picasso-instructions/scan.js +181 -0
- package/runtime/plugins/picasso-instructions/settings.js +150 -0
- package/runtime/plugins/picasso-instructions/skills.js +345 -0
- package/runtime/plugins/picasso-lean-context/index.js +6049 -0
- package/runtime/plugins/picasso-lean-context/package.json +26 -0
- package/runtime/plugins/picasso-lean-context/timeline.js +417 -0
- package/runtime/plugins/picasso-librarian/classify.js +146 -0
- package/runtime/plugins/picasso-librarian/index.js +878 -0
- package/runtime/plugins/picasso-librarian/notes.js +159 -0
- package/runtime/plugins/picasso-librarian/package.json +34 -0
- package/runtime/plugins/picasso-librarian/state.js +478 -0
- package/runtime/plugins/picasso-lifecycle-hooks/index.js +186 -0
- package/runtime/plugins/picasso-lifecycle-hooks/package.json +25 -0
- package/runtime/plugins/picasso-mail/inbox.js +207 -0
- package/runtime/plugins/picasso-mail/index.js +293 -0
- package/runtime/plugins/picasso-mail/package.json +22 -0
- package/runtime/plugins/picasso-memory/index.js +1184 -0
- package/runtime/plugins/picasso-memory/package.json +21 -0
- package/runtime/plugins/picasso-model-router/connections.js +895 -0
- package/runtime/plugins/picasso-model-router/index.js +1081 -0
- package/runtime/plugins/picasso-model-router/package.json +30 -0
- package/runtime/plugins/picasso-options/index.js +263 -0
- package/runtime/plugins/picasso-options/package.json +28 -0
- package/runtime/plugins/picasso-progress-reports/index.js +267 -0
- package/runtime/plugins/picasso-progress-reports/package.json +30 -0
- package/runtime/plugins/picasso-progress-reports/report.js +235 -0
- package/runtime/plugins/picasso-provider-models/index.js +436 -0
- package/runtime/plugins/picasso-provider-models/package.json +28 -0
- package/runtime/plugins/picasso-repo-index/index.js +1850 -0
- package/runtime/plugins/picasso-repo-index/package.json +28 -0
- package/runtime/plugins/picasso-schedule-pause/index.js +344 -0
- package/runtime/plugins/picasso-schedule-pause/package.json +28 -0
- package/runtime/plugins/picasso-session-lifecycle/index.js +796 -0
- package/runtime/plugins/picasso-session-lifecycle/package.json +28 -0
- package/runtime/plugins/picasso-trackers/index.js +895 -0
- package/runtime/plugins/picasso-trackers/package.json +28 -0
- package/runtime/plugins/picasso-web-search/backends.js +350 -0
- package/runtime/plugins/picasso-web-search/config.js +121 -0
- package/runtime/plugins/picasso-web-search/failure.js +111 -0
- package/runtime/plugins/picasso-web-search/format.js +35 -0
- package/runtime/plugins/picasso-web-search/index.js +66 -0
- package/runtime/plugins/picasso-web-search/package.json +38 -0
- package/runtime/plugins/picasso-web-search/router.js +155 -0
- package/runtime/plugins/picasso-x-reader/circle.js +324 -0
- package/runtime/plugins/picasso-x-reader/command.js +169 -0
- package/runtime/plugins/picasso-x-reader/index.js +170 -0
- package/runtime/plugins/picasso-x-reader/package.json +39 -0
- package/runtime/plugins/picasso-x-reader/read.js +370 -0
- package/runtime/plugins/picasso-x-reader/rewrite.js +38 -0
- package/runtime/plugins/picasso-x-reader/settings.js +131 -0
- package/runtime/plugins/picasso-x-reader/urls.js +154 -0
- package/runtime/plugins/picasso-x-reader/xmd.js +512 -0
- package/runtime/relay/attachment-text.mjs +737 -0
- package/runtime/relay/companion/companion.css +126 -0
- package/runtime/relay/companion/companion.js +648 -0
- package/runtime/relay/companion/icon.svg +5 -0
- package/runtime/relay/companion/index.html +25 -0
- package/runtime/relay/companion/manifest.json +13 -0
- package/runtime/relay/companion/sw.js +41 -0
- package/runtime/relay/main.mjs +37252 -0
- package/runtime/relay/model-listing.mjs +614 -0
- package/runtime/relay/versions-routes.mjs +1198 -0
- package/runtime/verify-plugins.mjs +205 -0
- package/runtime-lock/package-lock.json +10779 -0
- package/runtime-lock/package.json +7 -0
- package/scripts/install-runtime.mjs +400 -0
- package/README.md +0 -3
|
@@ -0,0 +1,886 @@
|
|
|
1
|
+
// @picasso/sdk-server — drop-in replacement for @deepseek-ai/dsh-sdk-jsonrpc-server.
|
|
2
|
+
//
|
|
3
|
+
// It serves the stock dsh SDK protocol unchanged (initialize, session/prompt,
|
|
4
|
+
// session/resume, session/wait, session/working-directory/*, shutdown and the
|
|
5
|
+
// session.event / session.status / subagent.* notifications) by delegating to
|
|
6
|
+
// dsh's own HarnessSdkJsonRpcServer, and adds the methods the SDK lacks but the
|
|
7
|
+
// Picasso surfaces need (see packages/core/src/dsh/contract.ts):
|
|
8
|
+
//
|
|
9
|
+
// client -> server picasso/capabilities, session/cancel, approval/respond,
|
|
10
|
+
// question/respond, command/run, command/list,
|
|
11
|
+
// workspace/changes/get, workspace/changes/diff,
|
|
12
|
+
// workspace/restore
|
|
13
|
+
// server -> client approval/request, question/request (JSON-RPC requests)
|
|
14
|
+
//
|
|
15
|
+
// Approvals and ask-user questions are answered through dsh's own answerer
|
|
16
|
+
// waterfalls ('approval/request', 'user-questions/request'), so dsh still writes
|
|
17
|
+
// the durable approval/asked + approval/decided audit pair. When no client has
|
|
18
|
+
// announced the capability the waterfall falls through (fail-closed, as stock).
|
|
19
|
+
//
|
|
20
|
+
// Mounted by dsh/picasso-sdk.patch.yml, which disables the stock server entry.
|
|
21
|
+
import { existsSync, readdirSync, realpathSync } from 'node:fs';
|
|
22
|
+
import { approvalRequester, requesterFields } from './requester.js';
|
|
23
|
+
import { homedir, tmpdir } from 'node:os';
|
|
24
|
+
import { dirname, join } from 'node:path';
|
|
25
|
+
import process from 'node:process';
|
|
26
|
+
import { pathToFileURL } from 'node:url';
|
|
27
|
+
import { ASIDE_METHODS, createAside, removeSessionLogs } from './aside.js';
|
|
28
|
+
import { createIgnoreIndex, installIgnoreGuard } from './ignore-file.js';
|
|
29
|
+
import { withoutIgnoredFiles } from './ignore-snapshot.js';
|
|
30
|
+
import { assertImageInput } from './image-input.js';
|
|
31
|
+
import { createModelSelection, MODEL_METHODS } from './model-selection.js';
|
|
32
|
+
import { connectionMethods, CONNECTION_METHODS } from './connections.js';
|
|
33
|
+
import { createPicassoRecords, RECORD_METHODS } from './picasso-records.js';
|
|
34
|
+
import { createQueue, QUEUE_METHODS, QUEUE_NOTIFICATION } from './queue.js';
|
|
35
|
+
import { createRewind, REWIND_METHODS } from './rewind.js';
|
|
36
|
+
import { createSessionGrants, GRANT_METHODS } from './session-grants.js';
|
|
37
|
+
import { createSessionState, forkBoundary } from './session-state.js';
|
|
38
|
+
import { installStreamDeltas, SESSION_DELTA_METHOD } from './stream-deltas.js';
|
|
39
|
+
import { installToolOutputDeltas } from './tool-output.js';
|
|
40
|
+
import {
|
|
41
|
+
adoptAutoPreset as adoptAutoPresetFor,
|
|
42
|
+
createApprovalCritic,
|
|
43
|
+
createAutoCommit,
|
|
44
|
+
createVerifySteer,
|
|
45
|
+
createWave2Guards,
|
|
46
|
+
verifyTimeoutMs,
|
|
47
|
+
} from './wave2-hooks.js';
|
|
48
|
+
import { createWorkspaceArchive } from './workspace-archive.js';
|
|
49
|
+
import { restorePathFilter, restoreWorkspace } from './workspace-restore.js';
|
|
50
|
+
import { createEgressGate, EGRESS_METHODS } from './egress-gate.js';
|
|
51
|
+
import { createMcpGate } from './mcp-gate.js';
|
|
52
|
+
import { createFailurePolicy, envFailureRules } from './failure-policy.js';
|
|
53
|
+
import { createFileInput, FILE_METHODS } from './file-input.js';
|
|
54
|
+
import { createJobs, JOB_METHODS } from './jobs.js';
|
|
55
|
+
import { createSchedule, SCHEDULE_METHODS } from './schedule.js';
|
|
56
|
+
import { createTeamTasks, TEAM_TASK_METHODS } from './team-tasks.js';
|
|
57
|
+
import { createGoalControl, GOAL_METHODS } from './goal.js';
|
|
58
|
+
import { createToolsList, TOOLS_LIST_METHODS } from './tools-list.js';
|
|
59
|
+
import { createContextTimeline, CONTEXT_TIMELINE_METHODS } from './context-timeline.js';
|
|
60
|
+
import { createInstructions, INSTRUCTIONS_METHODS } from './instructions.js';
|
|
61
|
+
import { createProviderReload, PROVIDER_RELOAD_METHODS } from './provider-reload.js';
|
|
62
|
+
|
|
63
|
+
export const name = 'picasso-sdk-server';
|
|
64
|
+
export const inject = ['agents', 'workingDirectory'];
|
|
65
|
+
|
|
66
|
+
export const PICASSO_SDK_METHODS = Object.freeze([
|
|
67
|
+
'picasso/capabilities',
|
|
68
|
+
'session/cancel',
|
|
69
|
+
'session/fork',
|
|
70
|
+
'approval/respond',
|
|
71
|
+
...GRANT_METHODS,
|
|
72
|
+
'question/respond',
|
|
73
|
+
'command/run',
|
|
74
|
+
'command/list',
|
|
75
|
+
'workspace/changes/get',
|
|
76
|
+
'workspace/changes/diff',
|
|
77
|
+
'workspace/restore',
|
|
78
|
+
...QUEUE_METHODS,
|
|
79
|
+
...REWIND_METHODS,
|
|
80
|
+
...MODEL_METHODS,
|
|
81
|
+
...CONNECTION_METHODS,
|
|
82
|
+
...ASIDE_METHODS,
|
|
83
|
+
...RECORD_METHODS,
|
|
84
|
+
...JOB_METHODS,
|
|
85
|
+
...SCHEDULE_METHODS,
|
|
86
|
+
...TEAM_TASK_METHODS,
|
|
87
|
+
...GOAL_METHODS,
|
|
88
|
+
...EGRESS_METHODS,
|
|
89
|
+
...FILE_METHODS,
|
|
90
|
+
...TOOLS_LIST_METHODS,
|
|
91
|
+
...PROVIDER_RELOAD_METHODS,
|
|
92
|
+
...CONTEXT_TIMELINE_METHODS,
|
|
93
|
+
...INSTRUCTIONS_METHODS,
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
/** Locate a package inside the running dsh install (bare specifiers do not resolve from this file). */
|
|
97
|
+
function dshPackageEntry(pkg, file = join('lib', 'index.js')) {
|
|
98
|
+
const starts = [process.argv[1], process.env.PICASSO_DSH_INSTALL_DIR].filter(Boolean);
|
|
99
|
+
for (const start of starts) {
|
|
100
|
+
let dir;
|
|
101
|
+
try {
|
|
102
|
+
dir = dirname(realpathSync(start));
|
|
103
|
+
} catch {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
for (;;) {
|
|
107
|
+
const candidate = join(dir, 'node_modules', '@deepseek-ai', pkg, file);
|
|
108
|
+
if (existsSync(candidate)) return pathToFileURL(candidate).href;
|
|
109
|
+
const parent = dirname(dir);
|
|
110
|
+
if (parent === dir) break;
|
|
111
|
+
dir = parent;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
throw new Error(`picasso-sdk-server: cannot locate @deepseek-ai/${pkg} next to the dsh runtime`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The workspace-changes scratch object store that holds a snapshot tree (loose object lookup). */
|
|
118
|
+
function snapshotObjectDir(tree) {
|
|
119
|
+
const roots = [...new Set([tmpdir(), process.env.TMPDIR].filter(Boolean))];
|
|
120
|
+
for (const root of roots) {
|
|
121
|
+
let entries = [];
|
|
122
|
+
try {
|
|
123
|
+
entries = readdirSync(root).filter((name) => name.startsWith('dsh-workspace-changes-'));
|
|
124
|
+
} catch {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
for (const name of entries) {
|
|
128
|
+
const objects = join(root, name, 'objects');
|
|
129
|
+
if (existsSync(join(objects, tree.slice(0, 2), tree.slice(2)))) return objects;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const APPROVAL_OUTCOMES = new Set(['allowed-once', 'rejected', 'cancelled', 'unavailable']);
|
|
136
|
+
|
|
137
|
+
const [{ HarnessSdkJsonRpcServer }, { JsonRpcLineTransport }, { buildForkSeed }, { setSandboxMode }, { BlockAssembler, createUserMessage, freezeMessage }, { installModelSelection }, { setApprovalPolicy }] = await Promise.all([
|
|
138
|
+
import(dshPackageEntry('dsh-sdk-jsonrpc-server')),
|
|
139
|
+
import(dshPackageEntry('dsh-sdk-protocol')),
|
|
140
|
+
import(dshPackageEntry('dsh-session', join('lib', 'types', 'fork.js'))),
|
|
141
|
+
import(dshPackageEntry('dsh-sandbox-policy')),
|
|
142
|
+
import(dshPackageEntry('dsh-llm')),
|
|
143
|
+
import(dshPackageEntry('dsh-agent')),
|
|
144
|
+
import(dshPackageEntry('dsh-user-approval')),
|
|
145
|
+
]);
|
|
146
|
+
const { appendPluginRecord } = await import(dshPackageEntry('dsh-session'));
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The fork boundary: the given seq, else the last event of turn `turn` (or of
|
|
150
|
+
* the latest completed turn), extended over trailing log-only events exactly
|
|
151
|
+
* like dsh's own `latestCompletedPrefixBoundary`.
|
|
152
|
+
*/
|
|
153
|
+
export { forkBoundary };
|
|
154
|
+
|
|
155
|
+
export function apply(ctx, config = {}) {
|
|
156
|
+
const rootFiber = ctx.root.fiber;
|
|
157
|
+
const input = config.input ?? process.stdin;
|
|
158
|
+
const output = config.output ?? process.stdout;
|
|
159
|
+
const exit = config.exit ?? ((code) => process.exit(code));
|
|
160
|
+
const transport = new JsonRpcLineTransport(input, output);
|
|
161
|
+
const server = new HarnessSdkJsonRpcServer(ctx, transport, {
|
|
162
|
+
maxTokensAsSuccess: config.maxTokensAsSuccess ?? false,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
/** Client-announced capabilities (picasso/capabilities params.client). */
|
|
166
|
+
const client = { approvals: false, questions: false, deltas: false };
|
|
167
|
+
/** Session-scoped "allow for session" grants (session-grants.js). */
|
|
168
|
+
const grants = createSessionGrants();
|
|
169
|
+
/** Pending server->client asks that `approval/respond` / `question/respond` may settle out of band. */
|
|
170
|
+
const pendingAsks = new Map();
|
|
171
|
+
const state = createSessionState({
|
|
172
|
+
query: () => ctx.get?.('sessionQuery') ?? ctx.sessionQuery,
|
|
173
|
+
enforce: setSandboxMode,
|
|
174
|
+
defaultMode: process.env.DSH_PERMISSION_MODE ?? 'workspace-write',
|
|
175
|
+
});
|
|
176
|
+
const archive = createWorkspaceArchive(
|
|
177
|
+
join(process.env.DSH_HOME ?? join(homedir(), '.dsh'), 'picasso-workspace'),
|
|
178
|
+
snapshotObjectDir,
|
|
179
|
+
);
|
|
180
|
+
const snapshotTasks = new Map();
|
|
181
|
+
const flushSnapshots = async (sessionId) => {
|
|
182
|
+
await snapshotTasks.get(sessionId);
|
|
183
|
+
};
|
|
184
|
+
/** `.picassoignore` lookups shared by the tool guard and the workspace snapshots. */
|
|
185
|
+
const ignoreIndex = createIgnoreIndex();
|
|
186
|
+
const isIgnored = (cwd, path) => ignoreIndex.ignored(cwd, path, false);
|
|
187
|
+
/** A summary as clients see it: ignored files are never listed, diffed or restored. */
|
|
188
|
+
const visibleSummary = (summary) => (summary === undefined || summary === null || typeof summary.cwd !== 'string'
|
|
189
|
+
? summary
|
|
190
|
+
: withoutIgnoredFiles(summary, (path) => isIgnored(summary.cwd, path)));
|
|
191
|
+
const summaryFor = async (sessionId, seq) => {
|
|
192
|
+
await flushSnapshots(sessionId);
|
|
193
|
+
return visibleSummary(workspaceChanges().summary(sessionId, seq) ?? await archive.read(sessionId, seq));
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const agentFor = (sessionId) => {
|
|
197
|
+
// HarnessSdkJsonRpcServer keeps SDK-owned agents in `sessions`; child
|
|
198
|
+
// sessions are reached through the registry.
|
|
199
|
+
const record = server.sessions?.get(sessionId);
|
|
200
|
+
if (record?.handle?.agent !== undefined) return record.handle.agent;
|
|
201
|
+
const registry = ctx.agents;
|
|
202
|
+
return registry?.get?.(sessionId) ?? registry?.find?.(sessionId);
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const ask = (kind, params, signal) =>
|
|
206
|
+
new Promise((resolve, reject) => {
|
|
207
|
+
const askId = params.id;
|
|
208
|
+
const settle = (value) => {
|
|
209
|
+
if (!pendingAsks.has(askId)) return;
|
|
210
|
+
pendingAsks.delete(askId);
|
|
211
|
+
resolve(value);
|
|
212
|
+
};
|
|
213
|
+
pendingAsks.set(askId, { kind, settle });
|
|
214
|
+
signal?.addEventListener('abort', () => settle(undefined), { once: true });
|
|
215
|
+
transport.request(`${kind}/request`, params, signal).then(settle, (error) => {
|
|
216
|
+
if (pendingAsks.has(askId)) {
|
|
217
|
+
pendingAsks.delete(askId);
|
|
218
|
+
reject(error);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
/** Side-question sessions (`session/aside/*`), created below. */
|
|
224
|
+
let aside;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Second-model critique for grant-based auto-approvals (T2.19). Reads like
|
|
228
|
+
* the auto-review plugin's protocol: the session's own route is asked for
|
|
229
|
+
* {"verdict":"allow"|"deny"|"unsure","reason"?}. Only a confident `allow`
|
|
230
|
+
* keeps the grant auto-approve; deny/unsure/parse failure/missing llm all
|
|
231
|
+
* defer to the ordinary ask. Opt out with PICASSO_APPROVAL_CRITIC=0.
|
|
232
|
+
* (wave2-hooks.js)
|
|
233
|
+
*/
|
|
234
|
+
const critiqueAutoApproval = createApprovalCritic({
|
|
235
|
+
llm: () => ctx.get?.('llm') ?? ctx.llm,
|
|
236
|
+
defaults: () => ({ provider: server.provider, model: server.model }),
|
|
237
|
+
createAssembler: () => new BlockAssembler(),
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
ctx.on('approval/request', async (req, next) => {
|
|
241
|
+
const sessionId = req.agent?.session?.id;
|
|
242
|
+
if (sessionId !== undefined && aside?.isAside(sessionId) === true) return 'rejected';
|
|
243
|
+
if (sessionId !== undefined) {
|
|
244
|
+
try {
|
|
245
|
+
if (await state.rejectApproval(sessionId)) return 'rejected';
|
|
246
|
+
} catch {
|
|
247
|
+
return 'unavailable';
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (!client.approvals) return next();
|
|
251
|
+
let criticReason;
|
|
252
|
+
if (grants.has(sessionId, req.toolName)) {
|
|
253
|
+
// T2.19 critic gate: a session grant would auto-approve this call. A
|
|
254
|
+
// second model vets it first; only a confident allow stays automatic,
|
|
255
|
+
// anything else (deny, unsure, reviewer failure) defers to the
|
|
256
|
+
// ordinary client ask below.
|
|
257
|
+
const verdict = await critiqueAutoApproval(req);
|
|
258
|
+
if (verdict.allow) return 'allowed-once';
|
|
259
|
+
criticReason = verdict.reason;
|
|
260
|
+
}
|
|
261
|
+
const id = `appr_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`;
|
|
262
|
+
try {
|
|
263
|
+
const answer = await ask(
|
|
264
|
+
'approval',
|
|
265
|
+
{
|
|
266
|
+
id,
|
|
267
|
+
sessionId,
|
|
268
|
+
toolName: req.toolName,
|
|
269
|
+
...(req.callId === undefined ? {} : { callId: req.callId }),
|
|
270
|
+
...(req.reason === undefined ? {} : { reason: req.reason }),
|
|
271
|
+
...(criticReason === undefined ? {} : { criticReason }),
|
|
272
|
+
...requesterFields(approvalRequester(req.agent)),
|
|
273
|
+
},
|
|
274
|
+
req.signal,
|
|
275
|
+
);
|
|
276
|
+
if (answer === undefined) return 'cancelled';
|
|
277
|
+
const decision = typeof answer === 'string' ? answer : answer?.decision;
|
|
278
|
+
if (decision === 'allow-session') {
|
|
279
|
+
grants.add(sessionId, req.toolName);
|
|
280
|
+
return 'allowed-once';
|
|
281
|
+
}
|
|
282
|
+
if (decision === 'allow' || decision === 'allowed-once') return 'allowed-once';
|
|
283
|
+
if (decision === 'deny' || decision === 'rejected') return 'rejected';
|
|
284
|
+
return APPROVAL_OUTCOMES.has(decision) ? decision : 'unavailable';
|
|
285
|
+
} catch {
|
|
286
|
+
return 'unavailable';
|
|
287
|
+
}
|
|
288
|
+
}, { prepend: true });
|
|
289
|
+
|
|
290
|
+
installStreamDeltas(ctx, {
|
|
291
|
+
enabled: () => client.deltas,
|
|
292
|
+
notify: (method, params) => transport.notify(method, params),
|
|
293
|
+
});
|
|
294
|
+
// Live shell output (`session.delta` kind `tool-output`, tool-output.js).
|
|
295
|
+
installToolOutputDeltas(ctx, {
|
|
296
|
+
enabled: () => client.deltas,
|
|
297
|
+
notify: (method, params) => transport.notify(method, params),
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
// `.picassoignore`: denied paths and filtered glob/grep results (ignore-file.js).
|
|
301
|
+
installIgnoreGuard(ctx, {
|
|
302
|
+
cwdOf: (session) => (ctx.get?.('workingDirectory') ?? ctx.workingDirectory)?.get(session),
|
|
303
|
+
index: ignoreIndex,
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
const cwdOfSession = (session) => {
|
|
307
|
+
try {
|
|
308
|
+
return (ctx.get?.('workingDirectory') ?? ctx.workingDirectory)?.get(session) ?? process.cwd();
|
|
309
|
+
} catch {
|
|
310
|
+
return process.cwd();
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
// -- Wave-2 guards (fail-closed vetoes on tools/pre-execute) --------------
|
|
315
|
+
//
|
|
316
|
+
// env-file guard: tool calls (fs tools and shell commands alike, via
|
|
317
|
+
// toolCallReferences) that touch a `*.env` path are denied by default;
|
|
318
|
+
// secrets must never enter the agent pipeline. PICASSO_ENV_GUARD=0 opts out.
|
|
319
|
+
// external-directory guard: a call that asks the sandbox for an escalation
|
|
320
|
+
// (`sandbox_permissions`) while referencing paths outside the session
|
|
321
|
+
// workspace is denied; escapes must go through ordinary approval.
|
|
322
|
+
// doom-loop guard: the same tool with identical arguments more than twice in
|
|
323
|
+
// a row within one turn is denied. (wave2-hooks.js)
|
|
324
|
+
const wave2Guards = createWave2Guards({
|
|
325
|
+
cwdOf: cwdOfSession,
|
|
326
|
+
// Two-argument call on purpose: the ignore index stats the path itself
|
|
327
|
+
// (unlike the `isIgnored` helper above, which forces isDirectory=false).
|
|
328
|
+
isIgnored: (cwd, path) => ignoreIndex.ignored(cwd, path),
|
|
329
|
+
});
|
|
330
|
+
ctx.on('session/event', (session, event) => wave2Guards.observe(session, event));
|
|
331
|
+
ctx.on('tools/pre-execute', (exec, next) => wave2Guards.preExecute(exec, next));
|
|
332
|
+
// No prepend: the .picassoignore guard (prepended above) must see paths
|
|
333
|
+
// first so ignored files report the ignore reason rather than env-guard's.
|
|
334
|
+
|
|
335
|
+
// -- Post-execute verification steer-back (opt-in via PICASSO_VERIFY_CMD) --
|
|
336
|
+
//
|
|
337
|
+
// When PICASSO_VERIFY_CMD names a command (e.g. `pnpm lint`), every
|
|
338
|
+
// successful file-writing tool call runs it in the session workspace; a
|
|
339
|
+
// non-zero exit steers the running turn with the output so the model fixes
|
|
340
|
+
// the breakage. Bounded to two steers per turn so a persistently failing
|
|
341
|
+
// check cannot loop forever. (wave2-hooks.js)
|
|
342
|
+
const verifyCmd = process.env.PICASSO_VERIFY_CMD;
|
|
343
|
+
if (typeof verifyCmd === 'string' && verifyCmd.trim().length > 0) {
|
|
344
|
+
const verify = createVerifySteer({
|
|
345
|
+
command: verifyCmd,
|
|
346
|
+
timeoutMs: verifyTimeoutMs(process.env.PICASSO_VERIFY_TIMEOUT_MS),
|
|
347
|
+
cwdOf: cwdOfSession,
|
|
348
|
+
createUserMessage,
|
|
349
|
+
warn: (message) => ctx.logger?.warn?.(message),
|
|
350
|
+
});
|
|
351
|
+
ctx.on('session/event', (session, event) => verify.observe(session, event));
|
|
352
|
+
ctx.on('tools/post-execute', (exec, result, next) => verify.postExecute(exec, result, next));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// -- Auto-commit per turn (opt-in via PICASSO_AUTO_COMMIT=1) ----------------
|
|
356
|
+
//
|
|
357
|
+
// On `turn/end`, stage exactly the paths the turn changed and commit them
|
|
358
|
+
// with a `Picasso-Thread: <sessionId>` trailer. Skips when the work tree
|
|
359
|
+
// holds dirty paths the turn did not touch so the user's own edits are
|
|
360
|
+
// never swept into a Picasso commit. (wave2-hooks.js)
|
|
361
|
+
const autoCommit = createAutoCommit({
|
|
362
|
+
changesSeq: (sessionId) => state.changesSeq(sessionId),
|
|
363
|
+
getSummary: (sessionId, seq) => workspaceChanges().summary(sessionId, seq),
|
|
364
|
+
cwdOf: cwdOfSession,
|
|
365
|
+
isAside: (sessionId) => aside?.isAside(sessionId),
|
|
366
|
+
warn: (message) => ctx.logger?.warn?.(message),
|
|
367
|
+
info: (message) => ctx.logger?.info?.(message),
|
|
368
|
+
});
|
|
369
|
+
ctx.on('session/event', (session, event) => autoCommit.observe(session, event));
|
|
370
|
+
|
|
371
|
+
// -- Runtime seams the pinned dsh already ships (T4.x closeout) ------------
|
|
372
|
+
//
|
|
373
|
+
// Each module refuses with a coded error when the upstream service it rides
|
|
374
|
+
// is not mounted — `picasso/capabilities` still advertises the method names
|
|
375
|
+
// and the client checks `supports()` before calling, so a stock SDK server
|
|
376
|
+
// reports them absent instead.
|
|
377
|
+
const jobs = createJobs({
|
|
378
|
+
jobs: () => ctx.get?.('jobs') ?? ctx.jobs,
|
|
379
|
+
agentFor,
|
|
380
|
+
});
|
|
381
|
+
const schedules = createSchedule({
|
|
382
|
+
schedule: () => ctx.get?.('schedule') ?? ctx.schedule,
|
|
383
|
+
agentFor,
|
|
384
|
+
});
|
|
385
|
+
const teamTasks = createTeamTasks({
|
|
386
|
+
agentFor,
|
|
387
|
+
eventsFor: async (sessionId) => {
|
|
388
|
+
const query = ctx.get?.('sessionQuery') ?? ctx.sessionQuery;
|
|
389
|
+
if (query === undefined) throw new Error('session query is not mounted');
|
|
390
|
+
const observed = await query.observeSession(sessionId);
|
|
391
|
+
try {
|
|
392
|
+
return [...observed.events];
|
|
393
|
+
} finally {
|
|
394
|
+
observed[Symbol.dispose]?.();
|
|
395
|
+
}
|
|
396
|
+
},
|
|
397
|
+
warn: (message) => ctx.logger?.warn?.(message),
|
|
398
|
+
});
|
|
399
|
+
ctx.on('session/event', (session, event) => teamTasks.observe(session, event));
|
|
400
|
+
const goalControl = createGoalControl({
|
|
401
|
+
goals: () => ctx.get?.('goals') ?? ctx.goals,
|
|
402
|
+
agentFor,
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
// Domain-gated egress for gated sessions (T4.9): wraps ctx.shell so the
|
|
406
|
+
// rule's proxy env reaches bash execs; pre-execute denies tools the env
|
|
407
|
+
// seam cannot reach. `install` returns the tools/pre-execute disposer.
|
|
408
|
+
const egress = createEgressGate({ warn: (message) => ctx.logger?.warn?.(message) });
|
|
409
|
+
const disposeEgress = egress.install(ctx);
|
|
410
|
+
ctx.on('session/disposed', (session) => egress.drop(typeof session === 'string' ? session : session?.id));
|
|
411
|
+
|
|
412
|
+
// MCP connector per-call gate (mcp-connector-review condition 1): asks on
|
|
413
|
+
// every mcp__*/shared-resource tool call — unconditional, no config.
|
|
414
|
+
const mcpGate = createMcpGate();
|
|
415
|
+
const disposeMcpGate = mcpGate.install(ctx);
|
|
416
|
+
|
|
417
|
+
// On-failure policy (T4.5): `tools/execute` waterfall lets us re-dispatch a
|
|
418
|
+
// failed call (retry:N), escalate to a fresh approval ask (ask), or abort
|
|
419
|
+
// the turn (abort) per preset/env declaration.
|
|
420
|
+
const failurePolicy = createFailurePolicy({
|
|
421
|
+
rules: envFailureRules(),
|
|
422
|
+
presetSpec: (session) => {
|
|
423
|
+
const presets = ctx.get?.('permissionPresets') ?? ctx.permissionPresets;
|
|
424
|
+
const presetName = presets?.current?.(session);
|
|
425
|
+
return typeof presetName === 'string' ? presets?.resolve?.(presetName)?.onFailure : undefined;
|
|
426
|
+
},
|
|
427
|
+
requestApproval: (request) => {
|
|
428
|
+
const approval = ctx.get?.('approval') ?? ctx.approval;
|
|
429
|
+
return approval?.request?.(request);
|
|
430
|
+
},
|
|
431
|
+
note: (session, event) => {
|
|
432
|
+
try {
|
|
433
|
+
appendPluginRecord(session, 'picasso/failure-policy', event);
|
|
434
|
+
} catch (error) {
|
|
435
|
+
ctx.logger?.warn?.(`Picasso on-failure record failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
warn: (message) => ctx.logger?.warn?.(message),
|
|
439
|
+
});
|
|
440
|
+
ctx.on('tools/execute', (exec, next) => failurePolicy.execute(exec, next));
|
|
441
|
+
|
|
442
|
+
// Typed `type:"file"` prompt blocks (T4.1): admits workspace files through
|
|
443
|
+
// the runtime's attachment store so the model sees real file metadata.
|
|
444
|
+
const fileInput = createFileInput({
|
|
445
|
+
attachments: () => ctx.get?.('attachments') ?? ctx.attachments,
|
|
446
|
+
cwdFor: (sessionId) => cwdOfSession(agentFor(sessionId)?.session),
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
// -- Auto permission preset adoption (Picasso auto mode) -------------------
|
|
450
|
+
//
|
|
451
|
+
// `--permission-mode auto` sets DSH_PERMISSION_MODE_PICASSO=auto. The ecosystem
|
|
452
|
+
// patch mounts @deepseek-ai/dsh-experimental-auto-review for every launch (it
|
|
453
|
+
// reviews only `auto` sessions, and one that recorded `auto` must stay resumable).
|
|
454
|
+
// The plugin registers the reserved `auto` preset (its name only appears in
|
|
455
|
+
// `presets.names` once its registerAuto integration is live). We adopt the
|
|
456
|
+
// preset per top-level session; when the reviewer is not mounted, "auto" is
|
|
457
|
+
// absent from names and we never apply it; applying it then would mean
|
|
458
|
+
// unreviewed danger-full-access, which is a non-starter. (wave2-hooks.js)
|
|
459
|
+
const wantAutoPreset = process.env.DSH_PERMISSION_MODE_PICASSO === 'auto';
|
|
460
|
+
const adoptAutoPreset = (session) => adoptAutoPresetFor(session, {
|
|
461
|
+
wantAutoPreset,
|
|
462
|
+
isAside: (sessionId) => aside?.isAside(sessionId),
|
|
463
|
+
presets: ctx.get?.('permissionPresets') ?? ctx.permissionPresets,
|
|
464
|
+
applyPolicy: setApprovalPolicy,
|
|
465
|
+
warn: (message) => ctx.logger?.warn?.(message),
|
|
466
|
+
});
|
|
467
|
+
ctx.on('session/created', (session) => adoptAutoPreset(session));
|
|
468
|
+
// `ctx.sessions` is not in this plugin's inject list; the SDK server keeps
|
|
469
|
+
// its own session map, which is the same registry session/created feeds.
|
|
470
|
+
for (const session of server.sessions?.values?.() ?? []) adoptAutoPreset(session);
|
|
471
|
+
|
|
472
|
+
ctx.on('user-questions/request', async (request, next) => {
|
|
473
|
+
if (!client.questions) return next();
|
|
474
|
+
const id = `q_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`;
|
|
475
|
+
const answer = await ask(
|
|
476
|
+
'question',
|
|
477
|
+
{
|
|
478
|
+
id,
|
|
479
|
+
sessionId: request.agent?.session?.id,
|
|
480
|
+
questions: request.questions,
|
|
481
|
+
...(request.wait?.callId === undefined ? {} : { callId: request.wait.callId }),
|
|
482
|
+
},
|
|
483
|
+
request.signal,
|
|
484
|
+
);
|
|
485
|
+
if (answer === undefined || !Array.isArray(answer.answers)) return next();
|
|
486
|
+
return { answers: answer.answers };
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
const requireSession = (params) => {
|
|
490
|
+
if (typeof params?.sessionId !== 'string' || params.sessionId.length === 0) {
|
|
491
|
+
throw new TypeError('a non-empty sessionId is required');
|
|
492
|
+
}
|
|
493
|
+
const agent = agentFor(params.sessionId);
|
|
494
|
+
if (agent === undefined) throw new Error(`unknown or not-live session: ${params.sessionId}`);
|
|
495
|
+
return agent;
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
const workspaceChanges = () => {
|
|
499
|
+
const service = ctx.get?.('workspaceChanges') ?? ctx.workspaceChanges;
|
|
500
|
+
if (service === undefined) throw new Error('workspace-changes plugin is not mounted');
|
|
501
|
+
return service;
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
// Enforced plan mode: dsh's plan mode is advisory ("every tool remains
|
|
505
|
+
// available"). While `plan/mode.active`, Picasso switches the session's
|
|
506
|
+
// sandbox to read-only (a durable `sandbox/mode` event) and denies sandbox
|
|
507
|
+
// escalations; leaving plan mode restores the previous mode.
|
|
508
|
+
ctx.on('session/event', (session, event) => {
|
|
509
|
+
if (!state.ingest(session.id, event)) return;
|
|
510
|
+
const target = state.targetMode(session.id);
|
|
511
|
+
queueMicrotask(() => {
|
|
512
|
+
try {
|
|
513
|
+
setSandboxMode(session, target);
|
|
514
|
+
} catch {
|
|
515
|
+
// a disposed session needs no switch
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
ctx.on('tools/pre-execute', async (execution, next) => {
|
|
520
|
+
const session = execution.agent?.session;
|
|
521
|
+
if (session !== undefined) await state.ready(session);
|
|
522
|
+
return next();
|
|
523
|
+
}, { prepend: true });
|
|
524
|
+
|
|
525
|
+
/** `workspace/changes` seqs per session, from the live session log. */
|
|
526
|
+
const { changeSeqs } = state;
|
|
527
|
+
ctx.on('session/event', (session, event) => {
|
|
528
|
+
if (event?.type !== 'workspace/changes') return;
|
|
529
|
+
const task = (snapshotTasks.get(session.id) ?? Promise.resolve()).then(async () => {
|
|
530
|
+
const summary = workspaceChanges().summary(session.id, event.seq);
|
|
531
|
+
if (summary === undefined) throw new Error(`workspace summary unavailable for seq ${event.seq}`);
|
|
532
|
+
await archive.save(session.id, event.seq, summary, typeof summary.cwd === 'string' ? { isIgnored: (path) => isIgnored(summary.cwd, path) } : {});
|
|
533
|
+
}).catch((error) => {
|
|
534
|
+
ctx.logger?.warn?.(`Picasso workspace snapshot persistence failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
535
|
+
});
|
|
536
|
+
snapshotTasks.set(session.id, task);
|
|
537
|
+
});
|
|
538
|
+
/** Latest `workspace/changes` seq for a turn (or the newest one). */
|
|
539
|
+
const changesSeq = (sessionId, turn) => {
|
|
540
|
+
return state.changesSeq(sessionId, turn);
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
const queue = createQueue({
|
|
544
|
+
agentFor,
|
|
545
|
+
createUserMessage,
|
|
546
|
+
freezeMessage,
|
|
547
|
+
ready: (agent) => state.ready(agent.session),
|
|
548
|
+
notify: (method, params) => transport.notify(method, params),
|
|
549
|
+
prompt: (sessionId, content) => server.prompt({ sessionId, contentBlocks: content }),
|
|
550
|
+
});
|
|
551
|
+
ctx.on('session/event', (session, event) => queue.observe(session, event));
|
|
552
|
+
|
|
553
|
+
const models = createModelSelection({
|
|
554
|
+
agentFor,
|
|
555
|
+
llm: () => ctx.get?.('llm') ?? ctx.llm,
|
|
556
|
+
install: installModelSelection,
|
|
557
|
+
defaults: () => ({
|
|
558
|
+
provider: server.provider,
|
|
559
|
+
model: server.model,
|
|
560
|
+
...(server.reasoningEffort === undefined ? {} : { reasoningEffort: server.reasoningEffort }),
|
|
561
|
+
}),
|
|
562
|
+
});
|
|
563
|
+
// The live, non-persisting model switch other Picasso plugins reuse: @picasso/model-router's ordered connections
|
|
564
|
+
// move a session to the next connection through it (the same seam `/model` uses), never a saved default.
|
|
565
|
+
try {
|
|
566
|
+
ctx.provide?.('picassoModelSelection', Object.freeze({
|
|
567
|
+
/** `immediate`: also apply the pick to the step being retried (its prompt was assembled before the switch). */
|
|
568
|
+
set: async (params, { immediate = false } = {}) => {
|
|
569
|
+
const reply = await models.methods['session/model/set'](params);
|
|
570
|
+
if (immediate) {
|
|
571
|
+
const agent = agentFor(params.sessionId);
|
|
572
|
+
if (agent !== undefined) models.selectionFor(agent).assembled = reply.selected;
|
|
573
|
+
}
|
|
574
|
+
return reply;
|
|
575
|
+
},
|
|
576
|
+
}));
|
|
577
|
+
} catch (error) {
|
|
578
|
+
ctx.logger?.warn?.(`Picasso model selection seam not provided: ${error instanceof Error ? error.message : String(error)}`);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* A new session seeded with the source log up to a boundary: rewinding the
|
|
583
|
+
* conversation to a turn is a fork at the end of the turn before it. The new
|
|
584
|
+
* session carries the source's model selection (the live one when set).
|
|
585
|
+
*/
|
|
586
|
+
const forkSession = async (params) => {
|
|
587
|
+
const query = ctx.get?.('sessionQuery') ?? ctx.sessionQuery;
|
|
588
|
+
if (query === undefined) throw new Error('session query is not mounted');
|
|
589
|
+
const observed = await query.observeSession(params.sessionId);
|
|
590
|
+
try {
|
|
591
|
+
const events = observed.events;
|
|
592
|
+
const boundary = forkBoundary(events, params);
|
|
593
|
+
if (boundary === undefined || events[boundary]?.seq !== boundary) throw new Error('no completed turn to fork from');
|
|
594
|
+
await flushSnapshots(params.sessionId);
|
|
595
|
+
const seed = buildForkSeed(events, boundary);
|
|
596
|
+
const source = agentFor(params.sessionId);
|
|
597
|
+
const selected = source === undefined ? undefined : models.selectedFor(source);
|
|
598
|
+
const handle = await ctx.agents.create({
|
|
599
|
+
sessionId: params.newSessionId,
|
|
600
|
+
seed,
|
|
601
|
+
inheritedEventCount: boundary + 1,
|
|
602
|
+
meta: {
|
|
603
|
+
...(observed.header?.cwd === undefined ? {} : { cwd: observed.header.cwd }),
|
|
604
|
+
parentSession: params.sessionId,
|
|
605
|
+
isSeeded: true,
|
|
606
|
+
},
|
|
607
|
+
agentOptions: { provider: selected?.provider ?? server.provider, model: selected?.model ?? server.model },
|
|
608
|
+
});
|
|
609
|
+
await handle.dispose();
|
|
610
|
+
await archive.fork(params.sessionId, params.newSessionId, events.filter((event) => event.seq <= boundary && event.type === 'workspace/changes').map((event) => event.seq));
|
|
611
|
+
return { sessionId: params.newSessionId, boundary };
|
|
612
|
+
} finally {
|
|
613
|
+
observed[Symbol.dispose]?.();
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
const rewind = createRewind({
|
|
618
|
+
agentFor,
|
|
619
|
+
hydrate: (sessionId) => state.hydrate(sessionId),
|
|
620
|
+
changeSeqs,
|
|
621
|
+
readEvents: async (sessionId) => {
|
|
622
|
+
const query = ctx.get?.('sessionQuery') ?? ctx.sessionQuery;
|
|
623
|
+
if (query === undefined) throw new Error('session query is not mounted');
|
|
624
|
+
const observed = await query.observeSession(sessionId);
|
|
625
|
+
try {
|
|
626
|
+
return [...observed.events];
|
|
627
|
+
} finally {
|
|
628
|
+
observed[Symbol.dispose]?.();
|
|
629
|
+
}
|
|
630
|
+
},
|
|
631
|
+
fork: forkSession,
|
|
632
|
+
summaryFor,
|
|
633
|
+
archive,
|
|
634
|
+
findObjects: snapshotObjectDir,
|
|
635
|
+
isIgnored,
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
/** The provider/model a prompt to `sessionId` reaches: live selection, last request, else the initialized route. */
|
|
639
|
+
const routeFor = (sessionId) => {
|
|
640
|
+
const agent = agentFor(sessionId);
|
|
641
|
+
const selected = agent === undefined ? undefined : models.selectedFor(agent);
|
|
642
|
+
if (selected !== undefined) return selected;
|
|
643
|
+
const config = agent?.session?.requestHeader?.()?.config;
|
|
644
|
+
if (typeof config?.provider === 'string' && typeof config.model === 'string') return config;
|
|
645
|
+
return typeof server.provider === 'string' && typeof server.model === 'string' ? { provider: server.provider, model: server.model } : undefined;
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
aside = createAside({
|
|
649
|
+
agentFor,
|
|
650
|
+
fork: forkSession,
|
|
651
|
+
resume: async (sessionId) => {
|
|
652
|
+
await server.handleRequest('session/resume', { sessionId });
|
|
653
|
+
await state.ready(requireSession({ sessionId }).session);
|
|
654
|
+
},
|
|
655
|
+
create: async (sessionId) => {
|
|
656
|
+
const record = await server.getOrCreateSession(sessionId);
|
|
657
|
+
await state.ready(record.handle.agent.session);
|
|
658
|
+
},
|
|
659
|
+
inherit: async (source, _aside, asideId) => {
|
|
660
|
+
const selected = models.selectedFor(source) ?? source.session?.requestHeader?.()?.config;
|
|
661
|
+
if (typeof selected?.provider !== 'string' || typeof selected.model !== 'string') return;
|
|
662
|
+
if (selected.provider === server.provider && selected.model === server.model && selected.reasoningEffort === server.reasoningEffort) return;
|
|
663
|
+
try {
|
|
664
|
+
await models.methods['session/model/set']({
|
|
665
|
+
sessionId: asideId,
|
|
666
|
+
provider: selected.provider,
|
|
667
|
+
model: selected.model,
|
|
668
|
+
...(typeof selected.reasoningEffort === 'string' ? { reasoningEffort: selected.reasoningEffort } : {}),
|
|
669
|
+
});
|
|
670
|
+
} catch (error) {
|
|
671
|
+
ctx.logger?.warn?.(`Picasso aside kept the initialized model: ${error instanceof Error ? error.message : String(error)}`);
|
|
672
|
+
}
|
|
673
|
+
},
|
|
674
|
+
readOnly: (session) => {
|
|
675
|
+
try {
|
|
676
|
+
setSandboxMode(session, 'read-only');
|
|
677
|
+
} catch {
|
|
678
|
+
// the aside's tools still run under the session's own sandbox
|
|
679
|
+
}
|
|
680
|
+
},
|
|
681
|
+
discard: async (sessionId) => {
|
|
682
|
+
const record = server.sessions?.get(sessionId);
|
|
683
|
+
if (record !== undefined) {
|
|
684
|
+
server.sessions.delete(sessionId);
|
|
685
|
+
await record.handle.dispose();
|
|
686
|
+
}
|
|
687
|
+
await archive.remove(sessionId);
|
|
688
|
+
return removeSessionLogs(sessionId);
|
|
689
|
+
},
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
const extensions = {
|
|
693
|
+
async 'picasso/capabilities'(params) {
|
|
694
|
+
client.approvals = params?.client?.approvals === true;
|
|
695
|
+
client.questions = params?.client?.questions === true;
|
|
696
|
+
client.deltas = params?.client?.deltas === true;
|
|
697
|
+
return {
|
|
698
|
+
server: name,
|
|
699
|
+
methods: [...PICASSO_SDK_METHODS],
|
|
700
|
+
notifications: [SESSION_DELTA_METHOD, QUEUE_NOTIFICATION],
|
|
701
|
+
serverRequests: ['approval/request', 'question/request'],
|
|
702
|
+
client: { ...client },
|
|
703
|
+
};
|
|
704
|
+
},
|
|
705
|
+
async 'session/cancel'(params) {
|
|
706
|
+
const agent = requireSession(params);
|
|
707
|
+
agent.cancel({ kind: 'user' }, { keepInbox: params.keepInbox === true });
|
|
708
|
+
return {};
|
|
709
|
+
},
|
|
710
|
+
async 'session/fork'(params) {
|
|
711
|
+
// A new session seeded with the source log up to a boundary: rewinding
|
|
712
|
+
// the conversation to a turn is a fork at the end of the turn before it.
|
|
713
|
+
if (typeof params?.sessionId !== 'string' || typeof params.newSessionId !== 'string') {
|
|
714
|
+
throw new TypeError('session/fork requires sessionId and newSessionId');
|
|
715
|
+
}
|
|
716
|
+
return forkSession(params);
|
|
717
|
+
},
|
|
718
|
+
async 'approval/respond'(params) {
|
|
719
|
+
const pending = pendingAsks.get(params?.id);
|
|
720
|
+
if (pending === undefined || pending.kind !== 'approval') throw new Error(`no pending approval: ${params?.id}`);
|
|
721
|
+
pending.settle({ decision: params.decision });
|
|
722
|
+
return {};
|
|
723
|
+
},
|
|
724
|
+
...grants.methods,
|
|
725
|
+
async 'question/respond'(params) {
|
|
726
|
+
const pending = pendingAsks.get(params?.id);
|
|
727
|
+
if (pending === undefined || pending.kind !== 'question') throw new Error(`no pending question: ${params?.id}`);
|
|
728
|
+
pending.settle({ answers: params.answers ?? [] });
|
|
729
|
+
return {};
|
|
730
|
+
},
|
|
731
|
+
async 'command/list'(params) {
|
|
732
|
+
const agent = requireSession(params);
|
|
733
|
+
const commands = ctx.get?.('commands') ?? ctx.commands;
|
|
734
|
+
return { commands: commands?.list(agent) ?? [] };
|
|
735
|
+
},
|
|
736
|
+
async 'command/run'(params) {
|
|
737
|
+
const agent = requireSession(params);
|
|
738
|
+
await state.ready(agent.session);
|
|
739
|
+
const commands = ctx.get?.('commands') ?? ctx.commands;
|
|
740
|
+
if (commands === undefined) throw new Error('dsh-commands is not mounted');
|
|
741
|
+
const line = typeof params.line === 'string'
|
|
742
|
+
? params.line
|
|
743
|
+
: `/${String(params.name ?? '').replace(/^\//u, '')}${params.args ? ` ${params.args}` : ''}`;
|
|
744
|
+
const execution = await commands.execute(agent, line, [], AbortSignal.timeout(params.timeoutMs ?? 120000));
|
|
745
|
+
if (execution === undefined) throw new Error(`unknown command: ${line.split(/\s/u)[0]}`);
|
|
746
|
+
return { commandId: execution.commandId, result: execution.result };
|
|
747
|
+
},
|
|
748
|
+
async 'workspace/changes/get'(params) {
|
|
749
|
+
requireSession(params);
|
|
750
|
+
await state.hydrate(params.sessionId);
|
|
751
|
+
const seq = params.seq ?? changesSeq(params.sessionId, params.turn);
|
|
752
|
+
if (seq === undefined) return { summary: null };
|
|
753
|
+
return { seq, summary: await summaryFor(params.sessionId, seq) ?? null };
|
|
754
|
+
},
|
|
755
|
+
async 'workspace/changes/diff'(params) {
|
|
756
|
+
requireSession(params);
|
|
757
|
+
await state.hydrate(params.sessionId);
|
|
758
|
+
const seq = params.seq ?? changesSeq(params.sessionId, params.turn);
|
|
759
|
+
if (seq === undefined) return { diff: null };
|
|
760
|
+
const index = params.index ?? 0;
|
|
761
|
+
await flushSnapshots(params.sessionId);
|
|
762
|
+
// `index` addresses the visible (ignore-filtered) file list; dsh and the
|
|
763
|
+
// archive address their own lists, so the file is matched by path.
|
|
764
|
+
const file = (await summaryFor(params.sessionId, seq))?.files?.[index];
|
|
765
|
+
if (file === undefined) return { seq, diff: null };
|
|
766
|
+
const liveIndex = workspaceChanges().summary(params.sessionId, seq)?.files?.findIndex((entry) => entry.path === file.path) ?? -1;
|
|
767
|
+
const live = liveIndex < 0 ? undefined : await workspaceChanges().diff(params.sessionId, seq, liveIndex, AbortSignal.timeout(30000));
|
|
768
|
+
const archivedIndex = live === undefined ? (await archive.read(params.sessionId, seq))?.files?.findIndex((entry) => entry.path === file.path) ?? -1 : -1;
|
|
769
|
+
const diff = live ?? (archivedIndex < 0 ? undefined : await archive.diff(params.sessionId, seq, archivedIndex, snapshotObjectDir));
|
|
770
|
+
return { seq, diff: diff ?? null };
|
|
771
|
+
},
|
|
772
|
+
async 'workspace/restore'(params) {
|
|
773
|
+
// Restore the work tree to the snapshot dsh recorded before (or after) a
|
|
774
|
+
// turn. Snapshot trees live in workspace-changes' private object store,
|
|
775
|
+
// so git reads them through GIT_OBJECT_DIRECTORY + the repo's objects as
|
|
776
|
+
// an alternate. Only the work tree is written (`git restore --worktree`);
|
|
777
|
+
// HEAD, refs and the repository index are untouched. Files the turn
|
|
778
|
+
// created (absent from the snapshot) are removed. `paths`
|
|
779
|
+
// (workspace-relative files or directories) restricts the restore.
|
|
780
|
+
restorePathFilter(params?.paths);
|
|
781
|
+
const agent = requireSession(params);
|
|
782
|
+
if (agent.status !== 'idle') throw new Error('cannot restore the workspace while the session is running');
|
|
783
|
+
await state.hydrate(params.sessionId);
|
|
784
|
+
const seq = params.seq ?? changesSeq(params.sessionId, params.turn);
|
|
785
|
+
if (seq === undefined) throw new Error('no workspace snapshot recorded for that turn');
|
|
786
|
+
return restoreWorkspace({
|
|
787
|
+
sessionId: params.sessionId,
|
|
788
|
+
seq,
|
|
789
|
+
side: params.side,
|
|
790
|
+
summaryFor,
|
|
791
|
+
changeSeqs,
|
|
792
|
+
archive,
|
|
793
|
+
findObjects: snapshotObjectDir,
|
|
794
|
+
isIgnored,
|
|
795
|
+
only: params.paths,
|
|
796
|
+
});
|
|
797
|
+
},
|
|
798
|
+
...queue.methods,
|
|
799
|
+
...rewind.methods,
|
|
800
|
+
...models.methods,
|
|
801
|
+
...connectionMethods(() => ctx.get?.('modelRouter') ?? ctx.modelRouter),
|
|
802
|
+
...aside.methods,
|
|
803
|
+
...createPicassoRecords({ sessionFor: (sessionId) => requireSession({ sessionId }).session, appendPluginRecord }).methods,
|
|
804
|
+
...jobs.methods,
|
|
805
|
+
...schedules.methods,
|
|
806
|
+
...teamTasks.methods,
|
|
807
|
+
...goalControl.methods,
|
|
808
|
+
// `/context timeline`: the lean-context plugin's read-only timeline service (context-timeline.js).
|
|
809
|
+
...createContextTimeline({ timeline: () => ctx.get?.('contextTimeline') ?? undefined }).methods,
|
|
810
|
+
// `/instructions` and the `/context` section: `@picasso/instructions`' resolved sources (instructions.js).
|
|
811
|
+
...createInstructions({ instructions: () => ctx.get?.('instructions') ?? undefined }).methods,
|
|
812
|
+
// The engine updater's headless boot check: one throwaway agent, its tool schemas, no prompt (tools-list.js).
|
|
813
|
+
...createToolsList({
|
|
814
|
+
tools: () => ctx.get?.('tools') ?? ctx.tools,
|
|
815
|
+
openProbe: async (sessionId) => {
|
|
816
|
+
const record = await server.getOrCreateSession(sessionId);
|
|
817
|
+
return {
|
|
818
|
+
agent: record.handle.agent,
|
|
819
|
+
dispose: async () => {
|
|
820
|
+
server.sessions?.delete(sessionId);
|
|
821
|
+
await record.handle.dispose?.();
|
|
822
|
+
},
|
|
823
|
+
};
|
|
824
|
+
},
|
|
825
|
+
}).methods,
|
|
826
|
+
...egress.methods,
|
|
827
|
+
...fileInput.methods,
|
|
828
|
+
// A provider change made while this engine runs (the shared store): the route table, live (provider-reload.js).
|
|
829
|
+
...createProviderReload({
|
|
830
|
+
loader: () => ctx.get?.('loader') ?? ctx.loader,
|
|
831
|
+
loadOverlay: async (file) => (await import(dshPackageEntry('dsh-app-boot'))).loadOverlayPatches('dsh', file),
|
|
832
|
+
interpolate: async () => (await import(dshPackageEntry('cordis-plugin-loader'))).interpolate,
|
|
833
|
+
}).methods,
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
let exitTask;
|
|
837
|
+
const disposeAndExit = () => {
|
|
838
|
+
exitTask ??= (async () => {
|
|
839
|
+
await Promise.allSettled([Promise.resolve().then(() => transport.flush())]);
|
|
840
|
+
await Promise.allSettled([Promise.resolve().then(() => rootFiber.dispose())]);
|
|
841
|
+
exit(0);
|
|
842
|
+
})();
|
|
843
|
+
return exitTask;
|
|
844
|
+
};
|
|
845
|
+
|
|
846
|
+
transport.onRequest(async (method, params) => {
|
|
847
|
+
if (method === 'initialize') await ctx.get('loader')?.await();
|
|
848
|
+
if (method === 'session/queue') {
|
|
849
|
+
await assertImageInput({ llm: ctx.get?.('llm') ?? ctx.llm, route: routeFor(params?.sessionId), content: params?.content });
|
|
850
|
+
}
|
|
851
|
+
const extension = extensions[method];
|
|
852
|
+
if (extension !== undefined) return extension(params);
|
|
853
|
+
if (method === 'session/prompt') {
|
|
854
|
+
await assertImageInput({ llm: ctx.get?.('llm') ?? ctx.llm, route: routeFor(params?.sessionId), content: params?.contentBlocks });
|
|
855
|
+
const record = await server.getOrCreateSession(params.sessionId);
|
|
856
|
+
await state.ready(record.handle.agent.session);
|
|
857
|
+
}
|
|
858
|
+
if (method === 'shutdown') {
|
|
859
|
+
await aside.closeAll();
|
|
860
|
+
await Promise.all(snapshotTasks.values());
|
|
861
|
+
}
|
|
862
|
+
const result = await server.handleRequest(method, params);
|
|
863
|
+
if (method === 'session/resume') {
|
|
864
|
+
const resumed = requireSession(params).session;
|
|
865
|
+
await state.ready(resumed);
|
|
866
|
+
adoptAutoPreset(resumed);
|
|
867
|
+
}
|
|
868
|
+
if (method === 'session/wait') await flushSnapshots(params.sessionId);
|
|
869
|
+
if (method === 'shutdown') setImmediate(() => void disposeAndExit());
|
|
870
|
+
return result;
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
ctx.effect(() => {
|
|
874
|
+
transport.start();
|
|
875
|
+
return async () => {
|
|
876
|
+
for (const pending of pendingAsks.values()) pending.settle(undefined);
|
|
877
|
+
disposeEgress?.();
|
|
878
|
+
disposeMcpGate?.();
|
|
879
|
+
await egress.close?.();
|
|
880
|
+
await aside.closeAll();
|
|
881
|
+
await Promise.all(snapshotTasks.values());
|
|
882
|
+
await server.shutdown();
|
|
883
|
+
transport.close();
|
|
884
|
+
};
|
|
885
|
+
}, 'picasso-sdk.serve');
|
|
886
|
+
}
|