pi-agy-acp 0.1.0 → 0.1.2
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 +22 -0
- package/README.md +12 -36
- package/docs/development.md +60 -13
- package/docs/reference.md +39 -28
- package/index.ts +100 -9
- package/package.json +4 -2
- package/src/acp-process.ts +1 -3
- package/src/conversation.ts +107 -7
- package/src/format.ts +54 -12
- package/src/mcp-bridge.ts +124 -6
- package/src/pi-stream.ts +77 -44
- package/src/preflight.ts +61 -0
- package/src/quota.ts +15 -0
- package/src/runtime.ts +114 -382
- package/src/util.ts +23 -0
- package/src/version.ts +5 -0
package/src/runtime.ts
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { randomBytes, timingSafeEqual } from 'node:crypto'
|
|
11
|
-
import { existsSync } from 'node:fs'
|
|
12
11
|
import { homedir } from 'node:os'
|
|
13
12
|
import { join } from 'node:path'
|
|
14
13
|
import { LlmError } from './protocol.ts'
|
|
@@ -17,7 +16,6 @@ import type {
|
|
|
17
16
|
GenerateOptions,
|
|
18
17
|
RequestMessage,
|
|
19
18
|
StreamChunk,
|
|
20
|
-
ToolSchema,
|
|
21
19
|
} from './protocol.ts'
|
|
22
20
|
import {
|
|
23
21
|
AcpProcess,
|
|
@@ -25,9 +23,7 @@ import {
|
|
|
25
23
|
type AcpPermissionOutcome,
|
|
26
24
|
type AcpPermissionRequest,
|
|
27
25
|
type AcpPromptResult,
|
|
28
|
-
type AcpSessionUpdate,
|
|
29
26
|
type AcpSpawner,
|
|
30
|
-
type AcpToolCallMeta,
|
|
31
27
|
} from './acp-process.ts'
|
|
32
28
|
import { ModelCatalog, nearestEffort } from './catalog.ts'
|
|
33
29
|
import { asEffort, type Config, type Effort } from './config.ts'
|
|
@@ -36,16 +32,19 @@ import {
|
|
|
36
32
|
AWAIT_TOOL,
|
|
37
33
|
AWAIT_TOOL_NAME,
|
|
38
34
|
CALL_TOOL,
|
|
39
|
-
CALL_TOOL_NAME,
|
|
40
35
|
pumpStep,
|
|
41
36
|
relayNotice,
|
|
42
37
|
type ParkedCall,
|
|
38
|
+
type ReportedToolCall,
|
|
39
|
+
structuredToolMeta,
|
|
40
|
+
recordReportedToolCall,
|
|
41
|
+
isEmptyInput,
|
|
43
42
|
} from './conversation.ts'
|
|
44
43
|
import {
|
|
45
44
|
AGENT_TOOL_PREFIX,
|
|
45
|
+
CALL_TOOL_NAME,
|
|
46
46
|
bridgeSystemPrompt,
|
|
47
47
|
inputPrompt,
|
|
48
|
-
REPLAY_BUDGET_BYTES,
|
|
49
48
|
MCP_SERVER_NAME,
|
|
50
49
|
renderTranscript,
|
|
51
50
|
replayPrompt,
|
|
@@ -55,6 +54,8 @@ import {
|
|
|
55
54
|
import { JsonRpcClosedError, JsonRpcRemoteError } from './jsonrpc.ts'
|
|
56
55
|
import {
|
|
57
56
|
McpBridge,
|
|
57
|
+
consumeToolDelta,
|
|
58
|
+
toBridgeTools,
|
|
58
59
|
type BridgeCallRequest,
|
|
59
60
|
type BridgeContent,
|
|
60
61
|
type BridgeTool,
|
|
@@ -67,6 +68,7 @@ import {
|
|
|
67
68
|
GPT_OSS_CONTEXT_WINDOW,
|
|
68
69
|
DEFAULT_CONTEXT_WINDOW,
|
|
69
70
|
prepareHome,
|
|
71
|
+
removeSessionArtifacts,
|
|
70
72
|
readVersion,
|
|
71
73
|
resolveAgyBinary,
|
|
72
74
|
syncOAuthToken,
|
|
@@ -79,7 +81,8 @@ import {
|
|
|
79
81
|
type AccountInfo,
|
|
80
82
|
type QuotaSnapshot,
|
|
81
83
|
} from './quota.ts'
|
|
82
|
-
import { delay, digest, KeyedMutex } from './util.ts'
|
|
84
|
+
import { delay, digest, KeyedMutex, abortable } from './util.ts'
|
|
85
|
+
import { VERSION } from './version.ts'
|
|
83
86
|
|
|
84
87
|
/** Parked calls answer with a still-running notice before Gemini cache TTL expiry and agy's 180-second MCP deadline. */
|
|
85
88
|
export const DEFAULT_RELAY_AFTER_MS = 85_000
|
|
@@ -108,7 +111,6 @@ export interface RuntimeStatus {
|
|
|
108
111
|
account?: AccountInfo
|
|
109
112
|
contextWindow: number
|
|
110
113
|
defaultEffort: Effort
|
|
111
|
-
longDecodeContinuations: number
|
|
112
114
|
models: {
|
|
113
115
|
id: string
|
|
114
116
|
name: string
|
|
@@ -130,15 +132,6 @@ export interface RuntimeStatus {
|
|
|
130
132
|
}
|
|
131
133
|
}
|
|
132
134
|
|
|
133
|
-
/** Structured MCP metadata the agent reported for one tool call. */
|
|
134
|
-
interface ReportedToolCall {
|
|
135
|
-
kind?: string
|
|
136
|
-
serverName?: string
|
|
137
|
-
toolName?: string
|
|
138
|
-
/** Set when later updates contradicted an already established field. */
|
|
139
|
-
conflict?: boolean
|
|
140
|
-
}
|
|
141
|
-
|
|
142
135
|
interface Conversation {
|
|
143
136
|
readonly key: string
|
|
144
137
|
tenantKey: string | undefined
|
|
@@ -184,6 +177,7 @@ export class AntigravityRuntime {
|
|
|
184
177
|
private state: RuntimeStatus['state'] = 'idle'
|
|
185
178
|
private lastError: string | undefined
|
|
186
179
|
private tierDiscovery: Promise<void> | undefined
|
|
180
|
+
private warmupPromise: Promise<void> | undefined
|
|
187
181
|
private readonly conversations = new Map<string, Conversation>()
|
|
188
182
|
private readonly tenants = new Map<string, Conversation>()
|
|
189
183
|
private readonly mutex = new KeyedMutex()
|
|
@@ -237,16 +231,6 @@ export class AntigravityRuntime {
|
|
|
237
231
|
return this.catalog.fixedEffort(model) ?? requested
|
|
238
232
|
}
|
|
239
233
|
|
|
240
|
-
/** Default effort. */
|
|
241
|
-
defaultEffort(): Effort {
|
|
242
|
-
return this.host.config.defaultEffort
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/** Maximum automatic long-decode continuations. */
|
|
246
|
-
longDecodeContinuations(): number {
|
|
247
|
-
return this.host.config.longDecodeContinuations
|
|
248
|
-
}
|
|
249
|
-
|
|
250
234
|
/**
|
|
251
235
|
* Current status for diagnostics surfaces.
|
|
252
236
|
* @returns the status snapshot.
|
|
@@ -264,8 +248,7 @@ export class AntigravityRuntime {
|
|
|
264
248
|
home: this.home,
|
|
265
249
|
account: this.accountInfo(),
|
|
266
250
|
contextWindow: DEFAULT_CONTEXT_WINDOW,
|
|
267
|
-
defaultEffort: this.defaultEffort
|
|
268
|
-
longDecodeContinuations: this.longDecodeContinuations(),
|
|
251
|
+
defaultEffort: this.host.config.defaultEffort,
|
|
269
252
|
models: this.catalog.list().map((m) => ({
|
|
270
253
|
...m,
|
|
271
254
|
efforts: this.catalog.effortsFor(m.id),
|
|
@@ -343,25 +326,62 @@ export class AntigravityRuntime {
|
|
|
343
326
|
* Warm the process and refresh the catalog through a tool-less discovery session.
|
|
344
327
|
* @returns resolves when discovery finished.
|
|
345
328
|
*/
|
|
346
|
-
async warmup(
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
329
|
+
async warmup(
|
|
330
|
+
options: { force?: boolean; signal?: AbortSignal } = {},
|
|
331
|
+
): Promise<void> {
|
|
332
|
+
options.signal?.throwIfAborted()
|
|
333
|
+
if (this.warmupPromise !== undefined)
|
|
334
|
+
return abortable(this.warmupPromise, options.signal)
|
|
335
|
+
if (!options.force && this.catalog.observed) return
|
|
336
|
+
this.warmupPromise = (async () => {
|
|
337
|
+
const proc = await this.ensureReady()
|
|
338
|
+
const result = await proc.newSession(
|
|
339
|
+
{
|
|
340
|
+
cwd: this.preflight?.workspace ?? this.home,
|
|
341
|
+
mcpServers: [],
|
|
342
|
+
},
|
|
343
|
+
{ onUpdate: () => undefined },
|
|
344
|
+
120_000,
|
|
361
345
|
)
|
|
346
|
+
try {
|
|
347
|
+
if (
|
|
348
|
+
this.catalog.observe(
|
|
349
|
+
result.configOptions,
|
|
350
|
+
result.models?.availableModels,
|
|
351
|
+
result._meta?.antigravity?.tier,
|
|
352
|
+
)
|
|
353
|
+
)
|
|
354
|
+
this.host.onModelsChanged()
|
|
355
|
+
} finally {
|
|
356
|
+
try {
|
|
357
|
+
await proc.closeSession(result.sessionId)
|
|
358
|
+
} finally {
|
|
359
|
+
removeSessionArtifacts(this.home, result.sessionId)
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
})().finally(() => {
|
|
363
|
+
this.warmupPromise = undefined
|
|
364
|
+
})
|
|
365
|
+
return abortable(this.warmupPromise, options.signal)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** Close conversation sessions while retaining the process and discovery/quota caches. */
|
|
369
|
+
async resetConversations(): Promise<void> {
|
|
370
|
+
const conversations = [...this.conversations.values()]
|
|
371
|
+
for (const conv of conversations)
|
|
372
|
+
this.dropTurn(conv, 'The Pi session changed.')
|
|
373
|
+
await Promise.all(
|
|
374
|
+
conversations.map(async (conv) => {
|
|
375
|
+
const release = await this.mutex.acquire(conv.key)
|
|
376
|
+
try {
|
|
377
|
+
if (this.conversations.get(conv.key) !== conv) return
|
|
378
|
+
await this.resetSession(conv)
|
|
379
|
+
this.conversations.delete(conv.key)
|
|
380
|
+
} finally {
|
|
381
|
+
release()
|
|
382
|
+
}
|
|
383
|
+
}),
|
|
362
384
|
)
|
|
363
|
-
this.host.onModelsChanged()
|
|
364
|
-
await proc.closeSession(result.sessionId)
|
|
365
385
|
}
|
|
366
386
|
|
|
367
387
|
/** Stop every session, the process, and the bridge. */
|
|
@@ -406,7 +426,7 @@ export class AntigravityRuntime {
|
|
|
406
426
|
conv.toolNames = new Set(tools.map((t) => t.name))
|
|
407
427
|
const effort = this.effectiveEffort(
|
|
408
428
|
options.model,
|
|
409
|
-
asEffort(options.reasoningEffort) ?? this.defaultEffort
|
|
429
|
+
asEffort(options.reasoningEffort) ?? this.host.config.defaultEffort,
|
|
410
430
|
)
|
|
411
431
|
|
|
412
432
|
if (conv.turn !== undefined && conv.turn.pendingCalls.size > 0) {
|
|
@@ -571,7 +591,7 @@ export class AntigravityRuntime {
|
|
|
571
591
|
cwd: preflight.workspace,
|
|
572
592
|
spawn: this.host.spawnAcp,
|
|
573
593
|
clientName: 'pi-agy-acp',
|
|
574
|
-
clientVersion:
|
|
594
|
+
clientVersion: VERSION,
|
|
575
595
|
log: (message) => this.host.log.warn(message),
|
|
576
596
|
})
|
|
577
597
|
const started = proc
|
|
@@ -593,7 +613,10 @@ export class AntigravityRuntime {
|
|
|
593
613
|
}
|
|
594
614
|
|
|
595
615
|
private runPreflight(): Promise<PreflightResult> {
|
|
596
|
-
if (this.preflight !== undefined)
|
|
616
|
+
if (this.preflight !== undefined) {
|
|
617
|
+
prepareHome(this.home)
|
|
618
|
+
return Promise.resolve(this.preflight)
|
|
619
|
+
}
|
|
597
620
|
this.preflightPromise ??= (async () => {
|
|
598
621
|
const config = this.host.config
|
|
599
622
|
const binary = resolveAgyBinary(config.command)
|
|
@@ -608,7 +631,6 @@ export class AntigravityRuntime {
|
|
|
608
631
|
logFile: join(this.home, 'acp.log'),
|
|
609
632
|
}
|
|
610
633
|
this.preflight = result
|
|
611
|
-
this.host.onModelsChanged()
|
|
612
634
|
return result
|
|
613
635
|
})().finally(() => {
|
|
614
636
|
this.preflightPromise = undefined
|
|
@@ -632,16 +654,7 @@ export class AntigravityRuntime {
|
|
|
632
654
|
'TRANSPORT',
|
|
633
655
|
),
|
|
634
656
|
})
|
|
635
|
-
this.
|
|
636
|
-
conv.sessionId = undefined
|
|
637
|
-
conv.process = undefined
|
|
638
|
-
conv.tenantKey = undefined
|
|
639
|
-
conv.tenantSecret = undefined
|
|
640
|
-
conv.advertisedTools = []
|
|
641
|
-
conv.advertisedToolNames.clear()
|
|
642
|
-
conv.knownTools.clear()
|
|
643
|
-
conv.reportedToolCalls.clear()
|
|
644
|
-
|
|
657
|
+
this.clearSession(conv)
|
|
645
658
|
conv.dirty = true
|
|
646
659
|
}
|
|
647
660
|
}
|
|
@@ -719,12 +732,7 @@ export class AntigravityRuntime {
|
|
|
719
732
|
)
|
|
720
733
|
this.host.onModelsChanged()
|
|
721
734
|
} catch (error) {
|
|
722
|
-
this.
|
|
723
|
-
conv.tenantKey = undefined
|
|
724
|
-
conv.tenantSecret = undefined
|
|
725
|
-
conv.advertisedTools = []
|
|
726
|
-
conv.advertisedToolNames.clear()
|
|
727
|
-
conv.knownTools.clear()
|
|
735
|
+
this.clearSession(conv)
|
|
728
736
|
throw mapError(error)
|
|
729
737
|
}
|
|
730
738
|
}
|
|
@@ -965,9 +973,20 @@ export class AntigravityRuntime {
|
|
|
965
973
|
}
|
|
966
974
|
|
|
967
975
|
private async resetSession(conv: Conversation): Promise<void> {
|
|
976
|
+
let closing: Promise<void> | undefined
|
|
968
977
|
if (conv.sessionId !== undefined && conv.process !== undefined) {
|
|
969
|
-
|
|
978
|
+
const sessionId = conv.sessionId
|
|
979
|
+
closing = conv.process
|
|
980
|
+
.closeSession(sessionId)
|
|
981
|
+
.finally(() => removeSessionArtifacts(this.home, sessionId))
|
|
970
982
|
}
|
|
983
|
+
this.clearSession(conv)
|
|
984
|
+
conv.knownIds = undefined
|
|
985
|
+
conv.dirty = false
|
|
986
|
+
await closing
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
private clearSession(conv: Conversation): void {
|
|
971
990
|
this.releaseTenant(conv)
|
|
972
991
|
conv.sessionId = undefined
|
|
973
992
|
conv.tenantKey = undefined
|
|
@@ -977,9 +996,6 @@ export class AntigravityRuntime {
|
|
|
977
996
|
conv.advertisedToolNames.clear()
|
|
978
997
|
conv.knownTools.clear()
|
|
979
998
|
conv.reportedToolCalls.clear()
|
|
980
|
-
|
|
981
|
-
conv.knownIds = undefined
|
|
982
|
-
conv.dirty = false
|
|
983
999
|
}
|
|
984
1000
|
|
|
985
1001
|
private startPrompt(
|
|
@@ -988,7 +1004,7 @@ export class AntigravityRuntime {
|
|
|
988
1004
|
blocks: AcpPromptBlock[],
|
|
989
1005
|
): void {
|
|
990
1006
|
const turn = conv.turn as AcpTurn
|
|
991
|
-
proc.prompt(conv.sessionId as string, blocks).then(
|
|
1007
|
+
turn.promptSettled = proc.prompt(conv.sessionId as string, blocks).then(
|
|
992
1008
|
(result) => turn.events.push({ kind: 'done', result }),
|
|
993
1009
|
(error: unknown) =>
|
|
994
1010
|
turn.events.push({ kind: 'error', error: mapError(error) }),
|
|
@@ -1004,7 +1020,6 @@ export class AntigravityRuntime {
|
|
|
1004
1020
|
const turn = conv.turn as AcpTurn
|
|
1005
1021
|
const sessionId = conv.sessionId as string
|
|
1006
1022
|
let finishKind: string | undefined
|
|
1007
|
-
let usage: AcpPromptResult['usage']
|
|
1008
1023
|
const recordTurn = (
|
|
1009
1024
|
outcome: NonNullable<RuntimeStatus['lastTurn']>['outcome'],
|
|
1010
1025
|
) => {
|
|
@@ -1012,7 +1027,7 @@ export class AntigravityRuntime {
|
|
|
1012
1027
|
model: options.model,
|
|
1013
1028
|
effort,
|
|
1014
1029
|
steps: turn.toolSteps + turn.continuations + 1,
|
|
1015
|
-
usage,
|
|
1030
|
+
usage: turn.usage,
|
|
1016
1031
|
outcome,
|
|
1017
1032
|
durationMs: Math.max(0, performance.now() - turn.startedAt),
|
|
1018
1033
|
retryNotices: turn.retryNotices,
|
|
@@ -1024,13 +1039,10 @@ export class AntigravityRuntime {
|
|
|
1024
1039
|
turn,
|
|
1025
1040
|
{
|
|
1026
1041
|
parkSettleMs: this.host.config.parkSettleMs,
|
|
1027
|
-
maxContinuations: this.longDecodeContinuations
|
|
1042
|
+
maxContinuations: this.host.config.longDecodeContinuations,
|
|
1028
1043
|
continuePrompt: () =>
|
|
1029
1044
|
proc.prompt(sessionId, [{ type: 'text', text: CONTINUE_PROMPT }]),
|
|
1030
1045
|
cancel: () => proc.cancel(sessionId),
|
|
1031
|
-
recordUsage: (sample) => {
|
|
1032
|
-
usage = sample
|
|
1033
|
-
},
|
|
1034
1046
|
},
|
|
1035
1047
|
options.signal,
|
|
1036
1048
|
)) {
|
|
@@ -1056,7 +1068,7 @@ export class AntigravityRuntime {
|
|
|
1056
1068
|
)
|
|
1057
1069
|
if (finishKind === 'aborted') {
|
|
1058
1070
|
// A cancelled turn leaves partial agent-side history; rebuild from Pi history next time.
|
|
1059
|
-
await Promise.race([
|
|
1071
|
+
await Promise.race([turn.promptSettled, delay(5_000)])
|
|
1060
1072
|
conv.dirty = true
|
|
1061
1073
|
}
|
|
1062
1074
|
// The turn is over: clear the identities it established with it.
|
|
@@ -1202,39 +1214,17 @@ export class AntigravityRuntime {
|
|
|
1202
1214
|
request.tenantKey !== undefined
|
|
1203
1215
|
? this.tenants.get(request.tenantKey)
|
|
1204
1216
|
: undefined
|
|
1205
|
-
if (conv === undefined)
|
|
1206
|
-
return
|
|
1207
|
-
content: [
|
|
1208
|
-
{
|
|
1209
|
-
type: 'text',
|
|
1210
|
-
text: 'No harness turn is waiting for this tool call.',
|
|
1211
|
-
},
|
|
1212
|
-
],
|
|
1213
|
-
isError: true,
|
|
1214
|
-
}
|
|
1215
|
-
}
|
|
1217
|
+
if (conv === undefined)
|
|
1218
|
+
return toolError('No harness turn is waiting for this tool call.')
|
|
1216
1219
|
if (
|
|
1217
1220
|
request.conversationId !== undefined &&
|
|
1218
1221
|
(conv.sessionId === undefined ||
|
|
1219
1222
|
request.conversationId !== conv.sessionId)
|
|
1220
|
-
)
|
|
1221
|
-
return
|
|
1222
|
-
content: [{ type: 'text', text: 'Tenant session mismatch.' }],
|
|
1223
|
-
isError: true,
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1223
|
+
)
|
|
1224
|
+
return toolError('Tenant session mismatch.')
|
|
1226
1225
|
const turn = conv.turn
|
|
1227
|
-
if (turn === undefined || turn.finished)
|
|
1228
|
-
return
|
|
1229
|
-
content: [
|
|
1230
|
-
{
|
|
1231
|
-
type: 'text',
|
|
1232
|
-
text: 'No harness turn is waiting for this tool call.',
|
|
1233
|
-
},
|
|
1234
|
-
],
|
|
1235
|
-
isError: true,
|
|
1236
|
-
}
|
|
1237
|
-
}
|
|
1226
|
+
if (turn === undefined || turn.finished)
|
|
1227
|
+
return toolError('No harness turn is waiting for this tool call.')
|
|
1238
1228
|
if (request.name === AWAIT_TOOL_NAME) {
|
|
1239
1229
|
const callId =
|
|
1240
1230
|
typeof request.arguments.call_id === 'string'
|
|
@@ -1242,57 +1232,31 @@ export class AntigravityRuntime {
|
|
|
1242
1232
|
: ''
|
|
1243
1233
|
const call = turn.calls.get(callId)
|
|
1244
1234
|
if (call === undefined)
|
|
1245
|
-
return {
|
|
1246
|
-
content: [
|
|
1247
|
-
{ type: 'text', text: `Unknown harness call id ${callId}.` },
|
|
1248
|
-
],
|
|
1249
|
-
isError: true,
|
|
1250
|
-
}
|
|
1235
|
+
return toolError(`Unknown harness call id ${callId}.`)
|
|
1251
1236
|
turn.relaying = false
|
|
1252
1237
|
return this.awaitCall(turn, call, request.signal)
|
|
1253
1238
|
}
|
|
1254
|
-
|
|
1239
|
+
let name = request.name
|
|
1240
|
+
let args = request.arguments
|
|
1241
|
+
if (name === CALL_TOOL_NAME) {
|
|
1255
1242
|
const rawTarget =
|
|
1256
1243
|
typeof request.arguments.name === 'string'
|
|
1257
1244
|
? request.arguments.name.trim()
|
|
1258
1245
|
: ''
|
|
1259
|
-
|
|
1246
|
+
name = rawTarget.startsWith(AGENT_TOOL_PREFIX)
|
|
1260
1247
|
? rawTarget.slice(AGENT_TOOL_PREFIX.length)
|
|
1261
1248
|
: rawTarget
|
|
1262
1249
|
const rawArgs = request.arguments.arguments
|
|
1263
|
-
|
|
1250
|
+
args =
|
|
1264
1251
|
rawArgs !== null &&
|
|
1265
1252
|
typeof rawArgs === 'object' &&
|
|
1266
1253
|
!Array.isArray(rawArgs)
|
|
1267
1254
|
? (rawArgs as Record<string, unknown>)
|
|
1268
1255
|
: {}
|
|
1269
|
-
if (!conv.toolNames.has(targetName)) {
|
|
1270
|
-
return {
|
|
1271
|
-
content: [
|
|
1272
|
-
{
|
|
1273
|
-
type: 'text',
|
|
1274
|
-
text: `The harness has no tool named ${targetName}.`,
|
|
1275
|
-
},
|
|
1276
|
-
],
|
|
1277
|
-
isError: true,
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
const call = turn.park(targetName, targetArgs)
|
|
1281
|
-
return this.awaitCall(turn, call, request.signal)
|
|
1282
1256
|
}
|
|
1283
|
-
if (!conv.toolNames.has(
|
|
1284
|
-
return {
|
|
1285
|
-
|
|
1286
|
-
{
|
|
1287
|
-
type: 'text',
|
|
1288
|
-
text: `The harness has no tool named ${request.name}.`,
|
|
1289
|
-
},
|
|
1290
|
-
],
|
|
1291
|
-
isError: true,
|
|
1292
|
-
}
|
|
1293
|
-
}
|
|
1294
|
-
const call = turn.park(request.name, request.arguments)
|
|
1295
|
-
return this.awaitCall(turn, call, request.signal)
|
|
1257
|
+
if (!conv.toolNames.has(name))
|
|
1258
|
+
return toolError(`The harness has no tool named ${name}.`)
|
|
1259
|
+
return this.awaitCall(turn, turn.park(name, args), request.signal)
|
|
1296
1260
|
}
|
|
1297
1261
|
|
|
1298
1262
|
private async awaitCall(
|
|
@@ -1322,10 +1286,7 @@ export class AntigravityRuntime {
|
|
|
1322
1286
|
return relayNotice(call)
|
|
1323
1287
|
}
|
|
1324
1288
|
if (outcome === 'aborted')
|
|
1325
|
-
return
|
|
1326
|
-
content: [{ type: 'text', text: 'The tool call was cancelled.' }],
|
|
1327
|
-
isError: true,
|
|
1328
|
-
}
|
|
1289
|
+
return toolError('The tool call was cancelled.')
|
|
1329
1290
|
return outcome
|
|
1330
1291
|
} finally {
|
|
1331
1292
|
if (timer !== undefined) clearTimeout(timer)
|
|
@@ -1348,241 +1309,8 @@ function messageIds(messages: readonly RequestMessage[]): string[] {
|
|
|
1348
1309
|
return messages.map((m) => m.id ?? '')
|
|
1349
1310
|
}
|
|
1350
1311
|
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
return typeof value === 'string' && value.length > 0 ? value : undefined
|
|
1354
|
-
}
|
|
1355
|
-
|
|
1356
|
-
/**
|
|
1357
|
-
* Extract the structured `(serverName, toolName)` an agent claimed in one
|
|
1358
|
-
* permission request: top-level `mcpServerName`/`mcpToolName` or `_meta`.
|
|
1359
|
-
* `title` and `rawInput` are deliberately not consulted.
|
|
1360
|
-
*/
|
|
1361
|
-
function structuredToolMeta(toolCall: AcpPermissionRequest['toolCall']): {
|
|
1362
|
-
serverName?: string
|
|
1363
|
-
toolName?: string
|
|
1364
|
-
} {
|
|
1365
|
-
const meta: AcpToolCallMeta | undefined = toolCall?._meta
|
|
1366
|
-
const serverName =
|
|
1367
|
-
nonEmpty(toolCall?.mcpServerName) ??
|
|
1368
|
-
nonEmpty(meta?.mcpServerName) ??
|
|
1369
|
-
nonEmpty(meta?.serverName) ??
|
|
1370
|
-
nonEmpty(meta?.server)
|
|
1371
|
-
const toolName =
|
|
1372
|
-
nonEmpty(toolCall?.mcpToolName) ??
|
|
1373
|
-
nonEmpty(meta?.mcpToolName) ??
|
|
1374
|
-
nonEmpty(meta?.toolName) ??
|
|
1375
|
-
nonEmpty(meta?.tool)
|
|
1376
|
-
return {
|
|
1377
|
-
...(serverName !== undefined ? { serverName } : {}),
|
|
1378
|
-
...(toolName !== undefined ? { toolName } : {}),
|
|
1379
|
-
}
|
|
1380
|
-
}
|
|
1381
|
-
|
|
1382
|
-
/**
|
|
1383
|
-
* Record the structured metadata of one agent-reported tool-call update.
|
|
1384
|
-
* The first report establishes the identity; later updates merge only the
|
|
1385
|
-
* fields actually present, so a status-only `tool_call_update` never wipes an
|
|
1386
|
-
* established `server`/`tool`/`kind`. A later field that contradicts the
|
|
1387
|
-
* established identity keeps the original value and marks the record
|
|
1388
|
-
* anomalous — the permission gate refuses every further request for that call.
|
|
1389
|
-
* @returns 'created' | 'merged' | 'conflict', or undefined for ignored updates.
|
|
1390
|
-
*/
|
|
1391
|
-
function recordReportedToolCall(
|
|
1392
|
-
conv: Conversation,
|
|
1393
|
-
update: AcpSessionUpdate,
|
|
1394
|
-
): 'created' | 'merged' | 'conflict' | undefined {
|
|
1395
|
-
if (
|
|
1396
|
-
update.sessionUpdate !== 'tool_call' &&
|
|
1397
|
-
update.sessionUpdate !== 'tool_call_update'
|
|
1398
|
-
)
|
|
1399
|
-
return
|
|
1400
|
-
if (typeof update.toolCallId !== 'string' || update.toolCallId.length === 0)
|
|
1401
|
-
return
|
|
1402
|
-
const meta: AcpToolCallMeta | undefined = update._meta
|
|
1403
|
-
const serverName =
|
|
1404
|
-
nonEmpty(update.mcpServerName) ??
|
|
1405
|
-
nonEmpty(meta?.mcpServerName) ??
|
|
1406
|
-
nonEmpty(meta?.serverName) ??
|
|
1407
|
-
nonEmpty(meta?.server)
|
|
1408
|
-
const toolName =
|
|
1409
|
-
nonEmpty(update.mcpToolName) ??
|
|
1410
|
-
nonEmpty(meta?.mcpToolName) ??
|
|
1411
|
-
nonEmpty(meta?.toolName) ??
|
|
1412
|
-
nonEmpty(meta?.tool)
|
|
1413
|
-
const kind = typeof update.kind === 'string' ? update.kind : undefined
|
|
1414
|
-
const existing = conv.reportedToolCalls.get(update.toolCallId)
|
|
1415
|
-
const merged: ReportedToolCall = existing === undefined ? {} : { ...existing }
|
|
1416
|
-
let conflict = false
|
|
1417
|
-
if (kind !== undefined) {
|
|
1418
|
-
if (merged.kind !== undefined && merged.kind !== kind) conflict = true
|
|
1419
|
-
else merged.kind = kind
|
|
1420
|
-
}
|
|
1421
|
-
if (serverName !== undefined) {
|
|
1422
|
-
if (merged.serverName !== undefined && merged.serverName !== serverName)
|
|
1423
|
-
conflict = true
|
|
1424
|
-
else merged.serverName = serverName
|
|
1425
|
-
}
|
|
1426
|
-
if (toolName !== undefined) {
|
|
1427
|
-
if (merged.toolName !== undefined && merged.toolName !== toolName)
|
|
1428
|
-
conflict = true
|
|
1429
|
-
else merged.toolName = toolName
|
|
1430
|
-
}
|
|
1431
|
-
if (conflict) merged.conflict = true
|
|
1432
|
-
conv.reportedToolCalls.set(update.toolCallId, merged)
|
|
1433
|
-
if (conflict) return 'conflict'
|
|
1434
|
-
return existing === undefined ? 'created' : 'merged'
|
|
1435
|
-
}
|
|
1436
|
-
|
|
1437
|
-
/** Whether `rawInput` carries no arguments at all (MCP server loading probes). */
|
|
1438
|
-
function isEmptyInput(rawInput: unknown): boolean {
|
|
1439
|
-
if (rawInput === undefined || rawInput === null) return true
|
|
1440
|
-
if (typeof rawInput === 'string') return rawInput.trim().length === 0
|
|
1441
|
-
if (Array.isArray(rawInput)) return rawInput.length === 0
|
|
1442
|
-
if (typeof rawInput === 'object') return Object.keys(rawInput).length === 0
|
|
1443
|
-
return false
|
|
1444
|
-
}
|
|
1445
|
-
|
|
1446
|
-
/**
|
|
1447
|
-
* Diff active tools against the session's known tool set and render a compact
|
|
1448
|
-
* `<harness_tool_update>` block for newly added, modified, restored, or disabled tools.
|
|
1449
|
-
* @param conv - conversation tool state (`advertisedToolNames` and `knownTools`).
|
|
1450
|
-
* @param tools - active tools for the current step.
|
|
1451
|
-
* @returns `<harness_tool_update>` text block, or `''` when unchanged.
|
|
1452
|
-
*/
|
|
1453
|
-
export function consumeToolDelta(
|
|
1454
|
-
conv: {
|
|
1455
|
-
readonly advertisedToolNames: ReadonlySet<string>
|
|
1456
|
-
readonly knownTools: Map<string, string>
|
|
1457
|
-
},
|
|
1458
|
-
tools: readonly BridgeTool[],
|
|
1459
|
-
): string {
|
|
1460
|
-
const lines: string[] = []
|
|
1461
|
-
const active = new Set<string>()
|
|
1462
|
-
for (const tool of tools) {
|
|
1463
|
-
active.add(tool.name)
|
|
1464
|
-
const hash = digest(tool)
|
|
1465
|
-
if (conv.knownTools.get(tool.name) === hash) continue
|
|
1466
|
-
conv.knownTools.set(tool.name, hash)
|
|
1467
|
-
if (conv.advertisedToolNames.has(tool.name)) {
|
|
1468
|
-
lines.push(`[tool restored: ${AGENT_TOOL_PREFIX}${tool.name}]`)
|
|
1469
|
-
} else {
|
|
1470
|
-
lines.push(
|
|
1471
|
-
`[tool available via ${AGENT_TOOL_PREFIX}${CALL_TOOL_NAME} with {"name":"${tool.name}","arguments":{...}}]: ${JSON.stringify({ name: tool.name, description: tool.description, parameters: tool.inputSchema })}`,
|
|
1472
|
-
)
|
|
1473
|
-
}
|
|
1474
|
-
}
|
|
1475
|
-
for (const name of [...conv.knownTools.keys()]) {
|
|
1476
|
-
if (!active.has(name)) {
|
|
1477
|
-
conv.knownTools.delete(name)
|
|
1478
|
-
lines.push(`[tool disabled: ${name}]`)
|
|
1479
|
-
}
|
|
1480
|
-
}
|
|
1481
|
-
return lines.length > 0
|
|
1482
|
-
? `<harness_tool_update>\n${lines.join('\n')}\n</harness_tool_update>`
|
|
1483
|
-
: ''
|
|
1484
|
-
}
|
|
1485
|
-
|
|
1486
|
-
/** Byte ceiling keeping advertised MCP tool schemas below agy's ~1,500B lazy cutoff. */
|
|
1487
|
-
export const MAX_EAGER_BRIDGE_TOOL_BYTES = 1_350
|
|
1488
|
-
|
|
1489
|
-
function shortenText(text: string, maxChars: number): string {
|
|
1490
|
-
const clean = text.replace(/\s+/gu, ' ').trim()
|
|
1491
|
-
if (clean.length <= maxChars) return clean
|
|
1492
|
-
const sentenceEnd = clean.indexOf('. ')
|
|
1493
|
-
if (sentenceEnd >= 32 && sentenceEnd + 1 <= maxChars)
|
|
1494
|
-
return clean.slice(0, sentenceEnd + 1)
|
|
1495
|
-
return `${clean.slice(0, Math.max(1, maxChars - 1)).trimEnd()}…`
|
|
1496
|
-
}
|
|
1497
|
-
|
|
1498
|
-
function compactSchemaNode(value: unknown, maxDescChars: number): unknown {
|
|
1499
|
-
if (Array.isArray(value))
|
|
1500
|
-
return value.map((item) => compactSchemaNode(item, maxDescChars))
|
|
1501
|
-
if (value === null || typeof value !== 'object') return value
|
|
1502
|
-
const record = value as Record<string, unknown>
|
|
1503
|
-
const out: Record<string, unknown> = {}
|
|
1504
|
-
for (const [key, entry] of Object.entries(record)) {
|
|
1505
|
-
if (key === 'description' && typeof entry === 'string') {
|
|
1506
|
-
if (maxDescChars > 0) out[key] = shortenText(entry, maxDescChars)
|
|
1507
|
-
continue
|
|
1508
|
-
}
|
|
1509
|
-
if (
|
|
1510
|
-
key === 'properties' &&
|
|
1511
|
-
entry !== null &&
|
|
1512
|
-
typeof entry === 'object' &&
|
|
1513
|
-
!Array.isArray(entry)
|
|
1514
|
-
) {
|
|
1515
|
-
const props: Record<string, unknown> = {}
|
|
1516
|
-
for (const [propKey, propVal] of Object.entries(
|
|
1517
|
-
entry as Record<string, unknown>,
|
|
1518
|
-
)) {
|
|
1519
|
-
props[propKey] = compactSchemaNode(propVal, maxDescChars)
|
|
1520
|
-
}
|
|
1521
|
-
out[key] = props
|
|
1522
|
-
continue
|
|
1523
|
-
}
|
|
1524
|
-
out[key] = compactSchemaNode(entry, maxDescChars)
|
|
1525
|
-
}
|
|
1526
|
-
return out
|
|
1527
|
-
}
|
|
1528
|
-
|
|
1529
|
-
/**
|
|
1530
|
-
* Keep each MCP-advertised tool schema under `agy`'s eager size threshold while
|
|
1531
|
-
* preserving every parameter name, type, required list, and enum constraint.
|
|
1532
|
-
* Full prose instructions remain in Pi's system prompt.
|
|
1533
|
-
* @param tool - raw bridge tool descriptor.
|
|
1534
|
-
* @returns compacted descriptor when oversized, otherwise the original descriptor.
|
|
1535
|
-
*/
|
|
1536
|
-
export function compactBridgeTool(tool: BridgeTool): BridgeTool {
|
|
1537
|
-
if (
|
|
1538
|
-
Buffer.byteLength(JSON.stringify(tool), 'utf8') <=
|
|
1539
|
-
MAX_EAGER_BRIDGE_TOOL_BYTES
|
|
1540
|
-
)
|
|
1541
|
-
return tool
|
|
1542
|
-
const pass1: BridgeTool = {
|
|
1543
|
-
name: tool.name,
|
|
1544
|
-
description: shortenText(tool.description, 220),
|
|
1545
|
-
inputSchema: compactSchemaNode(tool.inputSchema, 90) as Record<
|
|
1546
|
-
string,
|
|
1547
|
-
unknown
|
|
1548
|
-
>,
|
|
1549
|
-
}
|
|
1550
|
-
if (
|
|
1551
|
-
Buffer.byteLength(JSON.stringify(pass1), 'utf8') <=
|
|
1552
|
-
MAX_EAGER_BRIDGE_TOOL_BYTES
|
|
1553
|
-
)
|
|
1554
|
-
return pass1
|
|
1555
|
-
return {
|
|
1556
|
-
name: tool.name,
|
|
1557
|
-
description: shortenText(tool.description, 140),
|
|
1558
|
-
inputSchema: compactSchemaNode(tool.inputSchema, 0) as Record<
|
|
1559
|
-
string,
|
|
1560
|
-
unknown
|
|
1561
|
-
>,
|
|
1562
|
-
}
|
|
1563
|
-
}
|
|
1564
|
-
|
|
1565
|
-
function toBridgeTools(tools: readonly ToolSchema[] | undefined): BridgeTool[] {
|
|
1566
|
-
return (tools ?? []).map((t) =>
|
|
1567
|
-
compactBridgeTool({
|
|
1568
|
-
name: t.name,
|
|
1569
|
-
description: t.description,
|
|
1570
|
-
inputSchema:
|
|
1571
|
-
t.parameters.type === undefined
|
|
1572
|
-
? { type: 'object', ...t.parameters }
|
|
1573
|
-
: t.parameters,
|
|
1574
|
-
}),
|
|
1575
|
-
)
|
|
1576
|
-
}
|
|
1577
|
-
|
|
1578
|
-
function turnSettled(turn: AcpTurn): Promise<void> {
|
|
1579
|
-
return (async () => {
|
|
1580
|
-
while (turn.events.size > 0 || !turn.finished) {
|
|
1581
|
-
const head = turn.events.peek()
|
|
1582
|
-
if (head?.kind === 'done' || head?.kind === 'error') return
|
|
1583
|
-
await delay(100)
|
|
1584
|
-
}
|
|
1585
|
-
})()
|
|
1312
|
+
function toolError(text: string): BridgeToolResult {
|
|
1313
|
+
return { content: [{ type: 'text', text }], isError: true }
|
|
1586
1314
|
}
|
|
1587
1315
|
|
|
1588
1316
|
/**
|
|
@@ -1614,10 +1342,14 @@ export function mapError(error: unknown): Error {
|
|
|
1614
1342
|
return new LlmError(message, 'QUOTA', { cause: error })
|
|
1615
1343
|
if (/rate.?limit|429|too many requests|overloaded|capacity/i.test(message))
|
|
1616
1344
|
return new LlmError(message, 'RATE_LIMIT', { cause: error })
|
|
1617
|
-
if (/context|too long|token limit|maximum.*tokens/i.test(message))
|
|
1618
|
-
return new LlmError(message, 'CONTEXT_WINDOW_EXCEEDED', { cause: error })
|
|
1619
1345
|
if (/timeout|timed out|deadline/i.test(message))
|
|
1620
1346
|
return new LlmError(message, 'TIMEOUT', { cause: error })
|
|
1347
|
+
if (
|
|
1348
|
+
/context.*(length|window|limit|overflow|exceeded)|too long|token limit|maximum.*tokens/i.test(
|
|
1349
|
+
message,
|
|
1350
|
+
)
|
|
1351
|
+
)
|
|
1352
|
+
return new LlmError(message, 'CONTEXT_WINDOW_EXCEEDED', { cause: error })
|
|
1621
1353
|
if (error instanceof JsonRpcRemoteError)
|
|
1622
1354
|
return new LlmError(`Antigravity: ${message}`, 'SERVER', { cause: error })
|
|
1623
1355
|
return new LlmError(message, 'TRANSPORT', { cause: error })
|