@0perator/operator 0.1.222 → 0.1.261
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/README.md +6 -5
- package/dashboard/server.mjs +1 -1
- package/dist/acp/agent.js +320 -53
- package/dist/acp/auth.js +44 -0
- package/dist/acp/config-options.js +34 -0
- package/dist/acp/prompt.js +41 -0
- package/dist/acp/run.js +116 -10
- package/dist/acp/tool-map.js +48 -0
- package/dist/auth/byo.js +125 -0
- package/dist/auth/client.js +12 -6
- package/dist/auth/commands.js +71 -3
- package/dist/auth/identity.js +136 -0
- package/dist/auth/store.js +7 -5
- package/dist/autonomy/board.js +3 -2
- package/dist/autonomy/cron.js +332 -0
- package/dist/autonomy/delegate.js +168 -0
- package/dist/autonomy/dream.js +1 -1
- package/dist/autonomy/index.js +16 -2
- package/dist/autonomy/media-robot.js +137 -0
- package/dist/autonomy/profile.js +77 -0
- package/dist/autonomy/routines.js +5 -4
- package/dist/autonomy/session-factory.js +19 -10
- package/dist/background/index.js +8 -1
- package/dist/browser/cdp.js +266 -0
- package/dist/browser/session.js +286 -0
- package/dist/cli.js +207 -106
- package/dist/compose/client.js +56 -0
- package/dist/config/brand.js +3 -3
- package/dist/config/home.js +70 -0
- package/dist/config/managed.js +230 -0
- package/dist/config/persona.js +243 -21
- package/dist/config/scaffold.js +142 -0
- package/dist/config/settings.js +87 -20
- package/dist/config/skills-index.js +175 -0
- package/dist/consolidation/scheduler.js +10 -7
- package/dist/consolidation/sleep.js +17 -3
- package/dist/engine/agent.js +231 -21
- package/dist/engine/aurora.js +79 -14
- package/dist/engine/batch-scheduler.js +128 -0
- package/dist/engine/compaction.js +129 -2
- package/dist/engine/computer-event.js +139 -0
- package/dist/engine/continuation.js +55 -0
- package/dist/engine/contract.js +3 -1
- package/dist/engine/grandmaster.js +13 -3
- package/dist/engine/json-repair.js +105 -0
- package/dist/engine/messages.js +52 -5
- package/dist/engine/redact.js +25 -0
- package/dist/engine/rolling-summary.js +28 -11
- package/dist/engine/router-adapter.js +2 -0
- package/dist/engine/unrestricted.js +51 -0
- package/dist/engine/verify-gate.js +6 -0
- package/dist/enterprise/cli.js +137 -0
- package/dist/flywheel/processor.js +30 -2
- package/dist/flywheel/secret-gate.js +123 -0
- package/dist/index.browser.js +4 -0
- package/dist/index.js +4 -0
- package/dist/mcp/index.js +18 -24
- package/dist/memory/index.js +0 -0
- package/dist/memory/session-index.js +324 -0
- package/dist/memory/session-rewind.js +92 -0
- package/dist/memory/state-maintenance.js +441 -0
- package/dist/observability/index.js +3 -3
- package/dist/permissions/audit.js +4 -1
- package/dist/permissions/index.js +24 -12
- package/dist/permissions/policy.js +4 -3
- package/dist/permissions/tier.js +31 -14
- package/dist/remote/host.js +9 -2
- package/dist/router/models.js +62 -2
- package/dist/selfimprove/index.js +9 -7
- package/dist/session.js +129 -42
- package/dist/skills/catalogue.js +167 -0
- package/dist/telemetry/audit-verify.js +94 -0
- package/dist/telemetry/events.js +92 -7
- package/dist/telemetry/lifecycle.js +18 -0
- package/dist/tools/computer.js +1 -1
- package/dist/tools/deferred.js +117 -0
- package/dist/tools/executor.js +27 -23
- package/dist/tools/memory.js +117 -0
- package/dist/tools/mutating.js +6 -0
- package/dist/tools/registry.js +1022 -46
- package/dist/tools/sandbox.js +63 -17
- package/dist/tools/searxng.js +2 -2
- package/dist/tools/shell-session.js +36 -1
- package/dist/tools/tool-output-spill.js +4 -4
- package/dist/ui/ActivitySpinner.js +17 -6
- package/dist/ui/App.js +36 -14
- package/dist/ui/ArtifactPicker.js +109 -0
- package/dist/ui/Composer.js +39 -8
- package/dist/ui/Markdown.js +5 -3
- package/dist/ui/MouseProvider.js +99 -19
- package/dist/ui/QuestionPrompt.js +24 -3
- package/dist/ui/ThinkingSpinner.js +9 -5
- package/dist/ui/ToolStep.js +17 -12
- package/dist/ui/Transcript.js +28 -9
- package/dist/ui/capabilities.js +0 -9
- package/dist/ui/listhit.js +23 -0
- package/dist/ui/mouse.js +22 -0
- package/dist/ui/osc.js +7 -4
- package/dist/ui/progressbar.js +46 -0
- package/dist/ui/shimmer.js +43 -0
- package/dist/ui/theme.js +2 -0
- package/dist/ui/tick.js +48 -0
- package/dist/ui/toolicons.js +69 -0
- package/dist/ui/useInputHistory.js +3 -3
- package/dist/util/slash.js +1 -0
- package/dist/vault/cli.js +48 -0
- package/dist/vault/store.js +157 -0
- package/dist/vault/tool.js +50 -0
- package/package.json +3 -5
- package/CHANGELOG.md +0 -1629
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Operator
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
AXE's agentic terminal coding agent: a TypeScript + [Ink](https://github.com/vadimdemedes/ink) app backed by
|
|
4
|
+
the Aurora model, built to the frozen `SPEC/` contract shared with the Python core.
|
|
5
5
|
Launched as **`operator`** (1M-only) / **`operator+`** (Air⇄1M) — AXE's agentic terminal, built on the Aurora model.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Its reason to exist: a **full-frame React reconciler + Ink `<Static>`** render, which makes the
|
|
8
8
|
scroll / echo / prompt-overwrite bug class of the ANSI-cursor Python REPL *structurally impossible*.
|
|
9
9
|
|
|
10
10
|
## Architecture
|
|
@@ -59,7 +59,8 @@ operator # 1M-only
|
|
|
59
59
|
operator+ --resume # reload the last conversation
|
|
60
60
|
```
|
|
61
61
|
Requires Aurora serving locally (Air `:8098` / 1M `:8097`). Dev: `npm install && npm run dev`.
|
|
62
|
-
Env: `KODE_AIR`, `KODE_1M`, `KODE_SADDLE`,
|
|
62
|
+
Env (the internal `KODE_` prefix is retained for compatibility): `KODE_AIR`, `KODE_1M`, `KODE_SADDLE`,
|
|
63
|
+
`KODE_TSI_ROUTER=1m`, `KODE_SYSTEM_PROMPT`, `KODE_SESSION_FILE`.
|
|
63
64
|
|
|
64
65
|
## Testing
|
|
65
66
|
|
package/dashboard/server.mjs
CHANGED
|
@@ -48,7 +48,7 @@ const DENIALS_MAX = 500;
|
|
|
48
48
|
|
|
49
49
|
const KINDS = new Set(["tool", "turn", "control", "service"]);
|
|
50
50
|
const TIERS = new Set(["admin", "enterprise", "readonly"]);
|
|
51
|
-
const SURFACES = new Set(["cli", "web", "mobile"]);
|
|
51
|
+
const SURFACES = new Set(["cli", "web", "mobile", "acp"]);
|
|
52
52
|
|
|
53
53
|
function validateEvent(e) {
|
|
54
54
|
if (!e || typeof e !== "object") return "not an object";
|
package/dist/acp/agent.js
CHANGED
|
@@ -3,101 +3,368 @@
|
|
|
3
3
|
// layer: an ACP `session/prompt` becomes `Session.send()`, and the session's streaming callbacks become ACP
|
|
4
4
|
// `session/update` notifications. Nothing re-implements the agent loop — like the remote-control host, ACP
|
|
5
5
|
// funnels into the one `runTurn` (via Session), so compaction, loop-guard, and the verify gate come for free.
|
|
6
|
+
//
|
|
7
|
+
// Enterprise posture (see _ai-docs/decisions/2026-10-07-acp-hardening.md):
|
|
8
|
+
// - tool approvals under gated/enterprise policy become `session/request_permission` (fail-closed);
|
|
9
|
+
// - each session is isolated: its own root (no process.chdir), rolling summary, shell, audit identity;
|
|
10
|
+
// - protocol errors carry proper JSON-RPC codes; a session can't be driven by two prompts at once;
|
|
11
|
+
// - sessions are torn down when the editor detaches (no orphaned shells / MCP servers).
|
|
6
12
|
import { randomUUID } from "node:crypto";
|
|
7
|
-
import {
|
|
13
|
+
import { readFileSync, rmSync, statSync } from "node:fs";
|
|
14
|
+
import { isAbsolute } from "node:path";
|
|
15
|
+
import { PROTOCOL_VERSION, RequestError } from "@zed-industries/agent-client-protocol";
|
|
8
16
|
import { createAutonomySession } from "../autonomy/session-factory.js";
|
|
17
|
+
import { modelRegistry, visibleModels } from "../router/models.js";
|
|
18
|
+
import { hasByoToken } from "../auth/byo.js";
|
|
19
|
+
import { operatorPath } from "../config/home.js";
|
|
20
|
+
import { redactSecrets } from "../engine/redact.js";
|
|
21
|
+
import { EventSink } from "../telemetry/events.js";
|
|
22
|
+
import { PermissionPolicy } from "../permissions/index.js";
|
|
23
|
+
import { signatureLine } from "../permissions/signature.js";
|
|
24
|
+
import { allowSandboxWriteRoot } from "../tools/sandbox.js";
|
|
25
|
+
import { buildModelConfigOption, configOptionUpdate } from "./config-options.js";
|
|
26
|
+
import { ACP_AUTH_METHOD, ACP_AUTH_METHOD_ID, defaultAcpAuth } from "./auth.js";
|
|
27
|
+
import { promptToText } from "./prompt.js";
|
|
28
|
+
import { capResult, resultStatus, toolKind, toolLocations, toolTitle } from "./tool-map.js";
|
|
29
|
+
export const defaultSessionFactory = (settings, o) => createAutonomySession(settings, {
|
|
30
|
+
cwd: o.cwd,
|
|
31
|
+
extra: { sessionId: o.sessionId, approve: o.approve, eventSink: o.eventSink, eventActor: o.eventActor, rollingSummaryPath: o.rollingSummaryPath },
|
|
32
|
+
});
|
|
33
|
+
const ALLOW_ONCE = "allow_once";
|
|
34
|
+
const REJECT_ONCE = "reject_once";
|
|
35
|
+
const DEFAULT_MAX_SESSIONS = 32;
|
|
36
|
+
const DEFAULT_MAX_PROMPT_CHARS = 2_000_000;
|
|
37
|
+
const PERMISSION_OPTIONS = [
|
|
38
|
+
{ optionId: ALLOW_ONCE, name: "Allow", kind: "allow_once" },
|
|
39
|
+
{ optionId: REJECT_ONCE, name: "Reject", kind: "reject_once" },
|
|
40
|
+
];
|
|
41
|
+
function agentVersion() {
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return "0.0.0";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function envInt(name, fallback) {
|
|
50
|
+
const n = Number(process.env[name]);
|
|
51
|
+
return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback;
|
|
52
|
+
}
|
|
53
|
+
/** Correlates a tool call the engine announced (onToolStart, with its id) to the (name,args) pair the
|
|
54
|
+
* permission wrapper later asks approval for — ApproveFn carries no call id, and the editor wants the
|
|
55
|
+
* approval attached to the card it already shows, not a second phantom one. */
|
|
56
|
+
export class ToolCallIndex {
|
|
57
|
+
byKey = new Map();
|
|
58
|
+
static key(name, args) { return `${name}\u0000${JSON.stringify(args ?? {})}`; }
|
|
59
|
+
start(id, name, args) {
|
|
60
|
+
const k = ToolCallIndex.key(name, args);
|
|
61
|
+
const q = this.byKey.get(k);
|
|
62
|
+
if (q)
|
|
63
|
+
q.push(id);
|
|
64
|
+
else
|
|
65
|
+
this.byKey.set(k, [id]);
|
|
66
|
+
}
|
|
67
|
+
idFor(name, args) {
|
|
68
|
+
return this.byKey.get(ToolCallIndex.key(name, args))?.[0];
|
|
69
|
+
}
|
|
70
|
+
settle(id) {
|
|
71
|
+
for (const [k, q] of this.byKey) {
|
|
72
|
+
const i = q.indexOf(id);
|
|
73
|
+
if (i >= 0) {
|
|
74
|
+
q.splice(i, 1);
|
|
75
|
+
if (!q.length)
|
|
76
|
+
this.byKey.delete(k);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
clear() { this.byKey.clear(); }
|
|
82
|
+
}
|
|
9
83
|
export class OperatorAcpAgent {
|
|
10
84
|
conn;
|
|
11
85
|
settings;
|
|
86
|
+
sessionFactory;
|
|
12
87
|
sessions = new Map();
|
|
13
|
-
|
|
88
|
+
auth;
|
|
89
|
+
sink;
|
|
90
|
+
actorOverride;
|
|
91
|
+
maxSessions;
|
|
92
|
+
maxPromptChars;
|
|
93
|
+
subject;
|
|
94
|
+
inflight = 0;
|
|
95
|
+
constructor(conn, settings, sessionFactory = defaultSessionFactory, opts = {}) {
|
|
14
96
|
this.conn = conn;
|
|
15
97
|
this.settings = settings;
|
|
98
|
+
this.sessionFactory = sessionFactory;
|
|
99
|
+
this.auth = opts.auth ?? defaultAcpAuth(settings, { requireSignIn: settings.managed?.auth?.requireSignIn });
|
|
100
|
+
this.sink = opts.sink ?? new EventSink();
|
|
101
|
+
this.actorOverride = opts.actor;
|
|
102
|
+
this.maxSessions = opts.maxSessions ?? envInt("KODE_ACP_MAX_SESSIONS", DEFAULT_MAX_SESSIONS);
|
|
103
|
+
this.maxPromptChars = opts.maxPromptChars ?? envInt("KODE_ACP_MAX_PROMPT_CHARS", DEFAULT_MAX_PROMPT_CHARS);
|
|
16
104
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
105
|
+
// Read live from `settings`, not frozen at construction: a sign-in through `authenticate` re-resolves the
|
|
106
|
+
// seat (tier, subject) on the shared settings object, and the audit trail must follow it.
|
|
107
|
+
get actor() {
|
|
108
|
+
return { tier: this.settings.permissions?.tier ?? "admin", surface: "acp", sub: this.subject ?? this.settings.seat?.sub, ...this.actorOverride };
|
|
109
|
+
}
|
|
110
|
+
get audits() { return new PermissionPolicy(this.settings.permissions ?? {}).audits; }
|
|
111
|
+
async initialize(params) {
|
|
112
|
+
// Spec: answer the client's version when we speak it, else our latest. We speak exactly PROTOCOL_VERSION.
|
|
113
|
+
void params;
|
|
114
|
+
// loadSession is not supported (each `session/new` is a fresh context); embeddedContext lets the editor
|
|
115
|
+
// inline file refs. The advertised auth method appears ONLY when this deployment needs a sign-in — a
|
|
116
|
+
// loopback/LAN engine keeps the empty list and zero ceremony.
|
|
20
117
|
return {
|
|
21
118
|
protocolVersion: PROTOCOL_VERSION,
|
|
22
119
|
agentCapabilities: {
|
|
23
120
|
loadSession: false,
|
|
24
121
|
promptCapabilities: { image: false, audio: false, embeddedContext: true },
|
|
25
122
|
},
|
|
26
|
-
authMethods:
|
|
123
|
+
authMethods: this.auth.required() ? [{ ...ACP_AUTH_METHOD }] : [],
|
|
124
|
+
// Extra field: the SDK validates REQUESTS only. Zed shows this as the agent's name/version.
|
|
125
|
+
agentInfo: { name: "operator", title: "Operator", version: agentVersion() },
|
|
27
126
|
};
|
|
28
127
|
}
|
|
29
128
|
async newSession(params) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
129
|
+
const cwd = params.cwd;
|
|
130
|
+
if (typeof cwd !== "string" || !isAbsolute(cwd)) {
|
|
131
|
+
throw RequestError.invalidParams({ field: "cwd", reason: "cwd must be an absolute path" });
|
|
132
|
+
}
|
|
133
|
+
let isDir = false;
|
|
134
|
+
try {
|
|
135
|
+
isDir = statSync(cwd).isDirectory();
|
|
136
|
+
}
|
|
137
|
+
catch { /* falls to the error below */ }
|
|
138
|
+
if (!isDir)
|
|
139
|
+
throw RequestError.invalidParams({ field: "cwd", reason: "cwd is not an existing directory" });
|
|
140
|
+
if (this.sessions.size >= this.maxSessions) {
|
|
141
|
+
throw RequestError.invalidRequest({ reason: `session limit reached (${this.maxSessions}); close an editor thread first` });
|
|
142
|
+
}
|
|
143
|
+
if (this.auth.required()) {
|
|
144
|
+
const st = await this.auth.check(false);
|
|
145
|
+
if (!st.ok)
|
|
146
|
+
throw RequestError.authRequired({ authMethods: [ACP_AUTH_METHOD], reason: "sign in with `operator auth login`, then retry" });
|
|
147
|
+
this.subject = st.subject ?? this.subject;
|
|
148
|
+
}
|
|
149
|
+
// Spawning a client-named command is a security-sensitive surface we have not wired; say so rather
|
|
150
|
+
// than silently dropping the editor's tool config.
|
|
151
|
+
if (params.mcpServers?.length) {
|
|
152
|
+
process.stderr.write(`[operator-acp] ignoring ${params.mcpServers.length} client-supplied MCP server(s): not supported yet\n`);
|
|
153
|
+
}
|
|
154
|
+
const sid = randomUUID();
|
|
155
|
+
const auditId = `acp-${sid.slice(0, 12)}`;
|
|
156
|
+
const summaryPath = operatorPath("session-memory", "acp", `${sid}.md`);
|
|
157
|
+
const live = { sid, auditId, cwd, summaryPath, busy: false, cancelled: false, calls: new ToolCallIndex(), pending: new Set(), releaseRoot: allowSandboxWriteRoot(cwd) };
|
|
158
|
+
try {
|
|
159
|
+
live.session = this.sessionFactory(this.settings, {
|
|
160
|
+
cwd, sessionId: auditId, rollingSummaryPath: summaryPath,
|
|
161
|
+
approve: this.approver(live),
|
|
162
|
+
eventSink: this.sink,
|
|
163
|
+
eventActor: this.actor,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
catch (e) {
|
|
167
|
+
live.releaseRoot();
|
|
168
|
+
throw e;
|
|
169
|
+
}
|
|
170
|
+
this.sessions.set(sid, live);
|
|
171
|
+
this.audit(auditId, { event: "session_start", detail: `surface=acp tier=${this.actor.tier} mode=${this.settings.permissions?.mode ?? "bypass"} cwd=${cwd}` });
|
|
172
|
+
// GW-40: the stabilized configOptions surface (Zed dropdown for new clients; the SDK-0.4.5 legacy
|
|
173
|
+
// set_model below for current ones). Extra field on the response — the SDK validates REQUESTS only.
|
|
174
|
+
return { sessionId: sid, configOptions: [this.modelOption(sid)] };
|
|
175
|
+
}
|
|
176
|
+
/** The live model selector for a session — honest (MOA listed only if configured), current value
|
|
177
|
+
* read from the Session itself so a /model-style switch elsewhere is reflected here too. */
|
|
178
|
+
modelOption(sessionId) {
|
|
179
|
+
const live = this.sessions.get(sessionId);
|
|
180
|
+
const moa = this.settings.moa;
|
|
181
|
+
return buildModelConfigOption(visibleModels(modelRegistry(this.settings.models), hasByoToken), live?.session.currentModel() ?? "", !!moa?.proposers?.length);
|
|
182
|
+
}
|
|
183
|
+
/** SDK-0.4.5 legacy path (wire: session/set_model) — what Zed ships today. The spec-stable
|
|
184
|
+
* session/set_config_option is translated onto this method by the run.ts shim; both client
|
|
185
|
+
* generations end up here, one switch, one source of truth (Session.setModel + its MOA guard). */
|
|
186
|
+
async setSessionModel(params) {
|
|
187
|
+
const live = this.live(params.sessionId);
|
|
188
|
+
const r = live.session.setModel(params.modelId);
|
|
189
|
+
if (!r.ok)
|
|
190
|
+
throw new RequestError(-32602, r.error ?? `unknown model ${params.modelId}`, { modelId: params.modelId });
|
|
191
|
+
// Spec: the agent notifies with the COMPLETE config state after its own changes.
|
|
192
|
+
void this.conn.sessionUpdate({
|
|
193
|
+
sessionId: params.sessionId,
|
|
194
|
+
update: configOptionUpdate([this.modelOption(params.sessionId)]),
|
|
195
|
+
}).catch(() => { });
|
|
196
|
+
return {};
|
|
197
|
+
}
|
|
198
|
+
/** Shim answer for session/set_config_option: the spec response is the full updated options. */
|
|
199
|
+
currentConfigOptions(sessionId) {
|
|
200
|
+
return { configOptions: [this.modelOption(sessionId)] };
|
|
201
|
+
}
|
|
202
|
+
/** Local deployments advertise no auth method, so this is a no-op ack (as before). When sign-in IS
|
|
203
|
+
* required, it re-checks the stored credential online — the editor calls it after the user has run
|
|
204
|
+
* `operator auth login` in a terminal — and adopts the token for the already-running engine. */
|
|
205
|
+
async authenticate(params) {
|
|
206
|
+
if (!this.auth.required())
|
|
207
|
+
return {};
|
|
208
|
+
if (params.methodId !== ACP_AUTH_METHOD_ID) {
|
|
209
|
+
throw RequestError.invalidParams({ field: "methodId", reason: `unknown auth method ${params.methodId}` });
|
|
210
|
+
}
|
|
211
|
+
const st = await this.auth.check(true);
|
|
212
|
+
if (!st.ok)
|
|
213
|
+
throw RequestError.authRequired({ authMethods: [ACP_AUTH_METHOD], reason: "no valid credential — run `operator auth login`" });
|
|
214
|
+
this.subject = st.subject ?? this.subject;
|
|
38
215
|
return {};
|
|
39
216
|
}
|
|
40
217
|
async prompt(params) {
|
|
218
|
+
const live = this.live(params.sessionId);
|
|
219
|
+
if (live.busy) {
|
|
220
|
+
throw RequestError.invalidRequest({ sessionId: params.sessionId, reason: "a prompt turn is already in flight for this session; send session/cancel first" });
|
|
221
|
+
}
|
|
222
|
+
const text = promptToText(params.prompt, live.cwd);
|
|
223
|
+
if (!text.trim())
|
|
224
|
+
throw RequestError.invalidParams({ field: "prompt", reason: "prompt has no usable text content" });
|
|
225
|
+
if (text.length > this.maxPromptChars) {
|
|
226
|
+
throw RequestError.invalidParams({ field: "prompt", reason: `prompt exceeds ${this.maxPromptChars} characters` });
|
|
227
|
+
}
|
|
228
|
+
live.busy = true;
|
|
229
|
+
live.cancelled = false;
|
|
230
|
+
this.inflight++;
|
|
231
|
+
const handlers = acpHandlers((update) => void this.conn.sessionUpdate({ sessionId: live.sid, update: update }).catch(() => { }), { calls: live.calls, cwd: live.cwd });
|
|
232
|
+
try {
|
|
233
|
+
const outcome = await live.session.send(text, handlers);
|
|
234
|
+
return { stopReason: live.cancelled ? "cancelled" : acpStopReason(outcome) };
|
|
235
|
+
}
|
|
236
|
+
catch (e) {
|
|
237
|
+
if (live.cancelled)
|
|
238
|
+
return { stopReason: "cancelled" };
|
|
239
|
+
const message = redactSecrets(e instanceof Error ? e.message : String(e));
|
|
240
|
+
process.stderr.write(`[operator-acp] turn failed (${live.auditId}): ${message}\n`);
|
|
241
|
+
throw RequestError.internalError({ message });
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
live.busy = false;
|
|
245
|
+
live.calls.clear();
|
|
246
|
+
this.inflight--;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async cancel(params) {
|
|
41
250
|
const live = this.sessions.get(params.sessionId);
|
|
42
251
|
if (!live)
|
|
43
|
-
|
|
44
|
-
|
|
252
|
+
return;
|
|
253
|
+
live.cancelled = true;
|
|
254
|
+
this.settleApprovals(live, false); // spec: the client answers a pending permission request "cancelled"; don't depend on it
|
|
255
|
+
live.session.interrupt?.();
|
|
256
|
+
}
|
|
257
|
+
/** Editor detached (stdin closed) or the process is going down: stop every turn, deny every open
|
|
258
|
+
* approval, and tear each session down so no shell / MCP server / summary file outlives the link. */
|
|
259
|
+
dispose(reason = "client_closed") {
|
|
260
|
+
for (const live of [...this.sessions.values()])
|
|
261
|
+
this.endSession(live, reason);
|
|
262
|
+
}
|
|
263
|
+
/** Resolve once every cancelled turn has settled (or after `timeoutMs`) so their final frames reach the pipe. */
|
|
264
|
+
async drain(timeoutMs) {
|
|
265
|
+
const deadline = Date.now() + timeoutMs;
|
|
266
|
+
while (this.inflight > 0 && Date.now() < deadline)
|
|
267
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
268
|
+
}
|
|
269
|
+
endSession(live, reason) {
|
|
270
|
+
this.sessions.delete(live.sid);
|
|
271
|
+
live.cancelled = true;
|
|
272
|
+
this.settleApprovals(live, false);
|
|
45
273
|
try {
|
|
46
|
-
|
|
47
|
-
process.chdir(live.cwd);
|
|
274
|
+
live.session.interrupt?.();
|
|
48
275
|
}
|
|
49
|
-
catch { /*
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
276
|
+
catch { /* best-effort */ }
|
|
277
|
+
try {
|
|
278
|
+
live.session.shutdown?.();
|
|
279
|
+
}
|
|
280
|
+
catch { /* best-effort */ }
|
|
281
|
+
live.releaseRoot();
|
|
282
|
+
try {
|
|
283
|
+
rmSync(live.summaryPath, { force: true });
|
|
284
|
+
}
|
|
285
|
+
catch { /* derived artifact; nothing to keep */ }
|
|
286
|
+
this.audit(live.auditId, { event: "session_end", detail: reason });
|
|
54
287
|
}
|
|
55
|
-
|
|
56
|
-
|
|
288
|
+
settleApprovals(live, allow) {
|
|
289
|
+
for (const settle of [...live.pending])
|
|
290
|
+
settle(allow);
|
|
291
|
+
live.pending.clear();
|
|
292
|
+
}
|
|
293
|
+
live(sessionId) {
|
|
294
|
+
const live = this.sessions.get(sessionId);
|
|
295
|
+
if (!live)
|
|
296
|
+
throw RequestError.invalidParams({ field: "sessionId", sessionId, reason: "unknown session" });
|
|
297
|
+
return live;
|
|
298
|
+
}
|
|
299
|
+
audit(session, control) {
|
|
300
|
+
if (!this.audits)
|
|
301
|
+
return; // inert-by-default: an unenforced owner session writes nothing
|
|
302
|
+
this.sink.emit({ kind: "control", session, actor: this.actor, control });
|
|
303
|
+
}
|
|
304
|
+
/** The permission bridge: a "gated" mutating call (or any "ask") becomes `session/request_permission`.
|
|
305
|
+
* FAIL-CLOSED — anything other than an explicit "Allow" (cancel, reject, error, an option we never
|
|
306
|
+
* offered, a hung-up client) is a denial, and a cancel/teardown denies whatever is still open. */
|
|
307
|
+
approver(live) {
|
|
308
|
+
return (name, args) => new Promise((resolve) => {
|
|
309
|
+
if (live.cancelled)
|
|
310
|
+
return resolve(false);
|
|
311
|
+
const settle = (allow) => { live.pending.delete(settle); resolve(allow); };
|
|
312
|
+
live.pending.add(settle);
|
|
313
|
+
const line = signatureLine(name, args);
|
|
314
|
+
this.conn.requestPermission({
|
|
315
|
+
sessionId: live.sid,
|
|
316
|
+
toolCall: {
|
|
317
|
+
toolCallId: live.calls.idFor(name, args) ?? `perm-${randomUUID()}`,
|
|
318
|
+
title: `${line ? `[human sign-off: ${line}] ` : ""}${toolTitle(name, args)}`,
|
|
319
|
+
kind: toolKind(name),
|
|
320
|
+
status: "pending",
|
|
321
|
+
rawInput: args,
|
|
322
|
+
locations: toolLocations(args, live.cwd),
|
|
323
|
+
},
|
|
324
|
+
options: PERMISSION_OPTIONS,
|
|
325
|
+
}).then((r) => settle(r?.outcome?.outcome === "selected" && r.outcome.optionId === ALLOW_ONCE), () => settle(false));
|
|
326
|
+
});
|
|
57
327
|
}
|
|
58
328
|
}
|
|
329
|
+
/** ACP stop reason for a settled turn: a model that hit its output cap or a content filter says so instead
|
|
330
|
+
* of masquerading as a clean end_turn. */
|
|
331
|
+
export function acpStopReason(outcome) {
|
|
332
|
+
if (outcome.interrupted)
|
|
333
|
+
return "cancelled";
|
|
334
|
+
if (outcome.stopReason === "length")
|
|
335
|
+
return "max_tokens";
|
|
336
|
+
if (outcome.stopReason === "content_filter")
|
|
337
|
+
return "refusal";
|
|
338
|
+
return "end_turn";
|
|
339
|
+
}
|
|
59
340
|
/** Map Operator's turn callbacks to ACP session/update payloads. `emit` sends one update. Exported so the
|
|
60
341
|
* mapping can be unit-tested without a live connection or model. */
|
|
61
|
-
export function acpHandlers(emit) {
|
|
342
|
+
export function acpHandlers(emit, ctx = {}) {
|
|
62
343
|
return {
|
|
63
344
|
onDelta: (chunk) => emit({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: chunk } }),
|
|
64
345
|
onReasoning: (chunk) => emit({ sessionUpdate: "agent_thought_chunk", content: { type: "text", text: chunk } }),
|
|
65
346
|
onToolStart: (calls) => {
|
|
66
347
|
for (const c of calls) {
|
|
67
|
-
|
|
348
|
+
ctx.calls?.start(c.id, c.name, c.args);
|
|
349
|
+
emit({
|
|
350
|
+
sessionUpdate: "tool_call",
|
|
351
|
+
toolCallId: c.id,
|
|
352
|
+
title: toolTitle(c.name, c.args),
|
|
353
|
+
kind: toolKind(c.name),
|
|
354
|
+
status: "in_progress",
|
|
355
|
+
rawInput: c.args,
|
|
356
|
+
locations: toolLocations(c.args, ctx.cwd),
|
|
357
|
+
});
|
|
68
358
|
}
|
|
69
359
|
},
|
|
70
360
|
onToolResult: (id, result) => {
|
|
361
|
+
ctx.calls?.settle(id);
|
|
71
362
|
emit({
|
|
72
363
|
sessionUpdate: "tool_call_update",
|
|
73
364
|
toolCallId: id,
|
|
74
|
-
status:
|
|
75
|
-
content: [{ type: "content", content: { type: "text", text: result } }],
|
|
365
|
+
status: resultStatus(result),
|
|
366
|
+
content: [{ type: "content", content: { type: "text", text: capResult(result) } }],
|
|
76
367
|
});
|
|
77
368
|
},
|
|
78
369
|
};
|
|
79
370
|
}
|
|
80
|
-
/** Flatten ACP prompt content blocks to the plain text Operator's Session.send expects. Text blocks pass
|
|
81
|
-
* through; a resource_link / embedded resource becomes an `@path` mention the agent can act on. */
|
|
82
|
-
function promptToText(blocks) {
|
|
83
|
-
return blocks
|
|
84
|
-
.map((b) => {
|
|
85
|
-
if (b.type === "text")
|
|
86
|
-
return b.text;
|
|
87
|
-
if (b.type === "resource_link")
|
|
88
|
-
return `@${b.uri}`;
|
|
89
|
-
if (b.type === "resource") {
|
|
90
|
-
const r = b.resource;
|
|
91
|
-
return r.text ?? (r.uri ? `@${r.uri}` : "");
|
|
92
|
-
}
|
|
93
|
-
return "";
|
|
94
|
-
})
|
|
95
|
-
.filter(Boolean)
|
|
96
|
-
.join("\n");
|
|
97
|
-
}
|
|
98
|
-
/** A human-readable tool_call title for the editor's tool timeline: "file_edit: src/x.ts", "bash: npm test". */
|
|
99
|
-
function toolTitle(c) {
|
|
100
|
-
const a = (c.args ?? {});
|
|
101
|
-
const hint = a.path ?? a.file_path ?? a.command ?? a.query ?? a.url ?? a.pattern;
|
|
102
|
-
return hint ? `${c.name}: ${String(hint).slice(0, 80)}` : c.name;
|
|
103
|
-
}
|
package/dist/acp/auth.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// ACP authentication seam. The ACP link itself is a local stdio pipe (the OS process boundary is the trust
|
|
2
|
+
// boundary), so a loopback/LAN engine needs no sign-in. What DOES need one is an engine fronted by the AXE
|
|
3
|
+
// gateway (…axe.onl): without the operator bearer every prompt would 401 mid-turn. Surfacing that at
|
|
4
|
+
// `session/new` as ACP's `auth_required` lets the editor tell the user up front, and `authenticate` lets a
|
|
5
|
+
// login done AFTER the agent process started (the usual order: editor spawns us, user signs in in a
|
|
6
|
+
// terminal) take effect without restarting the agent.
|
|
7
|
+
import { loadCredentials } from "../auth/store.js";
|
|
8
|
+
import { verifyToken } from "../auth/client.js";
|
|
9
|
+
import { resolveSeat } from "../auth/identity.js";
|
|
10
|
+
import { requiresOperatorToken, setOperatorToken } from "../engine/aurora.js";
|
|
11
|
+
import { applySeat, setActiveSeat } from "../config/settings.js";
|
|
12
|
+
export const ACP_AUTH_METHOD_ID = "operator-login";
|
|
13
|
+
export const ACP_AUTH_METHOD = {
|
|
14
|
+
id: ACP_AUTH_METHOD_ID,
|
|
15
|
+
name: "Sign in to Operator",
|
|
16
|
+
description: "Run `operator auth login` in a terminal (email code or browser), then retry.",
|
|
17
|
+
};
|
|
18
|
+
export function defaultAcpAuth(settings, opts = {}) {
|
|
19
|
+
return {
|
|
20
|
+
required: () => !!opts.requireSignIn || requiresOperatorToken(settings.endpoints?.aurora?.base),
|
|
21
|
+
async check(verify) {
|
|
22
|
+
const env = process.env.KODE_OPERATOR_TOKEN;
|
|
23
|
+
const creds = env ? { token: env, user: undefined } : await loadCredentials().catch(() => null);
|
|
24
|
+
if (!creds?.token)
|
|
25
|
+
return { ok: false };
|
|
26
|
+
// A managed sign-in requirement that resolved to a lockdown (expired / foreign-domain / rejected) is not "signed in".
|
|
27
|
+
if (!verify)
|
|
28
|
+
return opts.requireSignIn && settings.seat?.source === "lockdown" ? { ok: false } : { ok: true, subject: creds.user };
|
|
29
|
+
const account = await verifyToken(creds.token);
|
|
30
|
+
if (!account)
|
|
31
|
+
return { ok: false };
|
|
32
|
+
setOperatorToken(creds.token);
|
|
33
|
+
// The seat was resolved at process start, possibly as a signed-out lockdown. Re-resolve it now that
|
|
34
|
+
// there is an identity, on the SHARED settings object, so sessions created from here on get the tier
|
|
35
|
+
// this identity is entitled to (and the audit trail names it).
|
|
36
|
+
const { seat } = await resolveSeat(settings, { creds });
|
|
37
|
+
setActiveSeat(seat);
|
|
38
|
+
Object.assign(settings, applySeat(settings, seat));
|
|
39
|
+
if (opts.requireSignIn && seat.source === "lockdown")
|
|
40
|
+
return { ok: false };
|
|
41
|
+
return { ok: true, subject: account.external_id };
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** The honest model selector: only profiles Session.setModel would ACCEPT today are listed —
|
|
2
|
+
* MOA is omitted unless a moa block exists in settings (setModel refuses it otherwise, and a
|
|
3
|
+
* dropdown that can fail is the dishonesty SPEC/providers.md §1 bans). Descriptions carry ctx
|
|
4
|
+
* size and kind so the user can price a switch with their eyes. */
|
|
5
|
+
export function buildModelConfigOption(registry, currentId, moaAvailable) {
|
|
6
|
+
const options = [];
|
|
7
|
+
for (const p of registry) {
|
|
8
|
+
if (p.kind === "orchestrated" && !moaAvailable)
|
|
9
|
+
continue;
|
|
10
|
+
const bits = [p.desc, p.kind === "single" && p.ctx ? `${Math.round(p.ctx / 1000)}k ctx` : null, p.kind === "orchestrated" ? "mixture-of-agents" : null].filter(Boolean);
|
|
11
|
+
options.push({ value: p.id, name: p.label, description: bits.join(" · ") || undefined });
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
id: "model",
|
|
15
|
+
name: "Model",
|
|
16
|
+
category: "model",
|
|
17
|
+
type: "select",
|
|
18
|
+
currentValue: options.some((o) => o.value === currentId) ? currentId : options[0]?.value ?? currentId,
|
|
19
|
+
options,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Request-frame translation: session/set_config_option{sessionId,configId,value} ->
|
|
23
|
+
* session/set_model{sessionId,modelId}. Returns the rewritten params when configId is the model
|
|
24
|
+
* selector; null otherwise (mode-style options are not claimed — honesty over fake support). */
|
|
25
|
+
export function translateSetConfigOption(params) {
|
|
26
|
+
const p = params;
|
|
27
|
+
if (!p || typeof p.sessionId !== "string" || p.configId !== "model" || typeof p.value !== "string")
|
|
28
|
+
return null;
|
|
29
|
+
return { sessionId: p.sessionId, modelId: p.value };
|
|
30
|
+
}
|
|
31
|
+
/** Agent-side update notification shape (spec: complete config state). */
|
|
32
|
+
export function configOptionUpdate(options) {
|
|
33
|
+
return { sessionUpdate: "config_option_update", configOptions: options };
|
|
34
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// ACP prompt content blocks -> the plain text Session.send expects. A resource_link / embedded resource
|
|
2
|
+
// becomes a labelled `@path` mention the agent can act on; file:// URIs are turned into paths (relative to
|
|
3
|
+
// the session root when inside it) because that is the form the file tools and the model both understand.
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { isAbsolute, relative } from "node:path";
|
|
6
|
+
export function uriToMention(uri, cwd) {
|
|
7
|
+
if (!uri.startsWith("file://"))
|
|
8
|
+
return `@${uri}`;
|
|
9
|
+
let p;
|
|
10
|
+
try {
|
|
11
|
+
p = fileURLToPath(uri);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return `@${uri}`;
|
|
15
|
+
}
|
|
16
|
+
if (cwd) {
|
|
17
|
+
const rel = relative(cwd, p);
|
|
18
|
+
if (rel && !rel.startsWith("..") && !isAbsolute(rel))
|
|
19
|
+
return `@${rel}`;
|
|
20
|
+
}
|
|
21
|
+
return `@${p}`;
|
|
22
|
+
}
|
|
23
|
+
export function promptToText(blocks, cwd) {
|
|
24
|
+
return blocks
|
|
25
|
+
.map((b) => {
|
|
26
|
+
if (b.type === "text")
|
|
27
|
+
return b.text;
|
|
28
|
+
if (b.type === "resource_link")
|
|
29
|
+
return uriToMention(b.uri, cwd);
|
|
30
|
+
if (b.type === "resource") {
|
|
31
|
+
const r = b.resource;
|
|
32
|
+
const label = r.uri ? uriToMention(r.uri, cwd) : "";
|
|
33
|
+
if (typeof r.text === "string")
|
|
34
|
+
return label ? `${label}\n${r.text}` : r.text;
|
|
35
|
+
return label;
|
|
36
|
+
}
|
|
37
|
+
return "";
|
|
38
|
+
})
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.join("\n");
|
|
41
|
+
}
|