Skip to content
Merged
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
20 changes: 13 additions & 7 deletions nixos/services/pm-stack/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@
# credentials BY NAME, exactly as the hand units consumed them).
# pm-dispatch's env files are external the same way:
# ~/pm-stack/dispatch/.env (rendered by pm-stack/dispatch/install.sh
# from the same SOPS file) and ~/secrets/dispatch.env (operator secrets
# that are not in SOPS). install.sh remains the env renderer and the
# fallback installer; this module owns the unit lifecycle.
# from the same SOPS file — it carries the GitHub App material and the
# fleet Supabase config: supabase_url + supabase_service_role_key for
# muse-dev2) and ~/secrets/dispatch.env (operator-only secrets that
# are not in SOPS: DISPATCH_API_TOKEN). install.sh remains the env
# renderer and the fallback installer; this module owns the unit
# lifecycle.
#
# The Kaneo stack is the exception to the checkout model: pm-kaneo
# runs deploy.sh from the ~/pm-stack deployment dir, because the
Expand Down Expand Up @@ -171,10 +174,13 @@ in
WorkingDirectory = "${repo}/pm-stack/dispatch";
ExecStart = "${bun} run src/server.ts";
# The env files stay outside the module, exactly like the
# aggregator's: install.sh renders the sops-derived file,
# and the operator file is laptop-only. The "-" keeps the
# hand unit's semantics — a missing operator file degrades
# the service instead of crash-looping it.
# aggregator's: install.sh renders the sops-derived file
# (GitHub App + Supabase config), and the operator file is
# laptop-only (DISPATCH_API_TOKEN). The "-" keeps the hand
# unit's semantics — a missing operator file degrades the
# service instead of crash-looping it. systemd applies the
# files in order, so the operator file wins on a collision:
# it must never carry SUPABASE_* lines (install.sh warns).
EnvironmentFile = [
"${stackDir}/dispatch/.env"
"-${home}/secrets/dispatch.env"
Expand Down
5 changes: 4 additions & 1 deletion pm-stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ All secrets live in [`../secrets/pm-stack.sops.yaml`](../secrets/pm-stack.sops.y
`cloudflare_tunnel_token` — plus the Phase 2 additions:
`pad_api_key`, `pad_admin_email`, `pad_admin_password`,
`pad_api_token`, `grist_session_secret`, `grist_admin_password`,
`grist_api_key`, `grist_boot_key`. The same values are also filed in
`grist_api_key`, `grist_boot_key` — plus the pm-dispatch Supabase
additions: `supabase_url`, `supabase_service_role_key` (fleet
Supabase is muse-dev2; Bitwarden source:
`supabase.muse-dev2.service-role-key`). The same values are also filed in
Bitwarden and 1Password under the names recorded in the design
record (Bitwarden/1Password names use dots, e.g. `pad.api-token`,
`grist.boot-key`).
Expand Down
7 changes: 4 additions & 3 deletions pm-stack/dispatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ touching any caller.
| `DISPATCH_API_TOKEN` | — | from `~/secrets/dispatch.env` |
| `DISPATCH_OIDC_AUDIENCE` | `pm-dispatch` | |
| `DISPATCH_REPO_ALLOWLIST` | the six fleet repos | |
| `SUPABASE_URL` | `https://nltgxorqwxhgfzseiirt.supabase.co` | |
| `SUPABASE_SERVICE_ROLE_KEY` | — | from `~/secrets/dispatch.env` (Bitwarden `supabase.service-role-key`) |
| `SUPABASE_URL` | — (required, no default) | rendered by install.sh from `~/secrets/pm-stack.sops.yaml` (`supabase_url`, muse-dev2) |
| `SUPABASE_SERVICE_ROLE_KEY` | — | rendered by install.sh from `~/secrets/pm-stack.sops.yaml` (`supabase_service_role_key`; Bitwarden source `supabase.muse-dev2.service-role-key`) |
| `OPENCODE_URL` | `http://127.0.0.1:13420` | laptop standing server |
| `OPENCODE_AUTH_FILE` | `~/.local/state/opencode/service-prod.json` | password read at runtime, never logged |
| `GITHUB_APP_ID` / `GITHUB_APP_PRIVATE_KEY_BASE64` | — | rendered by install.sh from `~/secrets/pm-stack.sops.yaml` |
Expand Down Expand Up @@ -515,7 +515,8 @@ bash pm-stack/dispatch/install.sh
```

install.sh copies the tree to `~/pm-stack/dispatch`, renders `.env`
from the sops file, ensures `~/secrets/dispatch.env` exists (generating
from the sops file (GitHub App material and the fleet Supabase
config), ensures `~/secrets/dispatch.env` exists (generating
`DISPATCH_API_TOKEN` if absent — value never printed), installs
`pm-stack/systemd/pm-dispatch.service` as a user unit, restarts only on
change, and fails loudly unless `/health` reports the deployed build
Expand Down
36 changes: 25 additions & 11 deletions pm-stack/dispatch/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@
# deployed.
#
# Secrets layout:
# - From the sops file: github_app_id, github_private_key_base64
# (rendered into ~/pm-stack/dispatch/.env, mode 600).
# - NOT in sops: SUPABASE_SERVICE_ROLE_KEY and DISPATCH_API_TOKEN live
# in ~/secrets/dispatch.env (mode 600, laptop-only), which the unit
# also reads. This script ensures that file exists and carries a
# generated DISPATCH_API_TOKEN when one is absent; the Supabase key is
# placed there by the operator (Bitwarden: supabase.service-role-key)
# and is never generated or printed here.
# - From the sops file: github_app_id, github_private_key_base64,
# supabase_url, supabase_service_role_key (rendered into
# ~/pm-stack/dispatch/.env, mode 600). Fleet Supabase is muse-dev2;
# the key's source of truth is Bitwarden
# `supabase.muse-dev2.service-role-key`. (The un-namespaced
# `supabase.service-role-key` in Bitwarden is the DELETED legacy
# muse-dev project's key — never use it; see code#5120.)
# - NOT in sops: DISPATCH_API_TOKEN lives in ~/secrets/dispatch.env
# (mode 600, laptop-only), which the unit also reads. This script
# ensures that file exists and carries a generated
# DISPATCH_API_TOKEN when one is absent. SUPABASE_URL /
# SUPABASE_SERVICE_ROLE_KEY lines left in that file are stale: the
# unit reads it AFTER the rendered file, so they would override the
# sops-managed values — this script warns about them below, and the
# operator copies should be deleted.
#
# Usage (on the laptop, from the repo checkout): bash install.sh
set -euo pipefail
Expand Down Expand Up @@ -47,8 +54,8 @@ if ! grep -q '^DISPATCH_API_TOKEN=' "$SECRETS_ENV"; then
chmod 600 "$SECRETS_ENV"
echo "generated a DISPATCH_API_TOKEN into $SECRETS_ENV (value not printed)"
fi
if ! grep -q '^SUPABASE_SERVICE_ROLE_KEY=' "$SECRETS_ENV"; then
echo "WARNING: $SECRETS_ENV has no SUPABASE_SERVICE_ROLE_KEY — queue endpoints will 502 until the operator adds it (Bitwarden: supabase.service-role-key)" >&2
if grep -q '^SUPABASE_URL=\|^SUPABASE_SERVICE_ROLE_KEY=' "$SECRETS_ENV"; then
echo "WARNING: $SECRETS_ENV still carries SUPABASE_URL / SUPABASE_SERVICE_ROLE_KEY lines — Supabase config is sops-managed now (rendered into $DST/.env from $SOPS), and because the unit reads $SECRETS_ENV after the rendered file, those lines override the managed values. Delete them from $SECRETS_ENV." >&2
fi

# --- Change detection + build marker ------------------------------------
Expand Down Expand Up @@ -101,7 +108,8 @@ if [ "$CHANGED" -eq 1 ]; then
cp -r "$SRC/test" "$DST/test"
cp -r "$SRC/examples" "$DST/examples"

# Service env: non-secret config + the GitHub App material from sops.
# Service env: non-secret config + the GitHub App material and the
# Supabase config from sops.
umask 077
ENV_FILE="$DST/.env"
{
Expand All @@ -114,6 +122,12 @@ if [ "$CHANGED" -eq 1 ]; then
else
echo "WARNING: github app keys not readable from $SOPS — issue creation will use the gh CLI fallback" >&2
fi
if [ -f "$SOPS" ] && sops -d --extract '["supabase_url"]' "$SOPS" >/dev/null 2>&1 && sops -d --extract '["supabase_service_role_key"]' "$SOPS" >/dev/null 2>&1; then
echo "SUPABASE_URL=$(sops -d --extract '["supabase_url"]' "$SOPS")"
echo "SUPABASE_SERVICE_ROLE_KEY=$(sops -d --extract '["supabase_service_role_key"]' "$SOPS")"
else
echo "WARNING: supabase_url / supabase_service_role_key not readable from $SOPS — the rendered env will lack SUPABASE_URL and pm-dispatch will refuse to start (config fails loudly, code#5120)" >&2
fi
} >"$ENV_FILE"
chmod 600 "$ENV_FILE"

Expand Down
13 changes: 11 additions & 2 deletions pm-stack/dispatch/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Environment configuration. Secrets are read by NAME from the environment
// only — never committed, never printed. Everything has a fixture-safe default.
// only — never committed, never printed. Everything has a fixture-safe
// default except SUPABASE_URL, which is required: its previous default
// pointed at the deleted legacy muse-dev project (#5120), so a missing
// value now fails loudly at startup instead of silently queueing nowhere.
import { homedir } from "node:os";
import { join } from "node:path";

Expand Down Expand Up @@ -97,14 +100,20 @@ export function loadConfig(env: Record<string, string | undefined> = process.env
appPem = null;
}
}
const supabaseUrl = env.SUPABASE_URL?.trim();
if (!supabaseUrl) {
throw new Error(
"SUPABASE_URL is not set: pm-dispatch requires the fleet Supabase (muse-dev2) project URL. There is no default — the previous one pointed at the deleted legacy muse-dev project (#5120). The laptop deployment renders it from the pm-stack SOPS file (pm-stack/dispatch/install.sh).",
);
}
return {
port,
hosts,
buildVersion: s(env.DISPATCH_BUILD_VERSION, "dev"),
apiToken: env.DISPATCH_API_TOKEN?.trim() ? env.DISPATCH_API_TOKEN.trim() : null,
oidcAudience: s(env.DISPATCH_OIDC_AUDIENCE, "pm-dispatch"),
repoAllowlist: allowlist,
supabaseUrl: s(env.SUPABASE_URL, "https://nltgxorqwxhgfzseiirt.supabase.co").replace(/\/$/, ""),
supabaseUrl: supabaseUrl.replace(/\/$/, ""),
supabaseServiceRoleKey: env.SUPABASE_SERVICE_ROLE_KEY?.trim() ? env.SUPABASE_SERVICE_ROLE_KEY.trim() : null,
opencodeUrl: s(env.OPENCODE_URL, "http://127.0.0.1:13420").replace(/\/$/, ""),
opencodeAuthFile: s(env.OPENCODE_AUTH_FILE, join(homedir(), ".local", "state", "opencode", "service-prod.json")),
Expand Down
7 changes: 6 additions & 1 deletion pm-stack/dispatch/test/attach_verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
// bogus target). These tests stub the GitHub lookup/client — no network.
import { describe, expect, test } from "bun:test";
import { generateKeyPairSync } from "node:crypto";
import { verifyAttachTarget, type AttachTargetInfo } from "../src/server.ts";
import type { AttachTargetInfo } from "../src/server.ts";

// src/server.ts loads config at import; loadConfig requires SUPABASE_URL
// (#5120), so pin a fixture project before the dynamic import below.
process.env.SUPABASE_URL ??= "https://fixture.supabase.co";
const { verifyAttachTarget } = await import("../src/server.ts");
import { GitHubClient, isNotFoundError } from "../src/github.ts";

const REPO = "developing-today/code";
Expand Down
26 changes: 20 additions & 6 deletions pm-stack/dispatch/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { describe, expect, test } from "bun:test";
import { APP_INSTALLED_REPOS, FLEET_REPOS, REPO_DIRS, REPO_QUEUES, loadConfig } from "../src/config.ts";

/** loadConfig requires SUPABASE_URL (#5120); tests use a fixture project. */
const ENV = { SUPABASE_URL: "https://fixture.supabase.co" };

describe("fleet config", () => {
for (const value of ["abc", "4879.5", "0", "-1", "99999", "0x1234", "1e4"]) {
test(`rejects invalid DISPATCH_PORT ${JSON.stringify(value)}`, () => {
Expand All @@ -9,10 +12,10 @@ describe("fleet config", () => {
}

test("keeps the default and accepts a decimal DISPATCH_PORT", () => {
expect(loadConfig({}).port).toBe(4879);
expect(loadConfig({ DISPATCH_PORT: "" }).port).toBe(4879);
expect(loadConfig({ DISPATCH_PORT: " " }).port).toBe(4879);
expect(loadConfig({ DISPATCH_PORT: "4879" }).port).toBe(4879);
expect(loadConfig({ ...ENV }).port).toBe(4879);
expect(loadConfig({ ...ENV, DISPATCH_PORT: "" }).port).toBe(4879);
expect(loadConfig({ ...ENV, DISPATCH_PORT: " " }).port).toBe(4879);
expect(loadConfig({ ...ENV, DISPATCH_PORT: "4879" }).port).toBe(4879);
});

for (const value of [",", " , ,"]) {
Expand All @@ -22,7 +25,7 @@ describe("fleet config", () => {
}

test("keeps a normal DISPATCH_HOSTS list unchanged", () => {
expect(loadConfig({ DISPATCH_HOSTS: "0.0.0.0, 127.0.0.1" }).hosts).toEqual(["0.0.0.0", "127.0.0.1"]);
expect(loadConfig({ ...ENV, DISPATCH_HOSTS: "0.0.0.0, 127.0.0.1" }).hosts).toEqual(["0.0.0.0", "127.0.0.1"]);
});

test("the allowlist default is exactly the six fleet repos", () => {
Expand All @@ -34,7 +37,18 @@ describe("fleet config", () => {
"developing-today/zettel",
"dezren39/dezren39",
]);
expect(loadConfig({}).repoAllowlist).toEqual([...FLEET_REPOS]);
expect(loadConfig({ ...ENV }).repoAllowlist).toEqual([...FLEET_REPOS]);
});

test("SUPABASE_URL is required and has no default (#5120)", () => {
expect(() => loadConfig({})).toThrow("SUPABASE_URL is not set");
expect(() => loadConfig({ SUPABASE_URL: "" })).toThrow("SUPABASE_URL is not set");
expect(() => loadConfig({ SUPABASE_URL: " " })).toThrow("SUPABASE_URL is not set");
});

test("SUPABASE_URL is used verbatim, minus a trailing slash", () => {
expect(loadConfig({ SUPABASE_URL: "https://fixture.supabase.co" }).supabaseUrl).toBe("https://fixture.supabase.co");
expect(loadConfig({ SUPABASE_URL: "https://fixture.supabase.co/" }).supabaseUrl).toBe("https://fixture.supabase.co");
});

test("every fleet repo has a queue and a session directory", () => {
Expand Down
5 changes: 4 additions & 1 deletion pm-stack/dispatch/test/dispatch_preexisting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
// orchestration (enqueueDispatchTask) with a stubbed queue — no network,
// no Supabase.
import { describe, expect, test } from "bun:test";
import { enqueueDispatchTask, isPreExistingTaskRow } from "../src/server.ts";
// src/server.ts loads config at import; loadConfig requires SUPABASE_URL
// (#5120), so pin a fixture project before the dynamic import below.
process.env.SUPABASE_URL ??= "https://fixture.supabase.co";
const { enqueueDispatchTask, isPreExistingTaskRow } = await import("../src/server.ts");
import type { TaskRow } from "../src/queue.ts";

// BEFORE/AFTER stand in for the two skew directions: a DB-behind fresh
Expand Down
4 changes: 4 additions & 0 deletions pm-stack/dispatch/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ function signOidc(repository: string): string {
return `${header}.${payload}.${b64url(signer.sign(privateKey))}`;
}

// src/server.ts loads config at import; loadConfig requires SUPABASE_URL
// (#5120), so the suite pins a fixture project (no service-role key: the
// queue stays disabled, as these tests intend).
process.env.SUPABASE_URL ??= "https://fixture.supabase.co";
const { handler } = await import("../src/server.ts");

function sessionsRequest(token: string, body: Record<string, unknown>): Request {
Expand Down
9 changes: 6 additions & 3 deletions pm-stack/systemd/pm-dispatch.service
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ WorkingDirectory=%h/pm-stack/dispatch
# Cloudflare tunnel (amd-6-pm-stack) once Drew adds it. Listens on 4879.
ExecStart=/run/current-system/sw/bin/bun run src/server.ts
# Rendered by install.sh from ~/secrets/pm-stack.sops.yaml (mode 600,
# never committed): build version, bind hosts, GitHub App material.
# never committed): build version, bind hosts, GitHub App material,
# and the fleet Supabase config (SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY).
EnvironmentFile=%h/pm-stack/dispatch/.env
# Operator secrets that are NOT in the sops file (mode 600, laptop-only):
# SUPABASE_SERVICE_ROLE_KEY, DISPATCH_API_TOKEN. Optional-file syntax so
# a missing file degrades to "queue/bearer unavailable", not a crash loop.
# DISPATCH_API_TOKEN only. Optional-file syntax so a missing file
# degrades to bearer-auth-off, not a crash loop. This file is read
# AFTER the rendered file, so it must never carry SUPABASE_* lines —
# they would override the sops-managed Supabase config above.
EnvironmentFile=-%h/secrets/dispatch.env
Restart=on-failure
RestartSec=5
Expand Down
Loading