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
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
src/lib/exomem-hosted/__tests__/migration-runner.integration.test.ts
src/lib/exomem-hosted/__tests__/postgres.integration.test.ts
src/lib/exomem-hosted/__tests__/alert-receiver-postgres.integration.test.ts
src/lib/exomem-hosted/__tests__/backup-growth-postgres.integration.test.ts
src/lib/exomem-hosted/__tests__/access-recovery.test.ts
- name: Hosted Backup version-commit and lifecycle PostgreSQL authority, shared driver
env:
Expand Down
50 changes: 50 additions & 0 deletions migrations/0101_exomem_cloud_backup_growth.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- A claimed UUID can never open another window. Keep only the identity for old runs.
CREATE TABLE exomem_cloud_backup_growth_runs (
run_id uuid PRIMARY KEY
);

-- The singleton fences writers and holds the last accepted receipt across a successor's scan.
CREATE TABLE exomem_cloud_backup_growth_window (
singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton),
run_id uuid REFERENCES exomem_cloud_backup_growth_runs (run_id),
opened_at timestamptz,
accepted_run_id uuid REFERENCES exomem_cloud_backup_growth_runs (run_id),
accepted_at timestamptz,
accepted_batch_count integer,
scan_complete boolean,
scan_has_gaps boolean NOT NULL DEFAULT false,
resume_after text CHECK (resume_after ~ '^[a-z2-7]{16}$'),
accepted_observed_through timestamptz,
crossed_count integer
);

INSERT INTO exomem_cloud_backup_growth_window DEFAULT VALUES;

-- A report updates operator state only after its complete set of batches arrives.
CREATE TABLE exomem_cloud_backup_growth_batches (
ordinal integer PRIMARY KEY CHECK (ordinal > 0),
observations jsonb NOT NULL CHECK (jsonb_typeof(observations) = 'array')
);

CREATE TABLE exomem_cloud_backup_growth_state (
cell_id text COLLATE "C" PRIMARY KEY CHECK (cell_id ~ '^[a-z2-7]{16}$'),
retained_bytes bigint NOT NULL CHECK (retained_bytes >= 0),
ceiling_bytes bigint NOT NULL CHECK (ceiling_bytes > 0),
observed_from timestamptz NOT NULL,
observed_through timestamptz NOT NULL CHECK (observed_through >= observed_from),
active boolean NOT NULL CHECK (active = (retained_bytes > ceiling_bytes))
);

CREATE INDEX exomem_cloud_backup_growth_active
ON exomem_cloud_backup_growth_state (cell_id) WHERE active;

-- Later observations can replace state before an alert is delivered. Keep each alert's crossing evidence.
CREATE TABLE exomem_cloud_backup_growth_crossings (
run_id uuid NOT NULL REFERENCES exomem_cloud_backup_growth_runs (run_id),
cell_id text COLLATE "C" NOT NULL,
retained_bytes bigint NOT NULL CHECK (retained_bytes > ceiling_bytes),
ceiling_bytes bigint NOT NULL CHECK (ceiling_bytes > 0),
observed_from timestamptz NOT NULL,
observed_through timestamptz NOT NULL CHECK (observed_through >= observed_from),
PRIMARY KEY (run_id, cell_id)
);
16 changes: 15 additions & 1 deletion src/app/api/cron/exomem-key-watcher/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextRequest, NextResponse } from "next/server";
import { NextRequest, NextResponse, after } from "next/server";
import { verifyCronAuth } from "@/lib/hosted-backup/cron-auth";
import { emitAccessEvent, newRequestId } from "@/lib/exomem-hosted/http";
import {
Expand All @@ -7,6 +7,7 @@ import {
evaluateKeyWatcherHeartbeat,
runAlertNotificationPass,
} from "@/lib/exomem-hosted/alert-receiver";
import { pruneExpiredBackupGrowthBatches } from "@/lib/exomem-hosted/backup-growth-receiver";

export const runtime = "nodejs";
export const dynamic = "force-dynamic";
Expand All @@ -25,6 +26,19 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
emitAccessEvent({ event: "alerts.key_watcher.check", outcome: "denied", requestId, errorCode: "CRON_AUTH_REJECTED" });
return NextResponse.json({ success: false }, { status: 401, headers: { "cache-control": "no-store" } });
}
const logPruneFailure = () => {
emitAccessEvent({
event: "alerts.backup_growth.prune",
outcome: "failed",
requestId,
errorCode: "BACKUP_GROWTH_PRUNE_UNAVAILABLE",
});
};
try {
after(() => pruneExpiredBackupGrowthBatches().catch(logPruneFailure));
} catch {
logPruneFailure();
}
try {
const heartbeat = await evaluateKeyWatcherHeartbeat(new Date());
if (!heartbeat.enabled) {
Expand Down
37 changes: 37 additions & 0 deletions src/app/api/exomem/backup-growth/[token]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NextRequest, NextResponse } from "next/server";
import { alertEndpointNotFound, verifyAlertToken } from "@/lib/exomem-hosted/alert-receiver";
import {
BACKUP_GROWTH_MAX_BODY_BYTES,
receiveBackupGrowth,
} from "@/lib/exomem-hosted/backup-growth-receiver";
import { newRequestId, readBoundedJsonRequest } from "@/lib/exomem-hosted/http";
import {
capabilityEndpointNotFoundResponse,
safeErrorResponse,
} from "@/lib/exomem-hosted/next-error-response";

export const runtime = "nodejs";
export const dynamic = "force-dynamic";

export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ token: string }> }
): Promise<NextResponse> {
const requestId = newRequestId();
try {
const { token } = await params;
if (verifyAlertToken(token) !== "key-watcher") throw alertEndpointNotFound();
const payload = await readBoundedJsonRequest(request, BACKUP_GROWTH_MAX_BODY_BYTES);
return NextResponse.json(await receiveBackupGrowth(payload), {
headers: { "cache-control": "no-store" },
});
} catch (error) {
return safeErrorResponse(error, requestId);
}
}

export const GET = capabilityEndpointNotFoundResponse;
export const PUT = capabilityEndpointNotFoundResponse;
export const PATCH = capabilityEndpointNotFoundResponse;
export const DELETE = capabilityEndpointNotFoundResponse;
export const OPTIONS = capabilityEndpointNotFoundResponse;
Loading
Loading