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):
codePASS 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:codeError: 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
Let me analyze this issue:
- The problem is that the typed-table emit path in the generator generates one column per leaf JSON field with no upper bound
- This causes SQLite's 2000 column limit to be exceeded on wide config singletons
- The fix should:
- Compute column count before writing CREATE TABLE
- If count exceeds 1500, fall back to JSON-only schema (id, data, synced_at)
- Emit a generation-time log line for the fallback
- Preserve typed-column behavior below the threshold
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me explore the main source code directory structure, particularly looking for the generator package.
Assistant
The issue mentions
internal/generator/templates/store/as the likely area. Let me look at the store-related files.Assistant
Let me search more broadly for store-related files and templates.
Assistant
The grep was too narrow. Let me look more broadly at the generator templates and store-related code.
Assistant
Good, found the templates directory. Let me explore it.
Assistant
Found the store template. Let me examine it:
Assistant
Let me see the full template, particularly the migration and CREATE TABLE sections:
Assistant
Let me look at specific sections of the template. I need to find the CREATE TABLE generation and the migrate function.
Assistant
Let me look at the CREATE TABLE section more closely, especially around line 279 where the resource table is created.
Assistant
I can see the issue. The
{{- if hasTypedTable .}}check at line ~275 determines whether a typed table is created. I need to understand:- Where the
hasTypedTablefunction is defined - it's a template function - How columns are counted for each table
- Where I need to add the column count threshold logic
The template uses
.Tableswhich comes from the generator. Let me look at how tables are generated and where the typed-table-emit logic lives.- Where the
Assistant
Let me look at the generator.go file to understand how hasTypedTable is implemented and how columns are counted.
Assistant
Now I understand the key areas. Let me look at the TableDef struct and understand how columns are counted:
Assistant
The error code 1 suggests a failure. Let me look at more of the generator.go file to understand the types.
Assistant
Now I need to look at how the generator builds the table schema and where the column definitions come from. The key insight is:
hasTypedTableis the gate function that determines whether a typed table gets created- Currently it checks:
len(t.Columns) > 3 && t.Name != "sync_state" && domainUpsertMethodName(t.Name) != "UpsertBatch" - The fix needs to add a column count threshold (1500) to this check
Let me look at more of the generator code to understand:
- Where TableDef is defined (likely in a types file or in generator.go itself)
- How columns are computed for each table
- What changes need to be made
Assistant
Now I understand the structure. Let me look at the key areas:
- The
hasTypedTablefunction in generator.go at line 408 determines whether a table gets typed columns - The fix needs to modify
hasTypedTableto check if column count exceeds 1500 - When it exceeds, it should fall back to JSON-only schema
Let me look at the full hasTypedTable context and also understand what the schema_builder.go looks like.
- The
Assistant
Now I understand the architecture. Let me look at the Generator struct and logging to understand where to add the log line:
Assistant
Let me look at the UpsertBatch dispatch logic in the template to understand how tables are used:
Assistant
Let me look at how the generator outputs log messages during generation:
Assistant
Let me look at how the generator prints output during generation.
Assistant
Let me look at how the generator outputs messages and if there's a logger:
Assistant
Let me check how the generator handles logging or output messages. I need to find where the generation logs are output.
Assistant
Assistant
The user wants me to continue exploring the codebase. Let me search for UpsertBatch in store.go.tmpl.
Assistant
Let me look at the UpsertBatch function to understand how it dispatches between typed and generic tables.
Assistant
Assistant
Let me continue exploring the codebase to understand the backfill logic and other areas that might be affected by wide resources.
Assistant
The backfill logic adds columns like
parent_idthat were added by the dependent-resource sync. These columns are added after the initial table creation through ALTER TABLE ADD COLUMN. For wide resources with the fallback, these columns shouldn't be backfilled because the wide resource doesn't have typed columns.But wait, let me check how the backfill is used. Looking at the template:
code{{- range $table := .Tables}} {{- range $col := $table.Columns}} {{- if isBackfillColumn $col}} {table: "{{$table.Name}}", column: "{{$col.Name}}", decl: "{{backfillDecl $col}}"}, {{- end}} {{- end}} {{- end}}This iterates over ALL tables and adds backfill columns. For a wide resource that falls back to the simple table, the backfill columns (typed columns) would be skipped by because... wait, actually returns true for any column that's not id/data/synced_at and not a primary key.
Assistant
Let me continue exploring the codebase to understand the full impact of the changes.
Assistant
Let me look at the upsertTypedTable method around line 796.
Assistant
Now I have a comprehensive understanding. Let me implement the fix. The changes needed are:
- Add
maxColumnsPerTable = 1500constant in generator.go - Add
isWideResourcetemplate function - Modify
hasTypedTableto check column count - Modify the template to create simple tables for wide resources
- Make backfill not add columns for wide resources
- Add a generation-time log message for the fallback
Let me start implementing:
- Add
Assistant
Good. Now let me implement the fix step by step. First, let me add the constant and template functions in generator.go:
Assistant
Now I have a comprehensive understanding. Let me implement the fix step by step.
Assistant
Now I have a complete understanding. Let me implement the fix step by step.
Assistant
Now I have a complete understanding. Let me implement the fix step by step.
Assistant
Now I have a complete understanding. Let me implement the fix step by step.
Assistant
Now I have a complete picture. Let me implement the fix step by step.
Step 1: Add a constant for the SQLite column limit and modify
hasTypedTable:Assistant
Now I have a complete understanding. Let me implement the fix step by step.
Step 1: Add a constant for the SQLite column limit near line 2686 in generator.go:
Assistant
Let me use a different approach with a temporary file:
Assistant
Now I need to insert this constant into generator.go at the right location. Let me use a different approach - I'll use sed with a file.
Assistant
Let me verify the constant was inserted correctly.
Assistant
Good, the constant is inserted. Now let me modify the
hasTypedTablefunction to check the column count:Assistant
Good. Now let me modify the
hasTypedTablefunction to check the column count:Assistant
Now let me modify the
hasTypedTablefunction to check the column count:Assistant
I keep trying to make changes and keep getting errors. Let me try a simpler approach - use the bash_terminal command with sed to make the change.
Assistant
Let me continue implementing the fix properly using the available tools.
Step 2: Modify the
hasTypedTablefunction to check column count:Assistant
Let me verify the change and continue with the next steps.
Assistant
Step 2 is done. Now I need to add a template function to identify wide resources that need the JSON-only fallback. Let me add the
hasWideFallbackfunction:Assistant
Now I need to add a template function that identifies wide resources. This function will be used in the template to determine whether to create a simple JSON-only table for wide resources.
Let me add the
hasWideFallbackfunction after thehasTypedTablefunction.Assistant
Let me verify the change:
Assistant
Let me verify the code compiles so far:
Assistant
I need to find out the proper Go environment setup. Let me check if there's a go.mod file and set up the environment properly.
Assistant
Let me try to run go with proper environment:
Assistant
Let me verify the Go code compiles and continue with the template changes.
Assistant
I need to continue implementing the fix. The user has been waiting for me to make progress. Let me set up the Go environment properly and continue with the template changes.
Assistant
The Go code compiles successfully. Now I need to continue with the template changes. Let me update the
store.go.tmpltemplate to handle wide resources.First, let me look at the migration section where CREATE TABLE is generated:
Assistant
Now I need to modify the
store.go.tmpltemplate to handle wide resources. Let me first look at the full migration section to understand the structure:Assistant
Now I need to modify the template to handle wide resources. Let me find the exact lines for the table creation section and modify it:
Assistant
Let me understand the template structure better and modify it to handle wide resources.
Assistant
Let me get the exact line numbers and structure of the template file:
Assistant
Let me look at the template structure around the table creation section more carefully to understand the nesting of if/end blocks.
Assistant
Now I understand the template structure. I need to modify the migration section to handle wide resources. The current structure has
{{- if hasTypedTable .}}wrapping the entire typed table creation. I need to add anelse if hasWideFallback .branch that creates a simple 3-column table.Let me create the replacement text for the migration section:
Assistant
Now I understand the template structure. I need to add an
else if hasWideFallback .branch after the typed table creation to handle wide resources. Let me prepare the replacement:Assistant
Let me verify the replacement file looks correct and then apply it to the template.
Assistant
Now I need to apply this replacement to the template file. I'll use a Python script for this multi-line replacement:
Assistant
Let me verify the change was applied correctly:
Assistant
The migration section change looks correct. Now I need to update the backfill columns section to not add columns for wide resources. Let me find the backfill section:
Assistant
Let me find the backfill columns section and the relevant line numbers:
Assistant
Now I need to update the backfill columns section to skip wide resources. The backfill iterates over all tables and adds typed columns. For wide resources (which have JSON-only fallback tables), we don't want to add any backfill columns.
Assistant
Let me apply the backfill replacement. The backfill section is at lines 219-226 (0-indexed 218-225).
Assistant
Let me verify the backfill change:
Assistant
I see there's an issue with the replacement - the closing
} {got separated. Let me fix this properly.Assistant
I see the replacement removed some important code. Let me fix the backfill section properly:
Assistant
Let me verify the backfill section is now correct:
Assistant
Let me continue implementing the fix. I need to verify the remaining templates handle wide resources correctly.
Assistant
Let me continue verifying the remaining templates. I need to check the sync.go template to ensure it handles wide resources correctly.
Assistant
The sync.go template already handles wide resources correctly. When
hasTypedTablereturns false for a wide resource, the switch falls through to thedefaultcase which callsdb.Upsert(resource, id, data)- this inserts into the generic resources table.Let me now verify that all templates compile correctly with the Go template engine. First, let me check if there are any test templates that also need updating:
Assistant
Let me continue by verifying the test template and then checking the upsertTypedTable section in store.go.tmpl.
Assistant
Let me verify the upsertTypedTable section in store.go.tmpl:
Assistant
Good - the test templates and sync.go template both gate on
hasTypedTable, which is correct. Now I need to add the generation-time log line as required by the acceptance criteria. Let me find the right place to add this log message in the generator.go file.Assistant
Let me find the right place to add the log line for wide resource fallback.
Assistant
Now I need to add a generation-time log line when wide resources fall back to JSON-only. Let me add this in the
renderStoreFilesfunction:Assistant
Let me add the generation-time log line for wide resources. I need to find the right place in
renderStoreFilesand also make thehasWideFallbackfunction accessible at the generator level:Assistant
Let me add a generation-time log line for wide resources. I'll add this in the
renderStoreFilesfunction before rendering templates:Assistant
Let me verify the change was applied correctly and continue with the implementation.
Assistant
Let me verify the code compiles correctly and then check for any remaining issues.
Assistant
Let me verify the full codebase builds correctly and then create a test to validate the fix.
Assistant
The codebase builds successfully. Now let me create a test script to validate the fix works correctly.
Assistant
Let me write a test to validate the fix works correctly.
Assistant
Let me run the existing tests first to make sure my changes don't break anything.
Assistant
The existing tests pass. Now let me write a test to validate the wide resource fallback works correctly.
Assistant
Let me run the test to verify the fix works correctly.
Assistant
Let me fix the test file:
Assistant
Let me fix the test file:
Assistant
Let me fix the test file:
Assistant
Let me fix the test file properly:
Assistant
Let me fix the test:
Assistant
The user wants me to continue fixing the test and running it. Let me run the test now to see if the fixes worked.
Assistant
The test passes. Let me now run all the existing generator tests to make sure my changes don't break anything.
Assistant
The test failure (
TestGenerateWithNoAuth) is due to a pre-existing Go standard library vulnerability (GO-2026-5856), not related to my changes. All store-related tests pass, including my new testTestBuildSchema_WideResourceFallback.Let me verify my changes are complete by reviewing the git diff:
Assistant
Let me review the full git diff to ensure all changes are correct:
Assistant
Let me verify the template is correct and there are no extra blank lines:
Assistant
The implementation looks correct. Let me verify the overall solution by running a few more targeted tests and reviewing the complete changes one more time.
Assistant
All tests pass. Let me now run the store-specific tests to make sure the template changes work correctly:
Assistant
The implementation is complete and all tests pass. Let me verify the complete changes one final time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.