feat: ctg fix / ctg doctor --fix to repair corrupted config and artifacts
Context
Validating a real dApp at /home/dionebastos/Documentos/PROJETOS/mydapp (template react-vite-counter) revealed a gap:
caatinga.config.js was corrupted by repeated external edits (LLM harness wrote to it): mixture of // caatinga.config.ts header + const config = {} + module.exports + two orphan blocks network: ... outside any object (caatinga.config.js:1,15,24). File is syntactically broken but not detected as legacy.
caatinga.artifacts.json is only migratable via ctg migrate artifacts (schema migration). No command repairs content drift/corruption.
caatinga.config.ts had wrong networks.testnet.rpcUrl = https://testnet.stellar.org (should be https://soroban-testnet.stellar.org), causing ctg read counter.get --network testnet to fail with CAATINGA_INVOKE_FAILED / client error (Connect) while stellar contract invoke ... --rpc-url https://soroban-testnet.stellar.org -- get returns 0 immediately. ctg doctor reports ready but does not offer a fix.
- Template declares canonical config as
files.config = caatinga.config.ts (caatinga.template.json:15), but a stray caatinga.config.js is not flagged as orphan.
A harness or manual edit can corrupt these files; recovery today is manual.
Proposal
Add a repair command — either ctg fix or ctg doctor --fix (preferred alias fix for discoverability):
ctg fix [--dry-run] [--yes] [--include-orphan-js]
ctg doctor --fix [--dry-run] [--yes]
Behavior
1. caatinga.config.ts
- Validate against the Zod schema already used by
doctor (same error messages).
- On parse failure: create
caatinga.config.ts.bak.<timestamp> (0600), then:
- if
caatinga.config.js exists and is orphan: propose to remove it (with caatinga.config.js.bak), not to merge.
- if fixable (e.g. missing
networks.testnet.rpcUrl, wrong passphrase, missing frontend.bindingsOutput): patch in place and report diff.
--dry-run prints diff only.
2. caatinga.artifacts.json
- Validate JSON + schema
version. On corruption: backup .bak, then re-derive from on-chain state via inspect/status (contractId, wasmHash, deployedAt) when network reachable; otherwise fail with hint to run ctg deploy.
- Drift: if
wasmHash != local target/.../release/*.wasm hash, warn and suggest ctg build / ctg deploy.
3. Orphan caatinga.config.js
- Detected when
caatinga.config.ts exists and template.files.config === caatinga.config.ts. Action: prompt to delete/move to .bak (--include-orphan-js to auto-remove in CI). Never attempt to auto-merge CJS→TS.
4. Safety
- All writes atomic (tmp + rename,
restrict_to_owner 0600 for secrets).
--yes required for non-interactive fix; otherwise prompt.
- Exit code 0 if fixed or already clean, 1 if needs
--fix, 2 if unrecoverable.
Acceptance
ctg doctor --strict after ctg fix --dry-run shows clean.
npx ctg read counter.get --network testnet succeeds after fixing rpcUrl.
- Corrupted
caatinga.config.js as reproduced above is detected and offered for removal.
Out of scope
- Auto-fixing user contract logic or
Cargo.toml.
Repro
- Corrupt
caatinga.config.js with duplicated blocks (see file in description).
- Set
rpcUrl: https://testnet.stellar.org in caatinga.config.ts.
npx ctg read counter.get --network testnet → Connect error; doctor says ready.
Related
ctg migrate artifacts — schema only
caatinga.template.json — canonical file list
feat:
ctg fix/ctg doctor --fixto repair corrupted config and artifactsContext
Validating a real dApp at
/home/dionebastos/Documentos/PROJETOS/mydapp(templatereact-vite-counter) revealed a gap:caatinga.config.jswas corrupted by repeated external edits (LLM harness wrote to it): mixture of// caatinga.config.tsheader +const config = {}+module.exports+ two orphan blocksnetwork: ...outside any object (caatinga.config.js:1,15,24). File is syntactically broken but not detected as legacy.caatinga.artifacts.jsonis only migratable viactg migrate artifacts(schema migration). No command repairs content drift/corruption.caatinga.config.tshad wrongnetworks.testnet.rpcUrl = https://testnet.stellar.org(should behttps://soroban-testnet.stellar.org), causingctg read counter.get --network testnetto fail withCAATINGA_INVOKE_FAILED / client error (Connect)whilestellar contract invoke ... --rpc-url https://soroban-testnet.stellar.org -- getreturns0immediately.ctg doctorreportsreadybut does not offer a fix.files.config = caatinga.config.ts(caatinga.template.json:15), but a straycaatinga.config.jsis not flagged as orphan.A harness or manual edit can corrupt these files; recovery today is manual.
Proposal
Add a repair command — either
ctg fixorctg doctor --fix(preferred aliasfixfor discoverability):Behavior
1.
caatinga.config.tsdoctor(same error messages).caatinga.config.ts.bak.<timestamp>(0600), then:caatinga.config.jsexists and is orphan: propose to remove it (withcaatinga.config.js.bak), not to merge.networks.testnet.rpcUrl, wrong passphrase, missingfrontend.bindingsOutput): patch in place and report diff.--dry-runprints diff only.2.
caatinga.artifacts.jsonversion. On corruption: backup.bak, then re-derive from on-chain state viainspect/status(contractId, wasmHash, deployedAt) when network reachable; otherwise fail with hint to runctg deploy.wasmHash!= localtarget/.../release/*.wasmhash, warn and suggestctg build/ctg deploy.3. Orphan
caatinga.config.jscaatinga.config.tsexists andtemplate.files.config === caatinga.config.ts. Action: prompt to delete/move to.bak(--include-orphan-jsto auto-remove in CI). Never attempt to auto-merge CJS→TS.4. Safety
restrict_to_owner0600 for secrets).--yesrequired for non-interactive fix; otherwise prompt.--fix, 2 if unrecoverable.Acceptance
ctg doctor --strictafterctg fix --dry-runshows clean.npx ctg read counter.get --network testnetsucceeds after fixingrpcUrl.caatinga.config.jsas reproduced above is detected and offered for removal.Out of scope
Cargo.toml.Repro
caatinga.config.jswith duplicated blocks (see file in description).rpcUrl: https://testnet.stellar.orgincaatinga.config.ts.npx ctg read counter.get --network testnet→Connecterror;doctorsaysready.Related
ctg migrate artifacts— schema onlycaatinga.template.json— canonical file list