import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import * as fs from "node:fs";
import nodeFs from "node:fs";
import { syncBuiltinESMExports } from "node:module";
import * as os from "node:os";
import * as path from "node:path";
import { afterEach, describe, it } from "node:test";
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
import {
	SCHEDULED_RUN_ACTIONS,
	createScheduledRunManager,
	isScheduledRunAction,
	listScheduledRunSummaries,
	parseScheduleInterval,
	parseScheduledRunTime,
	scheduledRunStorePath,
	scheduledRunsEnabled,
	type ScheduledRunManager,
} from "../../src/runs/background/scheduled-runs.ts";
import type { ExtensionConfig } from "../../src/shared/types.ts";
import { createMission, readMission, resolveMissionStoreLocation } from "../../src/missions/store.ts";

type Timer = { callback: () => void; delay: number };
class FakeTimers {
	readonly values = new Map<number, Timer>();
	private id = 0;
	setTimeout = (callback: () => void, delay: number) => {
		const id = ++this.id;
		this.values.set(id, { callback, delay });
		return id as unknown as ReturnType<typeof setTimeout>;
	};
	clearTimeout = (id: ReturnType<typeof setTimeout>) => void this.values.delete(id as unknown as number);
	fireAll(): void {
		const pending = [...this.values.entries()];
		for (const [id, timer] of pending) {
			this.values.delete(id);
			timer.callback();
		}
	}
}

type Launch = {
	params: Record<string, unknown>;
	ctx: ExtensionContext;
	resolve(result: { content: Array<{ type: "text"; text: string }>; details: Record<string, unknown>; isError?: boolean }): void;
};

type Harness = {
	manager: ScheduledRunManager;
	ctx: ExtensionContext;
	clock: { now: number };
	timers: FakeTimers;
	launches: Launch[];
	root: string;
};

const roots: string[] = [];
afterEach(() => {
	for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true });
});

function git(cwd: string, args: string[]): void {
	execFileSync("git", args, { cwd, stdio: "ignore" });
}

function context(cwd: string, sessionId = "session-a"): ExtensionContext {
	return {
		cwd,
		sessionManager: {
			getSessionId: () => sessionId,
			getSessionFile: () => path.join(cwd, `${sessionId}.jsonl`),
		},
	} as unknown as ExtensionContext;
}

function harness(options: { cwd?: string; sessionId?: string; now?: number; config?: ExtensionConfig; randomId?: () => string; kill?: (pid: number) => boolean } = {}): Harness {
	const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-schedule-test-"));
	roots.push(root);
	const project = options.cwd ?? path.join(root, "project");
	fs.mkdirSync(project, { recursive: true });
	const ctx = context(project, options.sessionId);
	const clock = { now: options.now ?? Date.parse("2030-01-01T00:00:00Z") };
	const timers = new FakeTimers();
	const launches: Launch[] = [];
	let id = 0;
	const manager = createScheduledRunManager({
		config: options.config ?? { scheduledRuns: { enabled: true } },
		storeRoot: path.join(root, "stores"),
		now: () => clock.now,
		randomId: options.randomId ?? (() => `id-${++id}`),
		timers,
		launch: (params, launchCtx) => new Promise((resolve) => launches.push({ params: params as Record<string, unknown>, ctx: launchCtx, resolve: resolve as Launch["resolve"] })) as never,
		kill: options.kill,
	});
	manager.bindSession(ctx);
	return { manager, ctx, clock, timers, launches, root };
}

function text(result: Awaited<ReturnType<ScheduledRunManager["handleToolCall"]>>): string {
	return result.content[0]?.type === "text" ? result.content[0].text : "";
}

function detailRecords(result: Awaited<ReturnType<ScheduledRunManager["handleToolCall"]>>): Array<Record<string, unknown>> {
	return result.details?.schedules?.records as Array<Record<string, unknown>> ?? [];
}

async function flush(): Promise<void> {
	for (let i = 0; i < 8; i++) await Promise.resolve();
}

describe("calendar schedule execution", () => {
	const script = "return runs.run('main', { agent: 'reviewer' })";
	async function daily(h: Harness, extra: Record<string, unknown> = {}) {
		const result = await h.manager.handleToolCall({ action: "schedule.create", id: "calendar", every: "day", at: "09:00", timezone: "Asia/Taipei", workflowScript: script, ...extra }, h.ctx);
		assert.equal(result.isError, undefined, text(result));
		return result;
	}
	async function trigger(h: Harness) {
		return detailRecords(await h.manager.handleToolCall({ action: "schedule.show", id: "calendar" }, h.ctx))[0]!.trigger as Record<string, unknown>;
	}
	it("persists daily and weekly rules, displays the zone, and rearms an early timer", async () => {
		const h = harness();
		await daily(h, { every: "week", on: ["fri", "tue", "fri"], quiet: true });
		assert.deepEqual((await trigger(h)).on, ["tue", "fri"]);
		assert.equal((await trigger(h)).nextLocalDate, "2030-01-01");
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.show", id: "calendar" }, h.ctx)), /every week.*09:00 Asia\/Taipei/);
		h.timers.fireAll();
		await flush();
		assert.equal(h.launches.length, 0);
		assert.equal(h.timers.values.size, 1);
		h.clock.now = Date.parse("2030-01-01T01:00:00Z");
		h.timers.fireAll();
		await flush();
		assert.equal((h.launches[0]!.params.scheduleOrigin as Record<string, unknown>).quiet, true);
		h.launches[0]!.resolve({ content: [], details: { asyncId: "calendar-run" } });
		await flush();
		assert.equal((await trigger(h)).nextLocalDate, "2030-01-04");
	});
	it("runs only the latest missed date after a long pause", async () => {
		const h = harness();
		await daily(h);
		await h.manager.handleToolCall({ action: "schedule.pause", id: "calendar" }, h.ctx);
		h.clock.now = Date.parse("2035-01-01T02:00:00Z");
		await h.manager.handleToolCall({ action: "schedule.resume", id: "calendar" }, h.ctx);
		const due = h.manager.handleToolCall({ action: "schedule.run-due" }, h.ctx);
		await flush();
		h.launches[0]!.resolve({ content: [], details: { asyncId: "latest" } });
		const result = await due;
		assert.equal(result.details?.schedules?.runs?.[0]?.plannedAt, "2035-01-01T01:00:00.000Z");
		assert.equal(h.launches.length, 1);
		assert.equal((await trigger(h)).nextLocalDate, "2035-01-02");
	});
	it("records one missed receipt for catchUp:none and advances past downtime", async () => {
		const h = harness();
		await daily(h, { catchUp: "none" });
		await h.manager.handleToolCall({ action: "schedule.pause", id: "calendar" }, h.ctx);
		h.clock.now = Date.parse("2035-01-01T02:00:00Z");
		await h.manager.handleToolCall({ action: "schedule.resume", id: "calendar" }, h.ctx);
		const history = await h.manager.handleToolCall({ action: "schedule.history", id: "calendar" }, h.ctx);
		assert.equal(history.details?.schedules?.runs?.length, 1);
		assert.equal(history.details?.schedules?.runs?.[0]?.state, "missed");
		assert.equal(h.launches.length, 0);
		assert.equal((await trigger(h)).nextLocalDate, "2035-01-02");
	});
	it("successful manual attachment satisfies the pending future date", async () => {
		const h = harness();
		await daily(h);
		const manual = h.manager.handleToolCall({ action: "schedule.run", id: "calendar" }, h.ctx);
		await flush();
		h.launches[0]!.resolve({ content: [], details: { asyncId: "manual" } });
		await manual;
		assert.equal((await trigger(h)).nextLocalDate, "2030-01-02");
		assert.equal((await trigger(h)).nextRunAt, "2030-01-02T01:00:00.000Z");
	});
	it("failed manual attachment preserves both pending fields", async () => {
		const h = harness();
		await daily(h);
		const before = await trigger(h);
		const manual = h.manager.handleToolCall({ action: "schedule.run", id: "calendar" }, h.ctx);
		await flush();
		h.launches[0]!.resolve({ content: [{ type: "text", text: "launch rejected" }], details: {}, isError: true });
		assert.equal((await manual).isError, true);
		assert.deepEqual(await trigger(h), before);
	});
	for (const paused of [false, true]) it(`restores an overlapped calendar fire after failed manual attachment, paused: ${paused}`, async () => {
		const h = harness();
		await daily(h);
		const before = await trigger(h);
		const manual = h.manager.handleToolCall({ action: "schedule.run", id: "calendar" }, h.ctx);
		await flush();
		h.clock.now = Date.parse("2030-01-01T01:00:00Z");
		h.timers.fireAll();
		await flush();
		assert.equal((await trigger(h)).nextLocalDate, "2030-01-02");
		if (paused) await h.manager.handleToolCall({ action: "schedule.pause", id: "calendar" }, h.ctx);
		h.launches[0]!.resolve({ content: [{ type: "text", text: "launch rejected" }], details: {}, isError: true });
		assert.equal((await manual).isError, true);
		assert.deepEqual(await trigger(h), before);
		assert.equal(detailRecords(await h.manager.handleToolCall({ action: "schedule.show", id: "calendar" }, h.ctx))[0]!.paused, paused);
		assert.equal(h.launches.length, 1);
		if (paused) assert.equal(h.timers.values.size, 0);
		else {
			h.timers.fireAll();
			await flush();
			assert.equal(h.launches.length, 2);
			h.launches[1]!.resolve({ content: [], details: { asyncId: "natural-retry" } });
			await flush();
			assert.equal((await trigger(h)).nextLocalDate, "2030-01-02");
			assert.equal((await trigger(h)).nextRunAt, "2030-01-02T01:00:00.000Z");
		}
	});
	it("does not consume an extra date when a natural fire overlaps manual attachment", async () => {
		const h = harness();
		await daily(h);
		const manual = h.manager.handleToolCall({ action: "schedule.run", id: "calendar" }, h.ctx);
		await flush();
		h.clock.now = Date.parse("2030-01-01T01:00:00Z");
		h.timers.fireAll();
		await flush();
		h.launches[0]!.resolve({ content: [], details: { asyncId: "manual-overlap" } });
		await manual;
		assert.equal((await trigger(h)).nextLocalDate, "2030-01-02");
		assert.equal(h.launches.length, 1);
	});
	it("re-resolves the pending date on restoration without list rewriting the cache", async () => {
		const h = harness();
		await daily(h);
		const file = path.join(scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores")), "calendar", "schedule.json");
		const saved = JSON.parse(fs.readFileSync(file, "utf-8"));
		saved.trigger.nextRunAt = "2030-01-01T03:00:00.000Z";
		fs.writeFileSync(file, JSON.stringify(saved));
		await h.manager.handleToolCall({ action: "schedule.list" }, h.ctx);
		assert.equal(JSON.parse(fs.readFileSync(file, "utf-8")).trigger.nextRunAt, saved.trigger.nextRunAt);
		h.manager.stop();
		h.manager.bindSession(h.ctx);
		assert.equal(JSON.parse(fs.readFileSync(file, "utf-8")).trigger.nextRunAt, "2030-01-01T01:00:00.000Z");
	});
	it("does not fire twice during a repeated hour, including restoration", async () => {
		const h = harness({ now: Date.parse("2026-11-01T04:00:00Z") });
		await daily(h, { at: "01:30", timezone: "America/New_York" });
		h.clock.now = Date.parse("2026-11-01T05:30:00Z");
		h.timers.fireAll();
		await flush();
		h.launches[0]!.resolve({ content: [], details: { asyncId: "fold" } });
		await flush();
		h.manager.handleAsyncCompletion({ id: "fold", success: true });
		h.clock.now = Date.parse("2026-11-01T06:45:00Z");
		h.manager.bindSession(h.ctx);
		await h.manager.handleToolCall({ action: "schedule.run-due" }, h.ctx);
		assert.equal(h.launches.length, 1);
		assert.equal((await trigger(h)).nextRunAt, "2026-11-02T06:30:00.000Z");
	});
});

describe("schedule helpers", () => {
	it("recognizes only the dot-action schedule API", () => {
		assert.deepEqual(SCHEDULED_RUN_ACTIONS, ["schedule.create", "schedule.list", "schedule.show", "schedule.history", "schedule.pause", "schedule.resume", "schedule.run", "schedule.run-due", "schedule.delete"]);
		assert.equal(isScheduledRunAction("schedule.create"), true);
		assert.equal(isScheduledRunAction("schedule"), false);
	});

	it("uses a stable project store independent of session id", () => {
		const root = path.join("tmp", "schedules");
		assert.equal(scheduledRunStorePath("project", "a", root), scheduledRunStorePath("project", "b", root));
		assert.notEqual(scheduledRunStorePath("project", "a", root), scheduledRunStorePath("other", "a", root));
		assert.equal(scheduledRunStorePath("/project"), path.join(path.resolve("/project"), ".pi/subagents", "schedules"));
	});

	it("parses one-shot and fixed interval forms strictly", () => {
		const now = Date.parse("2030-01-01T00:00:00Z");
		assert.equal(parseScheduledRunTime("+10m", now), now + 600_000);
		assert.equal(parseScheduledRunTime("2030-01-02T00:00:00Z", now), now + 86_400_000);
		assert.equal(parseScheduledRunTime("2030-01-02T09:00:00+05:30", now), Date.parse("2030-01-02T09:00:00+05:30"));
		assert.equal(parseScheduleInterval("30m"), 1_800_000);
		assert.equal(parseScheduleInterval("2w"), 1_209_600_000);
		assert.throws(() => parseScheduleInterval("day"), /fixed intervals/);
		assert.throws(() => parseScheduledRunTime("2030-01-01T00:00:00", now), /timezone/);
	});

	it("honors the explicit feature opt-out", () => {
		assert.equal(scheduledRunsEnabled({}), true);
		assert.equal(scheduledRunsEnabled({ scheduledRuns: { enabled: false } }), false);
	});
});

describe("project schedule management", () => {
	it("creates a project one-shot schedule and restores it in another session", async () => {
		const first = harness();
		const result = await first.manager.handleToolCall({ action: "schedule.create", id: "night-review", name: "Night review", at: "+10m", workflowScript: "return runs.run('main', { agent: 'reviewer', task: 'Review the diff' })" }, first.ctx);
		assert.equal(result.isError, undefined);
		assert.match(text(result), /Created schedule night-review/);
		assert.equal(first.timers.values.size, 1);
		const records = listScheduledRunSummaries(first.ctx.cwd, path.join(first.root, "stores"));
		assert.equal(records[0]?.name, "Night review");
		assert.equal(records[0]?.cwd, first.ctx.cwd);

		first.manager.stop();
		const secondTimers = new FakeTimers();
		const second = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(first.root, "stores"),
			now: () => first.clock.now,
			timers: secondTimers,
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		second.bindSession(context(first.ctx.cwd, "session-b"));
		assert.equal(secondTimers.values.size, 1, "a different session restores the project schedule");
		const shown = await second.handleToolCall({ action: "schedule.show", id: "night-review" }, context(first.ctx.cwd, "session-b"));
		assert.match(text(shown), /Night review/);
	});

	it("restores session-only schedules only in the creating session", async () => {
		const owner = harness({ sessionId: "owner-session" });
		const created = await owner.manager.handleToolCall({
			action: "schedule.create",
			id: "owner-only",
			name: "Owner only",
			every: "1h",
			cwd: owner.ctx.cwd,
			sessionOnly: true,
			workflowScript: "return 1",
		}, owner.ctx);
		assert.equal(created.isError, undefined);
		assert.equal(detailRecords(created)[0]?.sessionOnly, true);
		assert.equal("ownerSessionFile" in detailRecords(created)[0]!, false);

		const root = scheduledRunStorePath(owner.ctx.cwd, undefined, path.join(owner.root, "stores"));
		const [record] = listScheduledRunSummaries(owner.ctx.cwd, path.join(owner.root, "stores"));
		assert.equal(record?.sessionOnly, true);
		assert.equal(record?.ownerSessionFile, owner.ctx.sessionManager.getSessionFile());

		owner.manager.stop();
		const otherTimers = new FakeTimers();
		const otherLaunches: Launch[] = [];
		const other = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(owner.root, "stores"),
			now: () => owner.clock.now,
			timers: otherTimers,
			launch: (params, launchCtx) => new Promise((resolve) => otherLaunches.push({ params: params as Record<string, unknown>, ctx: launchCtx, resolve: resolve as Launch["resolve"] })) as never,
		});
		const otherContext = context(owner.ctx.cwd, "other-session");
		other.bindSession(otherContext);
		assert.equal(otherTimers.values.size, 0, "non-owner sessions must not arm session-only schedules");

		const manual = await other.handleToolCall({ action: "schedule.run", id: "owner-only" }, otherContext);
		assert.match(text(manual), /not its owner/);
		assert.equal("ownerSessionFile" in detailRecords(manual)[0]!, false);
		const shownToOther = await other.handleToolCall({ action: "schedule.show", id: "owner-only" }, otherContext);
		assert.equal("ownerSessionFile" in detailRecords(shownToOther)[0]!, false);
		const listedToOther = await other.handleToolCall({ action: "schedule.list" }, otherContext);
		assert.equal("ownerSessionFile" in detailRecords(listedToOther)[0]!, false);
		assert.equal(otherLaunches.length, 0);
		assert.equal(listScheduledRunSummaries(owner.ctx.cwd, path.join(owner.root, "stores"))[0]?.ownerSessionFile, owner.ctx.sessionManager.getSessionFile());
		assert.equal(fs.existsSync(path.join(root, "owner-only", "active.lock")), false);

		const ownerTimers = new FakeTimers();
		const ownerAgain = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(owner.root, "stores"),
			now: () => owner.clock.now,
			timers: ownerTimers,
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		ownerAgain.bindSession(context(owner.ctx.cwd, "owner-session"));
		assert.equal(ownerTimers.values.size, 1, "the creating session must restore its schedule");
	});

	it("re-arms an owner timer after a non-owner context consumes it", async () => {
		const h = harness({ sessionId: "owner-session" });
		await h.manager.handleToolCall({ action: "schedule.create", id: "owner-only", cwd: h.ctx.cwd, every: "1h", sessionOnly: true, workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		const nextRunAt = listScheduledRunSummaries(h.ctx.cwd, path.join(h.root, "stores"))[0]?.trigger.nextRunAt;
		h.manager.bindSession(context(h.ctx.cwd, "other-session"));
		h.clock.now += 3_600_000;
		h.timers.fireAll();
		await flush();
		assert.equal(h.launches.length, 0, "a non-owner context must not launch the schedule");
		assert.equal(h.timers.values.size, 0, "the consumed non-owner timer is not left spinning");
		assert.equal(listScheduledRunSummaries(h.ctx.cwd, path.join(h.root, "stores"))[0]?.trigger.nextRunAt, nextRunAt, "a non-owner context must not advance the schedule");

		h.manager.bindSession(h.ctx);
		assert.equal(h.timers.values.size, 1, "rebinding the owner must restore the timer");
		h.timers.fireAll();
		await flush();
		assert.equal(h.launches.length, 1);
		assert.equal(h.launches[0]!.ctx.sessionManager.getSessionId(), "owner-session");
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "owner-async" } });
		await flush();
	});

	it("does not let completed one-shot schedules consume maxPending capacity", async () => {
		const h = harness({ config: { scheduledRuns: { enabled: true, maxPending: 1 } } });
		await h.manager.handleToolCall({ action: "schedule.create", id: "completed", at: "+1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		h.clock.now += 3_600_000;
		h.timers.fireAll();
		await flush();
		assert.equal(h.launches.length, 1);
		const activeResult = await h.manager.handleToolCall({ action: "schedule.create", id: "active-blocked", at: "+2h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		assert.equal(activeResult.isError, true);
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "completed-async" } });
		await flush();
		h.manager.handleAsyncCompletion({ runId: "completed-async", success: true });

		const result = await h.manager.handleToolCall({ action: "schedule.create", id: "replacement", at: "+2h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		assert.equal(result.isError, undefined);
		const completedDir = path.join(scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores")), "completed");
		assert.equal(fs.existsSync(path.join(completedDir, "history.json")), true);
		assert.equal(fs.existsSync(path.join(completedDir, "events.jsonl")), true);
		const completedHistory = await h.manager.handleToolCall({ action: "schedule.history", id: "completed" }, h.ctx);
		assert.match(text(completedHistory), /completed.*async completed-async/);
	});

	it("stores explicit cwd schedules in the target project", async () => {
		const h = harness();
		const target = path.join(h.root, "other-project");
		fs.mkdirSync(target);
		h.manager.bindSession(context(target, "target-session"));
		h.manager.bindSession(h.ctx);
		await h.manager.handleToolCall({ action: "schedule.create", id: "other", cwd: target, every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		assert.equal(listScheduledRunSummaries(h.ctx.cwd, path.join(h.root, "stores")).length, 0);
		assert.equal(listScheduledRunSummaries(target, path.join(h.root, "stores"))[0]?.cwd, target);
		const listed = await h.manager.handleToolCall({ action: "schedule.list", cwd: target }, h.ctx);
		assert.match(text(listed), /other/);
	});

	it("treats a deleted project cwd as having no schedules during restore and listing", () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-schedule-deleted-project-"));
		roots.push(root);
		const project = path.join(root, "project");
		fs.mkdirSync(project);
		fs.rmSync(project, { recursive: true });
		assert.deepEqual(listScheduledRunSummaries(project), []);
		const manager = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		assert.doesNotThrow(() => manager.bindSession(context(project)));
	});

	it("rejects direct schedule targets and requires a workflow script", async () => {
		const h = harness();
		const result = await h.manager.handleToolCall({ action: "schedule.create", id: "direct", every: "1h", agent: "worker", task: "Review" }, h.ctx);
		assert.equal(result.isError, true);
		assert.match(text(result), /requires workflow: true or a workflow script path/);
	});

	it("fails closed on persisted legacy agent targets", () => {
		const h = harness();
		h.manager.stop();
		const root = scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores"));
		const dir = path.join(root, "legacy");
		fs.mkdirSync(dir, { recursive: true });
		fs.writeFileSync(path.join(dir, "schedule.json"), JSON.stringify({
			schemaVersion: 1,
			id: "legacy",
			name: "Legacy direct target",
			cwd: h.ctx.cwd,
			trigger: { kind: "interval", every: "1h", everyMs: 3_600_000, anchorAt: new Date(h.clock.now).toISOString(), nextRunAt: new Date(h.clock.now + 3_600_000).toISOString() },
			target: { agent: "worker", task: "Review backlog" },
			overlap: "skip",
			catchUp: "latest",
			paused: false,
			createdAt: new Date(h.clock.now).toISOString(),
			updatedAt: new Date(h.clock.now).toISOString(),
		}), "utf-8");
		const manager = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "stores"),
			now: () => h.clock.now,
			timers: h.timers,
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});

		assert.throws(() => manager.bindSession(context(h.ctx.cwd, "session-b")), /removed legacy agent target.*schedule\.create and workflow: true or a workflow script path/i);
	});

	it("supports workflowScript targets and rejects unsafe or deferred shapes", async () => {
		const h = harness();
		const workflow = await h.manager.handleToolCall({ action: "schedule.create", id: "workflow", every: "6h", workflowScript: "return await runs.run('review', {agent:'reviewer'})" }, h.ctx);
		assert.equal(workflow.isError, undefined);
		assert.match(text(workflow), /workflow -> agent reviewer/);
		const dynamic = await h.manager.handleToolCall({ action: "schedule.create", id: "dynamic", every: "6h", workflowScript: "const agent = 'worker'; return runs.run('main', { agent })" }, h.ctx);
		assert.equal(dynamic.isError, undefined);
		assert.match(text(dynamic), /workflow \(dynamic\)/);
		for (const params of [
			{ action: "schedule.create", id: "../escape", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" },
			{ action: "schedule.create", id: "both", at: "+1h", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" },
			{ action: "schedule.create", id: "calendar", every: "day", at: "09:00", workflowScript: "return runs.run('main', { agent: 'worker' })" },
			{ action: "schedule.create", id: "two-targets", every: "1h", agent: "worker", workflowScript: "return 1" },
			{ action: "schedule.create", id: "fork", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })", context: "fork" },
			{ action: "schedule.create", id: "invalid-base-ref", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })", baseRef: "unsafe..ref" },
			{ action: "schedule.create", id: "revision-alias-base-ref", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })", baseRef: "@" },
			{ action: "schedule.create", id: "object-id-base-ref", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })", baseRef: "a".repeat(40) },
			{ action: "schedule.create", id: "sha256-object-id-base-ref", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })", baseRef: "a".repeat(64) },
			{ action: "schedule.create", id: "invalid-args", every: "1h", workflowScript: "return 1", args: { task: "" } },
			{ action: "schedule.create", id: "mission-id", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })", missionId: "mission-1" },
			{ action: "schedule.create", id: "mission-off", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })", mission: false },
		] as const) {
			const result = await h.manager.handleToolCall(params, h.ctx);
			assert.equal(result.isError, true, JSON.stringify(params));
		}
	});

	it("persists and forwards scheduled workflow arguments and baseRef", async () => {
		const h = harness();
		const args = { task: "review", nested: { retries: 2 } };
		const created = await h.manager.handleToolCall({
			action: "schedule.create",
			id: "base-ref",
			every: "1h",
			baseRef: "@/foo",
			args,
			workflowScript: "return runs.run('main', { agent: 'worker' })",
		}, h.ctx);
		assert.equal(created.isError, undefined);
		assert.doesNotMatch(text(created), /review|retries/);
		assert.equal(listScheduledRunSummaries(h.ctx.cwd, path.join(h.root, "stores"))[0]?.target.baseRef, "@/foo");
		assert.deepEqual(listScheduledRunSummaries(h.ctx.cwd, path.join(h.root, "stores"))[0]?.target.args, args);
		args.nested.retries = 99;

		h.manager.stop();
		const restoredLaunches: Launch[] = [];
		const restored = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "stores"),
			now: () => h.clock.now,
			timers: new FakeTimers(),
			launch: (params, launchCtx) => new Promise((resolve) => restoredLaunches.push({ params: params as Record<string, unknown>, ctx: launchCtx, resolve: resolve as Launch["resolve"] })) as never,
		});
		restored.bindSession(h.ctx);
		const running = restored.handleToolCall({ action: "schedule.run", id: "base-ref" }, h.ctx);
		await flush();
		assert.equal(restoredLaunches[0]?.params.baseRef, "@/foo");
		assert.deepEqual(restoredLaunches[0]?.params.args, { task: "review", nested: { retries: 2 } });
		restoredLaunches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "workflow", results: [], asyncId: "base-ref-async" } });
		const result = await running;
		assert.equal(result.isError, undefined);
	});

	it("attaches an existing mission without activating it and forwards it on manual and due fires", async () => {
		const config: ExtensionConfig = { missions: { directory: ".missions", globalIndex: false, enabled: false } };
		const h = harness({ config });
		const location = resolveMissionStoreLocation({ projectRoot: h.ctx.cwd, config: config.missions });
		const mission = createMission(location, { title: "Backlog", objective: "Track progress" });
		const created = await h.manager.handleToolCall({ action: "schedule.create", id: "mission", every: "1h", workflowScript: "return 1", missionId: mission.id }, h.ctx);
		assert.equal(created.isError, undefined);
		assert.deepEqual(readMission(location, mission.id), mission, "scheduling must not activate the mission");
		assert.equal((detailRecords(created)[0]?.target as { missionId?: string }).missionId, mission.id);
		assert.equal(detailRecords(created)[0]?.schemaVersion, 2, "old readers must reject a mission-bound definition");
		assert.match(text(created), new RegExp(`Mission: ${mission.id}`));
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.show", id: "mission" }, h.ctx)), new RegExp(`Mission: ${mission.id}`));
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.list" }, h.ctx)), new RegExp(`mission ${mission.id}`));
		for (const [index, action] of ["schedule.run", "schedule.run-due"].entries()) {
			if (index) h.clock.now += 3_600_000;
			const pending = h.manager.handleToolCall({ action, id: "mission" }, h.ctx);
			await flush();
			const launch = h.launches[index]!;
			assert.equal(launch.params.missionId, mission.id);
			assert.equal(Object.hasOwn(launch.params, "mission"), false, "mission:false conflicts with an explicit ID");
			const asyncId = `mission-${index}`;
			launch.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "workflow", results: [], asyncId } });
			assert.equal((await pending).isError, undefined);
			h.manager.handleAsyncCompletion({ id: asyncId, success: true });
		}
	});

	it("rejects missing or invalid mission attachment without persisting a schedule", async () => {
		const h = harness({ config: { missions: { directory: ".missions", globalIndex: false } } });
		for (const missionId of ["missing", "../escape", "", null]) {
			const result = await h.manager.handleToolCall({ action: "schedule.create", id: "rejected", every: "1h", workflowScript: "return 1", missionId } as never, h.ctx);
			assert.equal(result.isError, true);
			assert.match(text(result), /missionId|was not found/);
		}
		assert.deepEqual(listScheduledRunSummaries(h.ctx.cwd, path.join(h.root, "stores")), []);
		assert.equal(h.launches.length, 0);
	});

	it("resolves mission attachment from the target cwd and configured store", async () => {
		const h = harness({ config: { missions: { directory: ".missions", globalIndex: false } } });
		const targetCwd = path.join(h.root, "target-project");
		fs.mkdirSync(targetCwd);
		const location = resolveMissionStoreLocation({ projectRoot: targetCwd, config: { directory: ".missions", globalIndex: false } });
		const mission = createMission(location, { title: "Target project", objective: "Keep project state" });
		const request = { action: "schedule.create", id: "target-mission", every: "1h", workflowScript: "return 1", missionId: mission.id };
		const wrongStore = await h.manager.handleToolCall(request, h.ctx);
		assert.equal(wrongStore.isError, true);
		h.manager.bindSession(context(targetCwd, "target-session"));
		h.manager.bindSession(h.ctx);
		const created = await h.manager.handleToolCall({ ...request, cwd: targetCwd }, h.ctx);
		assert.equal(created.isError, undefined, text(created));
		assert.equal(detailRecords(created)[0]?.cwd, targetCwd);
		assert.deepEqual(readMission(location, mission.id), mission);

		const shared = harness({ config: { missions: { directory: location.missionDir, globalIndex: false } } });
		const sharedResult = await shared.manager.handleToolCall(request, shared.ctx);
		assert.equal(sharedResult.isError, undefined, "an explicit shared store retains ordinary mission lookup semantics");
		assert.deepEqual(readMission(location, mission.id), mission);
	});

	it("rejects a malformed persisted mission ID instead of dropping the attachment", async () => {
		const config: ExtensionConfig = { missions: { directory: ".missions", globalIndex: false } };
		const h = harness({ config });
		const location = resolveMissionStoreLocation({ projectRoot: h.ctx.cwd, config: config.missions });
		const mission = createMission(location, { title: "Backlog", objective: "Track progress" });
		await h.manager.handleToolCall({ action: "schedule.create", id: "tampered-mission", every: "1h", workflowScript: "return 1", missionId: mission.id }, h.ctx);
		h.manager.stop();
		const file = path.join(scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores")), "tampered-mission", "schedule.json");
		const record = JSON.parse(fs.readFileSync(file, "utf-8"));
		record.target.missionId = null;
		fs.writeFileSync(file, JSON.stringify(record));
		assert.throws(() => h.manager.bindSession(h.ctx), /invalid missionId/);
		assert.equal(h.timers.values.size, 0);
		assert.equal(h.launches.length, 0);
	});

	it("keeps unbound definitions readable and rejects inconsistent attachment versions", async () => {
		const config: ExtensionConfig = { missions: { directory: ".missions", globalIndex: false } };
		const h = harness({ config });
		const plain = await h.manager.handleToolCall({ action: "schedule.create", id: "plain", every: "1h", workflowScript: "return 1" }, h.ctx);
		assert.equal(detailRecords(plain)[0]?.schemaVersion, 1);
		const location = resolveMissionStoreLocation({ projectRoot: h.ctx.cwd, config: config.missions });
		const mission = createMission(location, { title: "Backlog", objective: "Track progress" });
		await h.manager.handleToolCall({ action: "schedule.create", id: "bound", every: "1h", workflowScript: "return 1", missionId: mission.id }, h.ctx);
		h.manager.stop();
		const file = path.join(scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores")), "bound", "schedule.json");
		const record = JSON.parse(fs.readFileSync(file, "utf-8"));
		for (const malformed of [
			{ ...record, schemaVersion: 1 },
			{ ...record, target: { workflowScript: record.target.workflowScript, args: record.target.args } },
		]) {
			fs.writeFileSync(file, JSON.stringify(malformed));
			const rejected = await h.manager.handleToolCall({ action: "schedule.run", id: "bound" }, h.ctx);
			assert.equal(rejected.isError, true);
			assert.match(text(rejected), /requires schemaVersion 2/);
		}
		assert.equal(h.launches.length, 0);
	});

	it("pauses, resumes, lists, and deletes an inactive schedule", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "maintenance", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.list" }, h.ctx)), /maintenance/);
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.pause", id: "maintenance" }, h.ctx)), /Paused/);
		assert.equal(h.timers.values.size, 0);
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.resume", id: "maintenance" }, h.ctx)), /Resumed/);
		assert.equal(h.timers.values.size, 1);
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.delete", id: "maintenance" }, h.ctx)), /Deleted/);
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.list" }, h.ctx)), /No project schedules/);
	});

	it("deletes a foreign session-only schedule only after its exact async run is terminal", async () => {
		const owner = harness({ sessionId: "owner-session" });
		await owner.manager.handleToolCall({ action: "schedule.create", id: "owner-only", every: "1h", sessionOnly: true, workflowScript: "return runs.run('main', { agent: 'worker' })" }, owner.ctx);
		const running = owner.manager.handleToolCall({ action: "schedule.run", id: "owner-only" }, owner.ctx);
		await flush();
		const asyncDir = path.join(owner.root, "async-owner-only");
		fs.mkdirSync(asyncDir);
		owner.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "async-owner-only", asyncDir } });
		await running;
		owner.manager.stop();

		fs.writeFileSync(path.join(asyncDir, "status.json"), JSON.stringify({ runId: "async-owner-only", mode: "single", state: "running", startedAt: owner.clock.now }), "utf-8");
		const other = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(owner.root, "stores"),
			now: () => owner.clock.now,
			timers: new FakeTimers(),
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		const otherCtx = context(owner.ctx.cwd, "other-session");
		other.bindSession(otherCtx);
		const refused = await other.handleToolCall({ action: "schedule.delete", id: "owner-only" }, otherCtx);
		assert.equal(refused.isError, true);
		assert.match(text(refused), /has active run/);

		fs.writeFileSync(path.join(asyncDir, "status.json"), JSON.stringify({ runId: "async-owner-only", mode: "single", state: ["failed"], startedAt: owner.clock.now, endedAt: owner.clock.now }), "utf-8");
		const malformed = await other.handleToolCall({ action: "schedule.delete", id: "owner-only" }, otherCtx);
		assert.equal(malformed.isError, true);
		assert.match(text(malformed), /has active run/);

		fs.writeFileSync(path.join(asyncDir, "status.json"), JSON.stringify({ runId: "async-owner-only", mode: "single", state: "failed", startedAt: owner.clock.now, endedAt: owner.clock.now }), "utf-8");
		assert.match(text(await other.handleToolCall({ action: "schedule.delete", id: "owner-only" }, otherCtx)), /Deleted/);
		assert.match(text(await other.handleToolCall({ action: "schedule.list" }, otherCtx)), /No project schedules/);
	});

	it("ignores schedules deleted by another session while a timer is still armed", async () => {
		const first = harness();
		await first.manager.handleToolCall({ action: "schedule.create", id: "stale", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, first.ctx);
		const secondTimers = new FakeTimers();
		const second = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(first.root, "stores"),
			now: () => first.clock.now,
			timers: secondTimers,
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		const secondCtx = context(first.ctx.cwd, "session-b");
		second.bindSession(secondCtx);
		assert.equal(first.timers.values.size, 1);
		assert.equal(secondTimers.values.size, 1);

		assert.match(text(await second.handleToolCall({ action: "schedule.delete", id: "stale" }, secondCtx)), /Deleted/);
		assert.equal(secondTimers.values.size, 0);
		assert.equal(first.timers.values.size, 1);
		first.clock.now += 3_600_000;
		const warnings: string[] = [];
		const originalWarn = console.warn;
		console.warn = (message?: unknown) => void warnings.push(String(message));
		try {
			first.timers.fireAll();
			await flush();
		} finally {
			console.warn = originalWarn;
		}
		assert.deepEqual(warnings, []);
		assert.equal(first.launches.length, 0);
		assert.equal(first.timers.values.size, 0);
		assert.match(text(await first.manager.handleToolCall({ action: "schedule.list" }, first.ctx)), /No project schedules/);
	});

	it("reports corrupt project schedule records instead of dropping them", async () => {
		const h = harness();
		const root = scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores"));
		const dir = path.join(root, "broken");
		fs.mkdirSync(dir, { recursive: true });
		fs.writeFileSync(path.join(dir, "schedule.json"), "{ bad", "utf-8");
		const result = await h.manager.handleToolCall({ action: "schedule.list" }, h.ctx);
		assert.equal(result.isError, true);
		assert.match(text(result), /Failed to read schedule record/);
	});

	it("fails closed when persisted schedule arguments are invalid", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "tampered-args", every: "1h", workflowScript: "return 1", args: { task: "valid" } }, h.ctx);
		h.manager.stop();
		const schedulePath = path.join(scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores")), "tampered-args", "schedule.json");
		const record = JSON.parse(fs.readFileSync(schedulePath, "utf-8")) as { target: { args: unknown } };
		record.target.args = { task: "" };
		fs.writeFileSync(schedulePath, JSON.stringify(record));

		const restored = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "stores"),
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		assert.throws(() => restored.bindSession(h.ctx), /workflow args\.task must not be empty/);
	});

	it("skips orphan schedule directories during restore and listing", async () => {
		const h = harness();
		h.manager.stop();
		const root = scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores"));
		fs.mkdirSync(path.join(root, "orphan"), { recursive: true });
		const manager = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "stores"),
			timers: h.timers,
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		assert.doesNotThrow(() => manager.bindSession(h.ctx));
		assert.match(text(await manager.handleToolCall({ action: "schedule.list" }, h.ctx)), /No project schedules/);
	});

	it("rejects same-id create when an orphan directory contains stale state", async () => {
		const h = harness();
		const root = scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores"));
		const dir = path.join(root, "orphan");
		fs.mkdirSync(dir, { recursive: true });
		fs.writeFileSync(path.join(dir, "active.lock"), "stale", "utf-8");
		fs.writeFileSync(path.join(dir, "history.json"), JSON.stringify({ schemaVersion: 1, runs: [] }), "utf-8");
		const result = await h.manager.handleToolCall({ action: "schedule.create", id: "orphan", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		assert.equal(result.isError, true);
		assert.match(text(result), /already exists/);
		assert.equal(fs.existsSync(path.join(dir, "schedule.json")), false);
		assert.equal(fs.readFileSync(path.join(dir, "active.lock"), "utf-8"), "stale");
	});

	it("rejects schedule directories that escape through a symlink", async () => {
		const h = harness();
		const root = scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores"));
		const outside = path.join(h.root, "outside");
		fs.mkdirSync(root, { recursive: true });
		fs.mkdirSync(outside);
		fs.symlinkSync(outside, path.join(root, "escaped"), process.platform === "win32" ? "junction" : "dir");
		const result = await h.manager.handleToolCall({ action: "schedule.create", id: "escaped", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		assert.equal(result.isError, true);
		assert.match(text(result), /must be a real directory/);
		assert.equal(fs.existsSync(path.join(outside, "schedule.json")), false);
	});

	it("rejects a default project schedule root that escapes through .pi/subagents", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-schedule-root-link-"));
		roots.push(root);
		const project = path.join(root, "project");
		const outside = path.join(root, "outside");
		fs.mkdirSync(project);
		fs.mkdirSync(outside);
		const ctx = context(project);
		const manager = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		manager.bindSession(ctx);
		fs.mkdirSync(path.join(project, ".pi"));
		fs.symlinkSync(outside, path.join(project, ".pi/subagents"), process.platform === "win32" ? "junction" : "dir");

		const result = await manager.handleToolCall({ action: "schedule.create", id: "escaped-root", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, ctx);
		assert.equal(result.isError, true);
		assert.match(text(result), /resolves outside the real project/);
		assert.equal(fs.existsSync(path.join(outside, "schedules")), false);
	});

	it("allows the home directory's own .pi symlinked outside home, but not a project's .pi linked to it", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-schedule-home-link-"));
		roots.push(root);
		const home = path.join(root, "home");
		const config = path.join(root, "pi-config");
		const project = path.join(root, "project");
		fs.mkdirSync(home);
		fs.mkdirSync(config);
		fs.mkdirSync(project);
		const linkType = process.platform === "win32" ? "junction" : "dir";
		fs.symlinkSync(config, path.join(home, ".pi"), linkType);
		fs.symlinkSync(config, path.join(project, ".pi"), linkType);
		const previous = { HOME: process.env.HOME, USERPROFILE: process.env.USERPROFILE };
		process.env.HOME = home;
		process.env.USERPROFILE = home;
		try {
			const manager = () => createScheduledRunManager({
				config: { scheduledRuns: { enabled: true } },
				launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
			});
			const homeManager = manager();
			const ctx = context(home);
			homeManager.bindSession(ctx);
			const result = await homeManager.handleToolCall({ action: "schedule.create", id: "home", every: "1h", workflowScript: "return 1" }, ctx);
			assert.equal(result.isError, undefined);
			assert.equal(fs.existsSync(path.join(config, "subagents", "schedules", "home", "schedule.json")), true);
			assert.throws(() => manager().bindSession(context(project)), /resolves outside the real project/);
		} finally {
			for (const [key, value] of Object.entries(previous)) {
				if (value === undefined) delete process.env[key];
				else process.env[key] = value;
			}
		}
	});

	it("allows a default project schedule root through a shared Git worktree .pi symlink", async () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-schedule-worktree-link-"));
		roots.push(root);
		const repository = path.join(root, "repository");
		const worktree = path.join(repository, ".worktrees", "feature");
		fs.mkdirSync(repository, { recursive: true });
		git(repository, ["init"]);
		git(repository, ["config", "user.email", "test@example.com"]);
		git(repository, ["config", "user.name", "Test User"]);
		fs.writeFileSync(path.join(repository, "tracked.txt"), "base\n", "utf-8");
		git(repository, ["add", "tracked.txt"]);
		git(repository, ["commit", "-m", "base"]);
		fs.mkdirSync(path.dirname(worktree), { recursive: true });
		git(repository, ["worktree", "add", "-b", "feature", worktree]);
		fs.mkdirSync(path.join(repository, ".pi"), { recursive: true });
		fs.symlinkSync(path.join(repository, ".pi"), path.join(worktree, ".pi"), process.platform === "win32" ? "junction" : "dir");

		const ctx = context(worktree);
		const manager = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		manager.bindSession(ctx);
		const result = await manager.handleToolCall({ action: "schedule.create", id: "shared", every: "1h", workflowScript: "return 1" }, ctx);
		assert.equal(result.isError, undefined);
		assert.equal(fs.existsSync(path.join(repository, ".pi", "subagents", "schedules", "shared", "schedule.json")), true);
	});

	it("rejects a Git worktree .pi symlink outside the shared config root", () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-schedule-worktree-escape-"));
		roots.push(root);
		const repository = path.join(root, "repository");
		const worktree = path.join(repository, ".worktrees", "feature");
		const outside = path.join(root, "outside");
		fs.mkdirSync(repository, { recursive: true });
		git(repository, ["init"]);
		git(repository, ["config", "user.email", "test@example.com"]);
		git(repository, ["config", "user.name", "Test User"]);
		fs.writeFileSync(path.join(repository, "tracked.txt"), "base\n", "utf-8");
		git(repository, ["add", "tracked.txt"]);
		git(repository, ["commit", "-m", "base"]);
		fs.mkdirSync(path.dirname(worktree), { recursive: true });
		git(repository, ["worktree", "add", "-b", "feature", worktree]);
		fs.mkdirSync(path.join(repository, ".pi"), { recursive: true });
		fs.mkdirSync(outside);
		fs.symlinkSync(outside, path.join(worktree, ".pi"), process.platform === "win32" ? "junction" : "dir");

		const manager = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		assert.throws(() => manager.bindSession(context(worktree)), /resolves outside the real project/);
		assert.equal(fs.existsSync(path.join(outside, "subagents")), false);
	});

	it("rejects separate-git-dir primary checkout sharing without a registered root", () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-schedule-separate-git-dir-"));
		roots.push(root);
		const repository = path.join(root, "repository");
		const gitDir = path.join(root, "git-data", "metadata");
		const worktree = path.join(root, "worktree");
		fs.mkdirSync(repository, { recursive: true });
		fs.mkdirSync(path.dirname(gitDir), { recursive: true });
		git(repository, ["init", "--separate-git-dir", gitDir]);
		git(repository, ["config", "user.email", "test@example.com"]);
		git(repository, ["config", "user.name", "Test User"]);
		fs.writeFileSync(path.join(repository, "tracked.txt"), "base\n", "utf-8");
		git(repository, ["add", "tracked.txt"]);
		git(repository, ["commit", "-m", "base"]);
		git(repository, ["worktree", "add", "-b", "feature", worktree]);
		const primaryConfig = path.join(repository, ".pi");
		fs.mkdirSync(primaryConfig, { recursive: true });
		fs.symlinkSync(primaryConfig, path.join(worktree, ".pi"), process.platform === "win32" ? "junction" : "dir");

		const manager = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		assert.throws(() => manager.bindSession(context(worktree)), /resolves outside the real project/);
		assert.equal(fs.existsSync(path.join(primaryConfig, "subagents")), false);
	});

	it("rejects an unregistered checkout ancestor with the same Git directory", () => {
		const root = fs.mkdtempSync(path.join(os.tmpdir(), "pi-schedule-unregistered-git-dir-"));
		roots.push(root);
		const repository = path.join(root, "repository");
		const worktree = path.join(repository, ".worktrees", "feature");
		const outside = path.join(root, "outside");
		fs.mkdirSync(repository, { recursive: true });
		git(repository, ["init"]);
		git(repository, ["config", "user.email", "test@example.com"]);
		git(repository, ["config", "user.name", "Test User"]);
		fs.writeFileSync(path.join(repository, "tracked.txt"), "base\n", "utf-8");
		git(repository, ["add", "tracked.txt"]);
		git(repository, ["commit", "-m", "base"]);
		fs.mkdirSync(path.dirname(worktree), { recursive: true });
		git(repository, ["worktree", "add", "-b", "feature", worktree]);
		const outsideConfig = path.join(outside, ".pi");
		fs.mkdirSync(outsideConfig, { recursive: true });
		fs.writeFileSync(path.join(outside, ".git"), `gitdir: ${path.join(repository, ".git")}\n`, "utf-8");
		fs.symlinkSync(outsideConfig, path.join(worktree, ".pi"), process.platform === "win32" ? "junction" : "dir");

		const manager = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		assert.throws(() => manager.bindSession(context(worktree)), /resolves outside the real project/);
		assert.equal(fs.existsSync(path.join(outsideConfig, "subagents")), false);
	});
});

describe("quiet schedules", () => {
	const script = "return runs.run('main', { agent: 'worker', task: 'Maintain backlog' })";

	it("persists quiet only on recurring create and applies it to automatic fires", async () => {
		const h = harness();
		const created = await h.manager.handleToolCall({ action: "schedule.create", id: "quiet-hourly", every: "1h", quiet: true, workflowScript: script }, h.ctx);
		assert.equal(detailRecords(created)[0]?.quiet, true);
		const onDisk = JSON.parse(fs.readFileSync(path.join(scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores")), "quiet-hourly", "schedule.json"), "utf-8")) as { quiet?: unknown };
		assert.equal(onDisk.quiet, true);

		h.clock.now += 3_600_000;
		h.timers.fireAll();
		assert.deepEqual(h.launches[0]?.params.scheduleOrigin, { id: "quiet-hourly", name: "workflow -> agent worker", quiet: true });
	});

	it("rejects a non-boolean quiet value", async () => {
		const h = harness();
		const rejected = await h.manager.handleToolCall({ action: "schedule.create", id: "bad-quiet", every: "1h", quiet: "yes" as unknown as boolean, workflowScript: script }, h.ctx);
		assert.equal(rejected.isError, true);
		assert.match(text(rejected), /quiet must be a boolean/);
	});

	it("keeps one-shot at schedules noisy", async () => {
		const h = harness();
		const rejected = await h.manager.handleToolCall({ action: "schedule.create", id: "quiet-once", at: "+10m", quiet: true, workflowScript: script }, h.ctx);
		assert.equal(rejected.isError, true);
		assert.match(text(rejected), /quiet is only supported for recurring schedules/);

		const created = await h.manager.handleToolCall({ action: "schedule.create", id: "once", at: "+10m", workflowScript: script }, h.ctx);
		assert.equal("quiet" in detailRecords(created)[0]!, false);
		h.clock.now += 10 * 60_000;
		h.timers.fireAll();
		await flush();
		assert.equal("quiet" in (h.launches[0]?.params.scheduleOrigin as Record<string, unknown>), false);
	});

	it("keeps schedule.run noisy unless that launch asks for quiet", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "quiet-hourly", every: "1h", quiet: true, workflowScript: script }, h.ctx);

		const manual = h.manager.handleToolCall({ action: "schedule.run", id: "quiet-hourly" }, h.ctx);
		await flush();
		assert.equal("quiet" in (h.launches[0]?.params.scheduleOrigin as Record<string, unknown>), false);
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "manual-loud" } });
		await manual;
		h.manager.handleAsyncCompletion({ runId: "manual-loud", success: true, summary: "Done" });

		const explicit = h.manager.handleToolCall({ action: "schedule.run", id: "quiet-hourly", quiet: true }, h.ctx);
		await flush();
		assert.deepEqual(h.launches[1]?.params.scheduleOrigin, { id: "quiet-hourly", name: "workflow -> agent worker", quiet: true });
		h.launches[1]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "manual-quiet" } });
		await explicit;
	});
});

describe("recurring schedule execution", () => {
	it("launches a fixed interval from its planned time and records durable history/events", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "hourly", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker', task: 'Maintain backlog' })" }, h.ctx);
		h.clock.now += 3_600_000;
		h.timers.fireAll();
		assert.equal(h.launches.length, 1);
		assert.deepEqual(h.launches[0]?.params, { workflowScript: "return runs.run('main', { agent: 'worker', task: 'Maintain backlog' })", args: {}, async: true, context: "fresh", cwd: h.ctx.cwd, mission: false, scheduleOrigin: { id: "hourly", name: "workflow -> agent worker" } });
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async worker" }], details: { mode: "single", results: [], asyncId: "async-1", asyncDir: "/tmp/async-1" } });
		await flush();
		assert.deepEqual([...h.manager.observedCompletionRunIds()], ["async-1"]);

		let history = await h.manager.handleToolCall({ action: "schedule.history", id: "hourly" }, h.ctx);
		assert.match(text(history), /running.*async async-1/);
		const scheduleRoot = scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores"));
		const dir = path.join(scheduleRoot, "hourly");
		assert.equal(fs.existsSync(path.join(dir, "history.json")), true);
		assert.equal(fs.existsSync(path.join(dir, "events.jsonl")), true);
		assert.equal(fs.readdirSync(path.join(dir, "runs")).length, 1);

		h.manager.handleAsyncCompletion({ runId: "async-1", success: true, summary: "Done" });
		assert.deepEqual([...h.manager.observedCompletionRunIds()], []);
		history = await h.manager.handleToolCall({ action: "schedule.history", id: "hourly" }, h.ctx);
		assert.match(text(history), /completed.*async async-1/);
		assert.equal(fs.existsSync(path.join(dir, "active.lock")), false);
		const shown = await h.manager.handleToolCall({ action: "schedule.show", id: "hourly" }, h.ctx);
		assert.match(text(shown), /2030-01-01T02:00:00.000Z/, "next occurrence advances from the planned time without completion drift");
	});

	it("delays retrying an overdue recurring schedule after an unexpected timer failure", async () => {
		const h = harness({ randomId: () => { throw new Error("persistent id failure"); } });
		await h.manager.handleToolCall({ action: "schedule.create", id: "hourly", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		h.clock.now += 3_600_000;
		const warnings: string[] = [];
		const originalWarn = console.warn;
		console.warn = (message?: unknown) => void warnings.push(String(message));
		try {
			h.timers.fireAll();
			await flush();
		} finally {
			console.warn = originalWarn;
		}

		assert.equal(h.launches.length, 0);
		assert.equal(h.timers.values.size, 1);
		assert.equal([...h.timers.values.values()][0]?.delay, 3_600_000);
		assert.match(warnings[0] ?? "", /failed to fire: persistent id failure/);
		const shown = await h.manager.handleToolCall({ action: "schedule.show", id: "hourly" }, h.ctx);
		assert.match(text(shown), /State: scheduled/);
		assert.match(text(shown), /2030-01-01T01:00:00.000Z/);
	});

	it("re-arms overdue catch-up-none schedules when missed-run recovery fails", async () => {
		const h = harness({ randomId: () => { throw new Error("persistent id failure"); } });
		await h.manager.handleToolCall({ action: "schedule.create", id: "none", every: "1h", catchUp: "none", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		h.clock.now += 2 * 3_600_000;
		const warnings: string[] = [];
		const originalWarn = console.warn;
		console.warn = (message?: unknown) => void warnings.push(String(message));
		try {
			h.timers.fireAll();
			await flush();
		} finally {
			console.warn = originalWarn;
		}

		assert.equal(h.launches.length, 0);
		assert.equal(h.timers.values.size, 1);
		assert.equal([...h.timers.values.values()][0]?.delay, 3_600_000);
		assert.match(warnings[0] ?? "", /failed to fire: persistent id failure/);
		assert.match(warnings[1] ?? "", /could not be restored after fire failure: persistent id failure/);
		const shown = await h.manager.handleToolCall({ action: "schedule.show", id: "none" }, h.ctx);
		assert.match(text(shown), /2030-01-01T01:00:00.000Z/);
	});

	it("does not immediately retry an overdue one-shot after an unexpected timer failure", async () => {
		const h = harness({ randomId: () => { throw new Error("persistent id failure"); } });
		await h.manager.handleToolCall({ action: "schedule.create", id: "once", at: "+1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		h.clock.now += 3_600_000;
		const warnings: string[] = [];
		const originalWarn = console.warn;
		console.warn = (message?: unknown) => void warnings.push(String(message));
		try {
			h.timers.fireAll();
			await flush();
		} finally {
			console.warn = originalWarn;
		}

		assert.equal(h.launches.length, 0);
		assert.equal(h.timers.values.size, 0);
		assert.match(warnings[0] ?? "", /failed to fire: persistent id failure/);
		const shown = await h.manager.handleToolCall({ action: "schedule.show", id: "once" }, h.ctx);
		assert.match(text(shown), /State: scheduled/);
		assert.match(text(shown), /2030-01-01T01:00:00.000Z/);
	});

	it("run-due launches the latest missed occurrence while catchUp none records a miss", async () => {
		const latest = harness();
		await latest.manager.handleToolCall({ action: "schedule.create", id: "latest", every: "1h", catchUp: "latest", workflowScript: "return runs.run('main', { agent: 'worker' })" }, latest.ctx);
		latest.manager.stop();
		latest.clock.now += 3 * 3_600_000;
		latest.manager.bindSession(latest.ctx);
		const duePromise = latest.manager.handleToolCall({ action: "schedule.run-due" }, latest.ctx);
		await flush();
		assert.equal(latest.launches.length, 1);
		latest.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "late-1" } });
		assert.match(text(await duePromise), /Processed 1 due schedule/);
		assert.match(text(await latest.manager.handleToolCall({ action: "schedule.history", id: "latest" }, latest.ctx)), /2030-01-01T03:00:00.000Z/, "latest catch-up selects the latest missed slot");

		const none = harness();
		await none.manager.handleToolCall({ action: "schedule.create", id: "none", every: "1h", catchUp: "none", workflowScript: "return runs.run('main', { agent: 'worker' })" }, none.ctx);
		none.manager.stop();
		none.clock.now += 3 * 3_600_000;
		none.manager.bindSession(none.ctx);
		assert.match(text(await none.manager.handleToolCall({ action: "schedule.history", id: "none" }, none.ctx)), /missed/);
		assert.equal(none.launches.length, 0);
	});

	it("keeps project timers, contexts, and completion ownership across session_start bindings", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "project-a", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);

		const projectB = path.join(h.root, "project-b");
		fs.mkdirSync(projectB);
		const projectBCtx = context(projectB, "session-b");
		const sourceSessionManager = h.ctx.sessionManager as { getSessionId(): string | null; getSessionFile(): string | null };
		const getSourceSessionId = sourceSessionManager.getSessionId;
		const getSourceSessionFile = sourceSessionManager.getSessionFile;
		sourceSessionManager.getSessionId = () => "session-b";
		sourceSessionManager.getSessionFile = () => path.join(projectB, "session-b.jsonl");
		h.manager.bindSession(projectBCtx);
		await h.manager.handleToolCall({ action: "schedule.create", id: "project-b", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, projectBCtx);
		assert.equal(h.timers.values.size, 2, "both project timers remain armed after session_start binds project B");

		h.clock.now += 3_600_000;
		h.timers.fireAll();
		await flush();
		assert.equal(h.launches.length, 2);
		assert.equal(h.launches[0]!.ctx.cwd, h.ctx.cwd);
		assert.equal(h.launches[0]!.ctx.sessionManager.getSessionId(), "session-a");
		assert.equal(h.launches[0]!.ctx.sessionManager.getSessionFile(), path.join(h.ctx.cwd, "session-a.jsonl"));
		assert.equal(h.launches[1]!.ctx.cwd, projectB);
		assert.equal(h.launches[1]!.ctx.sessionManager.getSessionId(), "session-b");
		assert.equal(h.launches[1]!.ctx.sessionManager.getSessionFile(), path.join(projectB, "session-b.jsonl"));
		sourceSessionManager.getSessionId = getSourceSessionId;
		sourceSessionManager.getSessionFile = getSourceSessionFile;
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "async-a" } });
		h.launches[1]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "async-b" } });
		await flush();

		h.manager.handleAsyncCompletion({ runId: "async-a", success: true });
		const history = await h.manager.handleToolCall({ action: "schedule.history", id: "project-a" }, h.ctx);
		assert.match(text(history), /completed.*async async-a/);
		const projectARoot = scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores"));
		assert.equal(fs.existsSync(path.join(projectARoot, "project-a", "active.lock")), false);
	});

	it("uses only a bound target session for an explicit cross-project cwd", async () => {
		const h = harness();
		const target = path.join(h.root, "explicit-target");
		fs.mkdirSync(target);
		const unbound = await h.manager.handleToolCall({ action: "schedule.create", id: "targeted", cwd: target, every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		assert.equal(unbound.isError, true);
		assert.match(text(unbound), /until that project has been opened/);

		const targetCtx = context(target, "target-session");
		h.manager.bindSession(targetCtx);
		h.manager.bindSession(h.ctx);
		const sessionOnly = await h.manager.handleToolCall({ action: "schedule.create", id: "session-only-targeted", cwd: target, every: "1h", sessionOnly: true, workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		assert.equal(sessionOnly.isError, true);
		assert.match(text(sessionOnly), /sessionOnly schedules cannot use an explicit cross-project cwd/);
		assert.deepEqual(listScheduledRunSummaries(target, path.join(h.root, "stores")), [], "rejected session-only schedules must not dead-end in the target project");

		await h.manager.handleToolCall({ action: "schedule.create", id: "targeted", cwd: target, every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		h.clock.now += 3_600_000;
		h.timers.fireAll();
		await flush();
		assert.equal(h.launches.length, 1);
		assert.equal(h.launches[0]!.ctx.cwd, target);
		assert.equal(h.launches[0]!.ctx.sessionManager.getSessionId(), "target-session");
		assert.equal(h.launches[0]!.ctx.sessionManager.getSessionFile(), path.join(target, "target-session.jsonl"));
		assert.equal(h.launches[0]!.params.cwd, target);
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "target-async" } });
		await flush();
	});

	it("reconciles owner completion after an unrelated store becomes malformed", async () => {
		const h = harness();
		const badProject = path.join(h.root, "bad-project");
		fs.mkdirSync(badProject);
		const badCtx = context(badProject, "bad-session");
		const ownerCtx = context(h.ctx.cwd, "owner-session");
		const timers = new FakeTimers();
		const launches: Launch[] = [];
		let id = 0;
		const manager = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "isolated-stores"),
			now: () => h.clock.now,
			randomId: () => `isolated-${++id}`,
			timers,
			launch: (params, launchCtx) => new Promise((resolve) => launches.push({ params: params as Record<string, unknown>, ctx: launchCtx, resolve: resolve as Launch["resolve"] })) as never,
		});
		manager.bindSession(badCtx);
		manager.bindSession(ownerCtx);
		await manager.handleToolCall({ action: "schedule.create", id: "owner", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, ownerCtx);
		const manual = manager.handleToolCall({ action: "schedule.run", id: "owner" }, ownerCtx);
		await flush();
		launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "owner-async" } });
		await manual;

		const badDir = path.join(scheduledRunStorePath(badProject, undefined, path.join(h.root, "isolated-stores")), "broken");
		fs.mkdirSync(badDir, { recursive: true });
		fs.writeFileSync(path.join(badDir, "schedule.json"), "{ bad", "utf-8");
		const ownerRoot = scheduledRunStorePath(ownerCtx.cwd, undefined, path.join(h.root, "isolated-stores"));
		const badSiblingDir = path.join(ownerRoot, "broken-sibling");
		fs.mkdirSync(badSiblingDir, { recursive: true });
		fs.writeFileSync(path.join(badSiblingDir, "schedule.json"), "{ bad", "utf-8");
		const originalError = console.error;
		console.error = () => {};
		try {
			manager.handleAsyncCompletion({ runId: "owner-async", success: true });
		} finally {
			console.error = originalError;
		}
		const history = await manager.handleToolCall({ action: "schedule.history", id: "owner" }, ownerCtx);
		assert.match(text(history), /completed.*async owner-async/);
		assert.equal(fs.existsSync(path.join(ownerRoot, "owner", "active.lock")), false);
	});

	it("reconciles a terminal async status after a new session binds", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "restart", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		h.clock.now += 3_600_000;
		h.timers.fireAll();
		const asyncDir = path.join(h.root, "async-restart");
		fs.mkdirSync(asyncDir);
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "async-restart", asyncDir } });
		await flush();
		h.manager.stop();
		fs.writeFileSync(path.join(asyncDir, "status.json"), JSON.stringify({ runId: "async-restart", mode: "single", state: "complete", startedAt: h.clock.now, endedAt: h.clock.now }), "utf-8");

		const next = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "stores"),
			now: () => h.clock.now,
			randomId: () => "reconciled-id",
			timers: new FakeTimers(),
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		const nextCtx = context(h.ctx.cwd, "session-b");
		next.bindSession(nextCtx);
		const history = await next.handleToolCall({ action: "schedule.history", id: "restart" }, nextCtx);
		assert.match(text(history), /completed.*async async-restart/);
	});

	it("records elapsed overlap and catch-up-none slots without an immediate rerun", async () => {
		const latest = harness();
		await latest.manager.handleToolCall({ action: "schedule.create", id: "overlap", every: "1h", catchUp: "latest", workflowScript: "return runs.run('main', { agent: 'worker' })" }, latest.ctx);
		latest.clock.now += 3_600_000;
		latest.timers.fireAll();
		latest.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "long-run" } });
		await flush();
		latest.clock.now += 3 * 3_600_000;
		latest.timers.fireAll();
		assert.equal(latest.launches.length, 1, "an overlapping occurrence is skipped instead of queued");
		latest.manager.handleAsyncCompletion({ runId: "long-run", success: true });
		const latestHistory = await latest.manager.handleToolCall({ action: "schedule.history", id: "overlap" }, latest.ctx);
		assert.match(text(latestHistory), /skipped/);
		assert.match(text(latestHistory), /completed.*async long-run/);
		assert.match(text(await latest.manager.handleToolCall({ action: "schedule.show", id: "overlap" }, latest.ctx)), /2030-01-01T05:00:00.000Z/);

		const none = harness();
		await none.manager.handleToolCall({ action: "schedule.create", id: "none-overlap", every: "1h", catchUp: "none", workflowScript: "return runs.run('main', { agent: 'worker' })" }, none.ctx);
		none.clock.now += 3_600_000;
		none.timers.fireAll();
		none.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "none-long-run" } });
		await flush();
		none.clock.now += 3 * 3_600_000;
		none.timers.fireAll();
		assert.equal(none.launches.length, 1);
		none.manager.handleAsyncCompletion({ runId: "none-long-run", success: true });
		assert.match(text(await none.manager.handleToolCall({ action: "schedule.history", id: "none-overlap" }, none.ctx)), /skipped/);
		assert.match(text(await none.manager.handleToolCall({ action: "schedule.show", id: "none-overlap" }, none.ctx)), /2030-01-01T05:00:00.000Z/);
	});

	it("manual run uses the normal async target and overlap skip prevents a second launch", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "manual", every: "1h", workflowScript: "return 1" }, h.ctx);
		const firstPromise = h.manager.handleToolCall({ action: "schedule.run", id: "manual" }, h.ctx);
		await flush();
		assert.equal(h.launches.length, 1);
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "workflow", results: [], asyncId: "workflow-1" } });
		assert.match(text(await firstPromise), /async workflow-1/);
		assert.deepEqual([...h.manager.referencedAsyncRunIds()], ["workflow-1"]);
		const second = await h.manager.handleToolCall({ action: "schedule.run", id: "manual" }, h.ctx);
		assert.match(text(second), /skipped/);
		assert.equal(h.launches.length, 1);
	});

	it("a successful manual launch satisfies the next natural fire", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "manual-interval", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		h.clock.now += 5 * 60_000;
		const manual = h.manager.handleToolCall({ action: "schedule.run", id: "manual-interval" }, h.ctx);
		await flush();
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "manual-async" } });
		await manual;
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.show", id: "manual-interval" }, h.ctx)), /Next: 2030-01-01T01:05:00.000Z/);
		h.manager.handleAsyncCompletion({ runId: "manual-async", success: true });
		h.clock.now = Date.parse("2030-01-01T01:00:00.000Z");
		h.timers.fireAll();
		await flush();
		assert.equal(h.launches.length, 1);
	});

	it("a failed manual launch still honors a natural fire that overlapped while attachment was pending", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "overlap-fail", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		const manual = h.manager.handleToolCall({ action: "schedule.run", id: "overlap-fail" }, h.ctx);
		await flush();
		h.clock.now += 3_600_000;
		h.timers.fireAll();
		await flush();
		assert.equal(h.launches.length, 1);
		h.launches[0]!.resolve({ content: [{ type: "text", text: "spawn failed" }], details: { mode: "management", results: [] }, isError: true });
		assert.match(text(await manual), /failed_launch/);
		assert.match(text(await h.manager.handleToolCall({ action: "schedule.show", id: "overlap-fail" }, h.ctx)), /Next: 2030-01-01T01:00:00.000Z/);
		h.timers.fireAll();
		await flush();
		assert.equal(h.launches.length, 2);
	});

	it("distinguishes failed launch from failed async completion", async () => {
		const h = harness();
		await h.manager.handleToolCall({ action: "schedule.create", id: "failures", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, h.ctx);
		const first = h.manager.handleToolCall({ action: "schedule.run", id: "failures" }, h.ctx);
		await flush();
		h.launches[0]!.resolve({ content: [{ type: "text", text: "spawn failed" }], details: { mode: "management", results: [] }, isError: true });
		assert.match(text(await first), /failed_launch/);

		const second = h.manager.handleToolCall({ action: "schedule.run", id: "failures" }, h.ctx);
		await flush();
		h.launches[1]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "single", results: [], asyncId: "async-fail" } });
		await second;
		h.manager.handleAsyncCompletion({ id: "async-fail", success: false, summary: "child failed" });
		assert.deepEqual([...h.manager.referencedAsyncRunIds()], ["async-fail"]);
		const history = await h.manager.handleToolCall({ action: "schedule.history", id: "failures" }, h.ctx);
		assert.match(text(history), /failed_run.*async async-fail/);
		assert.match(text(history), /failed_launch/);
	});
});

describe("active runs whose async runner died", () => {
	const DEAD_PID = 4242;
	const LIVE_PID = 4343;
	const kill = (pid: number) => {
		if (pid === LIVE_PID) return true;
		throw Object.assign(new Error("no such process"), { code: "ESRCH" });
	};
	const script = "return runs.run('main', { agent: 'monitor' })";

	function scheduleDir(h: Harness, id: string): string {
		return path.join(scheduledRunStorePath(h.ctx.cwd, undefined, path.join(h.root, "stores")), id);
	}

	function events(h: Harness, id: string): Array<{ event: string; runId?: string }> {
		return fs.readFileSync(path.join(scheduleDir(h, id), "events.jsonl"), "utf-8").trim().split("\n").map((line) => JSON.parse(line));
	}

	// The first fire attaches a running workflow whose status names the given runner PID.
	async function attach(h: Harness, id: string, pid: number, create: Record<string, unknown> = { every: "1h" }): Promise<{ runId: string; asyncDir: string }> {
		const created = await h.manager.handleToolCall({ action: "schedule.create", id, workflowScript: script, ...create }, h.ctx);
		assert.equal(created.isError, undefined, text(created));
		h.clock.now = nextRun(h, id);
		h.timers.fireAll();
		const asyncDir = path.join(h.root, `async-${id}`);
		fs.mkdirSync(asyncDir);
		fs.writeFileSync(path.join(asyncDir, "status.json"), JSON.stringify({ runId: `async-${id}`, mode: "workflow", state: "running", startedAt: h.clock.now, lastUpdate: h.clock.now, pid }), "utf-8");
		h.launches[0]!.resolve({ content: [{ type: "text", text: "Async" }], details: { mode: "workflow", results: [], asyncId: `async-${id}`, asyncDir } });
		await flush();
		const runId = JSON.parse(fs.readFileSync(path.join(scheduleDir(h, id), "schedule.json"), "utf-8")).activeRunId as string;
		assert.ok(runId);
		return { runId, asyncDir };
	}

	function nextRun(h: Harness, id: string): number {
		return Date.parse(JSON.parse(fs.readFileSync(path.join(scheduleDir(h, id), "schedule.json"), "utf-8")).trigger.nextRunAt);
	}

	function receipt(h: Harness, id: string, runId: string): Record<string, unknown> {
		return JSON.parse(fs.readFileSync(path.join(scheduleDir(h, id), "runs", `${runId}.json`), "utf-8"));
	}

	it("fails a dead runner's run on the next fire and launches that fire instead of skipping it", async () => {
		const h = harness({ kill });
		const { runId, asyncDir } = await attach(h, "dead", DEAD_PID);
		h.clock.now = nextRun(h, "dead");
		h.timers.fireAll();
		await flush();

		assert.equal(h.launches.length, 2, "the due fire launches a fresh run");
		assert.equal(receipt(h, "dead", runId).state, "failed_run");
		assert.match(String(receipt(h, "dead", runId).error), /Async runner process 4242 exited or disappeared/);
		assert.equal(JSON.parse(fs.readFileSync(path.join(asyncDir, "status.json"), "utf-8")).state, "failed");
		const schedule = JSON.parse(fs.readFileSync(path.join(scheduleDir(h, "dead"), "schedule.json"), "utf-8"));
		assert.notEqual(schedule.activeRunId, runId);
		assert.equal(fs.readFileSync(path.join(scheduleDir(h, "dead"), "active.lock"), "utf-8"), schedule.activeRunId);
		assert.equal(schedule.trigger.nextRunAt, "2030-01-01T03:00:00.000Z", "the fire advances the interval once");
		assert.deepEqual(events(h, "dead").filter((event) => event.event === "schedule.skipped_overlap"), []);
		assert.equal(events(h, "dead").filter((event) => event.event === "schedule.run.started" && event.runId === schedule.activeRunId).length, 1);
	});

	it("releases a dead runner's claim when another session restores the schedule", async () => {
		const h = harness({ kill });
		const { runId } = await attach(h, "restore", DEAD_PID);
		h.manager.stop();
		h.clock.now += 60_000;
		const timers = new FakeTimers();
		const next = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "stores"),
			now: () => h.clock.now,
			timers,
			kill,
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
		next.bindSession(context(h.ctx.cwd, "session-b"));

		assert.equal(receipt(h, "restore", runId).state, "failed_run");
		assert.equal(JSON.parse(fs.readFileSync(path.join(scheduleDir(h, "restore"), "schedule.json"), "utf-8")).activeRunId, undefined);
		assert.equal(fs.existsSync(path.join(scheduleDir(h, "restore"), "active.lock")), false);
		assert.equal(timers.values.size, 1, "the released schedule is armed for its next fire");
	});

	it("keeps skipping while the runner is alive and its status is fresh", async () => {
		const h = harness({ kill });
		const { runId, asyncDir } = await attach(h, "live", LIVE_PID);
		const lock = fs.readFileSync(path.join(scheduleDir(h, "live"), "active.lock"));
		const status = fs.readFileSync(path.join(asyncDir, "status.json"));
		h.clock.now = nextRun(h, "live");
		h.timers.fireAll();
		await flush();

		assert.equal(h.launches.length, 1);
		assert.equal(receipt(h, "live", runId).state, "running");
		assert.deepEqual(fs.readFileSync(path.join(scheduleDir(h, "live"), "active.lock")), lock);
		assert.deepEqual(fs.readFileSync(path.join(asyncDir, "status.json")), status);
		assert.equal(events(h, "live").filter((event) => event.event === "schedule.skipped_overlap").length, 1);
	});

	it("finds the active run by its receipt after it has left history", async () => {
		const h = harness({ kill });
		const { runId } = await attach(h, "trimmed", DEAD_PID);
		fs.writeFileSync(path.join(scheduleDir(h, "trimmed"), "history.json"), JSON.stringify({ schemaVersion: 1, runs: [] }), "utf-8");
		h.clock.now = nextRun(h, "trimmed");
		h.timers.fireAll();
		await flush();

		assert.equal(h.launches.length, 2);
		assert.equal(receipt(h, "trimmed", runId).state, "failed_run");
	});

	it("keeps a lock another session took after releasing the dead run", async () => {
		const h = harness({ kill });
		const { runId } = await attach(h, "raced", DEAD_PID);
		fs.writeFileSync(path.join(scheduleDir(h, "raced"), "active.lock"), "other-run", "utf-8");
		h.clock.now = nextRun(h, "raced");
		h.timers.fireAll();
		await flush();

		assert.equal(h.launches.length, 1, "the other session's claim wins this fire");
		assert.equal(receipt(h, "raced", runId).state, "failed_run");
		assert.equal(fs.readFileSync(path.join(scheduleDir(h, "raced"), "active.lock"), "utf-8"), "other-run");
	});

	it("keeps an overdue occurrence when a restore releases a dead runner's claim", async () => {
		const h = harness({ kill });
		const { runId } = await attach(h, "overdue", DEAD_PID, { every: "1h", catchUp: "latest" });
		h.manager.stop();
		h.clock.now = nextRun(h, "overdue") + 60_000;
		const timers = new FakeTimers();
		let launched = 0;
		const next = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "stores"),
			now: () => h.clock.now,
			timers,
			kill,
			launch: () => { launched++; return new Promise(() => {}); },
		});
		next.bindSession(context(h.ctx.cwd, "session-b"));

		assert.equal(receipt(h, "overdue", runId).state, "failed_run");
		assert.deepEqual(events(h, "overdue").filter((event) => event.event === "schedule.skipped_overlap"), []);
		timers.fireAll();
		await flush();
		assert.equal(launched, 1, "the overdue occurrence fires after the restore");
	});

	it("arms a restored schedule when recording its released dead run fails", async () => {
		const h = harness({ kill });
		await attach(h, "locked", DEAD_PID);
		h.manager.stop();
		h.clock.now += 60_000;
		// Another process holds the history.json lease, so recording the released run times out.
		const lease = path.join(fs.realpathSync(scheduleDir(h, "locked")), "history.json.write-lock");
		fs.mkdirSync(lease);
		fs.writeFileSync(path.join(lease, "owner.json"), JSON.stringify({ token: "other-session", pid: process.pid, hostname: os.hostname() }));
		const timers = new FakeTimers();
		const next = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "stores"),
			now: () => h.clock.now,
			timers,
			kill,
			launch: () => new Promise(() => {}),
		});

		assert.throws(() => next.bindSession(context(h.ctx.cwd, "session-b")), /Timed out waiting for another process/);
		assert.equal(JSON.parse(fs.readFileSync(path.join(scheduleDir(h, "locked"), "schedule.json"), "utf-8")).activeRunId, undefined);
		assert.equal(timers.values.size, 1, "the released schedule is armed despite the failed history write");
	});

	it("arms a restored schedule from disk and keeps the error when releasing its dead run cannot write the schedule", async () => {
		const h = harness({ kill });
		const { runId } = await attach(h, "full", DEAD_PID);
		h.manager.stop();
		h.clock.now += 60_000;
		const scheduleFile = path.join(fs.realpathSync(scheduleDir(h, "full")), "schedule.json");
		const originalRename = nodeFs.renameSync;
		nodeFs.renameSync = ((from: fs.PathLike, to: fs.PathLike) => {
			if (path.join(fs.realpathSync(path.dirname(String(to))), path.basename(String(to))) === scheduleFile) throw Object.assign(new Error("schedule write ENOSPC"), { code: "ENOSPC" });
			return originalRename(from, to);
		}) as typeof fs.renameSync;
		syncBuiltinESMExports();
		const timers = new FakeTimers();
		let launched = 0;
		const next = createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: path.join(h.root, "stores"),
			now: () => h.clock.now,
			timers,
			kill,
			launch: () => { launched++; return new Promise(() => {}); },
		});
		try {
			assert.throws(() => next.bindSession(context(h.ctx.cwd, "session-b")), (error: NodeJS.ErrnoException) => error.code === "ENOSPC");
		} finally {
			nodeFs.renameSync = originalRename;
			syncBuiltinESMExports();
		}
		assert.equal(JSON.parse(fs.readFileSync(scheduleFile, "utf-8")).activeRunId, runId);
		assert.equal(timers.values.size, 1, "the still-claimed schedule is armed so its next fire retries the release");

		h.clock.now = nextRun(h, "full");
		timers.fireAll();
		await flush();
		assert.equal(receipt(h, "full", runId).state, "failed_run");
		assert.equal(launched, 1);
	});

	it("leaves another session's newer claim alone when it released the dead run first", async () => {
		let h: Harness;
		const otherSessionClaims = (pid: number) => {
			// Session A releases the dead run and claims this occurrence while this fire reconciles it.
			const file = path.join(scheduleDir(h, "shared"), "schedule.json");
			fs.writeFileSync(file, JSON.stringify({ ...JSON.parse(fs.readFileSync(file, "utf-8")), activeRunId: "run-a" }), "utf-8");
			fs.writeFileSync(path.join(scheduleDir(h, "shared"), "active.lock"), "run-a", "utf-8");
			return kill(pid);
		};
		h = harness({ kill: otherSessionClaims });
		const { runId } = await attach(h, "shared", DEAD_PID);
		h.clock.now = nextRun(h, "shared");
		h.timers.fireAll();
		await flush();

		assert.equal(h.launches.length, 1, "this session launches nothing");
		assert.equal(JSON.parse(fs.readFileSync(path.join(scheduleDir(h, "shared"), "schedule.json"), "utf-8")).activeRunId, "run-a");
		assert.equal(fs.readFileSync(path.join(scheduleDir(h, "shared"), "active.lock"), "utf-8"), "run-a");
		assert.equal(receipt(h, "shared", runId).state, "running", "the session that released the run records its failure");
		assert.equal(h.timers.values.size, 1, "the schedule stays armed");
	});

	it("advances a calendar schedule once when the recovered fire launches", async () => {
		const h = harness({ kill });
		const { runId } = await attach(h, "calendar", DEAD_PID, { every: "day", at: "09:00", timezone: "Asia/Taipei" });
		assert.equal(new Date(h.clock.now).toISOString(), "2030-01-01T01:00:00.000Z");
		h.clock.now = nextRun(h, "calendar");
		h.timers.fireAll();
		await flush();

		assert.equal(h.launches.length, 2);
		assert.equal(receipt(h, "calendar", runId).state, "failed_run");
		const trigger = JSON.parse(fs.readFileSync(path.join(scheduleDir(h, "calendar"), "schedule.json"), "utf-8")).trigger;
		assert.equal(trigger.nextLocalDate, "2030-01-03");
		assert.equal(trigger.nextRunAt, "2030-01-03T01:00:00.000Z");
		assert.equal(events(h, "calendar").filter((event) => event.event === "schedule.run.started").length, 2, "one started event per launched fire");
		assert.deepEqual(events(h, "calendar").filter((event) => event.event === "schedule.skipped_overlap"), []);
	});
});

describe("a schedule record the store cannot read", () => {
	function storeRoot(h: Harness): string {
		return path.join(h.root, "stores");
	}
	function recordFile(h: Harness, id: string): string {
		return path.join(scheduledRunStorePath(h.ctx.cwd, undefined, storeRoot(h)), id, "schedule.json");
	}
	function manager(h: Harness, reportWarning?: (message: string, key: string) => void): ScheduledRunManager {
		return createScheduledRunManager({
			config: { scheduledRuns: { enabled: true } },
			storeRoot: storeRoot(h),
			now: () => h.clock.now,
			randomId: () => "generated",
			timers: h.timers,
			reportWarning,
			launch: async () => ({ content: [{ type: "text", text: "unused" }], details: { mode: "management", results: [] } }),
		});
	}
	/** A record paused the way an operator pauses it by editing the file: no next run. */
	function writePausedRecord(h: Harness, id: string, nextRunAt?: null): void {
		const file = recordFile(h, id);
		fs.mkdirSync(path.dirname(file), { recursive: true });
		fs.writeFileSync(file, JSON.stringify({
			schemaVersion: 1,
			id,
			name: "Encryption watch",
			cwd: h.ctx.cwd,
			trigger: { kind: "interval", every: "15m", everyMs: 900_000, anchorAt: new Date(h.clock.now).toISOString(), nextRunAt },
			target: { workflowScript: "return runs.run('main', { agent: 'worker' })", args: {} },
			overlap: "skip",
			catchUp: "latest",
			paused: true,
			createdAt: new Date(h.clock.now).toISOString(),
			updatedAt: new Date(h.clock.now).toISOString(),
		}), "utf-8");
	}

	it("loads a paused interval record with no next run, then arms it on resume", async () => {
		const h = harness();
		writePausedRecord(h, "paused-watch");
		const ctx = context(h.ctx.cwd, "session-b");
		const bound = manager(h);
		bound.bindSession(ctx);
		assert.match(text(await bound.handleToolCall({ action: "schedule.list" }, ctx)), /paused-watch \| paused \| no next run/);
		const resumed = await bound.handleToolCall({ action: "schedule.resume", id: "paused-watch" }, ctx);
		assert.equal(resumed.isError, undefined, text(resumed));
		const expected = new Date(h.clock.now + 900_000).toISOString();
		assert.match(text(await bound.handleToolCall({ action: "schedule.show", id: "paused-watch" }, ctx)), new RegExp(`Next: ${expected}`));
		assert.equal(h.timers.values.size, 1, "a resumed record is armed");
	});

	it("still rejects a paused interval record whose next run is present but not a time", () => {
		const h = harness();
		writePausedRecord(h, "null-watch", null);
		assert.throws(() => manager(h).bindSession(context(h.ctx.cwd, "session-b")), /invalid interval trigger/);
	});

	it("reports a record that stops parsing once, not on every async completion", async () => {
		const h = harness();
		const warnings = new Map<string, string>();
		const ctx = context(h.ctx.cwd, "session-b");
		const bound = manager(h, (message, key) => { if (!warnings.has(key)) warnings.set(key, message); });
		bound.bindSession(ctx);
		const created = await bound.handleToolCall({ action: "schedule.create", id: "watch", every: "1h", workflowScript: "return runs.run('main', { agent: 'worker' })" }, ctx);
		assert.equal(created.isError, undefined, text(created));
		const file = recordFile(h, "watch");
		fs.writeFileSync(file, JSON.stringify({ ...JSON.parse(fs.readFileSync(file, "utf-8")), target: { agent: "worker", task: "Review" } }), "utf-8");

		for (let i = 0; i < 3; i++) bound.handleAsyncCompletion({ runId: "unknown-run" });

		assert.deepEqual([...warnings.values()].map((message) => message.replace(/: .*/, "")), [`Failed to inspect schedule 'watch' in '${scheduledRunStorePath(h.ctx.cwd, undefined, storeRoot(h))}'`]);
	});
});
