Skip to content

Refactor .env.local handling and knowledge base document lifecycle - #405

Open
sroussey wants to merge 3 commits into
mainfrom
claude/elegant-fermi-brwjz1
Open

sroussey wants to merge 3 commits into
mainfrom
claude/elegant-fermi-brwjz1

Conversation

@sroussey

Copy link
Copy Markdown
Contributor

Summary

This PR refactors environment file handling to properly quote values for Bun's .env loader, implements knowledge base document purging on re-conversion, and improves XBRL fact querying for co-filed submissions.

Key Changes

Environment File Handling (src/task/init/envFile.ts)

  • New module extracted from InitApplyTask with proper .env.local quoting logic
  • Implements encodeEnvValue() that quotes values with single or double quotes and escapes $ signs, ensuring Bun's loader reads back exactly what was written
  • Adds mergeEnvConfig() to fold wizard answers into existing .env.local files, preserving non-wizard lines and removing stale keys
  • Implements writeEnvFile() with owner-only file permissions (0o600) for security
  • Adds redactInitConfig() to mask credentials in printed output

Knowledge Base Document Lifecycle

  • New purgeKbDocuments() function (src/kb/purgeKbDocuments.ts) removes documents and chunks from the knowledge base before re-conversion
  • retireIndexedDocuments() in ConvertFilingDocumentTask clears kb_indexed_at stamps before purging, ensuring interrupted operations don't permanently hide documents
  • Prevents stale embeddings from being cited after re-conversion and removes documents the new conversion no longer emits

Knowledge Base Indexing Stamp (src/config/kbIndexedStamp.ts)

  • Clarifies that kb_indexed_at means "embedded from the conversion on disk", not just "has a kb_document row"
  • Backfill runs only once during db setup; subsequent passes clear stamps for documents the knowledge base no longer backs
  • Enables re-converted documents to be re-indexed by clearing their stamps

XBRL Fact Querying for Co-filed Submissions (src/cli/queries/XbrlQuery.ts)

  • New accessionsConvertedFor() function queries which accessions a CIK has converted documents under
  • New factCriteria() function builds search criteria covering accessions in chunks (500 per in list) to respect SQLite/Postgres parameter limits
  • New pageAcross() and streamAcross() functions paginate and stream facts across multiple criteria in (accession, fact_index) order
  • Fixes issue where co-filed submission facts were only visible to the last filer to convert them; now all co-filers can query the same facts

CLI Initialization (src/cli/groups/init.ts)

  • New Prompter interface with ask() and askSecret() methods
  • askSecret() mutes stdout while reading passwords/URLs to prevent echoing credentials
  • Imports environment file functions from new envFile module
  • Updates messaging to clarify that existing .env.local is merged, not overwritten

Test Coverage

  • New reconversionIndex.sqlite.test.ts tests knowledge base behavior during re-conversion
  • New ConvertFilingDocumentTask.coFiled.test.ts verifies co-filed submission facts remain accessible to all filers
  • Enhanced init.test.ts with round-trip tests through Bun's .env loader, testing special characters, quotes, backslashes, and file merging
  • Enhanced XbrlQuery.test.ts with pagination tests across 501+ accessions

Notable Implementation Details

  • Bun's .env loader quirks: Values are quoted with whichever quote character they don't contain; $ is escaped as \$; line breaks, both quote types, and trailing backslashes are rejected
  • Co-filed submission facts: Stored once under the first filer's CIK, but now queryable by all co-filers through the filing_document table
  • Idempotent stamp sync: Backfill is one-time; clearing stale stamps is idempotent and runs every pass
  • Chunked accession queries: Large CIKs with 500+ accessions are

…er-only

Bun's .env loader expands $NAME inside single and double quotes alike,
honours only \$ as an escape, and keeps \" and \\ literally, so the old
writer turned a password like pa$$w0rd$HOME into pa$/root on the next
command. Values are now quoted with whichever quote they lack and every
$ is written \$; a line break, both quote kinds, or a trailing backslash
has no spelling and is refused with the key named.

Setup no longer replaces .env.local: it rewrites the keys it owns
(removing owned keys the new answers leave unset) and keeps API keys,
fetch limits and comments. The file is written and chmod'ed 0600, the
password and connection string are read without echo, and --dry-run
prints them masked.

The new test spawns bun to load the written file and compares.

Fixes #404

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015q4aqYG7aoVFafwF8YprdM
…on removed

The index selection anti-joined on kb_document existence, so a filing
re-converted after a FILING_CONVERTER_VERSION bump kept its old
knowledge-base row and was never re-embedded; `ask` went on citing the
previous converter's text, and a member the new pass no longer emits
stayed citable forever since nothing deleted knowledge-base documents.

- A re-conversion now retires what the knowledge base holds for the
  members it replaces: it clears kb_indexed_at on every row naming them
  (co-filers included) and then purges their kb_document and kb_chunk
  rows (purgeKbDocuments, a no-op wherever the index cannot exist).
- Once db setup has backfilled the stamp, the selection reads it alone:
  null means "not embedded from the conversion on disk". Before that
  (no partial index) it keeps the anti-join.
- db setup backfills stamps only in the pass that creates the partial
  index, so a later setup no longer re-stamps a re-converted document.
- countAlreadyIndexed counts the stamped complement.

Fixes #401

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015q4aqYG7aoVFafwF8YprdM
…irst

xbrl_fact is keyed by accession, but conversion runs once per
(cik, accession). Each co-filer's conversion deleted the accession's
facts and rewrote them under its own CIK, so `sec show xbrl --cik` went
empty for every filer but the last; the delete-then-put order was also
the one XbrlFactRepo documents as losing every fact on a rejected row.

- Facts belong to the submission. A conversion skips extraction when a
  co-filer's conversion at the current converter version already wrote
  them, and otherwise writes through XbrlFactRepo.replaceForAccession
  (write first, then trim the stale tail; an empty extraction clears).
  replaceForAccession now batches its putBulk calls.
- queryXbrlFacts by CIK reads the accessions that CIK has converted
  documents under, in ascending `in` chunks paged in order, falling back
  to the cik column when it has none. Existing databases need no
  migration: their facts already answer for every filer this way.
- Remove XbrlFactRepo.getByCikConcept, unused outside its test and
  wrong for co-filed submissions.

Fixes #402

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015q4aqYG7aoVFafwF8YprdM
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.

2 participants