@hagicode/cli 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,12 +5,12 @@
5
5
  [![license](https://img.shields.io/npm/l/%40hagicode%2Fcli?color=ffd43b)](https://www.npmjs.com/package/@hagicode/cli)
6
6
  [![Node.js](https://img.shields.io/badge/node-%3E%3D24.0.0-339933?logo=node.js&logoColor=white)](https://nodejs.org/)
7
7
 
8
- `@hagicode/cli` provides the published `hagi` command, which lets you drive a HagiCode backend from a terminal, scripts, or an AI agent without opening the app. Manage projects, proposals (generate, execute, archive, and send), vaults and their files, chat sessions, HagiTasks, scheduled plans, and skills (local list, enable, disable and uninstall, gallery search and install, and trusted providers). Every command supports a shared `--json` output for scripting, and an embedded Hagi skill under [`skills/`](skills/) guides agent workflows.
8
+ `@hagicode/cli` provides the published `hagi` command, which lets you drive a HagiCode backend from a terminal, scripts, or an AI agent without opening the app. Manage projects, proposals (generate, execute, archive, and send), local Sagas, vaults and their files, chat sessions, HagiTasks, scheduled plans, and skills (local list, enable, disable and uninstall, gallery search and install, and trusted providers). Every command supports a shared `--json` output for scripting, and an embedded Hagi skill under [`skills/`](skills/) guides agent workflows.
9
9
 
10
10
  ## Requirements
11
11
 
12
12
  - Node.js 24+
13
- - Access to a HagiCode backend API
13
+ - Access to a HagiCode backend API for backend-selected commands; Saga commands with `--path` work locally without one, except `saga session create`, which needs the backend to find the registered project
14
14
 
15
15
  ## Installation
16
16
 
@@ -76,9 +76,55 @@ Example:
76
76
  hagi --base-url https://api.example.com --token "$HAGI_TOKEN" project list --json
77
77
  ```
78
78
 
79
+ ## Saga Commands
80
+
81
+ Each Saga command requires exactly one project selector: `--path <directory>` or `--project-id <id>`. Missing, empty,
82
+ or conflicting selectors exit with code `2`. `--path` is resolved on the CLI host, relative to the current directory,
83
+ and does not read backend configuration or contact the backend; backend flags do not change local behavior. The one
84
+ exception is `saga session create`, which looks up the registered project through the backend (see below). Quote paths
85
+ that contain spaces.
86
+
87
+ ```bash
88
+ hagi saga init --path "./My Project"
89
+ hagi saga method run --json
90
+ hagi saga new demo --path "./My Project" --json
91
+ hagi saga list --path "./My Project"
92
+ hagi saga status demo --project-id <project-id> --json
93
+ hagi saga items demo --path "./My Project" --json
94
+ hagi saga claim demo --path "./My Project" --session orchestrator-1 --json
95
+ hagi saga check demo --path "./My Project" --phase run --json
96
+ hagi saga tick demo T1 --path "./My Project" --token "$SAGA_EXECUTION_TOKEN" --json
97
+ ```
98
+
99
+ Write and check commands (`claim`, `assert-owner`, `release`, `freeze`, `check`, `tick`, `block`, `set-status`, `amend`,
100
+ `archive`) run in the CLI itself. The CLI installs only the ignore rule and reads the six method prompts from its own package with `hagi saga method <name>`; there is no standalone Saga tool and no project-local copy of the methods. Mutating commands read the execution token
101
+ from `--token` or `SAGA_EXECUTION_TOKEN`; with `--project-id`, `--token` is rejected because it is the backend token, so use
102
+ `SAGA_EXECUTION_TOKEN` there. Exit code `0` means success, `1` a refusal or failed check, and `2` a usage error.
103
+
104
+ `saga session create --saga <id> --path <root> --title <title> --role implementation|verification --master <session-id> [--item <id>]`
105
+ creates one Worker chat session and records it in `runtime/sessions.md`. Unlike the other Saga commands it calls the
106
+ backend even with `--path`: the registered project whose real path matches `<root>` supplies the project ID, and no match
107
+ or several matches is a refusal. The execution token comes only from `SAGA_EXECUTION_TOKEN`; `--token` is refused with
108
+ either selector. The session carries `saga.*` metadata. The command sends no message and does not archive the session.
109
+ See the [Saga command reference](skills/hagi/references/saga.md#worker-sessions) for the options, metadata keys and exit codes.
110
+
111
+ With `--project-id`, the CLI uses the normal base URL, token, and language settings to resolve the project through the
112
+ backend. The returned project path must exist on the machine running the CLI; it is not a remote filesystem path.
113
+
114
+ `saga init` installs only the runtime ignore rule in `.hagicode/sagas/.gitignore`, merged additively, and creates no
115
+ `prompts/` directory. Existing project-local `prompts/` files are left untouched and are no longer read. `saga method <name>`
116
+ prints a published method (`goal`, `research`, `acceptance`, `plan`, `run`, `amend`) from the installed package, verified
117
+ against its manifest, with `--json` returning `{ name, sha256, content }`. `freeze` records the method digests in
118
+ `contract.lock`, and `check --phase run --json` reports method drift as a notice that never fails the check. Initialization
119
+ refuses redirected or unsafe targets and leaves identical resources untouched on repeat runs. See the
120
+ [Saga command reference](skills/hagi/references/saga.md) for outcomes, safety behavior, and JSON details.
121
+
122
+ For offline use, install the package or use a cached `npx` package. An uncached `npx @hagicode/cli` invocation may need
123
+ network access to download the package even though `saga ... --path` itself does not use a backend, except `saga session create`, which does.
124
+
79
125
  ## Connection Setup
80
126
 
81
- Every operation command resolves the backend base URL on each invocation, in this order:
127
+ Every backend operation resolves the base URL on each invocation, in this order:
82
128
 
83
129
  1. `--base-url <url>` flag
84
130
  2. `HAGI_API_BASE_URL` environment variable
@@ -86,11 +132,14 @@ Every operation command resolves the backend base URL on each invocation, in thi
86
132
 
87
133
  ```json
88
134
  {
89
- "baseUrl": "https://hagicode.example.com"
135
+ "baseUrl": "https://hagicode.example.com",
136
+ "steer": false
90
137
  }
91
138
  ```
92
139
 
93
- If none of the three is set, the command exits with code `2` and sends no backend request.
140
+ If none of the three is set, a backend operation exits with code `2` and sends no backend request. Saga commands using
141
+ `--path` bypass this resolution and do not read backend configuration, except `saga session create`, which resolves its project through the backend. The `steer` key is the default for `hagi chat send`,
142
+ `hagi proposal send`, and `hagi hagitask send` (see [Steer](#steer-send-option)); it must be `true` or `false`.
94
143
 
95
144
  Operation commands send requests without a credential by default. Pass `--token` (or set `HAGI_API_TOKEN`) only when your backend requires one.
96
145
 
@@ -111,16 +160,20 @@ hagi
111
160
  |-- project list | repositories | create | update | delete
112
161
  | `-- monospecs show | init | set | validate | reload | clone | remove-checkout
113
162
  |-- proposal list | create | optimize-description | generate | annotate | execute | archive | status | complete | send | get | messages
163
+ |-- saga init | new | list | status | items | claim | assert-owner | release | freeze | check | tick | block | set-status | amend | archive
114
164
  |-- vault list | create | update | delete
115
165
  | `-- files list | preview
116
166
  |-- chat list | create | archive | delete | send
117
167
  |-- hagitask list | schema | create | send | get | messages
118
168
  |-- plan list | get | create | update | enable | disable | delete | run
119
- `-- skills local list | enable | disable | uninstall
120
- gallery search | detail | install
121
- trusted list | upsert | delete | reset
169
+ |-- skills local list | enable | disable | uninstall
170
+ | gallery search | detail | install
171
+ | trusted list | upsert | delete | reset
172
+ `-- agent lock acquire | status | renew | heartbeat | release | requisition | list
122
173
  ```
123
174
 
175
+ The `send` commands of `chat`, `proposal`, and `hagitask` also take `--steer` and `--no-steer`, and `HAGI_SEND_STEER` sets the default. See [Steer](#steer-send-option).
176
+
124
177
  ## Example Setup
125
178
 
126
179
  The examples in the family sections run in order against a local backend. They assume:
@@ -243,6 +296,14 @@ hagi proposal status --session-id "$PROPOSAL_ID" --status Reviewing
243
296
  hagi proposal send --session-id "$PROPOSAL_ID" --content "Please continue"
244
297
  ```
245
298
 
299
+ Steer the message into the running turn, from the environment, or with a normal send that overrides the environment:
300
+
301
+ ```bash
302
+ hagi proposal send --session-id "$PROPOSAL_ID" --content "Please continue" --steer
303
+ HAGI_SEND_STEER=1 hagi proposal send --session-id "$PROPOSAL_ID" --content "Please continue"
304
+ hagi proposal send --session-id "$PROPOSAL_ID" --content "Please continue" --no-steer
305
+ ```
306
+
246
307
  The next steps are AI lifecycle actions and need a backend with an AI executor. `optimize-description` writes the optimized title, description, and proposal name to the session in one backend step, and `generate` needs that name. Without the AI executor, or before the name exists, the backend answers with a 400 that names the missing state:
247
308
 
248
309
  ```bash
@@ -288,6 +349,19 @@ The CLI may suggest a next command, but it never chains later lifecycle APIs aut
288
349
 
289
350
  Each command maps to one backend action. The CLI may suggest a next command, but it never chains later lifecycle APIs automatically.
290
351
 
352
+ `proposal create` takes the same `--metadata` and `--metadata-json` options as `chat create`, with the same rules and limits; see [Session Metadata](#session-metadata). A master marker and worker markers, for example:
353
+
354
+ ```bash
355
+ MASTER_ID=$(hagi chat create --title "Batch master" --project-id "$PROJECT_ID" --metadata role=master --metadata batch=2026-11-07 --json | json_field '.session.sessionId')
356
+ hagi proposal create --project-id "$PROJECT_ID" --chief-complaint "Repository scope
357
+ Writable repositories:
358
+ - none
359
+ Read-only repositories:
360
+ - none
361
+
362
+ Tighten the settings page copy" --default-title --metadata role=worker --metadata master="$MASTER_ID" --json
363
+ ```
364
+
291
365
  ## Vault Commands
292
366
 
293
367
  Create a vault with a local folder to browse. The new id is read from `vault.id`:
@@ -352,10 +426,31 @@ Send a message, archive, and delete:
352
426
 
353
427
  ```bash
354
428
  hagi chat send --session-id "$CHAT_ID" --content "Summarize the current diff"
429
+ hagi chat send --session-id "$CHAT_ID" --content "Summarize the current diff" --steer
430
+ HAGI_SEND_STEER=1 hagi chat send --session-id "$CHAT_ID" --content "Summarize the current diff"
431
+ hagi chat send --session-id "$CHAT_ID" --content "Summarize the current diff" --no-steer
355
432
  hagi chat archive --session-id "$CHAT_ID"
356
433
  hagi chat delete --session-id "$CHAT_ID"
357
434
  ```
358
435
 
436
+ ### Session Metadata
437
+
438
+ `chat create` and `proposal create` take write-once metadata entries, for example the role markers an orchestrator uses to find its sessions again:
439
+
440
+ ```bash
441
+ hagi chat create --title "Master" --project-id "$PROJECT_ID" --metadata role=master --metadata batch=2026-11-07 --json
442
+ hagi chat create --title "Worker 1" --project-id "$PROJECT_ID" --metadata role=worker --metadata master="$MASTER_ID" --json
443
+ printf '{"role":"worker","master":"%s"}' "$MASTER_ID" | hagi chat create --title "Worker 2" --project-id "$PROJECT_ID" --metadata-json - --json
444
+ ```
445
+
446
+ - `--metadata key=value` is repeatable. The text is split at the first `=`, so a value may contain `=`.
447
+ - `--metadata-json <file|->` reads a JSON object of string values from a file, or from stdin with `-`. It is repeatable.
448
+ - A key may appear once across both options. A missing `=`, an empty key, a repeated key, or JSON that is not an object of string values is a usage error: the command exits `2` and sends no request.
449
+ - Limits, checked by the backend: at most 16 entries; a key of 1 to 64 characters matching `^[a-z][a-z0-9._-]{0,63}$`; a value of 1 to 512 characters with no control characters. A rejection exits with the backend's message as it is, and the session is not created.
450
+ - Metadata is write-once: it is set when the session is created and cannot be changed later. Every reader of the session can see it, so do not put secrets in it.
451
+ - The entries come back as `userMetadata` in the `--json` output of `chat create`, `chat list`, `proposal create` and `proposal get`. The human output adds one `userMetadata` line, keys sorted, when there are entries. A session without metadata has no `userMetadata` field.
452
+
453
+
359
454
  ## HagiTask Commands
360
455
 
361
456
  A HagiTask is a preset task with a structured input. Read its schema first, write the input from the schema, then create the session. `hagitask list` shows the active tasks:
@@ -418,6 +513,14 @@ Send a follow-up message to the HagiTask session:
418
513
  hagi hagitask send --session-id "$HAGITASK_ID" --content "Retry with a smaller diff"
419
514
  ```
420
515
 
516
+ Steer the message into the running task, from the environment, or with a normal send that overrides the environment:
517
+
518
+ ```bash
519
+ hagi hagitask send --session-id "$HAGITASK_ID" --content "Retry with a smaller diff" --steer
520
+ HAGI_SEND_STEER=1 hagi hagitask send --session-id "$HAGITASK_ID" --content "Retry with a smaller diff"
521
+ hagi hagitask send --session-id "$HAGITASK_ID" --content "Retry with a smaller diff" --no-steer
522
+ ```
523
+
421
524
  Read one HagiTask session, or wait until its status settles, and read its messages:
422
525
 
423
526
  ```bash
@@ -626,6 +729,78 @@ hagi skills trusted delete --provider-id example-provider
626
729
  hagi skills trusted reset
627
730
  ```
628
731
 
732
+ ## Agent Lock Commands
733
+
734
+ `hagi agent lock` lets several agents take turns on one shared resource, such as a game server that only one agent may use at a time. The backend keeps a fair queue per lock; the lock is cooperative and cannot stop an action already running on the remote resource. There is no top-level `hagi lock`. The agent-facing protocol is in [`skills/hagi/references/resource-lock.md`](skills/hagi/references/resource-lock.md).
735
+
736
+ ```bash
737
+ hagi agent lock acquire --lock-id eu-1/duckov --owner sess-7f2c
738
+ hagi agent lock acquire --server eu-1 --game duckov --owner sess-7f2c --wait --timeout 900
739
+ hagi agent lock status --lock-id eu-1/duckov --owner sess-7f2c --json
740
+ hagi agent lock heartbeat --lock-id eu-1/duckov --owner sess-7f2c --epoch 7
741
+ hagi agent lock release --lock-id eu-1/duckov --owner sess-7f2c --epoch 7
742
+ hagi agent lock requisition --lock-id eu-1/duckov --owner sess-7f2c --reason "User order: take the lock now"
743
+ hagi agent lock list --prefix eu-1/
744
+ ```
745
+
746
+ - Lock: `--lock-id <id>` (one to four `/`-separated segments) or `--server <name> --game <name>`, which means `<server>/<game>`. Giving both forms, or only one of `--server` and `--game`, exits `2` before any request.
747
+ - Owner: `--owner <id>`, then `HAGI_LOCK_OWNER`, then `HAGI_SESSION_ID`. An agent passes its own session ID. The CLI never invents or stores an owner ID; with none set it exits `2`.
748
+ - `acquire` takes the lock or joins the queue and exits `0` for `Holding` and `Queued`. `acquire --wait` long-polls until the owner holds the lock (exit `0`), the `--timeout` passes (default 900 seconds, exit `31`, the queue entry it created is released), or the entry is dropped (exit `32`). Ctrl-C releases the entry it created and never a lock held before. `acquire` has no force option.
749
+ - `status` reads one owner's state (`Holding`, `Queued`, `Expired`, `Requisitioned`, `None`) and exits `0` for all of them; `--wait-seconds` long-polls. `renew` sends one renewal. `release` exits `0` whether or not anything was released. `list` shows the busy locks; `--prefix` filters them.
750
+ - `heartbeat` renews at once and then every `--interval` seconds (default 120), is quiet on success (`--verbose` prints each beat), prints a line and exits `32` when the lock is lost, retries transport errors with backoff and never reports them as loss. SIGINT or SIGTERM ends it with exit `0` and does not release. Run it under a Monitor or another watcher.
751
+ - `requisition` requires `--reason` (1 to 200 characters), takes the lock at once, and displaces the holder, who is told by its next heartbeat. Use it only when a user explicitly ordered a forced take.
752
+ - Answers show the valid-until time, the 24-hour hard cap, the check-in window and, for a queued owner, the turn window.
753
+
754
+ | Timing | Default |
755
+ | --- | --- |
756
+ | Feedback timeout (no heartbeat releases the lock) | 10 minutes |
757
+ | Maximum hold | 24 hours |
758
+ | Heartbeat interval | 2 minutes |
759
+ | Claim window after a handoff | 30 seconds |
760
+ | Waiter TTL | 60 seconds |
761
+ | `acquire --wait` timeout | 900 seconds |
762
+
763
+ Agent loop: `acquire`, read the windows, start `heartbeat` under a watcher before any work (or, when queued, a continuous `acquire --wait` watcher), stop the heartbeat when done and then `release`, and stop using the resource as soon as a command exits `32`. After losing track, repeat `acquire` with the same owner; it is safe.
764
+
765
+ ## Steer (send option)
766
+
767
+ `hagi chat send`, `hagi proposal send`, and `hagi hagitask send` deliver a message in one of two ways:
768
+
769
+ - **Normal send** (default): the message waits behind the running turn, as before.
770
+ - **Steer**: the message is inserted into the running turn through the backend's `SteerSession` RPC. The CLI makes one call.
771
+
772
+ Steer is off unless you ask for it. The first source that is set decides, in this order:
773
+
774
+ 1. `--steer` (on) or `--no-steer` (off) on the command.
775
+ 2. `HAGI_SEND_STEER`: `1`, `true`, `yes`, `on` turn Steer on; `0`, `false`, `no`, `off` turn it off. Case and surrounding spaces are ignored. Any other value exits with code `2` and sends no request.
776
+ 3. `"steer": true` or `"steer": false` in the config file. Any other type exits with code `2` and names the file.
777
+ 4. The default, `false`.
778
+
779
+ A lower source is not read once a higher one is set, so `--no-steer` works even when `HAGI_SEND_STEER` holds a value the CLI does not recognize.
780
+
781
+ ### Outcomes
782
+
783
+ - `steered`: a running task took the message. It does not guarantee the text was used: the backend can return a steered message to the queue when the run ends.
784
+ - `queued`: no running task took the message, so it was sent as the next message. This is a success and exits with code `0`. The backend also answers `queued` for an idle session, where the message starts a run at once.
785
+
786
+ Human output adds `steer`, `effective`, `outcome`, and `messageId` rows, and a line saying no running task took the message for `queued`. `--json` keeps `sessionId`, `message`, and `requested`, and adds a `steer` object: `{ requested, source, effective, outcome?, messageId?, reason? }`. `source` is `flag`, `env`, `config`, or `default`. `reason` appears only when Steer was requested but not used. On the Steer path `message` is `Steered` or `Queued`.
787
+
788
+ ### Conflicts and fallback
789
+
790
+ Steer carries text only, so it cannot be combined with `--mode` or `--executor-type`:
791
+
792
+ - `--steer` with either option exits with code `2` and sends nothing.
793
+ - When Steer comes only from `HAGI_SEND_STEER` or the config file, the command sends a normal message with the options and records the reason in `steer.reason`.
794
+ - When Steer comes only from `HAGI_SEND_STEER` or the config file and the backend does not implement `SteerSession`, the command sends a normal message and records the reason `the backend does not support Steer`. An explicit `--steer` reports the backend error instead.
795
+
796
+ A queued Steer is always sent in `Edit` mode. A session that must keep another mode should send with `--no-steer --mode <mode>`.
797
+
798
+ Steer works with the executors the backend supports. For any other executor, the backend answers `queued`.
799
+
800
+ ### Unattended messaging
801
+
802
+ For session-to-session messages sent by an orchestrator without a person watching, set `HAGI_SEND_STEER=1` in its environment. Add `--no-steer` to a single call that must go through the normal queue.
803
+
629
804
  ## Environment Variables
630
805
 
631
806
  The CLI resolves runtime settings centrally before every command. Explicit flags always override environment variables.
@@ -635,6 +810,7 @@ The CLI resolves runtime settings centrally before every command. Explicit flags
635
810
  | `HAGI_API_BASE_URL` | Backend base URL, used when `--base-url` is not given. Overrides the config file. Required when neither is set. |
636
811
  | `HAGI_API_TOKEN` | Bearer token attached as `Authorization: Bearer <token>`. |
637
812
  | `HAGI_API_LANG` | Shared `Accept-Language` header value. |
813
+ | `HAGI_SEND_STEER` | Turns Steer on (`1`, `true`, `yes`, `on`) or off (`0`, `false`, `no`, `off`) for the send commands, ignoring case and surrounding spaces. Any other value exits with code `2`. Overridden by `--steer` / `--no-steer`. Order: flag, then this variable, then the `steer` config key, then `false`. |
638
814
 
639
815
  ## Backend Transport
640
816
 
@@ -655,7 +831,8 @@ Stable non-zero exits used by the CLI. The HTTP numbers are the equivalents of t
655
831
  | `24` | HTTP 404 |
656
832
  | `29` | HTTP 409 |
657
833
  | `30` | Operation failed: the backend returned a refusal, for example `skills local enable` or `disable` on a name that is not a built-in skill, an invalid MonoSpecs configuration from `project monospecs validate`, or a failed clone from `project monospecs clone` or `set --clone` |
658
- | `31` | Wait timed out: `--wait` on `proposal get` or `hagitask get` reached `--timeout` before the session settled. The last status is printed. Not a success, and not a backend error |
834
+ | `31` | Wait timed out: `--wait` on `proposal get`, `hagitask get` or `agent lock acquire` reached `--timeout` before the session settled. The last status is printed. Not a success, and not a backend error |
835
+ | `32` | Lock lost: a `hagi agent lock` command learned that the owner no longer holds the lock (expired, taken back at the 24-hour cap, stale epoch, requisitioned). Stop using the resource. Distinct from `31` |
659
836
  | `40` | Other HTTP 4xx |
660
837
  | `50` | HTTP 5xx |
661
838
  | `60` | Transport or connection failure |