Refresh incrementally from SEC's daily form index (#12) #54

Merged
chris-kreidl merged 1 commit from 12-incremental-refresh into main 2026-11-12 16:22:45 +00:00
chris-kreidl commented 2026-11-12 13:54:46 +00:00 (Migrated from github.com)

🤖 Claude: Closes #12

bun cli.ts refresh now updates data/ from SEC's daily form index instead of downloading the ~1 GB zip each time.

How it works

  • Each quarter's daily-index/<year>/QTR<n>/index.json lists the form.<yyyymmdd>.idx files that exist, so weekends, holidays, and today's unpublished index are simply absent. A request for a missing file returns 403, the same status SEC uses for rate-limit blocks, so guessing from errors would be ambiguous.
  • Only exact 10-K rows count (not 10-K/A or 10-KT), since those are the only filings analyze reads. Each filing CIK's companyfacts is downloaded and replaces its file in place (written to a temp file, then renamed).
  • Lag check: a 10-K counts only if its accession number appears in the downloaded facts. Otherwise it goes into pending_10k and is retried on the next refresh. A 404 (no companyfacts for that company yet) is pending too.
  • Bulk zip: used on the first run (no refresh_state row or no data/), with --full, or when the last covered day is more than 30 days old. Coverage is set to the day before the zip's Last-Modified date (New York time), so the next refresh re-reads the day the zip was built.
  • State lives in SQLite: refresh_state and pending_10k, both created by ensureSchema. It is written in one transaction at the end, so a failed run leaves state unchanged and the next run repeats the same days.
  • Rate: secFetch in src/sec.ts sends SEC_HEADERS and paces all calls to at most 5 starts per second, with up to 5 downloads in flight. Any HTTP error other than 404 stops the refresh immediately rather than continuing to hit SEC.
  • The CLI takes --full, the refresh hint shows coverage and the pending count, and the summary lists pending and expired 10-Ks.

Decision for you

A 10-K that never shows up in SEC's API (for example, a filer with no XBRL) would otherwise be retried forever. I drop it after the same 30 days and list it as expired. Say if you'd rather handle that differently.

Demonstrated on real data

I copied yesterday's data/ to a separate folder. It came from the bulk zip SEC built the night of 11-10, so it covers filings through 11-09. In a second folder I ran a --full refresh today.

incremental (one day after a full) --full today
refresh 2.0 s: 1 daily index (11-10; 11-11 holiday skipped), 7 10-Ks fetched, 0 pending 138 s, 1,358 MB, 20,571 files
analyze 5,457 analyzed, 3,342 with ROTC 5,457 analyzed, 3,342 with ROTC
  • company_analysis: all 5,456 rows match on every analysis column (0 differences).
  • rank: I enriched once and copied the price and sector columns into the other database, since live prices drift between runs. Both produced byte-identical CSVs, each ranking 1,388 companies.
  • Nothing was pending, because 11-10 was a quiet day. The pending, retry, expiry, quarter-boundary, and 403 paths are covered by tests with a fake SEC, not by this run.

Definition of Done

  • Acceptance criteria met and demonstrated (above)
  • bun test passes; new logic has tests: 236 pass. test/refresh.test.ts has 15 tests covering each criterion, plus a CLI test for --full
  • bun check is clean and bun fmt --check passes
  • Runs end to end on real data (not just fixtures): see above
  • Stand-up posted on the milestone tracking issue when the story started and when this PR opened (#50)

🤖 Generated with Claude Code

🤖 **Claude:** Closes #12 `bun cli.ts refresh` now updates `data/` from SEC's daily form index instead of downloading the ~1 GB zip each time. ### How it works - Each quarter's `daily-index/<year>/QTR<n>/index.json` lists the `form.<yyyymmdd>.idx` files that exist, so weekends, holidays, and today's unpublished index are simply absent. A request for a missing file returns 403, the same status SEC uses for rate-limit blocks, so guessing from errors would be ambiguous. - Only exact `10-K` rows count (not `10-K/A` or `10-KT`), since those are the only filings `analyze` reads. Each filing CIK's `companyfacts` is downloaded and replaces its file in place (written to a temp file, then renamed). - **Lag check:** a 10-K counts only if its accession number appears in the downloaded facts. Otherwise it goes into `pending_10k` and is retried on the next refresh. A 404 (no companyfacts for that company yet) is pending too. - **Bulk zip:** used on the first run (no `refresh_state` row or no `data/`), with `--full`, or when the last covered day is more than 30 days old. Coverage is set to the day before the zip's `Last-Modified` date (New York time), so the next refresh re-reads the day the zip was built. - **State** lives in SQLite: `refresh_state` and `pending_10k`, both created by `ensureSchema`. It is written in one transaction at the end, so a failed run leaves state unchanged and the next run repeats the same days. - **Rate:** `secFetch` in `src/sec.ts` sends `SEC_HEADERS` and paces all calls to at most 5 starts per second, with up to 5 downloads in flight. Any HTTP error other than 404 stops the refresh immediately rather than continuing to hit SEC. - The CLI takes `--full`, the refresh hint shows coverage and the pending count, and the summary lists pending and expired 10-Ks. ### Decision for you A 10-K that never shows up in SEC's API (for example, a filer with no XBRL) would otherwise be retried forever. I drop it after the same 30 days and list it as expired. Say if you'd rather handle that differently. ### Demonstrated on real data I copied yesterday's `data/` to a separate folder. It came from the bulk zip SEC built the night of 11-10, so it covers filings through 11-09. In a second folder I ran a `--full` refresh today. | | incremental (one day after a full) | `--full` today | |---|---|---| | refresh | **2.0 s**: 1 daily index (11-10; 11-11 holiday skipped), **7 10-Ks fetched**, 0 pending | 138 s, 1,358 MB, 20,571 files | | analyze | 5,457 analyzed, 3,342 with ROTC | 5,457 analyzed, 3,342 with ROTC | - `company_analysis`: all 5,456 rows match on every analysis column (0 differences). - `rank`: I enriched once and copied the price and sector columns into the other database, since live prices drift between runs. Both produced byte-identical CSVs, each ranking 1,388 companies. - Nothing was pending, because 11-10 was a quiet day. The pending, retry, expiry, quarter-boundary, and 403 paths are covered by tests with a fake SEC, not by this run. ### Definition of Done - [x] Acceptance criteria met and demonstrated (above) - [x] `bun test` passes; new logic has tests: 236 pass. `test/refresh.test.ts` has 15 tests covering each criterion, plus a CLI test for `--full` - [x] `bun check` is clean and `bun fmt --check` passes - [x] Runs end to end on real data (not just fixtures): see above - [x] Stand-up posted on the milestone tracking issue when the story started and when this PR opened (#50) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
claude[bot] commented 2026-11-12 13:55:57 +00:00 (Migrated from github.com)

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

## Code review No issues found. Checked for bugs and CLAUDE.md compliance.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ckreidl/screener!54
No description provided.