Files
deepseek-harness/packages/subagent/tool-subagent-control/src/index.ts
T
Dudu-0223 853f4d5cfb refactor(subagent): drop host-user authority and split lifecycle publication
Remove the host-user continuation capability and the public residency query,
then separate the seam's public event payloads from its internal lifecycle
control interfaces.

`followup()` now takes the exact live direct parent `Agent` instead of a
`SubagentAuthority` union. No production adapter ever supplied user authority,
so the `UserAuthorityGrant` brand token existed only to stop a forged
discriminant from bypassing the direct-parent check — deleting the branch
retires the token, its mint method, and that attack surface together.

Narrowing `parent` from `Agent | undefined` to `Agent` removes three special
cases, including the path where a parentless epoch dispatched its lifecycle
events unscoped. Scoped-versus-global dispatch is now decided by the event, not
by whether a caller happened to have a parent.

`activationState()` had no caller; `ActivationState`, `ActivationObserver`, and
`ContinuationHost` are package-private.

New `src/lifecycle.ts` owns the contained emitter, the one-shot run observer,
and the Activation observer, while `SubagentRunInfo`/`SubagentRunEndInfo` move
to `src/types.ts` beside the other consumer-facing contracts. Those payloads are
public API — dsh-jsonrpc, hooks-claude, and the package invariant all consume
them — whereas the observer is a contract between two in-package collaborators,
so they no longer share a home merely for both being lifecycle-shaped. The
service keeps ownership of the scope carrier: `scopeTarget()` composes the
service's own context filter, so a narrowed stand-in would silently change
scope filtering.

Also drops now-unused dsh-tasks-local and dsh-tool-tasks dev dependencies, and
corrects the README claim that a pre-residency failure emits a terminal edge —
that path only ever rethrew.
2026-08-02 12:51:08 +08:00

76 lines
2.7 KiB
TypeScript

/**
* The globally named `send_message` tool: a thin model-facing adapter over
* `ctx.subagents.followup()`. It performs no lifecycle routing of its own —
* residency and cold resume belong to the subagent service — and it lives apart
* from the provider-bound `@deepseek-ai/dsh-tool-subagent` instances so multiple
* delegation tools share one control tool.
* @module @deepseek-ai/dsh-tool-subagent-control
*/
import type { Context } from 'cordis'
import { defineTool } from '@deepseek-ai/dsh-tools'
import type { ContentBlock } from '@deepseek-ai/dsh-llm'
import { SessionId } from '@deepseek-ai/dsh-session'
import type {} from '@deepseek-ai/dsh-subagent'
export const name = 'tool-subagent-control'
export const inject = ['tools', 'subagents']
/**
* Register the `send_message` tool.
* @param ctx - context carrying the tool registry and subagent service.
*/
export function apply(ctx: Context): void {
ctx.tools.register(defineTool({
name: 'send_message',
description:
'Send a message to a background subagent by its subagent id, continuing the same conversation. It '
+ 'becomes the subagent\'s next turn: if it is still working, the message waits until its current turn '
+ 'finishes, so it cannot redirect work already underway. The subagent does not reply to you, so use '
+ 'this only to give it more work. A failure means the message was NOT delivered.',
parameters: {
subagent_id: {
type: 'string',
required: true,
description: 'The subagent id returned when the background subagent was started.',
},
message: {
type: 'string',
required: true,
description: 'The message to deliver to the subagent.',
},
},
output: {
schema: {
type: 'object',
additionalProperties: false,
properties: {
messageId: { type: 'string', required: true },
},
},
render: (args, _value) => [{
type: 'text',
text: `message queued as the next turn for subagent ${args.subagent_id}`,
}],
},
async execute(args, exec) {
const parent = exec.agent
if (!parent) {
// Parent authority requires an exact live calling agent.
throw new Error('send_message requires a calling agent (exec.agent was undefined)')
}
const message: ContentBlock[] = [{ type: 'text', text: args.message }]
const messageId = await ctx.subagents.followup(
parent,
SessionId(args.subagent_id),
message,
{
source: { kind: 'coordinator', senderSessionId: parent.id },
signal: exec.signal,
},
)
return { messageId }
},
}))
}