/**
 * The goal text lives in request-only blocks that the extension replays at the
 * same places in every request. Whenever one of them goes missing, everything
 * after it is a prompt-cache miss. These tests walk the lifecycle events that
 * used to drop the blocks.
 */
import test from "node:test";
import assert from "node:assert/strict";

import { mkdirSync, rmSync } from "node:fs";
import path from "node:path";
import { stabilizationHarness } from "./support/stabilization-utils.ts";
import { createExtensionHarness } from "./support/extension-harness.ts";
import { tempDir } from "./support/fs.ts";
import { hasActiveDraft } from "../extensions/goal-drafting.ts";
import { contents, forgetSharedRetention, msg, sharedPrefix } from "./support/retention-helpers.ts";

import { GOAL_ENDED_STATE, TASK_LIST_NONE, LiveTailRetention, MAX_RETAINED_LIVE_TAILS } from "../extensions/goal-live-retention.ts";
import { saveLiveTails, restoreLiveTails } from "../extensions/goal-live-persist.ts";

const ENDED = { state: GOAL_ENDED_STATE, interim: true, onlyIfSent: true };
/** The request-only last block may change; everything before it must stay. */
function assertPrefixKept(before: unknown[], after: unknown[], what: string): void {
	assert.ok(sharedPrefix(before, after) >= before.length - 1, `${what}: the earlier request is no longer a prefix (${sharedPrefix(before, after)} of ${before.length} shared)`);
}

test("a goal that ends keeps the blocks already sent and says they are over", () => {
	const retention = new LiveTailRetention();
	const base = [msg("user", "h", 1)];
	const active = retention.apply("s", base, { state: "ACTIVE", counters: "TASKS", ephemeral: "usage 1", scope: "g1" });
	base.push(msg("assistant", "a", 2));
	const ended = retention.apply("s", base, ENDED);
	assertPrefixKept(active.messages, ended.messages, "goal ended");
	assert.ok(contents(ended.messages).some(text => text.includes("[PI GOAL ENDED]")));
	// Asking again adds nothing.
	base.push(msg("user", "next", 3));
	const again = retention.apply("s", base, ENDED);
	assert.equal(contents(again.messages).filter(text => text.includes("[PI GOAL ENDED]")).length, 1);
	assertPrefixKept(ended.messages, again.messages, "second request after the end");
});

test("nothing is retired when no goal text was ever sent", () => {
	const retention = new LiveTailRetention();
	const base = [msg("user", "h", 1)];
	assert.deepEqual(retention.apply("s", base, ENDED).messages, base);
	assert.deepEqual(retention.contents("s"), undefined, "no session is kept for it");
});

test("a new goal after the end starts without a reset and sends its task list again", () => {
	const retention = new LiveTailRetention();
	const base = [msg("user", "h", 1)];
	retention.apply("s", base, { state: "ONE", counters: "TASKS", scope: "g1" });
	base.push(msg("assistant", "a", 2));
	const ended = retention.apply("s", base, ENDED);
	base.push(msg("user", "new goal please", 3));
	const second = retention.apply("s", base, { state: "TWO", counters: "TASKS", scope: "g2" });
	assertPrefixKept(ended.messages, second.messages, "new goal after the end");
	assert.ok(contents(second.messages).some(text => text.includes("TWO")));
	assert.equal(contents(second.messages).filter(text => text === "TASKS").length, 2, "the ended note voided the old list, so the new one is sent");
});

test("a stale checkpoint or an unfocused pool does not reset the goal's blocks", () => {
	const retention = new LiveTailRetention();
	const base = [msg("user", "h", 1)];
	const active = retention.apply("s", base, { state: "ACTIVE", counters: "TASKS", scope: "g1" });
	base.push(msg("assistant", "a", 2));
	const stale = retention.apply("s", base, { state: "STALE", interim: true });
	assertPrefixKept(active.messages, stale.messages, "stale checkpoint");
	base.push(msg("assistant", "b", 3));
	const back = retention.apply("s", base, { state: "ACTIVE", counters: "TASKS", scope: "g1" });
	assertPrefixKept(stale.messages, back.messages, "back from the stale checkpoint");
});

test("a message the plain copy cannot hold does not reset the blocks on every request", () => {
	const retention = new LiveTailRetention();
	class Odd { text: string; constructor(text: string) { this.text = text; } }
	const odd = { role: "toolResult", toolCallId: "c", content: [{ type: "text", text: "ok" }], details: new Odd("x"), timestamp: 2 };
	const base: any[] = [msg("user", "h", 1), msg("assistant", "call", 1.5), odd];
	retention.apply("s", base, { state: "S", scope: "g" });
	for (let n = 0; n < 5; n++) {
		base.push(msg("assistant", `a${n}`, 10 + n));
		const next = retention.apply("s", base, { state: "S", scope: "g" });
		assert.equal(contents(next.messages).filter(text => text === "S").length, 1, "the policy block is still where it was");
		assert.equal(next.messages.findIndex(m => m.content === "S"), 3, "and it did not move");
	}
	// A different message in the same place is a rewrite.
	base[2] = { ...odd, timestamp: 99 };
	const rewritten = retention.apply("s", base, { state: "S", scope: "g" });
	assert.equal(rewritten.messages.findIndex(m => m.content === "S"), base.length, "a real rewrite still starts over");
});

test("the busiest session is not the one evicted when others come and go", () => {
	const retention = new LiveTailRetention();
	const main = [msg("user", "h", 1)];
	retention.apply("main", main, { state: "S", scope: "g" });
	for (let n = 0; n < 40; n++) {
		retention.apply(`other-${n}`, [msg("user", "x", 1)], { state: "S", scope: "g" });
		main.push(msg("assistant", `a${n}`, 2 + n));
		const next = retention.apply("main", main, { state: "S", scope: "g" });
		assert.equal(next.messages.findIndex(m => m.content === "S"), 1, `main lost its blocks after ${n + 1} other sessions`);
	}
});

test("the request keeps its prefix when the goal completes", async () => {
	const h = stabilizationHarness();
	try {
		await h.start();
		const ask = async (messages: any[]) => (await h.event("context_with_system", { messages })).messages as any[];
		const base = [msg("user", "work on it", 1)];
		const first = await ask(base);
		assert.ok(contents(first).some(text => String(text).includes("PI GOAL ACTIVE")));
		base.push(msg("assistant", "done", 2));
		const second = await ask(base);
		assertPrefixKept(first, second, "ordinary turn");
		h.core.state.goal = { ...h.core.state.goal!, status: "complete" };
		base.push(msg("user", "thanks", 3));
		const third = await ask(base);
		assertPrefixKept(second, third, "goal completed");
		assert.ok(contents(third).some(text => String(text).includes("PI GOAL ENDED")));
		base.push(msg("assistant", "you are welcome", 4));
		assertPrefixKept(third, await ask(base), "turn after the goal ended");
	} finally {
		await h.cleanup();
	}
});

test("a host that gives no session id still gets retained blocks", async () => {
	const h = stabilizationHarness();
	try {
		h.ctx.sessionManager.getSessionId = () => undefined;
		await h.start();
		const ask = async (messages: any[]) => (await h.event("context_with_system", { messages })).messages as any[];
		const base = [msg("user", "work on it", 1)];
		const first = await ask(base);
		base.push(msg("assistant", "step", 2));
		const second = await ask(base);
		assertPrefixKept(first, second, "no session id");
		assert.equal(contents(second).filter(text => String(text).includes("PI GOAL ACTIVE")).length, 1);
	} finally {
		await h.cleanup();
	}
});

test("the retention bound still applies after a goal ended", () => {
	const retention = new LiveTailRetention();
	const base = [msg("user", "h", 1)];
	retention.apply("s", base, { state: "ONE", counters: "T0", scope: "g" });
	retention.apply("s", base, ENDED);
	for (let n = 1; n <= MAX_RETAINED_LIVE_TAILS + 2; n++) {
		base.push(msg("assistant", `a${n}`, 1 + n));
		retention.apply("s", base, { state: "ONE", counters: `T${n}`, scope: "g" });
	}
	const last = retention.apply("s", base, { state: "ONE", counters: `T${MAX_RETAINED_LIVE_TAILS + 2}`, scope: "g" });
	assert.ok(last.messages.length < base.length + MAX_RETAINED_LIVE_TAILS + 3);
});

test("starting a draft after a goal ended keeps the request prefix", async () => {
	const cwd = tempDir("goal-cache-draft-");
	mkdirSync(path.join(cwd, ".pi", "goals", "archived"), { recursive: true });
	try {
		const h = createExtensionHarness({ cwd, hasUI: true, sessionId: "cache-draft", custom: "dialog" });
		await h.sessionStart();
		await h.commands.get("goal-direct")!.handler("First objective", h.ctx);
		const ask = async (messages: any[]) => (await h.emit("context_with_system", { messages })).messages as any[];
		const base = [msg("user", "work on it", 1)];
		const active = await ask(base);
		assert.ok(contents(active).some(text => String(text).includes("PI GOAL ACTIVE")));
		base.push(msg("assistant", "done", 2));
		h.core.state.goal = { ...h.core.state.goal!, status: "complete" };
		const ended = await ask(base);
		assertPrefixKept(active, ended, "goal completed");
		await h.commands.get("goal")!.handler("A new objective", h.ctx);
		assert.ok(hasActiveDraft(h.core), "the draft is open");
		base.push(msg("user", "/goal A new objective", 3));
		const drafting = await ask(base);
		assertPrefixKept(ended, drafting, "draft opened after the goal ended");
	} finally {
		rmSync(cwd, { recursive: true, force: true });
	}
});

test("a reload keeps the retained blocks, and so does a restart that finds them saved with the session", async () => {
	const cwd = tempDir("goal-cache-reload-");
	mkdirSync(path.join(cwd, ".pi", "goals", "archived"), { recursive: true });
	try {
		const first = createExtensionHarness({ cwd, hasUI: false, sessionId: "reload-session" });
		await first.sessionStart();
		await first.callTool("create_goal", { objective: "Survive a reload" });
		const ask = async (h: typeof first, messages: any[]) => (await h.emit("context_with_system", { messages })).messages as any[];
		const base = [msg("user", "work on it", 1), msg("assistant", "started", 2)];
		const before = await ask(first, base);
		assert.ok(contents(before).some(text => String(text).includes("PI GOAL ACTIVE")));
		assert.ok(first.entries.some(entry => entry.customType === "pi-goal-live-tails"), "the blocks were saved with the session");
		base.push(msg("assistant", "more", 3));
		const options = { cwd, hasUI: false, sessionId: "reload-session", sessionEntries: () => first.entries as any };

		// The same conversation under a new extension instance, as after /reload.
		const reloaded = createExtensionHarness(options);
		await reloaded.sessionStart("reload");
		const afterReload = await ask(reloaded, base);
		assert.equal(afterReload.filter(m => String(m.content).includes("PI GOAL ACTIVE")).length, 1, "the goal is focused again");
		assertPrefixKept(before, afterReload, "reload");

		// A new process: nothing in memory, so the blocks come from the session entries.
		forgetSharedRetention();
		const restarted = createExtensionHarness(options);
		await restarted.sessionStart("resume");
		const afterRestart = await ask(restarted, base);
		assertPrefixKept(before, afterRestart, "restart");
		assert.equal(afterRestart.filter(m => String(m.content).startsWith("[PI GOAL")).length, 1, "the saved block was reused, not sent again");

		// Another conversation under the same id must not pick the blocks up.
		forgetSharedRetention();
		const other = createExtensionHarness(options);
		await other.sessionStart("resume");
		const rewritten = [msg("user", "something else", 11), msg("assistant", "started", 12), msg("assistant", "more", 13)];
		const clean = await ask(other, rewritten);
		assert.equal(clean.findIndex(m => String(m.content).includes("[PI GOAL ")), rewritten.length, "changed history starts clean");
	} finally {
		rmSync(cwd, { recursive: true, force: true });
	}
});

test("pause and resume cycles point back to the policy instead of repeating it, so they never hit the block limit", () => {
	const retention = new LiveTailRetention();
	const base = [msg("user", "h", 1)];
	let previous = retention.apply("s", base, { state: "[PI GOAL ACTIVE goalId=g]\nBIG POLICY", counters: "TASKS", scope: "g" }).messages;
	for (let cycle = 0; cycle < 40; cycle++) {
		for (const state of ["[PI GOAL PAUSED goalId=g]\nPAUSED POLICY", "[PI GOAL ACTIVE goalId=g]\nBIG POLICY"]) {
			base.push(msg("assistant", `a${cycle}`, 2 + cycle));
			const next = retention.apply("s", base, { state, counters: "TASKS", scope: "g" }).messages;
			assertPrefixKept(previous, next, `cycle ${cycle}`);
			previous = next;
		}
	}
	const text = contents(previous).join("\n");
	assert.equal(text.split("BIG POLICY").length - 1, 1, "the full policy was sent once");
	assert.equal(text.split("PAUSED POLICY").length - 1, 1, "and so was the paused one");
	assert.match(text, /\[PI GOAL RESTORED\] Goal policy block #1 above, the one that begins "\[PI GOAL ACTIVE goalId=g\]", is in force again/);
	assert.equal(contents(previous).filter(c => String(c).includes("TASKS")).length, 1, "the task list was not repeated either");
});

test("the restart note says why the goal blocks were dropped", () => {
	const retention = new LiveTailRetention();
	assert.equal(retention.restartNote(), undefined);
	const base = [msg("user", "first", 1), msg("assistant", "second", 2)];
	retention.apply("s", base, { state: "S", scope: "a" });
	retention.apply("s", [...base, msg("user", "third", 3)], { state: "S", scope: "a" });
	assert.match(retention.restartNote()!, /kept in place for all 2 requests/);
	const edited = [msg("user", "first", 1), msg("assistant", "changed", 22), msg("user", "third", 3)];
	retention.apply("s", edited, { state: "S", scope: "a" });
	assert.match(retention.restartNote()!, /restarted 1 time in 3 requests; last on request 3: message 2 of the history changed.*assistant/);
});

test("another extension editing the newest message each request does not move the goal blocks", () => {
	const retention = new LiveTailRetention();
	const history: any[] = [msg("user", "start", 1)];
	const decorated = () => history.map((m, i) => i === history.length - 1 ? { ...m, content: `${m.content} [sent at ${Date.now()}.${i}]` } : m);
	let previous: any[] = [];
	let firstPolicyAt = -1;
	for (let n = 0; n < 20; n++) {
		const out = retention.apply("s", decorated(), { state: "S", counters: "V", scope: "g" });
		const at = out.messages.findIndex(m => m.content === "S");
		if (n === 0) firstPolicyAt = at;
		assert.equal(at, firstPolicyAt, `request ${n + 1}: the policy block moved`);
		previous = out.messages;
		history.push(msg("assistant", `a${n}`, 10 + 2 * n), msg("user", `u${n}`, 11 + 2 * n));
	}
	assert.equal(contents(previous).filter(text => text === "S").length, 1);
	assert.match(retention.restartNote()!, /kept in place/);
});

test("many edited messages in one request still count as a rewrite", () => {
	const retention = new LiveTailRetention();
	const history: any[] = Array.from({ length: 6 }, (_, i) => msg("user", `m${i}`, i + 1));
	retention.apply("s", history, { state: "S", scope: "g" });
	const rewritten = history.map(m => ({ ...m, content: `${m.content} changed` }));
	const out = retention.apply("s", rewritten, { state: "S", scope: "g" });
	assert.equal(out.messages.findIndex(m => m.content === "S"), rewritten.length);
	assert.match(retention.restartNote()!, /restarted 1 time/);
});

test("a restart that finds the saved blocks does not write them out again", async () => {
	const cwd = tempDir("goal-cache-resave-");
	mkdirSync(path.join(cwd, ".pi", "goals", "archived"), { recursive: true });
	try {
		const first = createExtensionHarness({ cwd, hasUI: false, sessionId: "resave-session" });
		await first.sessionStart();
		await first.callTool("create_goal", { objective: "Restart quietly" });
		const ask = async (h: typeof first, messages: any[]) => (await h.emit("context_with_system", { messages })).messages as any[];
		const base = [msg("user", "work on it", 1), msg("assistant", "started", 2)];
		await ask(first, base);
		forgetSharedRetention();
		const restarted = createExtensionHarness({ cwd, hasUI: false, sessionId: "resave-session", sessionEntries: () => first.entries as any });
		await restarted.sessionStart("resume");
		await ask(restarted, base);
		const saved = restarted.entries.filter(entry => entry.customType === "pi-goal-live-tails").map(entry => (entry.data as any).op);
		assert.ok(!saved.includes("reset"), `the saved blocks are not rewritten (${saved.join(",")})`);
	} finally {
		rmSync(cwd, { recursive: true, force: true });
	}
});

test("a goal with no task list after a stand-in does not leave the old goal's list as the only one", () => {
	const retention = new LiveTailRetention();
	const base = [msg("user", "h", 1)];
	retention.apply("s", base, { state: "ONE", counters: "[TASK LIST one]", scope: "g1" });
	base.push(msg("assistant", "a", 2));
	retention.apply("s", base, { state: "STALE", interim: true });
	base.push(msg("user", "next", 3));
	const second = retention.apply("s", base, { state: "TWO", scope: "g2" });
	const all = contents(second.messages);
	assert.ok(all.lastIndexOf(TASK_LIST_NONE) > all.indexOf("[TASK LIST one]"), "the old list is followed by a block that voids it");
});

test("a restored policy is named by its number and its own first line", () => {
	const retention = new LiveTailRetention();
	const base = [msg("user", "h", 1)];
	retention.apply("s", base, { state: "[PI GOAL ACTIVE goalId=X]\nbudget 1000", scope: "g" });
	base.push(msg("assistant", "a", 2));
	retention.apply("s", base, { state: "[PI GOAL ACTIVE goalId=X]\nbudget 50000", scope: "g" });
	base.push(msg("assistant", "b", 3));
	retention.apply("s", base, { state: "[PI GOAL PAUSED goalId=X]", scope: "g" });
	base.push(msg("assistant", "c", 4));
	const resumed = contents(retention.apply("s", base, { state: "[PI GOAL ACTIVE goalId=X]\nbudget 50000", scope: "g" }).messages);
	const pointer = resumed.find(text => String(text).startsWith("[PI GOAL RESTORED]"));
	assert.match(String(pointer), /block #2 /, "it points at the second block, not the first with the same first line");
});

test("opening a draft while a goal is active keeps the blocks and puts the goal on hold", async () => {
	const cwd = tempDir("goal-cache-hold-");
	mkdirSync(path.join(cwd, ".pi", "goals", "archived"), { recursive: true });
	try {
		const h = createExtensionHarness({ cwd, hasUI: true, sessionId: "cache-hold", custom: "dialog" });
		await h.sessionStart();
		await h.commands.get("goal-direct")!.handler("First objective", h.ctx);
		const ask = async (messages: any[]) => (await h.emit("context_with_system", { messages })).messages as any[];
		const base = [msg("user", "work on it", 1)];
		const active = await ask(base);
		base.push(msg("assistant", "done", 2));
		await h.commands.get("goal")!.handler("A new objective", h.ctx);
		assert.ok(hasActiveDraft(h.core), "the draft is open");
		base.push(msg("user", "/goal A new objective", 3));
		const drafting = await ask(base);
		assertPrefixKept(active, drafting, "draft opened while the goal was active");
		assert.ok(contents(drafting).some(text => String(text).includes("[PI GOAL ON HOLD]")));
	} finally {
		rmSync(cwd, { recursive: true, force: true });
	}
});

/** The retained blocks of a goal that went through a few states, saved into `entries`. */
function savedWalk(entries: any[], failOn?: number) {
	const retention = new LiveTailRetention();
	const history = [msg("user", "start", 1)];
	const key = "k";
	let appended = 0;
	const append = (type: string, data: unknown) => {
		if (failOn !== undefined && appended++ === failOn) throw new Error("disk full");
		entries.push({ type: "custom", customType: type, data });
	};
	const step = (state: string, ts: number) => {
		history.push(msg("assistant", `a${ts}`, ts));
		retention.apply(key, history, { state, scope: state });
		try { saveLiveTails(retention, key, "m", history, append); } catch { /* best effort, as in the extension */ }
	};
	return { retention, history, key, step };
}

test("after a failed save the next save rewrites everything, so a restart restores what is live", () => {
	const entries: any[] = [];
	const walk = savedWalk(entries, 1);
	walk.step("[PI GOAL ACTIVE goalId=a]\none", 2);
	walk.step("[PI GOAL ACTIVE goalId=b]\ntwo", 3);
	walk.step("[PI GOAL ACTIVE goalId=c]\nthree", 4);
	const restored = new LiveTailRetention();
	restoreLiveTails(restored, walk.key, "m", entries, walk.history);
	const strip = (state: any) => JSON.parse(JSON.stringify({ ...state, generation: 0 }));
	assert.deepEqual(strip(restored.exportState(walk.key)), strip(walk.retention.exportState(walk.key)));
});

test("a failed save followed by dropped blocks still clears what the session file holds", () => {
	const entries: any[] = [];
	const walk = savedWalk(entries);
	walk.step("[PI GOAL ACTIVE goalId=a]\none", 2);
	// The next save fails half way, then the blocks are dropped and that is saved.
	const failing = (type: string, data: unknown) => { if ((data as any).op === "tail") throw new Error("disk full"); entries.push({ type: "custom", customType: type, data }); };
	walk.history.push(msg("assistant", "a3", 3));
	walk.retention.apply(walk.key, walk.history, { state: "[PI GOAL ACTIVE goalId=b]\ntwo", scope: "b" });
	assert.throws(() => saveLiveTails(walk.retention, walk.key, "m", walk.history, failing));
	walk.retention.clear(walk.key);
	saveLiveTails(walk.retention, walk.key, "m", walk.history, (type, data) => entries.push({ type: "custom", customType: type, data }));
	const restored = new LiveTailRetention();
	restoreLiveTails(restored, walk.key, "m", entries, walk.history);
	assert.equal(restored.has(walk.key), false, "nothing stale comes back");
});

test("damaged or reordered saved entries are skipped or ignored, never thrown on or put in the wrong place", () => {
	const entries: any[] = [];
	const walk = savedWalk(entries);
	walk.step("[PI GOAL ACTIVE goalId=a]\none", 2);
	walk.step("[PI GOAL ACTIVE goalId=b]\ntwo", 3);
	const tails = entries.filter(entry => entry.data.op === "tail");
	assert.ok(tails.length >= 2);
	const restore = (list: any[]) => { const retention = new LiveTailRetention(); restoreLiveTails(retention, walk.key, "m", list, walk.history); return retention; };
	assert.doesNotThrow(() => restore([...entries, { type: "custom", customType: "pi-goal-live-tails", data: { v: 1, model: "m", op: "tail", anchorIndex: 1, kind: "goal-state", text: 42, prefixHash: 7 } }]));
	assert.doesNotThrow(() => restore([...entries, { type: "custom", customType: "pi-goal-live-tails", data: null }]));
	const swapped = [...entries];
	const [i, j] = [entries.indexOf(tails[0]), entries.indexOf(tails[1])];
	[swapped[i], swapped[j]] = [swapped[j], swapped[i]];
	assert.equal(restore(swapped).has(walk.key), false, "blocks out of order are not restored");
});
