/**
 * Regression and lifecycle tests for goal-level provider-error recovery.
 *
 * Regression seed (user report): a 503 `server_error` payload of the shape
 *
 *     Error: 503: {"type":"server_error","message":"Error from provider
 *     (Console): Upstream request failed: Endpoint is unavailable."}
 *     Error: Retry failed after 3 attempts: 503: {...}
 *
 * was NOT classified as a network error by isNetworkErrorAssistantMessage
 * (which only matched the literal text "network error"), so Pi's built-in
 * retries exhausted and no goal-level backoff ever engaged.
 */

import assert from "node:assert/strict";
import test from "node:test";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";

import type { ExtensionContext } from "@earendil-works/pi-coding-agent";

import {
	createGoal,
	goalFocusDetails,
	type GoalRecord,
	type GoalStateEntry,
} from "../extensions/goal-record.ts";
import {
	hasNetworkErrorAssistantMessage,
	isNetworkErrorAssistantMessage,
} from "../extensions/goal-format.ts";
import { createExtensionHarness } from "./support/extension-harness.ts";
import { writeProjectSettings } from "./support/settings.ts";
import { tempDir } from "./support/fs.ts";

const FIXTURE_GOAL = readFileSync(
	new URL("./fixtures/goals/active_goal_fixture.md", import.meta.url),
	"utf8",
);

/** The exact reported failure, as Pi surfaces it in errorMessage. */
const REPORTED_503_MESSAGE =
	'503: {"type":"server_error","message":"Error from provider (Console): Upstream request failed: Endpoint is unavailable."}';
const REPORTED_503_AFTER_RETRIES = `Retry failed after 3 attempts: ${REPORTED_503_MESSAGE}`;
const REPORTED_HTTP2_PROTOCOL_ERROR =
	'Post "https://chatgpt.com/backend-api/codex/responses": stream error: stream ID 1; PROTOCOL_ERROR; received from peer';

// ── Host ─────────────────────────────────────────────────────────────────────

/** A host that looks busy, so continuation reschedules; see idleCtx() for the idle one. */
function createHarness(cwd: string, goal: GoalRecord) {
	return createExtensionHarness({
		cwd,
		sessionEntries: sessionEntriesFor(goal),
		isIdle: () => false,
		hasPendingMessages: () => true,
		systemPrompt: "",
	});
}

function fixtureCwd(): { cwd: string; goal: GoalRecord } {
	const cwd = tempDir("goal-network-recovery-");
	mkdirSync(path.join(cwd, ".pi", "goals", "archived"), { recursive: true });
	writeFileSync(path.join(cwd, ".pi", "goals", "active_goal_fixture.md"), FIXTURE_GOAL);
	const parsed = createGoal(
		{ objective: "Golden fixture goal objective", autoContinue: true, sisyphus: false },
		Date.UTC(2026, 7, 3, 9, 0, 0),
	);
	return { cwd, goal: { ...parsed, id: "golden_fixture_goal" } };
}

function sessionEntriesFor(goal: GoalRecord) {
	const stateEntry: GoalStateEntry = {
		version: 3,
		goal: {
			...goal,
			activePath: ".pi/goals/active_goal_fixture.md",
			usage: { tokensUsed: 0, activeSeconds: 0 },
			taskList: undefined,
			verificationContract: undefined,
		},
	};
	return [
		{ type: "custom", customType: "pi-goal-focus", data: goalFocusDetails(goal.id, "created") },
		{ type: "custom", customType: "pi-goal-state", data: stateEntry },
	];
}

/** Idle ctx: continuation fires immediately instead of rescheduling. */
type Harness = ReturnType<typeof createHarness>;

function idleCtx(ctx: ExtensionContext): ExtensionContext {
	return {
		...ctx,
		isIdle: () => true,
		hasPendingMessages: () => false,
	} as unknown as ExtensionContext;
}

async function countCheckpoints(h: Harness): Promise<number> {
	await new Promise((resolve) => setTimeout(resolve, 20));
	return h.sentMessages.filter(
		(m) => m.customType === "pi-goal-event" && (m.details as { kind?: string } | undefined)?.kind === "checkpoint",
	).length;
}

async function markGoalWork(h: Harness): Promise<void> {
	await h.emit("turn_start", {}, h.ctx);
	await h.emit("tool_call", { toolName: "bash", args: { command: "ls" } }, h.ctx);
	await h.emit("tool_execution_end", {}, h.ctx);
	assert.equal(h.core.scheduler.declare(h.ctx, { kind: "ready" }).terminate, true);
}

// ── Classification unit coverage ─────────────────────────────────────────────

test("classification: HTTP/2 stream PROTOCOL_ERROR is transient", () => {
	assert.equal(
		isNetworkErrorAssistantMessage({
			role: "assistant",
			stopReason: "error",
			errorMessage: REPORTED_HTTP2_PROTOCOL_ERROR,
		}),
		true,
		"the reported transport failure must engage goal-level recovery",
	);
});

test("classification: HTTP 499 from a gateway is transient (#125)", () => {
	assert.equal(isNetworkErrorAssistantMessage({ role: "assistant", stopReason: "error", errorMessage: "HTTP 499" }), true);
});

test("lifecycle: an error that cannot be retried pauses the goal with the provider's message (#125)", async () => {
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	await h.sessionStart();
	await h.emit("before_agent_start", { systemPrompt: "base", prompt: "user typed: continue", systemPromptOptions: {} }, h.ctx);
	await h.emit("agent_end", {
		messages: [{ role: "assistant", stopReason: "error", errorMessage: "400 invalid_request_error: messages.3 is malformed" }],
	}, idleCtx(h.ctx));
	assert.equal(h.core.state.goal?.status, "active", "Pi may still retry, so nothing is paused before it settles");
	await h.emit("agent_settled", {}, idleCtx(h.ctx));
	assert.equal(h.core.state.goal?.status, "paused");
	assert.match(h.core.state.goal?.pauseReason ?? "", /messages\.3 is malformed/, "the pause reason carries the provider's message");
	assert.match(lastNotification(h), /Goal paused\. Use \/goal-resume to try again/);
});

test("lifecycle: a later successful run cancels a pending error pause", async () => {
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	await h.sessionStart();
	await h.emit("before_agent_start", { systemPrompt: "base", prompt: "user typed: continue", systemPromptOptions: {} }, h.ctx);
	await h.emit("agent_end", { messages: [{ role: "assistant", stopReason: "error", errorMessage: "400 bad request" }] }, idleCtx(h.ctx));
	await h.emit("agent_end", { messages: [{ role: "assistant", stopReason: "stop", content: [] }] }, idleCtx(h.ctx));
	await h.emit("agent_settled", {}, idleCtx(h.ctx));
	assert.equal(h.core.state.goal?.status, "active", "Pi's own retry succeeded, so the goal keeps going");
});

test("classification: exact reported 503 server_error payload is a network error", () => {
	assert.equal(
		isNetworkErrorAssistantMessage({
			role: "assistant",
			stopReason: "error",
			errorMessage: REPORTED_503_MESSAGE,
		}),
		true,
		"the raw 503 server_error payload must classify as recoverable",
	);
	assert.equal(
		isNetworkErrorAssistantMessage({
			role: "assistant",
			stopReason: "error",
			errorMessage: REPORTED_503_AFTER_RETRIES,
		}),
		true,
		"the payload after Pi exhausts its own retries must still classify",
	);
	assert.equal(
		hasNetworkErrorAssistantMessage([
			{ role: "assistant", stopReason: "error", errorMessage: REPORTED_503_AFTER_RETRIES },
		]),
		true,
	);
});

test("classification: 429 rate-limit payloads are transient", () => {
	assert.equal(
		isNetworkErrorAssistantMessage({
			role: "assistant",
			stopReason: "error",
			errorMessage: '429 status code (no body)',
		}),
		true,
		"pi's compact 429 formatter must classify",
	);
	const reported = JSON.stringify({
		message: "Provider returned error",
		code: 429,
		metadata: {
			raw: "stealth/ox-alpha is temporarily rate-limited upstream. Please retry shortly.",
			provider_name: "Stealth",
			limit_source: "upstream_provider_shared_pool",
		},
	});
	assert.equal(
		isNetworkErrorAssistantMessage({
			role: "assistant",
			stopReason: "error",
			errorMessage: `Retry failed after 3 attempts: 429: ${reported}`,
		}),
		true,
		"the exact field-reported 429 payload must classify",
	);
	assert.equal(
		isNetworkErrorAssistantMessage({
			role: "assistant",
			stopReason: "error",
			errorMessage: "429: quota exceeded for this billing period",
		}),
		false,
		"quota/billing exhaustion stays non-transient even with a 429 code",
	);
});

test("classification: non-transient errors stay non-recoverable", () => {
	for (const message of [
		"401: {\"type\":\"authentication_error\",\"message\":\"invalid api key\"}",
		"400: invalid_request_error: malformed payload",
		"Provider finish_reason: content_filter",
	]) {
		assert.equal(
			isNetworkErrorAssistantMessage({ role: "assistant", stopReason: "error", errorMessage: message }),
			false,
			`must not classify as network error: ${message}`,
		);
	}
});

test("classification: generic provider finish_reason: error is transient", () => {
	assert.equal(
		isNetworkErrorAssistantMessage({
			role: "assistant",
			stopReason: "error",
			errorMessage: "Provider finish_reason: error",
		}),
		true,
		"the generic provider finish_reason: error must engage goal-level recovery",
	);
});

// ── Regression through the real agent_end → agent_settled lifecycle ─────────

test("regression: reported 503 payload schedules goal-level recovery after settle", async () => {
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	try {
		await h.sessionStart();
		await h.emit("before_agent_start", {
			systemPrompt: "base",
			prompt: "user typed: continue",
			systemPromptOptions: {},
		}, h.ctx);

		await h.emit("agent_end", {
			messages: [{ role: "assistant", stopReason: "error", errorMessage: REPORTED_503_AFTER_RETRIES }],
		}, idleCtx(h.ctx));
		assert.equal(await countCheckpoints(h), 0, "agent_end must not race Pi's built-in retries");

		await h.emit("agent_settled", {}, idleCtx(h.ctx));
		assert.match(
			h.notices.at(-1)?.message ?? "",
			/Retrying the goal in 5s/,
			"the reported 503 outage must engage the bounded goal-level recovery",
		);
		assert.equal(await countCheckpoints(h), 0, "the first recovery is delayed by the backoff policy");
	} finally {
		// The delayed timer is unref'd and needs no test teardown.
	}
});

test("regression: generic Provider finish_reason: error schedules goal-level recovery after settle", async () => {
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	try {
		await h.sessionStart();
		await h.emit("before_agent_start", {
			systemPrompt: "base",
			prompt: "user typed: continue",
			systemPromptOptions: {},
		}, h.ctx);

		await h.emit("agent_end", {
			messages: [{ role: "assistant", stopReason: "error", errorMessage: "Provider finish_reason: error" }],
		}, idleCtx(h.ctx));
		assert.equal(await countCheckpoints(h), 0, "agent_end must not race Pi's built-in retries");

		await h.emit("agent_settled", {}, idleCtx(h.ctx));
		assert.match(
			h.notices.at(-1)?.message ?? "",
			/Retrying the goal in 5s/,
			"the generic finish_reason: error must engage the bounded goal-level recovery",
		);
		assert.equal(await countCheckpoints(h), 0, "the first recovery is delayed by the backoff policy");
	} finally {
		// The delayed timer is unref'd and needs no test teardown.
	}
});

test("lifecycle: provider-initiated abort routes into recovery instead of pausing", async () => {
	process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS = "25";
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	try {
		await h.sessionStart();
		await h.emit("before_agent_start", {
			systemPrompt: "base",
			prompt: "user typed: continue",
			systemPromptOptions: {},
		}, h.ctx);

		await h.emit("agent_end", {
			messages: [{ role: "assistant", stopReason: "aborted", errorMessage: "Aborted after 3 retry attempts" }],
		}, idleCtx(h.ctx));
		await h.emit("agent_settled", {}, idleCtx(h.ctx));

		assert.match(
			h.notices.at(-1)?.message ?? "",
			/Retrying the goal in \d+s \(recovery 1, unbounded\)/,
			"a provider-initiated abort without a user abort signal must engage recovery",
		);
	} finally {
		delete process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS;
	}
});

test("lifecycle: full real event ordering (message_end → turn_end → agent_end → settled) routes provider-side aborts into recovery", async () => {
	// The auditor-verified gap: message_end and turn_end fire BEFORE agent_end
	// in real pi runs. All three must agree: without a user abort signal, an
	// aborted assistant message never pauses the goal and recovery engages.
	process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS = "25";
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	try {
		await h.sessionStart();
		await h.emit("before_agent_start", {
			systemPrompt: "base",
			prompt: "user typed: continue",
			systemPromptOptions: {},
		}, h.ctx);

		const abortedMessage = {
			role: "assistant",
			stopReason: "aborted",
			usage: { input: 0, output: 0 },
		};

		await h.emit("message_end", { message: abortedMessage }, idleCtx(h.ctx));
		assert.equal(
			h.notices.some((n) => /Goal paused/.test(n.message)),
			false,
			"message_end must not pause without a user abort signal",
		);
		await h.emit("turn_end", { message: abortedMessage }, idleCtx(h.ctx));
		assert.equal(
			h.notices.some((n) => /Goal paused/.test(n.message)),
			false,
			"turn_end must not pause without a user abort signal",
		);
		await h.emit("agent_end", { messages: [abortedMessage] }, idleCtx(h.ctx));
		await h.emit("agent_settled", {}, idleCtx(h.ctx));

		assert.match(
			h.notices.at(-1)?.message ?? "",
			/Retrying the goal in \d+s \(recovery 1, unbounded\)/,
			"recovery must engage through the full real event ordering",
		);
	} finally {
		delete process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS;
	}
});

test("lifecycle: genuine user abort still pauses the goal", async () => {
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	try {
		await h.sessionStart();
		await h.emit("before_agent_start", {
			systemPrompt: "base",
			prompt: "user typed: continue",
			systemPromptOptions: {},
		}, h.ctx);

		const signalCtx = {
			...idleCtx(h.ctx),
			signal: { aborted: true },
		} as unknown as ExtensionContext;
		await h.emit("agent_end", {
			messages: [{ role: "assistant", stopReason: "aborted", errorMessage: "Aborted after 2 retry attempts" }],
		}, signalCtx);

		assert.match(
			h.notices.at(-1)?.message ?? "",
			/Goal paused\./,
			"a user abort signal must keep pausing the goal",
		);
		assert.equal(
			h.notices.some((n) => /Retrying the goal/.test(n.message)),
			false,
			"user-initiated pauses never schedule recovery",
		);
	} finally {
		// nothing to clean
	}
});

// ── Full lifecycle: escalation, plateau, bounded override, reset ────────────

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

function lastNotification(h: Harness): string {
	return h.notices.at(-1)?.message ?? "";
}

test("lifecycle: unbounded recovery keeps retrying past the old 5-attempt cap", async () => {
	process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS = "25";
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	try {
		await h.sessionStart();
		await h.emit("before_agent_start", {
			systemPrompt: "base",
			prompt: "user typed: continue",
			systemPromptOptions: {},
		}, h.ctx);

		for (let attempt = 1; attempt <= 7; attempt++) {
			await h.emit("agent_end", {
				messages: [{ role: "assistant", stopReason: "error", errorMessage: REPORTED_503_MESSAGE }],
			}, idleCtx(h.ctx));
			await h.emit("agent_settled", {}, idleCtx(h.ctx));
			assert.match(
				lastNotification(h),
				new RegExp(`Retrying the goal in 0s \\(recovery ${attempt}, unbounded\\)`),
				`cycle ${attempt} must keep scheduling recovery`,
			);
			// Let the (25ms) recovery timer fire and clear before the next cycle.
			await sleep(80);
		}
	} finally {
		delete process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS;
	}
});

test("lifecycle: a configured bounded cap pauses the goal when recovery runs out", async () => {
	process.env.PI_GOAL_NETWORK_RECOVERY_MAX_ATTEMPTS = "2";
	process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS = "25";
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	try {
		await h.sessionStart();
		await h.emit("before_agent_start", {
			systemPrompt: "base",
			prompt: "user typed: continue",
			systemPromptOptions: {},
		}, h.ctx);

		for (let attempt = 1; attempt <= 2; attempt++) {
			await h.emit("agent_end", {
				messages: [{ role: "assistant", stopReason: "error", errorMessage: REPORTED_503_MESSAGE }],
			}, idleCtx(h.ctx));
			await h.emit("agent_settled", {}, idleCtx(h.ctx));
			assert.match(lastNotification(h), new RegExp(`recovery ${attempt}/2`), `bounded cycle ${attempt}`);
			await sleep(80);
		}

		const checkpointsBeforeExhaustion = await countCheckpoints(h);
		await h.emit("agent_end", {
			messages: [{ role: "assistant", stopReason: "error", errorMessage: REPORTED_503_MESSAGE }],
		}, idleCtx(h.ctx));
		await h.emit("agent_settled", {}, idleCtx(h.ctx));
		assert.match(
			lastNotification(h),
			/persisted after all recovery attempts\. Goal paused\. Use \/goal-resume/,
			"exhaustion must stop the loop with a resume hint",
		);
		assert.equal(h.core.state.goal?.status, "paused", "a goal that cannot continue must not stay active (#125)");
		assert.equal(await countCheckpoints(h), checkpointsBeforeExhaustion, "exhausted recovery must not deliver a continuation");
	} finally {
		delete process.env.PI_GOAL_NETWORK_RECOVERY_MAX_ATTEMPTS;
		delete process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS;
	}
});

test("lifecycle: a successful turn resets the recovery counter and clears pending backoff", async () => {
	process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS = "25";
	const { cwd, goal } = fixtureCwd();
	writeProjectSettings(cwd, { maxAutonomousRuns: 100 });
	const h = createHarness(cwd, goal);
	try {
		await h.sessionStart();
		await h.emit("before_agent_start", {
			systemPrompt: "base",
			prompt: "user typed: continue",
			systemPromptOptions: {},
		}, h.ctx);

		// Two failed cycles advance the counter.
		for (const _ of [1, 2]) {
			await h.emit("agent_end", {
				messages: [{ role: "assistant", stopReason: "error", errorMessage: REPORTED_503_MESSAGE }],
			}, idleCtx(h.ctx));
			await h.emit("agent_settled", {}, idleCtx(h.ctx));
			await sleep(80);
		}

		// A successful turn uses the normal continuation path.
		const checkpointsFromRecoveryTimers = await countCheckpoints(h);
		const notificationsBeforeSuccess = h.notices.length;
		await markGoalWork(h);
		await h.emit("agent_end", { messages: [{ role: "assistant", stopReason: "end_turn" }] }, idleCtx(h.ctx));
		await h.emit("agent_settled", {}, idleCtx(h.ctx));
		assert.equal(await countCheckpoints(h), checkpointsFromRecoveryTimers + 1, "success queues the normal continuation");
		assert.equal(h.notices.length, notificationsBeforeSuccess, "success announces no recovery");

		// The next failure starts over at recovery 1.
		await h.emit("agent_end", {
			messages: [{ role: "assistant", stopReason: "error", errorMessage: REPORTED_503_MESSAGE }],
		}, idleCtx(h.ctx));
		await h.emit("agent_settled", {}, idleCtx(h.ctx));
		assert.match(lastNotification(h), /\(recovery 1, unbounded\)/, "counter restarted");
	} finally {
		delete process.env.PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS;
	}
});
// ── Failed compaction ─────────────────────────────────────────────────────

test("session_compact_failed warns the user instead of failing silently", async () => {
	// The extension charges progress at `session_before_compact`, so a failure
	// must not leave the goal silently uncompacted — that is how a long-running
	// goal walks into the same context overflow on the next turn.
	const { cwd, goal } = fixtureCwd();
	const h = createHarness(cwd, goal);
	await h.sessionStart();
	await h.emit("before_agent_start", { systemPrompt: "base", prompt: "user typed: go", systemPromptOptions: {} }, h.ctx);

	const before = h.notices.length;
	await h.emit("session_compact_failed", {
		type: "session_compact_failed",
		reason: "threshold",
		errorMessage: "provider rejected the summarization request",
		aborted: false,
		willRetry: false,
		fromExtension: false,
	}, h.ctx);

	assert.equal(h.notices.length, before + 1, "a failed compaction is announced");
	const message = lastNotification(h);
	assert.match(message, /compaction failed/i, "the warning names the failure");
	assert.match(message, /threshold/, "the warning names the trigger reason");
	assert.match(message, /provider rejected the summarization request/, "the warning carries the error text");
	assert.match(message, /stays active/i, "the goal is not silently stopped");
});

for (const phase of ["startup", "success", "network-error"]) test(`lifecycle: settlement abort after ${phase} pauses without continuation or recovery`, async () => {
	const { cwd, goal } = fixtureCwd();
	const h = createHarness(cwd, goal);
	try {
		await h.sessionStart();
		await h.emit("before_agent_start", { systemPrompt: "base", prompt: "continue", systemPromptOptions: {} }, h.ctx);
		if (phase !== "startup") await h.emit("agent_end", { messages: phase === "network-error" ? [{ role: "assistant", stopReason: "error", errorMessage: REPORTED_503_MESSAGE }] : [] }, idleCtx(h.ctx));
		await h.emit("agent_settled", { aborted: true }, idleCtx(h.ctx));
		assert.equal(h.core.state.goal?.status, "paused");
		assert.equal(await countCheckpoints(h), 0);
		assert.ok(!h.notices.some(n => /Retrying the goal/.test(n.message)));
	} finally { h.core.scheduler.shutdown(); h.core.runtime.clearContinuationState(); }
});

test("lifecycle: an aborted settlement does not pause a replacement goal", async () => {
	const { cwd, goal } = fixtureCwd();
	const h = createHarness(cwd, goal);
	await h.sessionStart();
	await h.emit("before_agent_start", { systemPrompt: "base", prompt: "continue", systemPromptOptions: {} }, h.ctx);
	await h.emit("agent_end", { messages: [] }, idleCtx(h.ctx));
	h.core.state.goal = { ...goal, id: "replacement-goal", status: "active" };
	await h.emit("agent_settled", { aborted: true }, idleCtx(h.ctx));
	assert.equal(h.core.state.goal.status, "active");
	assert.equal(await countCheckpoints(h), 0);
	h.core.scheduler.shutdown(); h.core.runtime.clearContinuationState();
});

test("session_compact_failed stays silent when the user aborted or no goal is focused", async () => {
	// An abort is a user action, not a fault: warning about it would be noise.
	const { cwd, goal } = fixtureCwd();
	const h = createHarness(cwd, goal);
	await h.sessionStart();
	await h.emit("before_agent_start", { systemPrompt: "base", prompt: "go", systemPromptOptions: {} }, h.ctx);

	const before = h.notices.length;
	await h.emit("session_compact_failed", {
		type: "session_compact_failed",
		reason: "manual",
		aborted: true,
		willRetry: false,
		fromExtension: false,
	}, h.ctx);
	assert.equal(h.notices.length, before, "an aborted compaction produces no warning");
});
