pi-agy-acp 0.2.1 → 0.2.3
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 +16 -0
- package/README.md +11 -7
- package/docs/architecture.md +262 -0
- package/docs/development.md +68 -156
- package/docs/reference.md +151 -128
- package/docs/releasing.md +56 -0
- package/index.ts +87 -22
- package/package.json +1 -1
- package/src/acp-process.ts +15 -1
- package/src/catalog.ts +66 -48
- package/src/config.ts +1 -1
- package/src/conversation.ts +66 -16
- package/src/format.ts +68 -23
- package/src/host.ts +148 -13
- package/src/jsonrpc.ts +38 -4
- package/src/mcp-bridge.ts +26 -2
- package/src/preflight.ts +296 -371
- package/src/quota.ts +39 -20
- package/src/runtime.ts +549 -244
- package/src/session-db.ts +670 -0
package/src/runtime.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { randomBytes, timingSafeEqual } from 'node:crypto'
|
|
11
|
-
import {
|
|
11
|
+
import { statSync } from 'node:fs'
|
|
12
12
|
import { join } from 'node:path'
|
|
13
13
|
import { LlmError } from './protocol.ts'
|
|
14
14
|
import type {
|
|
@@ -70,16 +70,20 @@ import {
|
|
|
70
70
|
BRIDGE_AGENT,
|
|
71
71
|
defaultAgyHomes,
|
|
72
72
|
defaultHome,
|
|
73
|
+
isDefaultPiAgyHome,
|
|
73
74
|
prepareHome,
|
|
74
|
-
readSessionContextMetadata,
|
|
75
|
-
readSessionNextMetadataIndex,
|
|
76
|
-
removeSessionArtifacts,
|
|
77
75
|
readVersion,
|
|
78
76
|
resolveAgyBinary,
|
|
79
77
|
syncOAuthToken,
|
|
80
78
|
type CommandRunner,
|
|
81
79
|
type PreflightResult,
|
|
82
80
|
} from './preflight.ts'
|
|
81
|
+
import {
|
|
82
|
+
readSessionCursors,
|
|
83
|
+
readSessionTurnData,
|
|
84
|
+
removeSessionArtifacts,
|
|
85
|
+
type HiddenTurnError,
|
|
86
|
+
} from './session-db.ts'
|
|
83
87
|
import {
|
|
84
88
|
QuotaReader,
|
|
85
89
|
readLoggedEmail,
|
|
@@ -91,6 +95,8 @@ import { VERSION } from './version.ts'
|
|
|
91
95
|
|
|
92
96
|
const CONTINUE_PROMPT =
|
|
93
97
|
'Continue exactly where your previous output stopped. Do not repeat any earlier text.'
|
|
98
|
+
const SWEEP_INTERVAL_MS = 60_000
|
|
99
|
+
const EMAIL_CACHE_TTL_MS = 10_000
|
|
94
100
|
|
|
95
101
|
/** Host services the runtime uses. */
|
|
96
102
|
export interface RuntimeHost {
|
|
@@ -102,6 +108,10 @@ export interface RuntimeHost {
|
|
|
102
108
|
onModelsChanged(): void
|
|
103
109
|
/** Delay before a parked call answers with a still-running notice; overrides config.relayAfterMs (85 seconds). */
|
|
104
110
|
relayAfterMs?: number
|
|
111
|
+
/** Monotonic clock in milliseconds for quota TTL checks; defaults to `performance.now()`. */
|
|
112
|
+
quotaNow?: () => number
|
|
113
|
+
/** Monotonic clock in milliseconds for idle-session sweeping, LRU ordering, and log-email cache TTL; defaults to `performance.now()`. */
|
|
114
|
+
monotonicNow?: () => number
|
|
105
115
|
}
|
|
106
116
|
|
|
107
117
|
/** Snapshot reported to the settings page and quota pill. */
|
|
@@ -158,9 +168,12 @@ interface Conversation {
|
|
|
158
168
|
turn: AcpTurn | undefined
|
|
159
169
|
dirty: boolean
|
|
160
170
|
lastUsed: number
|
|
171
|
+
idleMs: number
|
|
161
172
|
model: string | undefined
|
|
162
173
|
/** Lower `gen_metadata.idx` bound for `model`, or `null` when unobservable after a failed cursor read. */
|
|
163
174
|
metaFloorIdx: number | null
|
|
175
|
+
/** Last observed SQLite file/sidecar stamp used to skip unchanged reads during streaming. */
|
|
176
|
+
lastDbStamp?: string
|
|
164
177
|
effort: Effort | undefined
|
|
165
178
|
}
|
|
166
179
|
|
|
@@ -192,6 +205,15 @@ export class AntigravityRuntime {
|
|
|
192
205
|
/** Serializes capacity checks and session creation so concurrent requests cannot overshoot maxLiveSessions. */
|
|
193
206
|
private readonly capacityMutex = new KeyedMutex()
|
|
194
207
|
private sweeper: ReturnType<typeof setInterval> | undefined
|
|
208
|
+
private readonly monotonicNow: () => number
|
|
209
|
+
private lastSweepAt: number
|
|
210
|
+
/** In-flight `stream()` calls acquiring or using the front process; guards idle teardown. */
|
|
211
|
+
private activeStreams = 0
|
|
212
|
+
/** Monotonic milliseconds the front process has been completely idle. */
|
|
213
|
+
private processIdleMs = 0
|
|
214
|
+
private emailCache:
|
|
215
|
+
| { fingerprint: string; email: string | undefined; at: number }
|
|
216
|
+
| undefined
|
|
195
217
|
private lastTurn: RuntimeStatus['lastTurn']
|
|
196
218
|
private lifecycle: 'active' | 'disposing' | 'disposed' = 'active'
|
|
197
219
|
private readonly shutdown = new AbortController()
|
|
@@ -201,6 +223,8 @@ export class AntigravityRuntime {
|
|
|
201
223
|
/** @param host - host services. */
|
|
202
224
|
constructor(host: RuntimeHost) {
|
|
203
225
|
this.host = host
|
|
226
|
+
this.monotonicNow = host.monotonicNow ?? (() => performance.now())
|
|
227
|
+
this.lastSweepAt = this.monotonicNow()
|
|
204
228
|
this.home = host.config.home.length > 0 ? host.config.home : defaultHome()
|
|
205
229
|
this.catalog = new ModelCatalog(
|
|
206
230
|
join(this.home, 'catalog.json'),
|
|
@@ -222,6 +246,7 @@ export class AntigravityRuntime {
|
|
|
222
246
|
() => resolveAgyBinary(host.config.command),
|
|
223
247
|
host.runCommand,
|
|
224
248
|
host.config.quotaTtlMs,
|
|
249
|
+
host.quotaNow,
|
|
225
250
|
)
|
|
226
251
|
}
|
|
227
252
|
|
|
@@ -252,55 +277,120 @@ export class AntigravityRuntime {
|
|
|
252
277
|
}
|
|
253
278
|
if (
|
|
254
279
|
this.process?.alive &&
|
|
255
|
-
this.starting === undefined &&
|
|
256
280
|
this.processFingerprint !== undefined &&
|
|
257
|
-
this.processFingerprint !== fp
|
|
258
|
-
[...this.conversations.values()].every((c) => this.isIdle(c))
|
|
281
|
+
this.processFingerprint !== fp
|
|
259
282
|
) {
|
|
283
|
+
const canRestart =
|
|
284
|
+
this.starting === undefined &&
|
|
285
|
+
this.warmupPromise === undefined &&
|
|
286
|
+
this.activeStreams === 0 &&
|
|
287
|
+
[...this.conversations.values()].every((c) => this.isIdle(c))
|
|
288
|
+
if (!canRestart) {
|
|
289
|
+
// Defer catalog fingerprint sync while the old binary's process is still active,
|
|
290
|
+
// so live observations from the stale process cannot stamp the new fingerprint.
|
|
291
|
+
return
|
|
292
|
+
}
|
|
260
293
|
// Restart an idle ACP process after an on-disk `agy` upgrade so discovery probes the new binary.
|
|
261
294
|
const stale = this.process
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
295
|
+
const staleSessions = [...this.conversations.values()]
|
|
296
|
+
.filter((conv) => conv.process === stale)
|
|
297
|
+
.map((conv) => ({ conv, sessionId: conv.sessionId }))
|
|
298
|
+
this.detachProcess(stale)
|
|
299
|
+
void this.bridge.close()
|
|
300
|
+
for (const { conv, sessionId } of staleSessions) {
|
|
301
|
+
if (sessionId !== undefined) {
|
|
302
|
+
removeSessionArtifacts(this.home, sessionId)
|
|
303
|
+
}
|
|
304
|
+
this.clearSession(conv)
|
|
305
|
+
conv.dirty = true
|
|
306
|
+
}
|
|
265
307
|
}
|
|
266
308
|
if (this.catalog.syncBinaryFingerprint(fp)) {
|
|
267
309
|
this.host.onModelsChanged()
|
|
268
310
|
}
|
|
269
311
|
}
|
|
270
312
|
|
|
271
|
-
private
|
|
313
|
+
private harvestSessionTurnData(
|
|
314
|
+
conv: Conversation,
|
|
272
315
|
sessionId: string | undefined,
|
|
316
|
+
model: string | undefined,
|
|
273
317
|
modelFloor: number | null,
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
318
|
+
options: {
|
|
319
|
+
turn?: AcpTurn
|
|
320
|
+
lastStepIdx?: number | null
|
|
321
|
+
checkStepsError?: boolean
|
|
322
|
+
ifChangedSince?: string
|
|
323
|
+
} = {},
|
|
324
|
+
): HiddenTurnError | undefined {
|
|
325
|
+
if (this.lifecycle !== 'active' || !sessionId) return undefined
|
|
326
|
+
const data = readSessionTurnData(this.home, sessionId, {
|
|
327
|
+
metaFloorIdx: model ? modelFloor : null,
|
|
328
|
+
turnMetaFloorIdx: options.turn?.metaFloorIdx ?? null,
|
|
329
|
+
lastStepIdx: options.lastStepIdx,
|
|
330
|
+
checkStepsError: options.checkStepsError,
|
|
331
|
+
ifChangedSince: options.ifChangedSince,
|
|
332
|
+
})
|
|
333
|
+
if (data === undefined) return undefined
|
|
334
|
+
if (conv.sessionId === sessionId) conv.lastDbStamp = data.stamp
|
|
335
|
+
if (data.unchanged) return undefined
|
|
336
|
+
if (options.turn !== undefined && data.estimatedTokensUsed !== undefined) {
|
|
337
|
+
options.turn.estimatedContextUsed = data.estimatedTokensUsed
|
|
338
|
+
}
|
|
339
|
+
if (
|
|
340
|
+
model &&
|
|
341
|
+
this.catalog.observeContextWindow(model, data.maxContextTokens)
|
|
342
|
+
) {
|
|
343
|
+
this.host.onModelsChanged()
|
|
344
|
+
}
|
|
345
|
+
return data.hiddenError
|
|
278
346
|
}
|
|
279
347
|
|
|
280
|
-
private
|
|
281
|
-
|
|
348
|
+
private async runPromptSegment(
|
|
349
|
+
conv: Conversation,
|
|
350
|
+
turn: AcpTurn,
|
|
351
|
+
proc: AcpProcess,
|
|
352
|
+
sessionId: string,
|
|
282
353
|
model: string | undefined,
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
):
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
354
|
+
modelFloor: number | null,
|
|
355
|
+
blocks: AcpPromptBlock[],
|
|
356
|
+
): Promise<AcpPromptResult> {
|
|
357
|
+
const cursors = readSessionCursors(this.home, sessionId)
|
|
358
|
+
turn.metaFloorIdx =
|
|
359
|
+
modelFloor === null || cursors === null
|
|
360
|
+
? null
|
|
361
|
+
: Math.max(modelFloor, cursors.nextMetaIdx)
|
|
362
|
+
turn.lastStepIdx = cursors === null ? null : cursors.lastStepIdx
|
|
363
|
+
turn.estimatedContextUsed = undefined
|
|
364
|
+
let harvested = false
|
|
365
|
+
try {
|
|
366
|
+
const result = await proc.prompt(sessionId, blocks)
|
|
367
|
+
harvested = true
|
|
368
|
+
const checkStepsError =
|
|
369
|
+
result.stopReason === 'end_turn' || result.stopReason === 'refusal'
|
|
370
|
+
const hiddenError = this.harvestSessionTurnData(
|
|
371
|
+
conv,
|
|
372
|
+
sessionId,
|
|
373
|
+
model,
|
|
374
|
+
modelFloor,
|
|
375
|
+
{
|
|
376
|
+
turn,
|
|
377
|
+
lastStepIdx: turn.lastStepIdx,
|
|
378
|
+
checkStepsError,
|
|
379
|
+
},
|
|
380
|
+
)
|
|
381
|
+
if (hiddenError !== undefined) {
|
|
382
|
+
if (hiddenError.dirtySession) conv.dirty = true
|
|
383
|
+
throw new LlmError(hiddenError.message, hiddenError.code)
|
|
384
|
+
}
|
|
385
|
+
return result
|
|
386
|
+
} catch (error) {
|
|
387
|
+
if (!harvested) {
|
|
388
|
+
this.harvestSessionTurnData(conv, sessionId, model, modelFloor, {
|
|
389
|
+
turn,
|
|
390
|
+
checkStepsError: false,
|
|
391
|
+
})
|
|
392
|
+
}
|
|
393
|
+
throw error
|
|
304
394
|
}
|
|
305
395
|
}
|
|
306
396
|
|
|
@@ -365,11 +455,21 @@ export class AntigravityRuntime {
|
|
|
365
455
|
|
|
366
456
|
private accountInfo(): AccountInfo | undefined {
|
|
367
457
|
const paths = [join(this.home, 'acp.log'), this.home]
|
|
368
|
-
if (
|
|
369
|
-
this.home === join(homedir(), '.pi', 'agent', 'cache', 'antigravity-acp')
|
|
370
|
-
)
|
|
458
|
+
if (isDefaultPiAgyHome(this.home))
|
|
371
459
|
paths.push(...defaultAgyHomes())
|
|
372
|
-
const
|
|
460
|
+
const now = this.monotonicNow()
|
|
461
|
+
const fingerprint = logPathsFingerprint(paths)
|
|
462
|
+
let email: string | undefined
|
|
463
|
+
if (
|
|
464
|
+
this.emailCache !== undefined &&
|
|
465
|
+
this.emailCache.fingerprint === fingerprint &&
|
|
466
|
+
now - this.emailCache.at < EMAIL_CACHE_TTL_MS
|
|
467
|
+
) {
|
|
468
|
+
email = this.emailCache.email
|
|
469
|
+
} else {
|
|
470
|
+
email = readLoggedEmail(paths)
|
|
471
|
+
this.emailCache = { fingerprint, email, at: now }
|
|
472
|
+
}
|
|
373
473
|
const tier = this.catalog.tier()
|
|
374
474
|
if (!email && !tier) return undefined
|
|
375
475
|
return { email, plan: tier?.name, tierId: tier?.id }
|
|
@@ -379,12 +479,12 @@ export class AntigravityRuntime {
|
|
|
379
479
|
async readQuota(force = false): Promise<QuotaSnapshot> {
|
|
380
480
|
this.assertActive()
|
|
381
481
|
const quota = this.quota.read(force)
|
|
382
|
-
if (!this.catalog.tier()) {
|
|
482
|
+
if (!this.catalog.tier() && !this.catalog.observed) {
|
|
383
483
|
this.tierDiscovery ??= (async () => {
|
|
384
484
|
const state = this.state
|
|
385
485
|
const error = this.lastError
|
|
386
486
|
try {
|
|
387
|
-
await this.warmup()
|
|
487
|
+
await this.warmup({ force: true })
|
|
388
488
|
} catch {
|
|
389
489
|
// Optional account discovery must not leave an otherwise idle runtime in error.
|
|
390
490
|
if (
|
|
@@ -409,8 +509,10 @@ export class AntigravityRuntime {
|
|
|
409
509
|
*/
|
|
410
510
|
async ensureReady(): Promise<AcpProcess> {
|
|
411
511
|
this.assertActive()
|
|
512
|
+
this.processIdleMs = 0
|
|
412
513
|
if (this.sweeper === undefined) {
|
|
413
|
-
this.
|
|
514
|
+
this.lastSweepAt = this.monotonicNow()
|
|
515
|
+
this.sweeper = setInterval(() => this.sweep(), SWEEP_INTERVAL_MS)
|
|
414
516
|
this.sweeper.unref?.()
|
|
415
517
|
}
|
|
416
518
|
if (this.starting === undefined && this.process?.alive === true)
|
|
@@ -424,7 +526,10 @@ export class AntigravityRuntime {
|
|
|
424
526
|
}
|
|
425
527
|
|
|
426
528
|
/**
|
|
427
|
-
*
|
|
529
|
+
* Refresh the catalog through a tool-less discovery session when uncached,
|
|
530
|
+
* stale for the current `agy` binary, or forced. When this call starts the
|
|
531
|
+
* front process on an otherwise idle runtime, the temporary process and MCP
|
|
532
|
+
* bridge are stopped once discovery finishes unless a stream adopted them.
|
|
428
533
|
* @returns resolves when discovery finished.
|
|
429
534
|
*/
|
|
430
535
|
async warmup(
|
|
@@ -435,30 +540,43 @@ export class AntigravityRuntime {
|
|
|
435
540
|
if (this.warmupPromise !== undefined)
|
|
436
541
|
return abortable(this.warmupPromise, options.signal)
|
|
437
542
|
this.reconcileBinaryFingerprint()
|
|
438
|
-
if (!options.force && this.catalog.
|
|
543
|
+
if (!options.force && this.catalog.isCurrent()) return
|
|
544
|
+
const hadProcess =
|
|
545
|
+
this.process?.alive === true || this.starting !== undefined
|
|
439
546
|
this.warmupPromise = (async () => {
|
|
440
|
-
|
|
441
|
-
this.assertOwner(proc)
|
|
442
|
-
const result = await proc.newSession(
|
|
443
|
-
{
|
|
444
|
-
cwd: this.preflight?.workspace ?? this.home,
|
|
445
|
-
mcpServers: [],
|
|
446
|
-
},
|
|
447
|
-
{ onUpdate: () => undefined },
|
|
448
|
-
120_000,
|
|
449
|
-
)
|
|
547
|
+
let proc: AcpProcess | undefined
|
|
450
548
|
try {
|
|
549
|
+
proc = await this.ensureReady()
|
|
451
550
|
this.assertOwner(proc)
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
551
|
+
const result = await proc
|
|
552
|
+
.newSession(
|
|
553
|
+
{
|
|
554
|
+
cwd: this.preflight?.workspace ?? this.home,
|
|
555
|
+
mcpServers: [],
|
|
556
|
+
},
|
|
557
|
+
{ onUpdate: () => undefined },
|
|
558
|
+
120_000,
|
|
457
559
|
)
|
|
458
|
-
|
|
459
|
-
|
|
560
|
+
.catch((error: unknown) => {
|
|
561
|
+
throw mapError(error)
|
|
562
|
+
})
|
|
563
|
+
try {
|
|
564
|
+
this.assertOwner(proc)
|
|
565
|
+
if (
|
|
566
|
+
this.catalog.observe(
|
|
567
|
+
result.configOptions,
|
|
568
|
+
result.models?.availableModels,
|
|
569
|
+
result._meta?.antigravity?.tier,
|
|
570
|
+
)
|
|
571
|
+
)
|
|
572
|
+
this.host.onModelsChanged()
|
|
573
|
+
} finally {
|
|
574
|
+
await this.closeAndCleanSession(proc, result.sessionId)
|
|
575
|
+
}
|
|
460
576
|
} finally {
|
|
461
|
-
|
|
577
|
+
if (!hadProcess && proc !== undefined) {
|
|
578
|
+
await this.stopIdleProcess(proc, { ignoreWarmup: true })
|
|
579
|
+
}
|
|
462
580
|
}
|
|
463
581
|
})().finally(() => {
|
|
464
582
|
this.warmupPromise = undefined
|
|
@@ -469,6 +587,7 @@ export class AntigravityRuntime {
|
|
|
469
587
|
/** Close conversation sessions while retaining the process and discovery/quota caches. */
|
|
470
588
|
async resetConversations(): Promise<void> {
|
|
471
589
|
this.assertActive()
|
|
590
|
+
this.processIdleMs = 0
|
|
472
591
|
const conversations = [...this.conversations.values()]
|
|
473
592
|
for (const conv of conversations)
|
|
474
593
|
this.dropTurn(conv, 'The Pi session changed.')
|
|
@@ -493,8 +612,7 @@ export class AntigravityRuntime {
|
|
|
493
612
|
this.shutdown.abort(
|
|
494
613
|
new LlmError('Antigravity provider was disposed', 'TRANSPORT'),
|
|
495
614
|
)
|
|
496
|
-
|
|
497
|
-
this.sweeper = undefined
|
|
615
|
+
this.stopSweeper()
|
|
498
616
|
for (const conv of this.conversations.values()) {
|
|
499
617
|
conv.turn?.abandon('The harness provider was unloaded.')
|
|
500
618
|
this.clearSession(conv)
|
|
@@ -551,122 +669,131 @@ export class AntigravityRuntime {
|
|
|
551
669
|
}
|
|
552
670
|
return
|
|
553
671
|
}
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
const release = await this.mutex.acquire(key, this.shutdown.signal)
|
|
557
|
-
let conv: Conversation | undefined
|
|
672
|
+
this.activeStreams++
|
|
673
|
+
this.processIdleMs = 0
|
|
558
674
|
try {
|
|
559
|
-
this.
|
|
560
|
-
|
|
561
|
-
this.
|
|
562
|
-
conv
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
675
|
+
const proc = await this.ensureReady()
|
|
676
|
+
const key = options.sessionId ?? `anon-${randomBytes(6).toString('hex')}`
|
|
677
|
+
const release = await this.mutex.acquire(key, this.shutdown.signal)
|
|
678
|
+
let conv: Conversation | undefined
|
|
679
|
+
try {
|
|
680
|
+
this.assertActive()
|
|
681
|
+
conv = await this.acquireConversation(key)
|
|
682
|
+
this.assertOwner(proc, conv)
|
|
683
|
+
conv.lastUsed = this.monotonicNow()
|
|
684
|
+
conv.idleMs = 0
|
|
685
|
+
const split = splitSystem(options)
|
|
686
|
+
const tools = toBridgeTools(options.tools)
|
|
687
|
+
conv.toolNames = new Set(tools.map((t) => t.name))
|
|
688
|
+
const requestedEffort =
|
|
689
|
+
asEffort(options.reasoningEffort) ?? this.host.config.defaultEffort
|
|
690
|
+
let effort = this.effectiveEffort(options.model, requestedEffort)
|
|
691
|
+
|
|
692
|
+
if (options.signal?.aborted) {
|
|
693
|
+
this.dropTurn(conv, 'The harness cancelled this request.')
|
|
694
|
+
yield {
|
|
695
|
+
type: 'finish',
|
|
696
|
+
reason: {
|
|
697
|
+
kind: 'aborted',
|
|
698
|
+
failure: { message: 'Request aborted', code: 'ABORTED' },
|
|
699
|
+
},
|
|
700
|
+
}
|
|
701
|
+
return
|
|
578
702
|
}
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
703
|
+
if (conv.turn !== undefined && conv.turn.pendingCalls.size > 0) {
|
|
704
|
+
// Resuming a parked turn is only valid while the request still starts
|
|
705
|
+
// with the exact history and model/effort selection this ACP session
|
|
706
|
+
// already owns. A compaction checkpoint, pruned/rewritten prefix, or
|
|
707
|
+
// model/effort change means the live session is stale, so abandon the
|
|
708
|
+
// turn and let plan() rebuild from Pi's authoritative history.
|
|
709
|
+
const resumedEffort = clampEffortToOptions(conv.configOptions, effort)
|
|
710
|
+
if (
|
|
711
|
+
!this.historyPrefixIntact(conv, split.system, split.messages) ||
|
|
712
|
+
conv.model !== options.model ||
|
|
713
|
+
conv.effort !== resumedEffort
|
|
714
|
+
) {
|
|
715
|
+
this.dropTurn(
|
|
716
|
+
conv,
|
|
717
|
+
'The conversation history or model selection changed; rebuilding the session from the harness history.',
|
|
718
|
+
)
|
|
719
|
+
} else {
|
|
720
|
+
// Tool waiters can abort in their result microtask; hand off the exact turn synchronously.
|
|
721
|
+
const resumed = this.deliverResults(
|
|
722
|
+
conv,
|
|
723
|
+
split.messages,
|
|
724
|
+
proc,
|
|
725
|
+
tools,
|
|
726
|
+
)
|
|
727
|
+
this.assertActive()
|
|
728
|
+
if (resumed !== undefined) {
|
|
729
|
+
conv.knownIds = messageIds(split.messages)
|
|
730
|
+
yield* this.pump(conv, resumed, proc, options, resumedEffort)
|
|
731
|
+
return
|
|
732
|
+
}
|
|
733
|
+
this.dropTurn(
|
|
734
|
+
conv,
|
|
735
|
+
'The conversation history changed; the harness restarted this turn.',
|
|
736
|
+
)
|
|
609
737
|
}
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
'The conversation history changed; the harness restarted this turn.',
|
|
613
|
-
)
|
|
738
|
+
} else if (conv.turn !== undefined) {
|
|
739
|
+
this.dropTurn(conv, 'Superseded by a newer request.')
|
|
614
740
|
}
|
|
615
|
-
} else if (conv.turn !== undefined) {
|
|
616
|
-
this.dropTurn(conv, 'Superseded by a newer request.')
|
|
617
|
-
}
|
|
618
741
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
}
|
|
628
|
-
this.assertOwner(proc, conv, conv.sessionId)
|
|
629
|
-
await this.select(conv, proc, options.model, effort)
|
|
630
|
-
this.assertOwner(proc, conv, conv.sessionId)
|
|
631
|
-
let blocks: AcpPromptBlock[]
|
|
632
|
-
if (plan.rebuild) {
|
|
633
|
-
blocks = replayPrompt(plan.history, plan.input, {
|
|
634
|
-
system: split.system,
|
|
635
|
-
supplementalTools,
|
|
636
|
-
})
|
|
637
|
-
} else {
|
|
638
|
-
blocks = inputPrompt(plan.input)
|
|
639
|
-
const toolDelta = consumeToolDelta(conv, tools)
|
|
640
|
-
if (toolDelta.length > 0)
|
|
641
|
-
blocks.unshift({ type: 'text', text: `${toolDelta}\n\n` })
|
|
642
|
-
}
|
|
643
|
-
if (options.signal?.aborted) {
|
|
644
|
-
this.dropTurn(conv, 'The harness cancelled this request.')
|
|
645
|
-
yield {
|
|
646
|
-
type: 'finish',
|
|
647
|
-
reason: {
|
|
648
|
-
kind: 'aborted',
|
|
649
|
-
failure: { message: 'Request aborted', code: 'ABORTED' },
|
|
650
|
-
},
|
|
742
|
+
const plan = this.plan(conv, split.system, split.messages)
|
|
743
|
+
if (plan.rebuild) await this.resetSession(conv)
|
|
744
|
+
this.assertOwner(proc, conv)
|
|
745
|
+
let supplementalTools: readonly BridgeTool[] = []
|
|
746
|
+
if (conv.sessionId === undefined) {
|
|
747
|
+
conv.systemHash = digest(split.system)
|
|
748
|
+
supplementalTools = await this.openSession(conv, tools, proc)
|
|
749
|
+
effort = this.effectiveEffort(options.model, requestedEffort)
|
|
651
750
|
}
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
751
|
+
this.assertOwner(proc, conv, conv.sessionId)
|
|
752
|
+
effort = await this.select(conv, proc, options.model, effort)
|
|
753
|
+
this.assertOwner(proc, conv, conv.sessionId)
|
|
754
|
+
let blocks: AcpPromptBlock[]
|
|
755
|
+
if (plan.rebuild) {
|
|
756
|
+
blocks = replayPrompt(plan.history, plan.input, {
|
|
757
|
+
system: split.system,
|
|
758
|
+
supplementalTools,
|
|
759
|
+
})
|
|
760
|
+
} else {
|
|
761
|
+
blocks = inputPrompt(plan.input)
|
|
762
|
+
const toolDelta = consumeToolDelta(conv, tools)
|
|
763
|
+
if (toolDelta.length > 0)
|
|
764
|
+
blocks.unshift({ type: 'text', text: `${toolDelta}\n\n` })
|
|
765
|
+
}
|
|
766
|
+
if (options.signal?.aborted) {
|
|
767
|
+
this.dropTurn(conv, 'The harness cancelled this request.')
|
|
768
|
+
yield {
|
|
769
|
+
type: 'finish',
|
|
770
|
+
reason: {
|
|
771
|
+
kind: 'aborted',
|
|
772
|
+
failure: { message: 'Request aborted', code: 'ABORTED' },
|
|
773
|
+
},
|
|
774
|
+
}
|
|
775
|
+
return
|
|
666
776
|
}
|
|
777
|
+
conv.knownIds = messageIds(split.messages)
|
|
778
|
+
// A new turn starts with an empty reported-identity space: ids from past
|
|
779
|
+
// turns must never back a fresh permission request.
|
|
780
|
+
conv.reportedToolCalls.clear()
|
|
781
|
+
const turn = conv.turn = new AcpTurn(randomBytes(6).toString('hex'))
|
|
782
|
+
this.startPrompt(conv, turn, proc, blocks)
|
|
783
|
+
yield* this.pump(conv, turn, proc, options, effort)
|
|
667
784
|
} finally {
|
|
668
|
-
|
|
785
|
+
try {
|
|
786
|
+
if (conv !== undefined) {
|
|
787
|
+
conv.opening = false
|
|
788
|
+
await this.afterStep(conv)
|
|
789
|
+
}
|
|
790
|
+
} finally {
|
|
791
|
+
release()
|
|
792
|
+
}
|
|
669
793
|
}
|
|
794
|
+
} finally {
|
|
795
|
+
this.activeStreams--
|
|
796
|
+
this.processIdleMs = 0
|
|
670
797
|
}
|
|
671
798
|
}
|
|
672
799
|
|
|
@@ -682,6 +809,8 @@ export class AntigravityRuntime {
|
|
|
682
809
|
const existing = this.conversations.get(key)
|
|
683
810
|
if (existing !== undefined) {
|
|
684
811
|
existing.opening = true
|
|
812
|
+
existing.lastUsed = this.monotonicNow()
|
|
813
|
+
existing.idleMs = 0
|
|
685
814
|
return existing
|
|
686
815
|
}
|
|
687
816
|
const unlock = await this.capacityMutex.acquire(
|
|
@@ -693,6 +822,8 @@ export class AntigravityRuntime {
|
|
|
693
822
|
const again = this.conversations.get(key)
|
|
694
823
|
if (again !== undefined) {
|
|
695
824
|
again.opening = true
|
|
825
|
+
again.lastUsed = this.monotonicNow()
|
|
826
|
+
again.idleMs = 0
|
|
696
827
|
return again
|
|
697
828
|
}
|
|
698
829
|
const max = Math.max(1, this.host.config.maxLiveSessions)
|
|
@@ -740,7 +871,8 @@ export class AntigravityRuntime {
|
|
|
740
871
|
issuedIds: new Set(),
|
|
741
872
|
turn: undefined,
|
|
742
873
|
dirty: false,
|
|
743
|
-
lastUsed:
|
|
874
|
+
lastUsed: this.monotonicNow(),
|
|
875
|
+
idleMs: 0,
|
|
744
876
|
model: undefined,
|
|
745
877
|
metaFloorIdx: 0,
|
|
746
878
|
effort: undefined,
|
|
@@ -808,8 +940,7 @@ export class AntigravityRuntime {
|
|
|
808
940
|
if (this.lifecycle !== 'active') throw this.shutdown.signal.reason
|
|
809
941
|
await this.bridge.close()
|
|
810
942
|
this.assertActive()
|
|
811
|
-
|
|
812
|
-
this.sweeper = undefined
|
|
943
|
+
this.stopSweeper()
|
|
813
944
|
this.state = 'error'
|
|
814
945
|
this.lastError = error instanceof Error ? error.message : String(error)
|
|
815
946
|
throw mapError(error)
|
|
@@ -818,8 +949,9 @@ export class AntigravityRuntime {
|
|
|
818
949
|
|
|
819
950
|
private runPreflight(): Promise<PreflightResult> {
|
|
820
951
|
this.reconcileBinaryFingerprint()
|
|
952
|
+
const warn = (message: string) => this.host.log.warn(message)
|
|
821
953
|
if (this.preflight !== undefined) {
|
|
822
|
-
prepareHome(this.home)
|
|
954
|
+
prepareHome(this.home, undefined, undefined, warn)
|
|
823
955
|
return Promise.resolve(this.preflight)
|
|
824
956
|
}
|
|
825
957
|
this.preflightPromise ??= (async () => {
|
|
@@ -829,7 +961,7 @@ export class AntigravityRuntime {
|
|
|
829
961
|
const version = await readVersion(binary, this.host.runCommand)
|
|
830
962
|
this.assertActive()
|
|
831
963
|
assertSupportedAgyVersion(version, binary)
|
|
832
|
-
const workspace = prepareHome(this.home)
|
|
964
|
+
const workspace = prepareHome(this.home, undefined, undefined, warn)
|
|
833
965
|
const result: PreflightResult = {
|
|
834
966
|
binary,
|
|
835
967
|
version,
|
|
@@ -849,8 +981,11 @@ export class AntigravityRuntime {
|
|
|
849
981
|
private onProcessExit(proc: AcpProcess, error: Error): void {
|
|
850
982
|
const current = this.process === proc
|
|
851
983
|
if (current) {
|
|
984
|
+
this.stopSweeper()
|
|
852
985
|
this.process = undefined
|
|
853
986
|
this.processFingerprint = undefined
|
|
987
|
+
this.processIdleMs = 0
|
|
988
|
+
if (this.starting === undefined) void this.bridge.close()
|
|
854
989
|
}
|
|
855
990
|
if (current && this.lifecycle === 'active') {
|
|
856
991
|
this.state = 'error'
|
|
@@ -875,7 +1010,13 @@ export class AntigravityRuntime {
|
|
|
875
1010
|
proc: AcpProcess,
|
|
876
1011
|
): Promise<readonly BridgeTool[]> {
|
|
877
1012
|
this.assertOwner(proc, conv)
|
|
878
|
-
|
|
1013
|
+
try {
|
|
1014
|
+
syncOAuthToken(this.home)
|
|
1015
|
+
} catch (error) {
|
|
1016
|
+
this.host.log.warn(
|
|
1017
|
+
`antigravity-acp: OAuth token sync skipped: ${error instanceof Error ? error.message : String(error)}`,
|
|
1018
|
+
)
|
|
1019
|
+
}
|
|
879
1020
|
conv.reportedToolCalls.clear()
|
|
880
1021
|
const compacted = tools.map(compactBridgeTool)
|
|
881
1022
|
const supplementalTools = tools.filter(
|
|
@@ -924,20 +1065,33 @@ export class AntigravityRuntime {
|
|
|
924
1065
|
if (this.catalog.observe(update.configOptions))
|
|
925
1066
|
this.host.onModelsChanged()
|
|
926
1067
|
}
|
|
927
|
-
if (update.sessionUpdate === 'usage_update') {
|
|
928
|
-
this.observeSessionContextWindow(
|
|
929
|
-
conv.sessionId,
|
|
930
|
-
conv.model,
|
|
931
|
-
conv.metaFloorIdx,
|
|
932
|
-
conv.turn,
|
|
933
|
-
)
|
|
934
|
-
}
|
|
935
1068
|
if (recordReportedToolCall(conv, update) === 'conflict') {
|
|
936
1069
|
this.host.log.warn(
|
|
937
1070
|
`antigravity-acp: agent reported conflicting identity for tool call ${String(update.toolCallId)}`,
|
|
938
1071
|
)
|
|
939
1072
|
}
|
|
940
1073
|
conv.turn?.onUpdate(update)
|
|
1074
|
+
if (update.sessionUpdate === 'usage_update') {
|
|
1075
|
+
const hasCachedContextWindow =
|
|
1076
|
+
conv.model !== undefined &&
|
|
1077
|
+
this.catalog.contextWindowFor(conv.model) !== undefined
|
|
1078
|
+
const hasPositiveUsed =
|
|
1079
|
+
typeof update.used === 'number' &&
|
|
1080
|
+
Number.isFinite(update.used) &&
|
|
1081
|
+
update.used > 0
|
|
1082
|
+
if (!(hasCachedContextWindow && hasPositiveUsed)) {
|
|
1083
|
+
this.harvestSessionTurnData(
|
|
1084
|
+
conv,
|
|
1085
|
+
conv.sessionId,
|
|
1086
|
+
conv.model,
|
|
1087
|
+
conv.metaFloorIdx,
|
|
1088
|
+
{
|
|
1089
|
+
turn: conv.turn,
|
|
1090
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1091
|
+
},
|
|
1092
|
+
)
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
941
1095
|
},
|
|
942
1096
|
onPermission: (request) => this.owns(proc, conv) && conv.tenantKey === tenantKey
|
|
943
1097
|
? this.answerPermission(conv, request)
|
|
@@ -956,6 +1110,7 @@ export class AntigravityRuntime {
|
|
|
956
1110
|
conv.configOptions = result.configOptions ?? []
|
|
957
1111
|
conv.model = undefined
|
|
958
1112
|
conv.metaFloorIdx = 0
|
|
1113
|
+
conv.lastDbStamp = undefined
|
|
959
1114
|
conv.effort = undefined
|
|
960
1115
|
|
|
961
1116
|
conv.dirty = false
|
|
@@ -1091,7 +1246,7 @@ export class AntigravityRuntime {
|
|
|
1091
1246
|
proc: AcpProcess,
|
|
1092
1247
|
model: string,
|
|
1093
1248
|
effort: Effort,
|
|
1094
|
-
): Promise<
|
|
1249
|
+
): Promise<Effort> {
|
|
1095
1250
|
const sessionId = conv.sessionId as string
|
|
1096
1251
|
this.assertOwner(proc, conv, sessionId)
|
|
1097
1252
|
const modelOption = conv.configOptions.find(
|
|
@@ -1113,8 +1268,9 @@ export class AntigravityRuntime {
|
|
|
1113
1268
|
const previousModel = conv.model
|
|
1114
1269
|
const previousFloor = conv.metaFloorIdx
|
|
1115
1270
|
if (previousModel !== undefined && previousModel !== model) {
|
|
1116
|
-
this.
|
|
1271
|
+
this.harvestSessionTurnData(conv, sessionId, previousModel, previousFloor)
|
|
1117
1272
|
conv.metaFloorIdx = null
|
|
1273
|
+
conv.lastDbStamp = undefined
|
|
1118
1274
|
}
|
|
1119
1275
|
if (conv.model !== model && modelOption?.currentValue !== model) {
|
|
1120
1276
|
await this.applyConfigOption(
|
|
@@ -1126,27 +1282,48 @@ export class AntigravityRuntime {
|
|
|
1126
1282
|
)
|
|
1127
1283
|
}
|
|
1128
1284
|
if (previousModel !== undefined && previousModel !== model) {
|
|
1129
|
-
const
|
|
1285
|
+
const cursors = readSessionCursors(this.home, sessionId)
|
|
1130
1286
|
conv.metaFloorIdx =
|
|
1131
|
-
previousFloor === null ||
|
|
1287
|
+
previousFloor === null || cursors === null
|
|
1132
1288
|
? null
|
|
1133
|
-
: Math.max(previousFloor,
|
|
1289
|
+
: Math.max(previousFloor, cursors.nextMetaIdx)
|
|
1290
|
+
conv.lastDbStamp = undefined
|
|
1134
1291
|
}
|
|
1135
1292
|
conv.model = model
|
|
1136
1293
|
const levelOption = conv.configOptions.find(
|
|
1137
1294
|
(o) => o.id === 'thought_level' || o.category === 'thought_level',
|
|
1138
1295
|
)
|
|
1296
|
+
const offeredEfforts =
|
|
1297
|
+
levelOption?.options !== undefined
|
|
1298
|
+
? flattenConfigOptions(levelOption.options)
|
|
1299
|
+
.map((o) => asEffort(o.value))
|
|
1300
|
+
.filter((v): v is Effort => v !== undefined)
|
|
1301
|
+
: undefined
|
|
1302
|
+
const targetEffort =
|
|
1303
|
+
offeredEfforts !== undefined && offeredEfforts.length > 0
|
|
1304
|
+
? offeredEfforts.includes(effort)
|
|
1305
|
+
? effort
|
|
1306
|
+
: nearestEffort(effort, offeredEfforts)
|
|
1307
|
+
: effort
|
|
1139
1308
|
// Fixed-variant families (GPT-OSS) and single-variant orchestrators reject
|
|
1140
|
-
// thought_level writes, so only selectable families write it.
|
|
1309
|
+
// thought_level writes, so only selectable families with offered levels write it.
|
|
1141
1310
|
if (
|
|
1142
1311
|
this.catalog.supportsEffortSelection(model) &&
|
|
1143
1312
|
levelOption !== undefined &&
|
|
1144
|
-
|
|
1145
|
-
|
|
1313
|
+
(offeredEfforts === undefined || offeredEfforts.length > 0) &&
|
|
1314
|
+
conv.effort !== targetEffort &&
|
|
1315
|
+
levelOption.currentValue !== targetEffort
|
|
1146
1316
|
) {
|
|
1147
|
-
await this.applyConfigOption(
|
|
1317
|
+
await this.applyConfigOption(
|
|
1318
|
+
conv,
|
|
1319
|
+
proc,
|
|
1320
|
+
sessionId,
|
|
1321
|
+
levelOption.id,
|
|
1322
|
+
targetEffort,
|
|
1323
|
+
)
|
|
1148
1324
|
}
|
|
1149
|
-
conv.effort =
|
|
1325
|
+
conv.effort = targetEffort
|
|
1326
|
+
return targetEffort
|
|
1150
1327
|
}
|
|
1151
1328
|
|
|
1152
1329
|
private async applyConfigOption(
|
|
@@ -1163,6 +1340,9 @@ export class AntigravityRuntime {
|
|
|
1163
1340
|
})
|
|
1164
1341
|
this.assertOwner(proc, conv, sessionId)
|
|
1165
1342
|
conv.configOptions = configOptions
|
|
1343
|
+
if (this.catalog.observe(configOptions)) {
|
|
1344
|
+
this.host.onModelsChanged()
|
|
1345
|
+
}
|
|
1166
1346
|
}
|
|
1167
1347
|
|
|
1168
1348
|
private plan(
|
|
@@ -1253,7 +1433,9 @@ export class AntigravityRuntime {
|
|
|
1253
1433
|
}
|
|
1254
1434
|
|
|
1255
1435
|
private async resetSession(conv: Conversation): Promise<void> {
|
|
1256
|
-
this.
|
|
1436
|
+
this.harvestSessionTurnData(conv, conv.sessionId, conv.model, conv.metaFloorIdx, {
|
|
1437
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1438
|
+
})
|
|
1257
1439
|
let closing: Promise<void> | undefined
|
|
1258
1440
|
if (conv.sessionId !== undefined && conv.process !== undefined) {
|
|
1259
1441
|
closing = this.closeAndCleanSession(conv.process, conv.sessionId)
|
|
@@ -1271,6 +1453,7 @@ export class AntigravityRuntime {
|
|
|
1271
1453
|
conv.tenantSecret = undefined
|
|
1272
1454
|
conv.process = undefined
|
|
1273
1455
|
conv.metaFloorIdx = null
|
|
1456
|
+
conv.lastDbStamp = undefined
|
|
1274
1457
|
conv.advertisedTools = []
|
|
1275
1458
|
conv.advertisedToolDigests.clear()
|
|
1276
1459
|
conv.knownTools.clear()
|
|
@@ -1290,12 +1473,18 @@ export class AntigravityRuntime {
|
|
|
1290
1473
|
const sessionId = conv.sessionId as string
|
|
1291
1474
|
const model = conv.model
|
|
1292
1475
|
const modelFloor = conv.metaFloorIdx
|
|
1293
|
-
turn.
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1476
|
+
turn.bindPrompt(
|
|
1477
|
+
this.runPromptSegment(
|
|
1478
|
+
conv,
|
|
1479
|
+
turn,
|
|
1480
|
+
proc,
|
|
1481
|
+
sessionId,
|
|
1482
|
+
model,
|
|
1483
|
+
modelFloor,
|
|
1484
|
+
blocks,
|
|
1485
|
+
),
|
|
1486
|
+
{ mapError },
|
|
1487
|
+
)
|
|
1299
1488
|
} catch (error) {
|
|
1300
1489
|
turn.fail(mapError(error))
|
|
1301
1490
|
}
|
|
@@ -1350,33 +1539,16 @@ export class AntigravityRuntime {
|
|
|
1350
1539
|
{
|
|
1351
1540
|
parkSettleMs: this.host.config.parkSettleMs,
|
|
1352
1541
|
maxContinuations: this.host.config.longDecodeContinuations,
|
|
1353
|
-
continuePrompt:
|
|
1354
|
-
|
|
1542
|
+
continuePrompt: () =>
|
|
1543
|
+
this.runPromptSegment(
|
|
1544
|
+
conv,
|
|
1545
|
+
turn,
|
|
1546
|
+
proc,
|
|
1355
1547
|
sessionId,
|
|
1548
|
+
options.model,
|
|
1356
1549
|
metaFloorIdx,
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
try {
|
|
1360
|
-
const result = await proc.prompt(sessionId, [
|
|
1361
|
-
{ type: 'text', text: CONTINUE_PROMPT },
|
|
1362
|
-
])
|
|
1363
|
-
this.observeSessionContextWindow(
|
|
1364
|
-
sessionId,
|
|
1365
|
-
options.model,
|
|
1366
|
-
metaFloorIdx,
|
|
1367
|
-
turn,
|
|
1368
|
-
)
|
|
1369
|
-
return result
|
|
1370
|
-
} catch (error) {
|
|
1371
|
-
this.observeSessionContextWindow(
|
|
1372
|
-
sessionId,
|
|
1373
|
-
options.model,
|
|
1374
|
-
metaFloorIdx,
|
|
1375
|
-
turn,
|
|
1376
|
-
)
|
|
1377
|
-
throw error
|
|
1378
|
-
}
|
|
1379
|
-
},
|
|
1550
|
+
[{ type: 'text', text: CONTINUE_PROMPT }],
|
|
1551
|
+
),
|
|
1380
1552
|
cancel: () => {
|
|
1381
1553
|
if (conv.turn === turn) proc.cancel(sessionId)
|
|
1382
1554
|
},
|
|
@@ -1389,7 +1561,15 @@ export class AntigravityRuntime {
|
|
|
1389
1561
|
conv.issuedIds.add(chunk.block.id)
|
|
1390
1562
|
if (chunk.type === 'finish') {
|
|
1391
1563
|
finishKind = chunk.reason.kind
|
|
1392
|
-
|
|
1564
|
+
if (
|
|
1565
|
+
finishKind === 'aborted' ||
|
|
1566
|
+
(finishKind === 'tool-calls' &&
|
|
1567
|
+
this.catalog.contextWindowFor(options.model) === undefined)
|
|
1568
|
+
) {
|
|
1569
|
+
this.harvestSessionTurnData(conv, sessionId, options.model, metaFloorIdx, {
|
|
1570
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1571
|
+
})
|
|
1572
|
+
}
|
|
1393
1573
|
}
|
|
1394
1574
|
yield chunk
|
|
1395
1575
|
if (chunk.type === 'finish' && chunk.reason.kind === 'aborted') break
|
|
@@ -1397,13 +1577,17 @@ export class AntigravityRuntime {
|
|
|
1397
1577
|
const interrupted = turn.guard(undefined, signal)
|
|
1398
1578
|
if (interrupted !== undefined && finishKind !== 'aborted') {
|
|
1399
1579
|
finishKind = 'aborted'
|
|
1400
|
-
this.
|
|
1580
|
+
this.harvestSessionTurnData(conv, sessionId, options.model, metaFloorIdx, {
|
|
1581
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1582
|
+
})
|
|
1401
1583
|
yield interrupted
|
|
1402
1584
|
}
|
|
1403
1585
|
drained = true
|
|
1404
1586
|
} catch (error) {
|
|
1405
1587
|
finishKind = 'error'
|
|
1406
|
-
this.
|
|
1588
|
+
this.harvestSessionTurnData(conv, sessionId, options.model, metaFloorIdx, {
|
|
1589
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1590
|
+
})
|
|
1407
1591
|
throw mapError(error)
|
|
1408
1592
|
} finally {
|
|
1409
1593
|
// Only a fully consumed tool handoff keeps the turn and its cancellation owner.
|
|
@@ -1539,7 +1723,9 @@ export class AntigravityRuntime {
|
|
|
1539
1723
|
}
|
|
1540
1724
|
|
|
1541
1725
|
private async afterStep(conv: Conversation): Promise<void> {
|
|
1542
|
-
conv.lastUsed =
|
|
1726
|
+
conv.lastUsed = this.monotonicNow()
|
|
1727
|
+
conv.idleMs = 0
|
|
1728
|
+
this.processIdleMs = 0
|
|
1543
1729
|
const idle = this.isIdle(conv)
|
|
1544
1730
|
if (idle && conv.key.startsWith('anon-')) {
|
|
1545
1731
|
await this.resetSession(conv)
|
|
@@ -1593,8 +1779,12 @@ export class AntigravityRuntime {
|
|
|
1593
1779
|
? request.arguments.call_id
|
|
1594
1780
|
: ''
|
|
1595
1781
|
return (
|
|
1596
|
-
turn.awaitRelayedCall(
|
|
1597
|
-
|
|
1782
|
+
turn.awaitRelayedCall(
|
|
1783
|
+
callId,
|
|
1784
|
+
relayAfterMs,
|
|
1785
|
+
request.signal,
|
|
1786
|
+
request.commit,
|
|
1787
|
+
) ?? toolError(`Unknown harness call id ${callId}.`)
|
|
1598
1788
|
)
|
|
1599
1789
|
}
|
|
1600
1790
|
let name = request.name
|
|
@@ -1617,23 +1807,138 @@ export class AntigravityRuntime {
|
|
|
1617
1807
|
}
|
|
1618
1808
|
if (!conv.toolNames.has(name))
|
|
1619
1809
|
return toolError(`The harness has no tool named ${name}.`)
|
|
1620
|
-
return turn.invokeTool(
|
|
1810
|
+
return turn.invokeTool(
|
|
1811
|
+
name,
|
|
1812
|
+
args,
|
|
1813
|
+
relayAfterMs,
|
|
1814
|
+
request.signal,
|
|
1815
|
+
request.commit,
|
|
1816
|
+
)
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1819
|
+
private stopSweeper(): void {
|
|
1820
|
+
if (this.sweeper !== undefined) {
|
|
1821
|
+
clearInterval(this.sweeper)
|
|
1822
|
+
this.sweeper = undefined
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
private hasLiveConversations(): boolean {
|
|
1827
|
+
for (const conv of this.conversations.values()) {
|
|
1828
|
+
if (conv.sessionId !== undefined || !this.isIdle(conv)) return true
|
|
1829
|
+
}
|
|
1830
|
+
return false
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
private detachProcess(proc: AcpProcess): void {
|
|
1834
|
+
if (this.process !== proc) return
|
|
1835
|
+
this.stopSweeper()
|
|
1836
|
+
this.process = undefined
|
|
1837
|
+
this.processFingerprint = undefined
|
|
1838
|
+
this.processIdleMs = 0
|
|
1839
|
+
if (this.lifecycle === 'active') this.state = 'idle'
|
|
1840
|
+
proc.stop()
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
private async stopIdleProcess(
|
|
1844
|
+
proc: AcpProcess,
|
|
1845
|
+
options: { ignoreWarmup?: boolean } = {},
|
|
1846
|
+
): Promise<void> {
|
|
1847
|
+
if (
|
|
1848
|
+
this.process !== proc ||
|
|
1849
|
+
this.lifecycle !== 'active' ||
|
|
1850
|
+
this.starting !== undefined ||
|
|
1851
|
+
this.activeStreams > 0 ||
|
|
1852
|
+
(!options.ignoreWarmup && this.warmupPromise !== undefined) ||
|
|
1853
|
+
this.hasLiveConversations()
|
|
1854
|
+
) {
|
|
1855
|
+
return
|
|
1856
|
+
}
|
|
1857
|
+
this.detachProcess(proc)
|
|
1858
|
+
await this.bridge.close()
|
|
1621
1859
|
}
|
|
1622
1860
|
|
|
1623
1861
|
private sweep(): void {
|
|
1624
1862
|
const ttl = this.host.config.idleSessionTtlMs
|
|
1625
|
-
const now =
|
|
1863
|
+
const now = this.monotonicNow()
|
|
1864
|
+
const delta = Math.max(0, now - this.lastSweepAt)
|
|
1865
|
+
this.lastSweepAt = now
|
|
1866
|
+
const slice = Math.min(delta, SWEEP_INTERVAL_MS * 2)
|
|
1867
|
+
let allIdle =
|
|
1868
|
+
this.activeStreams === 0 &&
|
|
1869
|
+
this.warmupPromise === undefined &&
|
|
1870
|
+
this.starting === undefined
|
|
1871
|
+
const evictions: Promise<void>[] = []
|
|
1626
1872
|
for (const conv of [...this.conversations.values()]) {
|
|
1627
|
-
if (this.isIdle(conv)
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1873
|
+
if (!this.isIdle(conv)) {
|
|
1874
|
+
allIdle = false
|
|
1875
|
+
continue
|
|
1876
|
+
}
|
|
1877
|
+
conv.idleMs += slice
|
|
1878
|
+
if (conv.idleMs > ttl) {
|
|
1879
|
+
evictions.push(
|
|
1880
|
+
this.resetSession(conv).catch((error: unknown) => {
|
|
1881
|
+
this.host.log.warn(
|
|
1882
|
+
`session sweep cleanup failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1883
|
+
)
|
|
1884
|
+
}),
|
|
1885
|
+
)
|
|
1633
1886
|
this.conversations.delete(conv.key)
|
|
1634
1887
|
}
|
|
1635
1888
|
}
|
|
1889
|
+
if (!allIdle || this.process?.alive !== true) {
|
|
1890
|
+
this.processIdleMs = 0
|
|
1891
|
+
return
|
|
1892
|
+
}
|
|
1893
|
+
this.processIdleMs += slice
|
|
1894
|
+
if (this.processIdleMs > ttl && !this.hasLiveConversations()) {
|
|
1895
|
+
const proc = this.process
|
|
1896
|
+
if (evictions.length === 0) {
|
|
1897
|
+
void this.stopIdleProcess(proc)
|
|
1898
|
+
} else {
|
|
1899
|
+
void Promise.allSettled(evictions).then(() =>
|
|
1900
|
+
this.stopIdleProcess(proc),
|
|
1901
|
+
)
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
function clampEffortToOptions(
|
|
1908
|
+
configOptions: readonly AcpConfigOption[],
|
|
1909
|
+
effort: Effort,
|
|
1910
|
+
): Effort {
|
|
1911
|
+
const levelOption = configOptions.find(
|
|
1912
|
+
(o) => o.id === 'thought_level' || o.category === 'thought_level',
|
|
1913
|
+
)
|
|
1914
|
+
if (levelOption?.options === undefined) return effort
|
|
1915
|
+
const offered = flattenConfigOptions(levelOption.options)
|
|
1916
|
+
.map((o) => asEffort(o.value))
|
|
1917
|
+
.filter((v): v is Effort => v !== undefined)
|
|
1918
|
+
if (offered.length === 0 || offered.includes(effort)) return effort
|
|
1919
|
+
return nearestEffort(effort, offered)
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
function statSignature(path: string): string {
|
|
1923
|
+
try {
|
|
1924
|
+
const st = statSync(path, { bigint: true })
|
|
1925
|
+
return `${st.isDirectory() ? 'd' : 'f'}:${st.ino}:${st.mtimeNs}:${st.ctimeNs}:${st.size}`
|
|
1926
|
+
} catch {
|
|
1927
|
+
return 'missing'
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
function logPathsFingerprint(paths: readonly string[]): string {
|
|
1932
|
+
const parts: string[] = []
|
|
1933
|
+
for (const p of paths) {
|
|
1934
|
+
const sig = statSignature(p)
|
|
1935
|
+
parts.push(`${p}=${sig}`)
|
|
1936
|
+
if (sig.startsWith('d:')) {
|
|
1937
|
+
parts.push(`${p}/cli.log=${statSignature(join(p, 'cli.log'))}`)
|
|
1938
|
+
parts.push(`${p}/log=${statSignature(join(p, 'log'))}`)
|
|
1939
|
+
}
|
|
1636
1940
|
}
|
|
1941
|
+
return parts.join('|')
|
|
1637
1942
|
}
|
|
1638
1943
|
|
|
1639
1944
|
function messageIds(messages: readonly RequestMessage[]): string[] {
|