Skip to content

Commit 8dd2e18

Browse files
nocodet888-archsteipete
authored andcommitted
fix(gateway): trim audit.list exact-match filter ids
1 parent a172857 commit 8dd2e18

3 files changed

Lines changed: 120 additions & 6 deletions

File tree

‎src/gateway/server-methods/audit.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,27 @@ describe("audit gateway methods", () => {
208208
expect(listAuditEvents).toHaveBeenCalledWith(expect.objectContaining({ cursor: 11 }));
209209
},
210210
);
211+
212+
it.each(["audit.list", "audit.activity.list"] as const)(
213+
"trims exact-match filter ids for %s before store lookup",
214+
async (method) => {
215+
const respond = await runAuditHandler(method, {
216+
agentId: " main ",
217+
sessionKey: " agent:main:main ",
218+
runId: " run-1 ",
219+
});
220+
221+
expect(respond).toHaveBeenCalledWith(true, expect.anything());
222+
expect(listAuditEvents).toHaveBeenCalledWith(
223+
expect.objectContaining({
224+
filters: expect.objectContaining({
225+
agentId: "main",
226+
sessionKey: "agent:main:main",
227+
runId: "run-1",
228+
...(method === "audit.activity.list" ? { includeMessages: true } : {}),
229+
}),
230+
}),
231+
);
232+
},
233+
);
211234
});
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { expectDefined } from "@openclaw/normalization-core";
2+
import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
3+
import { cleanupTempDirs, makeTempDir } from "../../../test/helpers/temp-dir.js";
4+
import { listAuditEvents, recordAuditEvent } from "../../audit/audit-event-store.js";
5+
import {
6+
closeOpenClawStateDatabaseForTest,
7+
type OpenClawStateDatabaseOptions,
8+
} from "../../state/openclaw-state-db.js";
9+
import { auditHandlers } from "./audit.js";
10+
11+
const tempDirs: string[] = [];
12+
13+
function createDatabaseOptions(): OpenClawStateDatabaseOptions {
14+
return { env: { OPENCLAW_STATE_DIR: makeTempDir(tempDirs, "openclaw-audit-trim-") } };
15+
}
16+
17+
afterEach(() => {
18+
closeOpenClawStateDatabaseForTest();
19+
delete process.env.OPENCLAW_STATE_DIR;
20+
});
21+
22+
afterAll(() => {
23+
cleanupTempDirs(tempDirs);
24+
});
25+
26+
describe("audit.list padded filters against a real audit store", () => {
27+
it("returns the planted run for padded agentId/runId filters", async () => {
28+
const database = createDatabaseOptions();
29+
const stateDir = expectDefined(database.env?.OPENCLAW_STATE_DIR, "temp state dir");
30+
process.env.OPENCLAW_STATE_DIR = stateDir;
31+
32+
recordAuditEvent(
33+
{
34+
sourceId: "audit-trim-run-source",
35+
sourceSequence: 1,
36+
occurredAt: Date.now(),
37+
kind: "agent_run",
38+
action: "agent.run.finished",
39+
status: "succeeded",
40+
actorType: "agent",
41+
actorId: "main",
42+
agentId: "main",
43+
sessionKey: "agent:main:main",
44+
runId: "run-trim-1",
45+
},
46+
database,
47+
);
48+
49+
// Negative control: untrimmed filter values miss the planted row at the store.
50+
expect(
51+
listAuditEvents({
52+
limit: 20,
53+
filters: { agentId: " main ", runId: " run-trim-1 " },
54+
database,
55+
}).events,
56+
).toEqual([]);
57+
58+
const respond = vi.fn();
59+
await expectDefined(
60+
auditHandlers["audit.list"],
61+
"audit.list handler",
62+
)({
63+
params: {
64+
agentId: " main ",
65+
sessionKey: " agent:main:main ",
66+
runId: " run-trim-1 ",
67+
},
68+
respond,
69+
} as never);
70+
71+
expect(respond).toHaveBeenCalledWith(
72+
true,
73+
expect.objectContaining({
74+
events: [
75+
expect.objectContaining({
76+
agentId: "main",
77+
runId: "run-trim-1",
78+
action: "agent.run.finished",
79+
}),
80+
],
81+
}),
82+
);
83+
});
84+
});

‎src/gateway/server-methods/audit.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Metadata-only operator audit queries over the canonical shared SQLite ledger.
2+
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
23
import {
34
ErrorCodes,
45
errorShape,
@@ -90,13 +91,16 @@ export const auditHandlers: GatewayRequestHandlers = {
9091
);
9192
return;
9293
}
94+
const agentId = normalizeOptionalString(params.agentId);
95+
const sessionKey = normalizeOptionalString(params.sessionKey);
96+
const runId = normalizeOptionalString(params.runId);
9397
const page = listAuditEvents({
9498
limit: Math.min(params.limit ?? DEFAULT_AUDIT_LIST_LIMIT, MAX_AUDIT_LIST_LIMIT),
9599
...(parsed.cursor !== undefined ? { cursor: parsed.cursor } : {}),
96100
filters: {
97-
...(params.agentId ? { agentId: params.agentId } : {}),
98-
...(params.sessionKey ? { sessionKey: params.sessionKey } : {}),
99-
...(params.runId ? { runId: params.runId } : {}),
101+
...(agentId ? { agentId } : {}),
102+
...(sessionKey ? { sessionKey } : {}),
103+
...(runId ? { runId } : {}),
100104
...(params.kind ? { kind: params.kind } : {}),
101105
...(params.status ? { status: params.status } : {}),
102106
...(params.after !== undefined ? { after: params.after } : {}),
@@ -128,14 +132,17 @@ export const auditHandlers: GatewayRequestHandlers = {
128132
);
129133
return;
130134
}
135+
const agentId = normalizeOptionalString(params.agentId);
136+
const sessionKey = normalizeOptionalString(params.sessionKey);
137+
const runId = normalizeOptionalString(params.runId);
131138
const page = listAuditEvents({
132139
limit: Math.min(params.limit ?? DEFAULT_AUDIT_LIST_LIMIT, MAX_AUDIT_LIST_LIMIT),
133140
...(parsed.cursor !== undefined ? { cursor: parsed.cursor } : {}),
134141
filters: {
135142
includeMessages: true,
136-
...(params.agentId ? { agentId: params.agentId } : {}),
137-
...(params.sessionKey ? { sessionKey: params.sessionKey } : {}),
138-
...(params.runId ? { runId: params.runId } : {}),
143+
...(agentId ? { agentId } : {}),
144+
...(sessionKey ? { sessionKey } : {}),
145+
...(runId ? { runId } : {}),
139146
...(params.kind ? { kind: params.kind } : {}),
140147
...(params.status ? { status: params.status } : {}),
141148
...(params.direction ? { direction: params.direction } : {}),

0 commit comments

Comments
 (0)