Skip to content

feat(pm-stack): persist canonical C7 reports - #12364

Merged
developing-today-fleet[bot] merged 7 commits into
mainfrom
task/21000-c7-canonical-records
Nov 22, 2026
Merged

developing-today-fleet[bot] merged 7 commits into
mainfrom
task/21000-c7-canonical-records

Conversation

@dezren39

@dezren39 dezren39 commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Implement C7 canonical report records and durable append through the existing public.agg_store_documents KV table. This closes the acceptance in #12349 without adding a parallel report database or dual-writing stores.

  • Stable record IDs preserve source cursor, attribution, observed timestamps, template version, and period; the identity tuple is encoded without delimiter ambiguity.
  • Generated records, snapshots, and linked corrections append under namespaced immutable keys. A shared atomic content claim prevents concurrent event types from storing conflicting data under one record ID.
  • Identical retries return the original event; changed content with a reused ID conflicts.
  • Chicago daily period boundaries are DST-aware; report updates enforce the next-day 08:00 soft close and its append-only allowlist.
  • File JSONL remains available for local development and rollback; the remote adapter uses the existing PM-stack document store.
  • Storage/API contract and table map are documented.

Verification

  • bun run typecheck — passed.
  • bun test test/report-records-c7.test.ts test/report-periods-c7.test.ts test/turso-stores.test.ts — 22 passed, 0 failed, 2,154 assertions.
  • bun run contract:check and bun run provenance:check — passed.
  • git diff --check — passed.
  • Full aggregator test suite was attempted. Unrelated quota-contract/health tests could not bind port 4871 because the laptop's existing PM-stack listener already owns it; I left that service running.

Merge Danger

Door: two-way. Blast radius: additive report records and new namespaced rows in the existing document table. No existing documents are rewritten by C7 append calls.

Closes #12349.

@dezren39
dezren39 requested a review from a team as a code owner November 22, 2026 03:28
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 16 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f0094bd7-e79f-4810-9fdb-704f3f1ada6e
Commits

Reviewing files that changed from the base of the PR and between ea41220 and a7f33fb.

Files selected for processing: 10

  • pm-stack/SHARED-SCHEMA-MAP.md
  • pm-stack/aggregator/report-records-c7.md
  • pm-stack/aggregator/src/aa-last-good.ts
  • pm-stack/aggregator/src/remote-stores.ts
  • pm-stack/aggregator/src/report-periods.ts
  • pm-stack/aggregator/src/report-records.ts
  • pm-stack/aggregator/test/report-periods-c7.test.ts
  • pm-stack/aggregator/test/report-records-c7.test.ts
  • pm-stack/aggregator/test/turso-stores.test.ts
  • pm-stack/shared-schema/README.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T08:41:36.934102Z a7f33fb New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e15388c74

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +18 to +19
test("uses calendar boundaries across daylight-saving transitions", () => {
const spring = dailyReportPeriod("2026-03-08");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add generated coverage for the calendar-boundary invariants

The new suite covers only hand-picked dates, but this calendar conversion has meaningful invariants that the repository's testing doctrine requires property-based tests to cover; example-based acceptance tests do not replace that coverage. Add generated dates over a documented supported range and verify that boundaries round-trip to Chicago midnight, adjacent periods meet exactly, and soft close round-trips to next-day 08:00, using a named invariant or model oracle. This also covers periods immediately before DST transitions, where the offset changes between midnight and soft close.

AGENTS.md reference: AGENTS.md:L36-L39

Useful? React with 👍 / 👎.

@dezren39

Copy link
Copy Markdown
Member Author

Progress update for task 21000 / #12349:

  • Added the canonical report-record JSONL seam, stable IDs, append idempotency/conflicts, cursor projection, immutable snapshots, and soft-close checks.
  • Tightened both record and snapshot append boundaries to revalidate canonical IDs/period/content at runtime; added a regression for forged IDs.
  • Verified: bun test test/report-records-c7.test.ts test/report-periods-c7.test.ts (9 tests, 38 assertions), git diff --check.
  • bun run typecheck remains unavailable in this worktree: TS2688, missing Bun type definitions.

This is a reviewable partial slice, not full C7 completion: the repository does not expose the designed fleet KV append families or a selected fleet-wide permanent-store adapter, and weekly/other grains plus generation commands remain future work. Those gaps are documented in pm-stack/aggregator/report-records-c7.md; please do not interpret this PR update as completing #12349.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a4c91e31d4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

record,
ts: record.generatedAt,
};
await appendFile(file, `${JSON.stringify(event)}\n`, "utf8");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Separate new events from a torn trailing log entry

If an interrupted write leaves an incomplete final JSONL entry without a newline, readEvents ignores that fragment, but this append concatenates the new event directly onto it. The call returns inserted: true, yet subsequent reads discard the combined invalid line, silently losing the newly acknowledged record. The snapshot append has the same problem. Recover or terminate a torn trailing entry before appending another event.

Useful? React with 👍 / 👎.

Comment on lines +244 to +245
const events = await readEvents(file);
const existing = events.find((event) => event.type === "report.snapshot" && event.snapshot?.id === snapshot.id)?.snapshot;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject snapshots that conflict with an existing generation

If a generation has already been stored, a caller can rebuild its input with changed block text but the same period/template/cursor and successfully create the first snapshot from that conflicting record. assertCanonicalRecord accepts both because their IDs depend only on the key, and this lookup checks only existing snapshots. The log then contains different content under the same report-record ID, despite appendReportRecord rejecting that exact conflict. Compare the snapshot source against any stored generation with its ID before freezing it.

Useful? React with 👍 / 👎.

@dezren39

Copy link
Copy Markdown
Member Author

Follow-up for task 21000: hardened the C7 append path against torn trailing JSONL writes (new record and snapshot appends remain readable), reject snapshots whose canonical source conflicts with an already stored generation, and added deterministic generated-date coverage for Chicago-midnight adjacency and next-day 08:00 soft close. Verified on the laptop at d0b2def: bun test test/report-records-c7.test.ts test/report-periods-c7.test.ts — 11 pass, 0 fail, 2,092 assertions; git diff --check — pass. bun run typecheck remains unavailable because this worktree lacks @types/bun (TS2688). The broader C7 leaf still has follow-on work for fleet-KV integration and generation beyond the daily period; this PR is not marked as closing #12349.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d0b2def6a5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

assertCanonicalRecord(record);
return serializeAppend(file, async () => {
const events = await readEvents(file);
const existing = events.find((event) => event.type === "report.generated" && event.record?.id === record.id)?.record;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check frozen snapshots before accepting a generation

If a caller freezes a record before appending its generation, then appends changed block text with the same period/template/cursor, this lookup finds no generation and accepts the conflicting record. The log consequently exposes different content under the same report-record ID through readReportSnapshots and readReportRecords. The new generation check in appendReportSnapshot protects only the opposite call order; standalone snapshots are still accepted. Compare against snapshot records with the same report-record ID here as well, or require the matching generation to exist before freezing.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 176c723b97

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +361 to +363
return `{${Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${canonicalJson(object[key])}`).join(",")}}`;
}
return JSON.stringify(value) ?? "null";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Match JSON serialization when comparing optional properties

When a valid block contains links: undefined, the first append succeeds, but an identical rerun throws ReportRecordConflictError. JSON serialization omits that optional property from the stored event, whereas canonicalJson retains it as "links":null, so the persisted record and the unchanged input compare differently. Omit undefined object properties during canonicalization, or normalize records to their persisted JSON representation before comparison, to preserve the documented idempotency contract.

Useful? React with 👍 / 👎.

Comment on lines +321 to +323
if (snapshot.record.period.id !== record.period.id) throw new Error("correction period must match the snapshot");
correction.originalRecordId = snapshot.reportRecordId;
const existing = events.find((event) => event.type === "report.snapshot-correction" && event.correction?.id === correction.id)?.correction;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject correction content that conflicts with a stored record ID

After storing a generation and its snapshot, a caller can build changed block text with the original period/template/cursor and pass it to appendReportSnapshotCorrection. This checks only existing correction IDs, so the first correction succeeds even though its embedded record has the original generation's ID with different content; readReportRecords and readReportSnapshotCorrections then expose contradictory payloads for that ID. Check the correction source against stored generations and the frozen snapshot, rejecting conflicting content unless it uses a new record key.

Useful? React with 👍 / 👎.

@dezren39 dezren39 changed the title feat(pm-stack): add C7 Chicago daily period boundaries feat(pm-stack): persist canonical C7 reports Nov 22, 2026
@dezren39

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2b79ec3e81

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +253 to +254
assertRecordIdContent(await readLocationEvents(location), record);
const result = await appendDocumentEvent(location, event);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Enforce record-content consistency atomically across event types

When a generation and a snapshot containing different content for the same record ID are appended concurrently to the remote backend, both can pass assertRecordIdContent before either insert completes. Their document keys differ by event type, so both inserts succeed, leaving contradictory payloads under one report-record ID. Fresh evidence: running these calls together with Promise.all returned inserted: true for both, and subsequent reads exposed different block text in the generation and snapshot. The sequential checks fix the previously reported call-order cases but not this race; enforce a shared, atomic content claim per record ID before accepting generation, snapshot, or correction events.

Useful? React with 👍 / 👎.

Comment on lines +87 to +88

/** Build a validated canonical record; generatedAt is injectable for replay. */

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Encode the record identity tuple without delimiter ambiguity

If templateVersion or sourceCursor contains a newline, distinct accepted identity tuples can produce the same hash input. For example, (templateVersion: "daily-v1\nsource", sourceCursor: "cursor") and (templateVersion: "daily-v1", sourceCursor: "source\ncursor") produce identical record IDs for the same period. Both pass makeReportRecord validation, but after persisting one, appending the other raises a content conflict instead of retaining the distinct report version. Serialize the tuple unambiguously, such as with a JSON array, or reject delimiter characters before constructing the key.

Useful? React with 👍 / 👎.

@developing-today-fleet

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@developing-today-fleet
developing-today-fleet Bot merged commit 4b6b9cb into main Nov 22, 2026
1 check passed
@developing-today-fleet
developing-today-fleet Bot deleted the task/21000-c7-canonical-records branch November 22, 2026 08:40

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a7f33fbb31

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +33 to +34
describe("C7 canonical report records", () => {
test("persists provenance and cursor and makes a rerun a no-op", async () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Generate coverage for canonical record persistence invariants

The new record suite uses only hand-picked examples, although stable identity encoding, JSON round-trip idempotency, and immutable content across event types are meaningful invariants covered by the repository's mandatory property/model-based testing doctrine. The generated calendar tests do not exercise these persistence invariants. Add generated valid records and append-operation sequences, using named invariant or model oracles to verify distinct identity tuples, idempotent retries after serialization, and cross-event content consistency.

AGENTS.md reference: AGENTS.md:L37-L39

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C7 leaf 1: canonical report records, KV append, period generation, and snapshots

1 participant