|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + loadSatisfactionFloorStatus, |
| 4 | + SATISFACTION_FLOOR_LOOSENING_EVENT_TYPE, |
| 5 | + SATISFACTION_FLOOR_OVERRIDE_FLAG_KEY, |
| 6 | +} from "../../src/services/satisfaction-floor-loosening-run"; |
| 7 | +import { SATISFACTION_FLOOR_HARD_MINIMUM } from "../../src/services/satisfaction-floor-loosening"; |
| 8 | +import { LINKED_ISSUE_SATISFACTION_CONFIDENCE_FLOOR } from "../../src/services/linked-issue-satisfaction"; |
| 9 | +import { recordAuditEvent } from "../../src/db/repositories"; |
| 10 | +import { createApp } from "../../src/api/routes"; |
| 11 | +import { createTestEnv } from "../helpers/d1"; |
| 12 | + |
| 13 | +// Operator visibility for the loosening loop (#8161): the status read + its internal route. The loop's own |
| 14 | +// behavior lives in satisfaction-floor-loosening-run.test.ts — this file pins the reporting surface. |
| 15 | + |
| 16 | +const enabledEnv = () => createTestEnv({ SATISFACTION_FLOOR_AUTOTUNE_ENABLED: "true" as never }); |
| 17 | + |
| 18 | +async function setOverrideRow(env: Env, value: string) { |
| 19 | + await env.DB.prepare("INSERT INTO system_flags (key, value, updated_at) VALUES (?, ?, CURRENT_TIMESTAMP) ON CONFLICT(key) DO UPDATE SET value = excluded.value") |
| 20 | + .bind(SATISFACTION_FLOOR_OVERRIDE_FLAG_KEY, value) |
| 21 | + .run(); |
| 22 | +} |
| 23 | + |
| 24 | +describe("loadSatisfactionFloorStatus (#8161)", () => { |
| 25 | + it("reports the shipped defaults on a fresh deployment: flag off, no override, live floor = shipped, empty history", async () => { |
| 26 | + const status = await loadSatisfactionFloorStatus(createTestEnv()); |
| 27 | + expect(status).toEqual({ |
| 28 | + flagEnabled: false, |
| 29 | + shippedFloor: LINKED_ISSUE_SATISFACTION_CONFIDENCE_FLOOR, |
| 30 | + liveFloor: LINKED_ISSUE_SATISFACTION_CONFIDENCE_FLOOR, |
| 31 | + storedOverride: null, |
| 32 | + applied: [], |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("shows a lingering override row even while the flag is OFF, but liveFloor only follows it when ON", async () => { |
| 37 | + const offEnv = createTestEnv(); |
| 38 | + await setOverrideRow(offEnv, "0.4"); |
| 39 | + const offStatus = await loadSatisfactionFloorStatus(offEnv); |
| 40 | + expect(offStatus.storedOverride).toBe(0.4); |
| 41 | + expect(offStatus.flagEnabled).toBe(false); |
| 42 | + expect(offStatus.liveFloor).toBe(LINKED_ISSUE_SATISFACTION_CONFIDENCE_FLOOR); // flag off ⇒ shipped floor rules |
| 43 | + |
| 44 | + const onEnv = enabledEnv(); |
| 45 | + await setOverrideRow(onEnv, "0.4"); |
| 46 | + const onStatus = await loadSatisfactionFloorStatus(onEnv); |
| 47 | + expect(onStatus.liveFloor).toBe(0.4); |
| 48 | + }); |
| 49 | + |
| 50 | + it("rejects out-of-bounds or unparseable stored values from BOTH storedOverride and liveFloor", async () => { |
| 51 | + for (const bad of ["0.9", String(SATISFACTION_FLOOR_HARD_MINIMUM - 0.05), "junk", String(LINKED_ISSUE_SATISFACTION_CONFIDENCE_FLOOR)]) { |
| 52 | + const env = enabledEnv(); |
| 53 | + await setOverrideRow(env, bad); |
| 54 | + const status = await loadSatisfactionFloorStatus(env); |
| 55 | + expect(status.storedOverride).toBeNull(); |
| 56 | + expect(status.liveFloor).toBe(LINKED_ISSUE_SATISFACTION_CONFIDENCE_FLOOR); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + it("projects the applied history newest-first with both split verdicts, and a corrupt row degrades to nulls instead of vanishing", async () => { |
| 61 | + const env = enabledEnv(); |
| 62 | + await recordAuditEvent(env, { |
| 63 | + eventType: SATISFACTION_FLOOR_LOOSENING_EVENT_TYPE, |
| 64 | + actor: "loopover", |
| 65 | + targetKey: "linked_issue_scope_mismatch", |
| 66 | + outcome: "completed", |
| 67 | + metadata: { |
| 68 | + proposal: { |
| 69 | + currentFloor: 0.5, |
| 70 | + proposedFloor: 0.45, |
| 71 | + visibleCases: 24, |
| 72 | + heldOutCases: 7, |
| 73 | + visible: { verdict: "improved" }, |
| 74 | + heldOut: { verdict: "unchanged" }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + createdAt: "2026-07-20T00:00:00.000Z", |
| 78 | + }); |
| 79 | + await env.DB.prepare( |
| 80 | + "INSERT INTO audit_events (id, event_type, actor, target_key, outcome, detail, metadata_json, created_at) VALUES ('corrupt', ?, 'loopover', 'x', 'completed', '', 'not-json', '2026-07-21T00:00:00.000Z')", |
| 81 | + ) |
| 82 | + .bind(SATISFACTION_FLOOR_LOOSENING_EVENT_TYPE) |
| 83 | + .run(); |
| 84 | + |
| 85 | + // A parseable row whose proposal is not an object degrades the same way as unparseable JSON. |
| 86 | + await env.DB.prepare( |
| 87 | + "INSERT INTO audit_events (id, event_type, actor, target_key, outcome, detail, metadata_json, created_at) VALUES ('nonobject', ?, 'loopover', 'x', 'completed', '', '{\"proposal\": 5}', '2026-07-22T00:00:00.000Z')", |
| 88 | + ) |
| 89 | + .bind(SATISFACTION_FLOOR_LOOSENING_EVENT_TYPE) |
| 90 | + .run(); |
| 91 | + |
| 92 | + const status = await loadSatisfactionFloorStatus(env); |
| 93 | + expect(status.applied).toHaveLength(3); |
| 94 | + expect(status.applied[1]!.proposedFloor).toBeNull(); // the unparseable-JSON row |
| 95 | + expect(status.applied[0]).toEqual({ |
| 96 | + at: "2026-07-22T00:00:00.000Z", |
| 97 | + currentFloor: null, |
| 98 | + proposedFloor: null, |
| 99 | + visibleCases: null, |
| 100 | + heldOutCases: null, |
| 101 | + visibleVerdict: null, |
| 102 | + heldOutVerdict: null, |
| 103 | + }); |
| 104 | + expect(status.applied[2]).toEqual({ |
| 105 | + at: "2026-07-20T00:00:00.000Z", |
| 106 | + currentFloor: 0.5, |
| 107 | + proposedFloor: 0.45, |
| 108 | + visibleCases: 24, |
| 109 | + heldOutCases: 7, |
| 110 | + visibleVerdict: "improved", |
| 111 | + heldOutVerdict: "unchanged", |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it("fails safe to defaults + empty history on a DB error", async () => { |
| 116 | + const env = enabledEnv(); |
| 117 | + env.DB = { prepare: () => { throw new Error("boom"); } } as never; |
| 118 | + const status = await loadSatisfactionFloorStatus(env); |
| 119 | + expect(status.storedOverride).toBeNull(); |
| 120 | + expect(status.applied).toEqual([]); |
| 121 | + expect(status.liveFloor).toBe(LINKED_ISSUE_SATISFACTION_CONFIDENCE_FLOOR); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe("GET /v1/internal/calibration/satisfaction-floor (#8161)", () => { |
| 126 | + it("401s without the internal token, and is NOT flag-gated: 200s with the flag off (visibility must survive a flag flip)", async () => { |
| 127 | + const app = createApp(); |
| 128 | + const env = createTestEnv(); |
| 129 | + expect((await app.request("/v1/internal/calibration/satisfaction-floor", {}, env)).status).toBe(401); |
| 130 | + const res = await app.request("/v1/internal/calibration/satisfaction-floor", { headers: { authorization: `Bearer ${env.INTERNAL_JOB_TOKEN}` } }, env); |
| 131 | + expect(res.status).toBe(200); |
| 132 | + const body = (await res.json()) as { flagEnabled: boolean; liveFloor: number; applied: unknown[] }; |
| 133 | + expect(body.flagEnabled).toBe(false); |
| 134 | + expect(body.liveFloor).toBe(LINKED_ISSUE_SATISFACTION_CONFIDENCE_FLOOR); |
| 135 | + expect(JSON.stringify(body)).not.toMatch(/reward|payout|trust|wallet|hotkey|issueText|modelResponse/i); |
| 136 | + }); |
| 137 | +}); |
0 commit comments