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
2 changes: 2 additions & 0 deletions doc/fleet-schedule-registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ The OS-timer half of surface 2. Cadence is the unit's own `OnCalendar`/`OnUnitAc
| `openclaw-backup.timer` | `OnCalendar=*-*-*` | `openclaw-backup.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-03 |
| `opencode-usage-export.timer` | `OnCalendar=*-*-*` | `opencode-usage-export.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-03 |
| `pm-dashboard-backup.timer` | `OnCalendar=*-*-*` | `pm-dashboard-backup.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-03 |
| `pm-digest-daily.timer` | `OnCalendar=*-*-* 07:00` | `pm-digest-daily.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-03 |
| `pm-digest-weekly.timer` | `OnCalendar=Mon 07:05` | `pm-digest-weekly.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-03 |
| `pm-git-sync.timer` | `OnUnitActiveUSec=30s,OnBootUSec=30s` | `pm-git-sync.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-03 |
| `pm-kaneo.timer` | `OnUnitActiveUSec=30min,OnBootUSec=1min` | `pm-kaneo.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-03 |
| `pm-projector-reconcile.timer` | `OnCalendar=*-*-*` | `pm-projector-reconcile.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-03 |
Expand Down
61 changes: 61 additions & 0 deletions nixos/services/pm-stack/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ in
# checkout (Codex P1 on PR #5141, same failure class as
# code#4906 above).
"PM_AGG_ACTIVITY_FILE=${aggState}/activity-ingestion.json"
# The digest archive (#1278) is runtime state too: one
# JSON per day, rendered verbatim by /digest?date=<date>.
"PM_AGG_DIGESTS_DIR=${aggState}/digests"
];
Restart = "on-failure";
RestartSec = 5;
Expand Down Expand Up @@ -239,6 +242,42 @@ in
};
};

pm-digest-daily = {
Unit = {
Description = "PM daily digest — generate + archive + deliver the day's digest (NixOS; runs from ~/git/code checkout, #1278)";
After = [ "network-online.target" "pm-aggregator.service" ];
};
Service = {
Type = "oneshot";
TimeoutStartSec = 300;
WorkingDirectory = "${repo}/pm-stack/aggregator";
EnvironmentFile = "${aggState}/.env";
Environment = [
"PM_AGG_SOURCE=live"
"PM_AGG_DIGESTS_DIR=${aggState}/digests"
];
ExecStart = "${bun} run ${repo}/pm-stack/aggregator/scripts/generate-digest.ts --kind daily";
};
};

pm-digest-weekly = {
Unit = {
Description = "PM weekly digest — generate + archive + deliver the week's roll-up (NixOS; runs from ~/git/code checkout, #1278)";
After = [ "network-online.target" "pm-aggregator.service" ];
};
Service = {
Type = "oneshot";
TimeoutStartSec = 300;
WorkingDirectory = "${repo}/pm-stack/aggregator";
EnvironmentFile = "${aggState}/.env";
Environment = [
"PM_AGG_SOURCE=live"
"PM_AGG_DIGESTS_DIR=${aggState}/digests"
];
ExecStart = "${bun} run ${repo}/pm-stack/aggregator/scripts/generate-digest.ts --kind weekly";
};
};

fleet-feed-collector = {
Unit = {
Description = "Fleet feed collector for the PM aggregator — collect the fleet feed every ~5 minutes (NixOS; runs from ~/git/code checkout, #3363)";
Expand Down Expand Up @@ -366,6 +405,28 @@ in
Install.WantedBy = [ "timers.target" ];
};

pm-digest-daily = {
Unit.Description = "Run the PM daily digest every morning at 07:00 local (pm-stack, #1278)";
Timer = {
OnCalendar = "*-*-* 07:00:00";
Comment thread
dezren39 marked this conversation as resolved.
Resolved
RandomizedDelaySec = 60;
Persistent = true;
Unit = "pm-digest-daily.service";
};
Install.WantedBy = [ "timers.target" ];
};

pm-digest-weekly = {
Unit.Description = "Run the PM weekly digest on Mondays at 07:05 local (pm-stack, #1278)";
Timer = {
OnCalendar = "Mon *-*-* 07:05:00";
RandomizedDelaySec = 60;
Persistent = true;
Unit = "pm-digest-weekly.service";
};
Install.WantedBy = [ "timers.target" ];
};

fleet-feed-collector = {
Unit.Description = "Collect the fleet feed for the laptop aggregator every ~5 minutes";
Timer = {
Expand Down
107 changes: 107 additions & 0 deletions pm-stack/aggregator/scripts/generate-digest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env bun
// Generate the scheduled PM digest (#1278): build it from the canonical
// projection (the same Source the dashboard serves), archive it as
// JSON in the digests dir — the /digest/<date> page renders that file
// verbatim — and deliver it to chat via Web Push to the stored push
// subscriptions (the stack's delivery surface; class "digest").
//
// Usage:
// bun run scripts/generate-digest.ts [--kind daily|weekly] [--no-deliver]
//
// The weekly timer passes --kind weekly on the configured day
// (systemd OnCalendar=Mon, see systemd/digest-weekly.timer and
// nixos/services/pm-stack). Delivery failure never fails the archive:
// the digest is written first and the exit code reports delivery only
// when --strict-deliver is passed.
import { loadConfig } from "../src/config.ts";
import { buildDigest, digestText, writeDigestArchive, type DigestKind } from "../src/digest.ts";
import { fixtureSource } from "../src/sources/fixtures.ts";
import { liveSource } from "../src/sources/live.ts";
import { classEnabled, getAlertsStore, inQuietHours } from "../src/alerts.ts";
import { sendPush, type PushTarget } from "../src/push.ts";

function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`);
return i >= 0 ? process.argv[i + 1] : undefined;
}

async function main(): Promise<void> {
const kind = (arg("kind") ?? "daily") as DigestKind;
if (kind !== "daily" && kind !== "weekly") {
console.error(`generate-digest: --kind must be daily|weekly, got ${kind}`);
process.exit(2);
}
const cfg = loadConfig();
const source = cfg.source === "live" ? liveSource(cfg) : fixtureSource();
// Prime the items facet first (#1267 pattern): items() records the
// pool's feed freshness into provenance; projects() alone does not.
await source.items([]).catch(() => null);
const [pool, needsDrew, quota, poolAvailable] = await Promise.all([
source.projects(),
source.needsDrew ? source.needsDrew().catch(() => null) : Promise.resolve(null),
source.quota().catch(() => null),
source.projectsAvailable ? source.projectsAvailable().catch(() => undefined) : Promise.resolve(undefined),
]);
const digest = buildDigest({
pool,
needsDrew,
quota,
provenanceDetail: source.provenanceDetail?.() ?? null,
Comment thread
dezren39 marked this conversation as resolved.
Resolved
poolAvailable,
sourceKind: cfg.source,
now: new Date(),
kind,
});
const file = await writeDigestArchive(cfg.digestsDir, digest);
console.log(`generate-digest: archived ${kind} digest for ${digest.date} → ${file}`);
console.log(digestText(digest));

if (process.argv.includes("--no-deliver")) return;
// Delivery: Web Push to every stored subscription, mirroring the
// alert throat's send bookkeeping (gone subscriptions are noted,
// sends are logged under class "digest"). No store (Supabase
// unavailable) = no delivery surface; the archive still stands.
const store = await getAlertsStore();
if (!store) {
console.error("generate-digest: no alerts store — delivery skipped, archive stands");
return;
}
const subs = await store.listSubscriptions();
const payload = {
class: "digest",
objectRef: digest.archiveRoute,
url: digest.archiveRoute,
title: `PM ${kind} digest — ${digest.date}`,
body: digest.sections
.map((s) => `${s.title}: ${s.items.length}${s.stale ? " (stale)" : ""}`)
.join(" · "),
};
let sent = 0;
const now = new Date();
for (const sub of subs) {
// Subscription preferences are promises (#3676): a class the
// subscriber turned off gets nothing, and a non-breakthrough class
// like digest honors quiet hours (suppression is ledgered).
if (!classEnabled(sub, "digest")) continue;
if (inQuietHours(sub.quiet, now)) {
await store.logSend({ class: "digest", objectRef: digest.archiveRoute, outcome: "suppressed-quiet" });
continue;
}
const target: PushTarget = { endpoint: sub.endpoint, p256dh: sub.p256dh, auth: sub.auth };
const outcome = await sendPush(target, payload);
Comment thread
dezren39 marked this conversation as resolved.
Resolved
if (outcome.ok) {
sent += 1;
await store.logSend({ class: "digest", objectRef: digest.archiveRoute, outcome: "sent" });
await store.noteSubscriptionAlive(sub.endpoint);
} else if (outcome.gone) {
await store.logSend({ class: "digest", objectRef: digest.archiveRoute, outcome: "gone" });
await store.noteSubscriptionGone(sub.endpoint);
} else {
await store.logSend({ class: "digest", objectRef: digest.archiveRoute, outcome: "error" });
}
}
console.log(`generate-digest: delivered to ${sent}/${subs.length} subscriptions`);
if (process.argv.includes("--strict-deliver") && sent === 0 && subs.length > 0) process.exit(1);
}

await main();
3 changes: 3 additions & 0 deletions pm-stack/aggregator/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export interface Config {
registryFile: string;
/** Usage snapshot store (hub §4): a directory of monthly JSONL files. */
snapshotsDir: string;
/** Scheduled-digest archive (#1278): one JSON file per digest. */
digestsDir: string;
/** The stewardship quota view; the hub's live fallback source when the
* fleet feed carries no saas/payg sections yet (hub §2.3). */
quotaViewFile: string;
Expand Down Expand Up @@ -117,6 +119,7 @@ export function loadConfig(env: Record<string, string | undefined> = process.env
activityFile: s(env.PM_AGG_ACTIVITY_FILE, join(process.cwd(), "activity-ingestion.json")),
registryFile: s(env.PM_AGG_REGISTRY_FILE, join(process.cwd(), "registry.json")),
snapshotsDir: s(env.PM_AGG_SNAPSHOTS_DIR, join(process.cwd(), "snapshots")),
digestsDir: s(env.PM_AGG_DIGESTS_DIR, join(process.cwd(), "digests")),
quotaViewFile: s(
env.PM_AGG_QUOTA_VIEW,
join(
Expand Down
Loading
Loading