Merge branch 'worktree/process-plugin-streaming-72f9b3' into worktree/job-kill-delivery
# Conflicts: # apps/web/tests/background-job-list.e2e.ts # apps/web/tests/live-job-stream.e2e.ts # docs/config-catalog.i18n.yaml # docs/config-catalog.md # docs/config-catalog.zh.md # docs/event-producer-consumer.i18n.yaml # docs/event-producer-consumer.md # packages/client/ui-activity/README.md # packages/client/ui-activity/README.zh.md # packages/client/ui-activity/src/client/index.ts # packages/client/ui-jobs/README.i18n.yaml # packages/client/ui-jobs/src/client/JobListAction.tsx # packages/client/ui-jobs/tests/browser-plugin.client.spec.ts # packages/client/ui-jobs/tests/job-list-action.client.spec.tsx # packages/jobs/jobs-local/src/index.ts # packages/jobs/jobs/README.i18n.yaml # packages/jobs/jobs/README.md # packages/jobs/jobs/README.zh.md # packages/jobs/jobs/src/index.ts # packages/shell/tool-bash/src/index.ts # packages/shell/tool-bash/tests/observe.spec.ts # packages/shell/tool-pwsh/src/index.ts # packages/shell/tool-pwsh/tests/observe.spec.ts # packages/workflow/tool-workflow/tests/activity-mirror.spec.ts # pnpm-lock.yaml
This commit is contained in:
commit
a2fb67d06c
187 files changed
+3599
-7085
No files matched your search
@@ -106,6 +106,18 @@ async function flush(): Promise<void> {
|
||||
}
|
||||
|
||||
describe('Session Controller Client apply', () => {
|
||||
it('installs ctx.jobOutput over the captured session namespace', async () => {
|
||||
const bench = await mount()
|
||||
expect(bench.ctx.jobOutput).toBeDefined()
|
||||
// The fake observeJob never yields; opening and releasing must leave no
|
||||
// view and no error behind.
|
||||
const stop = bench.ctx.jobOutput.observe(sid('session-1'), 'bash-1' as never)
|
||||
await flush()
|
||||
stop()
|
||||
await flush()
|
||||
expect(bench.ctx.jobOutput.state.getSnapshot().observed).toEqual({})
|
||||
})
|
||||
|
||||
it('routes Session Remote Events and connection generations into the object layer', async () => {
|
||||
const connected = vi.spyOn(ClientSessions.prototype, 'handleConnected')
|
||||
const error = vi.spyOn(ClientSessions.prototype, 'handleSessionError')
|
||||
|
||||
@@ -163,6 +163,25 @@ describe('Session control jobs updates', () => {
|
||||
])
|
||||
})
|
||||
|
||||
it('carries outputTotal exactly for record-declaring jobs', async () => {
|
||||
const { ctx, agent, control } = await harness(true)
|
||||
const abort = new AbortController()
|
||||
const collected = collectJobs(control.control(abort.signal), 1, abort)
|
||||
ctx.jobs.start({
|
||||
kind: 'bash',
|
||||
label: 'observable run',
|
||||
owner: agent,
|
||||
record: true,
|
||||
run: (job) => {
|
||||
job.append('hi')
|
||||
return { cancel: () => {}, done: new Promise<never>(() => {}) }
|
||||
},
|
||||
})
|
||||
|
||||
const [frame] = await collected
|
||||
expect(frame?.jobs[0]?.outputTotal).toBe(2)
|
||||
})
|
||||
|
||||
it('fans an unowned change out to every attached session', async () => {
|
||||
const { ctx, control } = await harness(true)
|
||||
const second = ctx.sessions.create()
|
||||
|
||||
@@ -10,7 +10,7 @@ import type {
|
||||
import type {
|
||||
SessionAddress,
|
||||
SessionControlBaseline,
|
||||
SessionControlFrame,
|
||||
SessionControlFrame, SessionObserveJobFrame,
|
||||
SessionFollowFrame,
|
||||
SessionFollowRequest,
|
||||
SessionPage,
|
||||
@@ -237,6 +237,9 @@ export class FakeApiClient {
|
||||
page: request => this.page(request),
|
||||
follow: (request, signal) => this.openFollow(request, signal),
|
||||
control: signal => this.openControl(signal),
|
||||
// Job-record observation never yields here; the stream consumer is
|
||||
// exercised directly by job-output.client.spec.
|
||||
observeJob: () => (async function* (): AsyncGenerator<SessionObserveJobFrame> {})(),
|
||||
},
|
||||
subagents: {
|
||||
list: parentSessionId => this.record(
|
||||
|
||||
@@ -0,0 +1,413 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { Context } from '@deepseek-ai/cordis'
|
||||
import type { JobId } from '@deepseek-ai/dsh-jobs/brand'
|
||||
import type { SessionId } from '@deepseek-ai/dsh-session/types'
|
||||
import { ClientJobOutput, ClientJobOutputModel } from '../src/client/job-output.ts'
|
||||
import type { SessionObserveJobFrame } from '../src/types.ts'
|
||||
|
||||
const ID = 'bash-1' as JobId
|
||||
|
||||
function opened(model: ClientJobOutputModel, over: Partial<{ from: number; earliest: number; total: number }> = {}): void {
|
||||
model.observeOpened(ID, {
|
||||
type: 'opened',
|
||||
jobId: ID,
|
||||
from: 0,
|
||||
earliest: 0,
|
||||
total: 0,
|
||||
status: 'running',
|
||||
...over,
|
||||
})
|
||||
}
|
||||
|
||||
describe('ClientJobOutputModel observation', () => {
|
||||
it('accumulates output, advances the resume cursor, and settles on status', () => {
|
||||
const model = new ClientJobOutputModel()
|
||||
opened(model)
|
||||
expect(model.cursorOf(ID)).toBe(0)
|
||||
model.observeOutput(ID, { type: 'output', chunks: [{ at: 0, text: 'a' }, { at: 1, text: 'b' }], next: 2 })
|
||||
model.observeOutput(ID, { type: 'output', chunks: [{ at: 2, text: 'c' }], next: 3 })
|
||||
expect(model.cursorOf(ID)).toBe(3)
|
||||
const view = model.getSnapshot().observed[String(ID)]
|
||||
expect(view?.text).toBe('abc')
|
||||
expect(view?.streaming).toBe(true)
|
||||
model.observeStatus(ID, { type: 'status', status: 'completed', detail: 'exit code: 0' })
|
||||
const settled = model.getSnapshot().observed[String(ID)]
|
||||
expect(settled?.streaming).toBe(false)
|
||||
expect(settled?.status).toBe('completed')
|
||||
expect(settled?.detail).toBe('exit code: 0')
|
||||
})
|
||||
|
||||
it('keeps snapshot identity stable between changes and notifies subscribers', () => {
|
||||
const model = new ClientJobOutputModel()
|
||||
let notified = 0
|
||||
const unsubscribe = model.subscribe(() => { notified += 1 })
|
||||
const before = model.getSnapshot()
|
||||
expect(model.getSnapshot()).toBe(before)
|
||||
opened(model)
|
||||
expect(notified).toBe(1)
|
||||
expect(model.getSnapshot()).not.toBe(before)
|
||||
unsubscribe()
|
||||
model.observeStopped(ID)
|
||||
expect(notified).toBe(1)
|
||||
})
|
||||
|
||||
it('marks gaps from lossy frames, gap chunks, and a resume behind the retained head', () => {
|
||||
const model = new ClientJobOutputModel()
|
||||
opened(model, { from: 4, earliest: 8, total: 10 })
|
||||
expect(model.getSnapshot().observed[String(ID)]?.gapBefore).toBe(true)
|
||||
|
||||
const clean = new ClientJobOutputModel()
|
||||
opened(clean)
|
||||
clean.observeOutput(ID, { type: 'output', chunks: [{ at: 0, text: 'x', gapBefore: true }], next: 1 })
|
||||
expect(clean.getSnapshot().observed[String(ID)]?.gapBefore).toBe(true)
|
||||
|
||||
const lossy = new ClientJobOutputModel()
|
||||
opened(lossy)
|
||||
lossy.observeOutput(ID, { type: 'output', chunks: [], next: 5, lossy: true })
|
||||
expect(lossy.getSnapshot().observed[String(ID)]?.gapBefore).toBe(true)
|
||||
})
|
||||
|
||||
it('marks a fresh observation anchored past the evicted head', () => {
|
||||
// Fresh observations anchor at the registry's earliest retained byte, so
|
||||
// from === earliest > 0 means the head was already discarded.
|
||||
const fresh = new ClientJobOutputModel()
|
||||
opened(fresh, { from: 60_240, earliest: 60_240, total: 321_328 })
|
||||
expect(fresh.getSnapshot().observed[String(ID)]?.gapBefore).toBe(true)
|
||||
|
||||
// A retry that accumulated no text yet earns the mark the same way.
|
||||
const retried = new ClientJobOutputModel()
|
||||
opened(retried)
|
||||
expect(retried.getSnapshot().observed[String(ID)]?.gapBefore).toBe(false)
|
||||
opened(retried, { from: 6, earliest: 6, total: 6 })
|
||||
expect(retried.getSnapshot().observed[String(ID)]?.gapBefore).toBe(true)
|
||||
})
|
||||
|
||||
it('bounds the render tail without splitting a surrogate pair', () => {
|
||||
const model = new ClientJobOutputModel()
|
||||
opened(model)
|
||||
const emoji = '😀'.repeat((64 * 1024) + 8)
|
||||
model.observeOutput(ID, { type: 'output', chunks: [{ at: 0, text: emoji }], next: emoji.length * 2 })
|
||||
const view = model.getSnapshot().observed[String(ID)]
|
||||
expect(view?.gapBefore).toBe(true)
|
||||
expect(view!.text.length).toBeLessThanOrEqual(128 * 1024)
|
||||
// The bound landed between pairs: the surviving text still round-trips.
|
||||
expect(/^(?:😀)+$/u.test(view!.text)).toBe(true)
|
||||
})
|
||||
|
||||
it('carries the anchor detail and trims a plain-ASCII tail without a boundary shift', () => {
|
||||
const model = new ClientJobOutputModel()
|
||||
model.observeOpened(ID, {
|
||||
type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running', detail: 'exit soon',
|
||||
})
|
||||
expect(model.getSnapshot().observed[String(ID)]?.detail).toBe('exit soon')
|
||||
const long = 'x'.repeat((128 * 1024) + 5)
|
||||
model.observeOutput(ID, { type: 'output', chunks: [{ at: 0, text: long }], next: long.length })
|
||||
const view = model.getSnapshot().observed[String(ID)]
|
||||
expect(view?.text.length).toBe(128 * 1024)
|
||||
expect(view?.gapBefore).toBe(true)
|
||||
})
|
||||
|
||||
it('advances the cut past a low surrogate landing exactly on the bound', () => {
|
||||
const model = new ClientJobOutputModel()
|
||||
opened(model)
|
||||
// 'z' + one emoji + odd ASCII tail puts a low surrogate exactly at the cut index.
|
||||
const text = 'z😀' + 'a'.repeat((128 * 1024) - 1)
|
||||
model.observeOutput(ID, { type: 'output', chunks: [{ at: 0, text }], next: text.length })
|
||||
const view = model.getSnapshot().observed[String(ID)]
|
||||
expect(view?.text.length).toBe((128 * 1024) - 1)
|
||||
expect(view?.text.startsWith('a')).toBe(true)
|
||||
})
|
||||
|
||||
it('preserves accumulated text across a reconnect anchor and clears on stop', () => {
|
||||
const model = new ClientJobOutputModel()
|
||||
opened(model)
|
||||
model.observeOutput(ID, { type: 'output', chunks: [{ at: 0, text: 'kept' }], next: 4 })
|
||||
opened(model, { from: 4, earliest: 0, total: 4 })
|
||||
expect(model.getSnapshot().observed[String(ID)]?.text).toBe('kept')
|
||||
model.observeStopped(ID)
|
||||
expect(model.getSnapshot().observed[String(ID)]).toBeUndefined()
|
||||
expect(model.cursorOf(ID)).toBeUndefined()
|
||||
// A second stop is inert.
|
||||
model.observeStopped(ID)
|
||||
})
|
||||
|
||||
it('records a terminal stream failure on the live view', () => {
|
||||
const model = new ClientJobOutputModel()
|
||||
opened(model)
|
||||
model.observeFailed(ID, new Error('carrier gone'))
|
||||
const view = model.getSnapshot().observed[String(ID)]
|
||||
expect(view?.streaming).toBe(false)
|
||||
expect(view?.error).toContain('carrier gone')
|
||||
// A failure for an untracked id is inert.
|
||||
model.observeFailed('bash-9' as JobId, new Error('ignored'))
|
||||
})
|
||||
})
|
||||
|
||||
/** One scripted logical stream: frames are pushed by the test, never reopened. */
|
||||
class FakeStream {
|
||||
disposed = false
|
||||
/** Model a carrier whose disposal surfaces as an iterator throw. */
|
||||
throwOnDispose = false
|
||||
/** Hold the dispose promise open until {@link releaseDispose}. */
|
||||
deferDispose = false
|
||||
private disposeRelease: (() => void) | undefined
|
||||
private disposePending: Promise<void> | undefined
|
||||
private readonly frames: SessionObserveJobFrame[] = []
|
||||
private waiter: (() => void) | undefined
|
||||
private failure: Error | undefined
|
||||
readonly accepts: number[] = []
|
||||
|
||||
push(frame: SessionObserveJobFrame): void {
|
||||
this.frames.push(frame)
|
||||
this.waiter?.()
|
||||
}
|
||||
|
||||
poison(error: Error): void {
|
||||
this.failure = error
|
||||
this.waiter?.()
|
||||
}
|
||||
|
||||
dispose(): Promise<void> {
|
||||
if (this.throwOnDispose && this.failure === undefined) {
|
||||
this.failure = new Error('carrier tore down')
|
||||
} else {
|
||||
this.disposed = true
|
||||
}
|
||||
this.waiter?.()
|
||||
if (this.deferDispose) {
|
||||
// Repeat disposals (releaser plus the consumer's finally) share one
|
||||
// deferred promise, so releaseDispose resumes every waiter.
|
||||
this.disposePending ??= new Promise((resolve) => { this.disposeRelease = resolve })
|
||||
return this.disposePending
|
||||
}
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
releaseDispose(): void {
|
||||
this.disposeRelease?.()
|
||||
}
|
||||
|
||||
async *[Symbol.asyncIterator]() {
|
||||
let generation = 1
|
||||
while (!this.disposed) {
|
||||
if (this.failure !== undefined) throw this.failure
|
||||
const frame = this.frames.shift()
|
||||
if (frame === undefined) {
|
||||
await new Promise<void>((resolve) => { this.waiter = resolve })
|
||||
continue
|
||||
}
|
||||
yield {
|
||||
generation,
|
||||
value: frame,
|
||||
signal: new AbortController().signal,
|
||||
accept: () => { this.accepts.push(generation) },
|
||||
}
|
||||
generation = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function bench() {
|
||||
const ctx = new Context()
|
||||
const model = new ClientJobOutputModel()
|
||||
const streams: { options: { open: (signal: AbortSignal) => unknown }; stream: FakeStream }[] = []
|
||||
const observeCalls: unknown[] = []
|
||||
const remote = {
|
||||
$stream: (options: { open: (signal: AbortSignal) => unknown }) => {
|
||||
const stream = new FakeStream()
|
||||
streams.push({ options, stream })
|
||||
return stream
|
||||
},
|
||||
session: {
|
||||
observeJob: (request: unknown) => {
|
||||
observeCalls.push(request)
|
||||
return { [Symbol.asyncIterator]: async function* () { /* never yields */ } }
|
||||
},
|
||||
},
|
||||
}
|
||||
const output = new ClientJobOutput(ctx, remote as never, model)
|
||||
return { ctx, model, output, streams, observeCalls }
|
||||
}
|
||||
|
||||
const tick = () => new Promise<void>(resolve => setTimeout(resolve, 0))
|
||||
|
||||
describe('ClientJobOutput observation streams', () => {
|
||||
it('feeds anchor, output, and terminal frames into the model, then closes', async () => {
|
||||
const { model, output, streams } = bench()
|
||||
output.observe(undefined, ID)
|
||||
const { stream } = streams[0]!
|
||||
stream.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
stream.push({ type: 'output', chunks: [{ at: 0, text: 'hi' }], next: 2 })
|
||||
await tick()
|
||||
expect(model.getSnapshot().observed[String(ID)]?.text).toBe('hi')
|
||||
expect(stream.accepts).toHaveLength(1)
|
||||
|
||||
stream.push({ type: 'status', status: 'completed' })
|
||||
await tick()
|
||||
expect(model.getSnapshot().observed[String(ID)]?.streaming).toBe(false)
|
||||
expect(stream.disposed).toBe(true)
|
||||
})
|
||||
|
||||
it('shares one stream across observers and stops after the last release', async () => {
|
||||
const { model, output, streams } = bench()
|
||||
const first = output.observe(undefined, ID)
|
||||
const second = output.observe(undefined, ID)
|
||||
expect(streams).toHaveLength(1)
|
||||
streams[0]!.stream.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
await tick()
|
||||
|
||||
first()
|
||||
first()
|
||||
expect(streams[0]!.stream.disposed).toBe(false)
|
||||
second()
|
||||
await tick()
|
||||
expect(streams[0]!.stream.disposed).toBe(true)
|
||||
expect(model.getSnapshot().observed[String(ID)]).toBeUndefined()
|
||||
})
|
||||
|
||||
it('a late release of a superseded observation neither tears down nor clears its successor', async () => {
|
||||
const { model, output, streams } = bench()
|
||||
const firstStop = output.observe(undefined, ID)
|
||||
const first = streams[0]!.stream
|
||||
first.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
first.push({ type: 'status', status: 'completed' })
|
||||
await tick()
|
||||
// A successor observation replaces the settled entry while the first
|
||||
// observer still holds its releaser.
|
||||
const secondStop = output.observe(undefined, ID)
|
||||
const second = streams[1]!.stream
|
||||
second.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
await tick()
|
||||
|
||||
firstStop()
|
||||
await tick()
|
||||
expect(second.disposed).toBe(false)
|
||||
expect(model.getSnapshot().observed[String(ID)]).toBeDefined()
|
||||
|
||||
secondStop()
|
||||
await tick()
|
||||
expect(second.disposed).toBe(true)
|
||||
expect(model.getSnapshot().observed[String(ID)]).toBeUndefined()
|
||||
})
|
||||
|
||||
it('a re-observation inside the dispose round-trip keeps its fresh view', async () => {
|
||||
const { model, output, streams } = bench()
|
||||
const stop = output.observe(undefined, ID)
|
||||
const first = streams[0]!.stream
|
||||
first.deferDispose = true
|
||||
first.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
await tick()
|
||||
|
||||
stop()
|
||||
// Re-expand while the previous generation's dispose is still in flight.
|
||||
output.observe(undefined, ID)
|
||||
const second = streams[1]!.stream
|
||||
second.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
await tick()
|
||||
|
||||
first.releaseDispose()
|
||||
await tick()
|
||||
// The stale post-dispose clear must not blank the successor's view.
|
||||
expect(model.getSnapshot().observed[String(ID)]).toBeDefined()
|
||||
second.push({ type: 'output', chunks: [{ at: 0, text: 'alive' }], next: 5 })
|
||||
await tick()
|
||||
expect(model.getSnapshot().observed[String(ID)]?.text).toBe('alive')
|
||||
})
|
||||
|
||||
it('opens a fresh stream for a re-observed job and resumes from the model cursor', async () => {
|
||||
const { output, streams, observeCalls, model } = bench()
|
||||
const stop = output.observe('alice' as SessionId, ID)
|
||||
streams[0]!.stream.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
streams[0]!.stream.push({ type: 'output', chunks: [{ at: 0, text: 'abc' }], next: 3 })
|
||||
await tick()
|
||||
// The generation opener reads the live cursor at open time and carries the
|
||||
// fenced-read session.
|
||||
streams[0]!.options.open(new AbortController().signal)
|
||||
expect(observeCalls.at(-1)).toEqual({ jobId: ID, sessionId: 'alice', from: 3 })
|
||||
|
||||
stop()
|
||||
await tick()
|
||||
output.observe(undefined, ID)
|
||||
expect(streams).toHaveLength(2)
|
||||
streams[1]!.options.open(new AbortController().signal)
|
||||
// Stopped observation dropped the cursor: the fresh stream starts unanchored.
|
||||
expect(observeCalls.at(-1)).toEqual({ jobId: ID })
|
||||
expect(model.cursorOf(ID)).toBeUndefined()
|
||||
})
|
||||
|
||||
it('classifies a premature end as retryable only after the anchor', () => {
|
||||
const { output, streams } = bench()
|
||||
output.observe(undefined, ID)
|
||||
const options = streams[0]!.options as unknown as { ended: (accepted: boolean) => Error }
|
||||
expect(options.ended(true).name).toBe('RemoteStreamCarrierError')
|
||||
expect(options.ended(false).name).toBe('Error')
|
||||
})
|
||||
|
||||
it('records a consumer failure on the live view', async () => {
|
||||
const { model, output, streams } = bench()
|
||||
output.observe(undefined, ID)
|
||||
const { stream } = streams[0]!
|
||||
stream.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
await tick()
|
||||
stream.poison(new Error('frame decode broke'))
|
||||
await tick()
|
||||
const view = model.getSnapshot().observed[String(ID)]
|
||||
expect(view?.error).toContain('frame decode broke')
|
||||
expect(view?.streaming).toBe(false)
|
||||
})
|
||||
|
||||
it('treats a release after service disposal as inert and stays silent for post-stop failures', async () => {
|
||||
const { ctx, model, output, streams } = bench()
|
||||
const stop = output.observe(undefined, ID)
|
||||
const { stream } = streams[0]!
|
||||
stream.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
await tick()
|
||||
// Poison wakes the consumer, but the synchronous stop lands first, so the
|
||||
// failure arrives on an already-stopped entry and stays silent.
|
||||
stream.poison(new Error('after stop'))
|
||||
stop()
|
||||
await tick()
|
||||
expect(model.getSnapshot().observed[String(ID)]).toBeUndefined()
|
||||
|
||||
const second = output.observe(undefined, ID)
|
||||
await ctx.fiber.dispose()
|
||||
// The disposal path already removed every entry; a live releaser whose
|
||||
// entry is gone is inert.
|
||||
second()
|
||||
})
|
||||
|
||||
it('stays silent when the carrier teardown itself throws after a stop', async () => {
|
||||
const { model, output, streams } = bench()
|
||||
const stop = output.observe(undefined, ID)
|
||||
const { stream } = streams[0]!
|
||||
stream.throwOnDispose = true
|
||||
stream.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
await tick()
|
||||
// The releaser marks the entry stopped, then its dispose surfaces as an
|
||||
// iterator throw — which the consumer swallows for a stopped entry.
|
||||
stop()
|
||||
await tick()
|
||||
expect(model.getSnapshot().observed[String(ID)]?.error).toBeUndefined()
|
||||
})
|
||||
|
||||
it('records a terminal stream failure and service disposal closes live streams', async () => {
|
||||
const { ctx, model, output, streams } = bench()
|
||||
output.observe(undefined, ID)
|
||||
const failing = streams[0]!.stream
|
||||
failing.push({ type: 'opened', jobId: ID, from: 0, earliest: 0, total: 0, status: 'running' })
|
||||
await tick()
|
||||
const iterator = failing[Symbol.asyncIterator]()
|
||||
void iterator
|
||||
// Simulate a terminal consumer failure by disposing the underlying stream:
|
||||
// the consumer loop ends without a status frame and cleans up.
|
||||
const spy = vi.spyOn(model, 'observeFailed')
|
||||
await failing.dispose()
|
||||
await tick()
|
||||
|
||||
const other = 'bash-2' as JobId
|
||||
output.observe(undefined, other)
|
||||
await ctx.fiber.dispose()
|
||||
expect(streams.every(entry => entry.stream.disposed)).toBe(true)
|
||||
expect(spy).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,298 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { Context } from '@deepseek-ai/cordis'
|
||||
import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent'
|
||||
import type { Agent } from '@deepseek-ai/dsh-agent'
|
||||
import { JobId } from '@deepseek-ai/dsh-jobs'
|
||||
import type { JobOutcome, RunningJob } from '@deepseek-ai/dsh-jobs'
|
||||
import LocalJobRegistry from '@deepseek-ai/dsh-jobs-local'
|
||||
import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session'
|
||||
import { observeJobRecord } from '../src/observe-job.ts'
|
||||
import type { ObserveJobOptions } from '../src/observe-job.ts'
|
||||
import type { SessionObserveJobFrame } from '../src/types.ts'
|
||||
import { createSessionTestController } from './test-remote.ts'
|
||||
|
||||
async function harness() {
|
||||
const ctx = new Context()
|
||||
await ctx.plugin(AgentRegistry)
|
||||
await ctx.plugin(LocalJobRegistry, { retainBytes: 64, settledRetainBytes: 64 })
|
||||
ctx.jobs.attachController('observe-job-test')
|
||||
return ctx
|
||||
}
|
||||
|
||||
function registerAgent(ctx: Context, rawId: string): Agent {
|
||||
const scopeFiber = ctx.plugin(() => {})
|
||||
const id = SessionId(rawId)
|
||||
const session = Session.create(id)
|
||||
const agent = {
|
||||
id,
|
||||
session,
|
||||
inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }),
|
||||
status: 'idle',
|
||||
ctx: scopeFiber.ctx,
|
||||
} as unknown as Agent
|
||||
ctx.agents.register(agent)
|
||||
return agent
|
||||
}
|
||||
|
||||
/** Start one record job whose done settles on demand; expose the producer face. */
|
||||
function startRecordJob(ctx: Context, options: { label?: string; owner?: Agent; record?: boolean } = {}) {
|
||||
let settle!: (outcome: JobOutcome) => void
|
||||
let face!: RunningJob
|
||||
const id = ctx.jobs.start({
|
||||
kind: 'bash',
|
||||
label: options.label ?? 'echo',
|
||||
...options.owner !== undefined ? { owner: options.owner } : {},
|
||||
...options.record === false ? {} : { record: true },
|
||||
run: (job) => {
|
||||
face = job
|
||||
return {
|
||||
cancel() {},
|
||||
done: new Promise<JobOutcome>((resolve) => { settle = resolve }),
|
||||
}
|
||||
},
|
||||
})
|
||||
return {
|
||||
id,
|
||||
append: (text: string, opts?: Parameters<RunningJob['append']>[1]) => { face.append(text, opts) },
|
||||
settle: async (outcome: JobOutcome) => {
|
||||
settle(outcome)
|
||||
await new Promise(resolve => setTimeout(resolve, 0))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** Read frames until `count` arrive or the generator finishes, then abort. */
|
||||
async function collect<Frame>(
|
||||
iterable: AsyncIterable<Frame>,
|
||||
count: number,
|
||||
abort: AbortController,
|
||||
): Promise<Frame[]> {
|
||||
const frames: Frame[] = []
|
||||
const iterator = iterable[Symbol.asyncIterator]()
|
||||
try {
|
||||
while (frames.length < count) {
|
||||
const step = await iterator.next()
|
||||
if (step.done) return frames
|
||||
frames.push(step.value)
|
||||
}
|
||||
} finally {
|
||||
abort.abort()
|
||||
await iterator.next().catch(() => undefined)
|
||||
}
|
||||
return frames
|
||||
}
|
||||
|
||||
const OBSERVE: Omit<ObserveJobOptions, 'caller'> = { flushMs: 5, maxFrameBytes: 1024 }
|
||||
|
||||
describe('observeJobRecord', () => {
|
||||
function observed(ctx: Context, request: { jobId: JobId; from?: number }, caller?: Agent) {
|
||||
const abort = new AbortController()
|
||||
const frames: SessionObserveJobFrame[] = []
|
||||
const stream = observeJobRecord(ctx.jobs, request, { ...OBSERVE, caller }, abort.signal)
|
||||
const done = (async () => {
|
||||
for await (const frame of stream) frames.push(frame)
|
||||
})()
|
||||
return { abort, frames, done }
|
||||
}
|
||||
|
||||
it('anchors, streams coalesced output, and closes with the terminal status', async () => {
|
||||
const ctx = await harness()
|
||||
const job = startRecordJob(ctx)
|
||||
job.append('early ')
|
||||
const { frames, done } = observed(ctx, { jobId: job.id })
|
||||
await new Promise(resolve => setTimeout(resolve, 20))
|
||||
job.append('live', { channel: 'stderr' })
|
||||
await new Promise(resolve => setTimeout(resolve, 20))
|
||||
await job.settle({ status: 'completed', detail: 'exit code: 0' })
|
||||
await done
|
||||
|
||||
expect(frames[0]).toMatchObject({ type: 'opened', from: 0, earliest: 0, total: 6, status: 'running' })
|
||||
const outputs = frames.filter(frame => frame.type === 'output')
|
||||
const text = outputs.flatMap(frame => frame.chunks.map(chunk => chunk.text)).join('')
|
||||
expect(text).toBe('early live')
|
||||
expect(outputs.at(-1)?.next).toBe(10)
|
||||
expect(outputs.flatMap(frame => frame.chunks).find(chunk => chunk.text === 'live')?.channel).toBe('stderr')
|
||||
expect(frames.at(-1)).toMatchObject({ type: 'status', status: 'completed', detail: 'exit code: 0' })
|
||||
})
|
||||
|
||||
it('resumes from an explicit offset and flags an evicted resume lossy', async () => {
|
||||
const ctx = await harness()
|
||||
const job = startRecordJob(ctx, { label: 'spam' })
|
||||
job.append('a'.repeat(60))
|
||||
job.append('b'.repeat(60))
|
||||
await job.settle({ status: 'completed' })
|
||||
|
||||
const fresh = observed(ctx, { jobId: job.id, from: 0 })
|
||||
await fresh.done
|
||||
const output = fresh.frames.find(frame => frame.type === 'output')
|
||||
expect(output?.lossy).toBe(true)
|
||||
expect(output?.chunks.map(chunk => chunk.text).join('')).toBe('b'.repeat(60))
|
||||
|
||||
const resumed = observed(ctx, { jobId: job.id, from: 120 })
|
||||
await resumed.done
|
||||
expect(resumed.frames.some(frame => frame.type === 'output')).toBe(false)
|
||||
expect(resumed.frames.at(-1)?.type).toBe('status')
|
||||
})
|
||||
|
||||
it('splits oversized reads along the frame budget', async () => {
|
||||
const ctx = await harness()
|
||||
const job = startRecordJob(ctx, { label: 'wide' })
|
||||
job.append('x'.repeat(30))
|
||||
job.append('y'.repeat(30))
|
||||
await job.settle({ status: 'completed' })
|
||||
const abort = new AbortController()
|
||||
const frames = await collect<SessionObserveJobFrame>(
|
||||
observeJobRecord(ctx.jobs, { jobId: job.id }, {
|
||||
flushMs: 5,
|
||||
maxFrameBytes: 30,
|
||||
caller: undefined,
|
||||
}, abort.signal),
|
||||
5,
|
||||
abort,
|
||||
)
|
||||
const outputs = frames.filter(frame => frame.type === 'output')
|
||||
expect(outputs).toHaveLength(2)
|
||||
expect(outputs[0]?.next).toBe(30)
|
||||
expect(outputs[1]?.next).toBe(60)
|
||||
})
|
||||
|
||||
it('carries the lossy flag on the first frame of a split evicted read', async () => {
|
||||
const ctx = await harness()
|
||||
const job = startRecordJob(ctx, { label: 'evicted split' })
|
||||
job.append('a'.repeat(40))
|
||||
job.append('b'.repeat(40))
|
||||
job.append('c'.repeat(40))
|
||||
await job.settle({ status: 'completed' })
|
||||
const abort = new AbortController()
|
||||
const frames = await collect<SessionObserveJobFrame>(
|
||||
observeJobRecord(ctx.jobs, { jobId: job.id, from: 0 }, {
|
||||
flushMs: 5,
|
||||
maxFrameBytes: 40,
|
||||
caller: undefined,
|
||||
}, abort.signal),
|
||||
5,
|
||||
abort,
|
||||
)
|
||||
const outputs = frames.filter(frame => frame.type === 'output')
|
||||
expect(outputs[0]?.lossy).toBe(true)
|
||||
expect(outputs[1]?.lossy).toBeUndefined()
|
||||
})
|
||||
|
||||
it('reads owned records through the resolved caller and rejects foreign, bad, and unknown input', async () => {
|
||||
const ctx = await harness()
|
||||
const owner = registerAgent(ctx, 'alice')
|
||||
const job = startRecordJob(ctx, { label: 'secret', owner })
|
||||
job.append('classified')
|
||||
await job.settle({ status: 'completed' })
|
||||
|
||||
const { frames, done } = observed(ctx, { jobId: job.id }, owner)
|
||||
await done
|
||||
expect(frames.some(frame => frame.type === 'output'
|
||||
&& frame.chunks.some(chunk => chunk.text === 'classified'))).toBe(true)
|
||||
|
||||
const foreign = new AbortController()
|
||||
await expect(collect(
|
||||
observeJobRecord(ctx.jobs, { jobId: job.id }, { ...OBSERVE, caller: undefined }, foreign.signal),
|
||||
1,
|
||||
foreign,
|
||||
)).rejects.toThrow(/belongs to another session/)
|
||||
|
||||
const abort = new AbortController()
|
||||
await expect(collect(
|
||||
observeJobRecord(ctx.jobs, { jobId: job.id, from: -1 }, { ...OBSERVE, caller: owner }, abort.signal),
|
||||
1,
|
||||
abort,
|
||||
)).rejects.toThrow(/invalid observe offset/)
|
||||
|
||||
const abortUnknown = new AbortController()
|
||||
await expect(collect(
|
||||
observeJobRecord(ctx.jobs, { jobId: JobId('bash-999') }, { ...OBSERVE, caller: undefined }, abortUnknown.signal),
|
||||
1,
|
||||
abortUnknown,
|
||||
)).rejects.toThrow(/unknown job/)
|
||||
})
|
||||
|
||||
it('refuses a job that declared no record', async () => {
|
||||
const ctx = await harness()
|
||||
const job = startRecordJob(ctx, { record: false })
|
||||
const abort = new AbortController()
|
||||
await expect(collect(
|
||||
observeJobRecord(ctx.jobs, { jobId: job.id }, { ...OBSERVE, caller: undefined }, abort.signal),
|
||||
1,
|
||||
abort,
|
||||
)).rejects.toThrow(/declared no output record/)
|
||||
await job.settle({ status: 'completed' })
|
||||
})
|
||||
|
||||
it('ignores output signals for other jobs while waiting', async () => {
|
||||
const ctx = await harness()
|
||||
const watched = startRecordJob(ctx, { label: 'watched' })
|
||||
const noisy = startRecordJob(ctx, { label: 'noisy' })
|
||||
const { abort, frames, done } = observed(ctx, { jobId: watched.id })
|
||||
await new Promise(resolve => setTimeout(resolve, 15))
|
||||
noisy.append('unrelated')
|
||||
await new Promise(resolve => setTimeout(resolve, 15))
|
||||
await watched.settle({ status: 'completed' })
|
||||
await done
|
||||
abort.abort()
|
||||
expect(frames.filter(frame => frame.type === 'output')).toEqual([])
|
||||
await noisy.settle({ status: 'completed' })
|
||||
})
|
||||
|
||||
it('stops cleanly on abort while waiting for output', async () => {
|
||||
const ctx = await harness()
|
||||
const job = startRecordJob(ctx, { label: 'idle' })
|
||||
const { abort, frames, done } = observed(ctx, { jobId: job.id })
|
||||
await new Promise(resolve => setTimeout(resolve, 20))
|
||||
abort.abort()
|
||||
await done
|
||||
expect(frames[0]?.type).toBe('opened')
|
||||
expect(frames.at(-1)?.type).not.toBe('status')
|
||||
await job.settle({ status: 'completed' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('SessionController.observeJob', () => {
|
||||
const defaults = {
|
||||
defaultModelSelection: () => ({ provider: 'fixture', model: 'fixture-model' }),
|
||||
cwd: '/tmp',
|
||||
}
|
||||
|
||||
it('refuses observation without a job registry', async () => {
|
||||
const ctx = new Context()
|
||||
await ctx.plugin(SessionStore)
|
||||
await ctx.plugin(AgentRegistry)
|
||||
const controller = createSessionTestController(ctx, defaults)
|
||||
expect(() => controller.observeJob({ jobId: JobId('bash-1') }, new AbortController().signal))
|
||||
.toThrow(/job observation unavailable/)
|
||||
})
|
||||
|
||||
it('resolves the fenced-read caller from the request session', async () => {
|
||||
const ctx = new Context()
|
||||
await ctx.plugin(SessionStore)
|
||||
await ctx.plugin(AgentRegistry)
|
||||
await ctx.plugin(LocalJobRegistry)
|
||||
ctx.jobs.attachController('observe-job-test')
|
||||
const controller = createSessionTestController(ctx, defaults)
|
||||
const session = ctx.sessions.create(SessionId('observing-session'))
|
||||
const owner = {
|
||||
id: session.id,
|
||||
session,
|
||||
inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }),
|
||||
status: 'idle',
|
||||
ctx,
|
||||
} as unknown as Agent
|
||||
ctx.agents.register(owner)
|
||||
const job = startRecordJob(ctx, { label: 'owned run', owner })
|
||||
job.append('hi')
|
||||
await job.settle({ status: 'completed' })
|
||||
|
||||
const abort = new AbortController()
|
||||
const frames = await collect<SessionObserveJobFrame>(
|
||||
controller.observeJob({ sessionId: session.id, jobId: job.id }, abort.signal),
|
||||
3,
|
||||
abort,
|
||||
)
|
||||
expect(frames.map(frame => frame.type)).toEqual(['opened', 'output', 'status'])
|
||||
})
|
||||
})
|
||||
@@ -36,6 +36,8 @@ import type {
|
||||
SessionKillJobValue,
|
||||
SessionListRequest,
|
||||
SessionListValue,
|
||||
SessionObserveJobFrame,
|
||||
SessionObserveJobRequest,
|
||||
SessionOpenWorkspacePathRequest,
|
||||
SessionOpenWorkspacePathValue,
|
||||
SessionPage,
|
||||
@@ -74,6 +76,7 @@ export interface TestSessionRemote {
|
||||
page(request: SessionPageRequest, signal?: AbortSignal): Promise<RemoteResult<SessionPage>>
|
||||
follow(request: SessionFollowRequest, signal?: AbortSignal): AsyncIterable<SessionFollowFrame>
|
||||
control(signal?: AbortSignal): AsyncIterable<SessionControlFrame>
|
||||
observeJob(request: SessionObserveJobRequest, signal?: AbortSignal): AsyncIterable<SessionObserveJobFrame>
|
||||
}
|
||||
|
||||
/** Dependencies and policy supplied by a Session Controller unit harness. */
|
||||
@@ -277,5 +280,6 @@ export function createSessionTestRemote(
|
||||
),
|
||||
follow: (request, signal = new AbortController().signal) => direct.follow(request, signal),
|
||||
control: (signal = new AbortController().signal) => direct.control(signal),
|
||||
observeJob: (request, signal = new AbortController().signal) => direct.observeJob(request, signal),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user