@cognisphere-sh/cognisphere-harness 0.13.0 → 0.15.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.
Files changed (33) hide show
  1. package/CHANGELOG.md +104 -0
  2. package/dist-web/assets/{file-editor-CqGYKmT3.js → file-editor-DJydzVQK.js} +1 -1
  3. package/dist-web/assets/{index-CDE-jsH8.js → index-C2uTXv8R.js} +2 -2
  4. package/dist-web/index.html +1 -1
  5. package/home-template/docs/base-harness/README.md +25 -7
  6. package/home-template/docs/base-harness/skills.md +1 -1
  7. package/package.json +5 -4
  8. package/skills/create-plugin/CHANGELOG.md +7 -0
  9. package/skills/create-plugin/SKILL.md +10 -6
  10. package/src/agents/base-agent/system_prompts/0-base_prompt.md +2 -0
  11. package/src/agents/base-agent/workspace/router.ts +47 -0
  12. package/src/agents/base-agent/workspace/workflows/README.md +71 -0
  13. package/src/api/admin.ts +1 -1
  14. package/src/cli/agent.ts +1 -2
  15. package/src/core/agent-manager.ts +17 -9
  16. package/src/core/queue.ts +19 -6
  17. package/src/core/router.ts +258 -0
  18. package/src/core/runner.ts +273 -66
  19. package/src/core/types.ts +2 -14
  20. package/src/plugins/admin/index.ts +2 -2
  21. package/src/plugins/agent-messaging/index.ts +4 -3
  22. package/src/plugins/gws/index.ts +8 -120
  23. package/src/plugins/gws/seed/system_prompts/plugin-gws.md +8 -4
  24. package/src/plugins/scheduler/index.ts +4 -4
  25. package/src/plugins/scheduler/seed/system_prompts/plugin-scheduler.md +1 -1
  26. package/src/plugins/telegram/index.ts +4 -98
  27. package/src/plugins/telegram/seed/system_prompts/plugin-telegram.md +4 -6
  28. package/src/plugins/gws/seed/scripts/gws/routes +0 -117
  29. package/src/plugins/gws/seed/skills/gws/route-email/CHANGELOG.md +0 -14
  30. package/src/plugins/gws/seed/skills/gws/route-email/SKILL.md +0 -84
  31. package/src/plugins/telegram/seed/scripts/telegram/routes +0 -95
  32. package/src/plugins/telegram/seed/skills/telegram/route-chats/CHANGELOG.md +0 -7
  33. package/src/plugins/telegram/seed/skills/telegram/route-chats/SKILL.md +0 -63
@@ -16,7 +16,7 @@
16
16
  } catch (_) {}
17
17
  })();
18
18
  </script>
19
- <script type="module" crossorigin src="/assets/index-CDE-jsH8.js"></script>
19
+ <script type="module" crossorigin src="/assets/index-C2uTXv8R.js"></script>
20
20
  <link rel="modulepreload" crossorigin href="/assets/react-BIlrhC09.js">
21
21
  <link rel="modulepreload" crossorigin href="/assets/radix-DHW5y7jr.js">
22
22
  <link rel="modulepreload" crossorigin href="/assets/markdown-sumxz7Eq.js">
@@ -61,7 +61,6 @@ plus an optional `X-App-User` header (see `app/README.md`).
61
61
 
62
62
  - `agent.json` — `name`, `description?` (one-line role blurb shown to other
63
63
  agents in the harness roster), `model: {provider, id, thinkingLevel?}`,
64
- `threadIdStrategy` (`single` | `plugin` | `plugin_channel`),
65
64
  `maxConcurrentSlots?`, `devAgent?` (marks the developer agent), optional
66
65
  `secretsSchema`/`configSchema`/`config`.
67
66
  - `system_prompts/*.md` — concatenated in lexical order into the system
@@ -83,6 +82,26 @@ plus an optional `X-App-User` header (see `app/README.md`).
83
82
  upgrade or restart.
84
83
  - `workspace/` — the agent's durable notes: `index.md`, `daily_notes/`, and
85
84
  `threads/<ThreadId>/` (`notes.md`, `files/`, `tasks/<task-slug>/notes.md`).
85
+ - `workspace/router.ts` — **the only routing authority**: every event from
86
+ every plugin passes through it before it is queued. Its default export gets
87
+ the message (`notification`, `pluginId`, `channelId`, `text`, `metadata`,
88
+ `isSilent`) plus the workflow list, and returns a thread id, an array of
89
+ them, `workflow:<name>`, `undefined` (`metadata.threadId`, else `main`) or
90
+ `[]` (drop). Synchronous; edits apply within a few seconds (a broken edit
91
+ keeps the previous router). Agent-messaging, the scheduler and the console
92
+ set `metadata.threadId`; Telegram and Gmail don't, so they go to `main`
93
+ until a rule routes them (e.g. `return "telegram:" + msg.channelId`).
94
+ - `workspace/workflows/<name>/` — a workflow: `workflow.json`
95
+ (`{"description", "script"}`, both required) plus an executable script.
96
+ Its thread is `workflow:<name>`: input is queued like any thread's and the
97
+ script runs once per message from the agent folder (message JSON on stdin,
98
+ `PI_THREAD_ID=workflow:<name>`). Exit 0 = done; anything else, a 10-minute
99
+ timeout or an interruption = failed, **never retried**. The harness reports
100
+ only runs it killed (timeout, stop, crash) to `main` (`workflow_killed`). It invokes the agent with `scripts/agent-msg/send`; the
101
+ agent's reply to `workflow:<name>` runs it again. Best practice: wrap the
102
+ script in try/catch and send errors to the agent's `main` thread the same
103
+ way, so the agent can babysit it. See `workflows/README.md` in any
104
+ agent.
86
105
  - `knowledge/` — cross-thread reference docs (`index.md`, `files/`) plus
87
106
  `memory.md`, the agent's long-lived memory (grep-able sections separated by
88
107
  `-----$-----$-----$-----`).
@@ -99,9 +118,8 @@ plus an optional `X-App-User` header (see `app/README.md`).
99
118
  here: the harness runs the `pi` bundled with its own package, at the version
100
119
  it was tested with.
101
120
 
102
- A **thread** is one conversation. `threadIdStrategy` controls how inbound
103
- messages map to threads (one global thread, one per plugin, or one per
104
- plugin+channel).
121
+ A **thread** is one conversation. The agent's router (`workspace/router.ts`)
122
+ decides which thread each inbound message joins; by default `main`.
105
123
 
106
124
  ## Plugins
107
125
 
@@ -132,9 +150,9 @@ the open `/webhook/*` surface. Artifact HTML is always served with a CSP
132
150
  sandbox, so it can't reach the app's cookies or storage.
133
151
 
134
152
  The **telegram** plugin long-polls a bot; set
135
- `secrets.json → <agent>.telegram.TELEGRAM_BOT_TOKEN`. Sending `/reset` to the
136
- bot wipes that conversation's context (the thread's queue rows and session
137
- files) — the next message starts fresh.
153
+ `secrets.json → <agent>.telegram.TELEGRAM_BOT_TOKEN`. It picks no thread:
154
+ messages go to `main` unless the agent's router routes them (e.g. one thread
155
+ per chat). To reset a conversation, delete its thread from the console.
138
156
 
139
157
  ## Secrets and models
140
158
 
@@ -63,7 +63,7 @@ A plugin may ship a skill in its `seed/` tree under
63
63
  start and loaded like any other skill. Those directories are **plugin-owned** —
64
64
  overwritten on each start, so never hand-edit them; change the plugin's seed
65
65
  instead. Shipped this way: `artifacts` → `publish-artifact`, `gws` →
66
- `read-email` + `search-email` + `route-email`, `telegram` → `route-chats`. A plugin's system
66
+ `read-email` + `search-email`. A plugin's system
67
67
  prompt fragment is kept short (identity, event shape, always-on rules) and
68
68
  delegates every step-by-step procedure to one of these skills.
69
69
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cognisphere-sh/cognisphere-harness",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -27,8 +27,8 @@
27
27
  "access": "public"
28
28
  },
29
29
  "dependencies": {
30
- "@earendil-works/pi-ai": "0.99.2",
31
- "@earendil-works/pi-coding-agent": "0.99.2",
30
+ "@earendil-works/pi-ai": "1.0.0",
31
+ "@earendil-works/pi-coding-agent": "1.0.0",
32
32
  "@hono/node-server": "^1.13.7",
33
33
  "ajv": "^8.17.1",
34
34
  "better-sqlite3": "^11.7.0",
@@ -51,6 +51,7 @@
51
51
  "typecheck": "tsc --noEmit",
52
52
  "lint": "eslint src",
53
53
  "lint:fix": "eslint src --fix",
54
- "check": "tsc --noEmit && eslint src"
54
+ "check": "tsc --noEmit && eslint src test",
55
+ "test": "node scripts/test.mjs"
55
56
  }
56
57
  }
@@ -1,5 +1,12 @@
1
1
  # create-plugin — changelog
2
2
 
3
+ ## 1.4.0
4
+
5
+ - Routing moved to the agent's router (`workspace/router.ts`): the `notify`
6
+ payload has no `threadIdOverride`, and plugins no longer ship routing rules.
7
+ §1 says what to publish instead (`channelId`/`metadata` for the router,
8
+ `metadata.threadId` only for thread-targeted events).
9
+
3
10
  ## 1.3.0
4
11
 
5
12
  - Prompt-fragment budget: **over ~50 lines is the signal to split** — a
@@ -1,9 +1,9 @@
1
1
  ---
2
2
  name: create-plugin
3
- description: Author a new CogniSphere plugin in a harness's plugins/ directory — scaffold the index.ts + seed, enable it on an agent, and verify it loads and starts. Covers the duck-typed Plugin contract, the manifest (config/secrets schemas), the seed tree (prompt fragment, CLI scripts, plugin-owned skills), event/metadata conventions, enabling per agent, and the common load/start failures. Use when asked to "create a plugin", "author a new plugin", "add a custom plugin" or "write a plugin for the harness" — and whenever a capability an agent needs lives outside its own filesystem: polling or receiving from an external service, exposing an HTTP/webhook endpoint, running something on a schedule, or handing agents a CLI backed by harness-side state. (v1.3.0)
3
+ description: Author a new CogniSphere plugin in a harness's plugins/ directory — scaffold the index.ts + seed, enable it on an agent, and verify it loads and starts. Covers the duck-typed Plugin contract, the manifest (config/secrets schemas), the seed tree (prompt fragment, CLI scripts, plugin-owned skills), event/metadata conventions, enabling per agent, and the common load/start failures. Use when asked to "create a plugin", "author a new plugin", "add a custom plugin" or "write a plugin for the harness" — and whenever a capability an agent needs lives outside its own filesystem: polling or receiving from an external service, exposing an HTTP/webhook endpoint, running something on a schedule, or handing agents a CLI backed by harness-side state. (v1.4.0)
4
4
  metadata:
5
5
  author: cognisphere
6
- version: "1.3.0"
6
+ version: "1.4.0"
7
7
  argument-hint: <plugin-id>
8
8
  ---
9
9
 
@@ -48,8 +48,13 @@ harness types, so the plugin has no build step and no dependencies.
48
48
  `notify(name, payload)`, `httpBaseUrl`, `log`.
49
49
 
50
50
  `notify` payload needs at least `{ text, channelId }`; optional `metadata`,
51
- `threadIdOverride`, `isSilent`, `doNotSteer`, `priority`. It enqueues an event
52
- and wakes the agent (or steers a live run).
51
+ `isSilent`, `doNotSteer`, `priority`. A plugin doesn't pick the thread: the
52
+ agent's router (`workspace/router.ts`) does, from the plugin id, channel,
53
+ text and metadata. Put whatever a routing rule would need (chat id, subject,
54
+ sender) in `channelId`/`metadata`. Set `metadata.threadId` only when the event
55
+ targets a specific thread (as agent-messaging and the scheduler do); the
56
+ router honours it by default. Without either, the event goes to `main`.
57
+ `notify` enqueues the event and wakes the agent (or steers a live run).
53
58
 
54
59
  ### Event & metadata conventions
55
60
 
@@ -113,8 +118,7 @@ loaded like any other). Version it and give it a `CHANGELOG.md` exactly as
113
118
  `create-skill` describes; the scope dir `skills/<id>/` marks it plugin-owned,
114
119
  so agents must never edit it — change the seed instead. The bundled `artifacts`
115
120
  plugin is the worked example (`publish-artifact`); `gws` (`read-email`,
116
- `route-email`) and `telegram` (`route-chats`) are the same split applied to a
117
- larger surface.
121
+ `search-email`) is the same split applied to a larger surface.
118
122
 
119
123
  **A prompt fragment over ~50 lines is the signal to split.** It costs every
120
124
  turn of every thread, whether or not the plugin is in play; a skill costs one
@@ -289,6 +289,8 @@ Always use the bare ThreadId as the thread directory name — don't prefix it wi
289
289
 
290
290
  `workspace/index.md` is the entry point: pointers to every file and directory in the workspace. Keep it updated. Any subdirectory may carry its own `index.md` for a nested index.
291
291
 
292
+ **Routing.** `workspace/router.ts` is the only thing that decides which of your threads an incoming event joins — for every plugin. Without a rule, an event goes to the thread it names (agent-messaging, scheduler, console) or to `main`. To route events (e.g. one thread per Telegram chat or Gmail thread, or a workflow for a kind of email), edit the router; edits apply within a few seconds, and a broken edit keeps the previous router (check the harness log). A workflow is a script in `workspace/workflows/<name>/` that handles events routed to the thread `workflow:<name>` without you (see that folder's `README.md`). A message from a workflow arrives as an `agent_message` with `FromThread: workflow:<name>`; reply to that thread to hand the result back to the workflow (a reply runs it again). You babysit your workflows. A failed run is never retried, and the harness doesn't report ordinary failures, so when you write a workflow, wrap it in try/catch and send errors to yourself on `main` with `scripts/agent-msg/send` (see the workflows `README.md`). Only when the harness kills a script (timeout, stop, crash) does it post a `workflow_killed` notice to `main` itself (plugin `workflow`). When either arrives, read it and the script, fix the script, then re-send the work to `workflow:<name>` or ask the operator to requeue the event.
293
+
292
294
  **Workspace is for what must persist — not scratch.** Write intermediate/throwaway files (temp conversions, scratch parsing output, working copies) under `/tmp`, or delete them once you're done; don't leave them in `workspace/`. Likewise, don't copy input files from `plugins/<id>/inbox/` into `workspace/` by default — read them in place. Copy one into `threads/<ThreadId>/files/` only when it genuinely needs to outlive the inbox.
293
295
 
294
296
  ## Notes
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Message router — the only thing that decides which thread a message goes to.
3
+ * Every event from every plugin (Telegram, Gmail, scheduler, agent-messaging,
4
+ * the console, …) passes through `route` before it is queued. Return:
5
+ *
6
+ * - `"<threadId>"` one thread
7
+ * - `["a", "b"]` several threads (one queued copy each)
8
+ * - `"workflow:<name>"` a workflow thread: the script in
9
+ * `workspace/workflows/<name>/` runs once per message
10
+ * - `[]` drop the message
11
+ * - `undefined` `msg.metadata.threadId` if set, else `"main"`
12
+ *
13
+ * Plugins that target a thread (agent-messaging, the scheduler, the console)
14
+ * set `metadata.threadId`; the others (Telegram, Gmail) leave it to you, so
15
+ * write rules for them here. Keep it synchronous and fast: it runs inside the
16
+ * harness for every message. Edits apply within a few seconds; an edit that
17
+ * fails to load keeps the previous router, and a router that throws or returns
18
+ * an unsafe or unknown id sends the message to the default above.
19
+ */
20
+
21
+ export interface RouterMessage {
22
+ /** Event name, e.g. `message_received`, `email_received`, `schedule_fire`, `agent_message`. */
23
+ notification: string;
24
+ pluginId: string;
25
+ /** The source conversation: Telegram chat id, Gmail thread id, … */
26
+ channelId: string;
27
+ text: string;
28
+ metadata: Record<string, unknown>;
29
+ isSilent: boolean;
30
+ }
31
+
32
+ export interface WorkflowInfo {
33
+ name: string;
34
+ description: string;
35
+ }
36
+
37
+ export default function route(
38
+ msg: RouterMessage,
39
+ workflows: WorkflowInfo[],
40
+ ): string | string[] | undefined {
41
+ void workflows;
42
+ // Rules go here, for example:
43
+ // if (msg.pluginId === "telegram") return `telegram:${msg.channelId}`; // one thread per chat
44
+ // if (msg.pluginId === "gws" && /invoice/i.test(String(msg.metadata.Subject))) return "workflow:invoices";
45
+ const threadId = msg.metadata.threadId;
46
+ return typeof threadId === "string" && threadId ? threadId : "main";
47
+ }
@@ -0,0 +1,71 @@
1
+ # Workflows
2
+
3
+ A workflow is a static script that runs instead of a Pi turn. The router
4
+ (`workspace/router.ts`) sends a message to it by returning the thread id
5
+ `workflow:<name>`. Each workflow is a folder:
6
+
7
+ ```
8
+ workflows/<name>/
9
+ workflow.json {"description": "What it does and when to route to it.", "script": "run.sh"}
10
+ run.sh the script (executable: chmod +x)
11
+ ```
12
+
13
+ The folder name is the workflow's name. `description` and `script` are
14
+ required; a folder missing either, or whose script isn't executable, is skipped.
15
+
16
+ Messages to a workflow are queued like any thread's, and the script runs once
17
+ per message, one at a time, from the agent folder:
18
+
19
+ - **stdin:** the message as JSON: `id`, `notification`, `pluginId`,
20
+ `channelId`, `threadId`, `text`, `metadata`, `isSilent`, `attempts`.
21
+ - **env:** the same as a Pi turn (`.venv`, secrets, `PI_AGENT_ID`,
22
+ `HARNESS_BASE_URL`), with `PI_THREAD_ID=workflow:<name>`.
23
+ - **result:** exit 0 marks the message done. Anything else (or running past
24
+ 10 minutes, or being interrupted by a stop or crash) marks it **failed,
25
+ with no retry**. For an ordinary failure the harness only logs it and shows
26
+ it in the console's events; telling the agent is the script's job (below).
27
+ When the harness itself kills the script (timeout, stop, crash), the
28
+ script's handler can't run, so the harness posts a `workflow_killed`
29
+ notice to `main` instead. `attempts` > 0 on stdin means an operator
30
+ requeued it after an earlier run failed part-way.
31
+ - **To invoke the agent:** `scripts/agent-msg/send --to-agent "$PI_AGENT_ID" --thread-id <thread> --message "…"`.
32
+ The agent's reply to `workflow:<name>` arrives as a new message
33
+ (`notification: "agent_message"`), so don't wait for it in the script.
34
+
35
+ ## Best practice: catch errors and tell the agent
36
+
37
+ The agent babysits its workflows, but only hears about a failure if the
38
+ script tells it. Wrap the work in try/catch and send the agent what it needs
39
+ to act, with agent-messaging, to `main` (or any thread you choose):
40
+
41
+ ```python
42
+ #!/usr/bin/env python3
43
+ import json, os, subprocess, sys, traceback
44
+
45
+ msg = json.load(sys.stdin)
46
+
47
+ def tell_agent(text, thread="main"):
48
+ subprocess.run(["scripts/agent-msg/send", "--to-agent", os.environ["PI_AGENT_ID"],
49
+ "--thread-id", thread, "--subject", "workflow invoices", "--message", text])
50
+
51
+ try:
52
+ ... # the workflow
53
+ except Exception:
54
+ tell_agent(f"invoices failed on event {msg['id']} ({msg['text'][:200]!r}):\n{traceback.format_exc()}")
55
+ sys.exit(1) # marks the event failed in the console
56
+ ```
57
+
58
+ In a shell script, use `bash` with
59
+ `trap 'scripts/agent-msg/send --to-agent "$PI_AGENT_ID" --thread-id main --message "…failed at line $LINENO"; exit 1' ERR`.
60
+ A timeout, stop or crash kills the script before its handler runs; the
61
+ harness reports those to `main` itself.
62
+
63
+ Messages a workflow sends carry `FromThread: workflow:<name>`, so an agent
64
+ reply to them runs the workflow again. To redo the work after a fix, the agent
65
+ sends it to `workflow:<name>` (or the operator requeues the failed event in
66
+ the console).
67
+
68
+ Output goes to the harness log. A run ends when the script's stdout and stderr
69
+ close, so don't leave a child running in the background: it holds the worker
70
+ until it exits or the 10-minute timeout kills it. Edits to workflows and the
71
+ router apply within a few seconds.
package/src/api/admin.ts CHANGED
@@ -37,7 +37,7 @@ export function adminRouter(am: AgentManager): Hono {
37
37
  admin.deliver({
38
38
  text: body.text,
39
39
  channelId: body.channelId,
40
- threadIdOverride: body.threadId,
40
+ threadId: body.threadId,
41
41
  });
42
42
  return c.json({ ok: true });
43
43
  });
package/src/cli/agent.ts CHANGED
@@ -60,7 +60,7 @@ export function scaffoldAgent(
60
60
  }
61
61
  }
62
62
 
63
- // Starter config — edit model/strategy before first run. The agent stays
63
+ // Starter config — edit the model before first run. The agent stays
64
64
  // "failed" until its model provider is configured (secrets + models.json).
65
65
  writeJson(join(target, "agent.json"), {
66
66
  name,
@@ -68,7 +68,6 @@ export function scaffoldAgent(
68
68
  ? "Developer agent: owns and modifies this deployment's platform code — agent prompts/scripts, forked plugins, the user-facing frontend app, deploy scripts — and keeps docs/harness + docs/app current. Send code, app, docs and software-install requests here."
69
69
  : `TODO: one-line description of ${name}'s role (shown to other agents in the harness roster).`,
70
70
  model: { provider: "anthropic", id: "claude-sonnet-4-6" },
71
- threadIdStrategy: { type: "single" },
72
71
  ...(opts.dev ? { devAgent: true } : {}),
73
72
  });
74
73
  return target;
@@ -10,6 +10,7 @@ import type { PluginRegistry } from "./plugin-registry.js";
10
10
  import { CORE_PLUGIN_IDS } from "./plugin-registry.js";
11
11
  import { AgentDb } from "./queue.js";
12
12
  import { AgentRunner } from "./runner.js";
13
+ import { loadRouting, seedRouting, watchRouting } from "./router.js";
13
14
  import { findProviderInCatalog } from "./models-catalog.js";
14
15
  import { modelUnavailableReason } from "./model-access.js";
15
16
  import { ModelsStore } from "./models-store.js";
@@ -59,6 +60,8 @@ export interface AgentInstance {
59
60
  * a fresh runner with the new config. Cleared synchronously when the
60
61
  * swap fires. */
61
62
  staleReason: string | null;
63
+ /** Stops the router/workflows reload watcher; set while running. */
64
+ stopRoutingWatch: (() => void) | null;
62
65
  }
63
66
 
64
67
  export type LifecycleErrorCode = "not_found" | "conflict";
@@ -207,7 +210,7 @@ export class AgentManager {
207
210
  * Tear down one thread: refuse while a batch is in-flight (it would race
208
211
  * the runner writing the session jsonl), delete the thread's queue rows +
209
212
  * session binding, remove its `sessions/<threadId>/` dir. Shared by the
210
- * HTTP DELETE route and the plugin-context `resetThread`.
213
+ * HTTP DELETE route.
211
214
  */
212
215
  deleteThread(
213
216
  id: string,
@@ -350,6 +353,7 @@ export class AgentManager {
350
353
  changedAt: Date.now(),
351
354
  plugins: new Map(),
352
355
  staleReason: null,
356
+ stopRoutingWatch: null,
353
357
  };
354
358
  this.agents.set(id, inst);
355
359
  try {
@@ -478,6 +482,12 @@ export class AgentManager {
478
482
  // delays later agents.
479
483
  await runBootstrap(dir, this.log);
480
484
 
485
+ // 2.6. Load the agent's router and workflows. Agents created before
486
+ // routing existed get the starter router (write-if-absent).
487
+ const routingLog = childLogger(`agent:${inst.id}`);
488
+ seedRouting(dir, routingLog);
489
+ const routing = await loadRouting(dir, routingLog);
490
+
481
491
  // 3. Construct a fresh runner with the resolved env snapshot.
482
492
  inst.runner = new AgentRunner({
483
493
  rootDir: this.cfg.rootDir,
@@ -488,6 +498,7 @@ export class AgentManager {
488
498
  serverBaseUrl: this.cfg.serverBaseUrl,
489
499
  timezone: this.cfg.timezone,
490
500
  envSecrets,
501
+ routing,
491
502
  resolveProviderEnv: (providerId) =>
492
503
  resolveProviderEnv(this.models, providerId, dir, this.log),
493
504
  modelUnavailableReason: (providerId, modelId) =>
@@ -497,6 +508,9 @@ export class AgentManager {
497
508
  // When a stale-reload is pending and the last active batch finishes,
498
509
  // swap to a fresh runner. The listener captures `inst` (stable) and
499
510
  // checks `inst.runner` at fire time so a swap-in-progress no-ops.
511
+ // The agent edits its own router; apply edits without a restart.
512
+ const runner = inst.runner;
513
+ inst.stopRoutingWatch = watchRouting(dir, routing, (r) => runner.setRouting(r), routingLog);
500
514
  inst.runner.on("batch-completed", () => {
501
515
  if (inst.staleReason && inst.runner?.activeCount === 0) {
502
516
  void this.performStaleSwap(inst);
@@ -566,6 +580,8 @@ export class AgentManager {
566
580
  entry.changedAt = Date.now();
567
581
  }
568
582
 
583
+ inst.stopRoutingWatch?.();
584
+ inst.stopRoutingWatch = null;
569
585
  if (inst.runner) {
570
586
  try {
571
587
  await inst.runner.stop();
@@ -676,14 +692,6 @@ export class AgentManager {
676
692
  log.error({ err }, "notify failed");
677
693
  }
678
694
  },
679
- resetThread: (channelId, threadIdOverride) => {
680
- const runner = inst.runner;
681
- if (!runner) throw new Error("agent not running");
682
- const threadId =
683
- threadIdOverride ?? runner.threadIdFor(pluginId, channelId);
684
- this.deleteThread(inst.id, threadId);
685
- log.info({ threadId }, "thread reset by plugin");
686
- },
687
695
  };
688
696
 
689
697
  await pluginInstance.start(ctx);
package/src/core/queue.ts CHANGED
@@ -226,12 +226,15 @@ export class AgentDb {
226
226
  * this — `doNotSteer` only forbids steering into a live batch, not forming
227
227
  * a fresh prompt.
228
228
  *
229
+ * - `limit` — claim at most this many rows (workflow threads run one row
230
+ * per script run).
231
+ *
229
232
  * A plain call (no options) claims everything still queued — resend + fresh
230
233
  * rows — which the runner concatenates into one prompt.
231
234
  */
232
235
  dequeueBatch(
233
236
  threadId: string,
234
- opts: { continueOnly?: boolean; excludeDoNotSteer?: boolean } = {},
237
+ opts: { continueOnly?: boolean; excludeDoNotSteer?: boolean; limit?: number } = {},
235
238
  ): BatchMessage[] {
236
239
  const conds = ["thread_id = ?", "status = 'queued'"];
237
240
  if (opts.continueOnly) conds.push("pi_entry_id IS NOT NULL");
@@ -240,7 +243,8 @@ export class AgentDb {
240
243
  const txn = this.db.transaction((): BatchMessage[] => {
241
244
  const rows = this.db
242
245
  .prepare<unknown[], EventRow>(
243
- `SELECT * FROM events WHERE ${where} ORDER BY priority DESC, id ASC`,
246
+ `SELECT * FROM events WHERE ${where} ORDER BY priority DESC, id ASC` +
247
+ (opts.limit ? ` LIMIT ${Math.floor(opts.limit)}` : ""),
244
248
  )
245
249
  .all(threadId);
246
250
  if (rows.length === 0) return [];
@@ -488,13 +492,22 @@ export class AgentDb {
488
492
  return { events };
489
493
  }
490
494
 
491
- /** Crash recovery: any row stuck in 'in_flight' is treated as a failed attempt. */
492
- sweepInFlight(maxAttempts: number, error = "process died mid-batch") {
495
+ /** Crash recovery: any row stuck in 'in_flight' is treated as a failed
496
+ * attempt. Rows on a thread starting with `noRetryPrefix` (workflow
497
+ * threads, which never retry) fail outright. */
498
+ sweepInFlight(maxAttempts: number, error = "process died mid-batch", noRetryPrefix?: string) {
493
499
  const rows = this.db
494
- .prepare<unknown[], { id: number }>(
495
- `SELECT id FROM events WHERE status = 'in_flight'`,
500
+ .prepare<unknown[], { id: number; thread_id: string }>(
501
+ `SELECT id, thread_id FROM events WHERE status = 'in_flight'`,
496
502
  )
497
503
  .all();
504
+ if (noRetryPrefix) {
505
+ const noRetry = rows.filter((r) => r.thread_id.startsWith(noRetryPrefix)).map((r) => r.id);
506
+ const rest = rows.filter((r) => !r.thread_id.startsWith(noRetryPrefix)).map((r) => r.id);
507
+ const a = this.markBatchFailed(noRetry, `[crash] ${error}`, 1, null);
508
+ const b = this.markBatchFailed(rest, `[crash] ${error}`, maxAttempts, null);
509
+ return { retrying: b.retrying, dead: [...a.dead, ...b.dead] };
510
+ }
498
511
  if (rows.length === 0) return { retrying: [], dead: [] };
499
512
  // sessionId=null preserves any existing pi_session_id via COALESCE.
500
513
  return this.markBatchFailed(rows.map((r) => r.id), `[crash] ${error}`, maxAttempts, null);