1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! `task.run` JSON-RPC method (#2056, vision spec §4.3): start a
//! session-driven task execution.
//!
//! Why: this is the API surface the M1 control-plane cut line needs — a
//! caller creates (or targets an existing) session and asks the daemon to
//! actually EXECUTE a task against it, asynchronously, streaming progress via
//! the #2054/#2055 `session.attach` path. Kept as its own JSON-RPC method
//! (rather than overloading `session.send`) because `session.send`'s
//! existing #2054/#2055 contract — record an observable `SessionInput` event
//! — is already tested and shipped; `task.run` is the deliberate, explicit
//! "start executing" entry point the issue names.
//! What: [`register`] wires `task.run` onto a [`Router`], sharing the SAME
//! `Arc<SessionRegistry>` every `session.*` method uses. Params match the
//! vision spec §4.3 example almost verbatim: `task_description`,
//! `agent_name` (top-level/PM agent, default `"pm"`), `context` (accepted for
//! forward-compatibility; not yet injected into the prompt — project
//! `CLAUDE.md` context already flows through `run_task`'s existing
//! machinery, and free-form per-call `context` injection is a smaller
//! follow-up, not core to this cut line), `model_override` (pins the
//! delegated ENGINEER's model for this run), and an ADDITIONAL optional
//! `session_id` for "sessionful" execution against an already-`session.create`d
//! session (the spec's "single-shot or sessionful" phrasing).
//! Test: `task::protocol::tests::*`; the full flow end-to-end (a real
//! subprocess) in `tests/task_e2e.rs`.
use PathBuf;
use Arc;
use Deserialize;
use ;
use crateProjectBinding;
use crate;
use crateSessionRegistry;
use crateSharedWorkstreamStore;
use ;
use build_llm_client;
/// Default top-level/PM agent name used when `task.run`'s `agent_name` param
/// is omitted (#3437).
///
/// Why: #3437 — every GUI-initiated run omits `agent_name` (no agent-roster
/// endpoint exists yet for the GUI to pick from), so `task_run`'s default
/// MUST resolve against a real agent in every resolution tier (disk then
/// embedded, see `agents::resolve_agent`) or 100% of daemon-default runs fail
/// agent resolution before a single turn executes — exactly the failure mode
/// #3437 traced. A shared `pub` const (referenced from both this call site
/// and `assets::tests::default_task_run_agent_resolves_against_default_agents`)
/// is the drift-guard: the literal here and the embedded roster entry it must
/// resolve against can never independently rot out of sync the way two
/// separately-typed `"pm"` string literals could.
/// What: `"pm"` — must have a matching `EmbeddedAgent` entry in
/// [`crate::assets::DEFAULT_AGENTS`] (see `assets/agents/pm.md`).
/// Test: `task::protocol::tests::task_run_creates_session_when_none_given`
/// (exercises the default through a real `task_run` call);
/// `assets::tests::default_task_run_agent_resolves_against_default_agents`
/// (pins that the literal resolves against the embedded roster).
pub const DEFAULT_TASK_RUN_AGENT_NAME: &str = "pm";
/// Register `task.run` onto `router`.
///
/// Why: the one place that wires the method, mirroring
/// `session::protocol::register`'s role for `session.*`.
/// What: closes over `registry`, `binding`, and `agents_dir` — all shared,
/// cheap to clone (`Arc`/`PathBuf`) per call. `binding` replaces what was a
/// REQUIRED `project: PathBuf`: a `PathBuf` cannot express "no project", so the
/// projectless state the shell's entry screen renders was unreachable — the
/// daemon could not be started, let alone run a task, without one. It is now a
/// [`ProjectBinding`], whose `None` variant is that state.
/// Test: `task::protocol::tests::register_wires_task_run`,
/// `task::protocol::tests::register_wires_task_run_projectless`.
/// (Issue #3298) `workstreams` is the daemon's shared workstream store —
/// `task.run` resolves and persists a freshly-minted session's binding
/// (explicit `workstream_id` param, or DOC-48 §4.2's ambient
/// active-workstream default) through it, or enforces §4.1's immutability
/// rule against an EXISTING session's persisted binding — see
/// `task_run`'s docs.
/// [`register`] with the daemon's permission broker (#7948).
///
/// Why: `crate::serve::build_router` owns the one broker
/// `session.permission.respond` answers into; every other caller (tests)
/// keeps [`register`], whose runs are headless.
/// What: `permissions: None` makes every `task.run` headless — an `ask`
/// resolves at once per `TCODE_PERMISSION_MODE`. The mode is read from the
/// daemon's environment, never from the request: the caller asking to run a
/// task must not be able to widen its own permissions.
/// Test: `serve::tests::build_router_wires_task_run` (through `build_router`),
/// `permissions::tests::protocol_tests::respond_resolves_a_pending_request`.
/// `task.run` request params (vision spec §4.3).
/// `task.run(task_description, agent_name?, context?, model_override?,
/// session_id?, mode?, deadline_secs?, project?, workstream_id?,
/// no_delegate?, pm_model?, max_turns?) -> { session_id, status, mode }`.
///
/// Why: the single entry point that turns a request into a running
/// background execution.
///
/// **(Issue #3298, DOC-48 §4.1/§4.2) Workstream binding**, mirroring the
/// `project`/binding reconciliation immediately below: on the mint path
/// (`session_id` absent), resolves the EFFECTIVE workstream target —
/// `workstream_id` explicit param wins, else the daemon's active workstream
/// is the ambient default (§4.2), else the session stays projectless — and
/// persists it via `crate::workstreams::protocol::resolve_validate_bind`
/// (validated BEFORE the session is minted — see that helper's docs on the
/// PR #3354 phantom-session fix)
/// before this session is used further. On the reuse path (`session_id`
/// present), the session's own persisted `workstream_id` is authoritative;
/// `crate::workstreams::protocol::check_workstream_immutable` rejects a
/// `workstream_id` param that would silently redirect a session
/// `session.status`/`session.list` already reports as bound to a different
/// (or no) workstream — same rule, same rationale as the `project` mismatch
/// guard.
/// What: validates `task_description` is non-empty
/// (`-32003 invalid_argument`); resolves the EFFECTIVE binding for this call —
/// `project: Some(path)` resolves via [`ProjectBinding::resolve`] (mapping a
/// `BindingError` onto `-32003 invalid_argument`, exactly like
/// `session.create`); `project: None` keeps the process-boot-time `binding`
/// `register` closed over, so an existing caller that never sends `project`
/// observes no change (#3178 back-compat). Then resolves the target session —
/// an existing one by `session_id` (propagating `session_not_found` if it
/// doesn't exist) or a freshly `session.create`d one bound to the effective
/// binding; resolves the effective `HarnessMode` via `crate::mode::resolve_mode`
/// (#2059's three-tier precedence, rooted at the effective binding) and
/// persists it onto the session (`SessionRegistry::set_mode`) so it is
/// queryable afterward via `session.status`/`session.list` (`Session.mode`)
/// and `session.get_transcript` (`TranscriptRecord.mode`); builds the shared
/// LLM client (real or the #2056 offline mock, per `TCODE_MOCK_LLM`); and
/// calls `spawn_task_run`, which reserves the execution slot synchronously
/// (rejecting a second overlapping run) before handing off to the background
/// task. Returns immediately — the caller `session.attach`es to observe
/// progress, per the ticket's "must not block the request thread on the whole
/// LLM run" requirement. The response's own `mode`/`binding` fields are the
/// SAME resolved values, surfaced immediately rather than requiring a
/// follow-up `session.status` call.
///
/// **Invariant: a reused session's persisted binding is authoritative.** When
/// `session_id` is supplied, `project` may only RESTATE that session's own
/// `Session.binding` root (or be omitted); a `project` naming a DIFFERENT
/// root is rejected as `-32003 invalid_argument` rather than silently
/// executing the run against a project the session itself was never bound to
/// (`SessionRegistry` has no binding-update path — `create` sets it once).
/// Accepting the mismatch would let `session.status`/`session.list` report
/// the ORIGINAL binding forever while the run actually executed against a
/// different one — a silent audit/state divergence this validation exists to
/// prevent (code-critic HIGH finding, PR #3189).
///
/// **(#8184) Invariant: a reused session's agent SHAPE is authoritative too.**
/// A session minted solo (`session.create`'s default — see
/// `crate::session::protocol::create`) runs solo on every turn; a session
/// minted delegating rejects a `no_delegate: true` call with
/// `-32003 invalid_argument` rather than running a shape
/// `session.status`/`session.list` would keep reporting as the other one.
/// Same rule, same rationale as the `project` and `workstream_id` guards. The
/// mint path persists this call's own `no_delegate` onto the session it
/// creates, so the run and the record always agree.
///
/// The guard is ONE-DIRECTIONAL by construction: `no_delegate` is a plain
/// `bool` with a serde default, so an omitted field and an explicit `false`
/// are the same value on the wire and a request cannot express "I want
/// delegation" for the daemon to reject. A solo session therefore runs solo
/// for a caller that asked for either — which is the shape it reports.
/// Test: `task::protocol::tests::task_run_rejects_empty_task_description`,
/// `task::protocol::tests::task_run_creates_session_when_none_given`,
/// `task::protocol::tests::task_run_sessionful_reuses_existing_session`,
/// `task::protocol::tests::task_run_unknown_session_id_errors`,
/// `task::protocol::tests::task_run_resolves_and_reports_mode`,
/// `task::protocol::tests::task_run_without_project_keeps_boot_binding`,
/// `task::protocol::tests::task_run_with_project_overrides_boot_binding`,
/// `task::protocol::tests::task_run_rejects_invalid_project`,
/// `task::protocol::tests::task_run_session_id_with_matching_project_succeeds`,
/// `task::protocol::tests::task_run_session_id_with_mismatched_project_is_rejected`,
/// `task::protocol::tests::task_run_on_a_default_session_never_delegates`,
/// `task::protocol::tests::task_run_on_a_delegating_session_still_delegates`,
/// `task::protocol::tests::task_run_minted_session_records_its_no_delegate`,
/// `task::protocol::tests::task_run_no_delegate_against_a_delegating_session_is_rejected`.
// #7948: production registers through `register_with_permissions`; this
// headless five-argument form is what the unit tests drive.
async
/// [`task_run`] with the daemon's permission broker (#7948); `None` runs
/// headless. Called by [`register_with_permissions`].
/// Test: `task::protocol::tests::*` (through [`task_run`]).
async