# Repository Guidelines ## Project Structure & Module Organization This is a Bun + TypeScript toolkit for the `seal-use` CLI and optional MCP server. Entry points are `src/cli.ts`, `src/server.ts`, and `src/probe.ts`. Shared config, auth, token, and HTTP code is under `src/core/`. Seal-specific API and tool logic is under `src/domains/seal/`. The repository doubles as a ZCode plugin: `.zcode-plugin/plugin.json` declares the plugin manifest, `.mcp.json` declares the optional MCP server (disabled by default), and the agent skill lives under `skills/seal-use/`. The in-repo `.agents/skills/seal-use` symlink has been dropped; workspace and Codex skill loading use the global `~/.agents/skills/seal-use` copy installed by `seal-use skill install`. Tests are colocated as `*.test.ts` files in `src/`. OpenAPI source YAML lives in `openapi/`; bundled output is `dist/openapi.json`. Example enterprise configs are in `enterprises/example.*.json`; real configs must stay untracked. ## Build, Test, and Development Commands - `bun install`: install dependencies. - `bun run dev`: run `src/server.ts` with watch mode. - `bun run start`: run the MCP server once. - `bun run cli -- `: execute CLI commands, such as `bun run cli version`. - `bun run probe -- `: run the local connectivity/config probe. - `bun run check`: type-check with `tsc --noEmit`. - `bun test`: run all colocated Bun tests. - `bun run bump `: bump version across all manifest/skill/openapi files and bundle. - `bun run openapi:bundle`: rebuild `dist/openapi.json`. - `bun run build:plugin`: bundle the MCP server to `dist/mcp/server.js` (tracked, used by `.mcp.json`). - `bun run dev:override`: compile the host binary with Git dev version metadata, macOS sign, atomically override the Mac global command (`/opt/homebrew/bin/seal-use`), and sync the local Agent Skill. > **Pre-Push Bundle Gates**: Modifying `src/` files imported by the MCP server requires running `bun run build:plugin` to keep `dist/mcp/server.js` in sync; modifying `openapi/` requires `bun run openapi:bundle` to keep `dist/openapi.json` in sync. CI strictly checks for zero diff on both tracked bundles. ## Coding Style & Naming Conventions Use TypeScript ES modules with explicit `.js` import extensions for local files. Validate external data with Zod or narrow types before use. Follow existing style: two-space indentation, double quotes, omitted trailing semicolons, `camelCase` variables/functions, `PascalCase` classes/types, and lowercase hyphenated filenames such as `token-store.ts`. Prefer small domain functions over broad utility modules. ## CLI & Agent Harness Protocol Guidelines - **Machine Channel (`stdout`)**: Data commands MUST output clean, parseable JSON on `stdout`. Never dump human prose, raw logs, or progress banners into `stdout`. The Agent Harness parses `stdout` directly into typed payloads. - **Documented exception — document-output commands**: `seal-use --help` / `seal-use help ` (`src/cli/printer.ts`) and `seal-use doctor guide` (`src/cli/commands/doctor.ts`) intentionally write Markdown to `stdout`, because their output *is* the payload the caller redirects or reads (e.g. `seal-use doctor guide > guide.md`). Never add a new prose-on-stdout command outside this list: return structured JSON and put the readable rendering on `stderr` instead. `src/probe.ts` is a local developer probe script rather than a `bin` entry point, so its `[probe] …` lines are outside this contract. - **Guidance & Diagnostic Channel (`stderr`)**: Use `stderr` for runtime logs, network probing status, self-healing notifications, and explicit **Next-Step Command Suggestions** (e.g. `👉 Next action: seal-use deploy hosecloud sync `). - **Performance Contract & SWR Architecture**: Read-only discovery and version commands (`seal-use version`, `seal-use doctor`, `seal-use corps list`) MUST execute instantaneously (<10ms). Never block on external network I/O during routine invocations. Use the Stale-While-Revalidate (SWR) pattern: read local cache from `~/.config/seal-use/*.json` synchronously (<0.2ms), and refresh stale/missing data via detached, unref'd child processes (`triggerBackgroundUpdateCheck`, `unref()`). - **Safety Guardrail & Mutation Preview (`--dry-run`)**: All potentially destructive or state-mutating commands (e.g. `rules push/clear`, `corps sync-test`, `preference update`, `webhook add/update`) MUST support `--dry-run` returning structured JSON previews with `preview: true` and a clean diff, without executing mutations. Live execution requires explicit `--yes` or `--confirm`. - **Exit Code Contract**: - `0`: Successful execution. - `1`: Upstream API failure or domain business block. - `2`: Parameter validation failure, unknown subcommand, or invalid JSON. - `10`: Safety gate block requiring user confirmation (`--confirm` / `--yes`). - Never call `process.exit()` deep in domain functions; throw structured errors and let `src/cli/runner.ts` handle stream routing and exit code assignment. ## Testing Guidelines Use `bun:test` with `describe`, `test`, and `expect`. Place tests next to covered code using `name.test.ts`, for example `src/core/config/loader.test.ts`. Tests that change environment variables or working directories must restore them in `afterEach`. Ensure background tasks are guarded with `SEAL_DISABLE_BG_UPDATE=1` or `NODE_ENV=test` during tests. Detailed test hygiene guidelines are in `.agents/rules/testing.md`. ## Commit & Pull Request Guidelines Recent commits use concise imperative subjects, often `Add ...` or `Refine ...`, such as `Add compact approval rule listing`. Keep subjects under roughly 72 characters. Specific workflows and domain guidelines are maintained in `.agents/rules/`: - Issue triage & PR workflow (No premature release): `.agents/rules/issue-pr-workflow.md` - CLI stream & exit code protocol: `.agents/rules/cli-protocol.md` - Testing conventions & isolation: `.agents/rules/testing.md` - TypeScript & ESM conventions: `.agents/rules/typescript-style.md` - Staging & test sandbox regression: `.agents/rules/staging-simulation.md` - Webhook material pack & attachment policy: `.agents/rules/material-pack-policy.md` ## Release & Publishing Guidelines 1. **Automated Release Command (Preferred)**: - Run `bun run release ` (supports `--dry-run`). - This script automatically encapsulates the full 14-step protected-branch release lifecycle: 1. Pre-flight cleanliness & `gh` authentication checks. 2. Manifest version bumping (`bump-version.ts`) & automatic `CHANGELOG.md` entry generation. 3. Local validation (`bun test && bun run check`). 4. Release branch creation (`codex/release-vX.Y.Z`), commit, and push. 5. GitHub PR creation, CI monitoring, and squash/merge. 6. Main branch fast-forward & Gitea (`home/main`) mirror synchronization. 7. Annotated tag creation (`vX.Y.Z`) and dual-remote push (`home` then `origin`). 8. Clean removal of temporary release branch. 2. **Manual Protected-Branch Release & Workspace Isolation (Reference)**: - **Isolation Rule**: If the main working directory contains uncommitted changes or another branch is actively being edited, NEVER switch branches or stash in the shared repository. Instead, use an isolated worktree for release operations: ```bash git worktree add -b codex/release-vX.Y.Z /tmp/seal-use-release main cd /tmp/seal-use-release # Execute bump, commit, push, PR, merge, tag here # When finished, remove the worktree: git worktree remove /tmp/seal-use-release ``` - Do not tag or push `main` directly. Create a release branch, commit the version bump, push it, open a PR to `main`, wait for the required CI check, and merge the PR first. - After the PR is merged, fast-forward the local `main`, then synchronize `home/main` before creating the tag: ```bash git switch -c codex/release-vX.Y.Z git add git commit -m "Release vX.Y.Z: ..." git push origin HEAD:refs/heads/codex/release-vX.Y.Z gh pr create --base main --head codex/release-vX.Y.Z --title "Release vX.Y.Z: ..." --body "..." gh pr checks --watch gh pr merge --merge git switch main git pull --ff-only origin main git push home main ``` - Only after both `origin/main` and `home/main` contain the merged release should you create the annotated tag. Push the tag to `home` first, then to `origin`; pushing the GitHub tag last triggers the Release workflow after the Gitea tag and branch are ready: ```bash git tag -a vX.Y.Z -m "🚀 Release summary text describing key changes" git push home refs/tags/vX.Y.Z git push origin refs/tags/vX.Y.Z ``` - GitHub Actions will automatically extract the tag message, append commit notes via `--generate-notes`, cross-compile all 5 target binaries, and publish GitHub and Gitea releases. Wait for the workflow, then verify that both releases point to the intended tag commit and contain every expected asset exactly once. - The workflow enforces the merge-before-tag rule rather than trusting the operator: `release.yml` refuses to publish unless the tagged commit is an ancestor of `origin/main`, and the Gitea mirror job runs only after a successful publish. Actions in both workflows are pinned to commit SHAs rather than moving tags for supply-chain integrity. ## Security & Configuration Tips Never commit real enterprise credentials, Seal tokens, service logs, or local generated state. Use `enterprises/example.hose.json` or `enterprises/example.direct.json` as templates, then place real configs in `enterprises/local.json` or `~/.config/seal-use/enterprises` (with legacy fallback to `~/.config/seal-home/enterprises`). For auth or tenant changes, verify with `bun run probe` or `seal-use auth diagnose`. Credential files are written `0600`; a config placed by hand keeps the `0o644` default, so run `seal-use doctor` and act on its `credential_permissions` check before sharing a machine or a backup. See `SECURITY.md` for the trust model (local HTTP adapter, self-update, custom linters). ## Agent-Specific Instructions Do not create `.codex/skills`; Codex is compatible with `.agents/skills`, and this project treats `.agents/` as authoritative. At project scope, prefer `AGENT.md` instructions when present.