@hydraharness/harness-subagent 0.1.1-rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +155 -0
  3. package/lib/index.js +2743 -0
  4. package/lib/invariant.js +76 -0
  5. package/lib/types/activation-setup-registry.d.ts +57 -0
  6. package/lib/types/activation-setup-registry.js +148 -0
  7. package/lib/types/assistant-output.d.ts +48 -0
  8. package/lib/types/assistant-output.js +72 -0
  9. package/lib/types/budget.d.ts +34 -0
  10. package/lib/types/budget.js +54 -0
  11. package/lib/types/child-agent.d.ts +139 -0
  12. package/lib/types/child-agent.js +169 -0
  13. package/lib/types/client.d.ts +7 -0
  14. package/lib/types/client.js +7 -0
  15. package/lib/types/continuation.d.ts +457 -0
  16. package/lib/types/continuation.js +1157 -0
  17. package/lib/types/depth.d.ts +31 -0
  18. package/lib/types/depth.js +39 -0
  19. package/lib/types/descriptor-seed.d.ts +21 -0
  20. package/lib/types/descriptor-seed.js +24 -0
  21. package/lib/types/descriptor.d.ts +139 -0
  22. package/lib/types/descriptor.js +189 -0
  23. package/lib/types/error.d.ts +11 -0
  24. package/lib/types/error.js +14 -0
  25. package/lib/types/index.d.ts +296 -0
  26. package/lib/types/index.js +374 -0
  27. package/lib/types/invariant.d.ts +13 -0
  28. package/lib/types/invariant.js +91 -0
  29. package/lib/types/lifecycle.d.ts +113 -0
  30. package/lib/types/lifecycle.js +177 -0
  31. package/lib/types/list-children.d.ts +112 -0
  32. package/lib/types/list-children.js +316 -0
  33. package/lib/types/out-of-process.d.ts +117 -0
  34. package/lib/types/out-of-process.js +211 -0
  35. package/lib/types/projection-types.d.ts +60 -0
  36. package/lib/types/projection-types.js +7 -0
  37. package/lib/types/projection.d.ts +95 -0
  38. package/lib/types/projection.js +150 -0
  39. package/lib/types/run-settlement.d.ts +17 -0
  40. package/lib/types/run-settlement.js +66 -0
  41. package/lib/types/types.d.ts +315 -0
  42. package/lib/types/types.js +19 -0
  43. package/package.json +112 -0
@@ -0,0 +1,315 @@
1
+ /**
2
+ * The seam's consumer-facing contracts: request, result, and capability types
3
+ * for {@link SubagentProvider}, plus the `subagent/start` and `subagent/end`
4
+ * payloads that plugins and hosts observe. Internal control interfaces belong
5
+ * with their implementation — the lifecycle observer in `./lifecycle.ts`, the
6
+ * continuation host in `./continuation.ts` — so this module stays the published
7
+ * surface rather than a bag of everything type-shaped.
8
+ *
9
+ * @module @hydraharness/harness-subagent/types
10
+ */
11
+ import type { Agent, AgentOptions } from '@hydraharness/harness-agent';
12
+ import type { Branded } from '@hydraharness/harness-brand';
13
+ import type { ContentBlock } from '@hydraharness/harness-llm';
14
+ import type { SessionEvent, SessionId } from '@hydraharness/harness-session';
15
+ import type { ObjectJsonSchema, ToolRestriction } from '@hydraharness/harness-tools';
16
+ import type { SubagentDescriptorData } from './descriptor.ts';
17
+ /** Identifies one accepted subagent run across its lifecycle event pair. */
18
+ export type SubagentRunId = Branded<'SubagentRunId'>;
19
+ /**
20
+ * Brand a string as a {@link SubagentRunId}.
21
+ * @param id - the raw run id.
22
+ * @returns the same string, branded.
23
+ */
24
+ export declare function SubagentRunId(id: string): SubagentRunId;
25
+ /**
26
+ * Observe-only identifying detail for a published subagent run, carried by
27
+ * `subagent/start`. One-shot runs and continuable Activation epochs share this
28
+ * payload, so an observer sees the same vocabulary for both.
29
+ */
30
+ export interface SubagentRunInfo {
31
+ /** Unique identity shared with the paired terminal event. */
32
+ readonly runId: SubagentRunId;
33
+ /**
34
+ * Provider name recorded when the child was first created. The provider may
35
+ * be absent when an accepted one-shot run becomes ready or a persisted
36
+ * Activation cold-resumes, because neither lifecycle depends on continued
37
+ * registration.
38
+ */
39
+ readonly provider: string;
40
+ /** The child agent's id. */
41
+ readonly id: SessionId;
42
+ /** Snapshot of whether `SubagentRun.localAgent` was present when start fulfilled. */
43
+ readonly local: boolean;
44
+ }
45
+ /**
46
+ * Observe-only outcome detail for a settled subagent run, carried by
47
+ * `subagent/end` and paired with one {@link SubagentRunInfo} by `runId`.
48
+ */
49
+ export interface SubagentRunEndInfo {
50
+ /** Unique identity shared with the paired start event. */
51
+ readonly runId: SubagentRunId;
52
+ /** The same provider name carried by the paired start event. */
53
+ readonly provider: string;
54
+ /** The child agent's id. */
55
+ readonly id: SessionId;
56
+ /** Snapshot of whether `SubagentRun.localAgent` was present when start fulfilled. */
57
+ readonly local: boolean;
58
+ /** The terminal stop reason. */
59
+ readonly stopReason: SubagentResult['stopReason'];
60
+ /**
61
+ * The child's final assistant output, selected by the same rule as
62
+ * {@link SubagentResult.output}; absent on infrastructure rejection or when
63
+ * the child produced none.
64
+ */
65
+ readonly lastAssistantMessage?: ContentBlock[];
66
+ }
67
+ /**
68
+ * Which START-TIME features a provider supports. Checked by the service before delegating to
69
+ * {@link SubagentProvider.start}: a request that needs a capability the chosen provider lacks
70
+ * is rejected with a typed error rather than accepted-then-ignored (the "fail loud, no silent
71
+ * degradation" rule). These flags describe the ONE-SHOT
72
+ * {@link SubagentProvider.start} path, where the provider composes the child;
73
+ * continuable children are composed by the continuation manager itself and are
74
+ * gated by {@link SubagentProvider.prepareContinuable} instead. Each flag
75
+ * corresponds one-to-one to a {@link SubagentStartRequest} option: `depthLimit`
76
+ * to `maxDepth`; the other names match.
77
+ */
78
+ export interface SubagentCapabilities {
79
+ readonly outputSchema: boolean;
80
+ readonly depthLimit: boolean;
81
+ readonly toolFilter: boolean;
82
+ readonly persona: boolean;
83
+ }
84
+ /**
85
+ * What a caller asks for when starting a ONE-SHOT subagent. The tool layer
86
+ * builds this from the model's `{ description, prompt }` plus its own config;
87
+ * the service validates {@link SubagentCapabilities} against the named provider
88
+ * and resolves the durable descriptor before dispatching to
89
+ * {@link SubagentProvider.start}.
90
+ */
91
+ export interface SubagentStartRequest {
92
+ /** Optional short display label persisted with a session-backed child. */
93
+ readonly label?: string;
94
+ /** Content delivered as the child's user message. */
95
+ readonly prompt: ContentBlock[];
96
+ /**
97
+ * The spawning agent. In-process providers derive workspace, lineage, and
98
+ * delegation depth from its durable session state. ACP reads only its cwd,
99
+ * and only when no deployment `cwd` override is configured.
100
+ */
101
+ readonly parent: Agent;
102
+ /**
103
+ * Cancellation signal from the spawning context (the tool's `exec.signal`).
104
+ * This is the canonical cancellation channel both before and after startup:
105
+ * a provider rejects `start()` after cleaning partial resources when it
106
+ * fires before the run is published, and cancels the published run's
107
+ * remaining turn work when it fires afterward.
108
+ */
109
+ readonly signal: AbortSignal;
110
+ readonly agentOptions?: AgentOptions;
111
+ /**
112
+ * Object-rooted JSON Schema within `assertObjectJsonSchema`'s enforced subset. Start rejects
113
+ * unsupported schemas or providers without the capability. Data must be plain host-realm JSON;
114
+ * a successful child returns the matching value as {@link SubagentResult.structured}.
115
+ */
116
+ readonly outputSchema?: ObjectJsonSchema;
117
+ /**
118
+ * Optional absolute delegation-depth cap for the child being started: its
119
+ * computed depth must be less than or equal to this non-negative safe
120
+ * integer. Requires {@link SubagentCapabilities.depthLimit}; rejected at
121
+ * start otherwise.
122
+ */
123
+ readonly maxDepth?: number;
124
+ /**
125
+ * Optional child tool scoping. Requires {@link SubagentCapabilities.toolFilter};
126
+ * rejected at start otherwise. In-process backends apply it as a scoped
127
+ * `tools.restrict()` in the child's creation window: the named tools vanish
128
+ * from the child's prompt AND refuse to execute (one visibility), with loud
129
+ * unknown-name validation.
130
+ */
131
+ readonly toolFilter?: ToolRestriction;
132
+ /**
133
+ * Optional per-child persona. Requires {@link SubagentCapabilities.persona};
134
+ * rejected at start otherwise. In-process backends register it as a scoped
135
+ * `deployment:persona` section on the child, SHADOWING the deployment's
136
+ * persona for this child alone — same template semantics as the deployment
137
+ * persona (strict `{{…}}` interpolation against the registered variables).
138
+ */
139
+ readonly persona?: string;
140
+ }
141
+ /**
142
+ * Provider-facing one-shot request after {@link SubagentRuntime.start} resolves
143
+ * the durable child descriptor.
144
+ */
145
+ export interface ResolvedSubagentStartRequest extends SubagentStartRequest {
146
+ /** Detached descriptor a session-backed provider persists in the child log. */
147
+ readonly descriptor: SubagentDescriptorData;
148
+ }
149
+ /**
150
+ * What the continuation manager asks a provider for while materializing one
151
+ * continuable child's FIRST activation. The manager has already reserved the
152
+ * durable child identity and owns every later operation, so this request
153
+ * carries only what distinguishes a fresh child from one seeded with parent
154
+ * history.
155
+ */
156
+ export interface ContinuableCreateRequest {
157
+ /** The reserved durable child session id, for provider diagnostics. */
158
+ readonly sessionId: SessionId;
159
+ /** The delegating parent agent whose history a seeding provider reads. */
160
+ readonly parent: Agent;
161
+ /**
162
+ * Caller cancellation, which owns preparation only until the manager accepts
163
+ * the initial prompt into the child's inbox.
164
+ */
165
+ readonly signal: AbortSignal;
166
+ }
167
+ /**
168
+ * A provider's detached contribution to one continuable child's creation. This
169
+ * is DATA, never a capability: it carries no Agent, `AgentHandle`, prompt
170
+ * delivery, result, disposal, or resume operation, because the continuation
171
+ * manager owns the child's whole lifecycle after preparation.
172
+ */
173
+ export interface ContinuableCreateSpec {
174
+ /**
175
+ * Completed-turn prefix of the parent's log to seed the child session with,
176
+ * or absent for a fresh child. Same durable contract as
177
+ * `CreateAgentOptions.seed`: contiguous from seq 0, lossless JSON, balanced.
178
+ */
179
+ readonly seed?: readonly SessionEvent[];
180
+ }
181
+ /**
182
+ * Why a subagent run ended. Merge-extensible (a backend may add variants);
183
+ * consumers branch on the known cases and fall through `default`. The known
184
+ * cases mirror the harness turn-end vocabulary so the tool layer can map a
185
+ * non-`completed` result to an `isError` tool result.
186
+ */
187
+ export interface SubagentStopReasonMap {
188
+ /** The child finished its turn normally. */
189
+ completed: 'completed';
190
+ /** Cancelled through the request signal or disposal. */
191
+ aborted: 'aborted';
192
+ /** Model or transport failure. */
193
+ error: 'error';
194
+ /** The child hit its token ceiling before finishing. */
195
+ 'max-tokens': 'max-tokens';
196
+ /** The child declined the task. */
197
+ refusal: 'refusal';
198
+ }
199
+ /** The union over {@link SubagentStopReasonMap} — widens automatically as backends merge in variants. */
200
+ export type SubagentStopReason = SubagentStopReasonMap[keyof SubagentStopReasonMap];
201
+ /**
202
+ * The terminal outcome of a subagent run, resolved by {@link SubagentRun.result}.
203
+ */
204
+ export interface SubagentResult {
205
+ /**
206
+ * The child's final assistant output is the content of its last non-empty
207
+ * assistant message. Empty-content messages, including usage-only messages,
208
+ * are skipped. Without a non-empty message, the output is its accumulated
209
+ * assistant text stream, or `[]` when the child produced neither.
210
+ */
211
+ readonly output: ContentBlock[];
212
+ /**
213
+ * The structured result after a requested `outputSchema` was successfully
214
+ * satisfied. Requesting a schema does not guarantee presence: a provider can
215
+ * end with `stopReason: 'error'` when the child fails or finishes without a
216
+ * valid capture. The structured value is validated against the requested
217
+ * output schema by the provider; `unknown` here because the seam is
218
+ * schema-agnostic.
219
+ */
220
+ readonly structured?: unknown;
221
+ /**
222
+ * Provider-authored, non-assistant failure detail for a non-`completed`
223
+ * result. Providers keep this text free of tool inputs, file contents,
224
+ * environment values, credentials, and raw protocol payloads, and limit it
225
+ * to 4096 UTF-8 bytes. Consumers present it separately from {@link output}.
226
+ */
227
+ readonly diagnostic?: string;
228
+ /** Why the run ended. A non-`completed` reason means `output` may be partial. */
229
+ readonly stopReason: SubagentStopReason;
230
+ }
231
+ /**
232
+ * ONE-SHOT child handle returned after publication. Prompt submission, turn
233
+ * work, and infrastructure faults after that boundary belong to {@link result}.
234
+ * Consumers await that result and must always {@link dispose} to cancel
235
+ * remaining work and reach quiescence. A run is one disposable foreground
236
+ * delegation with one result; continuable conversations have no run — the
237
+ * continuation manager holds their `AgentHandle` directly and orders every
238
+ * turn through the child's own inbox.
239
+ */
240
+ export interface SubagentRun {
241
+ /**
242
+ * Parent-scoped run id. For a local run, this MUST equal the published child
243
+ * session id, whose `parentSession` records `request.parent.session.id`; a
244
+ * remote provider mints an id unique in the parent namespace.
245
+ */
246
+ readonly id: SessionId;
247
+ /**
248
+ * The exact published in-process child, or `undefined` for a remote run.
249
+ * When present, its id is {@link id}; the provider retains no ownership
250
+ * implication beyond the run's ordinary {@link dispose} contract.
251
+ */
252
+ readonly localAgent: Agent | undefined;
253
+ /**
254
+ * Resolves with the child's terminal {@link SubagentResult} when the run
255
+ * settles. Does NOT reject on a child-level failure — a model/transport
256
+ * failure resolves with `stopReason: 'error'` so the consumer maps it to an
257
+ * `isError` tool result. Rejects on an infrastructure fault the seam cannot
258
+ * represent as a stop reason.
259
+ */
260
+ readonly result: Promise<SubagentResult>;
261
+ /**
262
+ * Cancel remaining work, reach child quiescence, and release resources.
263
+ * Idempotent.
264
+ */
265
+ dispose(): Promise<void>;
266
+ }
267
+ /**
268
+ * One registered transport for running child agents. Providers are trusted
269
+ * same-process implementations; callers treat descriptors and returned values
270
+ * as borrowed immutable data. The service may call one provider concurrently
271
+ * for distinct children. Providers isolate operation-local mutable state; a
272
+ * shared capacity controller may delay an operation but must not couple its
273
+ * settlement or cleanup to a sibling.
274
+ */
275
+ export interface SubagentProvider {
276
+ /** Unique registry name (e.g. `spawn`, `fork`, `acp`). */
277
+ readonly name: string;
278
+ /** The start-time features this provider supports (see {@link SubagentCapabilities}). */
279
+ readonly capabilities: SubagentCapabilities;
280
+ /**
281
+ * Whether the child sees the parent's completed-turn prefix. This is descriptive, not a
282
+ * service-validated start capability: the model-facing tool derives truthful wording from it.
283
+ * It says nothing about tool registration, injected services, or authority inheritance.
284
+ */
285
+ readonly inheritsParentContext: boolean;
286
+ /**
287
+ * Establish a ONE-SHOT child and return its handle after publication.
288
+ * The service has already validated that every requested start-time
289
+ * capability is supported and resolved `request.descriptor`, so a
290
+ * session-backed implementation appends that descriptor inside the child's
291
+ * initial turn. Before fulfillment, the provider owns setup and cleans any
292
+ * unpublished partial resources before rejecting. Ownership transfers on
293
+ * fulfillment; subsequent turn or infrastructure failure settles through
294
+ * the returned run. Distinct starts may overlap; cancellation, failure,
295
+ * result settlement, and disposal remain independent for each run.
296
+ */
297
+ start(request: ResolvedSubagentStartRequest): Promise<SubagentRun>;
298
+ /**
299
+ * OPTIONAL (continuable-creation capability): contribute the detached
300
+ * creation inputs that distinguish this provider's continuable children —
301
+ * only whether the child session is seeded with parent history. Method
302
+ * presence IS the capability: the service rejects continuable starts on
303
+ * providers without it, while a provider that has it may still serve
304
+ * ordinary one-shot delegations.
305
+ *
306
+ * This is the provider's ONLY participation in a continuable child. The
307
+ * continuation manager owns identity reservation, composition, Agent
308
+ * creation, prompt delivery, cold resume, ownership, and disposal, so a
309
+ * provider never sees the child's Agent, handle, turns, or teardown.
310
+ * Distinct preparations may overlap; each follows its own signal and returns
311
+ * data belonging only to `request.sessionId`.
312
+ */
313
+ prepareContinuable?(request: ContinuableCreateRequest): Promise<ContinuableCreateSpec>;
314
+ }
315
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The seam's consumer-facing contracts: request, result, and capability types
3
+ * for {@link SubagentProvider}, plus the `subagent/start` and `subagent/end`
4
+ * payloads that plugins and hosts observe. Internal control interfaces belong
5
+ * with their implementation — the lifecycle observer in `./lifecycle.ts`, the
6
+ * continuation host in `./continuation.ts` — so this module stays the published
7
+ * surface rather than a bag of everything type-shaped.
8
+ *
9
+ * @module @hydraharness/harness-subagent/types
10
+ */
11
+ /**
12
+ * Brand a string as a {@link SubagentRunId}.
13
+ * @param id - the raw run id.
14
+ * @returns the same string, branded.
15
+ */
16
+ export function SubagentRunId(id) {
17
+ return id;
18
+ }
19
+ //# sourceMappingURL=types.js.map
package/package.json ADDED
@@ -0,0 +1,112 @@
1
+ {
2
+ "name": "@hydraharness/harness-subagent",
3
+ "description": "Abstract subagent seam (ctx.subagents): named-provider registry for delegating to child agents",
4
+ "hydra": {
5
+ "plugin": {
6
+ "application": "Delegate work to registered child-agent providers and track their continuations."
7
+ }
8
+ },
9
+ "version": "0.1.1-rc.5",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/MaiHongPhong1902/Hydra-Harness.git",
16
+ "directory": "packages/subagent/subagent"
17
+ },
18
+ "type": "module",
19
+ "main": "lib/index.js",
20
+ "types": "lib/types/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./lib/types/index.d.ts",
24
+ "default": "./lib/index.js"
25
+ },
26
+ "./invariant": {
27
+ "types": "./lib/types/invariant.d.ts",
28
+ "default": "./lib/invariant.js"
29
+ },
30
+ "./client": {
31
+ "types": "./lib/types/client.d.ts",
32
+ "default": "./lib/types/client.js"
33
+ },
34
+ "./src/*": "./src/*",
35
+ "./package.json": "./package.json"
36
+ },
37
+ "files": [
38
+ "lib/index.js",
39
+ "lib/invariant.js",
40
+ "lib/types/**/*.js",
41
+ "lib/types/**/*.d.ts"
42
+ ],
43
+ "license": "MIT",
44
+ "dependencies": {
45
+ "zod": "^4.4.3",
46
+ "@hydraharness/schemastery": "^3.18.2"
47
+ },
48
+ "peerDependencies": {
49
+ "@hydraharness/harness-agent": "^0.1.1-rc.5",
50
+ "@hydraharness/harness-brand": "^0.1.1-rc.5",
51
+ "@hydraharness/harness-agent-presets": "^0.1.1-rc.5",
52
+ "@hydraharness/harness-invariants": "^0.1.1-rc.5",
53
+ "@hydraharness/harness-llm": "^0.1.1-rc.5",
54
+ "@hydraharness/harness-sandbox-policy": "^0.1.1-rc.5",
55
+ "@hydraharness/harness-scope": "^0.1.1-rc.5",
56
+ "@hydraharness/harness-session": "^0.1.1-rc.5",
57
+ "@hydraharness/harness-session-persistence": "^0.1.1-rc.5",
58
+ "@hydraharness/harness-session-projection": "^0.1.1-rc.5",
59
+ "@hydraharness/harness-session-projection-cache": "^0.1.1-rc.5",
60
+ "@hydraharness/harness-tools": "^0.1.1-rc.5",
61
+ "@hydraharness/harness-jobs": "^0.1.1-rc.5",
62
+ "@hydraharness/harness-sandbox": "^0.1.1-rc.5",
63
+ "@hydraharness/cordis": "^4.0.2",
64
+ "@hydraharness/harness-user-approval": "^0.1.1-rc.5"
65
+ },
66
+ "peerDependenciesMeta": {
67
+ "@hydraharness/harness-agent-presets": {
68
+ "optional": true
69
+ },
70
+ "@hydraharness/harness-sandbox": {
71
+ "optional": true
72
+ },
73
+ "@hydraharness/harness-sandbox-policy": {
74
+ "optional": true
75
+ },
76
+ "@hydraharness/harness-session-persistence": {
77
+ "optional": true
78
+ },
79
+ "@hydraharness/harness-session-projection": {
80
+ "optional": true
81
+ },
82
+ "@hydraharness/harness-session-projection-cache": {
83
+ "optional": true
84
+ },
85
+ "@hydraharness/harness-jobs": {
86
+ "optional": true
87
+ },
88
+ "@hydraharness/harness-user-approval": {
89
+ "optional": true
90
+ }
91
+ },
92
+ "devDependencies": {
93
+ "@hydraharness/harness-agent": "^0.1.1-rc.5",
94
+ "@hydraharness/harness-llm": "^0.1.1-rc.5",
95
+ "@hydraharness/harness-brand": "^0.1.1-rc.5",
96
+ "@hydraharness/harness-invariants": "^0.1.1-rc.5",
97
+ "@hydraharness/harness-agent-presets": "^0.1.1-rc.5",
98
+ "@hydraharness/harness-sandbox": "^0.1.1-rc.5",
99
+ "@hydraharness/harness-sandbox-policy": "^0.1.1-rc.5",
100
+ "@hydraharness/harness-scope": "^0.1.1-rc.5",
101
+ "@hydraharness/harness-session": "^0.1.1-rc.5",
102
+ "@hydraharness/harness-session-persistence": "^0.1.1-rc.5",
103
+ "@hydraharness/harness-session-projection-cache": "^0.1.1-rc.5",
104
+ "@hydraharness/harness-storage": "^0.1.1-rc.5",
105
+ "@hydraharness/harness-storage-domain": "^0.1.1-rc.5",
106
+ "@hydraharness/harness-jobs": "^0.1.1-rc.5",
107
+ "@hydraharness/harness-session-projection": "^0.1.1-rc.5",
108
+ "@hydraharness/harness-tools": "^0.1.1-rc.5",
109
+ "@hydraharness/harness-user-approval": "^0.1.1-rc.5",
110
+ "@hydraharness/cordis": "^4.0.2"
111
+ }
112
+ }