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 mentioned in the issue description. Let's first check what's in the repository.
- Assistant
Let me explore the repository structure more thoroughly to understand the codebase.
- Assistant
Let me look for the internal/generator/templates/store/ directory as mentioned in the issue.
- Assistant
Great! Let me examine the store.go.tmpl file as it's likely where the CREATE TABLE logic is implemented.
- Assistant
Let me scroll down to see more of the store.go.tmpl file, particularly looking for the CREATE TABLE logic. Let's search for CREATE TABLE or column-related code.
- Assistant
Let me look at the section around line 279 where the resource table is created. Let me open the file at that line.
- Assistant
Let me continue scrolling to see the rest of the file, particularly looking for where the table columns are defined. I need to find where the
.Tablesdata is populated and how columns are determined. Let me search for where the table structures are built. - Assistant
Now I need to find where the Table structure is defined. It's likely in the generator code. Let me search for the Go code that generates the template data. Let me look for the generator code that builds the Tables data.
- Assistant
Let me check that file.
- Assistant
Let me continue reading the file to see where the gravity threshold is defined and how columns are added. Let me also search for where the column limit might be applied.
- Assistant
Let me continue reading to see the
computeDataGravityfunction andcollectResponseFields. I also need to check where the column count check should be added. Let me look at the rest of the file. - Assistant
Let me continue reading the file to see the rest of the
collectResponseFieldsfunction and understand how columns are added. I also need to find where the threshold check should be added. Let me read the rest of the file. - Assistant
Now I need to understand the issue. The problem is that when a resource has too many leaf fields (>2000), the generated CREATE TABLE statement exceeds SQLite's SQLITE_MAX_COLUMN limit (default 2000). I need to add a check in the BuildSchema function to count columns and fall back to a JSON-only schema (id, data, synced_at) when the column count exceeds 1500 (leaving headroom under the 2000 limit).
Let me first check if there are any tests to understand the expected behavior. Let me also look for existing tests.
- Assistant
Let me look at the test file to understand the expected behavior and see if there are any existing tests for column limits.
- Assistant
Let me continue reading the test file to see if there are any existing tests for column limits or wide schemas. Let me also check if there are any tests that show how the schema builder handles many columns.
- Assistant
Let me look at the test for columns from response schema to understand the structure better.
- Assistant
Let me create a reproduction script to test the current behavior with a wide schema.
- Assistant
I need to set up the Go environment properly first. Let me check what Go version and setup is available.
- Assistant
Let me check the go.mod file and try to understand the environment better.
- Assistant
Let me check the go.mod file first.
- Assistant
Let me download dependencies and then run my test script.
- Assistant
Go is having trouble because the module cache is not set. Let me set GOPATH and GOMODCACHE.
- Assistant
Now let me try running the test script again.
- Assistant
Let me create a test file inside the generator package instead.
- Assistant
Need to set GOCACHE as well.
- Assistant
Good, existing tests pass. Now let me understand the full picture - let me look at the rest of schema_builder.go, the hasTypedTable template function, and how tables are consumed in the template.
- Assistant
Now let me look at the hasTypedTable function in the templates.
- Assistant
Let me look at the generator.go to see the hasTypedTable function and understand how it determines whether a table gets typed columns.
- Assistant
Now I understand the core issue. The fix needs to happen in
BuildSchemainschema_builder.go. After building columns for a high-gravity resource, I need to check if the count exceeds 1500 and fall back to base columns only. Let me look for how logging is done in this codebase, and also check the template to understand what else needs to be conditioned on column count. - Assistant
No logging currently in schema_builder.go. Let me check what kind of logging is used in the generator.
- Assistant
Let me look at the specific parts of the template where FTS5, indexes and upsert logic depend on the column count.
- Assistant
Let me look at the UpsertBatch dispatch function.
- Assistant
Let me check the UpsertBatch dispatch to understand how it works.
- Assistant
Now I understand the architecture. The
hasTypedTablepredicate checkslen(t.Columns) > 3. If I reset columns to just base columns when count > 1500,hasTypedTablereturns false and the template will skip all typed-upsert, FTS, index creation for that table. That's exactly what we need.Let me also check
domainUpsertMethodNameto understand how it interacts. - Assistant
Now I have a solid understanding of the problem. Let me check the
domainUpsertMethodNamefunction to make sure my fix is correct. - Assistant
Let me understand the flow more. I need to see how
BuildSchemais called and how warnings are handled. - Assistant
Let me look at the context around line 2442 to understand how
BuildSchemais called and how warnings are handled. - Assistant
Now I understand the full picture. Let me now implement the fix. The key insight is:
- In
BuildSchema, after building columns for a high-gravity resource, check if column count exceeds 1500 - If so, reset to base columns only (id, data, synced_at), clear indexes and FTS5
- The
hasTypedTablefunction (len(t.Columns) > 3) will naturally cause the template to skip all typed upsert, FTS, index creation for that table
Let me also check how
hasTypedTableworks more carefully - it checkslen(t.Columns) > 3. If we reset tobaseTableColumns(3 columns), it returns false, which is exactly what we want. But we also need to make sure the FTS5 fields are cleared so FTS tables/triggers aren't generated.Let me also check if there's a need to handle the FTS5 block in the
BuildSchemafunction more carefully. - In
- Assistant
Now I have a complete understanding of the issue. The fix is straightforward:
- After building columns for a high-gravity resource (line 113), check if column count exceeds 1500
- If so, reset the table to base columns only, clear indexes and FTS5, and log the fallback
- The
hasTypedTablepredicate (len(t.Columns) > 3) will naturally cause the template to skip all typed upsert, FTS, index creation
Let me implement the fix:
- Assistant
Now let me make the changes. First, add the imports, then add the column cap constant and the fallback logic.
- Assistant
Now let me add the column cap constant and the fallback logic. I'll add the constant after
baseTableColumns: - Assistant
Good, the constant is added. Now I need to add the fallback logic after the column-building block and before the FTS5 block. Let me look at the exact lines:
- Assistant
Now I need to add the column cap check after the gravity >= 2 block (line 123) and before the FTS5 block (line 125). The check should reset the table to base columns if the column count exceeds MaxTypedColumns.
- Assistant
Good, the fallback logic is in place. But there's an important subtlety: the
schemaWithDependentParents()function adds aparent_idcolumn to dependent resources AFTERBuildSchemareturns. This could push a JSON-only table (3 cols) to 4 columns, makinghasTypedTablereturn true and triggering typed upsert code generation. I need to add aJSONOnlyflag to prevent this.Let me add the flag to
TableDef: - Assistant
Good, the
JSONOnlyflag is added toTableDef. Now I need to update the fallback logic to setJSONOnlyto true when the fallback fires. Let me update the fallback block: - Assistant
I see the edit went wrong. There's a duplicate
textFieldsline and it looks like the}closing brace for theif gravity >= 2block was lost. Let me look at the current state of the file more carefully: - Assistant
I see the issue - the closing brace for the
if gravity >= 2block was lost and there's a duplicatetextFieldsline. Let me fix the entire section properly: - Assistant
There's still a duplicate
tables = append(tables, table)on lines 154 and 156. Let me fix that: - Assistant
There's still a duplicate comment on lines 156-158. Let me fix that:
- Assistant
There's still a duplicate line 158. Let me fix that:
- Assistant
Good, the file looks correct now. Now I need to update the
hasTypedTablefunction in generator.go to check theJSONOnlyflag. Let me do that: - Assistant
Now I need to update the
hasTypedTablefunction to check theJSONOnlyflag. Let me edit it: - Assistant
Good, the
hasTypedTablefunction is updated. Now I need to update theschemaWithDependentParentsfunction to skip JSON-only tables. Let me look at that function: - Assistant
Now I need to update the
schemaWithDependentParentsfunction to skip JSON-only tables. Let me edit it: - Assistant
Good, the
schemaWithDependentParentsfunction is updated. Now let me verify the changes compile correctly by running the existing tests: - Assistant
Good, the code compiles. Now let me run the existing tests to make sure I didn't break anything:
- Assistant
All existing BuildSchema tests pass. Now let me also run the full generator test suite to make sure nothing is broken:
- Assistant
The failure is from
TestGenerateWithNoAuthand it's about agovulncheckvulnerability scan, not related to my changes. Let me run a subset of tests that are more likely to be affected by my changes - specifically the schema_builder tests and any store-related tests: - Assistant
All tests pass. Now let me also check if there are any other places where
hasTypedTableis used or whereJSONOnlyshould be checked. Let me search for references tohasTypedTableand other places where the column count might matter: - Assistant
Let me check what's on line 1025 to see if there's another place where FTS5 is used that might need the JSONOnly check:
- Assistant
Good, I can see at line 1025 there's a
.FTS5check that's not gated byhasTypedTable. But since I already added&& !table.JSONOnlyto the FTS5 assignment inBuildSchema, JSON-only tables will haveFTS5 = false, so this is already handled correctly.Now let me write a quick test script to verify the fix works correctly for both wide and narrow schemas:
- Assistant
Wait, I realize there's already a
findTablefunction in the test file. Let me check: - Assistant
There's already a
findTablefunction in the existing test file. I should remove my duplicate definition. Let me update my test file: - Assistant
The edit went wrong - it truncated the first function. Let me just rewrite the whole file cleanly:
- Assistant
Good. Now let me run these new tests to verify the fix works correctly:
- Assistant
The test logic for
ExactThresholdis wrong. With 1498 response fields + 3 base = 1501 columns > 1500, so it does trigger the fallback. The test is checkingJSONOnlyagainst 3 (the reset columns) instead of the original count. Let me fix the test: - Assistant
Now let me run all the tests again:
- Assistant
The
TestBuildSchema_WideResourceGravityBelowThresholdtest has the wrong expectation. With 2000 fields and 1 endpoint, gravity is actually ≥ 2 (1+2=3), so typed columns ARE added and then the fallback fires. Let me fix the test: - Assistant
Now let me run the tests again:
- Assistant
All tests pass. Now let me also run the store-related tests and the upsert tests to make sure I haven't broken anything:
- Assistant
All store and upsert tests pass. Now let me also run a broader set of tests to make sure I haven't broken anything else. Let me run all the generator tests except the one that fails due to vulncheck:
- Assistant
Let me try a different approach to avoid bash history expansion:
- Assistant
All tests pass. Now let me clean up the temporary test file since the issue says "UNDER NO CIRCUMSTANCES should you modify existing tests or add new tests to the repository". Let me remove it and review all changes:
- Assistant
Now let me clean up the temp test file:
- Assistant
Now let me review all my changes to make sure they're correct:
- Assistant
The diff looks clean and correct. Let me verify all the changes are right:
-
schema_builder.go:- Added
fmtandosimports for stderr logging - Added
JSONOnly boolfield toTableDefstruct - Added
MaxTypedColumns = 1500constant - Added column cap check in
BuildSchemaafter column-building loop: iflen(table.Columns) > MaxTypedColumns, reset to base columns, clear indexes, setJSONOnly = true, and log the fallback - Added
!table.JSONOnlyguard to FTS5 assignment to prevent FTS for wide resources
- Added
-
generator.go:- Updated
hasTypedTableto check!t.JSONOnlyfirst - Updated
schemaWithDependentParentsto skip JSON-only tables (don't inject parent_id)
- Updated
Let me run the existing tests one more time to confirm everything still passes:
-
- Assistant
All tests pass. Let me also make sure the generated test file was properly removed:
- Assistant
Wait, the git status says
wide_schema_test.gois still showing. Let me check if it was actually deleted: - Assistant
The file is already deleted. The
wide_schema_test.goin the status output was likely just from the current file being tracked. Let me run a clean build to verify: - Assistant
Good, the build passes. Let me also run
go vetto make sure there are no linting issues: - Assistant
All checks pass. Let me now do the final review of the changes before submitting:
- Assistant
Let me verify once more that the test file doesn't exist:
- Assistant
Good, the test file doesn't exist. Let me run one final comprehensive check to make sure everything works:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.