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
I'll start by exploring the repository structure to understand the codebase and locate the relevant files.
Assistant
Let me explore the repository structure more systematically.
Assistant
Now let me look at the templates directory, specifically the store templates:
Assistant
Now let me look at the store.go.tmpl file, which is the key template file:
Assistant
The open command needs to be formatted differently. Let me try without the file argument in the JSON.
Assistant
Now let me continue reading the store template to understand the table creation and the
hasTypedTablefunction:Assistant
Now I need to understand the template functions and the data structure being passed to the template. Let me look at the generator.go file where these functions are defined:
Assistant
Let me look at the
hasTypedTablefunction and theTableDefstruct:Assistant
Now I need to find the
TableDefstruct and understand the column counting logic:Assistant
Now I have a good understanding of the codebase. Let me look at how the
hasTypedTablefunction works and what I need to change:- The issue is that when a resource has too many leaf fields (>1500 columns), the generated
CREATE TABLEexceeds SQLite's default column limit of 2000. - The fix should add a column-count check in the
hasTypedTablefunction — if columns exceed a threshold (1500), fall back to JSON-only schema. - The
hasTypedTablefunction currently only checkslen(t.Columns) > 3— we need to also checklen(t.Columns) <= 1500.
Let me also look at what else the
hasTypedTablegate controls:- The issue is that when a resource has too many leaf fields (>1500 columns), the generated
Assistant
Let me look at each usage of
hasTypedTablein the store template:Assistant
Now let me see the rest of this section:
Assistant
Now I understand the code structure. Let me look at the
hasTypedTablefunction ingenerator.gomore closely and see the constant for the column limit:Assistant
No existing constant. Now let me look at the area around
hasTypedTableto understand where to add the constant and modify the function:Assistant
Now I need to understand what also needs to change. The issue mentions that when the column count exceeds the threshold, we should:
- 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
The simplest approach is to modify the
hasTypedTablefunction to returnfalsewhen columns exceed the limit. This will cause:- No
CREATE TABLEwith typed columns to be emitted (line 278) - No typed upsert functions to be generated (line 790)
- No typed case in
UpsertBatchdispatch (line 982)
But we still need to ensure the table gets created with the JSON-only fallback schema. Let me check if there's already a fallback table creation:
- Fall back to JSON-only schema:
Assistant
I see. When
hasTypedTablereturns false, the resource still gets stored in the genericresourcestable (viaupsertGenericResourceTx). So the "JSON-only fallback" is essentially theresourcestable that already exists. But the issue says "Resources exceeding the cap must retain their per-resource table but fall back to a JSON-only schema containingid,data, andsynced_at."This means we need a separate per-resource table with only 3 columns for wide resources. Let me think about this more carefully:
Looking at the evaluation contract: "Resources exceeding the cap must retain their per-resource table but fall back to a JSON-only schema containing
id,data, andsynced_at"So we need:
- For wide resources (>1500 cols): Create a table with only
id,data,synced_at— no typed columns, no indexes, no FTS, no typed upsert - For narrow resources (<1500 cols): Keep existing behavior
- For wide resources (>1500 cols): Create a table with only
Assistant
Assistant
Good, I can see that
fmt.Fprintf(os.Stderr, ...)is used for warnings during generation. Now let me look at where the schema is built and where I should add the log line:Assistant
Now I have a good understanding. Let me plan my changes:
Plan:
-
In
schema_builder.go:- Add constant
typedTableColumnLimit = 1500
- Add constant
-
In
generator.go:- Modify
hasTypedTablefunction to check column count - Add a new template function
shouldCreateTablethat returns true for all tables that need a CREATE TABLE (typed or JSON-only) - Actually, I think the simpler approach is: modify
hasTypedTableto return false for wide tables, and add a separate functionhasJSONFallbackTablefor wide tables
- Modify
-
In
store.go.tmpl:- Update the CREATE TABLE section to handle both typed and JSON-only fallback
- For JSON-only fallback, emit only the 3 base columns and no indexes/FTS/upsert
- Add a generation-time log line in the generator code
Let me start implementing:
-
Assistant
Now I'll implement the changes. Let me start with adding the constant to
schema_builder.go:Assistant
Good, the edit was applied correctly. Now let me verify the file looks right:
Assistant
I see the edit command didn't work as expected. Let me use sed to fix the file properly:
Assistant
bash_tag called
Assistant
Continuing my implementation:
- I need to add the
typedTableColumnLimit = 1500constant to schema_builder.go - Modify the
hasTypedTablefunction in generator.go to check column count - Add a
hasJSONFallbackTablefunction for wide tables - Update store.go.tmpl to handle JSON-only fallback tables
- Add generation-time log line
Let me try the edit command with proper syntax. ) => bash-
- I need to add the
Assistant
Let me fix the schema_builder.go file by adding the constant properly using the edit command with the right syntax.
Assistant
The edit command syntax isn't working as expected. Let me try using sed to add the constant to the file instead.
Assistant
Let me try using sed to insert the constant after the closing parenthesis of the import block and before the
type TableDef struct {line.Assistant
Good, the sed command worked. Let me verify the changes were applied correctly.
Assistant
Now I need to modify the
hasTypedTablefunction in generator.go to check column count and add ahasJSONFallbackTablefunction. Let me find the exact location:Assistant
I need to modify the
hasTypedTablefunction in generator.go to add the column limit check. The function currently returns true whenlen(t.Columns) > 3. I need to also check thatlen(t.Columns) <= typedTableColumnLimit(1500).I also need to add a
hasJSONFallbackTablefunction that returns true for wide tables (those exceeding the column limit but still having more than 3 columns).Let me use sed to make these changes.
Assistant
I'll continue implementing the fix. Let me modify the
hasTypedTablefunction in generator.go to add the column limit check.Assistant
Let me continue implementing the fix by modifying the
hasTypedTablefunction in generator.go to add the column limit check.Assistant
Now I'll modify the
hasTypedTablefunction to add the column limit check using sed.Assistant
The user is asking me to continue. Let me check the current state of the repository and verify my changes so far, then continue implementing the fix.
I need to:
- Verify the schema_builder.go change (constant added)
- Verify the generator.go change (hasTypedTable modified)
- Add hasJSONFallbackTable template function
- Update store.go.tmpl to handle JSON-only fallback tables
- Add generation-time log line
Let me check the current state of the files and continue.
Assistant
Let me verify the changes I've made so far and continue implementing the fix.
- I added
typedTableColumnLimit = 1500constant to schema_builder.go ✓ - I modified
hasTypedTablein generator.go to check column count ✓
Now I need to: 3. Add
hasJSONFallbackTabletemplate function - for tables that exceed the column limit but should still get a per-resource table with just 3 columns 4. Update store.go.tmpl to handle JSON-only fallback tables - create the table with just id/data/synced_at 5. Add generation-time log line - log when a table falls back to JSON-onlyLet me continue with the implementation. }
- I added
Assistant
The user wants me to continue implementing the fix. Let me check what I've done so far and what remains:
- ✅ Added
typedTableColumnLimit = 1500constant to schema_builder.go - ✅ Modified
hasTypedTablein generator.go to checklen(t.Columns) <= typedTableColumnLimit
Now I need to: 3. Add
hasJSONFallbackTabletemplate function for wide tables that still need a per-resource table 4. Update store.go.tmpl to create JSON-only fallback tables (just id/data/synced_at columns) 5. Add generation-time log lineLet me continue implementing. First, I need to add the
hasJSONFallbackTabletemplate function to generator.go, then update the store template.- ✅ Added
Assistant
The user is asking me to continue implementing the fix. Let me think about what I've done and what remains:
- ✅ Added
typedTableColumnLimit = 1500constant to schema_builder.go - ✅ Modified
hasTypedTablein generator.go to checklen(t.Columns) <= typedTableColumnLimit
Now I need to: 3. Add
hasJSONFallbackTabletemplate function for wide tables that still need a per-resource table 4. Update store.go.tmpl to create JSON-only fallback tables (just id/data/synced_at columns) 5. Add generation-time log lineThe
hasJSONFallbackTablefunction should return true for tables that:- Have more than 3 columns (they had typed columns before)
- Exceed the column limit (> 1500 columns)
- Are not sync_state
- Are not UpsertBatch
This way, wide resources will still get a per-resource table, but with only the 3 base columns.
Let me add this function after the hasTypedTable function and then update the template.
- ✅ Added
Assistant
Let me think about what I need to do:
- ✅ Added
typedTableColumnLimit = 1500constant to schema_builder.go - ✅ Modified
hasTypedTablein generator.go to check column count
Now I need to: 3. Add
hasJSONFallbackTabletemplate function for wide tables 4. Update store.go.tmpl to handle JSON-only fallback tables 5. Add generation-time log lineThe
hasJSONFallbackTableshould return true when a table would have been typed but exceeds the column limit. This means:- len(t.Columns) > typedTableColumnLimit (exceeds limit)
- t.Name != "sync_state" (not sync_state)
- domainUpsertMethodName(t.Name) != "UpsertBatch" (not batch method)
This ensures wide resources get a per-resource table with just the 3 base columns.
Let me add this function after the hasTypedTable function definition.
- ✅ Added
Assistant
Let me check if the sed command worked correctly and verify the current state of the file.
Assistant
The
hasJSONFallbackTablefunction has been added successfully. Now I need to:- Update
store.go.tmplto create JSON-only fallback tables for wide resources - Add generation-time log line for fallback resources
Let me look at the CREATE TABLE section in store.go.tmpl to understand what needs to be modified:
- Update
Assistant
I need to update the store.go.tmpl template to:
- Create JSON-only fallback tables for wide resources (hasJSONFallbackTable)
- The current template only creates tables when
hasTypedTableis true - I need to add an
else if hasJSONFallbackTablebranch that creates the table with just the 3 base columns
The current structure is:
{{- if hasTypedTable .}} `CREATE TABLE IF NOT EXISTS {{safeName .Name}} ( ... all columns ... )`, ... indexes, FTS ... {{- end}}I need to change it to:
{{- if hasTypedTable .}} `CREATE TABLE IF NOT EXISTS {{safeName .Name}} ( ... all columns ... )`, ... indexes, FTS ... {{- else if hasJSONFallbackTable .}} `CREATE TABLE IF NOT EXISTS {{safeName .Name}} ( id TEXT PRIMARY KEY, data JSON NOT NULL, synced_at DATETIME DEFAULT CURRENT_TIMESTAMP )`, {{- end}}Let me use sed to make this change. The key is to find the that closes the block for CREATE TABLE and add an else-if branch before it.
Assistant
Let me find the exact line numbers for the hasTypedTable block closing:
Assistant
Let me examine the exact template structure to understand where to insert the JSON fallback:
Assistant
I need to add the JSON fallback table creation in the template. Let me insert the else-if block for wide resources after line 321 (the FTS5 end) and before line 322 (the hasTypedTable end).
Assistant
Let me count the lines more carefully. Looking at the output:
- Line 320:
{{- end}}(closes the FTS5 else block) - Line 321:
{{- end}}(closes the{{- if .FTS5}}block) - Line 322:
{{- end}}(closes the{{- if hasTypedTable .}}block) - Line 323:
{{- end}}(closes the{{- range .Tables}}block)
I need to insert the JSON fallback between line 321 and line 322. Let me do that:
- Line 320:
Assistant
Let me verify the template change was applied correctly and continue with the implementation.
Assistant
The template change looks correct. Now I need to add the generation-time log line. Let me find where the store files are rendered to add the logging:
Assistant
I need to add the generation-time log line for wide resources that fall back to JSON-only. I should add this in the
renderStoreFilesfunction, before rendering the template. Let me find a good place to add the log loop.Assistant
I need to add a loop that logs wide resources before rendering the store template. I'll add the log loop after creating the storeData struct but before rendering the template. The log should use the format specified in the issue:
store-fallback: control (3747 cols) → JSON-onlyLet me insert the logging code after line 2503 (after the Tables assignment) and before line 2504 (the renderTemplate call).
Assistant
Let me verify the changes I made and check if the code compiles correctly.
Assistant
Good, the logging code is in place. Now I need to verify that the code compiles correctly. Let me run
go buildto check for any compilation errors.Assistant
Let me set up the Go environment properly:
Assistant
I need to set up the Go environment properly and then try to build.
Assistant
The build succeeded. Now let me verify all the changes are in place and run the existing tests to make sure nothing is broken.
Assistant
The user is asking me to continue. Let me check what I've done so far and verify the changes are complete. I need to:
- Verify all my changes are in place
- Run the existing tests to make sure nothing is broken
- Make sure the implementation meets all the acceptance criteria
Let me review what I've done:
- ✅ Added
typedTableColumnLimit = 1500constant to schema_builder.go - ✅ Modified
hasTypedTablein generator.go to checklen(t.Columns) <= typedTableColumnLimit - ✅ Added
hasJSONFallbackTabletemplate function for wide tables - ✅ Updated store.go.tmpl to create JSON-only fallback tables
- ✅ Added generation-time log line
Now I need to run the tests to make sure nothing is broken.
Assistant
Let me run the actual tests that exist for the schema builder and store generation to make sure my changes don't break anything.
Assistant
The schema builder tests pass. Now let me run more comprehensive tests to make sure the generator tests also pass, especially the ones related to store generation.
Assistant
The tests are running and passing. Let me wait for the full output to confirm all tests pass, then I should also look at the backfillColumns section in the template to make sure wide tables don't try to backfill columns that don't exist.
Assistant
All 21 store-related tests pass. Let me now run the full generator test suite to make sure nothing else is broken.
Assistant
The test failure is unrelated to my changes - it's a
govulncheckvulnerability scan failure about Go standard library TLS issues, which is a pre-existing environment issue. Let me run the tests that are specifically relevant to my changes:Assistant
All relevant tests pass. Now let me check the backfillColumns section to make sure wide tables don't cause issues:
Assistant
I notice a potential issue with the
backfillColumnssection. For wide tables (JSON-only fallback), the schema still has all the columns int.Columns, but the actual SQLite table only has 3 columns. The backfillColumns would try to add all those extra columns at migration time, which defeats the purpose of the JSON-only fallback.Let me fix this by gating the backfillColumns to only include typed tables:
Assistant
Actually, I think that sed command was too complex and may have broken the template. Let me check the current state of the file and fix it properly if needed.
Assistant
Let me fix the broken template by reverting and applying the change correctly:
Assistant
I need to re-apply all my changes to the store.go.tmpl template properly. Let me think about what changes I need:
- Add the
hasJSONFallbackTableelse-if branch in the CREATE TABLE section (around line 321) - Add the
hasTypedTableguard to the backfillColumns section so wide tables don't try to backfill columns that don't exist
Let me do this carefully with proper sed commands.
- Add the
Assistant
I need to apply the changes to store.go.tmpl. Let me first add the hasTypedTable guard around the backfillColumns loop, then add the JSON fallback table creation.
Assistant
I need to continue fixing the backfillColumns section. I added the
{{- if hasTypedTable $table}}line, but I also need to add the closing{{- end}}after the inner loops complete.Assistant
I see the issue - I need to add a closing
{{- end}}for theif hasTypedTableguard. Let me add it after line 225 (end of range $col):Assistant
Let me check the current state of the template to see if my changes are correct:
Assistant
Good, the backfillColumns section looks correct now. Now I need to add the JSON fallback table creation in the CREATE TABLE section. Let me find the right place:
Assistant
I need to find the exact line where I should insert the JSON fallback table creation. Let me check the line numbers more precisely:
Assistant
Looking at the template structure:
- Line 280:
{{- if hasTypedTable .}} - Line 319:
{{- end}}closes{{- else}}(for FTS5 without triggers) - Line 322:
{{- end}}closes{{- if .FTS5}} - Line 323:
{{- end}}closes the range for Indexes (wait, that doesn't make sense)
Let me look at the structure more carefully to understand where each
{{- end}}belongs:- Line 280:
Assistant
Now I understand the structure better:
Line 280:
{{- if hasTypedTable .}}Line 281-286: CREATE TABLE with columns Line 287-289: CREATE INDEX Line 290-319: FTS5 (with or without triggers) Line 290:{{- if .FTS5}}Line 291-314: FTS5Triggers case Line 315-318: else case Line 319:{{- end}}(closes .FTS5 else) Line 320:{{- end}}(closes .FTS5 if) Line 321:{{- end}}(closes hasTypedTable) Line 322:{{- end}}(closes range .Tables)I need to insert the
{{- else if hasJSONFallbackTable .}}block between line 320 (end of .FTS5) and line 321 (end of hasTypedTable).Assistant
Let me verify the change was applied correctly:
Assistant
I see there's an issue - the template structure is broken. The
{{- else if hasJSONFallbackTable .}}ended up inside the FTS5 else block, and there's a duplicate),` at line 328. Let me revert and fix this properly.Assistant
I need to carefully apply the template changes. Let me look at the exact structure of the template and make precise edits.
The structure is:
{{- range .Tables}} {{- if hasTypedTable .}} CREATE TABLE with typed columns... CREATE INDEX... {{- if .FTS5}} FTS5 stuff... {{- end}} {{- end}} {{- end}}I need to change it to:
{{- range .Tables}} {{- if hasTypedTable .}} CREATE TABLE with typed columns... CREATE INDEX... {{- if .FTS5}} FTS5 stuff... {{- end}} {{- else if hasJSONFallbackTable .}} CREATE TABLE with JSON-only columns... {{- end}} {{- end}}Let me count the exact line numbers again and make the changes carefully.
Assistant
I see the structure now. Let me look at lines 319-322:
- Line 319:
),` - end of FTS5 standalone else block - Line 320:
{{- end}}- closes{{- else}}(FTS5 without triggers) - Line 321:
{{- end}}- closes{{- if .FTS5}} - Line 322:
{{- end}}- closes{{- if hasTypedTable .}} - Line 323:
{{- end}}- closes{{- range .Tables}}
I need to insert the else-if block before line 322 (the end of hasTypedTable). Let me insert it after line 321 (end of .FTS5):
- Line 319:
Assistant
Let me use a Python script to make the template change precisely. This will be more reliable than sed for multi-line insertions.
Assistant
Let me verify the template changes are correct:
Assistant
The template change looks correct. Now I need to also add the
hasTypedTableguard to the backfillColumns section to prevent wide tables from trying to backfill columns that don't exist. Let me do that:Assistant
The user is asking me to continue. Let me verify the backfill section change and then test that everything works.
Assistant
The backfill section looks correct now. Let me verify the build still works and run the tests to make sure everything passes.
Assistant
The build succeeded. Now let me run the tests to make sure everything still works correctly.
Assistant
I see the issue - my
hasTypedTableguard in the backfillColumns section is preventing thesync_statetable from being processed becausehasTypedTableexplicitly excludessync_state. I need to fix this by also includingsync_statein the backfill guard.Assistant
I've updated the backfill guard to include
sync_state. Let me verify the change and run the tests again to confirm the fix.Assistant
The backfill guard looks correct now. Let me run the tests again to verify the fix works.
Assistant
All 20 store tests pass. Let me verify all my changes are complete by reviewing the full diff.
Assistant
The diff looks correct. Let me now summarize all the changes I've made and verify they meet the acceptance criteria:
Changes made:
-
internal/generator/schema_builder.go:- Added
typedTableColumnLimit = 1500constant with documentation
- Added
-
internal/generator/generator.go:- Modified
hasTypedTablefunction to include column limit check:len(t.Columns) <= typedTableColumnLimit - Added
hasJSONFallbackTablefunction for wide resources that exceed the column limit - Added generation-time log line:
store-fallback: <name> (<count> cols) → JSON-only
- Modified
-
internal/generator/templates/store.go.tmpl:- Added
{{- if or (hasTypedTable $table) (eq $table.Name "sync_state")}}guard to backfillColumns to prevent wide tables from backfilling non-existent columns - Added
{{- else if hasJSONFallbackTable .}}branch in CREATE TABLE section for JSON-only fallback tables with just id/data/synced_at columns
- Added
Let me verify the acceptance criteria are met by running a broader test and checking the behavior:
-
Assistant
All tests pass. Let me verify the build is clean one more time and then submit the changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.