@ancha-labs/cli 0.1.6 → 0.2.1
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/lib/ancha.mjs +12151 -7451
- package/lib/assets/SKILL.md +14 -0
- package/lib/assets/docs/apps.md +90 -0
- package/lib/assets/docs/cloud.md +1 -1
- package/lib/assets/docs/integrations.md +2 -0
- package/lib/assets/docs/run.md +2 -0
- package/lib/assets/docs/setup.md +3 -1
- package/lib/assets/harbor/companion.mjs +2370 -1759
- package/lib/assets/ui/app.js +1 -1
- package/lib/clock-preload.mjs +36 -0
- package/lib/mcp-entry.mjs +56 -12
- package/lib/worker.mjs +2703 -2033
- package/lib/world-server.mjs +2354 -1743
- package/package.json +1 -1
package/lib/assets/SKILL.md
CHANGED
|
@@ -25,6 +25,20 @@ Add `--json` to every command. Runs print NDJSON events that end with `run_finis
|
|
|
25
25
|
|
|
26
26
|
The starter scene only proves that the agent reaches each tool. For the agent's real job, `ancha test new "<the test in one sentence>" --json` drafts a test with people for the project's world (`ancha init --intent "<company and scenario>"` makes the world and a first test). Review its checks before you run it. To write a seed yourself, `ancha twins seed <tool> --json` prints the tool's fields, an example that validates as written, and the checks a scene can make on it. More: `ancha docs scenes`.
|
|
27
27
|
|
|
28
|
+
## An app, not a command
|
|
29
|
+
|
|
30
|
+
If the AI feature lives inside an app (a web app or API server with its database, workers and scheduled jobs), don't wrap it in `agent.command`. Ancha starts the app once per trial with `agent.start`, waits for `agent.ready_url`, delivers the tools' events to `agent.url`, and a scene's `world_rules.app_calls` call the app's own routes, such as its cron jobs, at a time. Read `ancha docs apps` before writing `ancha.yaml`. The start script gives the app a clean database, cache and seed every trial: a new world has new URLs and tokens, and stale rows or sync cursors make an app silently do nothing.
|
|
31
|
+
|
|
32
|
+
## From a production failure
|
|
33
|
+
|
|
34
|
+
A production run that went wrong becomes a test here. With a trace file (an export from the user's tracing):
|
|
35
|
+
|
|
36
|
+
1. `ancha import --offline <trace file> --json` makes the test on this computer and uploads nothing. It writes `scenes/from-traces/<name>/scene.yaml` and lists the questions one trace cannot settle, each with the default the test uses.
|
|
37
|
+
2. Ask the user for the answers; don't guess them. Pass them with `--answer <question>=<value>` and import again with `--force` (`ancha docs apps`).
|
|
38
|
+
3. `ancha run` on the `scene.yaml` against the version that failed in production should fail the same way. That is where the loop above starts. If it doesn't, `ancha traces diff <trace file> latest` shows the calls production made and the trial didn't; the import also lists the app's own setup calls, which the start script or an `app_call` has to make.
|
|
39
|
+
|
|
40
|
+
If the user's workspace takes traces (their agents send them to it), `ancha traces --failed --json` lists the ones where production failed a check, `ancha traces show <id> --json` previews the test, and `ancha pull <id> --json` saves it, with `--answer` for its questions. Checks you add to a pulled `scene.yaml` stay when you pull a newer version; any other edit stops the pull. A workspace that doesn't take traces says so; use the file.
|
|
41
|
+
|
|
28
42
|
## The rule
|
|
29
43
|
|
|
30
44
|
Fix the agent, not `ancha.yaml`, the scenes or their checks. The exception is a check that is wrong for the real API, such as a method the official SDK never sends. Then don't quietly edit it: tell the user, show the evidence (the call from `ancha explain`, the vendor's documentation), and change it only with their agreement.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Test an app
|
|
2
|
+
|
|
3
|
+
Use this when the AI feature lives inside an app (a web app, an API server and its workers) rather than in one command. `ancha run` starts the app once per trial against a fresh world. The world drives it: people send mail and messages, the app's own scheduled routes are called, and time passes. The checks read the world's end state and the calls the app made.
|
|
4
|
+
|
|
5
|
+
## ancha.yaml
|
|
6
|
+
|
|
7
|
+
```yaml
|
|
8
|
+
agent:
|
|
9
|
+
start: ./ancha/start.sh # once per trial; it starts the app on $ANCHA_AGENT_PORT
|
|
10
|
+
url: "http://127.0.0.1:{{agent.port}}/api/slack/events" # where the app receives the tools' events (webhooks, push)
|
|
11
|
+
ready_url: "http://127.0.0.1:{{agent.port}}/api/health" # answers 2xx once the app is up
|
|
12
|
+
startup_timeout: 180s # the start script's whole budget: reset, seed, boot
|
|
13
|
+
idle: 20s # how long the app may look quiet while it still works (Ancha can't see its model calls); skip_idle jumps the clock only after this much quiet
|
|
14
|
+
setup_calls: true # the app may call the world while it starts (subscriptions, lookups), before time starts
|
|
15
|
+
clock: node # Date in the app's Node processes follows the world's clock, for flows that wait days
|
|
16
|
+
env: # rendered per trial: {{slack.url}}, {{slack.token}}, {{gmail.url}}, {{gmail.token}} …
|
|
17
|
+
APP_SLACK_TOKEN: "{{slack.token}}"
|
|
18
|
+
scenes: [ancha/scenes/*.yaml]
|
|
19
|
+
trials: 3
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`ancha run --dry-run` checks this file and every scene and starts nothing.
|
|
23
|
+
|
|
24
|
+
- `{{agent.port}}` is reserved for the trial, so the health route and the event routes need nothing Ancha-specific.
|
|
25
|
+
- `clock: node` needs `start`. Apps in other runtimes keep real time: give them short thresholds in their seeded settings instead.
|
|
26
|
+
|
|
27
|
+
## The start script
|
|
28
|
+
|
|
29
|
+
- From your shell it gets only a base set (`PATH`, `HOME`, the locale and the like). It also gets Ancha's variables (`ANCHA_AGENT_PORT`, `ANCHA_TRIAL` …), the variables of every tool in the scene (`ancha connect <tool> --json` lists them) and `agent.env`. Set database and cache URLs and the app's test secrets in the script. A real key, such as the app's model key, goes in `agent.pass_env` by name (or `ancha run --pass-env NAME`): its value stays in your shell, not in a script you commit.
|
|
30
|
+
- Every trial is a new world with new URLs and tokens, so every trial starts the app clean:
|
|
31
|
+
1. restore the database from a template copy (`CREATE DATABASE app_trial TEMPLATE app_base`, with `app_base` migrated once);
|
|
32
|
+
2. flush caches and queues the tests own;
|
|
33
|
+
3. seed what the flow needs with the app's own seeds, factories or test helpers, storing the world's token where the app keeps its OAuth tokens;
|
|
34
|
+
4. `exec` the server. Start workers (job queues) from the same script; Ancha stops the whole process group after the trial.
|
|
35
|
+
- Old rows, caches and sync cursors (a stored history id, a last-seen timestamp) make an app silently do nothing in a new world.
|
|
36
|
+
- A world's ids repeat from trial to trial, as its seed does. Clear the app's dedupe keys and locks (keyed by message or event ids) at the start of every trial, and run trials one at a time (`parallel: 1`) unless each trial gets its own database and cache namespace.
|
|
37
|
+
- Seed what a real account has, through the app's own functions: the rules and settings its onboarding or settings page creates, not rows a test helper writes by hand. A helper's shortcut (a missing default rule, a rule without the id the settings page would store) changes what the app does.
|
|
38
|
+
- A production build boots faster than a development server, and boot time counts against `startup_timeout`. `.ancha/runs/<run>/trial-*/agent.log` has the script's and the app's output.
|
|
39
|
+
|
|
40
|
+
## Point the app at the tools
|
|
41
|
+
|
|
42
|
+
- Use the app's base-URL setting if it has one; `ancha connect <tool> --lang ts|py` prints the SDK settings. If the SDK has none, add a development-only initializer that reads the tool's variable and does nothing when it is unset. Keep app changes that small and say why in a comment.
|
|
43
|
+
- Google clients keep only the host of a `rootUrl` set on the client; Ancha routes those calls by their token.
|
|
44
|
+
- Gmail reaches an app the way it does in production: the app calls `users.watch`, and the world then pushes Pub/Sub notifications to `agent.url`. If the app registers its watch on a schedule, call that route with an `app_call` early in the scene. The world gives `GMAIL_API_URL`, `GMAIL_ACCESS_TOKEN` and `GMAIL_ADDRESS`, the mailbox's address (`{{gmail.url}}`, `{{gmail.token}}`, `{{gmail.address}}`); `GET /gmail/v1/users/me/profile` answers the same address, as in Gmail. `ancha connect gmail --lang ts` prints the client settings.
|
|
45
|
+
- Identifiers a scene relies on (an inbox token, a cron secret, an API key the world sends) should be fixed values your seed creates, because a scene is static.
|
|
46
|
+
|
|
47
|
+
## Scenes for an app
|
|
48
|
+
|
|
49
|
+
```yaml
|
|
50
|
+
id: stale-question
|
|
51
|
+
title: A customer's question gets an answer in the thread, and a reminder two days later if nobody resolved it
|
|
52
|
+
twins: [slack]
|
|
53
|
+
start_time: 2026-11-02T15:00:00Z # a Monday, for apps that count business days
|
|
54
|
+
timeout: 3d
|
|
55
|
+
skip_idle: true # jump the clock while the app and the world are quiet
|
|
56
|
+
seed:
|
|
57
|
+
slack:
|
|
58
|
+
users:
|
|
59
|
+
- { ref: bot, name: support-bot, is_bot: true }
|
|
60
|
+
- { ref: dan, name: dan, real_name: Dan Ito }
|
|
61
|
+
channels:
|
|
62
|
+
- { ref: support, name: support, members: [$bot, $dan] }
|
|
63
|
+
world_rules:
|
|
64
|
+
actors: # people acting in a tool at a time: slack.post, gmail.send, github.comment …
|
|
65
|
+
- at: 5s
|
|
66
|
+
slack.post: { ref: question, channel: $support, user: $dan, text: "How do I export my invoices?" }
|
|
67
|
+
app_calls: # the world calling the app: its cron routes, or a customer message into its API
|
|
68
|
+
- at: 2d1h
|
|
69
|
+
path: /api/cron/stale-questions
|
|
70
|
+
headers: { authorization: Bearer test-cron-secret }
|
|
71
|
+
checks:
|
|
72
|
+
- id: answered_in_thread
|
|
73
|
+
state: { slack.message: { channel: $support, from: $bot, in_thread_of: $question } }
|
|
74
|
+
- id: no_deletes
|
|
75
|
+
trace: { not_called: "* /api/chat.delete" }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
- `app_calls` take `at`, `method` (default GET), `path`, `headers` and `body`; they go to the origin of `ready_url`.
|
|
79
|
+
- `gmail.send` takes `from`, `to`, `cc`, `subject`, `body`, `in_reply_to` and `ref`. The `gmail.thread` check takes `of` (a message ref), `label` with `present`, `drafts_gte`, `drafts_lte` and `messages_gte`.
|
|
80
|
+
- People who answer like a person go in the scene's `people:` (`ancha schema scene` lists every field). One with a Gmail account reads the mail delivered to that mailbox, which must be in `seed.gmail.mailboxes`, and replies in the thread: `{ ref: tom, name: Tom, role: a client who answers email himself within a day or two, accounts: { gmail: tom@client.example }, replies_after: 1d..2d }`. People call a model with your `OPENROUTER_API_KEY`, or `ANCHA_PEOPLE_BASE_URL` and `ANCHA_PEOPLE_API_KEY` (`ancha docs cloud`). `ancha twins seed <tool> --json` prints each tool's seed fields and checks.
|
|
81
|
+
- Make every check able to fail. Run a scene once against a build with the feature broken, or also check the state in between (the label the app should have removed). A check can pass for the wrong reason: a reminder that never comes because the scheduled call ran too early proves nothing.
|
|
82
|
+
|
|
83
|
+
## From a production trace, on this computer
|
|
84
|
+
|
|
85
|
+
1. `ancha import --offline <trace file> --json` reads an Ancha Trace IR file, or an OTLP JSON export whose spans carry the bodies of the app's calls to its tools. It writes `scenes/from-traces/trace-<id>/scene.yaml` and `provenance.json`; nothing is uploaded.
|
|
86
|
+
2. It lists the questions one trace cannot settle, each with the default the test uses. Examples: should a label the app added still be on the thread at the end, and what header do the app's scheduled calls carry? Ask the user, then import again with `--answer <id>=<value>` for each and `--force`.
|
|
87
|
+
3. Run the scene against the build that failed in production. It should fail the way production did. Then fix the app and run again.
|
|
88
|
+
|
|
89
|
+
- A trace holds the app's calls, not what started each one. The import lists the calls production made before the first event the test replays: the app's own setup, such as a watch or its labels. The test has them only if the app makes them when it starts, or the start script or an `app_call` does.
|
|
90
|
+
- When a test does not fail the way production did, `ancha traces diff <trace file> <run>/<trial>` lists the calls only production made, the ones only the trial made, and the first call where their orders part. A call production made and the trial didn't (labels created at setup, a settings read) usually points at what the seed lacks.
|
package/lib/assets/docs/cloud.md
CHANGED
|
@@ -35,7 +35,7 @@ ancha runs upload latest # a run that already finished (also a run i
|
|
|
35
35
|
|
|
36
36
|
## People and world generation
|
|
37
37
|
|
|
38
|
-
The people in a world and `ancha world gen` call a language model. With `OPENROUTER_API_KEY` set they use your key. Logged in to a workspace without one, they use the workspace's model credit through its relay (`https://app.ancha.io/t/<workspace>/_relay/v1`, authorized by the workspace's API key; your own key never goes there). Those calls go through the hosted workspace and wake its machine, which can take up to a minute after it sleeps. The relay serves its own model list, takes requests up to 256 KB and about 120 a minute per workspace, and says when the workspace is out of credit or has model calls turned off; the error names the next step, and setting `OPENROUTER_API_KEY` always works.
|
|
38
|
+
The people in a world and `ancha world gen` call a language model. With `OPENROUTER_API_KEY` set they use your key. For another OpenAI-compatible endpoint, set `ANCHA_PEOPLE_BASE_URL` (ending in `/v1`) and `ANCHA_PEOPLE_API_KEY` instead; it must serve OpenRouter's model names. Logged in to a workspace without one, they use the workspace's model credit through its relay (`https://app.ancha.io/t/<workspace>/_relay/v1`, authorized by the workspace's API key; your own key never goes there). Those calls go through the hosted workspace and wake its machine, which can take up to a minute after it sleeps. The relay serves its own model list, takes requests up to 256 KB and about 120 a minute per workspace, and says when the workspace is out of credit or has model calls turned off; the error names the next step, and setting `OPENROUTER_API_KEY` always works.
|
|
39
39
|
|
|
40
40
|
## CI
|
|
41
41
|
|
|
@@ -8,4 +8,6 @@ Slack WebClient uses slackApiUrl. With Bolt Socket Mode configure both App clien
|
|
|
8
8
|
|
|
9
9
|
PagerDuty PDJS requires a full local url for every REST request; its server option assumes production HTTPS. Use response.data and next(); resource/all and Events convenience helpers have production-host assumptions. Use fetch to the local /v2/enqueue with a native Events v2 payload and a service routing key. REST tokens, routing keys, From identity and webhook signing secrets have separate roles.
|
|
10
10
|
|
|
11
|
+
Gmail: Ancha supplies GMAIL_API_URL/GMAIL_ACCESS_TOKEN and GMAIL_ADDRESS, the mailbox's address (`ancha connect gmail --lang ts`). Google clients take the URL as each call's `rootUrl` (`GMAIL_API_URL + '/'`) or the app's own base-URL setting; push to the app follows its `users.watch` call (`ancha docs apps`).
|
|
12
|
+
|
|
11
13
|
Verify webhook signatures against exact raw request bytes before JSON parsing. Slack uses its signing secret, GitHub its webhook secret, PagerDuty its signing secret; Datadog's declared custom webhook profile uses a bearer token. Native route support and delivery/retry policies are a bounded subset, not full vendor parity.
|
package/lib/assets/docs/run.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
`ancha run --json` loads `ancha.yaml` and runs the configured agent against every scene its `scenes:` globs match, `trials` times each, in fresh worlds. Pass scene files to run only those. `--agent "<command>"` overrides `agent.command`; `-n 1 --parallel 1` runs one diagnostic trial; `--out <dir>` chooses where results go (use the same `--out` for `explain`, `report` and `compare`). Paths are relative to the directory you run `ancha` from.
|
|
4
4
|
|
|
5
|
+
`ancha run --dry-run` checks `ancha.yaml` and every scene, prints the scenes, trials and parallelism, and starts nothing: no server, no agent, no trial.
|
|
6
|
+
|
|
5
7
|
The first line says where the worlds run: locally, or on the hosted workspace you signed in to (`--local` keeps them on this computer). NDJSON starts with `run_started`, which carries `workspace` and `target`, and ends with `run_finished` after a completed run. Without `--json`, each finished trial prints one line, such as `trial 2 ✗ 3m02s · 3 checks failed`, before the summary. The agent's exit code and the checks are independent: a clean exit with failed checks is a failure. Exits: 0 success; 1 failed checks, threshold or regression; 2 usage, config, scene or authentication error; 3 infrastructure or a trial the harness couldn't complete; 4 an unimplemented endpoint under `--strict`. A harness failure takes precedence over thresholds and strict gaps.
|
|
6
8
|
|
|
7
9
|
## What the agent gets
|
package/lib/assets/docs/setup.md
CHANGED
|
@@ -20,6 +20,8 @@ Requirements: Node 24 or newer. Install once with `npm install -g @ancha-labs/cl
|
|
|
20
20
|
|
|
21
21
|
The starter only proves the agent reaches its tools. Replace it with scenes for the agent's real job: `ancha docs scenes`.
|
|
22
22
|
|
|
23
|
+
If the AI feature lives inside an app (a web app or API server with its database, workers and scheduled jobs), Ancha starts the app for each trial instead of a command: `ancha docs apps`.
|
|
24
|
+
|
|
23
25
|
Fix the agent, not the scenes or checks. The exception is a check that is wrong for the real API; then say so and show the evidence (the call the official SDK makes, and the vendor's documentation).
|
|
24
26
|
|
|
25
27
|
## Where things run
|
|
@@ -37,4 +39,4 @@ Add `--json` to any command for one JSON object, or NDJSON events for runs. Erro
|
|
|
37
39
|
|
|
38
40
|
## Topics
|
|
39
41
|
|
|
40
|
-
`ancha docs <topic>`: `setup` (this page), `run`, `explain`, `compare`, `scenes`, `integrations` (SDK settings per tool), `cloud` (login, CI, secrets), `playgrounds` and `cli-quickstart` (agents in containers, tasks, attempts), `tools` (seeded tools without a scene).
|
|
42
|
+
`ancha docs <topic>`: `setup` (this page), `apps` (an app with its database and scheduled jobs, and traces made into tests on this computer), `run`, `explain`, `compare`, `scenes`, `integrations` (SDK settings per tool), `cloud` (login, CI, secrets), `playgrounds` and `cli-quickstart` (agents in containers, tasks, attempts), `tools` (seeded tools without a scene).
|