Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(secret-value-tracking): keep a finished run's counters for the UI

Calvinfda194f35f45
diff --git a/backend/e2e-test/routes/v1/org-secret-value-tracking.spec.ts b/backend/e2e-test/routes/v1/org-secret-value-tracking.spec.ts
index b98be8b68d..fcf7859700 100644
--- a/backend/e2e-test/routes/v1/org-secret-value-tracking.spec.ts
+++ b/backend/e2e-test/routes/v1/org-secret-value-tracking.spec.ts
@@ -116,10 +116,9 @@ describe("Org-wide secret value tracking", () => {
const started = await enable(authToken);
expect(started.statusCode).toBe(200);
- // Progress counters live in the run's Redis key, which the final chunk deletes, so a completed
- // run reports zeros rather than its final totals. That is the point of keeping completion on the
- // durable flag: there is only ever one answer to "is it done".
- await waitForCompletion(authToken);
+ const finished = await waitForCompletion(authToken);
+ expect(finished.projectsTotal).toBeGreaterThanOrEqual(1);
+ expect(finished.projectsDone).toBeGreaterThanOrEqual(1);
const found = await searchByValue("written-before-tracking", authToken);
expect(found.statusCode).toBe(200);
diff --git a/backend/src/services/secret-value-tracking/secret-value-tracking-fns.test.ts b/backend/src/services/secret-value-tracking/secret-value-tracking-fns.test.ts
index 90dc34bd8e..37145a6371 100644
--- a/backend/src/services/secret-value-tracking/secret-value-tracking-fns.test.ts
+++ b/backend/src/services/secret-value-tracking/secret-value-tracking-fns.test.ts
@@ -237,3 +237,14 @@ describe("run state shape validation", () => {
expect(isUsableRunState({ status: "running", projectsTotal: 1, projectsDone: 0, secretsProcessed: 0 })).toBe(false);
});
});
+
+describe("a finished run's state", () => {
+ test("a completed state is usable, so its counters survive to the status endpoint", () => {
+ expect(isUsableRunState({ ...running(minutesAgo(1)), status: "completed", cursor: null })).toBe(true);
+ });
+
+ test("the flag stays the durable answer, and the state only supplies the numbers", () => {
+ const completed: TBackfillRunState = { ...running(minutesAgo(1)), status: "completed", cursor: null };
+ expect(resolveRunStatus(completed, true, NOW)).toEqual({ status: JobState.Completed });
+ });
+});
diff --git a/backend/src/services/secret-value-tracking/secret-value-tracking-fns.ts b/backend/src/services/secret-value-tracking/secret-value-tracking-fns.ts
index 397292e7d0..50ba4ce76d 100644
--- a/backend/src/services/secret-value-tracking/secret-value-tracking-fns.ts
+++ b/backend/src/services/secret-value-tracking/secret-value-tracking-fns.ts
@@ -126,7 +126,7 @@ export const resolveRunStatus = (
export const isUsableRunState = (value: unknown): value is TBackfillRunState => {
const state = value as Partial<TBackfillRunState> | null;
if (!state || typeof state !== "object") return false;
- if (state.status !== "running" && state.status !== "failed") return false;
+ if (state.status !== "running" && state.status !== "failed" && state.status !== "completed") return false;
if (typeof state.lastProgressAt !== "string") return false;
return (["projectsTotal", "projectsDone", "secretsProcessed"] as const).every((field) =>
diff --git a/backend/src/services/secret-value-tracking/secret-value-tracking-queue.test.ts b/backend/src/services/secret-value-tracking/secret-value-tracking-queue.test.ts
index f9758d811f..b9b5168c96 100644
--- a/backend/src/services/secret-value-tracking/secret-value-tracking-queue.test.ts
+++ b/backend/src/services/secret-value-tracking/secret-value-tracking-queue.test.ts
@@ -162,6 +162,28 @@ describe("the backfill job", () => {
expect(harness.flaggedOrgs).toEqual([ORG_ID]);
});
+ test("a finished run leaves its final counters behind for the UI to show", async () => {
+ const harness = makeHarness({
+ projectIds: ["p1", "p2"],
+ foldersByProject: { p1: ["f1"], p2: ["f2"] },
+ secrets: [
+ { id: "s1", key: "A", folderId: "f1", hasOrgDigest: false },
+ { id: "s2", key: "B", folderId: "f2", hasOrgDigest: false }
+ ]
+ });
+
+ harness.setState(runningState(null));
+ await harness.runChunk();
+
+ // The durable answer to "is it done" is still the flag. These numbers only let the UI show what
+ // the run got through, so they have to survive the run rather than be cleared with the cursor.
+ const finished = harness.getState();
+ expect(finished?.status).toBe("completed");
+ expect(finished?.projectsDone).toBe(2);
+ expect(finished?.secretsProcessed).toBe(2);
+ expect(finished?.cursor).toBeNull();
+ });
+
test("a cursor on a project deleted since the last chunk carries on with the rest", async () => {
const harness = makeHarness({
projectIds: ["p1", "p2"],
diff --git a/backend/src/services/secret-value-tracking/secret-value-tracking-queue.ts b/backend/src/services/secret-value-tracking/secret-value-tracking-queue.ts
index a1a8279f93..e8c0a9ba0d 100644
--- a/backend/src/services/secret-value-tracking/secret-value-tracking-queue.ts
+++ b/backend/src/services/secret-value-tracking/secret-value-tracking-queue.ts
@@ -256,7 +256,16 @@ export const secretValueTrackingQueueFactory = ({
if (done) {
if (scope.scope === "org") await orgDAL.updateById(scope.orgId, { orgWideSecretValueTrackingEnabled: true });
- await state.clear(scopeId);
+ // Kept rather than cleared, so the UI can show what the run got through. The flag above is
+ // what actually answers "is it done", and this key is free to expire.
+ await state.write(scopeId, {
+ status: "completed",
+ cursor: null,
+ projectsTotal: runState.projectsTotal,
+ projectsDone,
+ secretsProcessed,
+ lastProgressAt: new Date().toISOString()
+ });
logger.info(
`SecretValueTrackingBackfill: complete [scopeId=${scopeId}] [projectsDone=${projectsDone}] [secretsProcessed=${secretsProcessed}]`
);
diff --git a/backend/src/services/secret-value-tracking/secret-value-tracking-state.ts b/backend/src/services/secret-value-tracking/secret-value-tracking-state.ts
index e72699473d..a58ea34e18 100644
--- a/backend/src/services/secret-value-tracking/secret-value-tracking-state.ts
+++ b/backend/src/services/secret-value-tracking/secret-value-tracking-state.ts
@@ -39,7 +39,8 @@ export const secretValueTrackingStateFactory = ({ keyStore }: TSecretValueTracki
};
const isStalled = (state: TBackfillRunState) => {
- if (state.status === "failed") return true;
+ // A finished run holds the key only to keep its counters readable, so it never blocks a new one.
+ if (state.status === "failed" || state.status === "completed") return true;
const lastProgress = new Date(state.lastProgressAt).getTime();
return Number.isNaN(lastProgress) || Date.now() - lastProgress > BACKFILL_STALE_AFTER_MS;
};
diff --git a/backend/src/services/secret-value-tracking/secret-value-tracking-types.ts b/backend/src/services/secret-value-tracking/secret-value-tracking-types.ts
index a1871c7446..622303deff 100644
--- a/backend/src/services/secret-value-tracking/secret-value-tracking-types.ts
+++ b/backend/src/services/secret-value-tracking/secret-value-tracking-types.ts
@@ -8,7 +8,9 @@ export type TBackfillCursor = {
export type TBackfillScope = { scope: "org"; orgId: string } | { scope: "project"; projectId: string };
export type TBackfillRunState = {
- status: "running" | "failed";
+ // "completed" carries the final counters so the UI can show what the run got through. It is never
+ // the answer to "is it done": that is the durable flag, and this key is allowed to expire.
+ status: "running" | "failed" | "completed";
cursor: TBackfillCursor | null;
projectsTotal: number;
projectsDone: number;