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
I'll start by exploring the repository structure to understand the codebase and find the relevant files.
Assistant
Let me explore the repository structure more systematically, focusing on the generator/templates area mentioned in the issue.
Assistant
I found the
store.go.tmpltemplate file. Let me also look for theschema_builder.gofile mentioned and understand the store generation code.Assistant
The user seems to have encountered an error with the open command. Let me try a different approach - I'll use bash to read the file content.
Assistant
Now let me see the migration section that creates the tables, which is the key part for this issue:
Assistant
Let me see the migration section more carefully:
Assistant
Now I can see the template structure. The key part is the
{{- range .Tables}}section that creates typed tables. I need to look at:- How
Tablestructs are built (the schema builder) - Where the column count is computed
- The template functions used
Let me look at the
schema_builder.gofile:- How
Assistant
Now let me look at the
generator.goto understand how tables are generated and passed to the template, and to find where the template functions are defined:Assistant
Now I understand the
hasTypedTablefunction. It checks iflen(t.Columns) > 3(more than just id, data, synced_at). Now I need to understand what happens when a table exceeds the column limit.Let me look at how the sync.go template dispatches upserts to understand the full picture:
Assistant
Now I understand the architecture. Let me look at how the store template generates typed upsert code and indexes:
Assistant
Let me see the Upsert method and UpsertBatch to understand the full flow:
Assistant
Let me see more of the UpsertBatch code and the typed upsert transaction method:
Assistant
Now let me look at the upsertTx methods to understand the typed upsert pattern:
Assistant
Now I have a good understanding of the codebase. Let me summarize:
schema_builder.gobuildsTableDefstructs with columns for each resourcestore.go.tmpluses these to generateCREATE TABLEstatements- The
hasTypedTablefunction determines if a resource gets typed columns (> 3 cols, not sync_state, not renamed by framework collision) - When too many columns are generated (> 2000), SQLite fails
The fix needs to:
- In
schema_builder.go: Cap the number of typed columns at 1500. If exceeding, fall back to only base columns (id, data, synced_at) - The
hasTypedTablefunction already handles tables with only 3 columns (returns false) - Add a log line for the fallback
Let me also look at the
schema_builder_test.goto understand the test patterns:Assistant
Now let me look at how the generation log messages are emitted. Let me search for "store-fallback" or similar logging patterns:
Assistant
Let me look for how warnings or notes are emitted during generation. Let me search for fmt.Fprintf or log output in generator.go:
Assistant
Good, I can see that warnings are emitted via
fmt.Fprintf(os.Stderr, ...)during generation. Now let me understand the full build process. Let me check whereBuildSchemais called:Assistant
Now I understand the flow. Let me also check what happens with FTS5 when tables have many columns. Let me also look at where the TableDef is used in templates to find all the places gated by
hasTypedTable:Assistant
Now I understand the full picture. Here's my plan:
-
In
schema_builder.go: Add a constantMaxTypedColumns = 1500and modifyBuildSchemaso that when a table would have more than 1500 columns, it falls back to just the base columns (id, data, synced_at). ThehasTypedTablefunction already returnsfalsefor tables with ≤ 3 columns, so this effectively makes wide tables fall back to JSON-only. -
Add a log line in
schemaWithDependentParents(where dependent parent columns are added) and inBuildSchemato emit the fallback warning. -
Also need to handle the case where adding a
parent_idcolumn would push a table over the limit.
Let me also check if there's any FTS5 or index logic that needs to be disabled for wide tables:
-
Assistant
Now I have a complete understanding. Let me implement the fix:
- In
schema_builder.go, add the column cap constant and modify theBuildSchemafunction to cap columns at 1500 - When capping, also clear indexes and FTS5 fields since we're falling back to JSON-only
- Add a log line for the fallback
Let me also verify how
schemaWithDependentParentsadds parent_id columns - those count toward the column total:- In
Assistant
Now let me implement the fix. The approach is:
- Define a
MaxTypedColumnsconstant (1500) inschema_builder.go - In
BuildSchema, after building all columns for a table, check if they exceed the cap - If they do, truncate to base columns only (id, data, synced_at), clear indexes and FTS5, and log a warning
- The
hasTypedTablefunction in the template already checkslen(t.Columns) > 3, so a table with only 3 base columns will fall through to JSON-only
Let me also check where the
schemaWithDependentParentsadds the parent_id to make sure we handle the edge case where adding parent_id would push us over:- Define a
Assistant
Command errored with exit code 1.
Assistant
Now let me implement the fix. I'll add the column cap logic to
schema_builder.go:Assistant
Now I'll implement the fix. First, add the necessary imports and constant, then modify
BuildSchemato enforce the column cap:Assistant
Now I also need to handle the case where the
schemaWithDependentParentsfunction adds aparent_idcolumn. If a table is already near the limit (e.g., 1499 columns) and adding parent_id pushes it to 1500+, we need to handle that ingenerator.go. Let me also add the column cap check there:Assistant
I need to add a post-processing step in
schemaWithDependentParentsto handle the case where adding aparent_idcolumn would push a table over the limit. Let me updategenerator.go:Assistant
Now let me verify the changes were applied correctly:
Assistant
Continuing my work on the fix. Let me verify the column cap check was properly inserted in schema_builder.go and also check the generator.go changes.
Assistant
Good, the schema_builder.go change looks correct. Now let me verify the generator.go changes:
Assistant
I notice the
\nin thefmt.Fprintfcall is being rendered as a literal newline instead of the escape sequence\n. Let me fix that:Assistant
The
\nwas rendered as a literal newline. I need to fix both files to use\nproperly:Assistant
Let me verify the fix:
Assistant
Wait, the newline format is wrong now. Let me look at the full line:
Assistant
The line got split across multiple lines. Let me fix this properly:
Assistant
I need to fix the split line. Let me use sed to join them:
Assistant
Now let me verify the fix is correct:
Assistant
The schema_builder.go fix looks correct. Let me also verify the generator.go fix:
Assistant
The generator.go file still has the issue. Let me check more context:
Assistant
The issue is that the Python replacement didn't match because the file has a literal newline instead of
\n. Let me fix it directly:Assistant
Continuing my work. Let me verify the generator.go fix was applied correctly.
Assistant
Both files are now fixed. Let me verify the code compiles:
Assistant
Assistant
I need to properly format the bash_terminal command.
Assistant
The code compiles. Now let me run the existing tests to make sure I haven't broken anything:
Assistant
All BuildSchema tests pass. Now let me run the schema_builder tests specifically:
Assistant
Now let me run a broader set of generator tests to make sure nothing is broken:
Assistant
The only failing test (
TestGenerateWithNoAuth) is due to agovulncheckvulnerability in the Go standard library (GO-2026-5856), which is unrelated to my changes. Let me verify my changes work correctly by writing a temporary test:Assistant
I need to create the test file inside the module to access internal packages. Let me create it properly:
Assistant
Let me run the new tests to verify my fix works correctly.
Assistant
The test is failing because
spec.Responseis not the correct type. Let me check the spec package to see the correct type name.Assistant
I see - the Response field is of type
ResponseDef. Let me fix the test file:Assistant
mean Continuing my work. Let me verify the fix was applied and run the tests.
Assistant
The core wide-column tests all pass. The "normal resource" test is failing because my test spec isn't set up correctly to trigger FTS5/indexes (needs the right field names and endpoint count for gravity). Let me fix the test spec:
Assistant
Continuing my work. Let me run the updated tests to verify they pass.
Assistant
All 4 new tests pass. Now let me remove the temporary test file since the instructions say I shouldn't add new tests:
Assistant
All 4 tests pass. I've removed the temporary test file. Let me now run the existing test suite to make sure my changes don't break anything:
Assistant
Let me continue by running a broader set of tests and then reviewing my complete changes before submitting.
Assistant
The only failing test is
TestGenerateWithNoAuthwhich fails due to agovulncheckissue with Go standard library vulnerability (GO-2026-5856 in crypto/tls). This is a pre-existing environment issue, not related to my changes.Let me verify that my changes don't affect any tests other than this pre-existing failure:
Assistant
Let me fix the bash escaping issue with the exclamation mark
Assistant
Let me review the changes I made and verify they're complete before submitting. I should check the git diff to see exactly what was changed.
Assistant
Let me verify the changes are complete by checking the final state of the modified files:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.