Skip to content

Commit 32fe63b

Browse files
committed
fix(gateway): enforce 30-day retention window in approval history query
listTerminalOperatorApprovals returned every resolved row; apply the same OPERATOR_APPROVAL_TERMINAL_RETENTION_MS cutoff the UI promises (injectable now for tests) so history can never surface rows past the window even if a prune has not just run.
1 parent 999176e commit 32fe63b

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎src/gateway/operator-approval-store.test.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,69 @@ describe("operator approval store", () => {
226226
).toMatchObject({ outcome: "resolved" });
227227
}
228228

229-
const firstPage = listTerminalOperatorApprovals({ limit: 2, databaseOptions });
229+
const firstPage = listTerminalOperatorApprovals({ limit: 2, nowMs: 3_000, databaseOptions });
230230
expect(firstPage.records.map((record) => record.id)).toEqual(["system-middle", "plugin-new"]);
231231
expect(firstPage.nextCursor).toEqual(expect.any(String));
232232

233233
const secondPage = listTerminalOperatorApprovals({
234234
cursor: firstPage.nextCursor,
235235
limit: 2,
236+
nowMs: 3_000,
236237
databaseOptions,
237238
});
238239
expect(secondPage.records.map((record) => record.id)).toEqual(["exec-old"]);
239240
expect(secondPage.nextCursor).toBeUndefined();
240241

241242
expect(
242-
listTerminalOperatorApprovals({ kind: "plugin", databaseOptions }).records.map(
243+
listTerminalOperatorApprovals({ kind: "plugin", nowMs: 3_000, databaseOptions }).records.map(
243244
(record) => record.id,
244245
),
245246
).toEqual(["plugin-new"]);
246247
});
247248

249+
it("excludes terminal rows resolved before the 30-day retention cutoff", () => {
250+
const databaseOptions = createDatabaseOptions();
251+
const day = 24 * 60 * 60_000;
252+
const now = 100 * day;
253+
expect(
254+
insertOperatorApproval({
255+
approval: approval("old", { createdAtMs: 1_000, expiresAtMs: now }),
256+
databaseOptions,
257+
}),
258+
).toMatchObject({ outcome: "inserted" });
259+
expect(
260+
insertOperatorApproval({
261+
approval: approval("recent", { createdAtMs: now - 2 * day, expiresAtMs: now }),
262+
databaseOptions,
263+
}),
264+
).toMatchObject({ outcome: "inserted" });
265+
// Resolve one row 40 days ago (past the window) and one 1 day ago (inside).
266+
expect(
267+
resolveOperatorApproval({
268+
id: "old",
269+
decision: "deny",
270+
resolver: { kind: "device", id: "reviewer-device" },
271+
nowMs: now - 40 * day,
272+
databaseOptions,
273+
}),
274+
).toMatchObject({ outcome: "resolved" });
275+
expect(
276+
resolveOperatorApproval({
277+
id: "recent",
278+
decision: "deny",
279+
resolver: { kind: "device", id: "reviewer-device" },
280+
nowMs: now - day,
281+
databaseOptions,
282+
}),
283+
).toMatchObject({ outcome: "resolved" });
284+
285+
expect(
286+
listTerminalOperatorApprovals({ nowMs: now, databaseOptions }).records.map(
287+
(record) => record.id,
288+
),
289+
).toEqual(["recent"]);
290+
});
291+
248292
it("filters an audience before applying the replay limit across scan pages", () => {
249293
const databaseOptions = createDatabaseOptions();
250294
for (let index = 0; index < 256; index += 1) {

‎src/gateway/operator-approval-store.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,13 +851,17 @@ export function listTerminalOperatorApprovals(
851851
cursor?: string;
852852
limit?: number;
853853
kind?: OperatorApprovalKind;
854+
nowMs?: number;
854855
databaseOptions?: OpenClawStateDatabaseOptions;
855856
} = {},
856857
): ListTerminalOperatorApprovalsResult {
857858
const requestedLimit = Number.isSafeInteger(params.limit)
858859
? (params.limit ?? OPERATOR_APPROVAL_HISTORY_DEFAULT_LIMIT)
859860
: OPERATOR_APPROVAL_HISTORY_DEFAULT_LIMIT;
860861
const resultLimit = Math.max(1, Math.min(requestedLimit, OPERATOR_APPROVAL_HISTORY_MAX_LIMIT));
862+
// Enforce the same 30-day retention the UI promises, independent of whether a
863+
// prune has run recently, so history can never surface rows past the window.
864+
const retentionCutoffMs = (params.nowMs ?? Date.now()) - OPERATOR_APPROVAL_TERMINAL_RETENTION_MS;
861865
let cursor =
862866
params.cursor === undefined ? undefined : decodeOperatorApprovalHistoryCursor(params.cursor);
863867
const database = openOpenClawStateDatabase(params.databaseOptions);
@@ -875,6 +879,7 @@ export function listTerminalOperatorApprovals(
875879
.selectAll()
876880
.where("status", "!=", "pending")
877881
.where("resolved_at_ms", "is not", null)
882+
.where("resolved_at_ms", ">=", retentionCutoffMs)
878883
.orderBy("resolved_at_ms", "desc")
879884
.orderBy("approval_id", "desc")
880885
.limit(batchLimit);

0 commit comments

Comments
 (0)