import { B as AGENT_LIFECYCLE_LANES } from './tasks-CnwUAUXf.js';
import { u as updateTask } from './update-Dj_q4-Re.js';
import { e as err, p as parseJsonBody, i as isParseError, r as requireString, g as gatewayClient, a as ok, b as isGatewayUnreachable } from './_dispatch-ncuRztUQ.js';
import { p as planLaneTransition } from './lane-transition-lM2ngbbc.js';
import { json } from '@sveltejs/kit';
import './paths-2M_UxD_H.js';
import 'node:fs';
import 'node:path';
import 'node:os';
import 'node:events';
import 'node:diagnostics_channel';
import 'fs';
import 'events';
import 'util';
import 'path';
import 'assert';
import 'worker_threads';
import 'module';
import 'node:url';
import 'url';
import 'buffer';
import 'node:module';
import 'node:child_process';
import 'node:crypto';
import 'node:process';
import 'node:async_hooks';
import './data-accessor-CVbz8X_7.js';
import './conduit-Ca_OFWR_.js';
import './indexes-aYlULWG3.js';
import './utils-BvHPJOVk.js';
import './unique-constraint-BM-Apx1H.js';
import './chunk-DDP5xfzh.js';
import './sqlite-Dsbe0wec.js';
import './dual-scope-db-ATrwq0LH.js';
import './memory-schema-Bi7cChwo.js';
import './agent-registry-BtE5uhMO.js';
import './nexus-sqlite2-DgTuv9BQ.js';
import './nexus-schema2-BEd-ol6e.js';
import './logger-ChCwlV5k.js';
import 'node:sqlite';
import './lock-BoHF25mx.js';
import './index-IVoIz4D5.js';
import 'constants';
import 'stream';
import 'node:fs/promises';
import 'jsonc-parser';
import '@cleocode/cant';
import 'node:util';
import 'node:buffer';
import 'child_process';
import './branch-lock-qP1EkZ7p.js';
import './src2-DK8DKufK.js';
import 'llmtxt/identity';
import './sessions-j73dkbCI.js';
import './registry2-BSyCTA3q.js';
import './index-Ca4C2M45.js';
import './mvi-helpers-BNsJ8ZpQ.js';
import './session-store-OwQWMSf_.js';
import './session-manifest-ZHrj54LR.js';
import './storage-B9UA3BHR.js';
import './config-B0nEjgSh.js';
import './json-DXl7iDYg.js';
import './data-safety-central-vmfu_Y4J.js';
import './sqlite-data-accessor-C59wZ0zp.js';
import './cross-db-cleanup-C_FLyd-6.js';
import './typed-query-l1D_QElt.js';

//#region src/routes/api/tasks/[id]/transition/+server.ts
/** Narrow a string to a known lane id. */
function asLane(v) {
	return AGENT_LIFECYCLE_LANES.includes(v) ? v : null;
}
var POST = async ({ locals, params, request }) => {
	const ctx = locals.projectCtx;
	if (!ctx.tasksDbExists) return json(err("E_DB_UNAVAILABLE", "tasks.db unavailable"), { status: 503 });
	const taskId = params.id;
	const body = await parseJsonBody(request);
	if (isParseError(body)) return json(err("E_VALIDATION", body._parseError), { status: 400 });
	const fromR = requireString(body, "fromLane", 32);
	if (!fromR.ok) return json(err("E_VALIDATION", fromR.message), { status: 400 });
	const toR = requireString(body, "toLane", 32);
	if (!toR.ok) return json(err("E_VALIDATION", toR.message), { status: 400 });
	const fromLane = asLane(fromR.value);
	const toLane = asLane(toR.value);
	if (!fromLane || !toLane) return json(err("E_VALIDATION", "fromLane / toLane must be valid lane ids"), { status: 400 });
	const planned = planLaneTransition(taskId, fromLane, toLane);
	if (!planned.ok) return json(err(`E_INVALID_TRANSITION:${planned.reason}`, planned.message), { status: 422 });
	const { status, summary } = planned.plan;
	try {
		const envelope = (await gatewayClient().tasks.update({ body: {
			taskId,
			status
		} })).data;
		if (envelope && envelope.success === false) return json(err("E_GATEWAY_REJECTED", envelope.error?.message ?? "Gateway rejected the update"), { status: 409 });
		return json(ok({
			taskId,
			status,
			fromLane,
			toLane,
			via: "gateway",
			summary
		}));
	} catch (gatewayErr) {
		if (!isGatewayUnreachable(gatewayErr)) return json(err("E_GATEWAY_ERROR", gatewayErr instanceof Error ? gatewayErr.message : "Gateway update failed"), { status: 502 });
	}
	try {
		await updateTask({
			taskId,
			status
		}, ctx.projectPath);
		return json(ok({
			taskId,
			status,
			fromLane,
			toLane,
			via: "core",
			summary
		}));
	} catch (coreErr) {
		const e = coreErr;
		if (e?.code === 4) return json(err("E_NOT_FOUND", `Task not found: ${taskId}`), { status: 404 });
		return json(err("E_UPDATE_FAILED", e?.message ?? "Failed to update task status"), { status: 500 });
	}
};

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