Skip to content

feat: ctg fix / doctor --fix to repair corrupted config and artifacts #213

Description

@Dione-b

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

  1. Corrupt caatinga.config.js with duplicated blocks (see file in description).
  2. Set rpcUrl: https://testnet.stellar.org in caatinga.config.ts.
  3. npx ctg read counter.get --network testnetConnect error; doctor says ready.

Related

  • ctg migrate artifacts — schema only
  • caatinga.template.json — canonical file list

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: file-ioAtomic writes, file operationsenhancementNew feature or requestpriority: mediumMedium priority fix - correctness or UX issues

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions