import { p as parseJsonBody, i as isParseError, e as err, g as gatewayClient, a as ok, b as isGatewayUnreachable } from './_dispatch-ncuRztUQ.js';
import { json } from '@sveltejs/kit';

//#region src/routes/api/tasks/[id]/dispatch/+server.ts
/**
* POST /api/tasks/[id]/dispatch — Conductor "Dispatch" action for the
* interactive dispatcher board (T11930 · M5).
*
* Dispatching a Backlog/Ready card SPAWNS a real worker in a git worktree via
* `orchestrate.spawn`. Spawning is real, expensive, and provisions filesystem +
* worktree state, so this is GATEWAY-ONLY and SERVER-SIDE:
*
*  - **Server-side only** — the spawn never runs from the raw browser client.
*    Secrets, worktree creation, and the orchestrate surface stay behind this
*    route (the request only carries the task id + tier).
*  - **Gateway-only (no in-process fallback)** — the spawn routes through the
*    `/v1` gateway SDK client into the daemon's orchestrate surface. When the
*    daemon listener is down we return a typed `E_GATEWAY_UNREACHABLE` so the UI
*    tells the operator to start `cleo daemon serve` rather than silently
*    spawning a worktree from a web request.
*
* On success the task transitions to the Running lane (the spawn marks it
* active / claimed) and the live worker stream (T11929) attaches to the card.
*
* @task T11930
* @epic T11559
*/
var POST = async ({ params, request }) => {
	const taskId = params.id;
	const body = await parseJsonBody(request);
	if (isParseError(body)) return json(err("E_VALIDATION", body._parseError), { status: 400 });
	const rawTier = body.tier;
	const tier = rawTier === 0 || rawTier === 2 ? rawTier : 1;
	try {
		const spawn = gatewayClient().orchestrate.spawn;
		const envelope = (await spawn({ body: {
			taskId,
			tier
		} })).data;
		if (envelope && envelope.success === false) return json(err("E_SPAWN_REJECTED", envelope.error?.message ?? "Spawn rejected"), { status: 409 });
		return json(ok({
			taskId,
			tier,
			spawn: envelope?.data ?? {}
		}));
	} catch (e) {
		if (isGatewayUnreachable(e)) return json(err("E_GATEWAY_UNREACHABLE", "The CLEO daemon is not running. Start it with `cleo daemon serve` to dispatch a worker."), { status: 503 });
		return json(err("E_SPAWN_ERROR", e instanceof Error ? e.message : "Spawn failed"), { status: 502 });
	}
};

export { POST };
//# sourceMappingURL=_server.ts-Bw7A4zgb.js.map
