fix: reuse runtime processes across project sessions

Multiple projects could spawn duplicate heavyweight runtimes, while settings
changes and remote reconnection could cancel or strand native conversations.
Share compatible local OpenCode and DeepSeek Harness process owners and
Agent-owned ACP connections, retaining workspace routing, per-session modes,
model attribution, question ownership, targeted cancellation and idle cleanup.

Restore remote pending questions and terminal task projections across reconnect
and desktop restart, and preserve accepted ordinary requests during settings
replacement while cancelling expert-team parents with their replaced scheduler.
Extend regression coverage and Agent type checks. Record measured resource and
real-model validation results, including failures and limitations.

Also include the conversation media-generation design drafts (not an implemented
feature), documentation entry links and dedicated Linux test-directory guidance.
Desktop and Agent changes must be delivered together for shared process results.

Validation: typecheck and lint passed; staged content and documentation links
checked. Prior full regression and live-model evidence are recorded in progress
notes; no new full test suite or model calls were run for this commit.

Release note: 多项目会话现在复用兼容的 OpenCode 和 DeepSeek Harness 进程,减少重复启动并保持工作区、工具和取消隔离。修复远端问答在断线、重启及配置更新后的恢复与状态同步问题,Desktop 与 Agent 需配套更新。
This commit is contained in:
mesalogo
2026-09-15 08:14:42 +08:00
parent b755951ecd
commit e5aa9ed688
65 changed files with 4116 additions and 365 deletions
+19 -6
View File
@@ -80,6 +80,7 @@ export class AgentRuntimeController implements AgentRuntime {
private retirement?: Promise<void>
private disposal?: Promise<void>
private readonly ownedConversationIds = new Set<string>()
private readonly questionOwners = new Map<string, { slot: RuntimeSlot; requestId: string }>()
constructor(
runtime: AgentRuntime,
@@ -147,11 +148,15 @@ export class AgentRuntimeController implements AgentRuntime {
return this.ownedConversationIds.size
}
get sharedProcess(): boolean {
return this.current.runtime.sharedProcess === true
}
get canRetire(): boolean {
return (
!this.closing &&
this.activeRequestCount === 0 &&
this.ownedConversationIds.size === 0
(this.sharedProcess || this.ownedConversationIds.size === 0)
)
}
@@ -269,7 +274,7 @@ export class AgentRuntimeController implements AgentRuntime {
slot.activeRequests += 1
try {
const status = await operation(slot.runtime)
if (slot !== this.current) {
if (slot !== this.current && !(slot.runtime.sharedProcess && stage === 'compact')) {
throw new Error('Runtime 已切换,请重试')
}
return status
@@ -347,13 +352,16 @@ export class AgentRuntimeController implements AgentRuntime {
signal,
effectiveAuthorize
)) {
if (slot !== this.current && !slot.drainable) {
if (slot !== this.current && !slot.drainable && !slot.runtime.sharedProcess) {
throw new Error('Runtime 已切换,当前请求已中断')
}
toolDenied = false
if (event.type === 'question') {
this.questionOwners.set(event.questionId, { slot, requestId: request.requestId })
}
yield event
}
if (slot !== this.current && !slot.drainable) {
if (slot !== this.current && !slot.drainable && !slot.runtime.sharedProcess) {
throw new Error('Runtime 已切换,当前请求已中断')
}
} catch (error) {
@@ -368,6 +376,9 @@ export class AgentRuntimeController implements AgentRuntime {
throw error
}
} finally {
for (const [questionId, owner] of this.questionOwners) {
if (owner.requestId === request.requestId) this.questionOwners.delete(questionId)
}
slot.activeRequests -= 1
if (
slot.retiring &&
@@ -419,14 +430,16 @@ export class AgentRuntimeController implements AgentRuntime {
questionId: string,
answers?: AgentQuestionAnswer[]
): Promise<void> {
if (this.closing) {
const owner = this.questionOwners.get(questionId)
if (this.closing && !owner) {
throw new Error('Agent Runtime 正在关闭')
}
const runtime = this.current.runtime
const runtime = (owner?.slot ?? this.current).runtime
if (!runtime.respondToQuestion) {
throw new Error('当前 Runtime 不支持回答交互式问题')
}
await runtime.respondToQuestion(questionId, answers)
this.questionOwners.delete(questionId)
}
private retireSlot(slot: RuntimeSlot): Promise<void> {