Skip to content

feat: bind TQQQ snapshots to successor calendar - #196

Closed
Pigbibi wants to merge 2 commits into
mainfrom
codex/tqqq-successor-calendar-bound-fresh-two-file-reslice-20260725
Closed

feat: bind TQQQ snapshots to successor calendar#196
Pigbibi wants to merge 2 commits into
mainfrom
codex/tqqq-successor-calendar-bound-fresh-two-file-reslice-20260725

Conversation

@Pigbibi

@Pigbibi Pigbibi commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Scope

  • binds the existing TQQQ snapshot materializer to the approved successor-calendar artifact through safe no-follow reads and exact coverage checks
  • publishes calendar-bound snapshots via digest-addressed exclusive directories with COMPLETE written last
  • adds retry safety and E4 freshness-binding validation

Verification

  • PYTHONPATH=src python3 -m pytest -q tests/test_tqqq_r1_snapshot.py (24 passed)
  • provider-free local integration readback against the approved real calendar artifact (8301 rows)
  • python3 -m ruff check src/us_equity_snapshot_pipelines/tqqq_r1_snapshot.py tests/test_tqqq_r1_snapshot.py
  • python3 -m py_compile src/us_equity_snapshot_pipelines/tqqq_r1_snapshot.py
  • python3 -m compileall -q src
  • full suite has 11 base-equivalent collection errors unrelated to this two-file change; no full-suite pass claim

No provider, price-data, E3/E4 generation, merge, or live action.

Co-Authored-By: Codex <noreply@openai.com>

@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: daa6a4318c

ℹ️ 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 +404 to +414
manifest = {
"contract_version": CONTRACT_VERSION,
"symbols": list(SYMBOLS),
"requested_lower_bound": REQUESTED_LOWER_BOUND,
"price_field": PRICE_FIELD,
"plugin": PLUGIN,
"mode": MODE,
"size": 0,
"row_count": len(prices),
"prices_sha256": _sha256(staging / "prices.csv"),
}

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 Bind the calendar digest into the published manifest

The digest-addressed artifact is derived only from the price bytes and generic metadata; expected_calendar_sha256 and the successor session never enter the manifest. Consequently, two accepted calendar artifacts with identical session dates but different close/early-close metadata produce the same manifest hash and publication directory, and the resulting snapshot can later be verified against either calendar by changing the external arguments. Include the trusted calendar digest and successor identity in the hashed artifact so its claimed provenance is immutable.

Useful? React with 👍 / 👎.

Comment on lines +149 to +150
if (pd.Timestamp(successor) - pd.Timestamp(dates[-2])).days not in (1, 3):
_invalid("calendar successor is not adjacent")

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 Accept successor gaps caused by exchange holidays

Restricting adjacent sessions to calendar-day gaps of 1 or 3 rejects valid XNYS calendars around holidays. For example, a Friday completed session followed by Tuesday after a Monday market holiday has a four-day gap, while a Wednesday-to-Friday sequence around a Thursday holiday has a two-day gap, so calendar-bound materialization fails despite the supplied calendar hash and session sequence being valid.

Useful? React with 👍 / 👎.

Comment on lines +439 to +442
except OSError as exc:
if attempt == 0 and _owned_directory_cleanup(destination, identity):
continue
raise SnapshotValidationError("immutable publication failed") from exc

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 Clean up the owned directory after the final failed write

If an OSError occurs during the second publication attempt, this branch raises without calling _owned_directory_cleanup. That leaves an incomplete digest-named directory under the output root, and every subsequent retry fails immediately with immutable output already exists, requiring manual repair after a transient second write or fsync failure.

Useful? React with 👍 / 👎.

Comment on lines +421 to +425
output_root.mkdir(parents=True, exist_ok=True)
destination = output_root / manifest_sha256
for attempt in range(2):
try:
os.mkdir(destination, 0o700)

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 Reject a symlinked calendar-bound output root

When the requested output root already exists as a symlink to a directory, mkdir(..., exist_ok=True) accepts it and the subsequent destination creation follows that symlink. In a shared writable parent, another user can therefore pre-create the root symlink and redirect publication to an arbitrary directory accessible to the pipeline process; verification still succeeds because the digest child itself is not a symlink. Reject the root symlink or anchor creation to a verified directory descriptor before publishing.

Useful? React with 👍 / 👎.

Comment on lines +431 to +434
for name in OUTPUT_FILENAMES:
_write_exclusive(destination / name, payloads[name])
_write_exclusive(destination / "COMPLETE", f"{manifest_sha256}\n".encode("ascii"))
result = verify_tqqq_r1_snapshot(destination, expected_manifest_sha256=manifest_sha256, calendar_path=calendar_path, expected_calendar_sha256=expected_calendar_sha256)

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 Fsync publication directories before reporting completion

Although _write_exclusive fsyncs each file, publishing COMPLETE and returning without fsyncing the destination directory or its parent does not durably commit the new directory entries. After a power loss or kernel crash, the function may already have reported success while the digest directory, one of its members, or the completion marker disappears on recovery, defeating the marker's immutable-completion guarantee.

Useful? React with 👍 / 👎.

@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown

🤖 Codex PR Review

Merge allowed: 1 finding(s) reported but none are blocking

ℹ️ Other Findings

1. 🟡 [MEDIUM] Logic in src/us_equity_snapshot_pipelines/tqqq_r1_snapshot.py

The new calendar-bound verification path never consumes or validates an E4 endpoint observation. _validate_e4_observation() is added and tested, but verify_tqqq_r1_snapshot() only accepts calendar_path/expected_calendar_sha256, so every calendar-bound readback will mark a snapshot valid even if there is no matching E4 observation or the upstream endpoint evidence is stale. That makes the advertised freshness-binding unenforced on a current entry point proven by the PR's local integration readback flow. (line 286)

Suggestion: Plumb the E4 observation (or a verified E4 artifact path/digest) into the public materialize/verify APIs and call _validate_e4_observation() before accepting or publishing a calendar-bound snapshot; fail verification when the E4 payload is missing or does not match the manifest/calendar binding.


Review by Codex PR Review bot • PR

Co-Authored-By: Codex <noreply@openai.com>

@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: 465d82ec93

ℹ️ 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".

destination = output_root / manifest_sha256
for attempt in range(2):
try:
os.mkdir(destination, 0o700)

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 Anchor publication to the checked output root

When the output root is in a concurrently writable location, an attacker can rename it and install a symlink after the checks at lines 417–424; this pathname-based os.mkdir and every subsequent member write then follow the replacement root. The final identity check covers only the redirected digest directory, so publication can report success outside the requested root. Fresh evidence after the earlier symlink fix is that destination creation still resolves the root by pathname rather than through a verified directory descriptor.

Useful? React with 👍 / 👎.

Comment on lines +419 to +424
if output_root.exists():
if not output_root.is_dir():
_invalid("calendar-bound output root must be a directory")
else:
os.mkdir(output_root, 0o700)
_fsync_directory(output_root.parent)

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 Fsync the parent when reusing an existing root

If a first call creates output_root but its parent fsync raises, the root remains visible and a retry enters this existing-root branch. That retry can publish successfully while never retrying the parent fsync; syncing the root itself does not durably commit the root's directory entry in its parent, so a crash can still remove the supposedly completed publication. The new fsync calls therefore leave this failure-and-retry path uncovered.

Useful? React with 👍 / 👎.

os.mkdir(destination, 0o700)
except FileExistsError as exc:
raise SnapshotValidationError(f"immutable output already exists: {destination}") from exc
created = os.lstat(destination)

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 Clean up when post-mkdir identity lookup fails

If os.mkdir(destination) succeeds but the immediately following lstat raises—for example due to a transient filesystem error—the lookup is outside the cleanup-protected try, leaving an empty digest-named directory behind. Every later publication attempt then fails with immutable output already exists, so this remaining failure point still defeats automatic recovery and requires manual deletion.

Useful? React with 👍 / 👎.

Comment on lines +87 to +88
if observed > _utc_now():
_invalid("future E4 endpoint observation")

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 Bound endpoint observations before the successor close

When validation runs after the successor session has closed, an endpoint_observed_at_utc at or after successor_close_utc passes as long as it is not later than the wall clock. Such evidence cannot establish that prices correctly end at completed_last_session, because the successor is already complete and should now be represented; this allows a stale snapshot cutoff to pass the E4 freshness check. Parse the bound successor close and require the observation to precede it.

Useful? React with 👍 / 👎.

@Pigbibi Pigbibi closed this Jul 25, 2026
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.

1 participant