Commit Graph

39 Commits

Author SHA1 Message Date
Hongming Wang
db6b196631 feat(connect): M1.2 — heartbeat + activity poll loops
Implements the runtime side of `molecule connect <id>`. After this PR
the CLI can actually attach to an external workspace and round-trip
inter-agent messages through any registered backend.

What's in:

- `internal/connect/client.go` — platform-API client with bearer auth.
  Endpoints: POST /registry/register (delivery_mode=poll, no URL),
  POST /registry/heartbeat, GET /workspaces/:id/activity?type=a2a_receive,
  POST /workspaces/:id/a2a (reply target).
  Errors split into TransientError (network/5xx — retry with backoff)
  and PermanentError (4xx — abort with clear message).

- `internal/connect/state.go` — atomic cursor persistence at
  ~/.config/molecule/state/<workspace-id>.json. Mode 0o600 (owner-only)
  from day 1 because future state additions may include rotated tokens.
  Atomic write-then-rename so a crash mid-write can never produce a
  half-written cursor.

- `internal/connect/connect.go` — Run() orchestrator. Wires register-
  with-bounded-retry, then heartbeat goroutine + poll goroutine.
  Both respect ctx cancellation for clean SIGTERM.

  Robustness contract per RFC #10:
    * Cursor advances AFTER successful dispatch — crash mid-batch
      re-delivers, never drops.
    * 410 on cursor lookup → reset to "" and re-fetch (don't deadlock
      on a pruned cursor).
    * Heartbeat permanent error stops the heartbeat loop only; poll
      loop keeps running so the operator sees "stopped" + reason in
      logs and can SIGTERM.
    * Backend dispatch is sequential within a batch (avoids out-of-
      order replies for in-flight conversations).
    * Inter-agent reply path: POST envelope to /workspaces/<source>/a2a.
    * Canvas-origin reply (source_id == nil) logs + skips for now —
      M1.3 wires that via the task_update activity convention.

- `internal/cmd/connect.go` — runConnect now actually calls
  connect.Run() (was a placeholder ctx-wait in M1.1).

Test plan:

- httptest workspace-server stub covers register / heartbeat / activity
  / a2a reply endpoints.
- TestRun_RoundTrip_AgentReply: end-to-end ping → mock backend → pong
  reply lands at source, cursor saved.
- TestRun_CanvasOriginMessageNotReplied: source_id=nil → backend fires
  but no reply post; cursor still advances.
- TestRun_CursorPruned410ResetsAndContinues: server returns 410 once,
  cursor resets to "", next poll dispatches the fresh row.
- TestRun_PermanentRegisterErrorAborts: 401 surfaces immediately.
- TestRun_TransientRegisterErrorRetries: 503 then 200 → register
  succeeds on second attempt.
- TestRun_OptionsValidation: missing Backend / WorkspaceID surface
  before any I/O.
- State: round-trip, file mode 0o600, atomic-rename leaves no .tmp
  artifacts, corrupted file surfaces error.
- All tests green under -race.

Out of scope (next PRs in this stack):

- M1.3: claude-code backend (canvas-origin reply convention rides
  with this)
- M1.4: GoReleaser tag-triggered release.yml workflow
- Push-mode (--mode push currently surfaces a clear "M4" error)

RFC: https://github.com/Molecule-AI/molecule-cli/issues/10

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 03:15:51 -07:00
Hongming Wang
268e0cd9ed
Merge pull request #11 from Molecule-AI/auto/connect-m1-foundation
feat(connect): M1.1 — Backend interface + connect skeleton + mock backend
2026-04-30 03:08:00 -07:00
Hongming Wang
3a6a7eb495 feat(connect): M1.1 — Backend interface + connect skeleton + mock backend
First step toward `molecule connect <id>` — the out-of-box external-
runtime workspace connector specified in RFC #10.

What's in this PR (foundational, ~300 LOC of code + matching tests):

- `internal/backends.Backend` — the seam every concrete handler
  implements: HandleA2A(ctx, Request) → Response, Close(). Two methods,
  no inheritance, no surprise side effects. Concurrency-safe by
  contract (poll dispatch may parallelise).
- Request/Response/Part/Config types — lossless JSON-RPC mirror so
  backends can re-issue downstream without re-parsing.
- Compile-time registry — `Register("name", factory)` from each
  backend's init(); `Build(name, cfg)` selects at runtime. Panics
  on duplicate registration so drift fails loudly at startup, not
  on first message.
- `mock` backend — single-template echo for CI smoke + tests + demos.
  `--backend-opt reply="<template>"` with `%s` for inbound text.
- `molecule connect <workspace-id>` cobra command — flag surface,
  validation, --dry-run for smoke. Loops (heartbeat, activity poll,
  dispatch) land in M1.2 in internal/connect/.

Coverage:
- Registry: duplicate-name panic, empty-name panic, nil-factory panic,
  Build unknown-name error includes registered list.
- Mock: default template, custom template, text-part concatenation,
  Final=true on terminal response.
- Connect: --backend-opt KEY=VALUE parser (incl. value with =),
  flag validation (missing token, bad mode, bad opt, unknown
  backend), --dry-run happy path.

All tests pass under -race.

Out of scope (subsequent M1 PRs):
- M1.2: heartbeat + activity poll loops in internal/connect/
- M1.3: claude-code backend (wraps molecule-mcp-claude-channel)
- M1.4: GoReleaser tag-triggered release.yml workflow

RFC: https://github.com/Molecule-AI/molecule-cli/issues/10

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 02:05:12 -07:00
Hongming Wang
6127b4c77b
Merge pull request #8 from Molecule-AI/fix/yaml-output-support
fix(cli): implement true YAML output support
2026-04-24 13:26:39 -07:00
Hongming Wang
7dde287e58
Merge pull request #7 from Molecule-AI/feat/init-force-flag
feat(cli): add --force flag to molecule init
2026-04-24 13:26:36 -07:00
Hongming Wang
38d7618085
Merge pull request #6 from Molecule-AI/feat/cli-full-command-tree
feat(cli): implement full CLI command tree
2026-04-24 13:25:23 -07:00
Hongming Wang
57802aa22f fix(ci): sync auto-promote workflow (ff-only, no-gates mode) 2026-04-24 08:35:20 -07:00
Hongming Wang
f378f20760
Merge pull request #9 from Molecule-AI/chore/add-auto-promote-staging
chore(ci): add auto-promote-staging workflow
2026-04-24 08:18:49 -07:00
Hongming Wang
2539d007e5 chore(ci): add auto-promote-staging workflow 2026-04-24 07:44:02 -07:00
211b57b5b3 fix(cli): remove stray brace in runPlatformHealth causing syntax error
The yaml-output-support refactor removed the combined json/yaml if-block
but left a dangling `}` that closed runPlatformHealth prematurely,
putting the tabwriter block outside the function body.

Removes the stray brace at line 137. CI vet was failing with:
  platform.go:138:2: syntax error: non-declaration statement outside function body

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-24 05:23:40 +00:00
ab91b652ef fix(cli): implement true YAML output support
The `--output yaml` flag was accepted but aliased to JSON since
printYAML() was never defined. Add printYAML() using gopkg.in/yaml.v3
(already an indirect dep via viper) and split all output format
branches into explicit json/yaml/table paths.

Affected commands: workspace list/create/inspect/audit,
agent list/inspect/peers, platform audit/health.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-24 03:47:00 +00:00
e835eec6e8 feat(cli): add --force flag to molecule init
Allows re-scaffolding an existing molecule.yaml without manual deletion.
Exit code 1 preserved for existing file without --force (usage error path).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 21:17:36 +00:00
bf29d8b00a fix(cli): rename root command mol→molecule, register initCmd, fix config scaffold path
- Change rootCmd Use/Short/Long from 'mol' to 'molecule'
- Register initCmd in root so 'molecule init' works
- Update viper config name lookup from 'mol' to 'molecule'
- Fix config init/set to write molecule.yaml instead of mol.yaml
- Update all user-facing strings mol→molecule

Fixes TestCLI_Version, TestCLI_Init, TestCLI_ConfigInit, TestCLI_Completion_GeneratesScript
2026-04-23 19:24:12 +00:00
230934561c feat: implement full CLI command tree
Implement the core CLI for molecule-cli:

- cmd/molecule/main.go: entry point calling cmd.Execute()
- internal/cmd/root.go: cobra root with global flags (--api-url,
  --verbose, --output, --config), registers all 4 command groups
- internal/cmd/workspace.go: 7 subcommands (list, create, inspect,
  delete, restart, audit, delegate)
- internal/cmd/agent.go: 4 subcommands (list, inspect, send, peers)
- internal/cmd/platform.go: 2 subcommands (audit, health)
- internal/cmd/config.go: 5 subcommands (list, get, set, init, view)
- internal/cmd/http.go: runHTTP helper shared by agent send and
  workspace delegate
- internal/client/platform.go: control plane HTTP client with
  workspace/agent/health/audit operations

All 18 subcommands wire to platform API via MOLECULE_API_URL.
Binary builds to ./bin/mol. Resolves KI-001, KI-002 (partial),
KI-003.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 18:44:24 +00:00
1ebe5fa999 fix(cli): build from module root ("go build .") instead of "./cmd/molecule"
Go 1.25 resolves "./cmd/molecule" relative to cmd.Dir as an absolute path
under the CWD, producing the doubled "cmd/molecule/cmd/molecule" error.
Building the whole module from the root and relying on the binary name
from -o is the idiomatic Go approach and avoids path arithmetic entirely.

Also tightened error message to match the new command.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 19:12:49 +00:00
7b254c926c fix(cli): use cwd as repoRoot instead of os.Executable path
os.Executable() in Go 1.x parallel tests resolves to <TempDir>/0/molecule.test
making path-based traversal from the binary back to the repo root unreliable.
Switch to os.Getwd() which always returns the repo checkout root in CI
(where `go test ./cmd/molecule/...` is invoked), and skip any path arithmetic.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 19:06:30 +00:00
9dfb5f9aa4 fix(cli): replace runtime.GOEXE with hardcoded "go" in test
runtime.GOEXE is not available in Go 1.25 standard library.
Replace with the standard "go" binary name which is on PATH in CI.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 19:03:36 +00:00
6d1bcc007f fix(cli): add missing runtime import to test file
go vet reports: cmd/molecule/molecule_test.go:209:11: undefined: runtime

runtime.GOEXE is used in mol() to find the Go binary for building the
test binary. The runtime package was referenced but not imported.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 19:02:08 +00:00
1c9a207444 fix(cli): fix undefined v in runConfigSet
v.SafeWriteConfig() was called without a local viper instance.
Create a fresh viper scoped to the target config file, read any
existing values, set the new key, then atomically write via SafeWriteConfig.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 18:52:05 +00:00
2f4296f98e fix(cli): replace v.Marshal with v.SafeWriteConfig in config set
viper.Viper has no Marshal method in v1.21. Use SafeWriteConfig instead
which atomically writes only explicitly-set keys to the config file.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 18:37:28 +00:00
a1dacff202 fix(cli): remove unused _ = workspaceID, fix repoRoot() path for test binary
- workspace.go: remove no-op _ = workspaceID (workspaceID consumed in runHTTP call)
- molecule_test.go: replace hardcoded /workspace/repos/clone-cli path with
  dynamic exe-based resolution so tests pass when built by CI

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 18:35:43 +00:00
1471cad4c1 feat(cli): add molecule completion [bash|zsh|fish|powershell] subcommand
Wires shell completion for all 4 shells via Cobra's built-in generators.
Covers the remaining item from the implementation status checklist.

Adds:
- internal/cmd/completion.go: cobra.GenXxxCompletion wrappers with
  usage examples for each shell
- 5 new integration tests in cmd/molecule/molecule_test.go:
  - TestCLI_Completion_Help (4 shells × help flag)
  - TestCLI_Completion_GeneratesScript (4 shells × script output)
  - TestCLI_Completion_InvalidShell (exit code 2 on bad shell)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 18:06:02 +00:00
47692cfb36 fix(cli): complete "mol" → "molecule" rename in test error messages
All test.Fatalf messages referenced "mol <subcommand>" but the binary
is now "molecule". Also fix configSet to use os.WriteFile atomic write
instead of viper.WriteConfig (avoids file-permission edge cases).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 07:35:31 +00:00
3afa0d8c0a docs(cli): sync CLAUDE.md command tree with actual implementation
- Update Section 2 build/test commands to reflect current state
- Rename Section 8 command tree from "mol" to "molecule" throughout
- Add --api-url flag to Global Flags table, remove --platform-url
- Update error examples to use "molecule" command name
- Replace Section 11 "Early / Stub" checklist with accurate status
- Mark all implemented commands and tests as done, keep remaining todos

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 07:02:39 +00:00
ea69db8bfc fix(cli): complete "mol" → "molecule" rename across all cmd packages
Rename all user-facing "mol" references to "molecule" to match the
published binary name (binary: molecule in .goreleaser.yaml):
- root.go: Use string, versionInfo, viper config name, flag descriptions
- init.go: section comment, Short, Long, cfgPath, scaffolded message
- config.go: Long description, Shorts, configFile, WriteFile, Stat check
- molecule_test.go: binary name, version output check, mol.yaml → molecule.yaml

Also fix test helper to use runtime.GOEXE instead of hardcoded /tmp/go/bin/go.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 06:36:42 +00:00
f654a2dff9 fix(cli): align rootCmd.Use to goreleaser binary name
rootCmd.Use was "mol" while .goreleaser.yaml sets binary: "molecule".
Align to "molecule" to match the published binary name, test
invocations, and docs. Also fix test helper to use runtime.GOEXE
instead of hardcoded /tmp/go/bin/go.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 05:36:07 +00:00
07ab67552a feat(cli): implement mol init + 2 new integration tests
- Add `mol init` command (internal/cmd/init.go): scaffolds mol.yaml
  in the current directory with commented config sections and
  environment variable documentation. Exits with a clear "next steps"
  message. Checks for existing mol.yaml before overwriting.
- Register initCmd in root.go alongside the other command groups.
- Add 2 integration tests: TestCLI_Init (scaffolding + output)
  and TestCLI_Init_AlreadyExists (error path).
- Update CLAUDE.md command reference to list mol init and note it
  is checked off the stub repo checklist.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 10:50:50 +00:00
d263a30f84 fix(ci): narrow test/vet job to ./cmd/molecule/... package
The repo has no Go files at the root — go test ./... and go vet ./...
fail with "no Go files in ." in CI. Narrow the test job to the only
package with tests, and vet to the scoped package dirs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 10:34:04 +00:00
29e025129f chore(cli): add .goreleaser.yaml and resolve KI-004
Add .goreleaser.yaml with the correct module root (dir: .) and main
package path (./cmd/molecule) so the first v* tag release produces
valid artifacts for all 6 targets. Mark KI-004 as resolved in
known-issues.md.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 10:33:08 +00:00
47b2804516 test(cli): add integration tests and fix CI workflow
- Add 24 integration tests in cmd/molecule/molecule_test.go covering
  all 18 subcommands (workspace, agent, platform, config) including
  error paths for not-found and missing-arg cases
- Tests use httptest mock server; binary built per-test with correct
  repo root for go build ./cmd/molecule
- Fix release.yml: correct binary name (molecule not molecli), correct
  package path (./cmd/molecule not ./cmd/molecli)
- Add test job (go mod tidy + vet + test) to release.yml, runs on
  every PR touching Go files
- Release job gated on test job; conditional on v* tag push
- Mark KI-005 resolved in known-issues.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 10:12:28 +00:00
molecule-ai[bot]
09417df58f
Merge pull request #4 from Molecule-AI/feat/cli-full-command-tree
feat(cli): implement full CLI command tree
2026-04-21 01:19:40 +00:00
3eabe3c780 feat: implement full CLI command tree
Implement the core CLI for molecule-cli:

- cmd/molecule/main.go: entry point calling cmd.Execute()
- internal/cmd/root.go: cobra root with global flags (--api-url,
  --verbose, --output, --config), registers all 4 command groups
- internal/cmd/workspace.go: 7 subcommands (list, create, inspect,
  delete, restart, audit, delegate)
- internal/cmd/agent.go: 4 subcommands (list, inspect, send, peers)
- internal/cmd/platform.go: 2 subcommands (audit, health)
- internal/cmd/config.go: 5 subcommands (list, get, set, init, view)
- internal/cmd/http.go: runHTTP helper shared by agent send and
  workspace delegate
- internal/client/platform.go: control plane HTTP client with
  workspace/agent/health/audit operations

All 18 subcommands wire to platform API via MOLECULE_API_URL.
Binary builds to ./bin/mol. Resolves KI-001, KI-002 (partial),
KI-003.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 01:18:24 +00:00
25601ba758 feat(cli): implement cmd/molecule/main.go with full command tree
- Root command: --output, --verbose, --config flags
- Workspace subcommands: create/list/inspect/delete/restart/delegate/audit
- Agent subcommands: list/inspect/send/peers
- Platform subcommands: audit/health
- Config subcommands: list/get/set/init/view
- Exit codes 0/1/2, errors to stderr, text/json/yaml output
- Viper config file support, Gin mode (verbose = debug, silent = release)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 01:00:38 +00:00
88bcae698b docs: expand CLAUDE.md with full command reference, output formats, Homebrew tap
- Full mol command tree: workspace/agent/platform/config subcommands
- Output format examples (text/JSON/YAML)
- Error codes + format conventions + global flags table
- Homebrew tap release notes + cross-platform binary build targets

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 00:53:51 +00:00
molecule-ai[bot]
5e5ec063bf
docs: add CLAUDE.md, known-issues.md, and .claude/settings.json (#2)
* docs: add CLAUDE.md for agent onboarding

Inherits platform conventions from molecule-core:
- Cron discipline and triage rules
- Build/test commands (go build, go test)
- Go module conventions (go mod tidy, go.sum hygiene)
- Release process (GoReleaser tag workflow)
- CLI design conventions (kubectl/gh patterns, stderr errors, exit codes)
- Stub repo status checklist

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: add CLAUDE.md known-issues ref, known-issues.md, .claude/settings.json

- CLAUDE.md: add known-issues.md reference in Known Issues section
- known-issues.md: 5 entries (KI-001 main.go, KI-002 API client,
  KI-003 go.sum, KI-004 goreleaser, KI-005 no tests)
- .claude/settings.json: permissions for go/goreleaser tools,
  PreToolUse Bash hook, cleanupPeriodDays 30

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Molecule AI SDK-Dev <sdk-dev@agents.moleculesai.app>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-20 23:10:40 +00:00
Hongming Wang
898d792888
Merge pull request #1 from Molecule-AI/chore/credentials-gitignore
chore: gitignore credentials
2026-04-16 09:23:52 -07:00
rabbitblood
e330e440f6 chore: gitignore credentials for molecule-cli
Adds standard credential gitignore (.env / *.pem / .secrets/ / .auth_token).
Per-CEO directive 2026-04-16: every plugin and template repo should
gitignore credentials so self-hosters can't accidentally commit real
tokens to public repos.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 09:18:55 -07:00
Hongming Wang
653a531eab ci: add Go binary release workflow (tag v* → GitHub Releases) 2026-04-16 03:50:28 -07:00
Hongming Wang
2ec176bf57 feat: initial CLI (extracted from molecule-monorepo/platform/cmd/cli)
molecli — Go TUI dashboard for Molecule AI workspace monitoring.
Note: currently depends on platform Go packages; full API-only
decoupling tracked as follow-up.
2026-04-16 03:15:58 -07:00