Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: number closing state by last turn

ogsamratfbc139132140
diff --git a/apps/server/src/session-archive/index.test.ts b/apps/server/src/session-archive/index.test.ts
index cc52c850..e9a887a7 100644
--- a/apps/server/src/session-archive/index.test.ts
+++ b/apps/server/src/session-archive/index.test.ts
@@ -743,6 +743,36 @@ describe("SessionArchiver final archives", () => {
expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "baselines"))).toHaveLength(1);
});
+ test("a final archive after turns that changed nothing takes the last completed turn, never one below the chain's", async () => {
+ const server = new FakeArchiveServer();
+ const { root } = await project();
+ const state = await tempDir("state");
+ const subject = archiver(server, state);
+ const id = "ses_final_turn_seen";
+
+ expect((await subject.captureBase(id, root)).status).toBe("queued");
+ await writeFile(join(root, "src/app.ts"), "export const app = 2;\n");
+ expect(await subject.captureDelta(id, root, 1)).toMatchObject({ status: "queued", sequence: 1 });
+ // Turns 2 and 3 change nothing: no delta.
+ expect(await subject.captureDelta(id, root, 2)).toMatchObject({ status: "skipped", reason: "unchanged" });
+ expect(await subject.captureDelta(id, root, 3)).toMatchObject({ status: "skipped", reason: "unchanged" });
+ // The user edits after turn 3: the final is turn 3's.
+ await writeFile(join(root, "src/user.ts"), "export const user = 1;\n");
+ expect(await subject.captureFinal(id, "idle")).toMatchObject({ status: "queued", sequence: 2 });
+ // A count that went back (a reverted turn) is no turn: the next final stays at 3.
+ expect(await subject.captureDelta(id, root, 2)).toEqual({ status: "skipped", reason: "stale_turn" });
+ await writeFile(join(root, "src/user.ts"), "export const user = 2;\n");
+ expect(await subject.captureFinal(id, "turn_incomplete")).toMatchObject({ status: "queued", sequence: 3 });
+ await writeFile(join(root, "src/app.ts"), "export const app = 4;\n");
+ expect(await subject.captureDelta(id, root, 4)).toMatchObject({ status: "queued", sequence: 4 });
+ expect(await subject.drain()).toMatchObject({ uploaded: 5, pending: 0, dropped: 0 });
+
+ const objects = server.objects();
+ expect(objects.map((object) => [object.request.kind, object.request.turn])).toEqual([["base", 0], ["delta", 1], ["delta", 3], ["delta", 3], ["delta", 4]]);
+ const manifests = await Promise.all(objects.map(async (object) => manifestOf(await openArchive(object.object!))));
+ expect(manifests.slice(1).map((manifest) => [manifest.turn, manifest.trigger])).toEqual([[1, "turn"], [3, "final"], [3, "final"], [4, "turn"]]);
+ });
+
test("a plain folder's final archives pass the all-folders refusals and policy again: one while it is on, none while it is off", async () => {
const server = new FakeArchiveServer();
server.policy = { all_folders: true };
@@ -1237,14 +1267,14 @@ describe("SessionArchiver touched files", () => {
await second.stop();
expect(await readdir(join(state, ARCHIVE_STATE_DIRECTORY, "touched"))).toHaveLength(1);
- // Online again, and the kept answer is old: the base carries every path from both runs, numbered with the turns seen.
+ // Online again, and the kept answer is old: the base carries every path from both runs, numbered with the turns seen (turn 2 ended while the policy was off).
server.apiHook = null;
now += POLICY_TTL_MS;
const third = make();
expect(await third.captureFinal(id, "idle")).toMatchObject({ status: "queued", kind: "base", sequence: 0 });
expect((await third.drain()).uploaded).toBe(1);
const [base] = server.objects();
- expect(base!.request).toMatchObject({ session_id: id, kind: "base", turn: 1, marker: "touched" });
+ expect(base!.request).toMatchObject({ session_id: id, kind: "base", turn: 2, marker: "touched" });
expect(await names(base!)).toEqual(["__omnirush__/manifest.json", "chart.svg", "draft.md"]);
});
diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.ts
index 48c12883..61825980 100644
--- a/apps/server/src/session-archive/index.ts
+++ b/apps/server/src/session-archive/index.ts
@@ -320,7 +320,7 @@ const sessionStateSchema = z.object({
final_due: z.boolean().optional(),
/** When the session was deleted in the app: no final archive at app start. */
ended: z.string().nullable().optional(),
- /** A touched-files chain without its base yet: the completed turns last seen, the turn of a base a final archive captures. */
+ /** The completed turns last seen at a turn's end: the turn of a touched-files base a final archive captures, and of any final archive. */
turn_seen: z.number().int().nonnegative().optional(),
/**
* When the session was last active: its base, a prompt (captureBase once
@@ -728,8 +728,10 @@ export class SessionArchiver {
const loaded = await this.loadSession(sessionId);
if (!loaded) return { status: "skipped", reason: "no_base" };
if (loaded.stopped || this.stoppedSessions.has(sessionId)) return { status: "skipped", reason: "stopped" };
- // A turn ended: the session was active, whatever this capture does.
- const state: SessionState = { ...loaded, last_activity_at: this.now().toISOString() };
+ // A turn ended: the session was active, whatever this capture does. Its count is kept (turn_seen):
+ // a turn that changed nothing gets no delta, and a final archive after it is numbered with it.
+ const seen = turn !== null && turn > (loaded.turn_seen ?? -1) ? { turn_seen: turn } : {};
+ const state: SessionState = { ...loaded, ...seen, last_activity_at: this.now().toISOString() };
if (generation === this.generation) await this.saveSession(state);
if (resolve(root) !== state.root) this.log("warn", "OmniRush archive delta root differs from the session root; using the session root", { sessionId });
const touched = state.marker === TOUCHED_MARKER;
@@ -779,7 +781,8 @@ export class SessionArchiver {
if (this.disabled || !this.uploader.configured) return { status: "skipped", reason: "disabled" };
const state = await this.loadSession(sessionId);
const unbased = state?.next_sequence === 0 && state.marker === TOUCHED_MARKER;
- const turn = unbased ? state.turn_seen ?? 0 : state && state.next_sequence > 0 ? state.last_turn : null;
+ // The completed turns: the last archived one, or a later turn that changed nothing (5.8.1). Never below the parent's.
+ const turn = unbased ? state.turn_seen ?? 0 : state && state.next_sequence > 0 && state.last_turn !== null ? Math.max(state.last_turn, state.turn_seen ?? 0) : null;
if (!state || turn === null) return { status: "skipped", reason: "no_base" };
if (state.stopped || this.stoppedSessions.has(sessionId)) return { status: "skipped", reason: "stopped" };
const skip = async (skipReason: CaptureSkipReason): Promise<CaptureResult> => {