ISSUE DESCRIPTION generator: typed-table emit exceeds SQLite column cap on wide config…
- User
ISSUE DESCRIPTION
generator: typed-table emit exceeds SQLite column cap on wide config singletons
Filed by
/printing-press-retrofrom 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
/Controlconfig singleton, this produced a 3,747-columnCREATE TABLEthat exceeded SQLite's defaultSQLITE_MAX_COLUMN=2000. Generation passed every quality gate;sync --full --dry-rundied cold withSQL 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/— whereverCREATE TABLEtext 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):
PASS go mod tidy / govulncheck / go vet / go build PASS halopsa-pp-cli --help / version / doctor Generated halopsa at .../working/halopsa-pp-cliFirst
sync --full --dry-run: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.
Workaround applied this run: hand-stripped
/Control*paths from the spec (5 paths) and added a stubControlschema so residual$reftargets resolved. Regenerated successfully.Suspected root cause
Assistant
Let me analyze the issue:
- The problem is in the typed-table-emit path in the generator component
- It generates one column per leaf JSON field with no upper bound
- On wide config singletons (like HaloPSA's
/Control), this produces 3,747 columns exceeding SQLite's 2,000 column limit - The solution is to add a threshold (1,500 columns) and fall back to JSON-only schema when exceeded
Let me start by exploring the repository structure to understand the codebase.
Assistant
I need to find the generator templates and store-related code. Let me look at the repository structure more specifically.
<tool_call> <function=bash_terminal> <parameter=command> ls -la /cli-printing-press/ </tool_call> <tool_call> <function=bash_terminal> <parameter=command> find /cli-printing-press -type d -name "generator" | head -20 </tool_call> <tool_call> <function=bash_terminal> <parameter=command> find /cli-printing-press -type d -name "templates" | head -20 </tool_call>
Assistant
Let me explore the repository structure to find the relevant code.
Assistant
Let me look at the internal directory to find the generator component.
Assistant
Now let me look specifically at the generator/templates directory since that's where the store templates would be, and also the store package.
Assistant
Let me look at all template files in the generator directory and specifically search for store-related files.
Assistant
Let me look for all files in the generator directory, including templates, to understand the structure better.
Assistant
Let me look at the main generator.go file and also find the store-related templates.
Assistant
Now I found the store template. Let me look at the store.go.tmpl file to understand how tables are generated:
Assistant
I found the store.go.tmpl template. Now let me look at the schema_builder.go to understand how tables and columns are built before rendering.
Assistant
Now I understand the structure. Let me see the rest of the store.go.tmpl template to find the CREATE TABLE emission part:
Assistant
The key line is 279 - let me look at the CREATE TABLE block starting around there. The issue is that when a table has many columns (3747 in the case of the "control" resource), it exceeds SQLite's 2000 column limit. I need to:
- Look at how tables and their columns are defined
- Add a check in schema_builder.go to count columns before emitting CREATE TABLE
- If column count exceeds the threshold (1500), fall back to JSON-only schema
Let me look at the template more carefully to understand the full CREATE TABLE emission.
Assistant
Let me look at the hasTypedTable function and understand the full template flow:
Assistant
Let me look at the generator.go file to understand the hasTypedTable function and the overall template execution.
Assistant
Now I understand the architecture. Let me look at more of the generator.go to see the table building and the full flow:
Assistant
Let me look at the domainUpsertMethodName function and understand the full picture.
Assistant
Now I have a clear understanding of the architecture. Let me look at the store_upsert_batch_test.go.tmpl and sync.go.tmpl to understand how the typed table dispatch works:
Assistant
Now let me look at the sync.go.tmpl to see how resources are upserted:
Assistant
Now I understand the full architecture. Let me look at how BuildSchema is called and how the generator works:
Assistant
Let me see the context around line 2442.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '2430,2500p' /cli-printing-press/internal/generator/generator.go </tool_call>
Assistant
I need to look at the context around line 2442 in generator.go where BuildSchema is called.
Assistant
Now I have a clear understanding of the codebase. Let me summarize my understanding:
BuildSchema()inschema_builder.gocreates table definitions with columnshasTypedTableingenerator.godetermines if a table gets typed columns- The template
store.go.tmplemits CREATE TABLE for each table - The issue is no column count check exists before emitting CREATE TABLE
My plan:
- Add a column count threshold (1500) to
TableDef - In
BuildSchema(), check if column count exceeds threshold and mark table as JSON-only - Modify
hasTypedTableto returnfalsefor JSON-only tables - Update template to handle JSON-only tables (skip typed columns)
Let me implement this:
Assistant
Now I have a complete understanding of the architecture. Let me implement the fix:
- Add a
WideResourcefield toTableDefto mark tables that exceed the column threshold - In
BuildSchema(), check column count and fall back to JSON-only if threshold is exceeded - Modify
hasTypedTableto returnfalsefor wide resources - This automatically gates CREATE TABLE, indexes, FTS5, and upsert code in templates
Let me start by modifying
schema_builder.go:- Add a
Assistant
Let me re-read the file and edit it properly.
Assistant
Let me try to edit the schema_builder.go file properly.
Assistant
The file got corrupted. Let me restore it from git and try again with a cleaner approach.
Assistant
Good, let me verify the file is restored and then use a more careful approach:
Assistant
Good, the file is restored. Let me use a careful approach with sed:
Assistant
Let me make the changes carefully using sed for small, precise edits:
Assistant
Let me verify the edit was applied correctly.
Assistant
The WideResource field was added successfully. Now I need to add the threshold check in BuildSchema() and modify hasTypedTable. Let me first look at the BuildSchema function to understand where to add the check:
Assistant
Let me look at where the table is finalized after all columns are added:
Assistant
Now I need to add the wide resource check after the FTS5 configuration and before appending the table. Let me insert the check at line 131 (before
tables = append(tables, table)):Assistant
Let me add the threshold constant and the wide resource check. I'll use sed to insert the code:
Assistant
Let me add the threshold constant and the wide resource check. First, I'll add the constant at the package level:
Assistant
Let me verify the constant was added:
Assistant
The sed didn't work as expected. Let me try a different approach using a Python one-liner to insert the text:
Assistant
Let me verify the changes were applied correctly.
Assistant
The constant was added. Now let me find the exact line where I need to add the wide resource check:
Assistant
I need to add the wide resource check before appending the table to the list. Let me look at the context around line 142 to see what's there and plan the edit.
Assistant
Now let me add the wide resource check before appending the table. I'll use Python for precision:
Assistant
Let me verify the wide resource check was added correctly:
Assistant
The wide resource check has been added. Now I need to update the
hasTypedTablefunction ingenerator.goto returnfalsefor wide resources:Assistant
I need to look at the hasTypedTable function and update it to check for WideResource.
Assistant
I need to update the hasTypedTable function in generator.go to also check for WideResource:
Assistant
I should add an explicit WideResource check in hasTypedTable for clarity and robustness:
Assistant
Let me verify the change was applied to generator.go:
Assistant
The hasTypedTable function in generator.go has been updated. Now let me verify the code compiles and check if there are any other places that need attention.
Assistant
Let me set up the Go environment properly:
Assistant
The environment has some GOPATH/GOCACHE configuration. Let me check what's already set up and use the proper configuration.
Assistant
Let me use the proper Go environment configuration:
Assistant
There's a duplicate declaration of
maxSQLiteColumns. Let me check and fix it:Assistant
The constant was declared twice. Let me fix this by removing the duplicate:
Assistant
I need to remove the duplicate maxSQLiteColumns declaration (lines 57-61). Let me fix this:
Assistant
Let me verify the fix and rebuild:
Assistant
Good, only one declaration remains. Let me rebuild:
Assistant
The code compiles successfully. Now let me run the existing tests to make sure nothing is broken:
Assistant
The test failure is unrelated to my changes - it's a pre-existing vulnerability scan issue (GO-2026-5856) in the Go standard library. All the actual generator tests passed. Let me verify the complete diff of my changes:
Assistant
Let me clean up the cache changes and verify just the source code changes:
Assistant
Let me check the diff again to see the final source code changes.
Assistant
The diff looks correct. Let me verify the build works and run the tests:
Assistant
Now let me think about the template behavior. The
hasTypedTablegate in the template controls CREATE TABLE emission. With my current code,hasTypedTablereturns false for wide resources, which means NO CREATE TABLE is emitted at all. But the acceptance criteria says "Resources exceeding the cap must retain their per-resource table but fall back to a JSON-only schema."I need to modify
hasTypedTableto return true for wide resources (so CREATE TABLE is emitted), while the wide resource check in BuildSchema already resets columns to just 3 base columns. Let me update the generator.go hasTypedTable function:Assistant
I need to reconsider my approach. Let me check how hasTypedTable gates everything in the templates and understand all the implications.
Assistant
Let me check all uses of hasTypedTable across templates:
Assistant
Let me reconsider the approach. Looking at the templates more carefully,
hasTypedTablegates both CREATE TABLE and typed Upsert methods. For wide resources, we need:- CREATE TABLE to be emitted (with 3 columns: id, data, synced_at)
- No typed Upsert methods
- No sync.go dispatch entry
So I need to:
- Make
hasTypedTablereturn true for wide resources (so CREATE TABLE is emitted) - Add a new
hasTypedUpsertfunction to gate typed Upsert/XxxTx methods
Let me first look at what changes I need:
Assistant
I changed hasTypedTable to return true when columns >= 3 (which includes wide resources with exactly 3 base columns). But I also need to ensure:
- No typed Upsert methods are generated for wide resources
- No sync.go dispatch entry for wide resources
- No typed upsert batch test for wide resources
Let me look at how these are gated in templates and add a new function
hasTypedUpsertto check for this.Actually, let me trace through the templates more carefully to understand what hasTypedTable gates.
Assistant
I need to understand how the templates use hasTypedTable more carefully. Let me examine each location:
Assistant
Let me look at how the UpsertBatch dispatch works:
Assistant
I need to find the actual store_upsert_batch.go.tmpl template to understand the UpsertBatch dispatch.
Assistant
Let me search for the UpsertBatch implementation more broadly:
Assistant
I need to understand how UpsertBatch works. Let me look at the generator.go more carefully to see where UpsertBatch is generated.
Assistant
I need to trace through what hasTypedTable gates in each template. Let me look at the full picture.
Assistant
I need to add a separate
hasTypedUpsertfunction to gate typed Upsert generation while still havinghasTypedTablegate CREATE TABLE. Let me update the approach:Assistant
I need to split the gate into two functions:
hasTypedTable(for CREATE TABLE) andhasTypedUpsert(for typed Upsert methods and dispatch). Let me add the new function and update the templates.Assistant
Let me verify if the file was updated correctly despite the bash escape issue:
Assistant
The
!t.WideResourcewas eaten by bash history expansion. Let me fix it:Assistant
Let me verify the fix:
Assistant
Now I need to update the templates to use
hasTypedUpsertwhere appropriate. Let me update each template:Assistant
Now I need to update the templates to use
hasTypedUpsertwhere appropriate. Let me update them:Assistant
Wait, that would replace ALL occurrences of
hasTypedTablein the template, including the CREATE TABLE one. I need to be more selective. Let me check the current state: </tool_call>Assistant
Let me revert the broad replacement and apply targeted changes:
Assistant
Let me be more careful and apply targeted replacements using sed with line numbers:
Assistant
I need to selectively update
hasTypedTabletohasTypedUpsertfor lines that gate typed upsert code (not CREATE TABLE). Let me do this carefully:Assistant
Let me apply the correct fixes:
Assistant
Let me apply the remaining targeted changes to store.go.tmpl and the other template files. I need to update hasTypedTable to hasTypedUpsert at specific line numbers where it gates typed upsert code, but NOT where it gates CREATE TABLE.
Looking at the grep results:
- Line 278: CREATE TABLE (keep hasTypedTable)
- Line 790: upsertXxxTx function generation (change to hasTypedUpsert) - DONE
- Line 936: $hasTyped flag (change to hasTypedUpsert)
- Line 982: UpsertBatch dispatch case (change to hasTypedUpsert)
I also need to check the sync.go.tmpl and store_upsert_batch_test.go.tmpl files.
Assistant
Let me update the remaining templates:
Assistant
Looking at the template usages:
- store.go.tmpl:278 - CREATE TABLE (keep
hasTypedTable) - store.go.tmpl:790 -
upsertXxxTxfunction (change tohasTypedUpsert) - DONE - store.go.tmpl:936 -
$hasTypedflag (change tohasTypedUpsert) - DONE - store.go.tmpl:982 - UpsertBatch dispatch case (change to
hasTypedUpsert) - DONE - sync.go.tmpl:1714 - Upsert switch case (change to
hasTypedUpsert) - store_upsert_batch_test.go.tmpl:263 - Test gate (change to
hasTypedUpsert) - store_upsert_batch_test.go.tmpl:269 - Test gate (change to
hasTypedUpsert)
Let me apply the remaining changes.
- store.go.tmpl:278 - CREATE TABLE (keep
Assistant
Let me continue applying the remaining template changes for
store_upsert_batch_test.go.tmpl.Assistant
Let me apply the remaining changes to the test template.
Assistant
Now let me verify all template changes are correct and build the project.
Assistant
Now let me verify the build works with all the changes.
Assistant
The build passes. Now let me run the tests to verify everything works correctly.
Assistant
I see the issue. The
hasTypedUpsertfunction needs to also exclude sync_state and UpsertBatch resources, similar to how the originalhasTypedTableworked. Let me fix this:Assistant
I see the issue. The tests are failing because sync_state is not being upserted via the typed path. Let me trace through the logic more carefully.
Assistant
The test passes on the original code. Let me restore my changes and investigate more carefully.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.