refactor(web): project usage and snapshot request context
This commit is contained in:
@@ -2,5 +2,5 @@
|
||||
# side as of the last confirmed-consistent state. Both languages carry equal authority;
|
||||
# after editing either side, bring the other along and re-record with:
|
||||
# pnpm run verify-translation-pairing --write packages/client/runtime/README.md
|
||||
README.md: 879730733f3d89c3962d8c54bcfd53795a980049
|
||||
README.zh.md: 1b1d7f03d1f5f03c054dfeaa790a9f6f91e0dca2
|
||||
README.md: ba9a7d455e8a193f23884411eb1928a10f21ddd0
|
||||
README.zh.md: 873fefca48585efed010589917f2c63653b08e5b
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
English | [中文](README.zh.md)
|
||||
|
||||
Client cordis boot and React-free object services: SlotsService wraps SlotCore and supplies renderer data sources; SessionsService owns Session objects, list/scope/history state; WorkspacesService depends on SessionsService and owns Workspace objects, list/actions, default-target derivation, and the New Session blank-reuse entry (`connectWorkspace`). The runtime fans the shared Host stream into both managers. Client sessions are always Host-born (Session+Agent+cwd in one `session.create`); the client holds no pre-entity session state — a session's Agent scope (the client mirror of host dsh-scope, keyed by the shared agent/session id) is born when its row enters the list mirror and dies with the prune. Contract: api-contracts v3 §4. Each `Session` holds a generic `ProjectionValueStore` seeded from the history-tail `projections` block and updated by `session/projection` frames under higher-seq-wins; domain keys (including `todos` and `title`) are read via `projections.faceOf` / `useProjection`, not via `ConversationSnapshot`. Durable `ConversationSnapshot.metrics` instead comes from the separate history-tail value and live `session/metrics` frames because point-in-time token-meter pressure can advance at the same durable log revision; only nondecreasing log and projection revisions are accepted. `ConversationSnapshot.modelRequestContextWindow` separately retains capacity from the latest `session/model-request` observed on the current mux connection. A later request replaces or clears that value, while `session/subscribed` clears both metrics ordering and capacity; reconnect, restore, and a new subscription therefore show no percentage until another request is observed. Missing metrics remain `null` rather than being inferred from the visible node window.
|
||||
Client cordis boot and React-free object services: SlotsService wraps SlotCore and supplies renderer data sources; SessionsService owns Session objects, list/scope/history state; WorkspacesService depends on SessionsService and owns Workspace objects, list/actions, default-target derivation, and the New Session blank-reuse entry (`connectWorkspace`). The runtime fans the shared Host stream into both managers. Client sessions are always Host-born (Session+Agent+cwd in one `session.create`); the client holds no pre-entity session state — a session's Agent scope (the client mirror of host dsh-scope, keyed by the shared agent/session id) is born when its row enters the list mirror and dies with the prune. Contract: api-contracts v3 §4. Each `Session` holds a generic `ProjectionValueStore` seeded from the history-tail `projections` block and updated by `session/projection` frames under higher-seq-wins; domain keys (including `todos`, `title`, and `tokenUsage`) are read via `projections.faceOf` / `useProjection`, not via `ConversationSnapshot`. `ConversationSnapshot.modelRequest` separately retains the complete latest `session/model-request` observed on the current mux connection. Each frame replaces the whole snapshot, so omitted numerator or capacity fields clear an earlier value. `SessionManager` buffers one pre-instantiation snapshot, while `session/subscribed`, disconnect, and removal clear resident and pending values; reconnect, restore, and a new subscription therefore show no context percentage until another request is observed. Model selection alone does not alter request telemetry.
|
||||
|
||||
## Workspace and Session lists
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
[English](README.md) | 中文
|
||||
|
||||
客户端 cordis 启动与不依赖 React 的对象服务:SlotsService 包装 SlotCore 并提供 renderer 数据源;SessionsService 拥有 Session 对象、列表/scope/history 状态;WorkspacesService 依赖 SessionsService,拥有 Workspace 对象、列表/操作、默认目标派生,以及 New Session 空会话复用入口(`connectWorkspace`)。运行时把共享 Host 流分发给两个 manager。客户端 Session 一律由 Host 出生(一次 `session.create` 同瞬产出 Session+Agent+cwd);客户端不持有任何实体化之前的会话状态——Agent scope(host dsh-scope 的客户端镜像,以 agent/session 共用 id 为键)在会话行进入列表镜像时出生,随 prune 死亡。契约:api-contracts v3 §4。每个 `Session` 持有一个通用的 `ProjectionValueStore`,由历史尾页的 `projections` 块播种,并经 `session/projection` 帧按 seq 高者胜更新;领域键(含 `todos` 与 `title`)经 `projections.faceOf`/`useProjection` 读取,不经 `ConversationSnapshot`。持久的 `ConversationSnapshot.metrics` 则来自独立的 history 尾页值与实时 `session/metrics` 帧,因为即时 token-meter 压力可以在相同持久日志修订号上继续变化;客户端只接受日志修订号与投影修订号均不减小的数据。`ConversationSnapshot.modelRequestContextWindow` 另行保留当前 mux 连接观察到的最新 `session/model-request` 容量。后续请求会替换或清除该值,`session/subscribed` 则同时清除指标顺序状态与容量;因此,重连、恢复和新订阅都不会显示百分比,直到观察到另一次请求。缺失的 metrics 保持为 `null`,而不是根据可见节点窗口推断。
|
||||
客户端 cordis 启动与不依赖 React 的对象服务:SlotsService 包装 SlotCore 并提供 renderer 数据源;SessionsService 拥有 Session 对象、列表/scope/history 状态;WorkspacesService 依赖 SessionsService,拥有 Workspace 对象、列表/操作、默认目标派生,以及 New Session 空会话复用入口(`connectWorkspace`)。运行时把共享 Host 流分发给两个 manager。客户端 Session 一律由 Host 出生(一次 `session.create` 同瞬产出 Session+Agent+cwd);客户端不持有任何实体化之前的会话状态——Agent scope(host dsh-scope 的客户端镜像,以 agent/session 共用 id 为键)在会话行进入列表镜像时出生,随 prune 死亡。契约:api-contracts v3 §4。每个 `Session` 持有一个通用的 `ProjectionValueStore`,由历史尾页的 `projections` 块播种,并经 `session/projection` 帧按 seq 高者胜更新;领域键(含 `todos`、`title` 与 `tokenUsage`)经 `projections.faceOf`/`useProjection` 读取,不经 `ConversationSnapshot`。`ConversationSnapshot.modelRequest` 另行保留当前 mux 连接观察到的最新完整 `session/model-request`。每个帧都会替换整个快照,因此分子或容量字段一旦缺失,就会清除先前值。`SessionManager` 会缓冲一个实例化前快照;`session/subscribed`、断开连接和移除会话则会清除常驻值与待处理值;因此,重连、恢复和新订阅都不会显示上下文百分比,直到观察到另一次请求。仅选择模型不会改变请求观测数据。
|
||||
|
||||
## Workspace 与 Session 列表
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import type { CommandId } from '@deepseek-ai/dsh-commands/brand'
|
||||
import type { ContentBlock } from '@deepseek-ai/dsh-llm/types'
|
||||
import type {
|
||||
RpcError, SessionId, SessionMetrics, ToolCallView, ToolResultView,
|
||||
ModelRequestTelemetry, RpcError, SessionId, ToolCallView, ToolResultView,
|
||||
} from '@deepseek-ai/dsh-client-connection/client'
|
||||
import type { PendingInteraction } from './pending.ts'
|
||||
|
||||
@@ -267,16 +267,6 @@ export interface ConversationSnapshot {
|
||||
*/
|
||||
blank: boolean
|
||||
lastAgentError: string | null
|
||||
/**
|
||||
* Host-owned cumulative usage/current pressure. Independent of `nodes`
|
||||
* pagination; null until a tail response or live metrics frame supplies a
|
||||
* current durable value.
|
||||
*/
|
||||
metrics: SessionMetrics | null
|
||||
/**
|
||||
* Capacity from the latest model-request attempt observed on this mux
|
||||
* generation. Absent before the first such request, after a request whose
|
||||
* registration exposes no capacity, and after `session/subscribed`.
|
||||
*/
|
||||
modelRequestContextWindow?: number
|
||||
/** Latest atomic model-request snapshot on this mux generation. */
|
||||
modelRequest: ModelRequestTelemetry | null
|
||||
}
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
// dispatch entry + list state, constructed and held by SessionsService (one per client runtime).
|
||||
// List data never enters zustand; React connects via subscribe/getListSnapshot.
|
||||
|
||||
import type { IApiClient, HostFrame, MuxFrame, RpcError, RpcRequest, RpcResult, SessionId, SessionSummary, WorkspaceId } from '@deepseek-ai/dsh-client-connection/client'
|
||||
import type {
|
||||
HostFrame, IApiClient, ModelRequestTelemetry, MuxFrame, RpcError, RpcRequest,
|
||||
RpcResult, SessionId, SessionSummary, WorkspaceId,
|
||||
} from '@deepseek-ai/dsh-client-connection/client'
|
||||
// Value import from the inline-safe wire layer (not the connection plugin):
|
||||
// plugin-to-plugin value imports are a bundle purity error.
|
||||
import { transportError } from '@deepseek-ai/dsh-host-apiproxy/api'
|
||||
@@ -58,11 +61,11 @@ export class SessionManager {
|
||||
* frames are low-frequency; overflow drops oldest) and dropped on session-removed (audit S7). */
|
||||
private readonly pendingBuffers = new Map<SessionId, RpcRequest<MuxFrame>[]>()
|
||||
/**
|
||||
* Latest model capacity observed for an uninstantiated session on the
|
||||
* Latest request telemetry observed for an uninstantiated session on the
|
||||
* current mux generation. Unlike durable history, this transient frame
|
||||
* cannot be backfilled when get() lazily creates the Session.
|
||||
*/
|
||||
private readonly modelRequestContextWindows = new Map<SessionId, number>()
|
||||
private readonly modelRequests = new Map<SessionId, ModelRequestTelemetry>()
|
||||
/** Outstanding approval questions per session, keyed by approvalId (idempotent under mux-open
|
||||
* replays of the same requested frame). Manager-owned rather than read off Session instances
|
||||
* because the sidebar must light up for sessions never instantiated. Cleared per connection
|
||||
@@ -171,7 +174,7 @@ export class SessionManager {
|
||||
}
|
||||
|
||||
private createSession(sessionId: SessionId): Session {
|
||||
const modelRequestContextWindow = this.modelRequestContextWindows.get(sessionId)
|
||||
const modelRequest = this.modelRequests.get(sessionId)
|
||||
return new Session(sessionId, this.api, {
|
||||
// The sender's local first-send flip mirrors into the list row so the
|
||||
// session surfaces (lists filter on blank) before any host frame lands.
|
||||
@@ -179,7 +182,7 @@ export class SessionManager {
|
||||
this.recordMutation({ kind: 'engaged', sessionId: engaged.sessionId })
|
||||
},
|
||||
projections: this.projectionStore(sessionId),
|
||||
...(modelRequestContextWindow === undefined ? {} : { modelRequestContextWindow }),
|
||||
...(modelRequest === undefined ? {} : { modelRequest }),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -355,13 +358,13 @@ export class SessionManager {
|
||||
return
|
||||
}
|
||||
if (frame.type === 'session/model-request') {
|
||||
// Transient and non-replayable: retain the latest capacity until lazy
|
||||
// instantiation. An absent value explicitly clears an earlier one.
|
||||
if (frame.contextWindow === undefined) this.modelRequestContextWindows.delete(frame.sessionId)
|
||||
else this.modelRequestContextWindows.set(frame.sessionId, frame.contextWindow)
|
||||
// Transient and non-replayable: retain the whole latest request until
|
||||
// lazy instantiation. Missing fields replace rather than inherit.
|
||||
const { type: _type, sessionId, ...modelRequest } = frame
|
||||
this.modelRequests.set(sessionId, modelRequest)
|
||||
}
|
||||
if (frame.type === 'session/subscribed') {
|
||||
this.modelRequestContextWindows.delete(frame.sessionId)
|
||||
this.modelRequests.delete(frame.sessionId)
|
||||
// Rows past the host's durable baseline rode state a restart lost; drop
|
||||
// them so last-wins cannot pin a phantom value over recomputed truth.
|
||||
this.projectionStores.get(frame.sessionId)?.truncate(frame.lastSeq)
|
||||
@@ -440,7 +443,7 @@ export class SessionManager {
|
||||
this.recordMutation({ kind: 'remove', sessionId: frame.sessionId })
|
||||
this.sessions.get(frame.sessionId)?.handleRemoved() // instance survives (resident-instance rule), only flagged in the snapshot
|
||||
this.pendingBuffers.delete(frame.sessionId) // a removed session's buffered frames must not replay on a future instantiation
|
||||
this.modelRequestContextWindows.delete(frame.sessionId) // connection-local request capacity dies with the Host session
|
||||
this.modelRequests.delete(frame.sessionId) // connection-local request telemetry dies with the Host session
|
||||
this.waitingApprovals.delete(frame.sessionId) // a removed session cannot wait on anyone
|
||||
this.projectionStores.delete(frame.sessionId) // removed sessions drop their projection rows with the instance
|
||||
return
|
||||
@@ -481,7 +484,7 @@ export class SessionManager {
|
||||
if (kept.length === 0) this.pendingBuffers.delete(sessionId)
|
||||
else this.pendingBuffers.set(sessionId, kept)
|
||||
}
|
||||
this.modelRequestContextWindows.clear()
|
||||
this.modelRequests.clear()
|
||||
for (const session of this.sessions.values()) session.handleReconnecting()
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { ContentBlock } from '@deepseek-ai/dsh-llm/types'
|
||||
import type { SessionEvent } from '@deepseek-ai/dsh-session/types'
|
||||
import type {
|
||||
HistoryEntry, IApiClient, MuxFrame, RpcError, RpcId, RpcResult,
|
||||
SessionId, SessionMetrics, ToolEventView,
|
||||
ModelRequestTelemetry, SessionId, ToolEventView,
|
||||
} from '@deepseek-ai/dsh-client-connection/client'
|
||||
// Value import from the inline-safe wire layer (not the connection plugin):
|
||||
// plugin-to-plugin value imports are a bundle purity error.
|
||||
@@ -43,8 +43,8 @@ export interface SessionOptions {
|
||||
* private store (bare object-layer construction).
|
||||
*/
|
||||
projections?: ProjectionValueStore
|
||||
/** Model capacity already observed on this mux generation before lazy construction. */
|
||||
modelRequestContextWindow?: number
|
||||
/** Request telemetry already observed on this mux generation before lazy construction. */
|
||||
modelRequest?: ModelRequestTelemetry
|
||||
}
|
||||
|
||||
/** Queue-row preview cap: the dock renders one line, the full content never leaves the host mirror. */
|
||||
@@ -110,10 +110,8 @@ export class Session implements SessionFace {
|
||||
private queueCache: { rev: number; value: QueuedMessage[] } | null = null
|
||||
private frozenRev = 0
|
||||
private nodesCache: { folded: readonly ConversationNode[]; frozenRev: number; value: readonly ConversationNode[] } | null = null
|
||||
/** Host-owned durable usage/current-pressure projection. */
|
||||
private metrics: SessionMetrics | null = null
|
||||
/** Latest capacity observed on this mux connection, independent of durable metrics arrival. */
|
||||
private contextWindow: number | undefined
|
||||
/** Latest atomic request snapshot observed on this mux connection. */
|
||||
private modelRequest: ModelRequestTelemetry | null
|
||||
/** `run_code` sub-dispatches by parent callId (window-derived, like openCalls). Appends
|
||||
* copy-on-write the per-parent array so published snapshot references never mutate. */
|
||||
private codeDispatches = new Map<string, readonly CodeSubCall[]>()
|
||||
@@ -175,7 +173,7 @@ export class Session implements SessionFace {
|
||||
private readonly options: SessionOptions = {},
|
||||
) {
|
||||
this.projections = options.projections ?? new ProjectionValueStore()
|
||||
this.contextWindow = options.modelRequestContextWindow
|
||||
this.modelRequest = options.modelRequest ?? null
|
||||
this.snapshotCache = this.buildSnapshot()
|
||||
}
|
||||
|
||||
@@ -331,10 +329,10 @@ export class Session implements SessionFace {
|
||||
* in-flight open first — its history request rode the dead connection and must not settle
|
||||
* the fresh generation into 'error' (audit S4). */
|
||||
async resync(): Promise<void> {
|
||||
// Queue, metrics, and request capacity are NOT cleared here: onConnected
|
||||
// Queue and request telemetry are NOT cleared here: onConnected
|
||||
// (which drives resync) races the mux frames — fresh-generation state may
|
||||
// have landed already, and the host never resends it. session/subscribed
|
||||
// owns the generation reset before the queue snapshot and metrics frames.
|
||||
// have landed already, and the host never resends request telemetry.
|
||||
// session/subscribed owns the reset before the queue snapshot.
|
||||
if (this.openState === 'cold') return // never opened: no window to rebuild (doOpen flips to 'loading' synchronously, so cold implies no in-flight open)
|
||||
this.openGeneration++
|
||||
this.openPromise = null
|
||||
@@ -415,24 +413,22 @@ export class Session implements SessionFace {
|
||||
this.queueRev++
|
||||
changed = true
|
||||
}
|
||||
if (this.contextWindow !== undefined) {
|
||||
this.contextWindow = undefined
|
||||
changed = true
|
||||
}
|
||||
if (this.metrics !== null) {
|
||||
this.metrics = null
|
||||
if (this.modelRequest !== null) {
|
||||
this.modelRequest = null
|
||||
changed = true
|
||||
}
|
||||
if (changed) this.notifier.markDirty()
|
||||
return
|
||||
}
|
||||
case 'session/metrics': {
|
||||
this.installMetrics(frame.metrics)
|
||||
return
|
||||
}
|
||||
case 'session/model-request': {
|
||||
if (this.contextWindow === frame.contextWindow) return
|
||||
this.contextWindow = frame.contextWindow
|
||||
const {
|
||||
type: _type,
|
||||
sessionId: _sessionId,
|
||||
...modelRequest
|
||||
} = frame
|
||||
// Whole-frame replacement is load-bearing: an omitted numerator or
|
||||
// capacity clears that field from the preceding request.
|
||||
this.modelRequest = modelRequest
|
||||
this.notifier.markDirty()
|
||||
return
|
||||
}
|
||||
@@ -508,17 +504,16 @@ export class Session implements SessionFace {
|
||||
/** Connection-loss boundary: clear values that are not replayed before the next stream starts. */
|
||||
handleReconnecting(): void {
|
||||
this.openGeneration++
|
||||
if (this.metrics === null && this.contextWindow === undefined) return
|
||||
this.metrics = null
|
||||
this.contextWindow = undefined
|
||||
if (this.modelRequest === null) return
|
||||
this.modelRequest = null
|
||||
this.notifier.markDirty()
|
||||
}
|
||||
|
||||
/** host/session-removed relay: flag the resident snapshot and clear connection-local capacity. */
|
||||
/** host/session-removed relay: flag the resident snapshot and clear request telemetry. */
|
||||
handleRemoved(): void {
|
||||
const changed = !this.removed || this.contextWindow !== undefined
|
||||
const changed = !this.removed || this.modelRequest !== null
|
||||
this.removed = true
|
||||
this.contextWindow = undefined
|
||||
this.modelRequest = null
|
||||
if (changed) this.notifier.markDirty()
|
||||
}
|
||||
|
||||
@@ -567,7 +562,6 @@ export class Session implements SessionFace {
|
||||
result.value.events,
|
||||
result.value.hasMore,
|
||||
result.value.projections,
|
||||
result.value.metrics,
|
||||
)
|
||||
// Gap detection (§D.3-4): baseline past the window tail and liveBuffer did not cover it -> pull the tail page once more.
|
||||
const tailSeq = this.windowTailSeq()
|
||||
@@ -579,7 +573,6 @@ export class Session implements SessionFace {
|
||||
result.value.events,
|
||||
result.value.hasMore,
|
||||
result.value.projections,
|
||||
result.value.metrics,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -606,7 +599,6 @@ export class Session implements SessionFace {
|
||||
entries: HistoryEntry[],
|
||||
hasMore: boolean,
|
||||
projections: ProjectionsBaseline | undefined,
|
||||
metrics: SessionMetrics | undefined,
|
||||
): void {
|
||||
this.events = entries.map(e => e.event)
|
||||
this.views = entries.map(e => e.view)
|
||||
@@ -615,7 +607,6 @@ export class Session implements SessionFace {
|
||||
this.foldAdapter.reset(this.events, this.baseSeq, this.views)
|
||||
this.rebuildDerivedFromWindow()
|
||||
if (projections !== undefined) this.projections.seed(projections)
|
||||
if (metrics !== undefined) this.installMetrics(metrics)
|
||||
const buffered = this.liveBuffer
|
||||
this.liveBuffer = []
|
||||
for (const item of buffered) this.appendLive(item.event, item.view)
|
||||
@@ -668,7 +659,6 @@ export class Session implements SessionFace {
|
||||
result.value.events,
|
||||
result.value.hasMore,
|
||||
result.value.projections,
|
||||
result.value.metrics,
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -854,20 +844,6 @@ export class Session implements SessionFace {
|
||||
return tail === undefined ? null : tail.seq
|
||||
}
|
||||
|
||||
/** Install a metrics snapshot unless a newer durable or publication revision already landed. */
|
||||
private installMetrics(metrics: SessionMetrics): void {
|
||||
const current = this.metrics
|
||||
if (
|
||||
current !== null
|
||||
&& (
|
||||
metrics.logRevision < current.logRevision
|
||||
|| metrics.projectionRevision < current.projectionRevision
|
||||
)
|
||||
) return
|
||||
this.metrics = metrics
|
||||
this.notifier.markDirty()
|
||||
}
|
||||
|
||||
private buildSnapshot(): ConversationSnapshot {
|
||||
const { nodes: folded, degraded } = this.foldAdapter.nodes()
|
||||
// Frozen interrupted nodes ride fractional seqs: a stable merge keeps them in flow order.
|
||||
@@ -920,10 +896,7 @@ export class Session implements SessionFace {
|
||||
promptError: this.promptError,
|
||||
blank: this.blankBit,
|
||||
lastAgentError: this.lastAgentError,
|
||||
metrics: this.metrics,
|
||||
...(this.contextWindow === undefined
|
||||
? {}
|
||||
: { modelRequestContextWindow: this.contextWindow }),
|
||||
modelRequest: this.modelRequest,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ describe('runtime client apply', () => {
|
||||
expect(bench.api.callsOf('session.create')).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('clears connection-local Session state on disconnect but not connected', async () => {
|
||||
it('clears connection-local request telemetry on reconnect but not connected', async () => {
|
||||
const bench = await mount()
|
||||
const sessions = bench.ctx.get('sessions') as SessionsService
|
||||
bench.sinks?.onHostEnvelope?.({
|
||||
@@ -112,21 +112,8 @@ describe('runtime client apply', () => {
|
||||
await Promise.resolve()
|
||||
const session = sessions.binding('s-state' as never)?.session
|
||||
if (session === undefined) throw new Error('session binding missing')
|
||||
const currentMetrics = {
|
||||
projectionRevision: 4,
|
||||
logRevision: 10,
|
||||
uncachedInputTokens: 10,
|
||||
outputTokens: 4,
|
||||
cacheReadTokens: 90,
|
||||
cacheWriteTokens: 3,
|
||||
contextTokens: 35,
|
||||
}
|
||||
bench.sinks?.onMuxEnvelope?.({
|
||||
rpcId: 'metrics' as never,
|
||||
payload: { type: 'session/metrics', sessionId: 's-state', metrics: currentMetrics } as never,
|
||||
})
|
||||
bench.sinks?.onMuxEnvelope?.({
|
||||
rpcId: 'capacity' as never,
|
||||
rpcId: 'request' as never,
|
||||
payload: {
|
||||
type: 'session/model-request',
|
||||
sessionId: 's-state',
|
||||
@@ -134,19 +121,20 @@ describe('runtime client apply', () => {
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'alpha',
|
||||
contextTokens: 32_000,
|
||||
contextWindow: 128_000,
|
||||
} as never,
|
||||
})
|
||||
|
||||
bench.sinks?.onConnected?.()
|
||||
expect(session.getSnapshot()).toMatchObject({
|
||||
metrics: currentMetrics,
|
||||
modelRequestContextWindow: 128_000,
|
||||
expect(session.getSnapshot().modelRequest).toMatchObject({
|
||||
model: 'alpha',
|
||||
contextTokens: 32_000,
|
||||
contextWindow: 128_000,
|
||||
})
|
||||
|
||||
bench.sinks?.onDisconnected?.()
|
||||
expect(session.getSnapshot().metrics).toBeNull()
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
bench.sinks?.onStateChange?.('reconnecting')
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
})
|
||||
|
||||
it('stops the stream loop when the plugin fiber unloads', async () => {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { CommandId } from '@deepseek-ai/dsh-commands/brand'
|
||||
import type {
|
||||
ClientResponse, CommandDescriptor, HostFrame, IApiClient, ModelTarget, MuxFrame,
|
||||
RpcError, RpcReceipt, RpcRequest, RpcResponse, SessionId, SessionMetrics, SessionModels,
|
||||
RpcError, RpcReceipt, RpcRequest, RpcResponse, SessionId, SessionModels,
|
||||
SessionProjectionsBlock, SkillEntry,
|
||||
WorkspaceId, WorkspaceView,
|
||||
} from '@deepseek-ai/dsh-client-connection/client'
|
||||
@@ -69,7 +69,6 @@ export class FakeApiClient implements IApiClient {
|
||||
events: never[]
|
||||
hasMore: boolean
|
||||
projections?: SessionProjectionsBlock
|
||||
metrics?: SessionMetrics
|
||||
}>> =
|
||||
() => Promise.resolve(ok({ events: [], hasMore: false }))
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import type { SessionId, SessionMetrics } from '@deepseek-ai/dsh-client-connection/client'
|
||||
import type { SessionId } from '@deepseek-ai/dsh-client-connection/client'
|
||||
import { SessionManager } from '../src/client/sessions/manager.ts'
|
||||
import { FakeApiClient, deferred, err, ok } from './fake-api.ts'
|
||||
import { entries, plainTurn } from './event-script.ts'
|
||||
@@ -41,7 +41,7 @@ describe('instances', () => {
|
||||
expect(manager.get(S2).getSnapshot().pending).toEqual([])
|
||||
})
|
||||
|
||||
it('retains the latest transient model capacity until lazy instantiation', () => {
|
||||
it('retains the latest transient request snapshot until lazy instantiation', () => {
|
||||
const api = new FakeApiClient()
|
||||
const manager = new SessionManager(api)
|
||||
manager.handleMuxEnvelope({
|
||||
@@ -53,6 +53,7 @@ describe('instances', () => {
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'alpha',
|
||||
contextTokens: 12_000,
|
||||
contextWindow: 128_000,
|
||||
},
|
||||
})
|
||||
@@ -65,14 +66,22 @@ describe('instances', () => {
|
||||
step: 2,
|
||||
provider: 'test',
|
||||
model: 'beta',
|
||||
contextTokens: 32_000,
|
||||
contextWindow: 256_000,
|
||||
},
|
||||
})
|
||||
|
||||
expect(manager.get(S1).getSnapshot().modelRequestContextWindow).toBe(256_000)
|
||||
expect(manager.get(S1).getSnapshot().modelRequest).toEqual({
|
||||
turn: 1,
|
||||
step: 2,
|
||||
provider: 'test',
|
||||
model: 'beta',
|
||||
contextTokens: 32_000,
|
||||
contextWindow: 256_000,
|
||||
})
|
||||
})
|
||||
|
||||
it('retains explicit capacity clearing before lazy instantiation', () => {
|
||||
it('retains whole-frame replacement before lazy instantiation', () => {
|
||||
const api = new FakeApiClient()
|
||||
const manager = new SessionManager(api)
|
||||
manager.handleMuxEnvelope({
|
||||
@@ -99,10 +108,15 @@ describe('instances', () => {
|
||||
},
|
||||
})
|
||||
|
||||
expect(manager.get(S1).getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
expect(manager.get(S1).getSnapshot().modelRequest).toEqual({
|
||||
turn: 1,
|
||||
step: 2,
|
||||
provider: 'test',
|
||||
model: 'unknown-capacity',
|
||||
})
|
||||
})
|
||||
|
||||
it('clears retained capacity on subscribed and resident capacity on removal', () => {
|
||||
it('clears retained request telemetry on subscribed and removal', () => {
|
||||
const api = new FakeApiClient()
|
||||
const manager = new SessionManager(api)
|
||||
manager.handleMuxEnvelope({
|
||||
@@ -122,7 +136,7 @@ describe('instances', () => {
|
||||
payload: { type: 'session/subscribed', sessionId: S1, lastSeq: 0 },
|
||||
})
|
||||
const session = manager.get(S1)
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
|
||||
manager.handleMuxEnvelope({
|
||||
rpcId: 'request-after-subscribe' as never,
|
||||
@@ -136,12 +150,12 @@ describe('instances', () => {
|
||||
contextWindow: 256_000,
|
||||
},
|
||||
})
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBe(256_000)
|
||||
expect(session.getSnapshot().modelRequest?.contextWindow).toBe(256_000)
|
||||
manager.handleHostEnvelope({
|
||||
rpcId: 'removed' as never,
|
||||
payload: { type: 'host/session-removed', sessionId: S1 },
|
||||
})
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
|
||||
manager.handleMuxEnvelope({
|
||||
rpcId: 'request-before-lazy-removal' as never,
|
||||
@@ -159,28 +173,15 @@ describe('instances', () => {
|
||||
rpcId: 'lazy-removed' as never,
|
||||
payload: { type: 'host/session-removed', sessionId: S2 },
|
||||
})
|
||||
expect(manager.get(S2).getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
expect(manager.get(S2).getSnapshot().modelRequest).toBeNull()
|
||||
})
|
||||
|
||||
it('clears resident metrics and capacity plus lazy capacity before reconnect', () => {
|
||||
it('clears resident and lazy request telemetry on disconnect', () => {
|
||||
const api = new FakeApiClient()
|
||||
const manager = new SessionManager(api)
|
||||
const session = manager.get(S1)
|
||||
const currentMetrics: SessionMetrics = {
|
||||
projectionRevision: 4,
|
||||
logRevision: 10,
|
||||
uncachedInputTokens: 10,
|
||||
outputTokens: 4,
|
||||
cacheReadTokens: 90,
|
||||
cacheWriteTokens: 3,
|
||||
contextTokens: 35,
|
||||
}
|
||||
manager.handleMuxEnvelope({
|
||||
rpcId: 'metrics' as never,
|
||||
payload: { type: 'session/metrics', sessionId: S1, metrics: currentMetrics },
|
||||
})
|
||||
manager.handleMuxEnvelope({
|
||||
rpcId: 'resident-capacity' as never,
|
||||
rpcId: 'resident-request' as never,
|
||||
payload: {
|
||||
type: 'session/model-request',
|
||||
sessionId: S1,
|
||||
@@ -188,11 +189,12 @@ describe('instances', () => {
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'resident',
|
||||
contextTokens: 35,
|
||||
contextWindow: 128_000,
|
||||
},
|
||||
})
|
||||
manager.handleMuxEnvelope({
|
||||
rpcId: 'lazy-capacity' as never,
|
||||
rpcId: 'lazy-request' as never,
|
||||
payload: {
|
||||
type: 'session/model-request',
|
||||
sessionId: S2,
|
||||
@@ -200,19 +202,20 @@ describe('instances', () => {
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'lazy',
|
||||
contextTokens: 70,
|
||||
contextWindow: 256_000,
|
||||
},
|
||||
})
|
||||
expect(session.getSnapshot()).toMatchObject({
|
||||
metrics: currentMetrics,
|
||||
modelRequestContextWindow: 128_000,
|
||||
expect(session.getSnapshot().modelRequest).toMatchObject({
|
||||
model: 'resident',
|
||||
contextTokens: 35,
|
||||
contextWindow: 128_000,
|
||||
})
|
||||
|
||||
manager.handleReconnecting()
|
||||
manager.handleDisconnected()
|
||||
|
||||
expect(session.getSnapshot().metrics).toBeNull()
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
expect(manager.get(S2).getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
expect(manager.get(S2).getSnapshot().modelRequest).toBeNull()
|
||||
})
|
||||
|
||||
it('caps the pending buffer at 32 keeping the newest, and drops it on session-removed', () => {
|
||||
|
||||
@@ -10,7 +10,7 @@ import { describe, expect, it, vi } from 'vitest'
|
||||
import { Context } from 'cordis'
|
||||
import type { SessionEvent } from '@deepseek-ai/dsh-session/types'
|
||||
import type {
|
||||
SessionId, SessionMetrics, SessionProjectionsBlock,
|
||||
SessionId, SessionProjectionsBlock,
|
||||
} from '@deepseek-ai/dsh-client-connection/client'
|
||||
import { Session } from '../src/client/sessions/session.ts'
|
||||
import { FakeApiClient, deferred, err, ok } from './fake-api.ts'
|
||||
@@ -29,34 +29,15 @@ function histResponse(
|
||||
events: SessionEvent[],
|
||||
hasMore = false,
|
||||
projections?: SessionProjectionsBlock,
|
||||
metrics?: SessionMetrics,
|
||||
) {
|
||||
// history now returns HistoryEntry[] ({event, view?}); these tests are view-less.
|
||||
return Promise.resolve(ok({
|
||||
events: entries(events) as never[],
|
||||
hasMore,
|
||||
...projections === undefined ? {} : { projections },
|
||||
...metrics === undefined ? {} : { metrics },
|
||||
}))
|
||||
}
|
||||
|
||||
function metrics(
|
||||
projectionRevision: number,
|
||||
logRevision: number,
|
||||
over: Partial<SessionMetrics> = {},
|
||||
): SessionMetrics {
|
||||
return {
|
||||
projectionRevision,
|
||||
logRevision,
|
||||
uncachedInputTokens: 10,
|
||||
outputTokens: 4,
|
||||
cacheReadTokens: 90,
|
||||
cacheWriteTokens: 3,
|
||||
contextTokens: 35,
|
||||
...over,
|
||||
}
|
||||
}
|
||||
|
||||
describe('open', () => {
|
||||
it('installs the tail page: cold → loading → open with window and nodes in place', async () => {
|
||||
const { api, session } = makeSession()
|
||||
@@ -70,19 +51,7 @@ describe('open', () => {
|
||||
expect(snapshot.openState).toBe('open')
|
||||
expect(snapshot.hasMore).toBe(true)
|
||||
expect(snapshot.nodes.map(n => n.kind)).toEqual(['user', 'assistant'])
|
||||
expect(snapshot.metrics).toBeNull()
|
||||
})
|
||||
|
||||
it('installs full-log metrics independently of older history pages', async () => {
|
||||
const { api, session } = makeSession()
|
||||
const tailMetrics = metrics(4, 106)
|
||||
api.onHistory = () => histResponse(plainTurn(100, 3, '问', '答'), true, undefined, tailMetrics)
|
||||
await session.open()
|
||||
expect(session.getSnapshot().metrics).toBe(tailMetrics)
|
||||
|
||||
api.onHistory = () => histResponse(plainTurn(94, 2, '旧问', '旧答'))
|
||||
await session.loadOlder()
|
||||
expect(session.getSnapshot().metrics).toBe(tailMetrics)
|
||||
expect(snapshot.modelRequest).toBeNull()
|
||||
})
|
||||
|
||||
it('is idempotent: concurrent opens share one history call, reopening when open is a no-op', async () => {
|
||||
@@ -147,17 +116,8 @@ describe('live event path', () => {
|
||||
expect(session.getSnapshot().nodes).toEqual(before.nodes)
|
||||
})
|
||||
|
||||
it('keeps live capacity separate from durable metrics, replaces or clears it on requests, and resets at subscription', async () => {
|
||||
it('replaces the whole request snapshot, clears omitted fields, and resets at subscription', async () => {
|
||||
const { session } = await opened()
|
||||
const current = metrics(8, 10)
|
||||
session.handleMuxEnvelope('m1' as never, {
|
||||
type: 'session/metrics',
|
||||
sessionId: SID,
|
||||
metrics: current,
|
||||
})
|
||||
expect(session.getSnapshot().metrics).toBe(current)
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
|
||||
session.handleMuxEnvelope('request-1' as never, {
|
||||
type: 'session/model-request',
|
||||
sessionId: SID,
|
||||
@@ -165,32 +125,17 @@ describe('live event path', () => {
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'alpha',
|
||||
contextTokens: 32_000,
|
||||
contextWindow: 128_000,
|
||||
})
|
||||
expect(session.getSnapshot().metrics).toBe(current)
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBe(128_000)
|
||||
|
||||
session.handleMuxEnvelope('m2' as never, {
|
||||
type: 'session/metrics',
|
||||
sessionId: SID,
|
||||
metrics: metrics(9, 9, { uncachedInputTokens: 1 }),
|
||||
expect(session.getSnapshot().modelRequest).toEqual({
|
||||
turn: 1,
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'alpha',
|
||||
contextTokens: 32_000,
|
||||
contextWindow: 128_000,
|
||||
})
|
||||
session.handleMuxEnvelope('m3' as never, {
|
||||
type: 'session/metrics',
|
||||
sessionId: SID,
|
||||
metrics: metrics(7, 11, { uncachedInputTokens: 2 }),
|
||||
})
|
||||
expect(session.getSnapshot().metrics).toBe(current)
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBe(128_000)
|
||||
|
||||
const ordinaryUpdate = metrics(9, 11, { contextTokens: 40 })
|
||||
session.handleMuxEnvelope('m4' as never, {
|
||||
type: 'session/metrics',
|
||||
sessionId: SID,
|
||||
metrics: ordinaryUpdate,
|
||||
})
|
||||
expect(session.getSnapshot().metrics).toBe(ordinaryUpdate)
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBe(128_000)
|
||||
|
||||
session.handleMuxEnvelope('request-2' as never, {
|
||||
type: 'session/model-request',
|
||||
@@ -200,16 +145,19 @@ describe('live event path', () => {
|
||||
provider: 'test',
|
||||
model: 'without-capacity',
|
||||
})
|
||||
expect(session.getSnapshot().metrics).toEqual(ordinaryUpdate)
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
expect(session.getSnapshot().modelRequest).toEqual({
|
||||
turn: 2,
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'without-capacity',
|
||||
})
|
||||
|
||||
session.handleMuxEnvelope('sub' as never, {
|
||||
type: 'session/subscribed',
|
||||
sessionId: SID,
|
||||
lastSeq: 5,
|
||||
})
|
||||
expect(session.getSnapshot().metrics).toBeNull()
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
session.handleMuxEnvelope('request-3' as never, {
|
||||
type: 'session/model-request',
|
||||
sessionId: SID,
|
||||
@@ -217,19 +165,17 @@ describe('live event path', () => {
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'beta',
|
||||
contextTokens: 20,
|
||||
contextWindow: 256_000,
|
||||
})
|
||||
const nextGeneration = metrics(0, 10, { contextTokens: 20 })
|
||||
session.handleMuxEnvelope('m5' as never, {
|
||||
type: 'session/metrics',
|
||||
sessionId: SID,
|
||||
metrics: nextGeneration,
|
||||
expect(session.getSnapshot().modelRequest).toMatchObject({
|
||||
turn: 3,
|
||||
contextTokens: 20,
|
||||
contextWindow: 256_000,
|
||||
})
|
||||
expect(session.getSnapshot().metrics).toBe(nextGeneration)
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBe(256_000)
|
||||
})
|
||||
|
||||
it('publishes a subscribed reset when capacity arrived before durable metrics', async () => {
|
||||
it('publishes a subscribed reset when request telemetry arrived first', async () => {
|
||||
const { session } = await opened()
|
||||
session.handleMuxEnvelope('request' as never, {
|
||||
type: 'session/model-request',
|
||||
@@ -238,17 +184,17 @@ describe('live event path', () => {
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'alpha',
|
||||
contextTokens: 8_000,
|
||||
contextWindow: 128_000,
|
||||
})
|
||||
expect(session.getSnapshot().metrics).toBeNull()
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBe(128_000)
|
||||
expect(session.getSnapshot().modelRequest?.contextWindow).toBe(128_000)
|
||||
|
||||
session.handleMuxEnvelope('sub' as never, {
|
||||
type: 'session/subscribed',
|
||||
sessionId: SID,
|
||||
lastSeq: 5,
|
||||
})
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
})
|
||||
|
||||
it('materializes a command node from live lifecycle frames and reproduces it from a history window', async () => {
|
||||
@@ -876,72 +822,61 @@ describe('remaining branches', () => {
|
||||
})
|
||||
|
||||
describe('resync', () => {
|
||||
it('fences pre-disconnect history behind a fresh mux metrics baseline', async () => {
|
||||
it('clears request telemetry on reconnect and drops a stale in-flight history response', async () => {
|
||||
const { api, session } = makeSession()
|
||||
const stale = deferred<Awaited<ReturnType<FakeApiClient['onHistory']>>>()
|
||||
api.onHistory = () => stale.promise
|
||||
const opening = session.open()
|
||||
const oldLiveMetrics = metrics(8, 10)
|
||||
session.handleMuxEnvelope('old-metrics' as never, {
|
||||
type: 'session/metrics',
|
||||
sessionId: SID,
|
||||
metrics: oldLiveMetrics,
|
||||
})
|
||||
session.handleMuxEnvelope('old-capacity' as never, {
|
||||
session.handleMuxEnvelope('old-request' as never, {
|
||||
type: 'session/model-request',
|
||||
sessionId: SID,
|
||||
turn: 1,
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'old',
|
||||
contextTokens: 20,
|
||||
contextWindow: 128_000,
|
||||
})
|
||||
|
||||
session.handleReconnecting()
|
||||
expect(session.getSnapshot().metrics).toBeNull()
|
||||
expect(session.getSnapshot().modelRequestContextWindow).toBeUndefined()
|
||||
const freshMetrics = metrics(0, 1, { contextTokens: 20 })
|
||||
session.handleMuxEnvelope('fresh-metrics' as never, {
|
||||
type: 'session/metrics',
|
||||
sessionId: SID,
|
||||
metrics: freshMetrics,
|
||||
})
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
|
||||
stale.resolve(ok({
|
||||
events: entries(plainTurn(0, 0, '旧问', '旧答')) as never[],
|
||||
hasMore: false,
|
||||
metrics: metrics(99, 99, { contextTokens: 999 }),
|
||||
}))
|
||||
await opening
|
||||
expect(session.getSnapshot().nodes).toEqual([])
|
||||
expect(session.getSnapshot().metrics).toBe(freshMetrics)
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
|
||||
api.onHistory = () => histResponse(plainTurn(6, 1, '新问', '新答'))
|
||||
await session.resync()
|
||||
expect(session.getSnapshot().openState).toBe('open')
|
||||
expect(session.getSnapshot().nodes.map(node => node.seq)).toEqual([7, 9])
|
||||
expect(session.getSnapshot().metrics).toBe(freshMetrics)
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
})
|
||||
|
||||
it('preserves fresh-generation metrics that arrive before a failing history refresh', async () => {
|
||||
it('preserves a fresh-generation request snapshot when history resync fails', async () => {
|
||||
const { api, session } = makeSession()
|
||||
const oldMetrics = metrics(8, 10)
|
||||
api.onHistory = () => histResponse(plainTurn(0, 0, 'a', 'b'), false, undefined, oldMetrics)
|
||||
api.onHistory = () => histResponse(plainTurn(0, 0, 'a', 'b'))
|
||||
await session.open()
|
||||
expect(session.getSnapshot().metrics).toBe(oldMetrics)
|
||||
|
||||
session.handleMuxEnvelope('sub' as never, {
|
||||
type: 'session/subscribed',
|
||||
sessionId: SID,
|
||||
lastSeq: 5,
|
||||
})
|
||||
expect(session.getSnapshot().metrics).toBeNull()
|
||||
expect(session.getSnapshot().modelRequest).toBeNull()
|
||||
|
||||
const freshMetrics = metrics(0, 10, { contextTokens: 20 })
|
||||
session.handleMuxEnvelope('fresh-metrics' as never, {
|
||||
type: 'session/metrics',
|
||||
session.handleMuxEnvelope('fresh-request' as never, {
|
||||
type: 'session/model-request',
|
||||
sessionId: SID,
|
||||
metrics: freshMetrics,
|
||||
turn: 2,
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'fresh',
|
||||
contextTokens: 20,
|
||||
contextWindow: 256_000,
|
||||
})
|
||||
api.onHistory = () => Promise.resolve(err({
|
||||
code: 'internal',
|
||||
@@ -953,7 +888,14 @@ describe('resync', () => {
|
||||
|
||||
expect(session.getSnapshot()).toMatchObject({
|
||||
openState: 'error',
|
||||
metrics: freshMetrics,
|
||||
modelRequest: {
|
||||
turn: 2,
|
||||
step: 1,
|
||||
provider: 'test',
|
||||
model: 'fresh',
|
||||
contextTokens: 20,
|
||||
contextWindow: 256_000,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user