generator: typed-table emit exceeds SQLite column cap on wide config singletons
Filed by /printing-press-retro from a run of HaloPSA (scorecard 91/100 Grade A, 100% verify pass).
Summary
The typed-table emit path generates one column per leaf JSON field in a resource's response schema with no upper bound. On HaloPSA's tenant-global /Control config singleton, this produced a 3,747-column CREATE TABLE that exceeded SQLite's default SQLITE_MAX_COLUMN=2000. Generation passed every quality gate; sync --full --dry-run died cold with SQL logic error: too many columns on control (1). The failure mode is silent until first sync.
Where to look
Component: generator
Likely area: Typed-table-emit path in internal/generator/templates/store/ — wherever CREATE TABLE text is composed per discovered resource.
Triggered when: A spec resource's response schema flattens to >~1500 leaf fields. Enterprise config singletons are the obvious class.
What we observed
Run-1 output (post-generate):
code
PASS go mod tidy / govulncheck / go vet / go build
PASS halopsa-pp-cli --help / version / doctor
Generated halopsa at .../working/halopsa-pp-cli
First sync --full --dry-run:
code
Error: opening local database: running migrations:
migration failed: SQL logic error: too many columns on control (1)
Per-table column counts (top 5 widest, recorded by Python introspection of internal/store/store.go):
table
columns
control
3747
tickets
847
projects
847
opportunities
847
client
447
Tickets / projects / opportunities sit just under the 2000 cap but with no headroom; the next major-version Halo response schema could push them over.
Assistant
I'll start by exploring the repository structure.
Assistant
Now let me look at the store template.
Assistant
Now let me write a temporary reproduction test.
Assistant
Now update schemaWithDependentParents in generator.go and the template helpers.
Assistant
Now the store template changes.
Assistant
Now add the JSON-only upsert helper and UpsertBatch dispatch.
Assistant
Now run the broader test suites.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Workaround applied this run: hand-stripped /Control* paths from the spec (5 paths) and added a stub Control schema so residual $ref targets resolved. Regenerated successfully.
Suspected root cause
Confident. The typed-table-emit path walks the resource's response schema and emits one column per leaf field with no width sanity check. The store package has no JSON-only fallback for resources whose schema exceeds a safe column count.
Suggested direction
In the typed-table-emit path, compute the column count before writing CREATE TABLE. If the count exceeds a threshold (1500 leaves headroom under SQLite's default 2000; tunable), fall back to JSON-only schema: id TEXT PRIMARY KEY, data JSON NOT NULL, synced_at DATETIME. Emit a generation-time log line naming the resource and the fallback (e.g., store-fallback: control (3747 cols) → JSON-only).
Preserve typed-column behavior below the threshold — the typed columns power offline FTS5 and cross-entity SQL joins; the fallback should only fire on pathological widths.
Acceptance criteria
positive: feed a synthetic spec where one resource has 3000+ leaf fields; generated store.go for that resource has only 3 columns; sync against an empty DB succeeds.
negative: feed a normal resource (<1500 cols); typed-table emission unchanged; flat columns still emitted.
Frequency
subclass:wide-config-singleton — complete-functional-failure when it fires.
Complexity
small (column-count check + JSON-only emit branch).
Scope boundary
Does NOT cover per-column truncation, schema-aware projection, or per-API column allowlists. Single threshold + JSON-only fallback only.
Dependencies
None.
Related issues
None — first sighting of this specific failure mode.
Use 1500 columns as the safety cap for a generated typed SQLite resource table, including dependent-parent columns. Resources exceeding the cap must retain their per-resource table but fall back to a JSON-only schema containing id, data, and synced_at; do not generate typed-column indexes, FTS structures, or typed upsert code for that fallback table. Batch store generation must dispatch wide resources to the JSON-only path while preserving the existing typed behavior for ordinary narrow resources.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work.
For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state.
You can also reset all changes in the repository using git reset --hard command.
Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later.
Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands.
However you don't need to use git add or git commit before submitting your changes. In fact,
submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.
CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
If an error was reported in the issue description, I have successfully reproduced it.
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
I have thought about edge cases and made sure my fix handles them as well.
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /cli-printing-press, current file: none) bash-$
All changes are in place and verified. Let me submit.
Checklist review:
"If an error was reported in the issue description, I have successfully reproduced it." — Done. I wrote a temporary generator test that builds a synthetic spec whose control resource response type has 3000+ leaf fields. Before the fix the generated internal/store/store.go contained a 3007-column CREATE TABLE "control", and the generated store tests died exactly as reported: running migrations: migration failed: SQL logic error: too many columns on control (1) (plus too many columns on sqlite_altertab_control in the ALTER-TABLE backfill path).
"I have confirmed that my fix resolves the error." — Done. After the change, generation logs store-fallback: control (3007 cols) → JSON-only (exceeds 1500-column cap), the emitted table is exactly "id" TEXT PRIMARY KEY, "data" JSON NOT NULL, "synced_at" DATETIME DEFAULT CURRENT_TIMESTAMP, no control_fts/indexes/upsertControlTx are generated, UpsertBatch dispatches case "control" to the shared upsertJSONOnlyTx, and the generated project passes go build ./..., go vet ./..., and its own store test suite against an empty DB (I also added a temporary in-generated-project test proving rows actually land in the fallback table via both UpsertBatch and UpsertJSONOnly).
"I have thought about edge cases." — Boundary: a table at exactly 1500 columns stays typed (with FTS5), 1501 falls back. Dependent-parent columns count toward the cap (cap re-applied after parent_id injection), and parent_id is never added to a fallback table so it stays exactly 3 columns. Wide sub-resource tables lose their NOT NULL parent FK along with the rest of the typed shape, so no unsatisfiable constraint. Indexes/FTS5/typed upserts/backfill columns/search dispatch are all suppressed for fallback tables; the sync single-object path routes wide resources to UpsertJSONOnly so the table isn't dead. Fallback is idempotent, so re-applying the cap is safe. Narrow resources are untouched — generated output for the clerk/stytch/loops fixtures is byte-identical to pre-change output.
"I have run existing tests that might have been affected." — Ran go test -short ./..., the non-short generator store/schema/sync/compile tests, and scripts/golden.sh verify. The only failures (govulncheck stdlib CVE in TestGenerateWithNoAuth, TestLiveCheck_BinaryRefreshReasonIncludesSourceWalkError, and golden diffs caused solely by this sandbox's repo path being /cli-printing-press so <REPO> normalization mangles the github.com/mvanhorn/cli-printing-press URL) reproduce identically on a stashed clean tree, i.e. pre-existing/environmental. No store-related golden content changed. I removed all temporary test/helper files (zz_tmp_*_test.go, tmpgen/, built binary, .gotmp) before submitting; no existing tests were modified.