Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apps/server/src/orchestration-v2/ShellStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,35 @@ describe("omitOlderSettledShells", () => {
expect(result.threads.map((thread) => thread.id).slice(25)).toEqual(["tie"]);
});

it("finds the same cut whatever order the settled threads arrive in", () => {
const ascending = Array.from({ length: 200 }, (_, index) =>
row(`${index % 2 === 0 ? "a" : "b"}-${index}`, index),
);
let seed = 7;
const shuffled = [...ascending];
for (let index = shuffled.length - 1; index > 0; index--) {
seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
const other = seed % (index + 1);
[shuffled[index], shuffled[other]] = [shuffled[other]!, shuffled[index]!];
}
const newestIds = new Set(ascending.slice(0, 25).map((thread) => thread.id));
for (const threads of [ascending, [...ascending].reverse(), shuffled]) {
const result = omit(threads);
expect(result.threads.map((thread) => thread.id)).toEqual(
threads.filter((thread) => newestIds.has(thread.id)).map((thread) => thread.id),
);
expect(result.olderSettled.before).toBe(
DateTime.formatIso(DateTime.makeUnsafe(100 * DAY - 24 * DAY)),
);
expect(result.olderSettled.counts).toEqual(
expect.arrayContaining([
{ projectId: "project-a", count: 87 },
{ projectId: "project-b", count: 88 },
]),
);
}
});

it("omits nothing with fewer than 25 settled threads, however old", () => {
const old = Array.from({ length: 5 }, (_, index) => row(`old-${index}`, 300 + index));
const result = omit(old);
Expand Down
28 changes: 25 additions & 3 deletions apps/server/src/orchestration-v2/ShellStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ function shellTrimFacts(rows: ReadonlyArray<ShellTrimRow>): ShellTrimFacts {
const subagentParentIds = new Set<string>();
const subagentThreadIds = new Set<string>();
const remoteTaskParentIds = new Set<string>();
// A bounded min-heap keeps the 25th newest timestamp at its root.
const settledTimes: Array<number> = [];
for (const row of rows) {
if (row.hasRemoteTasks === true) remoteTaskParentIds.add(row.id);
Expand All @@ -213,12 +214,33 @@ function shellTrimFacts(rows: ReadonlyArray<ShellTrimRow>): ShellTrimFacts {
row.settledOverride === "settled" &&
row.settledAt !== null
) {
settledTimes.push(DateTime.toEpochMillis(row.settledAt));
const settledAtMs = DateTime.toEpochMillis(row.settledAt);
let position = settledTimes.length;
if (position < NEWEST_SETTLED_KEPT) {
settledTimes.push(settledAtMs);
while (position > 0) {
const parent = (position - 1) >>> 1;
if (settledTimes[parent]! <= settledAtMs) break;
settledTimes[position] = settledTimes[parent]!;
position = parent;
}
} else {
if (settledAtMs <= settledTimes[0]!) continue;
position = 0;
while (position * 2 + 1 < settledTimes.length) {
let child = position * 2 + 1;
if (child + 1 < settledTimes.length && settledTimes[child + 1]! < settledTimes[child]!)
child++;
if (settledAtMs <= settledTimes[child]!) break;
settledTimes[position] = settledTimes[child]!;
position = child;
}
}
settledTimes[position] = settledAtMs;
}
}
settledTimes.sort((left, right) => right - left);
// With fewer than 25, the oldest one is the cut, so nothing is older.
const olderSettledBeforeMs = settledTimes[NEWEST_SETTLED_KEPT - 1] ?? settledTimes.at(-1);
const olderSettledBeforeMs = settledTimes[0];
const counts = new Map<ProjectId, number>();
if (olderSettledBeforeMs !== undefined) {
for (const row of rows) {
Expand Down
Loading