pi-agy-acp 0.2.2 → 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 +7 -0
- package/README.md +11 -7
- package/docs/architecture.md +262 -0
- package/docs/development.md +68 -165
- package/docs/reference.md +150 -134
- package/docs/releasing.md +56 -0
- package/index.ts +76 -20
- package/package.json +1 -1
- package/src/catalog.ts +37 -29
- package/src/config.ts +1 -1
- package/src/runtime.ts +252 -148
package/src/catalog.ts
CHANGED
|
@@ -114,11 +114,11 @@ export class ModelCatalog {
|
|
|
114
114
|
private supportsImageInput: boolean | undefined
|
|
115
115
|
/** Latest `availableModels` snapshot, kept for later option updates. */
|
|
116
116
|
private available: readonly AvailableModel[] | undefined
|
|
117
|
-
/**
|
|
118
|
-
private
|
|
119
|
-
/**
|
|
120
|
-
private
|
|
121
|
-
/** Whether a live ACP session has reported the catalog. */
|
|
117
|
+
/** Current on-disk `agy` binary fingerprint, if resolved. */
|
|
118
|
+
private currentFingerprint: string | undefined
|
|
119
|
+
/** Fingerprint of the `agy` binary that produced the cached catalog. */
|
|
120
|
+
private observedFingerprint: string | undefined
|
|
121
|
+
/** Whether a live ACP session has reported the catalog in this process. */
|
|
122
122
|
observed = false
|
|
123
123
|
private readonly cachePath: string
|
|
124
124
|
|
|
@@ -128,10 +128,22 @@ export class ModelCatalog {
|
|
|
128
128
|
*/
|
|
129
129
|
constructor(cachePath: string, binaryFingerprint?: string) {
|
|
130
130
|
this.cachePath = cachePath
|
|
131
|
+
this.currentFingerprint = binaryFingerprint
|
|
131
132
|
this.models = this.load(binaryFingerprint) ?? []
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Whether disk-cached models match the current `agy` binary fingerprint. */
|
|
136
|
+
hasFreshCache(): boolean {
|
|
137
|
+
return (
|
|
138
|
+
this.models.length > 0 &&
|
|
139
|
+
(this.currentFingerprint === undefined ||
|
|
140
|
+
this.observedFingerprint === this.currentFingerprint)
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Whether the catalog is current without probing ACP. */
|
|
145
|
+
isCurrent(): boolean {
|
|
146
|
+
return this.observed || this.hasFreshCache()
|
|
135
147
|
}
|
|
136
148
|
|
|
137
149
|
/** Input modalities advertised to Pi, derived from `initialize` `promptCapabilities.image`. */
|
|
@@ -160,20 +172,15 @@ export class ModelCatalog {
|
|
|
160
172
|
* @returns whether cached context windows were invalidated.
|
|
161
173
|
*/
|
|
162
174
|
syncBinaryFingerprint(fingerprint: string | undefined): boolean {
|
|
163
|
-
if (!fingerprint) return false
|
|
164
|
-
|
|
175
|
+
if (!fingerprint || this.currentFingerprint === fingerprint) return false
|
|
176
|
+
this.currentFingerprint = fingerprint
|
|
177
|
+
if (this.observedFingerprint === fingerprint) return false
|
|
165
178
|
const hadContextWindows = this.contextWindows.size > 0
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (
|
|
170
|
-
|
|
171
|
-
this.observed = false
|
|
172
|
-
this.fingerprintDirty = true
|
|
173
|
-
this.save()
|
|
174
|
-
return hadContextWindows
|
|
175
|
-
}
|
|
176
|
-
return false
|
|
179
|
+
this.contextWindows.clear()
|
|
180
|
+
this.observed = false
|
|
181
|
+
// Persist cleared contextWindows while keeping observedFingerprint intact until live ACP re-observes.
|
|
182
|
+
if (hadContextWindows) this.save()
|
|
183
|
+
return hadContextWindows
|
|
177
184
|
}
|
|
178
185
|
|
|
179
186
|
/**
|
|
@@ -304,6 +311,11 @@ export class ModelCatalog {
|
|
|
304
311
|
this.observed = true
|
|
305
312
|
const changed = JSON.stringify(next) !== JSON.stringify(this.models)
|
|
306
313
|
if (changed) this.models = next
|
|
314
|
+
const fingerprintChanged =
|
|
315
|
+
modelEntries.length > 0 &&
|
|
316
|
+
this.currentFingerprint !== undefined &&
|
|
317
|
+
this.observedFingerprint !== this.currentFingerprint
|
|
318
|
+
if (fingerprintChanged) this.observedFingerprint = this.currentFingerprint
|
|
307
319
|
let prunedChanged = false
|
|
308
320
|
if (modelEntries.length > 0) {
|
|
309
321
|
const activeIds = new Set(this.models.map((m) => m.id))
|
|
@@ -329,7 +341,7 @@ export class ModelCatalog {
|
|
|
329
341
|
prunedChanged ||
|
|
330
342
|
derivedChanged ||
|
|
331
343
|
beforeEfforts !== JSON.stringify(this.efforts)
|
|
332
|
-
if (changed || effortsChanged || tierChanged ||
|
|
344
|
+
if (changed || effortsChanged || tierChanged || fingerprintChanged)
|
|
333
345
|
this.save()
|
|
334
346
|
return changed || effortsChanged
|
|
335
347
|
}
|
|
@@ -395,8 +407,8 @@ export class ModelCatalog {
|
|
|
395
407
|
...(this.supportsImageInput !== undefined
|
|
396
408
|
? { supportsImage: this.supportsImageInput }
|
|
397
409
|
: {}),
|
|
398
|
-
...(this.
|
|
399
|
-
? { binaryFingerprint: this.
|
|
410
|
+
...(this.observedFingerprint !== undefined
|
|
411
|
+
? { binaryFingerprint: this.observedFingerprint }
|
|
400
412
|
: {}),
|
|
401
413
|
},
|
|
402
414
|
null,
|
|
@@ -404,7 +416,6 @@ export class ModelCatalog {
|
|
|
404
416
|
)}\n`,
|
|
405
417
|
)
|
|
406
418
|
renameSync(tempPath, this.cachePath)
|
|
407
|
-
this.fingerprintDirty = false
|
|
408
419
|
} catch (_cacheWrite) {
|
|
409
420
|
try {
|
|
410
421
|
unlinkSync(tempPath)
|
|
@@ -436,10 +447,7 @@ export class ModelCatalog {
|
|
|
436
447
|
const fingerprintMatches =
|
|
437
448
|
expectedFingerprint === undefined ||
|
|
438
449
|
storedFingerprint === expectedFingerprint
|
|
439
|
-
this.
|
|
440
|
-
? (storedFingerprint ?? expectedFingerprint)
|
|
441
|
-
: expectedFingerprint
|
|
442
|
-
this.fingerprintDirty = !fingerprintMatches
|
|
450
|
+
this.observedFingerprint = storedFingerprint
|
|
443
451
|
this.tierInfo = normalizeTier(parsed.tier)
|
|
444
452
|
if (typeof parsed.supportsImage === 'boolean') {
|
|
445
453
|
this.supportsImageInput = parsed.supportsImage
|
package/src/config.ts
CHANGED
|
@@ -33,7 +33,7 @@ export interface Config {
|
|
|
33
33
|
defaultEffort: Effort
|
|
34
34
|
/** Maximum concurrent ACP sessions kept alive across conversations, subagents, and auxiliary calls. */
|
|
35
35
|
maxLiveSessions: number
|
|
36
|
-
/** Idle time after which an
|
|
36
|
+
/** Idle time after which inactive ACP sessions and an otherwise idle ACP process are closed. */
|
|
37
37
|
idleSessionTtlMs: number
|
|
38
38
|
/** Window that groups parallel tool calls into one Pi step. */
|
|
39
39
|
parkSettleMs: number
|
package/src/runtime.ts
CHANGED
|
@@ -207,6 +207,10 @@ export class AntigravityRuntime {
|
|
|
207
207
|
private sweeper: ReturnType<typeof setInterval> | undefined
|
|
208
208
|
private readonly monotonicNow: () => number
|
|
209
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
|
|
210
214
|
private emailCache:
|
|
211
215
|
| { fingerprint: string; email: string | undefined; at: number }
|
|
212
216
|
| undefined
|
|
@@ -273,20 +277,26 @@ export class AntigravityRuntime {
|
|
|
273
277
|
}
|
|
274
278
|
if (
|
|
275
279
|
this.process?.alive &&
|
|
276
|
-
this.starting === undefined &&
|
|
277
280
|
this.processFingerprint !== undefined &&
|
|
278
|
-
this.processFingerprint !== fp
|
|
279
|
-
[...this.conversations.values()].every((c) => this.isIdle(c))
|
|
281
|
+
this.processFingerprint !== fp
|
|
280
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
|
+
}
|
|
281
293
|
// Restart an idle ACP process after an on-disk `agy` upgrade so discovery probes the new binary.
|
|
282
294
|
const stale = this.process
|
|
283
295
|
const staleSessions = [...this.conversations.values()]
|
|
284
296
|
.filter((conv) => conv.process === stale)
|
|
285
297
|
.map((conv) => ({ conv, sessionId: conv.sessionId }))
|
|
286
|
-
this.
|
|
287
|
-
this.
|
|
288
|
-
this.state = 'idle'
|
|
289
|
-
stale.stop()
|
|
298
|
+
this.detachProcess(stale)
|
|
299
|
+
void this.bridge.close()
|
|
290
300
|
for (const { conv, sessionId } of staleSessions) {
|
|
291
301
|
if (sessionId !== undefined) {
|
|
292
302
|
removeSessionArtifacts(this.home, sessionId)
|
|
@@ -469,12 +479,12 @@ export class AntigravityRuntime {
|
|
|
469
479
|
async readQuota(force = false): Promise<QuotaSnapshot> {
|
|
470
480
|
this.assertActive()
|
|
471
481
|
const quota = this.quota.read(force)
|
|
472
|
-
if (!this.catalog.tier()) {
|
|
482
|
+
if (!this.catalog.tier() && !this.catalog.observed) {
|
|
473
483
|
this.tierDiscovery ??= (async () => {
|
|
474
484
|
const state = this.state
|
|
475
485
|
const error = this.lastError
|
|
476
486
|
try {
|
|
477
|
-
await this.warmup()
|
|
487
|
+
await this.warmup({ force: true })
|
|
478
488
|
} catch {
|
|
479
489
|
// Optional account discovery must not leave an otherwise idle runtime in error.
|
|
480
490
|
if (
|
|
@@ -499,6 +509,7 @@ export class AntigravityRuntime {
|
|
|
499
509
|
*/
|
|
500
510
|
async ensureReady(): Promise<AcpProcess> {
|
|
501
511
|
this.assertActive()
|
|
512
|
+
this.processIdleMs = 0
|
|
502
513
|
if (this.sweeper === undefined) {
|
|
503
514
|
this.lastSweepAt = this.monotonicNow()
|
|
504
515
|
this.sweeper = setInterval(() => this.sweep(), SWEEP_INTERVAL_MS)
|
|
@@ -515,7 +526,10 @@ export class AntigravityRuntime {
|
|
|
515
526
|
}
|
|
516
527
|
|
|
517
528
|
/**
|
|
518
|
-
*
|
|
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.
|
|
519
533
|
* @returns resolves when discovery finished.
|
|
520
534
|
*/
|
|
521
535
|
async warmup(
|
|
@@ -526,30 +540,43 @@ export class AntigravityRuntime {
|
|
|
526
540
|
if (this.warmupPromise !== undefined)
|
|
527
541
|
return abortable(this.warmupPromise, options.signal)
|
|
528
542
|
this.reconcileBinaryFingerprint()
|
|
529
|
-
if (!options.force && this.catalog.
|
|
543
|
+
if (!options.force && this.catalog.isCurrent()) return
|
|
544
|
+
const hadProcess =
|
|
545
|
+
this.process?.alive === true || this.starting !== undefined
|
|
530
546
|
this.warmupPromise = (async () => {
|
|
531
|
-
|
|
532
|
-
this.assertOwner(proc)
|
|
533
|
-
const result = await proc.newSession(
|
|
534
|
-
{
|
|
535
|
-
cwd: this.preflight?.workspace ?? this.home,
|
|
536
|
-
mcpServers: [],
|
|
537
|
-
},
|
|
538
|
-
{ onUpdate: () => undefined },
|
|
539
|
-
120_000,
|
|
540
|
-
)
|
|
547
|
+
let proc: AcpProcess | undefined
|
|
541
548
|
try {
|
|
549
|
+
proc = await this.ensureReady()
|
|
542
550
|
this.assertOwner(proc)
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
551
|
+
const result = await proc
|
|
552
|
+
.newSession(
|
|
553
|
+
{
|
|
554
|
+
cwd: this.preflight?.workspace ?? this.home,
|
|
555
|
+
mcpServers: [],
|
|
556
|
+
},
|
|
557
|
+
{ onUpdate: () => undefined },
|
|
558
|
+
120_000,
|
|
548
559
|
)
|
|
549
|
-
|
|
550
|
-
|
|
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
|
+
}
|
|
551
576
|
} finally {
|
|
552
|
-
|
|
577
|
+
if (!hadProcess && proc !== undefined) {
|
|
578
|
+
await this.stopIdleProcess(proc, { ignoreWarmup: true })
|
|
579
|
+
}
|
|
553
580
|
}
|
|
554
581
|
})().finally(() => {
|
|
555
582
|
this.warmupPromise = undefined
|
|
@@ -560,6 +587,7 @@ export class AntigravityRuntime {
|
|
|
560
587
|
/** Close conversation sessions while retaining the process and discovery/quota caches. */
|
|
561
588
|
async resetConversations(): Promise<void> {
|
|
562
589
|
this.assertActive()
|
|
590
|
+
this.processIdleMs = 0
|
|
563
591
|
const conversations = [...this.conversations.values()]
|
|
564
592
|
for (const conv of conversations)
|
|
565
593
|
this.dropTurn(conv, 'The Pi session changed.')
|
|
@@ -584,8 +612,7 @@ export class AntigravityRuntime {
|
|
|
584
612
|
this.shutdown.abort(
|
|
585
613
|
new LlmError('Antigravity provider was disposed', 'TRANSPORT'),
|
|
586
614
|
)
|
|
587
|
-
|
|
588
|
-
this.sweeper = undefined
|
|
615
|
+
this.stopSweeper()
|
|
589
616
|
for (const conv of this.conversations.values()) {
|
|
590
617
|
conv.turn?.abandon('The harness provider was unloaded.')
|
|
591
618
|
this.clearSession(conv)
|
|
@@ -642,124 +669,131 @@ export class AntigravityRuntime {
|
|
|
642
669
|
}
|
|
643
670
|
return
|
|
644
671
|
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
const release = await this.mutex.acquire(key, this.shutdown.signal)
|
|
648
|
-
let conv: Conversation | undefined
|
|
672
|
+
this.activeStreams++
|
|
673
|
+
this.processIdleMs = 0
|
|
649
674
|
try {
|
|
650
|
-
this.
|
|
651
|
-
|
|
652
|
-
this.
|
|
653
|
-
conv
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
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
|
|
670
702
|
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
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
|
+
)
|
|
702
737
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
'The conversation history changed; the harness restarted this turn.',
|
|
706
|
-
)
|
|
738
|
+
} else if (conv.turn !== undefined) {
|
|
739
|
+
this.dropTurn(conv, 'Superseded by a newer request.')
|
|
707
740
|
}
|
|
708
|
-
} else if (conv.turn !== undefined) {
|
|
709
|
-
this.dropTurn(conv, 'Superseded by a newer request.')
|
|
710
|
-
}
|
|
711
741
|
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
}
|
|
721
|
-
this.assertOwner(proc, conv, conv.sessionId)
|
|
722
|
-
effort = await this.select(conv, proc, options.model, effort)
|
|
723
|
-
this.assertOwner(proc, conv, conv.sessionId)
|
|
724
|
-
let blocks: AcpPromptBlock[]
|
|
725
|
-
if (plan.rebuild) {
|
|
726
|
-
blocks = replayPrompt(plan.history, plan.input, {
|
|
727
|
-
system: split.system,
|
|
728
|
-
supplementalTools,
|
|
729
|
-
})
|
|
730
|
-
} else {
|
|
731
|
-
blocks = inputPrompt(plan.input)
|
|
732
|
-
const toolDelta = consumeToolDelta(conv, tools)
|
|
733
|
-
if (toolDelta.length > 0)
|
|
734
|
-
blocks.unshift({ type: 'text', text: `${toolDelta}\n\n` })
|
|
735
|
-
}
|
|
736
|
-
if (options.signal?.aborted) {
|
|
737
|
-
this.dropTurn(conv, 'The harness cancelled this request.')
|
|
738
|
-
yield {
|
|
739
|
-
type: 'finish',
|
|
740
|
-
reason: {
|
|
741
|
-
kind: 'aborted',
|
|
742
|
-
failure: { message: 'Request aborted', code: 'ABORTED' },
|
|
743
|
-
},
|
|
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)
|
|
744
750
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
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
|
|
759
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)
|
|
760
784
|
} finally {
|
|
761
|
-
|
|
785
|
+
try {
|
|
786
|
+
if (conv !== undefined) {
|
|
787
|
+
conv.opening = false
|
|
788
|
+
await this.afterStep(conv)
|
|
789
|
+
}
|
|
790
|
+
} finally {
|
|
791
|
+
release()
|
|
792
|
+
}
|
|
762
793
|
}
|
|
794
|
+
} finally {
|
|
795
|
+
this.activeStreams--
|
|
796
|
+
this.processIdleMs = 0
|
|
763
797
|
}
|
|
764
798
|
}
|
|
765
799
|
|
|
@@ -906,8 +940,7 @@ export class AntigravityRuntime {
|
|
|
906
940
|
if (this.lifecycle !== 'active') throw this.shutdown.signal.reason
|
|
907
941
|
await this.bridge.close()
|
|
908
942
|
this.assertActive()
|
|
909
|
-
|
|
910
|
-
this.sweeper = undefined
|
|
943
|
+
this.stopSweeper()
|
|
911
944
|
this.state = 'error'
|
|
912
945
|
this.lastError = error instanceof Error ? error.message : String(error)
|
|
913
946
|
throw mapError(error)
|
|
@@ -948,8 +981,11 @@ export class AntigravityRuntime {
|
|
|
948
981
|
private onProcessExit(proc: AcpProcess, error: Error): void {
|
|
949
982
|
const current = this.process === proc
|
|
950
983
|
if (current) {
|
|
984
|
+
this.stopSweeper()
|
|
951
985
|
this.process = undefined
|
|
952
986
|
this.processFingerprint = undefined
|
|
987
|
+
this.processIdleMs = 0
|
|
988
|
+
if (this.starting === undefined) void this.bridge.close()
|
|
953
989
|
}
|
|
954
990
|
if (current && this.lifecycle === 'active') {
|
|
955
991
|
this.state = 'error'
|
|
@@ -1689,6 +1725,7 @@ export class AntigravityRuntime {
|
|
|
1689
1725
|
private async afterStep(conv: Conversation): Promise<void> {
|
|
1690
1726
|
conv.lastUsed = this.monotonicNow()
|
|
1691
1727
|
conv.idleMs = 0
|
|
1728
|
+
this.processIdleMs = 0
|
|
1692
1729
|
const idle = this.isIdle(conv)
|
|
1693
1730
|
if (idle && conv.key.startsWith('anon-')) {
|
|
1694
1731
|
await this.resetSession(conv)
|
|
@@ -1779,24 +1816,91 @@ export class AntigravityRuntime {
|
|
|
1779
1816
|
)
|
|
1780
1817
|
}
|
|
1781
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()
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1782
1861
|
private sweep(): void {
|
|
1783
1862
|
const ttl = this.host.config.idleSessionTtlMs
|
|
1784
1863
|
const now = this.monotonicNow()
|
|
1785
1864
|
const delta = Math.max(0, now - this.lastSweepAt)
|
|
1786
1865
|
this.lastSweepAt = now
|
|
1787
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>[] = []
|
|
1788
1872
|
for (const conv of [...this.conversations.values()]) {
|
|
1789
|
-
if (!this.isIdle(conv))
|
|
1873
|
+
if (!this.isIdle(conv)) {
|
|
1874
|
+
allIdle = false
|
|
1875
|
+
continue
|
|
1876
|
+
}
|
|
1790
1877
|
conv.idleMs += slice
|
|
1791
1878
|
if (conv.idleMs > ttl) {
|
|
1792
|
-
|
|
1793
|
-
this.
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
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
|
+
)
|
|
1797
1886
|
this.conversations.delete(conv.key)
|
|
1798
1887
|
}
|
|
1799
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
|
+
}
|
|
1800
1904
|
}
|
|
1801
1905
|
}
|
|
1802
1906
|
|