feat(tasks): background task runtime, generic task_* control tools, bash/subagent producers

One shared ctx.tasks registry (branded <kind>-N ids, owner-fenced
read/kill/wait/list, attachSurface misconfiguration fence, reported-flag
notice dedup, atomic register) + dsh-tool-tasks (task_output/task_list/
task_kill, completion-notice injection, background prompt habit).
Producers opt in via their own enableRunInBackground config: bash
(stream kind; seam slimmed to resolve/run/start returning a BashProcess
handle, bash_output/bash_kill deleted) and subagent (final-output kind;
done settles after run.dispose()). Owner disposal drains tasks through
the new awaited ctx.agents.onCleanup seam in the loop's disposal chain.
Both RFCs moved to implemented/; docs, catalogs, snapshots re-pinned.
This commit is contained in:
Yichen Jiang
2026-07-09 21:22:54 +08:00
parent e7e382f9d1
commit 184e164091
83 changed files with 3909 additions and 1627 deletions
+124 -1
View File
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { Context } from 'cordis'
import { Session, SessionId } from '@deepseek-ai/dsh-session'
import AgentRegistry, { Agent, AgentId } from '@deepseek-ai/dsh-agent'
@@ -76,6 +76,129 @@ describe('AgentRegistry', () => {
})
})
describe('AgentRegistry.onCleanup / drainCleanups', () => {
it('drains cleanups in registration order, awaiting each', async () => {
const ctx = new Context()
await ctx.plugin(AgentRegistry)
const agent = stubAgent('a1')
ctx.agents.register(agent)
const ran: string[] = []
ctx.agents.onCleanup(agent.id, async () => {
ran.push('first:start')
await new Promise(r => setTimeout(r, 10))
ran.push('first:end')
})
ctx.agents.onCleanup(agent.id, () => {
ran.push('second')
return Promise.resolve()
})
await ctx.agents.drainCleanups(agent.id)
// Sequential await: the second cleanup starts only after the first settled.
expect(ran).toEqual(['first:start', 'first:end', 'second'])
// Drained cleanups are detached: a second drain is a no-op.
await ctx.agents.drainCleanups(agent.id)
expect(ran).toEqual(['first:start', 'first:end', 'second'])
})
it('contains a rejecting cleanup: logged, later cleanups still run', async () => {
const ctx = new Context()
await ctx.plugin(AgentRegistry)
const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => {})
const agent = stubAgent('a1')
ctx.agents.register(agent)
let ranAfter = false
ctx.agents.onCleanup(agent.id, () => Promise.reject(new Error('cleanup boom')))
ctx.agents.onCleanup(agent.id, () => {
ranAfter = true
return Promise.resolve()
})
await expect(ctx.agents.drainCleanups(agent.id)).resolves.toBeUndefined()
expect(ranAfter).toBe(true)
expect(warn).toHaveBeenCalledWith(expect.stringContaining('cleanup boom'))
})
it('rejects a cleanup for an agent that is not registered', async () => {
const ctx = new Context()
await ctx.plugin(AgentRegistry)
expect(() => ctx.agents.onCleanup(AgentId('ghost'), () => Promise.resolve()))
.toThrow('agent "ghost" is not registered')
})
it('detaches without running on disposer call and on fiber dispose (HMR safety)', async () => {
const ctx = new Context()
await ctx.plugin(AgentRegistry)
const agent = stubAgent('a1')
ctx.agents.register(agent)
let ranA = false
let ranB = false
const detach = ctx.agents.onCleanup(agent.id, () => {
ranA = true
return Promise.resolve()
})
detach()
const fiber = await ctx.plugin(Object.assign((inner: Context) => {
inner.agents.onCleanup(agent.id, () => {
ranB = true
return Promise.resolve()
})
}, { inject: ['agents'] }))
await fiber.dispose()
await ctx.agents.drainCleanups(agent.id)
expect(ranA).toBe(false)
expect(ranB).toBe(false)
})
it('runs a cleanup registered during the drain instead of leaking it', async () => {
const ctx = new Context()
await ctx.plugin(AgentRegistry)
const agent = stubAgent('a1')
ctx.agents.register(agent)
const ran: string[] = []
ctx.agents.onCleanup(agent.id, () => {
ran.push('outer')
// A settling task registering follow-up cleanup mid-drain: the drain
// loop must pick up the fresh set rather than strand it.
ctx.agents.onCleanup(agent.id, () => {
ran.push('mid-drain')
return Promise.resolve()
})
return Promise.resolve()
})
await ctx.agents.drainCleanups(agent.id)
expect(ran).toEqual(['outer', 'mid-drain'])
})
it('a stale disposer from a drained set does not remove a fresh registration', async () => {
const ctx = new Context()
await ctx.plugin(AgentRegistry)
const agent = stubAgent('a1')
ctx.agents.register(agent)
const detachOld = ctx.agents.onCleanup(agent.id, () => Promise.resolve())
await ctx.agents.drainCleanups(agent.id)
let ranFresh = false
ctx.agents.onCleanup(agent.id, () => {
ranFresh = true
return Promise.resolve()
})
// The old registration's disposer fires after its set was drained; the
// identity guard must keep it away from the fresh set under the same id.
detachOld()
await ctx.agents.drainCleanups(agent.id)
expect(ranFresh).toBe(true)
})
})
describe('AgentRegistry factory seam', () => {
/** A stub AgentFactory that records calls and returns a stub agent. */
function stubFactory() {