Skip to content
Closed
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
38 changes: 38 additions & 0 deletions src/install-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// #5548 P2: stable per-install identity for percent bucketing (§4.1) — same
// persistence discipline as the dismissal store (state dir, best-effort fs).
import fs from "node:fs";
import path from "node:path";
import { randomUUID } from "node:crypto";
import { stateDir } from "./paths.js";

const INSTALL_ID_FILE = "install-id";

function stateDirFor(env: NodeJS.ProcessEnv): string {
const v = env.XDG_STATE_HOME;
return v && v.length > 0 ? path.join(path.resolve(v), "billion-context") : stateDir();
}

let cached: { dir: string; id: string } | undefined;

/** Stable uuid persisted under the state dir. Returns "" when it cannot be
* persisted (read-only state) — percent-gated notices then hide rather than
* flicker buckets across restarts. */
export function installIdOf(env: NodeJS.ProcessEnv = process.env): string {
const dir = stateDirFor(env);
if (cached && cached.dir === dir) return cached.id;
try {
const file = path.join(dir, INSTALL_ID_FILE);
try {
const raw = fs.readFileSync(file, "utf8").trim();
if (/^[0-9a-f-]{36}$/i.test(raw)) return (cached = { dir, id: raw }).id;
} catch {
// first run — no file yet
}
const id = randomUUID();
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(file, id + "\n");
return (cached = { dir, id }).id;
} catch {
return (cached = { dir, id: "" }).id;
}
}
75 changes: 75 additions & 0 deletions src/local-announcements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// #5548 P2 lane B: local announcements — one validated record per *.json file
// under <configDir>/announcements/. The directory is re-scanned on every load;
// a result is memoized by a name:size:mtime signature so bad files log once
// per change instead of once per overview poll. A broken file never takes
// down the other files or the page (§六: any source unavailable → that source absent).
import fs from "node:fs";
import path from "node:path";
import { configDir } from "./paths.js";
import { log } from "./logger.js";
import { validateNoticeRecord, type NoticeRecord } from "./notices.js";

const ANNOUNCEMENTS_DIRNAME = "announcements";
export const LOCAL_FILE_CAP_BYTES = 64 * 1024; // §六.3 size cap

interface LocalAnnouncementLoad {
records: NoticeRecord[];
errors: Array<{ file: string; reason: string }>;
}

function announcementsDirFor(env: NodeJS.ProcessEnv): string {
const v = env.XDG_CONFIG_HOME;
const base = v && v.length > 0 ? path.join(path.resolve(v), "billion-context") : configDir();
return path.join(base, ANNOUNCEMENTS_DIRNAME);
}

let cache: { dir: string; sig: string; result: LocalAnnouncementLoad } | undefined;

export function loadLocalAnnouncements(env: NodeJS.ProcessEnv = process.env): LocalAnnouncementLoad {
const dir = announcementsDirFor(env);
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
// no directory = zero local notices, not an error
return { records: [], errors: [] };
}
const names = entries.filter((e) => e.isFile() && e.name.endsWith(".json")).map((e) => e.name).sort();
const parts: string[] = [];
const records: NoticeRecord[] = [];
const errors: LocalAnnouncementLoad["errors"] = [];
for (const name of names) {
const file = path.join(dir, name);
let st: fs.Stats;
try {
st = fs.statSync(file);
} catch {
continue; // vanished between readdir and stat
}
parts.push(`${name}:${st.size}:${st.mtimeMs}`);
if (st.size > LOCAL_FILE_CAP_BYTES) {
errors.push({ file: name, reason: `exceeds ${LOCAL_FILE_CAP_BYTES} bytes` });
continue;
}
let doc: unknown;
try {
doc = JSON.parse(fs.readFileSync(file, "utf8"));
} catch (e) {
errors.push({ file: name, reason: `bad JSON: ${String(e)}` });
continue;
}
const res = validateNoticeRecord(doc);
if (!res.ok) {
errors.push({ file: name, reason: res.reason });
continue;
}
if (res.droppedFields.length > 0) log("debug", `[notices] ${name}: dropped unknown field(s) ${res.droppedFields.join(", ")}`);
records.push(res.rec);
}
const sig = parts.join("|");
if (cache && cache.dir === dir && cache.sig === sig) return cache.result;
if (errors.length > 0) log("warn", `[notices] local lane: ${errors.map((e) => `${e.file} (${e.reason})`).join("; ")}`);
const result: LocalAnnouncementLoad = { records, errors };
cache = { dir, sig, result };
return result;
}
Loading
Loading