pi-agy-acp 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/docs/development.md +15 -6
- package/docs/reference.md +15 -8
- package/index.ts +11 -2
- package/package.json +1 -1
- package/src/acp-process.ts +15 -1
- package/src/catalog.ts +29 -19
- 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 +305 -104
- 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,11 @@ 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
|
+
private emailCache:
|
|
211
|
+
| { fingerprint: string; email: string | undefined; at: number }
|
|
212
|
+
| undefined
|
|
195
213
|
private lastTurn: RuntimeStatus['lastTurn']
|
|
196
214
|
private lifecycle: 'active' | 'disposing' | 'disposed' = 'active'
|
|
197
215
|
private readonly shutdown = new AbortController()
|
|
@@ -201,6 +219,8 @@ export class AntigravityRuntime {
|
|
|
201
219
|
/** @param host - host services. */
|
|
202
220
|
constructor(host: RuntimeHost) {
|
|
203
221
|
this.host = host
|
|
222
|
+
this.monotonicNow = host.monotonicNow ?? (() => performance.now())
|
|
223
|
+
this.lastSweepAt = this.monotonicNow()
|
|
204
224
|
this.home = host.config.home.length > 0 ? host.config.home : defaultHome()
|
|
205
225
|
this.catalog = new ModelCatalog(
|
|
206
226
|
join(this.home, 'catalog.json'),
|
|
@@ -222,6 +242,7 @@ export class AntigravityRuntime {
|
|
|
222
242
|
() => resolveAgyBinary(host.config.command),
|
|
223
243
|
host.runCommand,
|
|
224
244
|
host.config.quotaTtlMs,
|
|
245
|
+
host.quotaNow,
|
|
225
246
|
)
|
|
226
247
|
}
|
|
227
248
|
|
|
@@ -259,48 +280,107 @@ export class AntigravityRuntime {
|
|
|
259
280
|
) {
|
|
260
281
|
// Restart an idle ACP process after an on-disk `agy` upgrade so discovery probes the new binary.
|
|
261
282
|
const stale = this.process
|
|
283
|
+
const staleSessions = [...this.conversations.values()]
|
|
284
|
+
.filter((conv) => conv.process === stale)
|
|
285
|
+
.map((conv) => ({ conv, sessionId: conv.sessionId }))
|
|
262
286
|
this.process = undefined
|
|
263
287
|
this.processFingerprint = undefined
|
|
288
|
+
this.state = 'idle'
|
|
264
289
|
stale.stop()
|
|
290
|
+
for (const { conv, sessionId } of staleSessions) {
|
|
291
|
+
if (sessionId !== undefined) {
|
|
292
|
+
removeSessionArtifacts(this.home, sessionId)
|
|
293
|
+
}
|
|
294
|
+
this.clearSession(conv)
|
|
295
|
+
conv.dirty = true
|
|
296
|
+
}
|
|
265
297
|
}
|
|
266
298
|
if (this.catalog.syncBinaryFingerprint(fp)) {
|
|
267
299
|
this.host.onModelsChanged()
|
|
268
300
|
}
|
|
269
301
|
}
|
|
270
302
|
|
|
271
|
-
private
|
|
303
|
+
private harvestSessionTurnData(
|
|
304
|
+
conv: Conversation,
|
|
272
305
|
sessionId: string | undefined,
|
|
306
|
+
model: string | undefined,
|
|
273
307
|
modelFloor: number | null,
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
308
|
+
options: {
|
|
309
|
+
turn?: AcpTurn
|
|
310
|
+
lastStepIdx?: number | null
|
|
311
|
+
checkStepsError?: boolean
|
|
312
|
+
ifChangedSince?: string
|
|
313
|
+
} = {},
|
|
314
|
+
): HiddenTurnError | undefined {
|
|
315
|
+
if (this.lifecycle !== 'active' || !sessionId) return undefined
|
|
316
|
+
const data = readSessionTurnData(this.home, sessionId, {
|
|
317
|
+
metaFloorIdx: model ? modelFloor : null,
|
|
318
|
+
turnMetaFloorIdx: options.turn?.metaFloorIdx ?? null,
|
|
319
|
+
lastStepIdx: options.lastStepIdx,
|
|
320
|
+
checkStepsError: options.checkStepsError,
|
|
321
|
+
ifChangedSince: options.ifChangedSince,
|
|
322
|
+
})
|
|
323
|
+
if (data === undefined) return undefined
|
|
324
|
+
if (conv.sessionId === sessionId) conv.lastDbStamp = data.stamp
|
|
325
|
+
if (data.unchanged) return undefined
|
|
326
|
+
if (options.turn !== undefined && data.estimatedTokensUsed !== undefined) {
|
|
327
|
+
options.turn.estimatedContextUsed = data.estimatedTokensUsed
|
|
328
|
+
}
|
|
329
|
+
if (
|
|
330
|
+
model &&
|
|
331
|
+
this.catalog.observeContextWindow(model, data.maxContextTokens)
|
|
332
|
+
) {
|
|
333
|
+
this.host.onModelsChanged()
|
|
334
|
+
}
|
|
335
|
+
return data.hiddenError
|
|
278
336
|
}
|
|
279
337
|
|
|
280
|
-
private
|
|
281
|
-
|
|
338
|
+
private async runPromptSegment(
|
|
339
|
+
conv: Conversation,
|
|
340
|
+
turn: AcpTurn,
|
|
341
|
+
proc: AcpProcess,
|
|
342
|
+
sessionId: string,
|
|
282
343
|
model: string | undefined,
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
):
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
344
|
+
modelFloor: number | null,
|
|
345
|
+
blocks: AcpPromptBlock[],
|
|
346
|
+
): Promise<AcpPromptResult> {
|
|
347
|
+
const cursors = readSessionCursors(this.home, sessionId)
|
|
348
|
+
turn.metaFloorIdx =
|
|
349
|
+
modelFloor === null || cursors === null
|
|
350
|
+
? null
|
|
351
|
+
: Math.max(modelFloor, cursors.nextMetaIdx)
|
|
352
|
+
turn.lastStepIdx = cursors === null ? null : cursors.lastStepIdx
|
|
353
|
+
turn.estimatedContextUsed = undefined
|
|
354
|
+
let harvested = false
|
|
355
|
+
try {
|
|
356
|
+
const result = await proc.prompt(sessionId, blocks)
|
|
357
|
+
harvested = true
|
|
358
|
+
const checkStepsError =
|
|
359
|
+
result.stopReason === 'end_turn' || result.stopReason === 'refusal'
|
|
360
|
+
const hiddenError = this.harvestSessionTurnData(
|
|
361
|
+
conv,
|
|
362
|
+
sessionId,
|
|
363
|
+
model,
|
|
364
|
+
modelFloor,
|
|
365
|
+
{
|
|
366
|
+
turn,
|
|
367
|
+
lastStepIdx: turn.lastStepIdx,
|
|
368
|
+
checkStepsError,
|
|
369
|
+
},
|
|
370
|
+
)
|
|
371
|
+
if (hiddenError !== undefined) {
|
|
372
|
+
if (hiddenError.dirtySession) conv.dirty = true
|
|
373
|
+
throw new LlmError(hiddenError.message, hiddenError.code)
|
|
374
|
+
}
|
|
375
|
+
return result
|
|
376
|
+
} catch (error) {
|
|
377
|
+
if (!harvested) {
|
|
378
|
+
this.harvestSessionTurnData(conv, sessionId, model, modelFloor, {
|
|
379
|
+
turn,
|
|
380
|
+
checkStepsError: false,
|
|
381
|
+
})
|
|
382
|
+
}
|
|
383
|
+
throw error
|
|
304
384
|
}
|
|
305
385
|
}
|
|
306
386
|
|
|
@@ -365,11 +445,21 @@ export class AntigravityRuntime {
|
|
|
365
445
|
|
|
366
446
|
private accountInfo(): AccountInfo | undefined {
|
|
367
447
|
const paths = [join(this.home, 'acp.log'), this.home]
|
|
368
|
-
if (
|
|
369
|
-
this.home === join(homedir(), '.pi', 'agent', 'cache', 'antigravity-acp')
|
|
370
|
-
)
|
|
448
|
+
if (isDefaultPiAgyHome(this.home))
|
|
371
449
|
paths.push(...defaultAgyHomes())
|
|
372
|
-
const
|
|
450
|
+
const now = this.monotonicNow()
|
|
451
|
+
const fingerprint = logPathsFingerprint(paths)
|
|
452
|
+
let email: string | undefined
|
|
453
|
+
if (
|
|
454
|
+
this.emailCache !== undefined &&
|
|
455
|
+
this.emailCache.fingerprint === fingerprint &&
|
|
456
|
+
now - this.emailCache.at < EMAIL_CACHE_TTL_MS
|
|
457
|
+
) {
|
|
458
|
+
email = this.emailCache.email
|
|
459
|
+
} else {
|
|
460
|
+
email = readLoggedEmail(paths)
|
|
461
|
+
this.emailCache = { fingerprint, email, at: now }
|
|
462
|
+
}
|
|
373
463
|
const tier = this.catalog.tier()
|
|
374
464
|
if (!email && !tier) return undefined
|
|
375
465
|
return { email, plan: tier?.name, tierId: tier?.id }
|
|
@@ -410,7 +500,8 @@ export class AntigravityRuntime {
|
|
|
410
500
|
async ensureReady(): Promise<AcpProcess> {
|
|
411
501
|
this.assertActive()
|
|
412
502
|
if (this.sweeper === undefined) {
|
|
413
|
-
this.
|
|
503
|
+
this.lastSweepAt = this.monotonicNow()
|
|
504
|
+
this.sweeper = setInterval(() => this.sweep(), SWEEP_INTERVAL_MS)
|
|
414
505
|
this.sweeper.unref?.()
|
|
415
506
|
}
|
|
416
507
|
if (this.starting === undefined && this.process?.alive === true)
|
|
@@ -559,7 +650,8 @@ export class AntigravityRuntime {
|
|
|
559
650
|
this.assertActive()
|
|
560
651
|
conv = await this.acquireConversation(key)
|
|
561
652
|
this.assertOwner(proc, conv)
|
|
562
|
-
conv.lastUsed =
|
|
653
|
+
conv.lastUsed = this.monotonicNow()
|
|
654
|
+
conv.idleMs = 0
|
|
563
655
|
const split = splitSystem(options)
|
|
564
656
|
const tools = toBridgeTools(options.tools)
|
|
565
657
|
conv.toolNames = new Set(tools.map((t) => t.name))
|
|
@@ -584,10 +676,11 @@ export class AntigravityRuntime {
|
|
|
584
676
|
// already owns. A compaction checkpoint, pruned/rewritten prefix, or
|
|
585
677
|
// model/effort change means the live session is stale, so abandon the
|
|
586
678
|
// turn and let plan() rebuild from Pi's authoritative history.
|
|
679
|
+
const resumedEffort = clampEffortToOptions(conv.configOptions, effort)
|
|
587
680
|
if (
|
|
588
681
|
!this.historyPrefixIntact(conv, split.system, split.messages) ||
|
|
589
682
|
conv.model !== options.model ||
|
|
590
|
-
conv.effort !==
|
|
683
|
+
conv.effort !== resumedEffort
|
|
591
684
|
) {
|
|
592
685
|
this.dropTurn(
|
|
593
686
|
conv,
|
|
@@ -604,7 +697,7 @@ export class AntigravityRuntime {
|
|
|
604
697
|
this.assertActive()
|
|
605
698
|
if (resumed !== undefined) {
|
|
606
699
|
conv.knownIds = messageIds(split.messages)
|
|
607
|
-
yield* this.pump(conv, resumed, proc, options,
|
|
700
|
+
yield* this.pump(conv, resumed, proc, options, resumedEffort)
|
|
608
701
|
return
|
|
609
702
|
}
|
|
610
703
|
this.dropTurn(
|
|
@@ -626,7 +719,7 @@ export class AntigravityRuntime {
|
|
|
626
719
|
effort = this.effectiveEffort(options.model, requestedEffort)
|
|
627
720
|
}
|
|
628
721
|
this.assertOwner(proc, conv, conv.sessionId)
|
|
629
|
-
await this.select(conv, proc, options.model, effort)
|
|
722
|
+
effort = await this.select(conv, proc, options.model, effort)
|
|
630
723
|
this.assertOwner(proc, conv, conv.sessionId)
|
|
631
724
|
let blocks: AcpPromptBlock[]
|
|
632
725
|
if (plan.rebuild) {
|
|
@@ -682,6 +775,8 @@ export class AntigravityRuntime {
|
|
|
682
775
|
const existing = this.conversations.get(key)
|
|
683
776
|
if (existing !== undefined) {
|
|
684
777
|
existing.opening = true
|
|
778
|
+
existing.lastUsed = this.monotonicNow()
|
|
779
|
+
existing.idleMs = 0
|
|
685
780
|
return existing
|
|
686
781
|
}
|
|
687
782
|
const unlock = await this.capacityMutex.acquire(
|
|
@@ -693,6 +788,8 @@ export class AntigravityRuntime {
|
|
|
693
788
|
const again = this.conversations.get(key)
|
|
694
789
|
if (again !== undefined) {
|
|
695
790
|
again.opening = true
|
|
791
|
+
again.lastUsed = this.monotonicNow()
|
|
792
|
+
again.idleMs = 0
|
|
696
793
|
return again
|
|
697
794
|
}
|
|
698
795
|
const max = Math.max(1, this.host.config.maxLiveSessions)
|
|
@@ -740,7 +837,8 @@ export class AntigravityRuntime {
|
|
|
740
837
|
issuedIds: new Set(),
|
|
741
838
|
turn: undefined,
|
|
742
839
|
dirty: false,
|
|
743
|
-
lastUsed:
|
|
840
|
+
lastUsed: this.monotonicNow(),
|
|
841
|
+
idleMs: 0,
|
|
744
842
|
model: undefined,
|
|
745
843
|
metaFloorIdx: 0,
|
|
746
844
|
effort: undefined,
|
|
@@ -818,8 +916,9 @@ export class AntigravityRuntime {
|
|
|
818
916
|
|
|
819
917
|
private runPreflight(): Promise<PreflightResult> {
|
|
820
918
|
this.reconcileBinaryFingerprint()
|
|
919
|
+
const warn = (message: string) => this.host.log.warn(message)
|
|
821
920
|
if (this.preflight !== undefined) {
|
|
822
|
-
prepareHome(this.home)
|
|
921
|
+
prepareHome(this.home, undefined, undefined, warn)
|
|
823
922
|
return Promise.resolve(this.preflight)
|
|
824
923
|
}
|
|
825
924
|
this.preflightPromise ??= (async () => {
|
|
@@ -829,7 +928,7 @@ export class AntigravityRuntime {
|
|
|
829
928
|
const version = await readVersion(binary, this.host.runCommand)
|
|
830
929
|
this.assertActive()
|
|
831
930
|
assertSupportedAgyVersion(version, binary)
|
|
832
|
-
const workspace = prepareHome(this.home)
|
|
931
|
+
const workspace = prepareHome(this.home, undefined, undefined, warn)
|
|
833
932
|
const result: PreflightResult = {
|
|
834
933
|
binary,
|
|
835
934
|
version,
|
|
@@ -875,7 +974,13 @@ export class AntigravityRuntime {
|
|
|
875
974
|
proc: AcpProcess,
|
|
876
975
|
): Promise<readonly BridgeTool[]> {
|
|
877
976
|
this.assertOwner(proc, conv)
|
|
878
|
-
|
|
977
|
+
try {
|
|
978
|
+
syncOAuthToken(this.home)
|
|
979
|
+
} catch (error) {
|
|
980
|
+
this.host.log.warn(
|
|
981
|
+
`antigravity-acp: OAuth token sync skipped: ${error instanceof Error ? error.message : String(error)}`,
|
|
982
|
+
)
|
|
983
|
+
}
|
|
879
984
|
conv.reportedToolCalls.clear()
|
|
880
985
|
const compacted = tools.map(compactBridgeTool)
|
|
881
986
|
const supplementalTools = tools.filter(
|
|
@@ -924,20 +1029,33 @@ export class AntigravityRuntime {
|
|
|
924
1029
|
if (this.catalog.observe(update.configOptions))
|
|
925
1030
|
this.host.onModelsChanged()
|
|
926
1031
|
}
|
|
927
|
-
if (update.sessionUpdate === 'usage_update') {
|
|
928
|
-
this.observeSessionContextWindow(
|
|
929
|
-
conv.sessionId,
|
|
930
|
-
conv.model,
|
|
931
|
-
conv.metaFloorIdx,
|
|
932
|
-
conv.turn,
|
|
933
|
-
)
|
|
934
|
-
}
|
|
935
1032
|
if (recordReportedToolCall(conv, update) === 'conflict') {
|
|
936
1033
|
this.host.log.warn(
|
|
937
1034
|
`antigravity-acp: agent reported conflicting identity for tool call ${String(update.toolCallId)}`,
|
|
938
1035
|
)
|
|
939
1036
|
}
|
|
940
1037
|
conv.turn?.onUpdate(update)
|
|
1038
|
+
if (update.sessionUpdate === 'usage_update') {
|
|
1039
|
+
const hasCachedContextWindow =
|
|
1040
|
+
conv.model !== undefined &&
|
|
1041
|
+
this.catalog.contextWindowFor(conv.model) !== undefined
|
|
1042
|
+
const hasPositiveUsed =
|
|
1043
|
+
typeof update.used === 'number' &&
|
|
1044
|
+
Number.isFinite(update.used) &&
|
|
1045
|
+
update.used > 0
|
|
1046
|
+
if (!(hasCachedContextWindow && hasPositiveUsed)) {
|
|
1047
|
+
this.harvestSessionTurnData(
|
|
1048
|
+
conv,
|
|
1049
|
+
conv.sessionId,
|
|
1050
|
+
conv.model,
|
|
1051
|
+
conv.metaFloorIdx,
|
|
1052
|
+
{
|
|
1053
|
+
turn: conv.turn,
|
|
1054
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1055
|
+
},
|
|
1056
|
+
)
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
941
1059
|
},
|
|
942
1060
|
onPermission: (request) => this.owns(proc, conv) && conv.tenantKey === tenantKey
|
|
943
1061
|
? this.answerPermission(conv, request)
|
|
@@ -956,6 +1074,7 @@ export class AntigravityRuntime {
|
|
|
956
1074
|
conv.configOptions = result.configOptions ?? []
|
|
957
1075
|
conv.model = undefined
|
|
958
1076
|
conv.metaFloorIdx = 0
|
|
1077
|
+
conv.lastDbStamp = undefined
|
|
959
1078
|
conv.effort = undefined
|
|
960
1079
|
|
|
961
1080
|
conv.dirty = false
|
|
@@ -1091,7 +1210,7 @@ export class AntigravityRuntime {
|
|
|
1091
1210
|
proc: AcpProcess,
|
|
1092
1211
|
model: string,
|
|
1093
1212
|
effort: Effort,
|
|
1094
|
-
): Promise<
|
|
1213
|
+
): Promise<Effort> {
|
|
1095
1214
|
const sessionId = conv.sessionId as string
|
|
1096
1215
|
this.assertOwner(proc, conv, sessionId)
|
|
1097
1216
|
const modelOption = conv.configOptions.find(
|
|
@@ -1113,8 +1232,9 @@ export class AntigravityRuntime {
|
|
|
1113
1232
|
const previousModel = conv.model
|
|
1114
1233
|
const previousFloor = conv.metaFloorIdx
|
|
1115
1234
|
if (previousModel !== undefined && previousModel !== model) {
|
|
1116
|
-
this.
|
|
1235
|
+
this.harvestSessionTurnData(conv, sessionId, previousModel, previousFloor)
|
|
1117
1236
|
conv.metaFloorIdx = null
|
|
1237
|
+
conv.lastDbStamp = undefined
|
|
1118
1238
|
}
|
|
1119
1239
|
if (conv.model !== model && modelOption?.currentValue !== model) {
|
|
1120
1240
|
await this.applyConfigOption(
|
|
@@ -1126,27 +1246,48 @@ export class AntigravityRuntime {
|
|
|
1126
1246
|
)
|
|
1127
1247
|
}
|
|
1128
1248
|
if (previousModel !== undefined && previousModel !== model) {
|
|
1129
|
-
const
|
|
1249
|
+
const cursors = readSessionCursors(this.home, sessionId)
|
|
1130
1250
|
conv.metaFloorIdx =
|
|
1131
|
-
previousFloor === null ||
|
|
1251
|
+
previousFloor === null || cursors === null
|
|
1132
1252
|
? null
|
|
1133
|
-
: Math.max(previousFloor,
|
|
1253
|
+
: Math.max(previousFloor, cursors.nextMetaIdx)
|
|
1254
|
+
conv.lastDbStamp = undefined
|
|
1134
1255
|
}
|
|
1135
1256
|
conv.model = model
|
|
1136
1257
|
const levelOption = conv.configOptions.find(
|
|
1137
1258
|
(o) => o.id === 'thought_level' || o.category === 'thought_level',
|
|
1138
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
|
|
1266
|
+
const targetEffort =
|
|
1267
|
+
offeredEfforts !== undefined && offeredEfforts.length > 0
|
|
1268
|
+
? offeredEfforts.includes(effort)
|
|
1269
|
+
? effort
|
|
1270
|
+
: nearestEffort(effort, offeredEfforts)
|
|
1271
|
+
: effort
|
|
1139
1272
|
// Fixed-variant families (GPT-OSS) and single-variant orchestrators reject
|
|
1140
|
-
// thought_level writes, so only selectable families write it.
|
|
1273
|
+
// thought_level writes, so only selectable families with offered levels write it.
|
|
1141
1274
|
if (
|
|
1142
1275
|
this.catalog.supportsEffortSelection(model) &&
|
|
1143
1276
|
levelOption !== undefined &&
|
|
1144
|
-
|
|
1145
|
-
|
|
1277
|
+
(offeredEfforts === undefined || offeredEfforts.length > 0) &&
|
|
1278
|
+
conv.effort !== targetEffort &&
|
|
1279
|
+
levelOption.currentValue !== targetEffort
|
|
1146
1280
|
) {
|
|
1147
|
-
await this.applyConfigOption(
|
|
1281
|
+
await this.applyConfigOption(
|
|
1282
|
+
conv,
|
|
1283
|
+
proc,
|
|
1284
|
+
sessionId,
|
|
1285
|
+
levelOption.id,
|
|
1286
|
+
targetEffort,
|
|
1287
|
+
)
|
|
1148
1288
|
}
|
|
1149
|
-
conv.effort =
|
|
1289
|
+
conv.effort = targetEffort
|
|
1290
|
+
return targetEffort
|
|
1150
1291
|
}
|
|
1151
1292
|
|
|
1152
1293
|
private async applyConfigOption(
|
|
@@ -1163,6 +1304,9 @@ export class AntigravityRuntime {
|
|
|
1163
1304
|
})
|
|
1164
1305
|
this.assertOwner(proc, conv, sessionId)
|
|
1165
1306
|
conv.configOptions = configOptions
|
|
1307
|
+
if (this.catalog.observe(configOptions)) {
|
|
1308
|
+
this.host.onModelsChanged()
|
|
1309
|
+
}
|
|
1166
1310
|
}
|
|
1167
1311
|
|
|
1168
1312
|
private plan(
|
|
@@ -1253,7 +1397,9 @@ export class AntigravityRuntime {
|
|
|
1253
1397
|
}
|
|
1254
1398
|
|
|
1255
1399
|
private async resetSession(conv: Conversation): Promise<void> {
|
|
1256
|
-
this.
|
|
1400
|
+
this.harvestSessionTurnData(conv, conv.sessionId, conv.model, conv.metaFloorIdx, {
|
|
1401
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1402
|
+
})
|
|
1257
1403
|
let closing: Promise<void> | undefined
|
|
1258
1404
|
if (conv.sessionId !== undefined && conv.process !== undefined) {
|
|
1259
1405
|
closing = this.closeAndCleanSession(conv.process, conv.sessionId)
|
|
@@ -1271,6 +1417,7 @@ export class AntigravityRuntime {
|
|
|
1271
1417
|
conv.tenantSecret = undefined
|
|
1272
1418
|
conv.process = undefined
|
|
1273
1419
|
conv.metaFloorIdx = null
|
|
1420
|
+
conv.lastDbStamp = undefined
|
|
1274
1421
|
conv.advertisedTools = []
|
|
1275
1422
|
conv.advertisedToolDigests.clear()
|
|
1276
1423
|
conv.knownTools.clear()
|
|
@@ -1290,12 +1437,18 @@ export class AntigravityRuntime {
|
|
|
1290
1437
|
const sessionId = conv.sessionId as string
|
|
1291
1438
|
const model = conv.model
|
|
1292
1439
|
const modelFloor = conv.metaFloorIdx
|
|
1293
|
-
turn.
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1440
|
+
turn.bindPrompt(
|
|
1441
|
+
this.runPromptSegment(
|
|
1442
|
+
conv,
|
|
1443
|
+
turn,
|
|
1444
|
+
proc,
|
|
1445
|
+
sessionId,
|
|
1446
|
+
model,
|
|
1447
|
+
modelFloor,
|
|
1448
|
+
blocks,
|
|
1449
|
+
),
|
|
1450
|
+
{ mapError },
|
|
1451
|
+
)
|
|
1299
1452
|
} catch (error) {
|
|
1300
1453
|
turn.fail(mapError(error))
|
|
1301
1454
|
}
|
|
@@ -1350,33 +1503,16 @@ export class AntigravityRuntime {
|
|
|
1350
1503
|
{
|
|
1351
1504
|
parkSettleMs: this.host.config.parkSettleMs,
|
|
1352
1505
|
maxContinuations: this.host.config.longDecodeContinuations,
|
|
1353
|
-
continuePrompt:
|
|
1354
|
-
|
|
1506
|
+
continuePrompt: () =>
|
|
1507
|
+
this.runPromptSegment(
|
|
1508
|
+
conv,
|
|
1509
|
+
turn,
|
|
1510
|
+
proc,
|
|
1355
1511
|
sessionId,
|
|
1512
|
+
options.model,
|
|
1356
1513
|
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
|
-
},
|
|
1514
|
+
[{ type: 'text', text: CONTINUE_PROMPT }],
|
|
1515
|
+
),
|
|
1380
1516
|
cancel: () => {
|
|
1381
1517
|
if (conv.turn === turn) proc.cancel(sessionId)
|
|
1382
1518
|
},
|
|
@@ -1389,7 +1525,15 @@ export class AntigravityRuntime {
|
|
|
1389
1525
|
conv.issuedIds.add(chunk.block.id)
|
|
1390
1526
|
if (chunk.type === 'finish') {
|
|
1391
1527
|
finishKind = chunk.reason.kind
|
|
1392
|
-
|
|
1528
|
+
if (
|
|
1529
|
+
finishKind === 'aborted' ||
|
|
1530
|
+
(finishKind === 'tool-calls' &&
|
|
1531
|
+
this.catalog.contextWindowFor(options.model) === undefined)
|
|
1532
|
+
) {
|
|
1533
|
+
this.harvestSessionTurnData(conv, sessionId, options.model, metaFloorIdx, {
|
|
1534
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1535
|
+
})
|
|
1536
|
+
}
|
|
1393
1537
|
}
|
|
1394
1538
|
yield chunk
|
|
1395
1539
|
if (chunk.type === 'finish' && chunk.reason.kind === 'aborted') break
|
|
@@ -1397,13 +1541,17 @@ export class AntigravityRuntime {
|
|
|
1397
1541
|
const interrupted = turn.guard(undefined, signal)
|
|
1398
1542
|
if (interrupted !== undefined && finishKind !== 'aborted') {
|
|
1399
1543
|
finishKind = 'aborted'
|
|
1400
|
-
this.
|
|
1544
|
+
this.harvestSessionTurnData(conv, sessionId, options.model, metaFloorIdx, {
|
|
1545
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1546
|
+
})
|
|
1401
1547
|
yield interrupted
|
|
1402
1548
|
}
|
|
1403
1549
|
drained = true
|
|
1404
1550
|
} catch (error) {
|
|
1405
1551
|
finishKind = 'error'
|
|
1406
|
-
this.
|
|
1552
|
+
this.harvestSessionTurnData(conv, sessionId, options.model, metaFloorIdx, {
|
|
1553
|
+
ifChangedSince: conv.lastDbStamp,
|
|
1554
|
+
})
|
|
1407
1555
|
throw mapError(error)
|
|
1408
1556
|
} finally {
|
|
1409
1557
|
// Only a fully consumed tool handoff keeps the turn and its cancellation owner.
|
|
@@ -1539,7 +1687,8 @@ export class AntigravityRuntime {
|
|
|
1539
1687
|
}
|
|
1540
1688
|
|
|
1541
1689
|
private async afterStep(conv: Conversation): Promise<void> {
|
|
1542
|
-
conv.lastUsed =
|
|
1690
|
+
conv.lastUsed = this.monotonicNow()
|
|
1691
|
+
conv.idleMs = 0
|
|
1543
1692
|
const idle = this.isIdle(conv)
|
|
1544
1693
|
if (idle && conv.key.startsWith('anon-')) {
|
|
1545
1694
|
await this.resetSession(conv)
|
|
@@ -1593,8 +1742,12 @@ export class AntigravityRuntime {
|
|
|
1593
1742
|
? request.arguments.call_id
|
|
1594
1743
|
: ''
|
|
1595
1744
|
return (
|
|
1596
|
-
turn.awaitRelayedCall(
|
|
1597
|
-
|
|
1745
|
+
turn.awaitRelayedCall(
|
|
1746
|
+
callId,
|
|
1747
|
+
relayAfterMs,
|
|
1748
|
+
request.signal,
|
|
1749
|
+
request.commit,
|
|
1750
|
+
) ?? toolError(`Unknown harness call id ${callId}.`)
|
|
1598
1751
|
)
|
|
1599
1752
|
}
|
|
1600
1753
|
let name = request.name
|
|
@@ -1617,14 +1770,25 @@ export class AntigravityRuntime {
|
|
|
1617
1770
|
}
|
|
1618
1771
|
if (!conv.toolNames.has(name))
|
|
1619
1772
|
return toolError(`The harness has no tool named ${name}.`)
|
|
1620
|
-
return turn.invokeTool(
|
|
1773
|
+
return turn.invokeTool(
|
|
1774
|
+
name,
|
|
1775
|
+
args,
|
|
1776
|
+
relayAfterMs,
|
|
1777
|
+
request.signal,
|
|
1778
|
+
request.commit,
|
|
1779
|
+
)
|
|
1621
1780
|
}
|
|
1622
1781
|
|
|
1623
1782
|
private sweep(): void {
|
|
1624
1783
|
const ttl = this.host.config.idleSessionTtlMs
|
|
1625
|
-
const now =
|
|
1784
|
+
const now = this.monotonicNow()
|
|
1785
|
+
const delta = Math.max(0, now - this.lastSweepAt)
|
|
1786
|
+
this.lastSweepAt = now
|
|
1787
|
+
const slice = Math.min(delta, SWEEP_INTERVAL_MS * 2)
|
|
1626
1788
|
for (const conv of [...this.conversations.values()]) {
|
|
1627
|
-
if (this.isIdle(conv)
|
|
1789
|
+
if (!this.isIdle(conv)) continue
|
|
1790
|
+
conv.idleMs += slice
|
|
1791
|
+
if (conv.idleMs > ttl) {
|
|
1628
1792
|
void this.resetSession(conv).catch((error: unknown) => {
|
|
1629
1793
|
this.host.log.warn(
|
|
1630
1794
|
`session sweep cleanup failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -1636,6 +1800,43 @@ export class AntigravityRuntime {
|
|
|
1636
1800
|
}
|
|
1637
1801
|
}
|
|
1638
1802
|
|
|
1803
|
+
function clampEffortToOptions(
|
|
1804
|
+
configOptions: readonly AcpConfigOption[],
|
|
1805
|
+
effort: Effort,
|
|
1806
|
+
): Effort {
|
|
1807
|
+
const levelOption = configOptions.find(
|
|
1808
|
+
(o) => o.id === 'thought_level' || o.category === 'thought_level',
|
|
1809
|
+
)
|
|
1810
|
+
if (levelOption?.options === undefined) return effort
|
|
1811
|
+
const offered = flattenConfigOptions(levelOption.options)
|
|
1812
|
+
.map((o) => asEffort(o.value))
|
|
1813
|
+
.filter((v): v is Effort => v !== undefined)
|
|
1814
|
+
if (offered.length === 0 || offered.includes(effort)) return effort
|
|
1815
|
+
return nearestEffort(effort, offered)
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
function statSignature(path: string): string {
|
|
1819
|
+
try {
|
|
1820
|
+
const st = statSync(path, { bigint: true })
|
|
1821
|
+
return `${st.isDirectory() ? 'd' : 'f'}:${st.ino}:${st.mtimeNs}:${st.ctimeNs}:${st.size}`
|
|
1822
|
+
} catch {
|
|
1823
|
+
return 'missing'
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
function logPathsFingerprint(paths: readonly string[]): string {
|
|
1828
|
+
const parts: string[] = []
|
|
1829
|
+
for (const p of paths) {
|
|
1830
|
+
const sig = statSignature(p)
|
|
1831
|
+
parts.push(`${p}=${sig}`)
|
|
1832
|
+
if (sig.startsWith('d:')) {
|
|
1833
|
+
parts.push(`${p}/cli.log=${statSignature(join(p, 'cli.log'))}`)
|
|
1834
|
+
parts.push(`${p}/log=${statSignature(join(p, 'log'))}`)
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
return parts.join('|')
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1639
1840
|
function messageIds(messages: readonly RequestMessage[]): string[] {
|
|
1640
1841
|
return messages.map((m) => m.id ?? '')
|
|
1641
1842
|
}
|