omp-antigravity-acp-provider 0.1.34 → 0.1.36
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 +21 -0
- package/THIRD_PARTY_LICENSES.txt +2 -2
- package/extension/acp-errors.ts +118 -0
- package/extension/agent-notices.ts +83 -0
- package/extension/commands.ts +3 -3
- package/extension/context-settings.ts +1 -0
- package/extension/index.ts +5 -4
- package/extension/permission-identity.ts +183 -0
- package/extension/runtime.mjs +37234 -36613
- package/extension/tier-connection.ts +108 -8
- package/extension/tool-relay-protocol.mjs +19 -0
- package/extension/tool-relay.ts +228 -0
- package/extension/tool-routing.ts +3 -10
- package/extension/usage-db.ts +205 -0
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -23,6 +23,9 @@ This is an independent adaptation, **NOT** an official Google or OMP integration
|
|
|
23
23
|
- Lazy ACP startup and warm session reuse.
|
|
24
24
|
- `/login` plus `/antigravity-acp` account, usage and cleanup commands.
|
|
25
25
|
- API-equivalent cost estimates, not subscription billing.
|
|
26
|
+
- Long-running OMP tools survive the MCP request window through a relay. The model resumes the same result with `pi_omp_await_result`; the control tool is a separate MCP server and never appears in OMP's tool list.
|
|
27
|
+
- Permission requests are tied to the originating session, turn and tool call. Forged, conflicting or previous-turn identities fail closed, while normal requests still use OMP's human review.
|
|
28
|
+
- The MCP `tools/list` reflects the current OMP tool set. The offline wire test covers add, remove and restore on one warm ACP session.
|
|
26
29
|
|
|
27
30
|
## Install: for human
|
|
28
31
|
|
|
@@ -87,6 +90,7 @@ This shows the runtime, native-tool mode and usage state. Send a short prompt to
|
|
|
87
90
|
| Login opens the wrong Google account | Both login paths always start a fresh sign-in. Choose the intended account (or "Use another account") on Google's consent screen. |
|
|
88
91
|
| `disabled` native-tool mode rejects the session | The official runtime must advertise system-append support (1.4.1+). Upgrade the runtime or run `/antigravity-acp native-tools prefer`. |
|
|
89
92
|
| The first request is slow | Runtime download and cold ACP start happen once; later turns reuse the warm session. |
|
|
93
|
+
| A tool call returns a relay notice | The model should call `pi_omp_await_result` with the notice's `call_id`. Lower `OMP_ACP_TOOL_RELAY_MS` or set it to `0` to disable relay. |
|
|
90
94
|
|
|
91
95
|
## Install: for agents
|
|
92
96
|
|
|
@@ -109,12 +113,29 @@ The installation runbook is in [AGENT_INSTALL.md](AGENT_INSTALL.md). Hand that f
|
|
|
109
113
|
| `/antigravity-acp debug on\|off` | ACP tool-status lines |
|
|
110
114
|
| `/antigravity-acp permissions default\|auto-edit\|yolo` | Permission mode |
|
|
111
115
|
|
|
116
|
+
Supplemental `/usage` queries are off by default. Context occupancy still comes from ACP `usage_update`. Run `/antigravity-acp usage auto` to opt into the extra query.
|
|
117
|
+
|
|
112
118
|
## Steering and queued messages
|
|
113
119
|
|
|
114
120
|
By default a new user message waits for the active ACP turn to finish (`OMP_ACP_STEER_MODE=queue`) and then continues on the warm session, avoiding a full replay and OMP auto-compaction. Set `OMP_ACP_STEER_MODE=steer` to restore immediate interruption. At tool/permission boundaries the adapter falls back to steer and waits for the ACP terminal response; `OMP_ACP_STEER_GRACE_MS` (default 60000) and `OMP_ACP_STEER_GRACE_MAX_MS` (default 300000) bound that wait.
|
|
115
121
|
|
|
122
|
+
ACP runtimes stay warm for 15 minutes by default. Set `OMP_ACP_IDLE_TIMEOUT_MS=0` to keep them until OMP exits, or lower the value to reclaim memory sooner.
|
|
123
|
+
|
|
124
|
+
An ACP turn that ends at the model's output limit is continued on the same session up to three times. `OMP_ACP_MAX_CONTINUATIONS=0` disables the loop; if agy already reported its own output-cap continuation, the adapter does not add another.
|
|
125
|
+
|
|
116
126
|
Inference and `/antigravity-acp refresh` refuse to start ACP when no local Google login is available; run `/login` (or `/antigravity-acp login`) first instead of waiting for an interactive consent flow inside `session/new`.
|
|
117
127
|
|
|
128
|
+
## Long tools and relay
|
|
129
|
+
|
|
130
|
+
A tool call that outlives `OMP_ACP_TOOL_RELAY_MS` (default 85000, `0` disables, otherwise 5000 through 165000) returns a relay notice instead of holding the MCP request open. The model calls `pi_omp_await_result` with the notice's `call_id`; the adapter returns the same original result, not a second execution. The control tool is a separate MCP server and is not added to OMP's tool list. If the ACP turn ends before the result arrives, the adapter retries the same result in a fresh ACP turn; it never delivers a stale result to a new writer.
|
|
131
|
+
|
|
132
|
+
Permission requests are correlated by session, turn and `toolCallId` with structured `mcpServerName`/`mcpToolName` metadata. A conflicting update, a previous-turn id, or a permission-before-update that claims MCP identity is rejected with `reject_once` when available, otherwise `cancelled`. Normal requests still go through OMP's human review.
|
|
133
|
+
|
|
134
|
+
The bridge's MCP `tools/list` always reflects the current OMP tool set. The offline wire test covers add, remove and restore on one warm ACP session. A live agent re-list has not been verified against the official agy runtime, so the adapter does not claim that the agent will always see a changed list.
|
|
135
|
+
|
|
136
|
+
A `refusal` stop reason is reported as an explicit provider refusal. An empty `end_turn` remains tolerated to preserve OMP's warm `/retry` path.
|
|
137
|
+
|
|
138
|
+
|
|
118
139
|
## Limitations vs direct OAuth
|
|
119
140
|
|
|
120
141
|
- No direct model API endpoint or reverse-proxy mode.
|
package/THIRD_PARTY_LICENSES.txt
CHANGED
|
@@ -1046,7 +1046,7 @@ IN THE SOFTWARE.
|
|
|
1046
1046
|
|
|
1047
1047
|
============================================================
|
|
1048
1048
|
|
|
1049
|
-
typebox@1.3.
|
|
1049
|
+
typebox@1.3.36
|
|
1050
1050
|
License: MIT
|
|
1051
1051
|
|
|
1052
1052
|
license
|
|
@@ -1076,7 +1076,7 @@ THE SOFTWARE.
|
|
|
1076
1076
|
|
|
1077
1077
|
============================================================
|
|
1078
1078
|
|
|
1079
|
-
typebox@1.3.
|
|
1079
|
+
typebox@1.3.27
|
|
1080
1080
|
License: MIT
|
|
1081
1081
|
|
|
1082
1082
|
license
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// OMP decides whether to retry from the assistant error text. Keep the
|
|
2
|
+
// transport's distinction between account exhaustion and a transient throttle
|
|
3
|
+
// visible instead of collapsing both into "protocol".
|
|
4
|
+
export type AcpFailureCode =
|
|
5
|
+
| "auth"
|
|
6
|
+
| "quota"
|
|
7
|
+
| "rate_limit"
|
|
8
|
+
| "context"
|
|
9
|
+
| "timeout"
|
|
10
|
+
| "server"
|
|
11
|
+
| "transport"
|
|
12
|
+
| "protocol"
|
|
13
|
+
| "model"
|
|
14
|
+
| "refusal"
|
|
15
|
+
| "aborted";
|
|
16
|
+
|
|
17
|
+
export class AcpFailure extends Error {
|
|
18
|
+
constructor(readonly code: AcpFailureCode, message: string, options?: ErrorOptions) {
|
|
19
|
+
super(message, options);
|
|
20
|
+
this.name = "AcpFailure";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface FailureRule {
|
|
25
|
+
code: Exclude<AcpFailureCode, "aborted">;
|
|
26
|
+
match: RegExp;
|
|
27
|
+
ensure: RegExp;
|
|
28
|
+
append: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const AUTH_RULE: FailureRule = {
|
|
32
|
+
code: "auth",
|
|
33
|
+
match: /authentication required|not logged in|not signed in|unauthenticated|sign in|invalid_grant|401 unauthorized/i,
|
|
34
|
+
ensure: /run `agy`|terminal/i,
|
|
35
|
+
append: "run `agy` once in a terminal to sign in",
|
|
36
|
+
};
|
|
37
|
+
const QUOTA_RULE: FailureRule = {
|
|
38
|
+
code: "quota",
|
|
39
|
+
match: /quota|resource.?exhausted|limit reached|exceeded your|insufficient_quota|out of budget|billing/i,
|
|
40
|
+
ensure: /quota exceeded|insufficient_quota|billing/i,
|
|
41
|
+
append: "quota exceeded",
|
|
42
|
+
};
|
|
43
|
+
const RATE_LIMIT_RULE: FailureRule = {
|
|
44
|
+
code: "rate_limit",
|
|
45
|
+
match: /rate.?limit|429|too many requests|overloaded|capacity|server_busy|high demand/i,
|
|
46
|
+
ensure: /rate.?limit|429|too many requests/i,
|
|
47
|
+
append: "rate limit",
|
|
48
|
+
};
|
|
49
|
+
const CONTEXT_RULE: FailureRule = {
|
|
50
|
+
code: "context",
|
|
51
|
+
match: /context (?:window|length|limit)|context_length_exceeded|too long|token limit|maximum.*tokens|prompt is too long/i,
|
|
52
|
+
ensure: /context|token limit|too long/i,
|
|
53
|
+
append: "context window exceeded",
|
|
54
|
+
};
|
|
55
|
+
const TIMEOUT_RULE: FailureRule = {
|
|
56
|
+
code: "timeout",
|
|
57
|
+
match: /timeout|timed out|deadline/i,
|
|
58
|
+
ensure: /timeout|timed out/i,
|
|
59
|
+
append: "timeout",
|
|
60
|
+
};
|
|
61
|
+
const PROTOCOL_RULE: FailureRule = {
|
|
62
|
+
code: "protocol",
|
|
63
|
+
match: /ownership was lost|unexpected permission mode|already running|already in progress|protocol/i,
|
|
64
|
+
ensure: /protocol|ownership|permission mode|already/i,
|
|
65
|
+
append: "protocol error",
|
|
66
|
+
};
|
|
67
|
+
const SERVER_RULE: FailureRule = {
|
|
68
|
+
code: "server",
|
|
69
|
+
match: /internal.?error|server.?error|5\d\d|service.?(?:is.?)?(?:temporarily.?)?unavailable/i,
|
|
70
|
+
ensure: /server|5\d\d|unavailable/i,
|
|
71
|
+
append: "server error",
|
|
72
|
+
};
|
|
73
|
+
const TRANSPORT_RULE: FailureRule = {
|
|
74
|
+
code: "transport",
|
|
75
|
+
match: /network|connection|fetch failed|socket|stream ended|closed|ENOTFOUND|EAI_AGAIN|broken pipe|EPIPE|failed to bind|process exited/i,
|
|
76
|
+
ensure: /connection|network|socket|stream|bind|process/i,
|
|
77
|
+
append: "connection error",
|
|
78
|
+
};
|
|
79
|
+
const RULES: FailureRule[] = [
|
|
80
|
+
AUTH_RULE,
|
|
81
|
+
QUOTA_RULE,
|
|
82
|
+
RATE_LIMIT_RULE,
|
|
83
|
+
CONTEXT_RULE,
|
|
84
|
+
TIMEOUT_RULE,
|
|
85
|
+
PROTOCOL_RULE,
|
|
86
|
+
SERVER_RULE,
|
|
87
|
+
TRANSPORT_RULE,
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
function appendHint(message: string, rule: FailureRule): string {
|
|
91
|
+
return rule.ensure.test(message) ? message : `${message} (${rule.append})`;
|
|
92
|
+
}
|
|
93
|
+
interface AntigravityAcpErrorLike extends Error {
|
|
94
|
+
name: "AntigravityAcpError";
|
|
95
|
+
code: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function isAntigravityAcpError(error: unknown): error is AntigravityAcpErrorLike {
|
|
99
|
+
return error instanceof Error && error.name === "AntigravityAcpError"
|
|
100
|
+
&& typeof (error as { code?: unknown }).code === "string";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function classifyAcpFailure(error: unknown): Error {
|
|
104
|
+
if (isAntigravityAcpError(error)) {
|
|
105
|
+
if (error.code === "aborted") return error;
|
|
106
|
+
if (error.code === "auth") return new AcpFailure("auth", appendHint(error.message, AUTH_RULE), { cause: error });
|
|
107
|
+
if (error.code === "timeout") return new AcpFailure("timeout", appendHint(error.message, TIMEOUT_RULE), { cause: error });
|
|
108
|
+
if (error.code === "spawn" || error.code === "process_exit") return new AcpFailure("transport", appendHint(error.message, TRANSPORT_RULE), { cause: error });
|
|
109
|
+
if (error.code === "invalid_input") return new AcpFailure("protocol", appendHint(error.message, PROTOCOL_RULE), { cause: error });
|
|
110
|
+
if (error.code === "model") return new AcpFailure("model", error.message, { cause: error });
|
|
111
|
+
}
|
|
112
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
113
|
+
if (!message.trim()) return error instanceof Error ? error : new Error(message);
|
|
114
|
+
for (const rule of RULES) {
|
|
115
|
+
if (rule.match.test(message)) return new AcpFailure(rule.code, appendHint(message, rule), { cause: error });
|
|
116
|
+
}
|
|
117
|
+
return error instanceof Error ? error : new Error(message);
|
|
118
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// agy streams transport retries and its own output-cap continuation notice as
|
|
2
|
+
// assistant text. They are diagnostics, not model output, so keep them out of
|
|
3
|
+
// OMP's transcript without dropping a chunk that only looks like a prefix.
|
|
4
|
+
const AGENT_RETRY_NOTICE = /^\s*⚠\s*API error \(attempt \d+\)/i;
|
|
5
|
+
const OUTPUT_LIMIT_HEAD = "your previous response was cut off because it exceeded the output token limit";
|
|
6
|
+
const OUTPUT_LIMIT_TAIL = "please continue from where you left off, keeping your response shorter";
|
|
7
|
+
|
|
8
|
+
export type NoticeVerdict = "drop" | "hold" | "emit";
|
|
9
|
+
|
|
10
|
+
function noticeText(raw: string): string {
|
|
11
|
+
return raw.replace(/⚠/g, " ").replace(/\bError:/gi, " ").replace(/\s+/g, " ").trim().toLowerCase();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function classifyOutputLimitNotice(raw: string): NoticeVerdict {
|
|
15
|
+
const text = noticeText(raw);
|
|
16
|
+
if (text.length === 0) return "emit";
|
|
17
|
+
const stripped = text.replace(/\s*retries remaining:\s*\d*\s*$/i, "").trim();
|
|
18
|
+
const retry = text.slice(stripped.length).trim();
|
|
19
|
+
if (retry.length > 0 && !/^retries remaining:\s*\d*$/i.test(retry)) return "emit";
|
|
20
|
+
if (stripped.length === 0) return /^retries remaining:\s*\d+$/i.test(text) ? "drop" : "emit";
|
|
21
|
+
const full = `${OUTPUT_LIMIT_HEAD} ${OUTPUT_LIMIT_TAIL}`;
|
|
22
|
+
if (stripped === full || stripped === OUTPUT_LIMIT_HEAD || stripped === OUTPUT_LIMIT_TAIL) return "drop";
|
|
23
|
+
if (full.startsWith(stripped) && stripped.startsWith("your previous")) return "hold";
|
|
24
|
+
if (OUTPUT_LIMIT_TAIL.startsWith(stripped) && stripped.startsWith("please continue from where you left off")) return "hold";
|
|
25
|
+
if ("retries remaining:".startsWith(text) && text.startsWith("retries")) return "hold";
|
|
26
|
+
return "emit";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ACP notifications from several sessions share one connection callback, so
|
|
30
|
+
// the hold state is keyed by session and never leaks across streams.
|
|
31
|
+
export class AgentNoticeFilter {
|
|
32
|
+
private readonly holds = new Map<string, string>();
|
|
33
|
+
private readonly limitNotices = new Set<string>();
|
|
34
|
+
dropped = 0;
|
|
35
|
+
|
|
36
|
+
accept(sessionId: string, text: string): string | undefined {
|
|
37
|
+
const combined = (this.holds.get(sessionId) ?? "") + text;
|
|
38
|
+
const verdict = classifyOutputLimitNotice(combined);
|
|
39
|
+
if (verdict === "hold") {
|
|
40
|
+
this.holds.set(sessionId, combined);
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
this.holds.delete(sessionId);
|
|
44
|
+
if (verdict === "drop") {
|
|
45
|
+
this.dropped++;
|
|
46
|
+
this.limitNotices.add(sessionId);
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
if (AGENT_RETRY_NOTICE.test(combined)) {
|
|
50
|
+
this.dropped++;
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
return combined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// A thought chunk proves the held text was model output rather than the
|
|
57
|
+
// start of agy's notice. A finished notice is counted and discarded.
|
|
58
|
+
flush(sessionId: string): string | undefined {
|
|
59
|
+
const held = this.holds.get(sessionId);
|
|
60
|
+
if (held === undefined) return undefined;
|
|
61
|
+
this.holds.delete(sessionId);
|
|
62
|
+
if (classifyOutputLimitNotice(held) === "drop") {
|
|
63
|
+
this.dropped++;
|
|
64
|
+
this.limitNotices.add(sessionId);
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
return held;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
consumeOutputLimitNotice(sessionId: string): boolean {
|
|
71
|
+
return this.limitNotices.delete(sessionId);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
forget(sessionId: string): void {
|
|
75
|
+
this.holds.delete(sessionId);
|
|
76
|
+
this.limitNotices.delete(sessionId);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
clear(): void {
|
|
80
|
+
this.holds.clear();
|
|
81
|
+
this.limitNotices.clear();
|
|
82
|
+
}
|
|
83
|
+
}
|
package/extension/commands.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { describeCredentialStorage, inspectAntigravityAuth, savePermissionMode }
|
|
|
2
2
|
import { antigravityAccountEmail, authReady, formatLogoutSummary, keychain, ompCredentialSummary, type OmpAuthCoordinator, type OmpCommandContext } from "./auth-sync.ts";
|
|
3
3
|
import { parseNativeToolMode, type NativeToolMode } from "./tool-routing.ts";
|
|
4
4
|
import { saveToolPolicy, type ToolPolicy } from "./tool-policy.ts";
|
|
5
|
-
import { updateContextSettings, resolveContextOptions, saveContextSettings, type ContextSettings } from "./context-settings.ts";
|
|
5
|
+
import { DEFAULT_USAGE_MODE, updateContextSettings, resolveContextOptions, saveContextSettings, type ContextSettings } from "./context-settings.ts";
|
|
6
6
|
import type { CatalogStore, ConfiguredCatalogModel } from "./catalog-store.ts";
|
|
7
7
|
import type { SessionCleanup, CleanupMode } from "./session-cleanup.ts";
|
|
8
8
|
import type { Diagnostics } from "./diagnostics.ts";
|
|
@@ -254,8 +254,8 @@ export function createAntigravityCommandHandler(deps: CommandControllerDeps) {
|
|
|
254
254
|
if (command === "usage") {
|
|
255
255
|
const currentSettings = deps.getContextSettings();
|
|
256
256
|
ctx.ui.notify(JSON.stringify({
|
|
257
|
-
configured: currentSettings.defaults.usageMode ??
|
|
258
|
-
effective: process.env.OMP_ACP_USAGE === "off" ? "off" : currentSettings.defaults.usageMode ??
|
|
257
|
+
configured: currentSettings.defaults.usageMode ?? DEFAULT_USAGE_MODE,
|
|
258
|
+
effective: process.env.OMP_ACP_USAGE === "off" ? "off" : currentSettings.defaults.usageMode ?? DEFAULT_USAGE_MODE,
|
|
259
259
|
connections: [...deps.connections].map(connection => ({
|
|
260
260
|
context: [...connection.contextUsage].map(([sessionId, usage]) => ({
|
|
261
261
|
sessionId,
|
|
@@ -12,6 +12,7 @@ export interface ContextSettings {
|
|
|
12
12
|
defaults: ContextOptions & { usageMode?: "auto" | "off" };
|
|
13
13
|
models: Record<string, ContextOptions>;
|
|
14
14
|
}
|
|
15
|
+
export const DEFAULT_USAGE_MODE: "auto" | "off" = "off";
|
|
15
16
|
const ranges: Record<keyof ContextOptions, [number, number]> = {
|
|
16
17
|
contextWindow: [4096, 16_777_216], maxTokens: [1, 1_048_576], inputTokenBudget: [1, 16_777_216],
|
|
17
18
|
reserveTokens: [0, 1_048_576], maxTransferBytes: [65_536, 134_217_728], maxImageBytes: [0, 100_663_296],
|
package/extension/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { Diagnostics, instrumentRuntime } from "./diagnostics.ts";
|
|
|
10
10
|
import { stableTools } from "./tool-order.ts";
|
|
11
11
|
import { toolDeclarations, normalizeOmpSystemDeltas, type OmpToolDeclaration } from "./omp-context.ts";
|
|
12
12
|
import { OmpSessionLifecycle, type OmpProviderSessionState } from "./omp-session-lifecycle.ts";
|
|
13
|
-
import { loadContextSettings } from "./context-settings.ts";
|
|
13
|
+
import { DEFAULT_USAGE_MODE, loadContextSettings } from "./context-settings.ts";
|
|
14
14
|
import { selectRuntimeModel, type PublicCatalogModel } from "./model-catalog.ts";
|
|
15
15
|
import { TOOL_ROUTING } from "./tool-routing.ts";
|
|
16
16
|
import { TranscriptRouting } from "./transcript-routing.ts";
|
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
import { CatalogStore } from "./catalog-store.ts";
|
|
28
28
|
import { createAntigravityCommandHandler, type RuntimeBundleView, type ToolAuditSummary } from "./commands.ts";
|
|
29
29
|
import { formatCompactionNotice } from "./acp-compaction.ts";
|
|
30
|
+
import { classifyAcpFailure } from "./acp-errors.ts";
|
|
30
31
|
import {
|
|
31
32
|
AcpSessionStore,
|
|
32
33
|
AntigravityRuntime,
|
|
@@ -111,7 +112,7 @@ export async function registerOmpAdapter(pi: OmpHostApi, stateDir: string,
|
|
|
111
112
|
}
|
|
112
113
|
|
|
113
114
|
const connections = new Set<TierConnection>();
|
|
114
|
-
const idleTimeoutMs = Number(process.env.OMP_ACP_IDLE_TIMEOUT_MS ??
|
|
115
|
+
const idleTimeoutMs = Number(process.env.OMP_ACP_IDLE_TIMEOUT_MS ?? 900_000);
|
|
115
116
|
if (!Number.isSafeInteger(idleTimeoutMs) || idleTimeoutMs < 0 || idleTimeoutMs > 2_147_483_647 || (idleTimeoutMs > 0 && idleTimeoutMs < 1000)) {
|
|
116
117
|
throw new Error("OMP_ACP_IDLE_TIMEOUT_MS must be 0 (disabled) or an integer between 1000 and 2147483647");
|
|
117
118
|
}
|
|
@@ -138,7 +139,7 @@ export async function registerOmpAdapter(pi: OmpHostApi, stateDir: string,
|
|
|
138
139
|
...options,
|
|
139
140
|
ompNativeTools: toolPolicy.nativeTools,
|
|
140
141
|
ompDebugToolUpdates: toolPolicy.debugToolUpdates,
|
|
141
|
-
ompUsageMode: contextSettings.defaults.usageMode ??
|
|
142
|
+
ompUsageMode: contextSettings.defaults.usageMode ?? DEFAULT_USAGE_MODE,
|
|
142
143
|
ompCleanup: cleanup,
|
|
143
144
|
ompDiagnostics: diagnostics,
|
|
144
145
|
});
|
|
@@ -384,7 +385,7 @@ export async function registerOmpAdapter(pi: OmpHostApi, stateDir: string,
|
|
|
384
385
|
} finally { lease.release(); }
|
|
385
386
|
} catch (error) {
|
|
386
387
|
routingFailed = true;
|
|
387
|
-
deferred.fail(error, options?.signal?.aborted === true);
|
|
388
|
+
deferred.fail(classifyAcpFailure(error), options?.signal?.aborted === true);
|
|
388
389
|
} finally {
|
|
389
390
|
if (routingFailed) routing.noteFailure(routingId);
|
|
390
391
|
lifecycle.finish(ticket);
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
export interface PermissionDecision {
|
|
2
|
+
outcome:
|
|
3
|
+
| { outcome: "selected"; optionId: string }
|
|
4
|
+
| { outcome: "cancelled" };
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface ToolIdentity {
|
|
8
|
+
server?: string;
|
|
9
|
+
tool?: string;
|
|
10
|
+
kind?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface ToolRecord {
|
|
14
|
+
turn: number;
|
|
15
|
+
identity: ToolIdentity;
|
|
16
|
+
conflict: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type UnknownRecord = Record<string, unknown>;
|
|
20
|
+
|
|
21
|
+
const SERVER_KEYS = ["mcpServerName", "serverName", "server"] as const;
|
|
22
|
+
const TOOL_KEYS = ["mcpToolName", "toolName", "tool"] as const;
|
|
23
|
+
|
|
24
|
+
export class PermissionIdentityRegistry {
|
|
25
|
+
private readonly records = new Map<string, Map<string, ToolRecord>>();
|
|
26
|
+
private readonly seen = new Map<string, Map<string, number>>();
|
|
27
|
+
|
|
28
|
+
beginTurn(sessionId: string, turn: number): void {
|
|
29
|
+
const seen = this.seen.get(sessionId);
|
|
30
|
+
if (seen) {
|
|
31
|
+
for (const [toolCallId, seenTurn] of seen) {
|
|
32
|
+
if (seenTurn < turn - 1) seen.delete(toolCallId);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const session = this.records.get(sessionId);
|
|
36
|
+
if (!session) return;
|
|
37
|
+
for (const [toolCallId, record] of session) {
|
|
38
|
+
if (record.turn !== turn) session.delete(toolCallId);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
forget(sessionId: string): void {
|
|
43
|
+
this.seen.delete(sessionId);
|
|
44
|
+
this.records.delete(sessionId);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
clear(): void {
|
|
48
|
+
this.seen.clear();
|
|
49
|
+
this.records.clear();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
record(sessionId: string, turn: number, update: unknown): void {
|
|
53
|
+
if (!isPlainObject(update)) return;
|
|
54
|
+
const kind = update.sessionUpdate;
|
|
55
|
+
if (kind !== "tool_call" && kind !== "tool_call_update") return;
|
|
56
|
+
const toolCallId = stringValue(update.toolCallId);
|
|
57
|
+
if (!toolCallId) return;
|
|
58
|
+
const seenTurn = this.seen.get(sessionId)?.get(toolCallId);
|
|
59
|
+
if (seenTurn !== undefined && seenTurn !== turn) return;
|
|
60
|
+
this.seenFor(sessionId).set(toolCallId, turn);
|
|
61
|
+
const session = this.sessionRecords(sessionId);
|
|
62
|
+
const existing = session.get(toolCallId);
|
|
63
|
+
if (existing && existing.turn !== turn) {
|
|
64
|
+
session.delete(toolCallId);
|
|
65
|
+
}
|
|
66
|
+
const current = session.get(toolCallId) ?? { turn, identity: {}, conflict: false };
|
|
67
|
+
current.turn = turn;
|
|
68
|
+
const meta = isPlainObject(update._meta) ? update._meta : undefined;
|
|
69
|
+
this.merge(current, "server", fieldValue(update, meta, SERVER_KEYS));
|
|
70
|
+
this.merge(current, "tool", fieldValue(update, meta, TOOL_KEYS));
|
|
71
|
+
this.merge(current, "kind", kindValue(update.kind));
|
|
72
|
+
session.set(toolCallId, current);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
check(sessionId: string, turn: number, request: unknown): PermissionDecision | undefined {
|
|
76
|
+
if (!isPlainObject(request)) return reject(request);
|
|
77
|
+
const toolCall = isPlainObject(request.toolCall) ? request.toolCall : undefined;
|
|
78
|
+
const toolCallId = toolCall ? stringValue(toolCall.toolCallId) : undefined;
|
|
79
|
+
if (!toolCallId) return reject(request);
|
|
80
|
+
const record = this.records.get(sessionId)?.get(toolCallId);
|
|
81
|
+
if (!record && this.seen.get(sessionId)?.has(toolCallId)) return reject(request);
|
|
82
|
+
if (record && record.turn !== turn) return reject(request);
|
|
83
|
+
const meta = toolCall && isPlainObject(toolCall._meta) ? toolCall._meta : undefined;
|
|
84
|
+
const claimedServer = toolCall ? fieldValue(toolCall, meta, SERVER_KEYS) : undefined;
|
|
85
|
+
const claimedTool = toolCall ? fieldValue(toolCall, meta, TOOL_KEYS) : undefined;
|
|
86
|
+
const claimedKind = toolCall ? kindValue(toolCall.kind) : undefined;
|
|
87
|
+
if (!record) {
|
|
88
|
+
if (claimedServer !== undefined || claimedTool !== undefined) return reject(request);
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
if (record.conflict) return reject(request);
|
|
92
|
+
if (conflicts(record.identity.server, claimedServer)) return reject(request);
|
|
93
|
+
if (conflicts(record.identity.tool, claimedTool)) return reject(request);
|
|
94
|
+
if (conflicts(record.identity.kind, claimedKind)) return reject(request);
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private sessionRecords(sessionId: string): Map<string, ToolRecord> {
|
|
99
|
+
let session = this.records.get(sessionId);
|
|
100
|
+
if (!session) {
|
|
101
|
+
session = new Map();
|
|
102
|
+
this.records.set(sessionId, session);
|
|
103
|
+
}
|
|
104
|
+
return session;
|
|
105
|
+
}
|
|
106
|
+
private seenFor(sessionId: string): Map<string, number> {
|
|
107
|
+
let seen = this.seen.get(sessionId);
|
|
108
|
+
if (!seen) {
|
|
109
|
+
seen = new Map();
|
|
110
|
+
this.seen.set(sessionId, seen);
|
|
111
|
+
}
|
|
112
|
+
return seen;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private merge(record: ToolRecord, field: keyof ToolIdentity, value: string | null | undefined): void {
|
|
116
|
+
if (value === undefined) return;
|
|
117
|
+
if (value === null) {
|
|
118
|
+
if (record.identity[field] !== undefined) record.conflict = true;
|
|
119
|
+
delete record.identity[field];
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const current = record.identity[field];
|
|
123
|
+
if (current !== undefined && current !== value) {
|
|
124
|
+
record.conflict = true;
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
record.identity[field] = value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function conflicts(current: string | undefined, claimed: string | null | undefined): boolean {
|
|
132
|
+
if (claimed === undefined) return false;
|
|
133
|
+
if (claimed === null) return current !== undefined;
|
|
134
|
+
return current !== undefined && current !== claimed;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function reject(request: unknown): PermissionDecision {
|
|
138
|
+
const options = isPlainObject(request) && Array.isArray(request.options) ? request.options : [];
|
|
139
|
+
for (const option of options) {
|
|
140
|
+
if (!isPlainObject(option)) continue;
|
|
141
|
+
if (option.kind === "reject_once" || option.optionId === "reject_once") {
|
|
142
|
+
const optionId = stringValue(option.optionId);
|
|
143
|
+
if (optionId) return { outcome: { outcome: "selected", optionId } };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return { outcome: { outcome: "cancelled" } };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function fieldValue(
|
|
150
|
+
record: UnknownRecord,
|
|
151
|
+
meta: UnknownRecord | undefined,
|
|
152
|
+
keys: readonly string[],
|
|
153
|
+
): string | null | undefined {
|
|
154
|
+
let sawNull = false;
|
|
155
|
+
for (const source of [record, meta]) {
|
|
156
|
+
if (!source) continue;
|
|
157
|
+
for (const key of keys) {
|
|
158
|
+
if (!Object.hasOwn(source, key)) continue;
|
|
159
|
+
const value = source[key];
|
|
160
|
+
if (value === null) {
|
|
161
|
+
sawNull = true;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const text = stringValue(value);
|
|
165
|
+
if (text) return text;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return sawNull ? null : undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function kindValue(value: unknown): string | null | undefined {
|
|
172
|
+
if (value === undefined) return undefined;
|
|
173
|
+
if (value === null) return null;
|
|
174
|
+
return stringValue(value);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function stringValue(value: unknown): string | undefined {
|
|
178
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function isPlainObject(value: unknown): value is UnknownRecord {
|
|
182
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
183
|
+
}
|