aiterm-mcp 0.61.19 → 0.61.21
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/CHANGELOG.md +38 -1
- package/README.ja.md +36 -4
- package/README.md +38 -5
- package/dist/agent-goal-native-result.js +172 -0
- package/dist/agent-goal-ui.js +50 -0
- package/dist/agent-goal.js +221 -0
- package/dist/codex-goal-connection.js +23 -0
- package/dist/codex-relay-client.js +3 -2
- package/dist/core.js +311 -10
- package/dist/execution-contract.js +1 -1
- package/dist/execution-dispatch.js +4 -1
- package/dist/execution-immediate-stop.js +77 -24
- package/dist/execution-native-server.js +9 -5
- package/dist/harnesses/claude-goal.js +243 -0
- package/dist/harnesses/codex-goal-editor.js +134 -0
- package/dist/harnesses/codex-goal.js +94 -0
- package/dist/harnesses/cursor.js +27 -0
- package/dist/harnesses/grok-goal.js +261 -0
- package/dist/harnesses/grok.js +25 -2
- package/dist/index.js +22 -0
- package/dist/process-runtime.js +18 -0
- package/dist/process-start.js +35 -0
- package/dist/runtime-error-os.js +2 -32
- package/docs/DESIGN.md +44 -2
- package/package.json +1 -1
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// Goal state belongs to the CLI. This module holds no goal store or loop.
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { executionLaunchSchema } from "./execution-contract.js";
|
|
4
|
+
export const goalActionSchema = z.enum(["inspect", "create", "edit", "pause", "resume", "clear"]);
|
|
5
|
+
export const agentGoalInputObject = z.object({
|
|
6
|
+
...executionLaunchSchema.shape, action: goalActionSchema,
|
|
7
|
+
expected_thread_id: z.string().min(1).max(256).optional(),
|
|
8
|
+
objective: z.string().trim().min(1).refine(value => [...value].length <= 4000, "objective exceeds 4000 characters").optional(),
|
|
9
|
+
token_budget: z.number().int().positive().optional(),
|
|
10
|
+
}).strict();
|
|
11
|
+
export const agentGoalInputSchema = agentGoalInputObject.superRefine((value, context) => {
|
|
12
|
+
if (value.action === "create" && value.objective === undefined)
|
|
13
|
+
context.addIssue({ code: "custom", message: "create requires objective" });
|
|
14
|
+
if (value.action === "edit" && value.objective === undefined && value.token_budget === undefined)
|
|
15
|
+
context.addIssue({ code: "custom", message: "edit requires objective or token_budget" });
|
|
16
|
+
if (!["create", "edit"].includes(value.action) && (value.objective !== undefined || value.token_budget !== undefined))
|
|
17
|
+
context.addIssue({ code: "custom", message: "fields do not apply to this action" });
|
|
18
|
+
});
|
|
19
|
+
export const GOAL_HOST_META_KEY = "aiterm/goal";
|
|
20
|
+
const hostOrigin = z.object({ origin: z.literal("user") }).strict();
|
|
21
|
+
/** Host provenance on the existing local MCP channel, not authentication.
|
|
22
|
+
* BellTeam must authenticate the Owner and reject seat ingress before stamping
|
|
23
|
+
* this metadata. A model client's metadata never upgrades its origin. */
|
|
24
|
+
export function goalOriginFromHost(clientName, meta) {
|
|
25
|
+
if (clientName !== "bellteam" || !meta || typeof meta !== "object")
|
|
26
|
+
return "automatic";
|
|
27
|
+
return hostOrigin.safeParse(meta[GOAL_HOST_META_KEY]).success ? "user" : "automatic";
|
|
28
|
+
}
|
|
29
|
+
export const nativeGoalSchema = z.object({
|
|
30
|
+
threadId: z.string().min(1), objective: z.string().min(1),
|
|
31
|
+
status: z.enum(["active", "paused", "blocked", "usageLimited", "budgetLimited", "complete"]),
|
|
32
|
+
tokenBudget: z.number().int().positive().nullable(), tokensUsed: z.number().int().nonnegative(),
|
|
33
|
+
timeUsedSeconds: z.number().nonnegative(), createdAt: z.number().nonnegative(), updatedAt: z.number().nonnegative(),
|
|
34
|
+
}).strict();
|
|
35
|
+
const goalFieldCapabilitiesSchema = z.object({
|
|
36
|
+
create: z.object({ objective: z.boolean(), token_budget: z.boolean() }).strict(),
|
|
37
|
+
edit: z.object({ objective: z.boolean(), token_budget: z.boolean(), combined: z.boolean().optional() }).strict(),
|
|
38
|
+
}).strict();
|
|
39
|
+
export const agentGoalResultSchema = z.object({
|
|
40
|
+
schema: z.literal("aiterm.agent-goal-result.v1"),
|
|
41
|
+
session_id: z.string(), launch_id: z.string(),
|
|
42
|
+
harness: z.enum(["claude-code", "codex-cli", "grok-cli", "cursor-cli"]).nullable(),
|
|
43
|
+
binding: z.object({ ...executionLaunchSchema.shape, thread_id: z.string().min(1) }).nullable(),
|
|
44
|
+
availability: z.enum(["available", "unsupported", "unavailable"]),
|
|
45
|
+
capabilities: z.object({ inspect: z.boolean(), create: z.boolean(), edit: z.boolean(), pause: z.boolean(), resume: z.boolean(), clear: z.boolean() }),
|
|
46
|
+
field_capabilities: goalFieldCapabilitiesSchema.optional(),
|
|
47
|
+
outcome: z.enum(["observed", "applied", "rejected", "outcome_unknown", "unavailable", "unsupported"]),
|
|
48
|
+
mutation: z.enum(["not_requested", "not_issued", "acknowledged", "rejected", "outcome_unknown"]),
|
|
49
|
+
readback: z.enum(["confirmed", "not_attempted", "failed", "mismatch"]),
|
|
50
|
+
// Missing means unknown. null is only an authoritative native empty result.
|
|
51
|
+
goal: nativeGoalSchema.nullable().optional(),
|
|
52
|
+
reason: z.string(), observed_at: z.string(),
|
|
53
|
+
}).strict();
|
|
54
|
+
export function sameGoalTarget(current, target) {
|
|
55
|
+
return !!current && !!target && current.session_id === target.session_id && current.launch_id === target.launch_id
|
|
56
|
+
&& current.harness === target.harness && current.vendor_session_id === target.vendor_session_id
|
|
57
|
+
&& current.codex_home === target.codex_home && current.grok_home === target.grok_home && current.cwd === target.cwd && current.executable === target.executable
|
|
58
|
+
&& JSON.stringify(current.native_process) === JSON.stringify(target.native_process);
|
|
59
|
+
}
|
|
60
|
+
export class AgentGoalConnectionError extends Error {
|
|
61
|
+
availability;
|
|
62
|
+
reason;
|
|
63
|
+
constructor(availability, reason) {
|
|
64
|
+
super(reason);
|
|
65
|
+
this.availability = availability;
|
|
66
|
+
this.reason = reason;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Raised only by a trusted input owner that proved no goal submission. */
|
|
70
|
+
export class AgentGoalNotIssuedError extends Error {
|
|
71
|
+
reason;
|
|
72
|
+
outcome;
|
|
73
|
+
constructor(reason, outcome = "rejected") {
|
|
74
|
+
super(reason);
|
|
75
|
+
this.reason = reason;
|
|
76
|
+
this.outcome = outcome;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const capabilities = (enabled) => ({ inspect: enabled, create: enabled, edit: enabled, pause: enabled, resume: enabled, clear: enabled });
|
|
80
|
+
export async function manageAgentGoal(input, runtime, origin = "automatic") {
|
|
81
|
+
input = agentGoalInputSchema.parse(input);
|
|
82
|
+
const result = { schema: "aiterm.agent-goal-result.v1", session_id: input.session_id,
|
|
83
|
+
launch_id: input.launch_id, harness: null, binding: null, availability: "unavailable", capabilities: capabilities(false),
|
|
84
|
+
outcome: "unavailable", mutation: input.action === "inspect" ? "not_requested" : "not_issued",
|
|
85
|
+
readback: "not_attempted", reason: "binding_unavailable", observed_at: new Date().toISOString() };
|
|
86
|
+
let target;
|
|
87
|
+
try {
|
|
88
|
+
target = runtime.resolve();
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
const same = () => sameGoalTarget(runtime.resolve(), target);
|
|
94
|
+
if (!target || target.session_id !== input.session_id || target.launch_id !== input.launch_id)
|
|
95
|
+
return result;
|
|
96
|
+
result.harness = target.harness;
|
|
97
|
+
if (target.harness !== "codex-cli")
|
|
98
|
+
return { ...result, availability: "unsupported", outcome: "unsupported", reason: "native_goal_adapter_not_implemented" };
|
|
99
|
+
if (!target.vendor_session_id || !target.codex_home)
|
|
100
|
+
return { ...result, reason: "thread_binding_unavailable" };
|
|
101
|
+
if (input.expected_thread_id !== undefined && input.expected_thread_id !== target.vendor_session_id)
|
|
102
|
+
return { ...result, reason: "thread_binding_mismatch" };
|
|
103
|
+
const threadId = target.vendor_session_id;
|
|
104
|
+
let issued = false;
|
|
105
|
+
try {
|
|
106
|
+
return await runtime.connect(target, async (request, supported = capabilities(true), fields) => {
|
|
107
|
+
const read = async () => {
|
|
108
|
+
const value = z.object({ goal: nativeGoalSchema.nullable() }).parse(await request("thread/goal/get", { threadId }));
|
|
109
|
+
if (value.goal && value.goal.threadId !== threadId)
|
|
110
|
+
throw new Error("goal_thread_mismatch");
|
|
111
|
+
return value.goal;
|
|
112
|
+
};
|
|
113
|
+
if (!same())
|
|
114
|
+
return { ...result, reason: "generation_replaced" };
|
|
115
|
+
const before = await read();
|
|
116
|
+
if (!same())
|
|
117
|
+
return { ...result, reason: "generation_replaced" };
|
|
118
|
+
Object.assign(result, { availability: "available", capabilities: supported, goal: before,
|
|
119
|
+
binding: { session_id: target.session_id, launch_id: target.launch_id, thread_id: threadId },
|
|
120
|
+
readback: "confirmed", outcome: "observed", reason: "native_goal_observed" });
|
|
121
|
+
result.field_capabilities = fields ?? {
|
|
122
|
+
create: { objective: supported.create, token_budget: supported.create },
|
|
123
|
+
edit: { objective: supported.edit, token_budget: supported.edit },
|
|
124
|
+
};
|
|
125
|
+
if (input.action === "inspect")
|
|
126
|
+
return result;
|
|
127
|
+
if (!supported[input.action])
|
|
128
|
+
return { ...result, outcome: "unsupported", reason: "native_goal_operation_unsupported" };
|
|
129
|
+
if (input.action === "create" || input.action === "edit") {
|
|
130
|
+
const supportedFields = result.field_capabilities[input.action];
|
|
131
|
+
if ((input.objective !== undefined && !supportedFields.objective)
|
|
132
|
+
|| (input.token_budget !== undefined && !supportedFields.token_budget)
|
|
133
|
+
|| (input.action === "edit" && input.objective !== undefined && input.token_budget !== undefined
|
|
134
|
+
&& result.field_capabilities.edit.combined === false))
|
|
135
|
+
return { ...result, outcome: "unsupported", reason: "native_goal_field_unsupported" };
|
|
136
|
+
}
|
|
137
|
+
// The host may establish user provenance at authenticated UI ingress.
|
|
138
|
+
// Model tool arguments cannot select or upgrade their origin.
|
|
139
|
+
const reject = (reason) => ({ ...result, outcome: "rejected", reason });
|
|
140
|
+
if (origin !== "user")
|
|
141
|
+
return reject("user_authorization_required");
|
|
142
|
+
if (input.action === "create" && before !== null)
|
|
143
|
+
return reject("goal_already_exists");
|
|
144
|
+
if (input.action !== "create" && input.action !== "clear" && before === null)
|
|
145
|
+
return reject("goal_missing");
|
|
146
|
+
if (input.action === "resume" && before?.status !== "paused")
|
|
147
|
+
return reject("resume_requires_paused_goal");
|
|
148
|
+
if (input.action === "pause" && before?.status !== "active")
|
|
149
|
+
return reject("pause_requires_active_goal");
|
|
150
|
+
if (!same())
|
|
151
|
+
return { ...result, availability: "unavailable", outcome: "unavailable", reason: "generation_replaced" };
|
|
152
|
+
const method = input.action === "clear" ? "thread/goal/clear" : "thread/goal/set";
|
|
153
|
+
const params = { threadId, origin };
|
|
154
|
+
if (input.objective !== undefined)
|
|
155
|
+
params.objective = input.objective;
|
|
156
|
+
if (input.token_budget !== undefined)
|
|
157
|
+
params.tokenBudget = input.token_budget;
|
|
158
|
+
if (input.action === "create" || input.action === "resume")
|
|
159
|
+
params.status = "active";
|
|
160
|
+
if (input.action === "pause")
|
|
161
|
+
params.status = "paused";
|
|
162
|
+
issued = true;
|
|
163
|
+
delete result.goal;
|
|
164
|
+
result.readback = "not_attempted";
|
|
165
|
+
let response;
|
|
166
|
+
try {
|
|
167
|
+
response = await request(method, params);
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
if (!(error instanceof AgentGoalNotIssuedError))
|
|
171
|
+
throw error;
|
|
172
|
+
issued = false;
|
|
173
|
+
result.mutation = "not_issued";
|
|
174
|
+
// UI preparation may have taken time. Do not restore a stale snapshot.
|
|
175
|
+
try {
|
|
176
|
+
const current = await read();
|
|
177
|
+
if (!same())
|
|
178
|
+
throw new Error("generation_replaced");
|
|
179
|
+
return { ...result, goal: current, outcome: error.outcome, readback: "confirmed", reason: error.reason };
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return { ...result, outcome: error.outcome, readback: "failed", reason: error.reason };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (input.action === "clear")
|
|
186
|
+
z.object({ cleared: z.boolean() }).parse(response);
|
|
187
|
+
else {
|
|
188
|
+
const ack = z.object({ goal: nativeGoalSchema }).parse(response);
|
|
189
|
+
if (ack.goal.threadId !== threadId)
|
|
190
|
+
throw new Error("goal_thread_mismatch");
|
|
191
|
+
}
|
|
192
|
+
result.mutation = "acknowledged";
|
|
193
|
+
if (!same())
|
|
194
|
+
return { ...result, outcome: "outcome_unknown", readback: "failed", reason: "generation_replaced_after_mutation" };
|
|
195
|
+
try {
|
|
196
|
+
const after = await read();
|
|
197
|
+
if (!same())
|
|
198
|
+
return { ...result, outcome: "outcome_unknown", readback: "failed", reason: "generation_replaced_after_readback" };
|
|
199
|
+
result.goal = after;
|
|
200
|
+
const matches = input.action === "clear" ? after === null : after !== null
|
|
201
|
+
&& (input.objective === undefined || after.objective === input.objective)
|
|
202
|
+
&& (input.token_budget === undefined || after.tokenBudget === input.token_budget)
|
|
203
|
+
&& (params.status === undefined || after.status === params.status);
|
|
204
|
+
return { ...result, outcome: matches ? "applied" : "outcome_unknown", readback: matches ? "confirmed" : "mismatch",
|
|
205
|
+
reason: matches ? "native_goal_readback_confirmed" : "native_goal_changed_before_readback", observed_at: new Date().toISOString() };
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
return { ...result, outcome: "outcome_unknown", readback: "failed", reason: "native_goal_readback_failed" };
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
if (issued)
|
|
214
|
+
return { ...result, outcome: "outcome_unknown", mutation: result.mutation === "acknowledged" ? "acknowledged" : "outcome_unknown",
|
|
215
|
+
readback: "failed", reason: "native_goal_mutation_unconfirmed" };
|
|
216
|
+
if (error instanceof AgentGoalConnectionError)
|
|
217
|
+
return { ...result, availability: error.availability,
|
|
218
|
+
outcome: error.availability, capabilities: capabilities(false), reason: error.reason };
|
|
219
|
+
return { ...result, availability: "unavailable", outcome: "unavailable", capabilities: capabilities(false), reason: "native_goal_connection_unavailable" };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Official native control rendezvous. Resolve its protected socket alias;
|
|
2
|
+
// never create a directory, socket, worker, or daemon when one is missing.
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { verifyRelaySocket } from "./codex-relay-config.js";
|
|
6
|
+
import { AgentGoalConnectionError } from "./agent-goal.js";
|
|
7
|
+
export function nativeCodexGoalSocket(home) {
|
|
8
|
+
if (process.platform === "win32")
|
|
9
|
+
throw new AgentGoalConnectionError("unsupported", "windows_native_goal_transport_not_implemented");
|
|
10
|
+
if (!path.isAbsolute(home))
|
|
11
|
+
throw new Error("Codex home must be absolute");
|
|
12
|
+
const directory = path.join(home, "app-server-control");
|
|
13
|
+
const parent = fs.lstatSync(directory);
|
|
14
|
+
if (!parent.isDirectory() || parent.uid !== process.getuid?.() || (parent.mode & 0o022) !== 0)
|
|
15
|
+
throw new Error("Unverified native control directory");
|
|
16
|
+
const advertised = path.join(directory, "app-server-control.sock");
|
|
17
|
+
const entry = fs.lstatSync(advertised);
|
|
18
|
+
if (entry.uid !== process.getuid?.() || (!entry.isSymbolicLink() && !entry.isSocket()))
|
|
19
|
+
throw new Error("Unverified native control socket");
|
|
20
|
+
const socket = fs.realpathSync(advertised);
|
|
21
|
+
verifyRelaySocket(socket);
|
|
22
|
+
return socket;
|
|
23
|
+
}
|
|
@@ -4,7 +4,7 @@ import { CodexDeliveryError } from "./codex-delivery-error.js";
|
|
|
4
4
|
import { verifyRelaySocket } from "./codex-relay-config.js";
|
|
5
5
|
import { windowsSocketConnection } from "./windows-codex-connection.js";
|
|
6
6
|
/** 公式受付へ追加接続する。Desktopへの承認要求や通知には応答しない。 */
|
|
7
|
-
export async function withCodexRelay(socketPath, action, timeout = 15_000) {
|
|
7
|
+
export async function withCodexRelay(socketPath, action, timeout = 15_000, options = {}) {
|
|
8
8
|
let connection;
|
|
9
9
|
try {
|
|
10
10
|
if (process.platform === "win32")
|
|
@@ -81,7 +81,8 @@ export async function withCodexRelay(socketPath, action, timeout = 15_000) {
|
|
|
81
81
|
socket.once("error", () => reject(new CodexDeliveryError("CODEX_RELAY_UNAVAILABLE", "公式App Serverへ接続できません")));
|
|
82
82
|
socket.once("close", () => reject(new CodexDeliveryError("CODEX_RELAY_UNAVAILABLE", "公式App Serverが接続を閉じました")));
|
|
83
83
|
});
|
|
84
|
-
await request("initialize", { clientInfo: { name: "aiterm_parent_delivery", version: "1" }
|
|
84
|
+
await request("initialize", { clientInfo: { name: "aiterm_parent_delivery", version: "1" },
|
|
85
|
+
...(options.experimentalApi ? { capabilities: { experimentalApi: true } } : {}) });
|
|
85
86
|
socket.send(JSON.stringify({ method: "initialized" }));
|
|
86
87
|
return await action(request);
|
|
87
88
|
}
|