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
41 changes: 27 additions & 14 deletions pm-stack/aggregator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,11 @@ of silently showing canned data. `bun run dev` sets the switch for you.
| `GRIST_API_KEY` | — | Grist API key (Bitwarden `grist.api-key`) |
| `GRIST_ROADMAP_DOC` | `czYHYKgG9SHcm9CPwiKEq1` | The `Program roadmap` document id (Phase 2 seed) |
| `PM_PROJECTOR_URL` | `http://127.0.0.1:7801` | Projector + link registry base URL (`/items`, `/comments`, `/links`); `off` disables it |
| `PM_PROJECTOR_API_TOKEN` | — | Projector bearer token; the GitHub hot-list consumer uses it to acknowledge completed checkpoints (copy the same secret configured on the projector into the aggregator environment) |
| `PM_AGG_COMMENTS_FILE` | `./comments.json` | Dashboard comment store (#906) — aggregator-local; created on first comment |
| `PM_AGG_ACTIONS_FILE` | `./actions.json` | Dashboard action store (#1152/#1154/#1156) — aggregator-local; created on first action |
| `PM_AGG_REGISTRY_FILE` | `./registry.json` | Durable session identity registry (#906) — merged records persist across restarts |
| `PM_AGG_ACTIVITY_FILE` | `~/.local/state/pm-aggregator/activity-ingestion.json` | Activity-ingestion store (#3321) — per-source checkpoints + dedupe seen sets + gap records for the watchdog/Kaneo stream writers |
| `PM_AGG_ACTIVITY_FILE` | `~/.local/state/pm-aggregator/activity-ingestion.json` | Activity-ingestion store (#3321) — per-source checkpoints, dedupe seen sets, source status, and gap records for watchdog/Kaneo/GitHub writers |

Secrets are read from the environment only and are never committed or
printed. Names here reference Bitwarden entries (`kaneo.*`, `grist.*`).
Expand All @@ -609,19 +610,31 @@ are completion history and are not counted as WIP.

### Activity ingestion into the event stream (#3321)

Watchdog breach episodes and Kaneo board changes are ingested into the
#1739 event stream by `src/activity.ts`, with persisted per-source
checkpoints: replaying a duplicate delivery is a no-op, and a restart
resumes from the checkpoint instead of re-emitting. Known source
outages are recorded as gap records (source + time range) and rendered
on `/events` as holes in the history — never as complete coverage; a
backfill/recovery pass flips a gap's status. Per-source coverage
(supported / unsupported / historical ranges) is documented in
`ACTIVITY_SOURCE_COVERAGE` in `src/activity.ts`, rendered on `/events`,
and served at `/api/activity.json` together with the checkpoints and
gaps. GitHub activity is explicitly **unsupported** until the #2072
webhook/hot-list ingestion lands — #3321's boundary forbids a
competing receiver.
Watchdog breach episodes, Kaneo board changes, and GitHub events from
the #2072/#5204 projector hot list are ingested into the #1739 event
stream by `src/activity.ts`. The GitHub consumer in
`src/github-hot-list.ts` pins each sweep to the projector's high-water
and acknowledges the remote consumer checkpoint only after every row in
that sweep is durably ingested. A duplicate replay is a no-op, and an
aggregator restart resumes from the projector's persisted checkpoint
with the local seen-id store as a second idempotency guard. Set
`PM_PROJECTOR_API_TOKEN` in the aggregator environment to the same
bearer token configured for the projector; `install.sh` copies only that
one variable from the projector's state `.env` and does not expose the
GitHub App credentials to the aggregator. Without the token the GitHub
source is reported as `unconfigured` and is not represented as complete.

Known source outages and consumer lag are recorded as gap records
(source + time range) and rendered on `/events` as holes in the history;
recovery closes a gap as backfilled, while malformed/unrecoverable
checkpoint state is marked `unrecoverable`. `/api/activity.json` exposes
checkpoints, gaps, source status, and `completeHistory: false` for the
GitHub source: `current` means caught up to the hot-list high-water, not
complete GitHub history. Per-source coverage (supported / unsupported /
historical ranges) is documented in `ACTIVITY_SOURCE_COVERAGE` in
`src/activity.ts` and rendered on `/events`. GitHub coverage begins only
with rows retained in the hot list; upstream webhook deliveries the
projector never received and pre-hot-list history remain unsupported.

## Capability provenance matrix (#1200)

Expand Down
10 changes: 9 additions & 1 deletion pm-stack/aggregator/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if [ "$SRC" != "$EXPECTED_SRC" ]; then
fi
STATE_DIR="$HOME/.local/state/pm-aggregator"
SOPS="$HOME/secrets/pm-stack.sops.yaml"
PROJECTOR_ENV="$HOME/.local/state/pm-projector/.env"
# Resolve the unit against the checkout root via the logical parent of
# $SRC: "$SRC/../systemd" would be resolved by the kernel through the
# package dir itself, so a symlinked package dir (smoke fixture, legacy
Expand All @@ -52,7 +53,7 @@ DEPLOY_COMMIT="$(git -C "$SRC" rev-parse HEAD 2>/dev/null || true)"
tree_hash() {
local root="$1"
(cd "$root" && {
for f in package.json bun.lock tsconfig.json; do
for f in package.json bun.lock tsconfig.json install.sh; do
[ -f "$f" ] && echo "$f"
done
find src test scripts opencode-tools systemd -type f 2>/dev/null
Expand All @@ -73,6 +74,7 @@ deploy_inputs_hash() {
printf 'TAILNET_IP=%s\n' "$TAILNET_IP"
printf 'STATE_DIR=%s\n' "$STATE_DIR"
if [ -f "$SOPS" ]; then sha256sum "$SOPS"; else echo "sops:absent"; fi
if [ -f "$PROJECTOR_ENV" ]; then sha256sum "$PROJECTOR_ENV"; else echo "projector-env:absent"; fi
if [ -f "$UNIT_SRC" ]; then sha256sum "$UNIT_SRC"; else echo "unit:absent"; fi
} | sha256sum | cut -d' ' -f1
}
Expand Down Expand Up @@ -176,6 +178,12 @@ if [ "$CHANGED" -eq 1 ]; then
echo "PM_AGG_COVERAGE_SNAPSHOT_FILE=${STATE_DIR}/coverage-snapshot.json"
echo "PM_AGG_ROADMAP_ANNOTATIONS_FILE=${STATE_DIR}/roadmap-annotations.json"
echo "PM_PROJECTOR_URL=http://127.0.0.1:7801"
# The hot-list consumer shares only the projector's checkpoint-ack
# credential, not its webhook secret or GitHub App private key.
if [ -f "$PROJECTOR_ENV" ]; then
projector_token_line="$(grep -m 1 '^PM_PROJECTOR_API_TOKEN=' "$PROJECTOR_ENV" || true)"
if [ -n "$projector_token_line" ]; then printf '%s\n' "$projector_token_line"; fi
fi
for key in kaneo_admin_email:KANEO_ADMIN_EMAIL kaneo_admin_password:KANEO_ADMIN_PASSWORD grist_api_key:GRIST_API_KEY; do
sops_key="${key%%:*}"
env_name="${key##*:}"
Expand Down
175 changes: 161 additions & 14 deletions pm-stack/aggregator/src/activity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Activity ingestion for the cross-stack stream (#3321): the
// watchdog-event and Kaneo-change writers that map external activity
// watchdog-event, Kaneo-change, and GitHub-hot-list writers that map external activity
// into the #1739 StreamEvent log, with the ingestion discipline the
// local fire-and-forget writers never needed:
//
Expand Down Expand Up @@ -30,11 +30,9 @@
// recovery/backfill pass flips the gap's status; the /events page
// renders open gaps as holes, never as coverage.
//
// The GitHub slice is deliberately NOT here: #3321's boundary says
// GitHub activity reuses the #2072 webhook/hot-list ingestion rather
// than a competing receiver, and #2072 has not landed. The coverage
// table below states that plainly (github: unsupported) instead of
// implying coverage that does not exist.
// GitHub activity is consumed from #2072's append-only hot list; this
// module does not create a competing webhook receiver. Coverage below
// describes exactly what that bounded source can and cannot represent.
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import type { EventsStore } from "./events.ts";
Expand All @@ -50,7 +48,7 @@ export interface ActivityDelivery {
sourceId: string;
/** ISO timestamp of the change at the source. */
at: string;
/** StreamEvent kind ("watchdog-event" | "kaneo-change"). */
/** StreamEvent kind ("watchdog-event" | "kaneo-change" | "github-event"). */
kind: string;
/** StreamEvent ref (the item the change concerns). */
ref: string;
Expand Down Expand Up @@ -96,14 +94,36 @@ export const ACTIVITY_SOURCE_COVERAGE: readonly ActivityCoverage[] = [
},
{
source: "github",
status: "unsupported",
supported: "Nothing yet.",
status: "supported",
supported:
"Normalized issue, pull-request, comment/review, and push-commit rows present in the #2072/#5204 projector hot list are consumed into github-event records with the hot-list event id and source URL. The consumer checkpoint advances only after a complete fixed-high-water sweep is durably ingested.",
unsupported:
"All GitHub activity. #3321's boundary forbids a competing receiver: GitHub events enter through the #2072 webhook/hot-list ingestion, which has not landed. When it does, its writer joins this module with the same checkpoint/dedupe discipline.",
historical: "None claimed. GitHub's own history stays in GitHub until #2072 lands.",
"GitHub activity not represented by the hot-list webhook mapping, raw webhook bodies, and webhook deliveries the projector never received. This consumer cannot independently detect or reconstruct missing upstream webhook deliveries; a receiver outage or hot-list API outage is surfaced as a source gap instead of complete history.",
historical:
"Only rows retained in the #5204 hot list, from that ledger's start. A new consumer begins at the persisted checkpoint (zero on first use) and can consume rows already in the ledger; no pre-ledger GitHub history is claimed.",
},
];

export type ActivitySourceState = "unconfigured" | "current" | "lagging" | "outage" | "unrecoverable";

/** Durable operational status for a source consumer. Cursor distance is
* intentionally not named an event count: hot-list sequence numbers are
* monotonic but not necessarily dense. */
export interface ActivitySourceStatus {
source: string;
state: ActivitySourceState;
consumer: string | null;
/** Last source seq processed locally, including an in-progress fixed sweep. */
cursor: number | null;
/** Last cursor the projector has durably acknowledged. */
checkpointCursor: number | null;
highWater: number | null;
lastAttemptAt: string | null;
lastSuccessAt: string | null;
updatedAt: string;
error?: string;
}

// Cap `t` at `max` UTF-16 code units, but never leave half of a surrogate
// pair at the cut (#7725, mirroring the #7694/#7711 capUnits guard in
// actions.ts): an astral character straddling the cap is dropped whole, so
Expand Down Expand Up @@ -135,6 +155,80 @@ export function watchdogDeliveries(items: readonly TierItem[]): ActivityDelivery
}));
}

export interface GithubHotEvent {
seq: number;
event_id: string;
delivery_id: string | null;
repo: string;
event: string;
action: string | null;
ref: string | null;
actor: string | null;
source_url: string | null;
source_timestamp: string | null;
received_at: string;
}

function validGithubTimestamp(value: string | null): string | null {
if (!value || !Number.isFinite(Date.parse(value))) return null;
return new Date(value).toISOString();
}

function githubSourceLink(event: GithubHotEvent): string {
if (event.source_url) {
try {
const url = new URL(event.source_url);
if (
url.protocol === "https:" &&
!url.username &&
!url.password &&
(url.hostname === "github.com" || url.hostname === "www.github.com")
) {
return url.toString();
}
} catch {
// Fall through to the canonical repository link.
}
}
const repo = event.repo.trim();
if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo)) throw new Error("invalid_hot_event_repo");
return `https://github.com/${repo}`;
}

/** Map one normalized #5204 hot-list row into the #1739 activity contract.
* `event_id` is the source identity, so EventsStore persists the stable
* `activity:github:<event_id>` id and retries remain dedupe-safe. */
export function githubHotEventDelivery(event: GithubHotEvent): ActivityDelivery {
const sourceId = event.event_id.trim();
const repo = event.repo.trim();
const kind = event.event.trim();
if (!sourceId) throw new Error("invalid_hot_event_id");
if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo)) throw new Error("invalid_hot_event_repo");
if (!kind) throw new Error("invalid_hot_event_kind");
const at = validGithubTimestamp(event.source_timestamp) ?? validGithubTimestamp(event.received_at);
if (!at) throw new Error("invalid_hot_event_timestamp");
const sourceRef = event.ref?.trim() ?? "";
// EventsStore rejects refs longer than 300 characters or containing
// whitespace. Keep the original source ref visible in the bounded
// summary, but use the repository as a valid contract ref so one odd
// upstream branch name cannot permanently stall this hot-list page.
const ref = sourceRef && sourceRef.length <= 300 && !/\s/.test(sourceRef) ? sourceRef : repo;
const action = event.action?.trim();
const actor = event.actor?.trim();
const description = [event.event, action].filter(Boolean).join(" ");
const subject = sourceRef ? ` ref ${capUnits(sourceRef, 300)}` : "";
const by = actor ? ` by ${actor}` : "";
return {
source: "github",
sourceId,
at,
kind: "github-event",
ref,
summary: capUnits(`GitHub ${description} in ${repo}${subject}${by}`, 500),
link: githubSourceLink(event),
};
}

/** Pins each watchdog breach episode to the `since` it was FIRST
* observed with, until the episode clears (Codex P2, PR #5141).
* watchdogTierItems is pure and, for absent/future-dated feeds and
Expand Down Expand Up @@ -288,6 +382,7 @@ export interface IngestionFile {
checkpoints: Record<string, SourceCheckpoint>;
seen: Record<string, string[]>;
gaps: GapRecord[];
sourceStatus?: Record<string, ActivitySourceStatus>;
}

/** Per-source seen-id bound: dedupe memory is finite by design; an id
Expand Down Expand Up @@ -325,8 +420,30 @@ function isCheckpoint(v: unknown): v is SourceCheckpoint {
);
}

function isActivitySourceStatus(v: unknown): v is ActivitySourceStatus {
if (typeof v !== "object" || v === null) return false;
const s = v as Record<string, unknown>;
const cursor = (n: unknown): boolean => n === null || (Number.isSafeInteger(n) && Number(n) >= 0);
return (
typeof s.source === "string" &&
(s.state === "unconfigured" ||
s.state === "current" ||
s.state === "lagging" ||
s.state === "outage" ||
s.state === "unrecoverable") &&
(s.consumer === null || typeof s.consumer === "string") &&
cursor(s.cursor) &&
cursor(s.checkpointCursor) &&
cursor(s.highWater) &&
(s.lastAttemptAt === null || typeof s.lastAttemptAt === "string") &&
(s.lastSuccessAt === null || typeof s.lastSuccessAt === "string") &&
typeof s.updatedAt === "string" &&
(s.error === undefined || typeof s.error === "string")
);
}

export class IngestionStore {
private data: IngestionFile = { version: 1, checkpoints: {}, seen: {}, gaps: [] };
private data: IngestionFile = { version: 1, checkpoints: {}, seen: {}, gaps: [], sourceStatus: {} };
private loadPromise: Promise<void> | null = null;
private ingestChain: Promise<void> = Promise.resolve();
private writeChain: Promise<void> = Promise.resolve();
Expand Down Expand Up @@ -359,6 +476,12 @@ export class IngestionStore {
throw new Error("seen is not an object");
}
if (!Array.isArray(f.gaps)) throw new Error("gaps is not an array");
if (
f.sourceStatus !== undefined &&
(typeof f.sourceStatus !== "object" || f.sourceStatus === null || Array.isArray(f.sourceStatus))
) {
throw new Error("sourceStatus is not an object");
}
for (const cp of Object.values(f.checkpoints)) {
if (!isCheckpoint(cp)) throw new Error("a checkpoint row is malformed");
}
Expand All @@ -370,11 +493,15 @@ export class IngestionStore {
for (const gap of f.gaps) {
if (!isGapRecord(gap)) throw new Error("a gap row is malformed");
}
for (const status of Object.values(f.sourceStatus ?? {})) {
if (!isActivitySourceStatus(status)) throw new Error("a source status row is malformed");
}
this.data = {
version: 1,
checkpoints: f.checkpoints as Record<string, SourceCheckpoint>,
seen: f.seen as Record<string, string[]>,
gaps: f.gaps as GapRecord[],
sourceStatus: (f.sourceStatus ?? {}) as Record<string, ActivitySourceStatus>,
};
} catch (err) {
await this.quarantine(`is unparseable (${err instanceof Error ? err.message : String(err)})`);
Expand Down Expand Up @@ -432,6 +559,18 @@ export class IngestionStore {
return [...this.data.gaps].sort((a, b) => Date.parse(a.from) - Date.parse(b.from));
}

async sourceStatuses(): Promise<ActivitySourceStatus[]> {
await this.load();
return Object.values(this.data.sourceStatus ?? {}).sort((a, b) => a.source.localeCompare(b.source));
}

async setSourceStatus(status: ActivitySourceStatus): Promise<void> {
await this.load();
if (!this.data.sourceStatus) this.data.sourceStatus = {};
this.data.sourceStatus[status.source] = status;
await this.persist();
}

/**
* Ingest deliveries into the stream with dedupe + checkpointing.
* A delivery whose sourceId is already in the source's seen set is
Expand Down Expand Up @@ -535,8 +674,13 @@ export class IngestionStore {
await this.persist();
return open;
}
const baseId = `gap:${source}:${from}`;
let id = baseId;
for (let suffix = 1; this.data.gaps.some((gap) => gap.id === id); suffix++) {
id = `${baseId}:${suffix}`;
}
const gap: GapRecord = {
id: `gap:${source}:${from}`,
id,
source,
from,
to,
Expand All @@ -558,7 +702,10 @@ export class IngestionStore {
* failed poll. */
async resolveGap(id: string, status: Exclude<GapStatus, "open">, to?: string): Promise<GapRecord | null> {
await this.load();
const gap = this.data.gaps.find((g) => g.id === id);
// Prefer an open row if older persisted data contains duplicate IDs
// from a previously resolved episode and a later reopened gap.
const gap =
this.data.gaps.find((g) => g.id === id && g.status === "open") ?? this.data.gaps.find((g) => g.id === id);
if (!gap || gap.status !== "open") return gap ?? null;
if (to && Date.parse(to) > Date.parse(gap.to)) gap.to = to;
gap.status = status;
Expand Down
Loading