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
31 changes: 18 additions & 13 deletions pm-stack/aggregator/report-records-c7.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,29 @@ selected.

## Period composition and generation

`src/report-composer.ts` composes an explicit target period from current
canonical lower-grain records. Daily reports use source records; weekly reports
use daily records and separately label the prior Friday as reach-back context;
monthly, quarterly, and annual reports prefer composed records at the next
smaller grain, falling back to source records for a child period when no
composed version exists. Stable source-time and record-ID ordering makes the
result independent of record arrival order. Blocks bubble upward according to
their `importance` grains (or the default path from their source grain), and
classified activity carries linked members into the summary. The template
sections and composer identity are versioned as `c7-rollup-v1`.
`src/report-composer.ts` composes an explicit target period from the latest
retained lower-grain records available at `asOf`, and excludes blocks after
that instant or outside the target period. Daily reports use source records;
weekly reports use daily records and separately label only the prior Friday as
reach-back context; monthly, quarterly, and annual reports prefer composed
records at the next smaller grain, falling back to source records for a child
period when no composed version exists. Stable source-time and record-ID
ordering makes the result independent of record arrival order. Blocks bubble
upward according to their `importance` grains (or the default path from their
source grain). Non-visible `bubblingCandidates` preserve explicitly selected
higher grains across an excluded intermediate grain. Classified activity
carries only explicit member links; source attribution remains in `sources`.
The template sections and composer identity are versioned as `c7-rollup-v1`.

Generate a record with `bun run report:generate --kind <daily|weekly|monthly|quarterly|annual>
--period <identity> --as-of <canonical-ISO-timestamp> [--store <jsonl-path>]`.
Period identities are explicit: `YYYY-MM-DD` (daily), Monday `YYYY-MM-DD`
(weekly), `YYYY-MM` (monthly), `YYYY-QN` (quarterly), and `YYYY` (annual).
The command validates its complete argument list and period identity before
store I/O, reads the selected retained records, and appends the result
idempotently. It follows `PM_AGG_STORE_BACKEND`: file mode uses the local
The command validates its complete argument list, period identity, and
half-open `asOf` bounds before store I/O, selects retained records available at
that point in time, and appends the result idempotently. A changed `asOf` is
part of the derived source cursor so distinct point-in-time views cannot
conflict under one immutable record ID. It follows `PM_AGG_STORE_BACKEND`: file mode uses the local
report-record JSONL file (`PM_AGG_REPORT_RECORDS_FILE` or the default path),
and `--store` selects another JSONL path in file mode. Turso mode uses the
existing durable document adapter and never silently falls back to a file;
Expand Down
5 changes: 4 additions & 1 deletion pm-stack/aggregator/scripts/generate-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export function parseArgs(args: string[]): Command {
throw new Error("--as-of must be a canonical ISO-8601 timestamp");
}
// Resolve now so bad/implicit period identities fail before any store I/O.
reportPeriod(kind, identity);
const period = reportPeriod(kind, identity);
if (Date.parse(asOf) < Date.parse(period.periodStart) || Date.parse(asOf) >= Date.parse(period.periodEnd)) {
throw new Error("--as-of must fall within the report period");
}
const store = values.get("--store")?.trim();
if (values.has("--store") && !store) throw new Error("--store must not be empty");
return { kind, identity, asOf, ...(store ? { store } : {}) };
Expand Down
118 changes: 89 additions & 29 deletions pm-stack/aggregator/src/report-composer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Deterministic C7 period rollups over canonical, persisted lower-grain records.
import { dailyReportPeriod, type ReportPeriod, type ReportPeriodKind } from "./report-periods.ts";
import type { ReportRecord, ReportRecordBlock, ReportRecordSource, ReportRecordLocation } from "./report-records.ts";
import { appendReportRecord, makeReportRecord, readCurrentReportRecords } from "./report-records.ts";
import { appendReportRecord, makeReportRecord, readReportRecords } from "./report-records.ts";

export const REPORT_TEMPLATE_VERSION = "c7-rollup-v1";

Expand Down Expand Up @@ -42,6 +42,7 @@ export interface ComposedReport {
sourceCursor: string;
sources: ReportRecordSource[];
blocks: ReportRecordBlock[];
bubblingCandidates: ReportRecordBlock[];
classifiedActivity: ClassifiedActivity;
}

Expand Down Expand Up @@ -74,11 +75,12 @@ function lowerGrainRecords(
period: ReportPeriod,
records: ReportRecord[],
): { record: ReportRecord; reachBack: boolean }[] {
const daily = preferComposedChildren(records, "daily");
const daily =
period.kind === "daily"
? records.filter((record) => record.period.kind === "daily" && record.templateVersion !== REPORT_TEMPLATE_VERSION)
: preferComposedChildren(records, "daily");
if (period.kind === "daily")
return daily
.filter((record) => record.templateVersion !== REPORT_TEMPLATE_VERSION && inPeriod(record, period))
.map((record) => ({ record, reachBack: false }));
return daily.filter((record) => inPeriod(record, period)).map((record) => ({ record, reachBack: false }));

const current = daily.filter((record) => inPeriod(record, period)).map((record) => ({ record, reachBack: false }));
// Weekly reports carry the prior week's Friday as explicit context. It is
Expand All @@ -89,13 +91,9 @@ function lowerGrainRecords(
const mondayDate = new Date(`${monday}T00:00:00.000Z`);
mondayDate.setUTCDate(mondayDate.getUTCDate() - 3);
const friday = mondayDate.toISOString().slice(0, 10);
const fridayStart = Date.parse(dailyReportPeriod(friday).periodStart);
const mondayStart = Date.parse(period.periodStart);
const fridayPeriod = dailyReportPeriod(friday);
const reachBack = daily
.filter(
(record) =>
Date.parse(record.period.periodStart) >= fridayStart && Date.parse(record.period.periodStart) < mondayStart,
)
.filter((record) => record.period.id === fridayPeriod.id)
.map((record) => ({ record, reachBack: true }));
return [...current, ...reachBack];
}
Expand Down Expand Up @@ -135,20 +133,49 @@ export function composeReport(
throw new Error("asOf must fall within the report period");
}

const rows = stableSourceRows(period, records);
const orderedBlocks = rows
.flatMap(({ record, reachBack }) =>
record.blocks
.filter((block) => !block.importance || block.importance.includes(period.kind))
.map((block) => ({
...structuredClone(block),
id: `${record.id}:${block.id}`,
const rows = stableSourceRows(
period,
records.filter((record) => Date.parse(record.asOf) <= Date.parse(options.asOf)),
);
if (!rows.length) throw new Error("cannot compose a report without canonical source records");
const orderedBlocks: ReportRecordBlock[] = [];
const bubblingCandidates: ReportRecordBlock[] = [];
const orderedByIdentity = new Map<string, { block: ReportRecordBlock; reachBack: boolean }>();
const grainOrder: ReportPeriodKind[] = ["daily", "weekly", "monthly", "quarterly", "annual"];
const targetGrain = grainOrder.indexOf(period.kind);
for (const { record, reachBack } of rows) {
const composedInput = record.templateVersion === REPORT_TEMPLATE_VERSION;
const sourceBlocks = [
...new Map([...record.blocks, ...(record.bubblingCandidates ?? [])].map((block) => [block.id, block])).values(),
];
for (const block of sourceBlocks) {
// Templates and summaries describe their source report, not source
// activity. Rebuild them at the target grain instead of rolling them up.
if (composedInput && (block.id.startsWith("template:") || block.id.startsWith("activity:"))) continue;
const relatedAt = Date.parse(block.relatedAt);
const inRequestedPeriod = relatedAt >= Date.parse(period.periodStart) && relatedAt < Date.parse(period.periodEnd);
if (relatedAt > Date.parse(options.asOf) || (!inRequestedPeriod && !reachBack)) continue;
const importance = block.importance ?? defaultImportance(record.period.kind);
const normalized = { ...structuredClone(block), importance };
const id = composedInput ? block.id : `${record.id}:${block.id}`;
if (importance.includes(period.kind)) {
const ordered = {
...normalized,
id,
text: reachBack ? `[Prior-Friday reach-back] ${block.text}` : block.text,
links: [...new Set([...(block.links ?? []), ...record.sources.map((source) => source.uri)])].sort(),
importance: block.importance ?? defaultImportance(record.period.kind),
})),
)
.sort((left, right) => left.relatedAt.localeCompare(right.relatedAt) || left.id.localeCompare(right.id));
links: block.links ? [...new Set(block.links)].sort() : undefined,
};
const previous = orderedByIdentity.get(id);
if (!previous || (previous.reachBack && !reachBack)) orderedByIdentity.set(id, { block: ordered, reachBack });
}
if (!reachBack && importance.some((kind) => grainOrder.indexOf(kind) > targetGrain)) {
bubblingCandidates.push({ ...normalized, id });
}
}
}
orderedBlocks.push(...[...orderedByIdentity.values()].map(({ block }) => block));
const uniqueCandidates = [...new Map(bubblingCandidates.map((block) => [block.id, block])).values()];
orderedBlocks.sort((left, right) => left.relatedAt.localeCompare(right.relatedAt) || left.id.localeCompare(right.id));

const activity: ClassifiedActivity = {
implemented: [],
Expand All @@ -158,9 +185,22 @@ export function composeReport(
blocked: [],
queued: [],
};
const latestClassification = new Map<string, { relatedAt: string; id: string; key: keyof ClassifiedActivity }>();
for (const block of orderedBlocks) {
if (block.classification) activity[classificationKeys[block.classification]].push(...(block.links ?? []));
if (!block.classification) continue;
const key = classificationKeys[block.classification];
for (const member of block.links ?? []) {
const previous = latestClassification.get(member);
if (
!previous ||
block.relatedAt.localeCompare(previous.relatedAt) > 0 ||
(block.relatedAt === previous.relatedAt && block.id.localeCompare(previous.id) > 0)
) {
latestClassification.set(member, { relatedAt: block.relatedAt, id: block.id, key });
}
}
}
for (const [member, latest] of latestClassification) activity[latest.key].push(member);
for (const key of Object.keys(activity) as (keyof ClassifiedActivity)[])
activity[key] = [...new Set(activity[key])].sort();

Expand All @@ -174,14 +214,19 @@ export function composeReport(
importance: defaultImportance(period.kind),
}));
const sources = [
...new Map(rows.flatMap(({ record }) => record.sources).map((source) => [source.id, source])).values(),
...new Map(
rows
.flatMap(({ record }) => record.sources)
.filter((source) => Date.parse(source.observedAt) <= Date.parse(options.asOf))
.map((source) => [source.id, source]),
).values(),
].sort((left, right) => left.observedAt.localeCompare(right.observedAt) || left.id.localeCompare(right.id));
const sourceCursor =
options.sourceCursor ??
rows
`${rows
.map(({ record }) => record.id)
.sort()
.join(",");
.join(",")};asOf:${options.asOf}`;
if (!sourceCursor.trim()) throw new Error("cannot compose a report without canonical source records");
const templateBlocks: ReportRecordBlock[] = REPORT_TEMPLATES[period.kind].map((title, index) => ({
id: `template:section:${String(index + 1).padStart(2, "0")}`,
Expand All @@ -197,6 +242,7 @@ export function composeReport(
sourceCursor,
sources,
blocks: [...templateBlocks, ...summaryBlocks, ...orderedBlocks],
bubblingCandidates: uniqueCandidates,
classifiedActivity: activity,
};
}
Expand All @@ -207,7 +253,21 @@ export async function generateReportFromStoredRecords(
period: ReportPeriod,
options: ComposeReportOptions,
): Promise<{ record: ReportRecord; inserted: boolean }> {
const stored = await readCurrentReportRecords(location);
const allStored = await readReportRecords(location);
const latestAsOf = new Map<string, ReportRecord>();
for (const record of allStored) {
if (Date.parse(record.asOf) > Date.parse(options.asOf)) continue;
const key = `${record.period.id}\n${record.templateVersion}`;
const previous = latestAsOf.get(key);
if (
!previous ||
record.asOf > previous.asOf ||
(record.asOf === previous.asOf && record.generatedAt > previous.generatedAt)
) {
latestAsOf.set(key, record);
}
}
const stored = [...latestAsOf.values()];
const composed = composeReport(period, stored, options);
const record = makeReportRecord(composed, options.generatedAt);
const result = await appendReportRecord(location, record);
Expand Down
20 changes: 20 additions & 0 deletions pm-stack/aggregator/src/report-records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface ReportRecordInput {
sourceCursor: string;
sources: ReportRecordSource[];
blocks: ReportRecordBlock[];
/** Non-visible blocks retained for explicitly requested higher-grain rollups. */
bubblingCandidates?: ReportRecordBlock[];
}

export interface ReportRecord extends ReportRecordInput {
Expand Down Expand Up @@ -162,6 +164,24 @@ export function makeReportRecord(input: ReportRecordInput, generatedAt = new Dat
throw new Error(`report block ${block.id} has invalid links`);
}
}
for (const block of input.bubblingCandidates ?? []) {
if (!block.id.trim() || !block.text.trim()) throw new Error("each bubbling candidate requires an id and text");
requireIsoDate(block.relatedAt, "bubbling candidate relatedAt");
if (
block.importance &&
(!Array.isArray(block.importance) ||
block.importance.some((kind) => !(REPORT_PERIOD_KINDS as readonly string[]).includes(kind)) ||
new Set(block.importance).size !== block.importance.length)
) {
throw new Error(`bubbling candidate ${block.id} has invalid importance levels`);
}
if (
block.links &&
(!Array.isArray(block.links) || block.links.some((link) => typeof link !== "string" || !link.trim()))
) {
throw new Error(`bubbling candidate ${block.id} has invalid links`);
}
}
return { ...structuredClone(input), id: reportRecordId(input), generatedAt };
}

Expand Down
Loading