pi-agy-acp 0.2.2 → 0.2.4
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 +20 -0
- package/README.md +14 -9
- package/docs/architecture.md +284 -0
- package/docs/development.md +67 -165
- package/docs/reference.md +157 -134
- package/docs/releasing.md +56 -0
- package/index.ts +92 -30
- package/package.json +3 -5
- package/src/acp-process.ts +47 -3
- package/src/catalog.ts +65 -67
- package/src/config.ts +6 -20
- package/src/conversation.ts +1 -1
- package/src/history.ts +1 -4
- package/src/host.ts +16 -2
- package/src/jsonrpc.ts +1 -0
- package/src/mcp-bridge.ts +39 -28
- package/src/pi-stream.ts +22 -1
- package/src/preflight.ts +38 -102
- package/src/quota.ts +21 -18
- package/src/runtime.ts +560 -266
- package/src/session-db.ts +21 -1
- package/src/util.ts +76 -6
package/src/runtime.ts
CHANGED
|
@@ -23,12 +23,15 @@ import {
|
|
|
23
23
|
type AcpPermissionOutcome,
|
|
24
24
|
type AcpPermissionRequest,
|
|
25
25
|
type AcpPromptResult,
|
|
26
|
+
type AcpSessionUpdate,
|
|
26
27
|
type AcpSpawner,
|
|
27
28
|
} from './acp-process.ts'
|
|
28
29
|
import {
|
|
30
|
+
findConfigOption,
|
|
29
31
|
flattenConfigOptions,
|
|
30
32
|
ModelCatalog,
|
|
31
33
|
nearestEffort,
|
|
34
|
+
offeredEffortsFrom,
|
|
32
35
|
} from './catalog.ts'
|
|
33
36
|
import { asEffort, defaults, type Config, type Effort } from './config.ts'
|
|
34
37
|
import {
|
|
@@ -48,6 +51,7 @@ import {
|
|
|
48
51
|
bridgeSystemPrompt,
|
|
49
52
|
inputPrompt,
|
|
50
53
|
MCP_SERVER_NAME,
|
|
54
|
+
renderSessionInstructions,
|
|
51
55
|
renderTranscript,
|
|
52
56
|
replayPrompt,
|
|
53
57
|
splitSystem,
|
|
@@ -177,6 +181,13 @@ interface Conversation {
|
|
|
177
181
|
effort: Effort | undefined
|
|
178
182
|
}
|
|
179
183
|
|
|
184
|
+
interface ResumableSession {
|
|
185
|
+
readonly sessionId: string
|
|
186
|
+
readonly systemHash: string
|
|
187
|
+
readonly knownIds: readonly string[]
|
|
188
|
+
readonly issuedIds: ReadonlySet<string>
|
|
189
|
+
}
|
|
190
|
+
|
|
180
191
|
interface PromptPlan {
|
|
181
192
|
rebuild: boolean
|
|
182
193
|
history: readonly RequestMessage[]
|
|
@@ -200,6 +211,8 @@ export class AntigravityRuntime {
|
|
|
200
211
|
private tierDiscovery: Promise<void> | undefined
|
|
201
212
|
private warmupPromise: Promise<void> | undefined
|
|
202
213
|
private readonly conversations = new Map<string, Conversation>()
|
|
214
|
+
/** Dormant idle-swept sessions eligible for `session/resume` on next request. */
|
|
215
|
+
private readonly resumableSessions = new Map<string, ResumableSession>()
|
|
203
216
|
private readonly tenants = new Map<string, Conversation>()
|
|
204
217
|
private readonly mutex = new KeyedMutex()
|
|
205
218
|
/** Serializes capacity checks and session creation so concurrent requests cannot overshoot maxLiveSessions. */
|
|
@@ -207,6 +220,10 @@ export class AntigravityRuntime {
|
|
|
207
220
|
private sweeper: ReturnType<typeof setInterval> | undefined
|
|
208
221
|
private readonly monotonicNow: () => number
|
|
209
222
|
private lastSweepAt: number
|
|
223
|
+
/** In-flight `stream()` calls acquiring or using the front process; guards idle teardown. */
|
|
224
|
+
private activeStreams = 0
|
|
225
|
+
/** Monotonic milliseconds the front process has been completely idle. */
|
|
226
|
+
private processIdleMs = 0
|
|
210
227
|
private emailCache:
|
|
211
228
|
| { fingerprint: string; email: string | undefined; at: number }
|
|
212
229
|
| undefined
|
|
@@ -270,23 +287,31 @@ export class AntigravityRuntime {
|
|
|
270
287
|
) {
|
|
271
288
|
this.preflight = undefined
|
|
272
289
|
this.preflightFingerprint = undefined
|
|
290
|
+
this.clearResumableSessions()
|
|
273
291
|
}
|
|
274
292
|
if (
|
|
275
293
|
this.process?.alive &&
|
|
276
|
-
this.starting === undefined &&
|
|
277
294
|
this.processFingerprint !== undefined &&
|
|
278
|
-
this.processFingerprint !== fp
|
|
279
|
-
[...this.conversations.values()].every((c) => this.isIdle(c))
|
|
295
|
+
this.processFingerprint !== fp
|
|
280
296
|
) {
|
|
297
|
+
const canRestart =
|
|
298
|
+
this.starting === undefined &&
|
|
299
|
+
this.warmupPromise === undefined &&
|
|
300
|
+
this.activeStreams === 0 &&
|
|
301
|
+
[...this.conversations.values()].every((c) => this.isIdle(c))
|
|
302
|
+
if (!canRestart) {
|
|
303
|
+
// Defer catalog fingerprint sync while the old binary's process is still active,
|
|
304
|
+
// so live observations from the stale process cannot stamp the new fingerprint.
|
|
305
|
+
return
|
|
306
|
+
}
|
|
281
307
|
// Restart an idle ACP process after an on-disk `agy` upgrade so discovery probes the new binary.
|
|
282
308
|
const stale = this.process
|
|
283
309
|
const staleSessions = [...this.conversations.values()]
|
|
284
310
|
.filter((conv) => conv.process === stale)
|
|
285
311
|
.map((conv) => ({ conv, sessionId: conv.sessionId }))
|
|
286
|
-
this.
|
|
287
|
-
this.
|
|
288
|
-
this.
|
|
289
|
-
stale.stop()
|
|
312
|
+
this.detachProcess(stale)
|
|
313
|
+
void this.bridge.close()
|
|
314
|
+
this.clearResumableSessions()
|
|
290
315
|
for (const { conv, sessionId } of staleSessions) {
|
|
291
316
|
if (sessionId !== undefined) {
|
|
292
317
|
removeSessionArtifacts(this.home, sessionId)
|
|
@@ -469,12 +494,12 @@ export class AntigravityRuntime {
|
|
|
469
494
|
async readQuota(force = false): Promise<QuotaSnapshot> {
|
|
470
495
|
this.assertActive()
|
|
471
496
|
const quota = this.quota.read(force)
|
|
472
|
-
if (!this.catalog.tier()) {
|
|
497
|
+
if (!this.catalog.tier() && !this.catalog.observed) {
|
|
473
498
|
this.tierDiscovery ??= (async () => {
|
|
474
499
|
const state = this.state
|
|
475
500
|
const error = this.lastError
|
|
476
501
|
try {
|
|
477
|
-
await this.warmup()
|
|
502
|
+
await this.warmup({ force: true })
|
|
478
503
|
} catch {
|
|
479
504
|
// Optional account discovery must not leave an otherwise idle runtime in error.
|
|
480
505
|
if (
|
|
@@ -499,6 +524,7 @@ export class AntigravityRuntime {
|
|
|
499
524
|
*/
|
|
500
525
|
async ensureReady(): Promise<AcpProcess> {
|
|
501
526
|
this.assertActive()
|
|
527
|
+
this.processIdleMs = 0
|
|
502
528
|
if (this.sweeper === undefined) {
|
|
503
529
|
this.lastSweepAt = this.monotonicNow()
|
|
504
530
|
this.sweeper = setInterval(() => this.sweep(), SWEEP_INTERVAL_MS)
|
|
@@ -515,7 +541,10 @@ export class AntigravityRuntime {
|
|
|
515
541
|
}
|
|
516
542
|
|
|
517
543
|
/**
|
|
518
|
-
*
|
|
544
|
+
* Refresh the catalog through a tool-less discovery session when uncached,
|
|
545
|
+
* stale for the current `agy` binary, or forced. When this call starts the
|
|
546
|
+
* front process on an otherwise idle runtime, the temporary process and MCP
|
|
547
|
+
* bridge are stopped once discovery finishes unless a stream adopted them.
|
|
519
548
|
* @returns resolves when discovery finished.
|
|
520
549
|
*/
|
|
521
550
|
async warmup(
|
|
@@ -526,30 +555,43 @@ export class AntigravityRuntime {
|
|
|
526
555
|
if (this.warmupPromise !== undefined)
|
|
527
556
|
return abortable(this.warmupPromise, options.signal)
|
|
528
557
|
this.reconcileBinaryFingerprint()
|
|
529
|
-
if (!options.force && this.catalog.
|
|
558
|
+
if (!options.force && this.catalog.isCurrent()) return
|
|
559
|
+
const hadProcess =
|
|
560
|
+
this.process?.alive === true || this.starting !== undefined
|
|
530
561
|
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
|
-
)
|
|
562
|
+
let proc: AcpProcess | undefined
|
|
541
563
|
try {
|
|
564
|
+
proc = await this.ensureReady()
|
|
542
565
|
this.assertOwner(proc)
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
566
|
+
const result = await proc
|
|
567
|
+
.newSession(
|
|
568
|
+
{
|
|
569
|
+
cwd: this.preflight?.workspace ?? this.home,
|
|
570
|
+
mcpServers: [],
|
|
571
|
+
},
|
|
572
|
+
{ onUpdate: () => undefined },
|
|
573
|
+
120_000,
|
|
548
574
|
)
|
|
549
|
-
|
|
550
|
-
|
|
575
|
+
.catch((error: unknown) => {
|
|
576
|
+
throw mapError(error)
|
|
577
|
+
})
|
|
578
|
+
try {
|
|
579
|
+
this.assertOwner(proc)
|
|
580
|
+
if (
|
|
581
|
+
this.catalog.observe(
|
|
582
|
+
result.configOptions,
|
|
583
|
+
result.models?.availableModels,
|
|
584
|
+
result._meta?.antigravity?.tier,
|
|
585
|
+
)
|
|
586
|
+
)
|
|
587
|
+
this.host.onModelsChanged()
|
|
588
|
+
} finally {
|
|
589
|
+
await this.closeAndCleanSession(proc, result.sessionId)
|
|
590
|
+
}
|
|
551
591
|
} finally {
|
|
552
|
-
|
|
592
|
+
if (!hadProcess && proc !== undefined) {
|
|
593
|
+
await this.stopIdleProcess(proc, { ignoreWarmup: true })
|
|
594
|
+
}
|
|
553
595
|
}
|
|
554
596
|
})().finally(() => {
|
|
555
597
|
this.warmupPromise = undefined
|
|
@@ -559,15 +601,29 @@ export class AntigravityRuntime {
|
|
|
559
601
|
|
|
560
602
|
/** Close conversation sessions while retaining the process and discovery/quota caches. */
|
|
561
603
|
async resetConversations(): Promise<void> {
|
|
562
|
-
this.
|
|
604
|
+
if (this.lifecycle !== 'active' || this.shutdown.signal.aborted) return
|
|
605
|
+
this.processIdleMs = 0
|
|
606
|
+
this.clearResumableSessions()
|
|
563
607
|
const conversations = [...this.conversations.values()]
|
|
564
608
|
for (const conv of conversations)
|
|
565
609
|
this.dropTurn(conv, 'The Pi session changed.')
|
|
566
610
|
await Promise.all(
|
|
567
611
|
conversations.map(async (conv) => {
|
|
568
|
-
|
|
612
|
+
let release: () => void
|
|
613
|
+
try {
|
|
614
|
+
release = await this.mutex.acquire(conv.key, this.shutdown.signal)
|
|
615
|
+
} catch (error) {
|
|
616
|
+
if (this.lifecycle !== 'active' || this.shutdown.signal.aborted)
|
|
617
|
+
return
|
|
618
|
+
throw error
|
|
619
|
+
}
|
|
569
620
|
try {
|
|
570
|
-
if (
|
|
621
|
+
if (
|
|
622
|
+
this.lifecycle !== 'active' ||
|
|
623
|
+
this.shutdown.signal.aborted ||
|
|
624
|
+
this.conversations.get(conv.key) !== conv
|
|
625
|
+
)
|
|
626
|
+
return
|
|
571
627
|
await this.resetSession(conv)
|
|
572
628
|
this.conversations.delete(conv.key)
|
|
573
629
|
} finally {
|
|
@@ -584,8 +640,8 @@ export class AntigravityRuntime {
|
|
|
584
640
|
this.shutdown.abort(
|
|
585
641
|
new LlmError('Antigravity provider was disposed', 'TRANSPORT'),
|
|
586
642
|
)
|
|
587
|
-
|
|
588
|
-
this.
|
|
643
|
+
this.stopSweeper()
|
|
644
|
+
this.clearResumableSessions()
|
|
589
645
|
for (const conv of this.conversations.values()) {
|
|
590
646
|
conv.turn?.abandon('The harness provider was unloaded.')
|
|
591
647
|
this.clearSession(conv)
|
|
@@ -642,124 +698,170 @@ export class AntigravityRuntime {
|
|
|
642
698
|
}
|
|
643
699
|
return
|
|
644
700
|
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
const release = await this.mutex.acquire(key, this.shutdown.signal)
|
|
648
|
-
let conv: Conversation | undefined
|
|
701
|
+
this.activeStreams++
|
|
702
|
+
this.processIdleMs = 0
|
|
649
703
|
try {
|
|
650
|
-
this.
|
|
651
|
-
|
|
652
|
-
this.
|
|
653
|
-
conv
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
704
|
+
const proc = await this.ensureReady()
|
|
705
|
+
const key = options.sessionId ?? `anon-${randomBytes(6).toString('hex')}`
|
|
706
|
+
const release = await this.mutex.acquire(key, this.shutdown.signal)
|
|
707
|
+
let conv: Conversation | undefined
|
|
708
|
+
try {
|
|
709
|
+
this.assertActive()
|
|
710
|
+
conv = await this.acquireConversation(key)
|
|
711
|
+
this.assertOwner(proc, conv)
|
|
712
|
+
conv.lastUsed = this.monotonicNow()
|
|
713
|
+
conv.idleMs = 0
|
|
714
|
+
const split = splitSystem(options)
|
|
715
|
+
const tools = toBridgeTools(options.tools)
|
|
716
|
+
conv.toolNames = new Set(tools.map((t) => t.name))
|
|
717
|
+
const requestedEffort =
|
|
718
|
+
asEffort(options.reasoningEffort) ?? this.host.config.defaultEffort
|
|
719
|
+
let effort = this.effectiveEffort(options.model, requestedEffort)
|
|
720
|
+
|
|
721
|
+
if (options.signal?.aborted) {
|
|
722
|
+
this.dropTurn(conv, 'The harness cancelled this request.')
|
|
723
|
+
yield {
|
|
724
|
+
type: 'finish',
|
|
725
|
+
reason: {
|
|
726
|
+
kind: 'aborted',
|
|
727
|
+
failure: { message: 'Request aborted', code: 'ABORTED' },
|
|
728
|
+
},
|
|
729
|
+
}
|
|
730
|
+
return
|
|
670
731
|
}
|
|
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
|
-
|
|
732
|
+
if (conv.turn !== undefined && conv.turn.pendingCalls.size > 0) {
|
|
733
|
+
// Resuming a parked turn is only valid while the request still starts
|
|
734
|
+
// with the exact history and model/effort selection this ACP session
|
|
735
|
+
// already owns. A compaction checkpoint, pruned/rewritten prefix, or
|
|
736
|
+
// model/effort change means the live session is stale, so abandon the
|
|
737
|
+
// turn and let plan() rebuild from Pi's authoritative history.
|
|
738
|
+
const resumedEffort = clampEffortToOptions(conv.configOptions, effort)
|
|
739
|
+
if (
|
|
740
|
+
!this.historyPrefixIntact(conv, split.system, split.messages) ||
|
|
741
|
+
conv.model !== options.model ||
|
|
742
|
+
conv.effort !== resumedEffort
|
|
743
|
+
) {
|
|
744
|
+
this.dropTurn(
|
|
745
|
+
conv,
|
|
746
|
+
'The conversation history or model selection changed; rebuilding the session from the harness history.',
|
|
747
|
+
)
|
|
748
|
+
} else {
|
|
749
|
+
// Tool waiters can abort in their result microtask; hand off the exact turn synchronously.
|
|
750
|
+
const resumed = this.deliverResults(
|
|
751
|
+
conv,
|
|
752
|
+
split.messages,
|
|
753
|
+
proc,
|
|
754
|
+
tools,
|
|
755
|
+
)
|
|
756
|
+
this.assertActive()
|
|
757
|
+
if (resumed !== undefined) {
|
|
758
|
+
conv.knownIds = messageIds(split.messages)
|
|
759
|
+
yield* this.pump(conv, resumed, proc, options, resumedEffort)
|
|
760
|
+
return
|
|
761
|
+
}
|
|
762
|
+
this.dropTurn(
|
|
763
|
+
conv,
|
|
764
|
+
'The conversation history changed; the harness restarted this turn.',
|
|
765
|
+
)
|
|
702
766
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
'The conversation history changed; the harness restarted this turn.',
|
|
706
|
-
)
|
|
767
|
+
} else if (conv.turn !== undefined) {
|
|
768
|
+
this.dropTurn(conv, 'Superseded by a newer request.')
|
|
707
769
|
}
|
|
708
|
-
} else if (conv.turn !== undefined) {
|
|
709
|
-
this.dropTurn(conv, 'Superseded by a newer request.')
|
|
710
|
-
}
|
|
711
770
|
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
failure: { message: 'Request aborted', code: 'ABORTED' },
|
|
743
|
-
},
|
|
771
|
+
let plan = this.plan(conv, split.system, split.messages)
|
|
772
|
+
let supplementalTools: readonly BridgeTool[] = []
|
|
773
|
+
let resumed = false
|
|
774
|
+
if (conv.sessionId === undefined) {
|
|
775
|
+
const resumable = this.resumableSessions.get(conv.key)
|
|
776
|
+
if (resumable !== undefined) {
|
|
777
|
+
this.resumableSessions.delete(conv.key)
|
|
778
|
+
const resumeInput = this.deltaInput(
|
|
779
|
+
resumable,
|
|
780
|
+
split.system,
|
|
781
|
+
split.messages,
|
|
782
|
+
)
|
|
783
|
+
if (resumeInput === undefined) {
|
|
784
|
+
// History or system prompt diverged while dormant: discard persisted session DB.
|
|
785
|
+
removeSessionArtifacts(this.home, resumable.sessionId)
|
|
786
|
+
} else {
|
|
787
|
+
const resumedTools = await this.resumeSession(
|
|
788
|
+
conv,
|
|
789
|
+
resumable,
|
|
790
|
+
tools,
|
|
791
|
+
proc,
|
|
792
|
+
)
|
|
793
|
+
if (resumedTools !== undefined) {
|
|
794
|
+
supplementalTools = resumedTools
|
|
795
|
+
plan = { rebuild: false, history: [], input: resumeInput }
|
|
796
|
+
resumed = true
|
|
797
|
+
effort = this.effectiveEffort(options.model, requestedEffort)
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
}
|
|
744
801
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
802
|
+
if (!resumed) {
|
|
803
|
+
if (plan.rebuild) await this.resetSession(conv)
|
|
804
|
+
this.assertOwner(proc, conv)
|
|
805
|
+
if (conv.sessionId === undefined) {
|
|
806
|
+
conv.systemHash = digest(split.system)
|
|
807
|
+
supplementalTools = await this.openSession(conv, tools, proc)
|
|
808
|
+
effort = this.effectiveEffort(options.model, requestedEffort)
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
this.assertOwner(proc, conv, conv.sessionId)
|
|
812
|
+
effort = await this.select(conv, proc, options.model, effort)
|
|
813
|
+
this.assertOwner(proc, conv, conv.sessionId)
|
|
814
|
+
let blocks: AcpPromptBlock[]
|
|
815
|
+
if (plan.rebuild) {
|
|
816
|
+
blocks = replayPrompt(plan.history, plan.input, {
|
|
817
|
+
system: split.system,
|
|
818
|
+
supplementalTools,
|
|
819
|
+
})
|
|
820
|
+
} else {
|
|
821
|
+
blocks = inputPrompt(plan.input)
|
|
822
|
+
if (supplementalTools.length > 0) {
|
|
823
|
+
const toolInstructions = renderSessionInstructions(
|
|
824
|
+
'',
|
|
825
|
+
supplementalTools,
|
|
826
|
+
)
|
|
827
|
+
if (toolInstructions.length > 0)
|
|
828
|
+
blocks.unshift({ type: 'text', text: toolInstructions })
|
|
829
|
+
}
|
|
830
|
+
const toolDelta = consumeToolDelta(conv, tools)
|
|
831
|
+
if (toolDelta.length > 0)
|
|
832
|
+
blocks.unshift({ type: 'text', text: `${toolDelta}\n\n` })
|
|
833
|
+
}
|
|
834
|
+
if (options.signal?.aborted) {
|
|
835
|
+
this.dropTurn(conv, 'The harness cancelled this request.')
|
|
836
|
+
yield {
|
|
837
|
+
type: 'finish',
|
|
838
|
+
reason: {
|
|
839
|
+
kind: 'aborted',
|
|
840
|
+
failure: { message: 'Request aborted', code: 'ABORTED' },
|
|
841
|
+
},
|
|
842
|
+
}
|
|
843
|
+
return
|
|
759
844
|
}
|
|
845
|
+
conv.knownIds = messageIds(split.messages)
|
|
846
|
+
// A new turn starts with an empty reported-identity space: ids from past
|
|
847
|
+
// turns must never back a fresh permission request.
|
|
848
|
+
conv.reportedToolCalls.clear()
|
|
849
|
+
const turn = conv.turn = new AcpTurn(randomBytes(6).toString('hex'))
|
|
850
|
+
this.startPrompt(conv, turn, proc, blocks)
|
|
851
|
+
yield* this.pump(conv, turn, proc, options, effort)
|
|
760
852
|
} finally {
|
|
761
|
-
|
|
853
|
+
try {
|
|
854
|
+
if (conv !== undefined) {
|
|
855
|
+
conv.opening = false
|
|
856
|
+
await this.afterStep(conv)
|
|
857
|
+
}
|
|
858
|
+
} finally {
|
|
859
|
+
release()
|
|
860
|
+
}
|
|
762
861
|
}
|
|
862
|
+
} finally {
|
|
863
|
+
this.activeStreams--
|
|
864
|
+
this.processIdleMs = 0
|
|
763
865
|
}
|
|
764
866
|
}
|
|
765
867
|
|
|
@@ -906,8 +1008,7 @@ export class AntigravityRuntime {
|
|
|
906
1008
|
if (this.lifecycle !== 'active') throw this.shutdown.signal.reason
|
|
907
1009
|
await this.bridge.close()
|
|
908
1010
|
this.assertActive()
|
|
909
|
-
|
|
910
|
-
this.sweeper = undefined
|
|
1011
|
+
this.stopSweeper()
|
|
911
1012
|
this.state = 'error'
|
|
912
1013
|
this.lastError = error instanceof Error ? error.message : String(error)
|
|
913
1014
|
throw mapError(error)
|
|
@@ -918,7 +1019,10 @@ export class AntigravityRuntime {
|
|
|
918
1019
|
this.reconcileBinaryFingerprint()
|
|
919
1020
|
const warn = (message: string) => this.host.log.warn(message)
|
|
920
1021
|
if (this.preflight !== undefined) {
|
|
921
|
-
|
|
1022
|
+
// Preserve retained session DBs across idle front restarts when dormant sessions exist.
|
|
1023
|
+
prepareHome(this.home, undefined, undefined, warn, {
|
|
1024
|
+
cleanArtifacts: this.resumableSessions.size === 0,
|
|
1025
|
+
})
|
|
922
1026
|
return Promise.resolve(this.preflight)
|
|
923
1027
|
}
|
|
924
1028
|
this.preflightPromise ??= (async () => {
|
|
@@ -928,7 +1032,9 @@ export class AntigravityRuntime {
|
|
|
928
1032
|
const version = await readVersion(binary, this.host.runCommand)
|
|
929
1033
|
this.assertActive()
|
|
930
1034
|
assertSupportedAgyVersion(version, binary)
|
|
931
|
-
const workspace = prepareHome(this.home, undefined, undefined, warn
|
|
1035
|
+
const workspace = prepareHome(this.home, undefined, undefined, warn, {
|
|
1036
|
+
cleanArtifacts: true,
|
|
1037
|
+
})
|
|
932
1038
|
const result: PreflightResult = {
|
|
933
1039
|
binary,
|
|
934
1040
|
version,
|
|
@@ -948,8 +1054,11 @@ export class AntigravityRuntime {
|
|
|
948
1054
|
private onProcessExit(proc: AcpProcess, error: Error): void {
|
|
949
1055
|
const current = this.process === proc
|
|
950
1056
|
if (current) {
|
|
1057
|
+
this.stopSweeper()
|
|
951
1058
|
this.process = undefined
|
|
952
1059
|
this.processFingerprint = undefined
|
|
1060
|
+
this.processIdleMs = 0
|
|
1061
|
+
if (this.starting === undefined) void this.bridge.close()
|
|
953
1062
|
}
|
|
954
1063
|
if (current && this.lifecycle === 'active') {
|
|
955
1064
|
this.state = 'error'
|
|
@@ -968,10 +1077,40 @@ export class AntigravityRuntime {
|
|
|
968
1077
|
}
|
|
969
1078
|
}
|
|
970
1079
|
|
|
971
|
-
private
|
|
1080
|
+
private openSession(
|
|
1081
|
+
conv: Conversation,
|
|
1082
|
+
tools: readonly BridgeTool[],
|
|
1083
|
+
proc: AcpProcess,
|
|
1084
|
+
): Promise<readonly BridgeTool[]> {
|
|
1085
|
+
return this.establishSession(conv, tools, proc)
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
private async resumeSession(
|
|
1089
|
+
conv: Conversation,
|
|
1090
|
+
resumable: ResumableSession,
|
|
1091
|
+
tools: readonly BridgeTool[],
|
|
1092
|
+
proc: AcpProcess,
|
|
1093
|
+
): Promise<readonly BridgeTool[] | undefined> {
|
|
1094
|
+
try {
|
|
1095
|
+
return await this.establishSession(conv, tools, proc, resumable)
|
|
1096
|
+
} catch (error) {
|
|
1097
|
+
if (error instanceof JsonRpcRemoteError && this.owns(proc, conv)) {
|
|
1098
|
+
this.host.log.warn(
|
|
1099
|
+
`antigravity-acp: session/resume failed for ${resumable.sessionId} (${error.message}); falling back to session/new`,
|
|
1100
|
+
)
|
|
1101
|
+
this.clearSession(conv)
|
|
1102
|
+
removeSessionArtifacts(this.home, resumable.sessionId)
|
|
1103
|
+
return undefined
|
|
1104
|
+
}
|
|
1105
|
+
throw mapError(error)
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
private async establishSession(
|
|
972
1110
|
conv: Conversation,
|
|
973
1111
|
tools: readonly BridgeTool[],
|
|
974
1112
|
proc: AcpProcess,
|
|
1113
|
+
resumable?: ResumableSession,
|
|
975
1114
|
): Promise<readonly BridgeTool[]> {
|
|
976
1115
|
this.assertOwner(proc, conv)
|
|
977
1116
|
try {
|
|
@@ -1002,68 +1141,68 @@ export class AntigravityRuntime {
|
|
|
1002
1141
|
conv.tenantKey = tenantKey
|
|
1003
1142
|
conv.tenantSecret = tenantSecret
|
|
1004
1143
|
this.tenants.set(tenantKey, conv)
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
append: bridgeSystemPrompt(),
|
|
1014
|
-
},
|
|
1015
|
-
},
|
|
1144
|
+
const params: Record<string, unknown> = {
|
|
1145
|
+
...(resumable !== undefined ? { sessionId: resumable.sessionId } : {}),
|
|
1146
|
+
cwd: this.preflight?.workspace ?? this.home,
|
|
1147
|
+
mcpServers,
|
|
1148
|
+
_meta: {
|
|
1149
|
+
antigravity: {
|
|
1150
|
+
systemPrompt: {
|
|
1151
|
+
append: bridgeSystemPrompt(),
|
|
1016
1152
|
},
|
|
1017
1153
|
},
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1154
|
+
},
|
|
1155
|
+
}
|
|
1156
|
+
const listener = {
|
|
1157
|
+
onUpdate: (update: AcpSessionUpdate) => {
|
|
1158
|
+
if (!this.owns(proc, conv) || conv.tenantKey !== tenantKey) return
|
|
1159
|
+
if (
|
|
1160
|
+
update.sessionUpdate === 'config_option_update' &&
|
|
1161
|
+
Array.isArray(update.configOptions)
|
|
1162
|
+
) {
|
|
1163
|
+
conv.configOptions = update.configOptions
|
|
1164
|
+
if (this.catalog.observe(update.configOptions))
|
|
1165
|
+
this.host.onModelsChanged()
|
|
1166
|
+
}
|
|
1167
|
+
if (recordReportedToolCall(conv, update) === 'conflict') {
|
|
1168
|
+
this.host.log.warn(
|
|
1169
|
+
`antigravity-acp: agent reported conflicting identity for tool call ${String(update.toolCallId)}`,
|
|
1170
|
+
)
|
|
1171
|
+
}
|
|
1172
|
+
conv.turn?.onUpdate(update)
|
|
1173
|
+
if (update.sessionUpdate === 'usage_update') {
|
|
1174
|
+
const hasCachedContextWindow =
|
|
1175
|
+
conv.model !== undefined &&
|
|
1176
|
+
this.catalog.contextWindowFor(conv.model) !== undefined
|
|
1177
|
+
const hasPositiveUsed =
|
|
1178
|
+
typeof update.used === 'number' &&
|
|
1179
|
+
Number.isFinite(update.used) &&
|
|
1180
|
+
update.used > 0
|
|
1181
|
+
if (!(hasCachedContextWindow && hasPositiveUsed)) {
|
|
1182
|
+
this.harvestSessionTurnData(
|
|
1183
|
+
conv,
|
|
1184
|
+
conv.sessionId,
|
|
1185
|
+
conv.model,
|
|
1186
|
+
conv.metaFloorIdx,
|
|
1187
|
+
{
|
|
1188
|
+
turn: conv.turn,
|
|
1189
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1190
|
+
},
|
|
1191
|
+
)
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
},
|
|
1195
|
+
onPermission: (request: AcpPermissionRequest) =>
|
|
1196
|
+
this.owns(proc, conv) && conv.tenantKey === tenantKey
|
|
1197
|
+
? this.answerPermission(conv, request)
|
|
1198
|
+
: ({ outcome: 'cancelled' } as const),
|
|
1199
|
+
}
|
|
1200
|
+
try {
|
|
1201
|
+
const result =
|
|
1202
|
+
resumable !== undefined
|
|
1203
|
+
? await proc.resumeSession(params, listener, 120_000)
|
|
1204
|
+
: await proc.newSession(params, listener, 120_000)
|
|
1205
|
+
// A buffered session reply can resolve after its process has exited.
|
|
1067
1206
|
if (!this.owns(proc, conv)) {
|
|
1068
1207
|
await this.closeAndCleanSession(proc, result.sessionId)
|
|
1069
1208
|
this.assertActive()
|
|
@@ -1076,9 +1215,14 @@ export class AntigravityRuntime {
|
|
|
1076
1215
|
conv.metaFloorIdx = 0
|
|
1077
1216
|
conv.lastDbStamp = undefined
|
|
1078
1217
|
conv.effort = undefined
|
|
1079
|
-
|
|
1080
1218
|
conv.dirty = false
|
|
1081
|
-
|
|
1219
|
+
if (resumable !== undefined) {
|
|
1220
|
+
conv.systemHash = resumable.systemHash
|
|
1221
|
+
conv.knownIds = [...resumable.knownIds]
|
|
1222
|
+
conv.issuedIds = new Set(resumable.issuedIds)
|
|
1223
|
+
} else {
|
|
1224
|
+
conv.issuedIds.clear()
|
|
1225
|
+
}
|
|
1082
1226
|
if (
|
|
1083
1227
|
this.catalog.observe(
|
|
1084
1228
|
result.configOptions,
|
|
@@ -1090,6 +1234,7 @@ export class AntigravityRuntime {
|
|
|
1090
1234
|
return supplementalTools
|
|
1091
1235
|
} catch (error) {
|
|
1092
1236
|
this.clearSession(conv)
|
|
1237
|
+
if (resumable !== undefined) throw error
|
|
1093
1238
|
throw mapError(error)
|
|
1094
1239
|
}
|
|
1095
1240
|
}
|
|
@@ -1129,7 +1274,14 @@ export class AntigravityRuntime {
|
|
|
1129
1274
|
request: AcpPermissionRequest,
|
|
1130
1275
|
): AcpPermissionOutcome {
|
|
1131
1276
|
const pick = (kind: string): AcpPermissionOutcome | undefined => {
|
|
1132
|
-
|
|
1277
|
+
if (!Array.isArray(request.options)) return undefined
|
|
1278
|
+
const option = request.options.find(
|
|
1279
|
+
(o) =>
|
|
1280
|
+
o !== null &&
|
|
1281
|
+
typeof o === 'object' &&
|
|
1282
|
+
o.kind === kind &&
|
|
1283
|
+
typeof o.optionId === 'string',
|
|
1284
|
+
)
|
|
1133
1285
|
return option === undefined
|
|
1134
1286
|
? undefined
|
|
1135
1287
|
: { outcome: 'selected', optionId: option.optionId }
|
|
@@ -1213,9 +1365,7 @@ export class AntigravityRuntime {
|
|
|
1213
1365
|
): Promise<Effort> {
|
|
1214
1366
|
const sessionId = conv.sessionId as string
|
|
1215
1367
|
this.assertOwner(proc, conv, sessionId)
|
|
1216
|
-
const modelOption = conv.configOptions
|
|
1217
|
-
(o) => o.id === 'model' || o.category === 'model',
|
|
1218
|
-
)
|
|
1368
|
+
const modelOption = findConfigOption(conv.configOptions, 'model')
|
|
1219
1369
|
const modelValues =
|
|
1220
1370
|
modelOption?.options !== undefined
|
|
1221
1371
|
? flattenConfigOptions(modelOption.options)
|
|
@@ -1254,15 +1404,8 @@ export class AntigravityRuntime {
|
|
|
1254
1404
|
conv.lastDbStamp = undefined
|
|
1255
1405
|
}
|
|
1256
1406
|
conv.model = model
|
|
1257
|
-
const levelOption = conv.configOptions
|
|
1258
|
-
|
|
1259
|
-
)
|
|
1260
|
-
const offeredEfforts =
|
|
1261
|
-
levelOption?.options !== undefined
|
|
1262
|
-
? flattenConfigOptions(levelOption.options)
|
|
1263
|
-
.map((o) => asEffort(o.value))
|
|
1264
|
-
.filter((v): v is Effort => v !== undefined)
|
|
1265
|
-
: undefined
|
|
1407
|
+
const levelOption = findConfigOption(conv.configOptions, 'thought_level')
|
|
1408
|
+
const offeredEfforts = offeredEffortsFrom(levelOption)
|
|
1266
1409
|
const targetEffort =
|
|
1267
1410
|
offeredEfforts !== undefined && offeredEfforts.length > 0
|
|
1268
1411
|
? offeredEfforts.includes(effort)
|
|
@@ -1314,25 +1457,10 @@ export class AntigravityRuntime {
|
|
|
1314
1457
|
system: string,
|
|
1315
1458
|
messages: readonly RequestMessage[],
|
|
1316
1459
|
): PromptPlan {
|
|
1317
|
-
|
|
1318
|
-
conv
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
if (reusable) {
|
|
1322
|
-
const known = conv.knownIds as string[]
|
|
1323
|
-
let rest = messages.slice(known.length)
|
|
1324
|
-
let skip = 0
|
|
1325
|
-
while (
|
|
1326
|
-
skip < rest.length &&
|
|
1327
|
-
this.isOwn(conv, rest[skip] as RequestMessage)
|
|
1328
|
-
)
|
|
1329
|
-
skip++
|
|
1330
|
-
rest = rest.slice(skip)
|
|
1331
|
-
if (
|
|
1332
|
-
rest.length > 0 &&
|
|
1333
|
-
rest.every((m) => m.role === 'user' || m.role === 'system')
|
|
1334
|
-
) {
|
|
1335
|
-
return { rebuild: false, history: [], input: rest }
|
|
1460
|
+
if (conv.sessionId !== undefined && !conv.dirty) {
|
|
1461
|
+
const input = this.deltaInput(conv, system, messages)
|
|
1462
|
+
if (input !== undefined) {
|
|
1463
|
+
return { rebuild: false, history: [], input }
|
|
1336
1464
|
}
|
|
1337
1465
|
}
|
|
1338
1466
|
let cut = messages.length
|
|
@@ -1348,43 +1476,106 @@ export class AntigravityRuntime {
|
|
|
1348
1476
|
}
|
|
1349
1477
|
}
|
|
1350
1478
|
|
|
1479
|
+
/**
|
|
1480
|
+
* Compute incremental user/system input after a session's known prefix and
|
|
1481
|
+
* own assistant/tool messages, or `undefined` if the history diverged.
|
|
1482
|
+
*/
|
|
1483
|
+
private deltaInput(
|
|
1484
|
+
state: {
|
|
1485
|
+
systemHash: string | undefined
|
|
1486
|
+
knownIds: readonly string[] | undefined
|
|
1487
|
+
issuedIds: ReadonlySet<string>
|
|
1488
|
+
},
|
|
1489
|
+
system: string,
|
|
1490
|
+
messages: readonly RequestMessage[],
|
|
1491
|
+
): readonly RequestMessage[] | undefined {
|
|
1492
|
+
if (!this.historyPrefixIntact(state, system, messages)) return undefined
|
|
1493
|
+
const known = state.knownIds as readonly string[]
|
|
1494
|
+
let rest = messages.slice(known.length)
|
|
1495
|
+
let skip = 0
|
|
1496
|
+
while (
|
|
1497
|
+
skip < rest.length &&
|
|
1498
|
+
this.isOwn(state.issuedIds, rest[skip] as RequestMessage)
|
|
1499
|
+
)
|
|
1500
|
+
skip++
|
|
1501
|
+
rest = rest.slice(skip)
|
|
1502
|
+
if (
|
|
1503
|
+
rest.length > 0 &&
|
|
1504
|
+
rest.every((m) => m.role === 'user' || m.role === 'system')
|
|
1505
|
+
) {
|
|
1506
|
+
return rest
|
|
1507
|
+
}
|
|
1508
|
+
return undefined
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1351
1511
|
/**
|
|
1352
1512
|
* Whether the live ACP session's context is still an exact prefix of the
|
|
1353
1513
|
* request: the same system prompt and the same message ids in the same order.
|
|
1354
1514
|
* A compaction checkpoint, a pruned/rewritten prefix, or a replaced system
|
|
1355
1515
|
* prompt all make the session stale and force a rebuild.
|
|
1356
|
-
* @param
|
|
1516
|
+
* @param state - conversation or dormant session owning the prefix metadata.
|
|
1357
1517
|
* @param system - current Pi system prompt.
|
|
1358
1518
|
* @param messages - full request history after leading system messages.
|
|
1359
1519
|
* @returns true when only new messages were appended.
|
|
1360
1520
|
*/
|
|
1361
1521
|
private historyPrefixIntact(
|
|
1362
|
-
|
|
1522
|
+
state: {
|
|
1523
|
+
systemHash: string | undefined
|
|
1524
|
+
knownIds: readonly string[] | undefined
|
|
1525
|
+
},
|
|
1363
1526
|
system: string,
|
|
1364
1527
|
messages: readonly RequestMessage[],
|
|
1365
1528
|
): boolean {
|
|
1366
1529
|
// plan() has always required the same system digest for reuse; a changed
|
|
1367
1530
|
// digest is a genuinely new prompt, while mid-conversation updates travel
|
|
1368
1531
|
// as `system` messages inside `messages` (systemPromptUpdate: 'in-history').
|
|
1369
|
-
if (
|
|
1532
|
+
if (state.knownIds === undefined || state.systemHash !== digest(system))
|
|
1370
1533
|
return false
|
|
1371
|
-
const known =
|
|
1534
|
+
const known = state.knownIds
|
|
1372
1535
|
return (
|
|
1373
1536
|
known.length <= messages.length &&
|
|
1374
1537
|
known.every((id, index) => messages[index]?.id === id)
|
|
1375
1538
|
)
|
|
1376
1539
|
}
|
|
1377
1540
|
|
|
1378
|
-
private isOwn(
|
|
1541
|
+
private isOwn(
|
|
1542
|
+
issuedIds: ReadonlySet<string>,
|
|
1543
|
+
message: RequestMessage,
|
|
1544
|
+
): boolean {
|
|
1379
1545
|
if (message.role === 'assistant')
|
|
1380
1546
|
return (
|
|
1381
1547
|
message.source.kind === 'model' &&
|
|
1382
1548
|
message.source.provider === this.host.config.provider
|
|
1383
1549
|
)
|
|
1384
|
-
if (message.role === 'tool') return
|
|
1550
|
+
if (message.role === 'tool') return issuedIds.has(message.toolCallId)
|
|
1385
1551
|
return false
|
|
1386
1552
|
}
|
|
1387
1553
|
|
|
1554
|
+
private parkResumableSession(key: string, entry: ResumableSession): void {
|
|
1555
|
+
this.dropResumableSession(key)
|
|
1556
|
+
this.resumableSessions.set(key, entry)
|
|
1557
|
+
const maxResumable = Math.max(1, this.host.config.maxLiveSessions) * 2
|
|
1558
|
+
while (this.resumableSessions.size > maxResumable) {
|
|
1559
|
+
const oldestKey = this.resumableSessions.keys().next().value
|
|
1560
|
+
if (oldestKey === undefined) break
|
|
1561
|
+
this.dropResumableSession(oldestKey)
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
private dropResumableSession(key: string): void {
|
|
1566
|
+
const existing = this.resumableSessions.get(key)
|
|
1567
|
+
if (existing === undefined) return
|
|
1568
|
+
this.resumableSessions.delete(key)
|
|
1569
|
+
removeSessionArtifacts(this.home, existing.sessionId)
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
private clearResumableSessions(): void {
|
|
1573
|
+
for (const entry of this.resumableSessions.values()) {
|
|
1574
|
+
removeSessionArtifacts(this.home, entry.sessionId)
|
|
1575
|
+
}
|
|
1576
|
+
this.resumableSessions.clear()
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1388
1579
|
private async closeAndCleanSession(
|
|
1389
1580
|
proc: AcpProcess,
|
|
1390
1581
|
sessionId: string,
|
|
@@ -1396,7 +1587,38 @@ export class AntigravityRuntime {
|
|
|
1396
1587
|
}
|
|
1397
1588
|
}
|
|
1398
1589
|
|
|
1590
|
+
private async evictIdleSession(conv: Conversation): Promise<void> {
|
|
1591
|
+
this.harvestSessionTurnData(conv, conv.sessionId, conv.model, conv.metaFloorIdx, {
|
|
1592
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1593
|
+
})
|
|
1594
|
+
const canPark =
|
|
1595
|
+
conv.sessionId !== undefined &&
|
|
1596
|
+
conv.systemHash !== undefined &&
|
|
1597
|
+
!conv.dirty &&
|
|
1598
|
+
conv.knownIds !== undefined &&
|
|
1599
|
+
!conv.key.startsWith('anon-')
|
|
1600
|
+
if (!canPark) {
|
|
1601
|
+
await this.resetSession(conv)
|
|
1602
|
+
return
|
|
1603
|
+
}
|
|
1604
|
+
const sessionId = conv.sessionId as string
|
|
1605
|
+
const proc = conv.process
|
|
1606
|
+
this.parkResumableSession(conv.key, {
|
|
1607
|
+
sessionId,
|
|
1608
|
+
systemHash: conv.systemHash as string,
|
|
1609
|
+
knownIds: [...(conv.knownIds as string[])],
|
|
1610
|
+
issuedIds: new Set(conv.issuedIds),
|
|
1611
|
+
})
|
|
1612
|
+
this.clearSession(conv)
|
|
1613
|
+
conv.knownIds = undefined
|
|
1614
|
+
conv.dirty = false
|
|
1615
|
+
if (proc !== undefined) {
|
|
1616
|
+
await proc.closeSession(sessionId)
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1399
1620
|
private async resetSession(conv: Conversation): Promise<void> {
|
|
1621
|
+
this.dropResumableSession(conv.key)
|
|
1400
1622
|
this.harvestSessionTurnData(conv, conv.sessionId, conv.model, conv.metaFloorIdx, {
|
|
1401
1623
|
ifChangedSince: conv.lastDbStamp,
|
|
1402
1624
|
})
|
|
@@ -1655,9 +1877,6 @@ export class AntigravityRuntime {
|
|
|
1655
1877
|
data: block.data,
|
|
1656
1878
|
mimeType: block.mimeType,
|
|
1657
1879
|
})
|
|
1658
|
-
} else {
|
|
1659
|
-
const rendered = renderTranscript([{ role: 'user', content: [block] }])
|
|
1660
|
-
if (rendered.length > 0) items.push({ type: 'text', text: rendered })
|
|
1661
1880
|
}
|
|
1662
1881
|
}
|
|
1663
1882
|
if (items.length === 0) items.push({ type: 'text', text: '(no output)' })
|
|
@@ -1689,6 +1908,7 @@ export class AntigravityRuntime {
|
|
|
1689
1908
|
private async afterStep(conv: Conversation): Promise<void> {
|
|
1690
1909
|
conv.lastUsed = this.monotonicNow()
|
|
1691
1910
|
conv.idleMs = 0
|
|
1911
|
+
this.processIdleMs = 0
|
|
1692
1912
|
const idle = this.isIdle(conv)
|
|
1693
1913
|
if (idle && conv.key.startsWith('anon-')) {
|
|
1694
1914
|
await this.resetSession(conv)
|
|
@@ -1779,24 +1999,91 @@ export class AntigravityRuntime {
|
|
|
1779
1999
|
)
|
|
1780
2000
|
}
|
|
1781
2001
|
|
|
2002
|
+
private stopSweeper(): void {
|
|
2003
|
+
if (this.sweeper !== undefined) {
|
|
2004
|
+
clearInterval(this.sweeper)
|
|
2005
|
+
this.sweeper = undefined
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
|
|
2009
|
+
private hasLiveConversations(): boolean {
|
|
2010
|
+
for (const conv of this.conversations.values()) {
|
|
2011
|
+
if (conv.sessionId !== undefined || !this.isIdle(conv)) return true
|
|
2012
|
+
}
|
|
2013
|
+
return false
|
|
2014
|
+
}
|
|
2015
|
+
|
|
2016
|
+
private detachProcess(proc: AcpProcess): void {
|
|
2017
|
+
if (this.process !== proc) return
|
|
2018
|
+
this.stopSweeper()
|
|
2019
|
+
this.process = undefined
|
|
2020
|
+
this.processFingerprint = undefined
|
|
2021
|
+
this.processIdleMs = 0
|
|
2022
|
+
if (this.lifecycle === 'active') this.state = 'idle'
|
|
2023
|
+
proc.stop()
|
|
2024
|
+
}
|
|
2025
|
+
|
|
2026
|
+
private async stopIdleProcess(
|
|
2027
|
+
proc: AcpProcess,
|
|
2028
|
+
options: { ignoreWarmup?: boolean } = {},
|
|
2029
|
+
): Promise<void> {
|
|
2030
|
+
if (
|
|
2031
|
+
this.process !== proc ||
|
|
2032
|
+
this.lifecycle !== 'active' ||
|
|
2033
|
+
this.starting !== undefined ||
|
|
2034
|
+
this.activeStreams > 0 ||
|
|
2035
|
+
(!options.ignoreWarmup && this.warmupPromise !== undefined) ||
|
|
2036
|
+
this.hasLiveConversations()
|
|
2037
|
+
) {
|
|
2038
|
+
return
|
|
2039
|
+
}
|
|
2040
|
+
this.detachProcess(proc)
|
|
2041
|
+
await this.bridge.close()
|
|
2042
|
+
}
|
|
2043
|
+
|
|
1782
2044
|
private sweep(): void {
|
|
1783
2045
|
const ttl = this.host.config.idleSessionTtlMs
|
|
1784
2046
|
const now = this.monotonicNow()
|
|
1785
2047
|
const delta = Math.max(0, now - this.lastSweepAt)
|
|
1786
2048
|
this.lastSweepAt = now
|
|
1787
2049
|
const slice = Math.min(delta, SWEEP_INTERVAL_MS * 2)
|
|
2050
|
+
let allIdle =
|
|
2051
|
+
this.activeStreams === 0 &&
|
|
2052
|
+
this.warmupPromise === undefined &&
|
|
2053
|
+
this.starting === undefined
|
|
2054
|
+
const evictions: Promise<void>[] = []
|
|
1788
2055
|
for (const conv of [...this.conversations.values()]) {
|
|
1789
|
-
if (!this.isIdle(conv))
|
|
2056
|
+
if (!this.isIdle(conv)) {
|
|
2057
|
+
allIdle = false
|
|
2058
|
+
continue
|
|
2059
|
+
}
|
|
1790
2060
|
conv.idleMs += slice
|
|
1791
2061
|
if (conv.idleMs > ttl) {
|
|
1792
|
-
|
|
1793
|
-
this.
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
2062
|
+
evictions.push(
|
|
2063
|
+
this.evictIdleSession(conv).catch((error: unknown) => {
|
|
2064
|
+
this.host.log.warn(
|
|
2065
|
+
`session sweep cleanup failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
2066
|
+
)
|
|
2067
|
+
}),
|
|
2068
|
+
)
|
|
1797
2069
|
this.conversations.delete(conv.key)
|
|
1798
2070
|
}
|
|
1799
2071
|
}
|
|
2072
|
+
if (!allIdle || this.process?.alive !== true) {
|
|
2073
|
+
this.processIdleMs = 0
|
|
2074
|
+
return
|
|
2075
|
+
}
|
|
2076
|
+
this.processIdleMs += slice
|
|
2077
|
+
if (this.processIdleMs > ttl && !this.hasLiveConversations()) {
|
|
2078
|
+
const proc = this.process
|
|
2079
|
+
if (evictions.length === 0) {
|
|
2080
|
+
void this.stopIdleProcess(proc)
|
|
2081
|
+
} else {
|
|
2082
|
+
void Promise.allSettled(evictions).then(() =>
|
|
2083
|
+
this.stopIdleProcess(proc),
|
|
2084
|
+
)
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
1800
2087
|
}
|
|
1801
2088
|
}
|
|
1802
2089
|
|
|
@@ -1804,14 +2091,11 @@ function clampEffortToOptions(
|
|
|
1804
2091
|
configOptions: readonly AcpConfigOption[],
|
|
1805
2092
|
effort: Effort,
|
|
1806
2093
|
): Effort {
|
|
1807
|
-
const
|
|
1808
|
-
(
|
|
2094
|
+
const offered = offeredEffortsFrom(
|
|
2095
|
+
findConfigOption(configOptions, 'thought_level'),
|
|
1809
2096
|
)
|
|
1810
|
-
if (
|
|
1811
|
-
|
|
1812
|
-
.map((o) => asEffort(o.value))
|
|
1813
|
-
.filter((v): v is Effort => v !== undefined)
|
|
1814
|
-
if (offered.length === 0 || offered.includes(effort)) return effort
|
|
2097
|
+
if (offered === undefined || offered.length === 0 || offered.includes(effort))
|
|
2098
|
+
return effort
|
|
1815
2099
|
return nearestEffort(effort, offered)
|
|
1816
2100
|
}
|
|
1817
2101
|
|
|
@@ -1865,7 +2149,7 @@ export function mapError(error: unknown): Error {
|
|
|
1865
2149
|
)
|
|
1866
2150
|
) {
|
|
1867
2151
|
return new LlmError(
|
|
1868
|
-
`Antigravity requires sign-in: run \`agy\` once in a terminal (${message})`,
|
|
2152
|
+
`Antigravity requires sign-in: run \`agy\` once in a terminal or \`agy acp --login\` for headless/SSH setups (${message})`,
|
|
1869
2153
|
'AUTH',
|
|
1870
2154
|
{ cause: error },
|
|
1871
2155
|
)
|
|
@@ -1880,8 +2164,18 @@ export function mapError(error: unknown): Error {
|
|
|
1880
2164
|
return new LlmError(message, 'CONTEXT_WINDOW_EXCEEDED', { cause: error })
|
|
1881
2165
|
if (/rate.?limit|429|too many requests|overloaded|capacity/i.test(message))
|
|
1882
2166
|
return new LlmError(message, 'RATE_LIMIT', { cause: error })
|
|
1883
|
-
if (
|
|
2167
|
+
if (
|
|
2168
|
+
/quota|resource.?exhausted|limit reached|exceeded your|prepaid\s+balance|spend\s+cap|insufficient\s+(?:funds|credits?|balance)|billing\s+(?:limit|exhausted|disabled|required)/i.test(
|
|
2169
|
+
message,
|
|
2170
|
+
)
|
|
2171
|
+
)
|
|
1884
2172
|
return new LlmError(message, 'QUOTA', { cause: error })
|
|
2173
|
+
if (
|
|
2174
|
+
/\binvalid_argument\b|unable to process input image|http\s+400|\(code\s+400\)|invalid\s+(?:image|request|argument)/i.test(
|
|
2175
|
+
message,
|
|
2176
|
+
)
|
|
2177
|
+
)
|
|
2178
|
+
return new LlmError(message, 'INVALID_REQUEST', { cause: error })
|
|
1885
2179
|
if (error instanceof JsonRpcRemoteError)
|
|
1886
2180
|
return new LlmError(`Antigravity: ${message}`, 'SERVER', { cause: error })
|
|
1887
2181
|
return new LlmError(message, 'TRANSPORT', { cause: error })
|