fix: remove avoidable model runtime limits
Preserve long histories and parsed attachments, expose paged process and Subagent output, isolate MCP discovery failures, and expand browser snapshots and knowledge result selection. Remove fixed Harness output and updated remote Runtime duration limits. Keep released Agent profile and v1 bridge compatibility while enabling larger framed exchanges and optional limits for updated peers. Document remote coverage requirements and real-model boundary validation. Release note: Long model tasks can retain more context and continue reading complete tool output; remote execution preserves compatibility with released Agents. Updated remote limits require an updated Agent and Runtime bundle.
This commit is contained in:
@@ -3124,16 +3124,17 @@ describe('AssistantDatabase', () => {
|
||||
eventIndex,
|
||||
event
|
||||
})
|
||||
const recoveredText = `${'x'.repeat(1_000_001)} recovered tail`
|
||||
const textEvent = {
|
||||
requestId: taskId,
|
||||
type: 'text' as const,
|
||||
delta: '已恢复'
|
||||
delta: recoveredText
|
||||
}
|
||||
expect(append('1', 0, textEvent)).toBe(true)
|
||||
expect(append('1', 0, textEvent)).toBe(false)
|
||||
expect(
|
||||
database.getConversation(conversationId).messages[1]?.content
|
||||
).toBe('已恢复')
|
||||
).toBe(recoveredText)
|
||||
expect(() =>
|
||||
append('1', 0, {
|
||||
requestId: taskId,
|
||||
@@ -3143,7 +3144,7 @@ describe('AssistantDatabase', () => {
|
||||
).toThrow('provenance conflicts')
|
||||
expect(
|
||||
database.getConversation(conversationId).messages[1]?.content
|
||||
).toBe('已恢复')
|
||||
).toBe(recoveredText)
|
||||
|
||||
append('2', 0, {
|
||||
requestId: taskId,
|
||||
@@ -3217,7 +3218,7 @@ describe('AssistantDatabase', () => {
|
||||
).toBe('completed')
|
||||
expect(database.listRecoverableRemoteTasks()).toEqual([])
|
||||
expect(recoveredMessage).toMatchObject({
|
||||
content: '已恢复',
|
||||
content: recoveredText,
|
||||
reasoning: '先验证状态',
|
||||
state: 'complete',
|
||||
artifactIds: [
|
||||
@@ -3239,7 +3240,7 @@ describe('AssistantDatabase', () => {
|
||||
blocks: [
|
||||
expect.objectContaining({
|
||||
type: 'text',
|
||||
content: '已恢复'
|
||||
content: recoveredText
|
||||
}),
|
||||
expect.objectContaining({
|
||||
type: 'reasoning',
|
||||
@@ -3297,7 +3298,7 @@ describe('AssistantDatabase', () => {
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: assistantMessageId,
|
||||
content: '已恢复',
|
||||
content: recoveredText,
|
||||
state: 'complete'
|
||||
})
|
||||
])
|
||||
@@ -3313,7 +3314,7 @@ describe('AssistantDatabase', () => {
|
||||
reopened.getConversation(conversationId).messages[1]
|
||||
).toMatchObject({
|
||||
id: assistantMessageId,
|
||||
content: '已恢复',
|
||||
content: recoveredText,
|
||||
state: 'complete'
|
||||
})
|
||||
reopened.failRecoverableRemoteTask(
|
||||
@@ -3330,7 +3331,7 @@ describe('AssistantDatabase', () => {
|
||||
reopened.getConversation(conversationId).messages[1]
|
||||
).toMatchObject({
|
||||
id: assistantMessageId,
|
||||
content: '已恢复',
|
||||
content: recoveredText,
|
||||
state: 'error',
|
||||
status: '远端请求已不存在'
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { RuntimeModelUsageEvent } from '../agent/runtime'
|
||||
import {
|
||||
DIRECT_MODEL_SUBAGENT_ERROR_MAX_BYTES,
|
||||
@@ -16,6 +16,11 @@ type RequestContext = {
|
||||
authorizationSnapshot: string
|
||||
}
|
||||
|
||||
const services: DirectModelSubagentService<RequestContext>[] = []
|
||||
afterEach(async () => {
|
||||
await Promise.all(services.splice(0).map((service) => service.dispose()))
|
||||
})
|
||||
|
||||
const parent: DirectModelSubagentParent<RequestContext> = {
|
||||
requestId: 'parent-request',
|
||||
projectId: 'project',
|
||||
@@ -41,6 +46,7 @@ function createHarness(
|
||||
runChild,
|
||||
releaseConversation
|
||||
})
|
||||
services.push(service)
|
||||
const run = (
|
||||
overrides: Partial<Parameters<typeof service.run>[0]> = {}
|
||||
) =>
|
||||
@@ -176,6 +182,19 @@ describe('DirectModelSubagentService', () => {
|
||||
DIRECT_MODEL_SUBAGENT_OUTPUT_MAX_BYTES
|
||||
)
|
||||
expect(harness.events.at(-1)?.outputTruncated).toBe(true)
|
||||
const reference = result.outputReference!
|
||||
let fullOutput = result.output
|
||||
let cursor = reference.nextCursor
|
||||
while (cursor < reference.totalBytes) {
|
||||
const page = await harness.service.readOutput('owner', reference.handle, cursor)
|
||||
expect(page.nextCursor).toBeGreaterThan(cursor)
|
||||
fullOutput += page.content
|
||||
cursor = page.nextCursor
|
||||
}
|
||||
expect(fullOutput).toBe('你'.repeat(100_000) + 'late output')
|
||||
expect(harness.events.at(-1)?.outputReference).toEqual(reference)
|
||||
await harness.service.releaseOwner('owner')
|
||||
await expect(harness.service.readOutput('owner', reference.handle)).rejects.toThrow()
|
||||
harness.scheduler.dispose()
|
||||
})
|
||||
|
||||
@@ -210,6 +229,25 @@ describe('DirectModelSubagentService', () => {
|
||||
harness.scheduler.dispose()
|
||||
})
|
||||
|
||||
it.each(['failed', 'cancelled'] as const)('retains all partial output after a %s run until disposal', async (status) => {
|
||||
const controller = new AbortController()
|
||||
const text = 'x'.repeat(DIRECT_MODEL_SUBAGENT_OUTPUT_MAX_BYTES) + 'tail'
|
||||
const harness = createHarness(async (input) => {
|
||||
input.onOutput(text)
|
||||
if (status === 'cancelled') controller.abort(new Error('cancelled'))
|
||||
throw new Error('failed')
|
||||
})
|
||||
const result = await harness.run({ signal: controller.signal })
|
||||
expect(result.status).toBe(status)
|
||||
const reference = result.outputReference!
|
||||
await expect(harness.service.readOutput('owner', reference.handle, reference.nextCursor)).resolves.toMatchObject({
|
||||
content: 'tail', eof: true
|
||||
})
|
||||
await harness.service.dispose()
|
||||
await expect(harness.service.readOutput('owner', reference.handle)).rejects.toThrow()
|
||||
harness.scheduler.dispose()
|
||||
})
|
||||
|
||||
it('emits queued before the shared scheduler admits a run', async () => {
|
||||
const scheduler = new SubagentScheduler({
|
||||
concurrency: 1,
|
||||
|
||||
@@ -2,6 +2,12 @@ import { randomUUID } from 'node:crypto'
|
||||
import { z } from 'zod'
|
||||
import type { RuntimeModelUsageEvent } from '../agent/runtime'
|
||||
import { safeToolErrorDetail } from '../agent/approval-summary'
|
||||
import {
|
||||
PagedOutputStore,
|
||||
type PagedOutputPage,
|
||||
type PagedOutputReference,
|
||||
type PagedOutputWriter
|
||||
} from '../agent/paged-output-store'
|
||||
import { SubagentScheduler } from './subagent-scheduler'
|
||||
|
||||
export const DIRECT_MODEL_SUBAGENT_TASK_MAX_LENGTH = 100_000
|
||||
@@ -51,6 +57,7 @@ export type DirectModelSubagentEvent = {
|
||||
reason: string
|
||||
output?: string
|
||||
outputTruncated?: boolean
|
||||
outputReference?: PagedOutputReference
|
||||
error?: string
|
||||
errorTruncated?: boolean
|
||||
}
|
||||
@@ -68,6 +75,7 @@ export type DirectModelSubagentResult = {
|
||||
conversationId: string
|
||||
output: string
|
||||
outputTruncated: boolean
|
||||
outputReference?: PagedOutputReference
|
||||
error?: string
|
||||
errorTruncated?: boolean
|
||||
modelUsage?: {
|
||||
@@ -167,7 +175,9 @@ function addTokenCount(current: number, value: number): number {
|
||||
|
||||
export class DirectModelSubagentService<TRequestContext = unknown> {
|
||||
private readonly ownerRuns = new Map<string, Set<OwnerRun>>()
|
||||
private readonly outputs = new PagedOutputStore('subagent')
|
||||
private disposed = false
|
||||
private disposePromise?: Promise<void>
|
||||
|
||||
constructor(
|
||||
private readonly dependencies:
|
||||
@@ -229,25 +239,30 @@ export class DirectModelSubagentService<TRequestContext = unknown> {
|
||||
reason = '编程 Subagent 所属会话已释放'
|
||||
): Promise<void> {
|
||||
const runs = [...(this.ownerRuns.get(ownerId) ?? [])]
|
||||
if (runs.length === 0) {
|
||||
return
|
||||
}
|
||||
for (const run of runs) {
|
||||
run.controller.abort(new Error(reason))
|
||||
}
|
||||
await Promise.allSettled(runs.map((run) => run.settled))
|
||||
await this.outputs.releaseOwner(ownerId)
|
||||
}
|
||||
|
||||
async dispose(): Promise<void> {
|
||||
if (this.disposed) {
|
||||
return
|
||||
}
|
||||
readOutput(ownerId: string, handle: string, cursor?: number, limitBytes?: number): Promise<PagedOutputPage> {
|
||||
return this.outputs.read(ownerId, handle, cursor, limitBytes)
|
||||
}
|
||||
|
||||
dispose(): Promise<void> {
|
||||
this.disposePromise ??= this.disposeOnce()
|
||||
return this.disposePromise
|
||||
}
|
||||
|
||||
private async disposeOnce(): Promise<void> {
|
||||
this.disposed = true
|
||||
await Promise.allSettled(
|
||||
[...this.ownerRuns.keys()].map((ownerId) =>
|
||||
this.releaseOwner(ownerId, '编程 Subagent 服务已关闭')
|
||||
)
|
||||
)
|
||||
await this.outputs.dispose()
|
||||
}
|
||||
|
||||
private async execute(
|
||||
@@ -257,8 +272,9 @@ export class DirectModelSubagentService<TRequestContext = unknown> {
|
||||
signal: AbortSignal
|
||||
): Promise<DirectModelSubagentResult> {
|
||||
let output = ''
|
||||
let outputBytes = 0
|
||||
let outputTruncated = false
|
||||
let outputReference: PagedOutputReference | undefined
|
||||
let writer: PagedOutputWriter | undefined
|
||||
let inputTokens = 0
|
||||
let outputTokens = 0
|
||||
let usageReported = false
|
||||
@@ -289,6 +305,7 @@ export class DirectModelSubagentService<TRequestContext = unknown> {
|
||||
? {
|
||||
output,
|
||||
outputTruncated,
|
||||
...(outputReference ? { outputReference } : {}),
|
||||
...(outcome?.error
|
||||
? {
|
||||
error: outcome.error,
|
||||
@@ -320,8 +337,10 @@ export class DirectModelSubagentService<TRequestContext = unknown> {
|
||||
const scheduled = this.dependencies.scheduler.schedule(
|
||||
async (scheduledSignal) => {
|
||||
started = true
|
||||
emit('running')
|
||||
try {
|
||||
writer = await this.outputs.create(input.ownerId, DIRECT_MODEL_SUBAGENT_OUTPUT_MAX_BYTES)
|
||||
scheduledSignal.throwIfAborted()
|
||||
emit('running')
|
||||
await this.dependencies.runChild({
|
||||
task,
|
||||
context,
|
||||
@@ -333,16 +352,7 @@ export class DirectModelSubagentService<TRequestContext = unknown> {
|
||||
'编程 Subagent 输出必须是字符串'
|
||||
)
|
||||
}
|
||||
if (outputTruncated) {
|
||||
return
|
||||
}
|
||||
const remaining =
|
||||
DIRECT_MODEL_SUBAGENT_OUTPUT_MAX_BYTES -
|
||||
outputBytes
|
||||
const bounded = truncateUtf8(delta, remaining)
|
||||
output += bounded.value
|
||||
outputBytes += Buffer.byteLength(bounded.value)
|
||||
outputTruncated = bounded.truncated
|
||||
writer!.append(delta)
|
||||
},
|
||||
onModelUsage: (usage) => {
|
||||
usageReported = true
|
||||
@@ -425,13 +435,25 @@ export class DirectModelSubagentService<TRequestContext = unknown> {
|
||||
outcome.status = 'failed'
|
||||
}
|
||||
|
||||
emit(outcome.status, outcome)
|
||||
try {
|
||||
if (writer) {
|
||||
const saved = await writer.finish()
|
||||
output = saved.text
|
||||
outputTruncated = saved.truncated
|
||||
outputReference = saved.reference
|
||||
}
|
||||
emit(outcome.status, outcome)
|
||||
} catch (error) {
|
||||
await writer?.abort()
|
||||
throw error
|
||||
}
|
||||
return {
|
||||
status: outcome.status,
|
||||
childRunId: context.childRunId,
|
||||
conversationId: context.conversationId,
|
||||
output,
|
||||
outputTruncated,
|
||||
...(outputReference ? { outputReference } : {}),
|
||||
...(outcome.error
|
||||
? {
|
||||
error: outcome.error,
|
||||
|
||||
Reference in New Issue
Block a user