Skip to content

feat(#62): implement code generation layer#86

Merged
sugat009 merged 49 commits into
mainfrom
62-integrate-code-gen-layer
Jul 6, 2026
Merged

feat(#62): implement code generation layer#86
sugat009 merged 49 commits into
mainfrom
62-integrate-code-gen-layer

Conversation

@sugat009

@sugat009 sugat009 commented Apr 8, 2026

Copy link
Copy Markdown
Member

Summary

  • Add multi-round tool-use loop in Anthropic provider for filesystem access during code generation (read_file, list_directory)
  • Add Beads session tracking for code gen pipeline (plans, per-file progress, failures)
  • Add selective regeneration: carry forward passing files on retry, only regenerate failing ones
  • Replace naive positional diff with LCS-based diff package for validation prompts
  • Loosen manifest scope so the plan LLM can reference files outside the pre-gathered index (e.g. permission configs, shared settings)
  • Exclude test files from code gen plan (handled by separate Test Environment Agent)
  • Add per-file validation feedback (FileValidationFeedback) for targeted retries
  • Fix disableTools/tools option contradiction across CLI and API providers
  • Fix operator precedence bug in looksLikePythonScript
  • Extract shared utilities: createModel helper, FailingFileRef type, extractTextContent, capMaxTokens

#62

Test plan

  • npm run build passes
  • npm test passes (291 tests)
  • Full workflow tested with Claude CLI provider — generates correct files, no test files in plan
  • Full workflow tested with Claude API provider — generates correct files with tool-use, no test files in plan
  • Both providers produce functionally equivalent output for the same ticket
  • Manifest scope fix confirmed: default-permissions.js now included in plan when permission-related features are generated
  • Manual verification of generated CHT-Core code on localhost:5988

Update: legacy test-environment-agent removed

Drops the legacy src/agents/test-environment-agent.ts prototype that conflated test generation (#63), test environment setup (#66), and test orchestration (#67) into one LLM-driven agent. The architecture splits these across three layers under a future QA Supervisor (#64). The Development Supervisor's validation node is now code-only; the persistence helpers (writeToStaging, writeToChtCore, getAllGeneratedFiles) no longer reach into state.testEnvironment; and the setupTests graph node is gone, so the workflow runs generateCode → validateImpl directly. This supersedes the "handled by separate Test Environment Agent" note in the summary above.

Commits:

  • b461140 remove legacy test-environment-agent ahead of Implement Test Generation Layer #63 implementation
  • 097f078 drop test-env consumers from validation node and persistence
  • 5057169 clean stale test-env comments in edited files

Gates after removal: build and lint green, 629 tests passing, coverage 83.31/69.36/82.11/85.07 (thresholds 78/60/72/78), SonarCloud Quality Gate OK with 0 new issues. QA verified the rewired graph executes at runtime (generateCode → validateImpl, no setupTests) with no validation-score regression.

🤖 Generated with Claude Code

sugat009 and others added 13 commits November 19, 2025 20:50
…aml for frontmatter parsing and add more tests
Resolved conflicts:
- .eslintrc: kept main's rules
- package.json: merged scripts (added full and example:full)
- src/types/index.ts: kept development types added in this branch
- Other research files: accepted main's version
- Add Claude Code CLI as drop-in LLM provider (src/llm/providers/claude-cli.ts)
- Add TodoTracker utility for progress tracking (src/utils/todo-tracker.ts)
- Integrate todo tracking in all 4 agents:
  - documentation-search-agent
  - context-analysis-agent
  - code-generation-agent
  - test-environment-agent
- Integrate todo tracking in both supervisors:
  - research-supervisor
  - development-supervisor
- Add MCP client setup (src/mcp/)
- Add CLI validation script (src/scripts/validate-cli.ts)
- Add cht-core context utility (src/utils/cht-core-context.ts)
- Update LLM factory to support CLI provider with configurable maxTurns
- Update .gitignore to exclude log folder

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Remove ParsedTicket alias, use IssueTemplate directly
- Extract duplicated readEnv() to shared src/utils/env.ts
- Move CONTRIBUTING.md from src/ to designs/
- Move test file to test/layers/code-gen/ matching src/ convention
- Merge duplicate imports in test file
- Add missing test cases (env var fallback, alias resolution, empty inputs)
- Revert unrelated proxyquire change in context-loader.spec.ts
- Regenerate clean package-lock.json removing spurious peer additions
…eneration

Implement the full code generation pipeline with Claude CLI integration.
Key changes:
- Per-file sequential generation (one LLM call per planned file)
- Python transform mode for large JSON files (>50K chars)
- Search-replace mode for large non-JSON MODIFY files (>1000 lines)
- LLM preamble stripping and reasoning detection
- Plan reordering (JSON files generated last)
- Dev CLI for development workflow
- Zod schemas for plan validation
- Updated tests for registry, schemas, and claude-api module
…ovements

- Add multi-round tool-use loop in Anthropic provider (bindTools, ToolMessage)
- Add LLMToolDefinition, ToolHandler types and capMaxTokens utility
- Add read_file/list_directory tools for API provider filesystem access
- Add Beads session tracking for code gen (plan, per-file progress, failures)
- Add selective regeneration: carry forward passing files, only regenerate failing
- Add diff-based validation using `diff` package (replaces naive positional diff)
- Add per-file validation feedback (FileValidationFeedback) for targeted retries
- Loosen manifest scope: plan prompt no longer restricts files to known scope
- Exclude test files from code gen plan (separate agent handles tests)
- Extract createModel helper, FailingFileRef type, extractTextContent utility
- Fix operator precedence bug in looksLikePythonScript
- Fix disableTools/tools option contradiction across providers
@sugat009 sugat009 linked an issue Apr 8, 2026 that may be closed by this pull request
@sugat009 sugat009 changed the title feat(#62): integrate code generation layer feat(#62): implement code generation layer Apr 8, 2026
@sugat009 sugat009 self-assigned this Apr 8, 2026
@sugat009 sugat009 moved this from Todo to In Review in CHT Multi-Agent System (cht-agent) Apr 17, 2026
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
252 New issues
22 Security Hotspots

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@sugat009 sugat009 marked this pull request as draft May 13, 2026 10:27
@sugat009

Copy link
Copy Markdown
Member Author

Fixing merge conflicts...

@sugat009 sugat009 moved this from In Review to In Progress in CHT Multi-Agent System (cht-agent) May 13, 2026
…afety net

Two real code-gen modules selectable via CODE_GEN_MODULE:

- claude-code-cli: tool-using agent driven by the Claude Code CLI's
  native Read/Write/Edit/Glob/Grep tools. Uses Claude MAX subscription.
- claude-api (default): prompt-and-parse approach against the Anthropic
  API. Requires ANTHROPIC_API_KEY. Two-step plan-then-per-file generation.
- opencode: deferred stub.

Deterministic safety net at the agent layer:

- Compile gate runs tsc against all cht-core tsconfigs before rollback;
  errors surface as cross-file issues at HC2.
- Cross-file validator catches identifier mismatches (template bindings,
  selector references, action method calls).
- AST validator complements regex passes for shape and contract checks.
- Locale propagator auto-fills the 9 non-en messages-*.properties files
  with alphabetically inserted English placeholders.

LangGraph supervisor with 3-iteration refinement loop, execute-no-op
short-circuit, per-node shutdown checks, and HC2 banner grouping
cross-file issues by type.

Adds 4 real-issue ticket fixtures (10944, 10997, 11017, 11048) used by
the runner for cross-shape regression tests. Removes superseded planning
docs (BRANCH_CHANGES.md, cht-multi-agent-plan.md, tickets/9.md).
@sugat009 sugat009 marked this pull request as ready for review May 19, 2026 11:40
@Hareet Hareet requested review from witash and removed request for Hareet June 3, 2026 15:21
sugat009 added a commit that referenced this pull request Jun 6, 2026
* feat(#14): Implement code generation modules (#69)

* fix(#14): address PR #69 review comments

- Remove ParsedTicket alias, use IssueTemplate directly
- Extract duplicated readEnv() to shared src/utils/env.ts
- Move CONTRIBUTING.md from src/ to designs/
- Move test file to test/layers/code-gen/ matching src/ convention
- Merge duplicate imports in test file
- Add missing test cases (env var fallback, alias resolution, empty inputs)
- Revert unrelated proxyquire change in context-loader.spec.ts
- Regenerate clean package-lock.json removing spurious peer additions

* fix(#14): replace implementation plan with code scaffold in claude-api module

* fix(#14): add warnings to output, reject duplicate registration, clarify repo-specific examples

* fix(#14): reduce cognitive complexity and use modern string/array methods

* docs(#14): refresh code-gen CONTRIBUTING to the #86 target design

* fix(#14): restore scaffold fallback when phase components yield no files

---------

Co-authored-by: Shashi sah <86319846+shashi-sah2003@users.noreply.github.com>
@Hareet

Hareet commented Jun 24, 2026

Copy link
Copy Markdown
Member

@witash
Could we get your feedback/review on this?

@witash witash left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its maybe a moot point for this PR, but in future I think we need to split these large PRs into smaller pieces; its not realistic to meaningfully do a line-by-line review of 100 files and 22,000 lines; even if it ends up being mutliple PRs with the same total changes, its easier to understand independent pieces individually.

ran manually over both the sample tickets in the repo (just to see it working) and over the datasource migration tickets.
the datasource migration tickets can't take advantage of the context database as much, and there are some inaccuracies (or maybe preferences where the AI has chosen a different preference) in the ticket descriptions, that lead to inaccuracies in the generated code, but the agent generated apparently working and correct code for each of the tickets, based off the descriptions. Although not completely read to just submit as a PR because of test failures.

a few notes:
this branch implements both beads and has its own todo-tracker.
Are both really necessary?

I looked at the prompt rules, static validators, and the LLM validation.
Also due to the large diff size, its a little hard to understand and evaluate every piece, but not many notes...
For typescript shared-libs, the compile step is important; without it, it does generate code that doesn't compile. But its only for claude-code-cli, otherwise it doesn't run compile, is there a reason?

The tickets I tested did not do much on the frontend, so the Angular checks I just skimmed; claude proposed some small tweaks but I don't think its worth it to fiddle with them too much now. I guess these will iterate after this gets merged and gets more usage anyway.

The AST signature validator detect only growth in the parameters, and is relevant only for JS files or when the compile gate is not used, because tsc would catch signature mismatches usually. Most of the cht code is not TS so that's fine, but then do we want to make the signature validator stricter?

One thing thats notably missing is running unit tests...I know that's another PR, but a big difference between using these agents and claude code in interactive mode is that claude code runs the unit tests frequently; for tickets like the datasource migrations which include a lot of modifications and not many additions, tests are a much more concrete and measurable way to validate that nothing existing broke accidentally than the validations in this branch. And small changes to tests can end up being a large portion, even the majority, of the diff. Not running them in the validation stage seems missing, and can generate code that doesn't pass tests.
So nothing to change here, but just noting that when the test branch is merged, I think it will improve the validation as well.

@Hareet

Hareet commented Jul 1, 2026

Copy link
Copy Markdown
Member

Thanks for some great feedback! @witash I'm concerned about the 2nd paragraph -- Let's fix our data-labeling (domains, descriptions, input tickets, context funneling). Some narrower ideas below

an manually over both the sample tickets in the repo (just to see it working) and over the datasource migration tickets.
the datasource migration tickets can't take advantage of the context database as much, and there are some inaccuracies (or maybe preferences where the AI has chosen a different preference) in the ticket descriptions, that lead to inaccuracies in the generated code, but the agent generated apparently working and correct code for each of the tickets, based off the descriptions. Although not completely read to just submit as a PR because of test failures.

The datasource tickets struggle because it seems like a domain / context-selection gap: they route to data-sync, whose corpus is all replication memory, not the cht-datasource query-layer shape (qualifier/adapter/endpoint) these tickets would be better off with. The agent injects the wrong pattern there. Root cause is that cht-datasource has no clean home in our 9 domains (pitfall says data-sync, precedent says contacts, template says data-sync). #11174 shows the real axis is a Person/Place/Contact/Report data-access hierarchy.

Proposing a first-class data-access domain + populating its component index/patterns; I'll open a follow-up and link it here. Seems the description in #11174's "same qualifier union as getUuidsPage," pushed the agent toward the rejected design. cc: @sugat009

sugat009 added 12 commits July 1, 2026 18:19
…-layer

# Conflicts:
#	.env.example
#	.gitignore
#	.nycrc.json
#	package-lock.json
#	package.json
#	src/agents/documentation-search-agent.ts
#	src/layers/code-gen/interface.ts
#	src/layers/code-gen/modules/claude-api/index.ts
#	src/layers/code-gen/registry.ts
#	src/llm/providers/anthropic.ts
#	src/llm/providers/claude-cli.ts
#	src/llm/types.ts
#	src/mcp/index.ts
#	src/supervisors/research-supervisor.ts
#	src/types/index.ts
#	src/utils/domain-inference.ts
#	test/layers/code-gen/code-gen-registry.spec.ts
#	test/llm/factory.spec.ts
#	test/llm/providers/anthropic.spec.ts
#	test/llm/providers/claude-cli.spec.ts
Comment thread src/agents/ast-validator.ts Fixed
Comment thread src/layers/code-gen/lib/python-transform.ts Dismissed
Comment thread src/layers/code-gen/lib/python-transform.ts Dismissed
Comment thread src/utils/beads-client.ts Dismissed
@sugat009

sugat009 commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

@witash thanks for the thorough review, the datasource-migration runs were the most useful signal.

beads vs todo-tracker: different layers, not redundant. TodoTracker is ephemeral console progress; BeadsCodeGenSession wraps the external bd CLI for durable issues and is env-gated (a no-op when uninitialized). Removed the unused surface on both.

Compile gate only for claude-code-cli: closed this cycle, the claude-api path now runs the same gate. Since it synthesizes in memory, it snapshots cht-core, materializes the files behind a path guard, runs tsc --noEmit, and rolls back. Both Claude transports now type-check their output.

Stricter AST signature validator: deliberately light (param-growth only), and with both paths now on the compile gate tsc supersedes it, so its niche shrank further. Also fixed a ReDoS in its permission-literal regex (the brittle-regex bit you flagged). TS-aware checks are a follow-up if ever needed.

Running unit tests in validation: agreed it matters, but it's the downstream QA phase's job (Test Environment / Orchestration / QA Supervisor), which runs after development and isn't wired in yet. This PR is code-gen; its validation is generation-time (compile + static/AST). Your point is exactly what that phase is for.

Since your review: removed ~470 lines of dead code, fixed a parser bug, added the API compile gate, raised coverage toward main (85/72/84/86), and cleared the SonarCloud findings.

On PR size, fully taken, we'll keep the follow-ups (coverage to 95/85, writeToChtCore hardening, the manual claude-api run) as separate, smaller pieces.

@sugat009

sugat009 commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

@Hareet agreed, we settled this in the team meeting: proceed with a first-class datasource/data-access domain, go ahead and raise the PR for it.

Populate it by running the #119 pipeline over the cht-datasource tickets. That lines up with Kombo's observability, once the Langfuse instrumentation (#127) merges and points at the deployed instance, the datasource run rides on the same pipeline pass, so we get traces for it and a clean before/after. And agreed the input labeling (the #11174 description that steered the agent) needs fixing alongside the domain, not the domain alone.

@sugat009 sugat009 merged commit 35e956b into main Jul 6, 2026
4 checks passed
@sugat009 sugat009 deleted the 62-integrate-code-gen-layer branch July 6, 2026 04:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Implement Code Generation Layer

5 participants