本文定义 ZCode Protocol app-server 内的 session 常驻池实现合同。产品边界和 renderer 生命周期见
仓库根 docs/session-idle-deactivation.md。
每个 CLI 进程用 idle TTL 作为主回收策略,并以高低水位限制突发 resident 数量:
DEFAULT_SESSION_RESIDENT_TARGET_COUNT = 8;
DEFAULT_SESSION_RESIDENT_HIGH_WATER_COUNT = 16;
DEFAULT_SESSION_RESIDENT_IDLE_TIMEOUT_MS = 10 * 60 * 1000;
eligible session 空闲超过 TTL 后无论 resident 数量多少都去激活;当
context.sessions.size > highWater 时,不等待 TTL,按 LRU 回收到 target。若保护中的 session
数量超过 highWater,允许超额,不取消任务、不拒绝新 session、不引入 RSS 硬上限。
DEACTIVATED 必须等价于“当前 CLI 进程从未加载过该 session”:
context.sessions 无 record;eventStore.deleteSession;驻留期间的瞬态事件淘汰见 session-event-store-retention.md);session.removed;gateway readyFlights -> ColdSessionResumeCoordinator.ensureResumed -> activateSessionForResume -> projection hydration;
hydratePublisher 本身仍只负责 projection hydration。legacy resume 保持原 activation/snapshot 入口。SessionResidentPool位置:bootstrap/src/zcode-protocol/session-resident-pool.ts。
宿主窄接口:
interface SessionResidentPoolHost {
listSessionIds(): string[];
readResidencyFacts(sessionId: string): SessionResidencyFacts | null;
deactivate(sessionId: string): Promise<void>;
onDeactivated?(sessionId: string): void;
onError?(sessionId: string, error: unknown): void;
}
职责:
sessionId -> lastTouchedAt;sessionId -> eligibleSinceAt;sessionId -> deactivation Promise,作为 cold resume 闸门;eligibleSinceAt,首次重新 eligible 时从当前时间开始
新 TTL;max(lastTouchedAt, record.updatedAt) 升序选择 LRU,回收到 target;SessionResidencyFactsinterface SessionResidencyFacts {
persisted: boolean;
hasResidencyBlockingWork: boolean;
hasPendingInteractions: boolean;
hasQueuedCommands: boolean;
hasSubscribers: boolean;
hasLegacySubscriber: boolean;
lastActivityAt: number;
}
eligible 必须满足:
persisted
&& !hasResidencyBlockingWork
&& !hasPendingInteractions
&& !hasQueuedCommands
&& !hasSubscribers
&& !hasLegacySubscriber
&& pool.operationLeaseCount(sessionId) === 0
不得使用 deliveryKind !== undefined 作为保护条件。它不是订阅引用计数,读操作也可能写入。
旧 session/subscribe 没有 unsubscribe RPC,因此只在真正 subscribe 时设置
record.legacyStreamSubscribed,以该字段保守保护 replayable/bot stream 到 CLI 进程结束。
AgentRuntime 暴露同步、只读接口:
hasResidencyBlockingWork(): boolean
它统一聚合:
runtime-owned detached work 必须在同步启动 Promise 时加计数,并在同一 Promise 的 finally
减计数。新增 detached sidecar 时必须使用同一 tracker,不能在 pool 适配层逐个猜测。
bootstrap 还要 OR:
record.activeAbortController !== undefined,作为协议适配层的 active 防线;record.residencyFinalizationCount > 0,覆盖 background prompt、compact、goal continuation
释放 active lock 后仍在执行的 command fact、ensure-model、snapshot 与广播收尾。finalization lease 从后台 runner 启动时登记,在所有 state-mutation 收尾完成后的 finally 释放。
它不能通过延迟清空 activeAbortController 代替,因为后者是输入 admission 的 ready lock。
runtime sidecar start ──sync +1──> blocking
promise finally ───── -1───> eligible candidate
protocol background start ─ +1 finalization
active work complete ─ clear activeAbortController
mutation complete ──── -1 finalization
automation、定时/off-peak 任务、browser use、goal continuation 与 compact 不建立旁路判据: 它们必须落到上述 prompt/runtime/finalization owner 链。SSH/WSL/Docker 只改变运行 pool 的 CLI 进程位置,不改变事实来源。
ZCodeProtocolAgentServer.handleRequest 在 dispatch 之前获取 lease,在整个 async dispatch 完成后释放:
handleRequest(request)
-> pool.acquireOperation(sessionIdFromRequest)
-> 全进程 operationCount++
-> 等待同 session 的 in-flight deactivation
-> session operationLeaseCount++
-> touch(sessionId) 并重置 idle TTL
-> dispatchRequest(request)
-> release
-> touch(sessionId) 并重置 idle TTL
-> session operationLeaseCount--
-> 全进程 operationCount--
-> rebalance()
sessionIdFromRequest 读取:
sessionId;conversation/<sessionId> topic;v4/commands/query 中各 command key 的 sessionId;进程级 lease 防止 provider registry 等跨 session await 操作与 sampler 回收并发;按 session lease 同时进入 eligibility facts,便于测试并表达所有权。
触发点:
onSample 低频兜底。算法:
rebalance()
if globalOperationCount > 0: return
resident = host.listSessionIds()
for each resident
facts = read fresh facts
if !eligible(facts)
eligibleSinceAt.delete(sessionId)
else if no eligibleSinceAt
eligibleSinceAt.set(sessionId, now)
expired = eligible candidates where now - eligibleSinceAt >= idleTimeout
for candidate in expired ordered by eligibleSinceAt then LRU
fresh = readResidencyFacts(candidate)
if eligible(fresh) and TTL still expired
executeDeactivation(candidate, reason=idle_timeout)
remaining = host.listSessionIds()
if remaining.length > highWater
candidates = remaining fresh eligible ordered by lastUsedAt
for candidate until remainingCount <= target
fresh = readResidencyFacts(candidate)
if !eligible(fresh): continue
executeDeactivation(candidate, reason=high_water_lru)
排序用 sessionId 作为同时间戳稳定 tie-breaker。TTL 只基于进程内 eligibleSinceAt,不会把磁盘
中很旧的 record.updatedAt 当作刚冷恢复 session 的 idle 起点。touch 与保护状态转换会重置
TTL;LRU touch 与 record.updatedAt 只决定高水位候选顺序。
协议 request release 与既有 60 秒 process resource sampler 都调用 rebalance,不新增定时器。 因此默认 TTL 在成为 eligible 后约 10~11 分钟执行。
server 装配层按以下合同解析兼容配置:
sessionResidentTargetCount 只表达 low-water,不覆盖 high-water;targetCount 时,装配层必须解析出不低于 target 的有效 high-water,不能因默认 high-water
较小而让整个 Agent CLI 在构造期抛错;SessionResidentPool 构造校验拒绝。deactivateSessionRecord(context, sessionId) 的首个同步执行片:
record = sessions.get(sessionId)
v4Gateway.assertSessionRuntimeDeactivatable(sessionId)
record.unsubscribe()
v4Gateway.deactivateSession(sessionId)
sessions.delete(sessionId)
promise = record.app.close()
assertSessionRuntimeDeactivatable 是纯校验,必须位于第一个副作用之前。若校验拒绝,record、
runtime event subscription 与 gateway 运行态全部保持可用;app.close() 是摘除后的异步收尾,
失败由 session 生命周期 owner 记录为 faulted,不纳入可回滚的同步预检事务。
deactivateSession 复用 disposeSession 的 runtime cleanup,但
notifyIndexRemoved=false。清理 publisher、hydration、flush state、projection waiter、attachment
cache、raw sequence、telemetry 归一化状态,以及 CommandInbox 的 per-session settled LRU 和
admission 序号,不调用 indexPublisher.removeSession。CommandInbox 若仍有 in-flight/live pin,
fresh facts 必须阻止去激活;执行面也会在拆 publisher 前再次拒绝。
pool 的 in-flight map 只跟踪策略任务;真正的释放和恢复互斥归 session 生命周期 owner。
close 失败经 onError 记录 warn,策略闸门结束后,生命周期仍保留 faulted;cold resume 明确返回
recoveryRequired,不能因为池的日志处理已经完成就创建新实例。其他 session 的回收继续执行。
运行中的 subagent child 复用父 runtime 的外部 event sink,没有自己的 bootstrap record,但 raw child
event 会在 V4 gateway 建出独立的 conversation publisher(ingestDetachedLiveSession)。它不在
context.sessions 里,因此上文的 resident pool 与去激活执行面都管不到它。
Bug 根因:child 是一次性 session,只有一个 turn,publisher 建出后既没有 record 释放路径,也没有
后继 turn;真机上每跑一次 subagent 就多一套 publisher(完整 projection + 最多 2000 条日志)驻留到
CLI 进程退出(v4.detachedLive 计数器只增不减)。
生命周期合同:
ingestDetachedLiveSession(childId, event, parentSessionId) 记录 child → parent 归属。session/close、创建失败、idle 去激活)走 cleanupSessionRuntime(parent) 时,
连带释放其全部没有自己 record 的 detached child publisher;child 若已有 record(被显式 resume),
交给 child 自己的 record 生命周期。turn_complete / turn_error 记录终态时间;协议 server 借既有 60s 资源采样节拍
调用 pruneDetachedChildPublishers(),终态超过 DETACHED_CHILD_PUBLISHER_GRACE_MS = 120_000、
当前无 conversation 订阅者、且没有 record 的 child 一并释放。child 再次 turn_started 撤销终态记录。subagent_child 持久化在 session store,
hasLiveConversation 返回 false 即按历史 session 恢复,不新增第二条恢复链路。session.removed:detached child 不在 sessions-index 中。本实现不修改协议 schema,不增加通知。renderer 的 SessionDataLayer 在最后一个 pane lease 释放后
按既有 keep-warm timer 执行:
unsubscribe -> ConversationProjectionStore.close -> entries.delete(topic)
该 unsubscribe 让 CLI 的 hasSubscribers 变为 false。CLI pool 与 renderer cache 通过现有 subscription
lease 自然衔接,互不持有对方的生命周期状态。
hasActiveOrQueuedTurnWork 为 true;session/read 的 deliveryKind 不作为 lease;CommandInbox settled/admission 内存,in-flight/live pin 拒绝清理。session.removed;