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
302 changes: 295 additions & 7 deletions pm-stack/aggregator/src/alerts.ts

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions pm-stack/aggregator/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export interface Config {
snapshotsDir: string;
/** Scheduled-digest archive (#1278): one JSON file per digest. */
digestsDir: string;
/** Append-only notification state/send/suppression log (#3317). */
notificationsFile: 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 @@ -161,6 +163,10 @@ export function loadConfig(env: Record<string, string | undefined> = process.env
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")),
notificationsFile: s(
env.PM_AGG_NOTIFICATIONS_FILE,
join(homedir(), ".local", "state", "pm-aggregator", "notifications.jsonl"),
),
quotaViewFile: s(
env.PM_AGG_QUOTA_VIEW,
join(
Expand Down
45 changes: 42 additions & 3 deletions pm-stack/aggregator/src/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,18 @@ export interface SectionMatch {
*/
export function tierEntry(e: AttentionEntry, match?: SectionMatch | null): TieredEntry {
if (match && e.state !== "in-motion") {
if (match.tier === "needs-drew") return { ...e, tier: "needs-drew", drewReason: match.item.reason };
if (match.tier === "needs-drew")
return { ...e, tier: "needs-drew", tierSince: match.item.since, drewReason: match.item.reason };
const next = match.item.recommendedAction ? ` Next: ${match.item.recommendedAction}` : "";
return { ...e, tier: "needs-orchestrator", orchestratorNote: `${match.item.reason}${next}` };
}
if (e.state !== "in-motion" && e.supervisionNote && ESCALATION_NOTE.test(e.supervisionNote)) {
return { ...e, tier: "needs-drew", drewReason: `Keeper escalated this session — ${e.supervisionNote}` };
return {
...e,
tier: "needs-drew",
tierSince: e.lastActivityAt ?? undefined,
drewReason: `Keeper escalated this session — ${e.supervisionNote}`,
};
}
if (e.state !== "in-motion" && e.awaitingQuestion) {
const q = e.awaitingQuestion;
Expand All @@ -347,6 +353,7 @@ export function tierEntry(e: AttentionEntry, match?: SectionMatch | null): Tiere
return {
...e,
tier: "needs-drew",
tierSince: e.lastActivityAt ?? undefined,
drewReason: `A session is asking permission for something destructive — ${q}`,
};
}
Expand All @@ -355,13 +362,19 @@ export function tierEntry(e: AttentionEntry, match?: SectionMatch | null): Tiere
// Jules questions are the keeper's to answer (it replies with the
// standard proceed/feedback messages) — they never need Drew
// unless the orchestration job escalates them explicitly.
return { ...e, tier: "needs-drew", drewReason: `Waiting on your decision — ${q}` };
return {
...e,
tier: "needs-drew",
tierSince: e.lastActivityAt ?? undefined,
drewReason: `Waiting on your decision — ${q}`,
};
}
}
if (e.state !== "in-motion" && e.lastTurnError && CREDENTIAL_ERR.test(e.lastTurnError)) {
return {
...e,
tier: "needs-drew",
tierSince: e.lastActivityAt ?? undefined,
drewReason: `Blocked on a sign-in only you can complete — ${e.lastTurnError}`,
};
}
Expand Down Expand Up @@ -403,6 +416,32 @@ export interface AttentionView {
tierSource: TierSourceInfo;
}

/** Digest landing filter (#7193 Codex): the alert digest links to
* /attention?refs=<objectRefs>; keep only the entries those refs name.
* Ref forms mirror NotificationCandidate.objectRef: session:<id>,
* item:<id>, a bare session/item id, a GitHub ref (owner/repo#n), or
* gh:<ref>. Quota/health refs name no attention entries (their own
* pages carry them), so they simply match nothing here. */
export function filterAttentionViewByRefs(view: AttentionView, refs: string[]): AttentionView {
const wanted = new Set(refs.map((ref) => ref.trim()).filter(Boolean));
if (wanted.size === 0) return view;
const matchEntry = (e: TieredEntry) => wanted.has(`session:${e.sessionId}`) || wanted.has(e.sessionId);
const matchItem = (item: TierItem) =>
wanted.has(`item:${item.id}`) ||
wanted.has(item.id) ||
(item.sessionId ? wanted.has(`session:${item.sessionId}`) || wanted.has(item.sessionId) : false) ||
(item.ref ? wanted.has(item.ref) || wanted.has(`gh:${item.ref}`) : false);
return {
...view,
drew: view.drew.filter(matchEntry),
orchestrator: view.orchestrator.filter(matchEntry),
drewExtra: view.drewExtra.filter(matchItem),
orchestratorExtra: view.orchestratorExtra.filter(matchItem),
watchdog: view.watchdog.filter(matchItem),
all: view.all.filter(matchEntry),
};
}

/** Classify, expire, and tier a raw session feed in one pass. */
export function buildAttention(
feed: SessionFeedEntry[],
Expand Down
2 changes: 2 additions & 0 deletions pm-stack/aggregator/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ export type AttentionTier = "needs-drew" | "needs-orchestrator" | "in-motion";

export interface TieredEntry extends AttentionEntry {
tier: AttentionTier;
/** When the orchestration job first observed this session in its tier. */
tierSince?: string;
/** Why this needs Drew — present iff tier is "needs-drew". */
drewReason?: string;
/** What the fleet will do with it — present iff tier is "needs-orchestrator". */
Expand Down
34 changes: 34 additions & 0 deletions pm-stack/aggregator/src/notification-history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { appendFile, mkdir, readFile } from "node:fs/promises";
import { dirname } from "node:path";
import type { NotificationHistoryEntry } from "./notifications.ts";

/** Durable append-only history used by the notification transition policy. */
export interface NotificationHistoryStore {
read(): Promise<NotificationHistoryEntry[]>;
append(entry: NotificationHistoryEntry): Promise<void>;
}

export class JsonlNotificationHistory implements NotificationHistoryStore {
private writes: Promise<void> = Promise.resolve();

constructor(private readonly file: string) {}

async read(): Promise<NotificationHistoryEntry[]> {
const raw = await readFile(this.file, "utf8").catch((error: NodeJS.ErrnoException) => {
if (error.code === "ENOENT") return "";
throw error;
});
return raw
.split("\n")
.filter(Boolean)
.map((line) => JSON.parse(line) as NotificationHistoryEntry);
}

append(entry: NotificationHistoryEntry): Promise<void> {
this.writes = this.writes.then(async () => {
await mkdir(dirname(this.file), { recursive: true });
await appendFile(this.file, `${JSON.stringify(entry)}\n`, "utf8");
});
return this.writes;
}
}
Loading
Loading