Files
loki/scripts/apply-project-attrs.ts
CatoandClaude Fable 5.1 95c40a9cd8 refactor: rename the product from FleetCrown to Loki (#695)
The product, its assistant, the repo, the host and every unit on the box now
share one name. 638 files, ~3,000 mentions: UI copy, config, env keys
(FLEETCROWN_* -> LOKI_*), systemd units (fleetcrown-* -> loki-*), docs,
articles, the deploy workflow other repos reuse, and nine file names.

- Three article slugs carried the old name and had been shared; permanent
  redirects keep those links alive (next.config.ts).
- test-no-retired-name.sh now also forbids the old product name across the
  whole repo, so the rename cannot creep back through templates and examples.
- ASSISTANT_STACK collapses the product and the assistant into one entry,
  because they are now the same name.

Infra was cut over ahead of this merge: /opt/loki, loki-* units, the `loki`
database and role, loki.orangecat.ch (old host 308s to it; /api, /widget,
/embed keep proxying), GitHub repos bitbaum/loki and bitbaum/loki-releases.
The Resend sending domain stays fleetcrown.orangecat.ch until the new domain
is verified in DNS.


Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 16:13:10 -07:00

88 lines
3.3 KiB
TypeScript

// Write curated project attributes from a JSON file.
//
// The extractor (scripts/enrich-prod-profiles.ts) is the normal path. This is
// the escape hatch for when the model's budget is exhausted or a field is worth
// writing by hand: same validation, same upsert, same ownership check — the
// only difference is where the text came from.
//
// The JSON is `{ "<project name>": { "<attr key>": "<value>", … }, … }`, keyed
// by entities.name. Keys must exist in PROJECT_ATTR; values are validated by
// the same schema the model output goes through, so nothing can be written here
// that the extractor could not have written.
//
// Usage:
// npx tsx scripts/apply-project-attrs.ts <file.json> # dry run
// npx tsx scripts/apply-project-attrs.ts <file.json> --apply
//
// Env: DB creds from .env.hetzner.local, or AUDIT_DATABASE_URL for a tunnel
// (`eval "$(bash scripts/db-tunnel.sh)"`).
import { readFileSync } from "fs";
import { config } from "dotenv";
config({ path: ".env.local" });
config({ path: ".env.hetzner.local" });
const APPLY = process.argv.includes("--apply");
const FILE = process.argv.slice(2).find((a) => !a.startsWith("--"));
async function main() {
if (!FILE) throw new Error("usage: apply-project-attrs.ts <file.json> [--apply]");
if (process.env.AUDIT_DATABASE_URL) {
process.env.DATABASE_URL = process.env.AUDIT_DATABASE_URL;
} else if (!process.env.DATABASE_URL) {
const password = process.env.LOKI_DB_PASSWORD;
const host = process.env.HETZNER_IP;
if (!password || !host)
throw new Error("LOKI_DB_PASSWORD / HETZNER_IP missing from .env.hetzner.local");
process.env.DATABASE_URL = `postgres://loki:${encodeURIComponent(password)}@${host}:5432/loki?sslmode=require`;
}
const { db } = await import("../src/db");
const { entities } = await import("../src/db/schema");
const { eq } = await import("drizzle-orm");
const { ExtractedProfileSchema, applyProjectProfile } = await import("../src/lib/project-brief");
const input = JSON.parse(readFileSync(FILE, "utf-8")) as Record<string, Record<string, string>>;
const projects = await db
.select({ id: entities.id, userId: entities.userId, name: entities.name })
.from(entities)
.where(eq(entities.type, "project"));
let written = 0;
for (const [name, attrs] of Object.entries(input)) {
const p = projects.find((x) => x.name.toLowerCase() === name.toLowerCase());
if (!p) {
console.log(`— ${name}: no such project, skipped`);
continue;
}
// Same schema as model output: an unknown key or an over-long value fails
// here rather than landing in the DB.
const parsed = ExtractedProfileSchema.strict().safeParse(attrs);
if (!parsed.success) {
const issue = parsed.error.issues[0];
console.log(`✗ ${name}: ${issue?.path.join(".")} ${issue?.message}`);
continue;
}
console.log(`${APPLY ? "✚" : "DRY"} ${p.name}: ${Object.keys(parsed.data).join(", ")}`);
if (APPLY) {
const ok = await applyProjectProfile(p.userId, p.id, parsed.data);
if (!ok) {
console.log(`✗ ${p.name}: apply failed`);
continue;
}
}
written++;
}
console.log(`\n${APPLY ? "applied" : "would apply"}: ${written}/${Object.keys(input).length}`);
process.exit(0);
}
main().catch((e) => {
console.error("FAIL:", e);
process.exit(1);
});