From f18b3da88fc7680fb824cbef79edac45586f524c Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Wed, 22 Apr 2026 06:35:30 +0800 Subject: [PATCH 01/14] Add OpenClaw polling and browser auth support --- Cargo.lock | 1 + Cargo.toml | 3 +- plugins/corall-polling/.gitignore | 1 + plugins/corall-polling/README.md | 37 + plugins/corall-polling/dist/index.d.ts | 2 + plugins/corall-polling/dist/index.js | 18 + plugins/corall-polling/dist/index.js.map | 1 + plugins/corall-polling/dist/src/config.d.ts | 6 + plugins/corall-polling/dist/src/config.js | 100 ++ plugins/corall-polling/dist/src/config.js.map | 1 + plugins/corall-polling/dist/src/http.d.ts | 10 + plugins/corall-polling/dist/src/http.js | 80 + plugins/corall-polling/dist/src/http.js.map | 1 + plugins/corall-polling/dist/src/service.d.ts | 15 + plugins/corall-polling/dist/src/service.js | 253 +++ .../corall-polling/dist/src/service.js.map | 1 + plugins/corall-polling/dist/src/types.d.ts | 63 + plugins/corall-polling/dist/src/types.js | 2 + plugins/corall-polling/dist/src/types.js.map | 1 + .../dist/test/service.test.d.ts | 1 + .../corall-polling/dist/test/service.test.js | 532 ++++++ .../dist/test/service.test.js.map | 1 + plugins/corall-polling/index.ts | 22 + plugins/corall-polling/openclaw.plugin.json | 108 ++ plugins/corall-polling/package-lock.json | 50 + plugins/corall-polling/package.json | 33 + plugins/corall-polling/src/config.ts | 132 ++ plugins/corall-polling/src/http.ts | 102 ++ .../src/openclaw-plugin-sdk.d.ts | 12 + plugins/corall-polling/src/service.ts | 406 +++++ plugins/corall-polling/src/types.ts | 71 + plugins/corall-polling/test/service.test.ts | 593 ++++++ plugins/corall-polling/tsconfig.json | 23 + skills/corall/.claude-plugin/plugin.json | 2 +- skills/corall/SKILL.md | 14 +- skills/corall/evals/cases.md | 25 +- skills/corall/references/browser-login.md | 26 + skills/corall/references/cli-reference.md | 58 +- skills/corall/references/file-upload.md | 2 +- skills/corall/references/order-create.md | 4 +- skills/corall/references/order-handle.md | 6 +- skills/corall/references/setup-employer.md | 12 +- .../references/setup-provider-openclaw.md | 80 +- .../corall/references/skill-package-submit.md | 125 ++ src/client.rs | 63 +- src/commands/auth.rs | 144 +- src/commands/eventbus.rs | 72 + src/commands/mod.rs | 2 + src/commands/openclaw.rs | 297 ++- src/commands/skill_packages.rs | 160 ++ src/credentials.rs | 95 +- src/eventbus.rs | 1589 +++++++++++++++++ src/main.rs | 16 + tests/browser_auth.rs | 362 ++++ tests/eventbus_polling.rs | 272 +++ tests/openclaw_setup.rs | 133 ++ tests/skill_contract.rs | 136 ++ 57 files changed, 6254 insertions(+), 123 deletions(-) create mode 100644 plugins/corall-polling/.gitignore create mode 100644 plugins/corall-polling/README.md create mode 100644 plugins/corall-polling/dist/index.d.ts create mode 100644 plugins/corall-polling/dist/index.js create mode 100644 plugins/corall-polling/dist/index.js.map create mode 100644 plugins/corall-polling/dist/src/config.d.ts create mode 100644 plugins/corall-polling/dist/src/config.js create mode 100644 plugins/corall-polling/dist/src/config.js.map create mode 100644 plugins/corall-polling/dist/src/http.d.ts create mode 100644 plugins/corall-polling/dist/src/http.js create mode 100644 plugins/corall-polling/dist/src/http.js.map create mode 100644 plugins/corall-polling/dist/src/service.d.ts create mode 100644 plugins/corall-polling/dist/src/service.js create mode 100644 plugins/corall-polling/dist/src/service.js.map create mode 100644 plugins/corall-polling/dist/src/types.d.ts create mode 100644 plugins/corall-polling/dist/src/types.js create mode 100644 plugins/corall-polling/dist/src/types.js.map create mode 100644 plugins/corall-polling/dist/test/service.test.d.ts create mode 100644 plugins/corall-polling/dist/test/service.test.js create mode 100644 plugins/corall-polling/dist/test/service.test.js.map create mode 100644 plugins/corall-polling/index.ts create mode 100644 plugins/corall-polling/openclaw.plugin.json create mode 100644 plugins/corall-polling/package-lock.json create mode 100644 plugins/corall-polling/package.json create mode 100644 plugins/corall-polling/src/config.ts create mode 100644 plugins/corall-polling/src/http.ts create mode 100644 plugins/corall-polling/src/openclaw-plugin-sdk.d.ts create mode 100644 plugins/corall-polling/src/service.ts create mode 100644 plugins/corall-polling/src/types.ts create mode 100644 plugins/corall-polling/test/service.test.ts create mode 100644 plugins/corall-polling/tsconfig.json create mode 100644 skills/corall/references/browser-login.md create mode 100644 skills/corall/references/skill-package-submit.md create mode 100644 src/commands/eventbus.rs create mode 100644 src/commands/skill_packages.rs create mode 100644 src/eventbus.rs create mode 100644 tests/browser_auth.rs create mode 100644 tests/eventbus_polling.rs create mode 100644 tests/openclaw_setup.rs create mode 100644 tests/skill_contract.rs diff --git a/Cargo.lock b/Cargo.lock index 2a4745c..cd6bd5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -209,6 +209,7 @@ dependencies = [ "json5", "rand", "reqwest", + "ring", "rustls", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index c488c75..acae7f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,9 +17,10 @@ hex = "0.4" json5 = "1.3.1" rand = "0.10.0" reqwest = { version = "0.13.2", default-features = false, features = ["json", "rustls-no-provider"] } +ring = "0.17" rustls = { version = "0.23", default-features = false, features = ["ring", "std"] } serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.149" sha2 = "0.11" -tokio = { version = "1.50.0", features = ["rt-multi-thread", "macros"] } +tokio = { version = "1.50.0", features = ["rt-multi-thread", "macros", "net", "io-util"] } zip = { version = "8", default-features = false, features = ["deflate"] } diff --git a/plugins/corall-polling/.gitignore b/plugins/corall-polling/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/plugins/corall-polling/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/plugins/corall-polling/README.md b/plugins/corall-polling/README.md new file mode 100644 index 0000000..1e647e9 --- /dev/null +++ b/plugins/corall-polling/README.md @@ -0,0 +1,37 @@ +# Corall Polling Plugin + +Native OpenClaw plugin that long-polls the Corall resident eventbus and forwards each event's `hook` payload to the local OpenClaw `/hooks/agent` endpoint. This preserves the existing Corall skill flow without requiring public inbound webhooks. + +## Install + +```bash +corall openclaw setup --eventbus-url http://127.0.0.1:8080 +openclaw gateway restart +``` + +The released `corall` CLI embeds the compiled plugin files and installs them +with `openclaw plugins install --force`. For local plugin development, run +`npm ci && npm run build` before compiling the CLI so the embedded `dist/` +files are current. OpenClaw loads the compiled ESM entry at `dist/index.js`. + +## Config + +Add this under `plugins.entries.corall-polling` in `~/.openclaw/openclaw.json`: + +```json +{ + "enabled": true, + "config": { + "baseUrl": "http://127.0.0.1:8080", + "waitSeconds": 30 + } +} +``` + +Notes: + +- `agentId` defaults to `~/.corall/credentials/provider.json` and `agentToken` defaults to `hooks.token`, so the minimal config only needs `baseUrl`. +- If you use a different Corall credential profile, set `credentialProfile`. +- Event polling uses `Authorization: Bearer `, so create/update the Corall agent with the same token you keep in `hooks.token` via `--webhook-token`. +- If `hookUrl` is omitted, the plugin forwards to `http://127.0.0.1:/hooks/agent`. +- Local forwarding uses `hooks.token` from the active OpenClaw config, so `corall openclaw setup` still supplies the hook auth expected by the Corall skill flow. diff --git a/plugins/corall-polling/dist/index.d.ts b/plugins/corall-polling/dist/index.d.ts new file mode 100644 index 0000000..d003ee0 --- /dev/null +++ b/plugins/corall-polling/dist/index.d.ts @@ -0,0 +1,2 @@ +declare const _default: import("openclaw/plugin-sdk/plugin-entry").PluginEntry; +export default _default; diff --git a/plugins/corall-polling/dist/index.js b/plugins/corall-polling/dist/index.js new file mode 100644 index 0000000..5456f86 --- /dev/null +++ b/plugins/corall-polling/dist/index.js @@ -0,0 +1,18 @@ +import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry"; +import { resolvePluginConfig } from "./src/config.js"; +import { createPollingService } from "./src/service.js"; +export default definePluginEntry({ + id: "corall-polling", + name: "Corall Polling", + description: "Poll Corall resident events and forward hook payloads to the local OpenClaw hook endpoint.", + register(api) { + const config = resolvePluginConfig(api.pluginConfig); + const service = createPollingService({ api, config }); + api.registerService({ + id: "corall-polling", + start: service.start, + stop: service.stop, + }); + }, +}); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/plugins/corall-polling/dist/index.js.map b/plugins/corall-polling/dist/index.js.map new file mode 100644 index 0000000..0c228e7 --- /dev/null +++ b/plugins/corall-polling/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAErE,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAGxD,eAAe,iBAAiB,CAAC;IAC/B,EAAE,EAAE,gBAAgB;IACpB,IAAI,EAAE,gBAAgB;IACtB,WAAW,EACT,4FAA4F;IAC9F,QAAQ,CAAC,GAAsB;QAC7B,MAAM,MAAM,GAAG,mBAAmB,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QACrD,MAAM,OAAO,GAAG,oBAAoB,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC;QAEtD,GAAG,CAAC,eAAe,CAAC;YAClB,EAAE,EAAE,gBAAgB;YACpB,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,IAAI,EAAE,OAAO,CAAC,IAAI;SACnB,CAAC,CAAC;IACL,CAAC;CACF,CAAC,CAAC"} \ No newline at end of file diff --git a/plugins/corall-polling/dist/src/config.d.ts b/plugins/corall-polling/dist/src/config.d.ts new file mode 100644 index 0000000..e09a3a9 --- /dev/null +++ b/plugins/corall-polling/dist/src/config.d.ts @@ -0,0 +1,6 @@ +import type { OpenClawConfig, PluginConfig, RuntimeConfig } from "./types.js"; +export declare function resolvePluginConfig(rawValue: unknown): PluginConfig; +export declare function materializeRuntimeConfig(pluginConfig: PluginConfig, openclawConfig: OpenClawConfig): RuntimeConfig; +export declare function resolveHookUrl(openclawConfig: OpenClawConfig, pluginConfig: PluginConfig): string; +export declare function resolveHooksToken(openclawConfig: OpenClawConfig): string | undefined; +export declare function validateRuntimeConfig(runtimeConfig: RuntimeConfig, openclawConfig: OpenClawConfig): string[]; diff --git a/plugins/corall-polling/dist/src/config.js b/plugins/corall-polling/dist/src/config.js new file mode 100644 index 0000000..d62d8a5 --- /dev/null +++ b/plugins/corall-polling/dist/src/config.js @@ -0,0 +1,100 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +const DEFAULT_GATEWAY_PORT = 18789; +const DEFAULT_HOOK_URL_PATH = "/hooks/agent"; +const DEFAULT_WAIT_SECONDS = 30; +const DEFAULT_IDLE_DELAY_MS = 1000; +const DEFAULT_ACK_TIMEOUT_MS = 10_000; +const DEFAULT_ERROR_BACKOFF_MS = 2_000; +const DEFAULT_MAX_ERROR_BACKOFF_MS = 30_000; +const DEFAULT_RECENT_EVENT_TTL_MS = 10 * 60 * 1000; +const DEFAULT_CREDENTIAL_PROFILE = "provider"; +function asObject(value) { + return value !== null && typeof value === "object" && !Array.isArray(value) + ? value + : {}; +} +function asString(value) { + return typeof value === "string" && value.trim() ? value.trim() : undefined; +} +function asInteger(value, fallback) { + return Number.isInteger(value) && typeof value === "number" && value >= 0 ? value : fallback; +} +function stripTrailingSlashes(value) { + return value.replace(/\/+$/, ""); +} +export function resolvePluginConfig(rawValue) { + const raw = asObject(rawValue); + const waitSeconds = Math.min(asInteger(raw.waitSeconds, DEFAULT_WAIT_SECONDS), 60); + const rawAgentId = asString(raw.agentId); + const rawBaseUrl = asString(raw.baseUrl); + const rawConsumerId = asString(raw.consumerId); + return { + baseUrl: rawBaseUrl ? stripTrailingSlashes(rawBaseUrl) : undefined, + agentId: rawAgentId, + agentToken: asString(raw.agentToken), + credentialProfile: asString(raw.credentialProfile) ?? DEFAULT_CREDENTIAL_PROFILE, + consumerId: rawConsumerId, + waitSeconds, + hookUrl: asString(raw.hookUrl), + requestTimeoutMs: Math.max(asInteger(raw.requestTimeoutMs, waitSeconds * 1000 + 15_000), waitSeconds * 1000 + 1_000), + ackTimeoutMs: asInteger(raw.ackTimeoutMs, DEFAULT_ACK_TIMEOUT_MS), + idleDelayMs: asInteger(raw.idleDelayMs, DEFAULT_IDLE_DELAY_MS), + errorBackoffMs: asInteger(raw.errorBackoffMs, DEFAULT_ERROR_BACKOFF_MS), + maxErrorBackoffMs: Math.max(asInteger(raw.maxErrorBackoffMs, DEFAULT_MAX_ERROR_BACKOFF_MS), asInteger(raw.errorBackoffMs, DEFAULT_ERROR_BACKOFF_MS)), + recentEventTtlMs: asInteger(raw.recentEventTtlMs, DEFAULT_RECENT_EVENT_TTL_MS), + }; +} +export function materializeRuntimeConfig(pluginConfig, openclawConfig) { + const agentId = pluginConfig.agentId ?? readAgentIdFromCredentials(pluginConfig.credentialProfile); + const agentToken = pluginConfig.agentToken ?? resolveHooksToken(openclawConfig); + const consumerId = pluginConfig.consumerId ?? `corall-polling:${agentId ?? "unknown"}:${os.hostname()}`; + return { + ...pluginConfig, + agentId, + agentToken, + consumerId, + hookUrl: resolveHookUrl(openclawConfig, pluginConfig), + }; +} +export function resolveHookUrl(openclawConfig, pluginConfig) { + if (pluginConfig.hookUrl) { + return pluginConfig.hookUrl; + } + const gateway = asObject(openclawConfig.gateway); + const port = asInteger(gateway.port, DEFAULT_GATEWAY_PORT); + return `http://127.0.0.1:${port}${DEFAULT_HOOK_URL_PATH}`; +} +export function resolveHooksToken(openclawConfig) { + const hooks = asObject(openclawConfig.hooks); + return asString(hooks.token); +} +export function validateRuntimeConfig(runtimeConfig, openclawConfig) { + const errors = []; + if (!runtimeConfig.baseUrl) { + errors.push("config.baseUrl is required"); + } + if (!runtimeConfig.agentId) { + errors.push("config.agentId is required or must exist in ~/.corall/credentials/.json"); + } + if (!runtimeConfig.agentToken) { + errors.push("config.agentToken is required or must match hooks.token"); + } + if (!resolveHooksToken(openclawConfig)) { + errors.push("hooks.token is missing from the active OpenClaw config"); + } + return errors; +} +function readAgentIdFromCredentials(profile) { + const credentialsPath = path.join(os.homedir(), ".corall", "credentials", `${profile}.json`); + try { + const raw = fs.readFileSync(credentialsPath, "utf8"); + const parsed = JSON.parse(raw); + return asString(asObject(parsed).agentId); + } + catch { + return undefined; + } +} +//# sourceMappingURL=config.js.map \ No newline at end of file diff --git a/plugins/corall-polling/dist/src/config.js.map b/plugins/corall-polling/dist/src/config.js.map new file mode 100644 index 0000000..f1bc8b4 --- /dev/null +++ b/plugins/corall-polling/dist/src/config.js.map @@ -0,0 +1 @@ +{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAI7B,MAAM,oBAAoB,GAAG,KAAK,CAAC;AACnC,MAAM,qBAAqB,GAAG,cAAc,CAAC;AAC7C,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAChC,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,MAAM,sBAAsB,GAAG,MAAM,CAAC;AACtC,MAAM,wBAAwB,GAAG,KAAK,CAAC;AACvC,MAAM,4BAA4B,GAAG,MAAM,CAAC;AAC5C,MAAM,2BAA2B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACnD,MAAM,0BAA0B,GAAG,UAAU,CAAC;AAI9C,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACzE,CAAC,CAAE,KAAoB;QACvB,CAAC,CAAC,EAAE,CAAC;AACT,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;AAC9E,CAAC;AAED,SAAS,SAAS,CAAC,KAAc,EAAE,QAAgB;IACjD,OAAO,MAAM,CAAC,SAAS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S,CAAC;IACnB,CAAC;AACH,CAAC"} \ No newline at end of file diff --git a/plugins/corall-polling/dist/src/http.d.ts b/plugins/corall-polling/dist/src/http.d.ts new file mode 100644 index 0000000..9a183a1 --- /dev/null +++ b/plugins/corall-polling/dist/src/http.d.ts @@ -0,0 +1,10 @@ +export declare function isAbortError(error: unknown): boolean; +export declare function sleep(ms: number, signal?: AbortSignal): Promise; +interface FetchWithTimeoutOptions extends RequestInit { + timeoutMs: number; + signal?: AbortSignal; +} +export declare function fetchWithTimeout(url: URL, options: FetchWithTimeoutOptions): Promise; +export declare function fetchJson(url: URL, options: FetchWithTimeoutOptions): Promise; +export declare function fetchOk(url: URL, options: FetchWithTimeoutOptions): Promise; +export {}; diff --git a/plugins/corall-polling/dist/src/http.js b/plugins/corall-polling/dist/src/http.js new file mode 100644 index 0000000..71d80dd --- /dev/null +++ b/plugins/corall-polling/dist/src/http.js @@ -0,0 +1,80 @@ +function abortError(message) { + const error = new Error(message); + error.name = "AbortError"; + return error; +} +export function isAbortError(error) { + return error instanceof Error && error.name === "AbortError"; +} +export async function sleep(ms, signal) { + if (ms <= 0) { + return; + } + await new Promise((resolve, reject) => { + const cleanup = () => { + clearTimeout(timeout); + signal?.removeEventListener("abort", onAbort); + }; + const onAbort = () => { + cleanup(); + reject(abortError("Operation aborted")); + }; + const timeout = setTimeout(() => { + cleanup(); + resolve(); + }, ms); + if (signal?.aborted) { + cleanup(); + reject(abortError("Operation aborted")); + return; + } + signal?.addEventListener("abort", onAbort, { once: true }); + }); +} +export async function fetchWithTimeout(url, options) { + const { timeoutMs, signal, ...fetchOptions } = options; + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), timeoutMs); + const onAbort = () => controller.abort(); + try { + if (signal?.aborted) { + throw abortError("Operation aborted"); + } + signal?.addEventListener("abort", onAbort, { once: true }); + return await fetch(url, { + ...fetchOptions, + signal: controller.signal, + }); + } + catch (error) { + if (controller.signal.aborted || signal?.aborted) { + throw abortError(`Request aborted for ${url.toString()}`); + } + throw error; + } + finally { + clearTimeout(timeout); + signal?.removeEventListener("abort", onAbort); + } +} +export async function fetchJson(url, options) { + const response = await fetchWithTimeout(url, options); + const bodyText = await response.text(); + if (!response.ok) { + const details = bodyText ? `: ${bodyText}` : ""; + throw new Error(`HTTP ${response.status} ${response.statusText}${details}`); + } + if (!bodyText) { + return null; + } + return JSON.parse(bodyText); +} +export async function fetchOk(url, options) { + const response = await fetchWithTimeout(url, options); + const bodyText = await response.text(); + if (!response.ok) { + const details = bodyText ? `: ${bodyText}` : ""; + throw new Error(`HTTP ${response.status} ${response.statusText}${details}`); + } +} +//# sourceMappingURL=http.js.map \ No newline at end of file diff --git a/plugins/corall-polling/dist/src/http.js.map b/plugins/corall-polling/dist/src/http.js.map new file mode 100644 index 0000000..caba8aa --- /dev/null +++ b/plugins/corall-polling/dist/src/http.js.map @@ -0,0 +1 @@ +{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/http.ts"],"names":[],"mappings":"AAAA,SAAS,UAAU,CAAC,OAAe;IACjC,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;IACjC,KAAK,CAAC,IAAI,GAAG,YAAY,CAAC;IAC1B,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,CAAC;AAC/D,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,EAAU,EAAE,MAAoB;IAC1D,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC;QACZ,OAAO;IACT,CAAC;IAED,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC1C,MAAM,OAAO,GAAG,GAAS,EAAE;YACzB,YAAY,CAAC,OAAO,CAAC,CAAC;YACtB,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAChD,CAAC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EAAE,EAAE,CAAC;QACjB,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,KAAK,CAAC,QAAQ,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,UAAU,GAAG,OAAO,EAAE,CAAC,CAAC;IAC9E,CAAC;AACH,CAAC"} \ No newline at end of file diff --git a/plugins/corall-polling/dist/src/service.d.ts b/plugins/corall-polling/dist/src/service.d.ts new file mode 100644 index 0000000..a838213 --- /dev/null +++ b/plugins/corall-polling/dist/src/service.d.ts @@ -0,0 +1,15 @@ +import type { OpenClawConfig, PluginConfig, Logger } from "./types.js"; +interface PollingApi { + config: OpenClawConfig; + logger: Logger; +} +interface PollingService { + start(): Promise; + stop(): Promise; +} +interface CreatePollingServiceOptions { + api: PollingApi; + config: PluginConfig; +} +export declare function createPollingService({ api, config }: CreatePollingServiceOptions): PollingService; +export {}; diff --git a/plugins/corall-polling/dist/src/service.js b/plugins/corall-polling/dist/src/service.js new file mode 100644 index 0000000..59e048b --- /dev/null +++ b/plugins/corall-polling/dist/src/service.js @@ -0,0 +1,253 @@ +import { materializeRuntimeConfig, resolveHooksToken, validateRuntimeConfig } from "./config.js"; +import { fetchJson, fetchOk, isAbortError, sleep } from "./http.js"; +function asObject(value) { + return value !== null && typeof value === "object" && !Array.isArray(value) + ? value + : null; +} +function asString(value) { + return typeof value === "string" && value.trim() ? value.trim() : undefined; +} +function isHookPayload(value) { + const hook = asObject(value); + return Boolean(hook && + typeof hook.message === "string" && + typeof hook.name === "string" && + typeof hook.sessionKey === "string" && + typeof hook.deliver === "boolean"); +} +function buildAuthHeaders(token) { + return { + authorization: `Bearer ${token}`, + accept: "application/json", + }; +} +function extractEvents(payload) { + if (Array.isArray(payload)) { + return payload; + } + const objectPayload = asObject(payload); + if (!objectPayload) { + return []; + } + if (Array.isArray(objectPayload.events)) { + return objectPayload.events; + } + if (objectPayload.event) { + return [objectPayload.event]; + } + if (objectPayload.hook) { + return [objectPayload]; + } + return []; +} +function normalizeEvent(value) { + const raw = asObject(value); + if (!raw) { + return null; + } + const hook = raw.hook; + const id = asString(raw.id) ?? asString(raw.streamId) ?? asString(raw.stream_id); + if (!id || !isHookPayload(hook)) { + return null; + } + const dedupeId = asString(raw.eventId) ?? + asString(raw.event_id) ?? + asString(raw.dedupeId) ?? + asString(raw.dedupe_id) ?? + hook.sessionKey ?? + id; + return { + id, + dedupeId, + hook, + }; +} +function isPollingEvent(event) { + return event !== null; +} +function pruneRecentEvents(recentEvents, ttlMs) { + const cutoff = Date.now() - ttlMs; + for (const [eventId, timestamp] of recentEvents.entries()) { + if (timestamp < cutoff) { + recentEvents.delete(eventId); + } + } +} +function readyConfigOrNull(config) { + if (!config.baseUrl || !config.agentId || !config.agentToken || !config.hookUrl) { + return null; + } + return { + ...config, + baseUrl: config.baseUrl, + agentId: config.agentId, + agentToken: config.agentToken, + hookUrl: config.hookUrl, + }; +} +async function pollEvents(config, signal) { + const url = new URL(`/v1/agents/${encodeURIComponent(config.agentId)}/events`, config.baseUrl); + url.searchParams.set("consumerId", config.consumerId); + url.searchParams.set("wait", String(config.waitSeconds)); + const payload = await fetchJson(url, { + method: "GET", + headers: buildAuthHeaders(config.agentToken), + timeoutMs: config.requestTimeoutMs, + signal, + }); + return extractEvents(payload).map(normalizeEvent).filter(isPollingEvent); +} +async function ackEvent(config, eventId, signal) { + const url = new URL(`/v1/agents/${encodeURIComponent(config.agentId)}/events/${encodeURIComponent(eventId)}/ack`, config.baseUrl); + await fetchOk(url, { + method: "POST", + headers: buildAuthHeaders(config.agentToken), + timeoutMs: config.ackTimeoutMs, + signal, + }); +} +async function forwardHook(hookUrl, hooksToken, hook, timeoutMs, signal) { + await fetchOk(new URL(hookUrl), { + method: "POST", + headers: { + ...buildAuthHeaders(hooksToken), + "content-type": "application/json", + }, + body: JSON.stringify(hook), + timeoutMs, + signal, + }); +} +async function handleEvent({ api, config, hooksToken, event, recentEvents, signal, }) { + const alreadyForwarded = recentEvents.has(event.dedupeId); + if (!alreadyForwarded) { + await forwardHook(config.hookUrl, hooksToken, event.hook, config.ackTimeoutMs, signal); + recentEvents.set(event.dedupeId, Date.now()); + api.logger.debug(`[corall-polling] Forwarded event ${event.dedupeId} (${event.id}) to ${config.hookUrl}`); + } + await ackEvent(config, event.id, signal); +} +async function runLoop({ api, config, hooksToken, isCurrentConfig, signal, recentEvents, }) { + let backoffMs = config.errorBackoffMs; + while (!signal.aborted) { + if (!isCurrentConfig()) { + api.logger.info("[corall-polling] Runtime config changed; restarting poller"); + return; + } + try { + pruneRecentEvents(recentEvents, config.recentEventTtlMs); + const events = await pollEvents(config, signal); + if (events.length === 0) { + backoffMs = config.errorBackoffMs; + await sleep(config.idleDelayMs, signal); + continue; + } + for (const event of events) { + if (signal.aborted) { + return; + } + await handleEvent({ api, config, hooksToken, event, recentEvents, signal }); + } + backoffMs = config.errorBackoffMs; + } + catch (error) { + if (isAbortError(error)) { + return; + } + api.logger.warn(`[corall-polling] Poll cycle failed: ${error instanceof Error ? error.message : String(error)}`); + await sleep(backoffMs, signal); + backoffMs = Math.min(Math.max(backoffMs * 2, config.errorBackoffMs), config.maxErrorBackoffMs); + } + } +} +function runtimeIssueMessage(runtimeErrors, readyConfig) { + if (runtimeErrors.length > 0) { + return runtimeErrors.join("; "); + } + if (!readyConfig) { + return "runtime config is incomplete"; + } + return "unknown runtime issue"; +} +function sameReadyConfig(left, right) { + return (left.baseUrl === right.baseUrl && + left.agentId === right.agentId && + left.agentToken === right.agentToken && + left.consumerId === right.consumerId && + left.hookUrl === right.hookUrl); +} +async function runSupervisor({ api, config, signal, recentEvents, }) { + let lastIssue = null; + while (!signal.aborted) { + const runtimeConfig = materializeRuntimeConfig(config, api.config); + const runtimeErrors = validateRuntimeConfig(runtimeConfig, api.config); + const hooksToken = resolveHooksToken(api.config); + const readyConfig = readyConfigOrNull(runtimeConfig); + if (runtimeErrors.length > 0 || !hooksToken || !readyConfig) { + const issue = runtimeIssueMessage(runtimeErrors, readyConfig); + if (issue !== lastIssue) { + api.logger.warn(`[corall-polling] Waiting for config: ${issue}`); + lastIssue = issue; + } + await sleep(runtimeConfig.idleDelayMs, signal); + continue; + } + api.logger.info(`[corall-polling] Starting poller for agent ${readyConfig.agentId} using consumer ${readyConfig.consumerId}`); + await runLoop({ + api, + config: readyConfig, + hooksToken, + isCurrentConfig: () => { + const currentConfig = readyConfigOrNull(materializeRuntimeConfig(config, api.config)); + return currentConfig !== null && sameReadyConfig(currentConfig, readyConfig); + }, + signal, + recentEvents, + }); + } +} +export function createPollingService({ api, config }) { + let loopPromise = null; + let stopController = null; + const recentEvents = new Map(); + return { + async start() { + if (loopPromise) { + return; + } + stopController = new AbortController(); + loopPromise = runSupervisor({ + api, + config, + signal: stopController.signal, + recentEvents, + }) + .catch((error) => { + if (!isAbortError(error)) { + api.logger.error(`[corall-polling] Poller stopped unexpectedly: ${error instanceof Error ? error.message : String(error)}`); + } + }) + .finally(() => { + loopPromise = null; + stopController = null; + recentEvents.clear(); + }); + }, + async stop() { + if (!loopPromise || !stopController) { + return; + } + stopController.abort(); + try { + await loopPromise; + } + catch (error) { + if (!isAbortError(error)) { + throw error; + } + } + }, + }; +} +//# sourceMappingURL=service.js.map \ No newline at end of file diff --git a/plugins/corall-polling/dist/src/service.js.map b/plugins/corall-polling/dist/src/service.js.map new file mode 100644 index 0000000..f83ec2d --- /dev/null +++ b/plugins/corall-polling/dist/src/service.js.map @@ -0,0 +1 @@ +{"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACjG,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AA4BpE,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,KAAK,KAAK,IAAI,IAA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A2B,IAAI,CAAC;IAClD,MAAM,YAAY,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE/C,OAAO;QACL,KAAK,CAAC,KAAK;YACT,IAAI,WAAW,EAAE,CAAC;gBAChB,OAAO;YACT,CAAC;YAED,cAAc,GAAG,IAAI,eAAe,EAAE,CAAC;YAEvC,WAAW,GAAG,aAAa,CAAC;gBAC1B,GAAG;gBACH,MAAM;gBACN,MAAM,EAAE,cAAc,CAAC,MAAM;gBAC7B,YAAY;aACb,CAAC;iBACC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;gBACxB,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;oBACzB,GAAG,CAAC,MAAM,CAAC,KAAK,CACd,iDACE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CACvD,EAAE,CACH,CAAC;gBACJ,CAAC;YACH,CAAC,CAAC;iBACD,OAAO,CAAC,GAAG,EAAE;gBACZ,WAAW,GAAG,IAAI,CAAC;gBACnB,cAAc,GAAG,IAAI,CAAC;gBACtB,YAAY,CAAC,KAAK,EAAE,CAAC;YACvB,CAAC,CAAC,CAAC;QACP,CAAC;QAED,KAAK,CAAC,IAAI;YACR,IAAI,CAAC,WAAW,IAAI,CAAC,cAAc,EAAE,CAAC;gBACpC,OAAO;YACT,CAAC;YAED,cAAc,CAAC,KAAK,EAAE,CAAC;YAEvB,IAAI,CAAC;gBACH,MAAM,WAAW,CAAC;YACpB,CAAC;YAAC,OAAO,KAAc,EAAE,CAAC;gBACxB,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;oBACzB,MAAM,KAAK,CAAC;gBACd,CAAC;YACH,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"} \ No newline at end of file diff --git a/plugins/corall-polling/dist/src/types.d.ts b/plugins/corall-polling/dist/src/types.d.ts new file mode 100644 index 0000000..9c2ba72 --- /dev/null +++ b/plugins/corall-polling/dist/src/types.d.ts @@ -0,0 +1,63 @@ +export interface Logger { + debug(message: string): void; + info(message: string): void; + warn(message: string): void; + error(message: string): void; +} +export interface OpenClawConfig { + gateway?: { + port?: unknown; + }; + hooks?: { + token?: unknown; + }; +} +export interface RegisteredService { + id: string; + start(): Promise; + stop(): Promise; +} +export interface OpenClawPluginApi { + pluginConfig: unknown; + config: OpenClawConfig; + logger: Logger; + registerService(service: RegisteredService): void; +} +export interface PluginConfig { + baseUrl: string | undefined; + agentId: string | undefined; + agentToken: string | undefined; + credentialProfile: string; + consumerId: string | undefined; + waitSeconds: number; + hookUrl: string | undefined; + requestTimeoutMs: number; + ackTimeoutMs: number; + idleDelayMs: number; + errorBackoffMs: number; + maxErrorBackoffMs: number; + recentEventTtlMs: number; +} +export interface RuntimeConfig extends PluginConfig { + agentId: string | undefined; + agentToken: string | undefined; + consumerId: string; + hookUrl: string; +} +export interface ReadyRuntimeConfig extends RuntimeConfig { + baseUrl: string; + agentId: string; + agentToken: string; + hookUrl: string; +} +export interface HookPayload { + message: string; + name: string; + sessionKey: string; + deliver: boolean; +} +export interface PollingEvent { + id: string; + dedupeId: string; + hook: HookPayload; +} diff --git a/plugins/corall-polling/dist/src/types.js b/plugins/corall-polling/dist/src/types.js new file mode 100644 index 0000000..718fd38 --- /dev/null +++ b/plugins/corall-polling/dist/src/types.js @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=types.js.map \ No newline at end of file diff --git a/plugins/corall-polling/dist/src/types.js.map b/plugins/corall-polling/dist/src/types.js.map new file mode 100644 index 0000000..7b5fff8 --- /dev/null +++ b/plugins/corall-polling/dist/src/types.js.map @@ -0,0 +1 @@ +{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/plugins/corall-polling/dist/test/service.test.d.ts b/plugins/corall-polling/dist/test/service.test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/plugins/corall-polling/dist/test/service.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/plugins/corall-polling/dist/test/service.test.js b/plugins/corall-polling/dist/test/service.test.js new file mode 100644 index 0000000..3b06126 --- /dev/null +++ b/plugins/corall-polling/dist/test/service.test.js @@ -0,0 +1,532 @@ +import assert from "node:assert/strict"; +import fs from "node:fs/promises"; +import http, {} from "node:http"; +import os from "node:os"; +import path from "node:path"; +import test from "node:test"; +import { createPollingService } from "../src/service.js"; +test("polling service forwards hook payload and acks event", async () => { + const hookPayload = { + message: "You have a new order", + name: "Corall", + sessionKey: "hook:corall:order-1", + deliver: false, + }; + const forwardedHooks = []; + const acks = []; + let pollCount = 0; + const hookServer = http.createServer(async (req, res) => { + assert.equal(req.method, "POST"); + assert.equal(req.url, "/hooks/agent"); + assert.equal(req.headers.authorization, "Bearer hook-token"); + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + const eventbusServer = http.createServer(async (req, res) => { + assert.equal(req.headers.authorization, "Bearer agent-token"); + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + if (req.method === "GET") { + assert.equal(url.pathname, "/v1/agents/agent-1/events"); + assert.equal(url.searchParams.get("consumerId"), "test-consumer"); + pollCount += 1; + sendJson(res, 200, { + events: pollCount === 1 ? [{ id: "stream-1", hook: hookPayload }] : [], + }); + return; + } + assert.equal(req.method, "POST"); + assert.equal(url.pathname, "/v1/agents/agent-1/events/stream-1/ack"); + acks.push("stream-1"); + sendJson(res, 200, { ok: true }); + }); + await listen(hookServer); + await listen(eventbusServer); + const eventbusUrl = serverUrl(eventbusServer); + const hookUrl = `${serverUrl(hookServer)}/hooks/agent`; + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: eventbusUrl, + agentId: "agent-1", + agentToken: "agent-token", + credentialProfile: "provider", + consumerId: "test-consumer", + hookUrl, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + try { + await service.start(); + await waitFor(() => forwardedHooks.length === 1 && acks.length === 1); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, ["stream-1"]); + } + finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + } +}); +test("polling service deduplicates repeated delivery while acking every stream id", async () => { + const hookPayload = { + message: "You have a duplicated order", + name: "Corall", + sessionKey: "hook:corall:order-duplicate", + deliver: false, + }; + const forwardedHooks = []; + const acks = []; + let pollCount = 0; + const hookServer = http.createServer(async (req, res) => { + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + const eventbusServer = http.createServer((req, res) => { + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + if (req.method === "GET") { + pollCount += 1; + sendJson(res, 200, { + events: pollCount === 1 + ? [ + { id: "stream-1", eventId: "order.paid:order-duplicate", hook: hookPayload }, + { id: "stream-2", eventId: "order.paid:order-duplicate", hook: hookPayload }, + ] + : [], + }); + return; + } + assert.equal(req.method, "POST"); + acks.push(url.pathname); + sendJson(res, 200, { ok: true }); + }); + await listen(hookServer); + await listen(eventbusServer); + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: serverUrl(eventbusServer), + agentId: "agent-1", + agentToken: "agent-token", + credentialProfile: "provider", + consumerId: "test-consumer", + hookUrl: `${serverUrl(hookServer)}/hooks/agent`, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + try { + await service.start(); + await waitFor(() => forwardedHooks.length === 1 && acks.length === 2); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, [ + "/v1/agents/agent-1/events/stream-1/ack", + "/v1/agents/agent-1/events/stream-2/ack", + ]); + } + finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + } +}); +test("polling service recovers after poll request failure", async () => { + const hookPayload = { + message: "You have a recovered order", + name: "Corall", + sessionKey: "hook:corall:order-recovered", + deliver: false, + }; + const forwardedHooks = []; + const acks = []; + let pollCount = 0; + const hookServer = http.createServer(async (req, res) => { + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + const eventbusServer = http.createServer((req, res) => { + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + if (req.method === "GET") { + pollCount += 1; + if (pollCount === 1) { + sendJson(res, 503, { error: "temporary redis failure" }); + return; + } + sendJson(res, 200, { + events: pollCount === 2 + ? [{ id: "stream-recovered", eventId: "order.paid:order-recovered", hook: hookPayload }] + : [], + }); + return; + } + assert.equal(req.method, "POST"); + acks.push(url.pathname); + sendJson(res, 200, { ok: true }); + }); + await listen(hookServer); + await listen(eventbusServer); + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: serverUrl(eventbusServer), + agentId: "agent-1", + agentToken: "agent-token", + credentialProfile: "provider", + consumerId: "test-consumer", + hookUrl: `${serverUrl(hookServer)}/hooks/agent`, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + try { + await service.start(); + await waitFor(() => forwardedHooks.length === 1 && acks.length === 1); + assert.equal(pollCount >= 2, true); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, ["/v1/agents/agent-1/events/stream-recovered/ack"]); + } + finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + } +}); +test("polling service does not forward again when ack fails after hook delivery", async () => { + const hookPayload = { + message: "You have an ack retry order", + name: "Corall", + sessionKey: "hook:corall:order-ack-retry", + deliver: false, + }; + const forwardedHooks = []; + let ackAttempts = 0; + const hookServer = http.createServer(async (req, res) => { + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + const eventbusServer = http.createServer((req, res) => { + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + if (req.method === "GET") { + sendJson(res, 200, { + events: ackAttempts < 2 + ? [{ id: "stream-ack-retry", eventId: "order.paid:order-ack-retry", hook: hookPayload }] + : [], + }); + return; + } + assert.equal(req.method, "POST"); + ackAttempts += 1; + if (ackAttempts === 1) { + sendJson(res, 503, { error: "temporary ack failure" }); + return; + } + sendJson(res, 200, { ok: true }); + }); + await listen(hookServer); + await listen(eventbusServer); + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: serverUrl(eventbusServer), + agentId: "agent-1", + agentToken: "agent-token", + credentialProfile: "provider", + consumerId: "test-consumer", + hookUrl: `${serverUrl(hookServer)}/hooks/agent`, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + try { + await service.start(); + await waitFor(() => forwardedHooks.length === 1 && ackAttempts === 2); + assert.deepEqual(forwardedHooks[0], hookPayload); + } + finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + } +}); +test("polling service starts after credentials add agent id", async () => { + const homeDir = await fs.mkdtemp(path.join(os.tmpdir(), "corall-polling-home-")); + const previousHome = process.env.HOME; + const profile = "late-provider"; + const hookPayload = { + message: "You have a new order", + name: "Corall", + sessionKey: "hook:corall:order-late", + deliver: false, + }; + const forwardedHooks = []; + const acks = []; + let pollCount = 0; + process.env.HOME = homeDir; + const hookServer = http.createServer(async (req, res) => { + assert.equal(req.method, "POST"); + assert.equal(req.url, "/hooks/agent"); + assert.equal(req.headers.authorization, "Bearer hook-token"); + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + const eventbusServer = http.createServer(async (req, res) => { + assert.equal(req.headers.authorization, "Bearer agent-token"); + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + if (req.method === "GET") { + assert.equal(url.pathname, "/v1/agents/agent-late/events"); + assert.match(url.searchParams.get("consumerId") ?? "", /^corall-polling:agent-late:/); + pollCount += 1; + sendJson(res, 200, { + events: pollCount === 1 ? [{ id: "stream-late", hook: hookPayload }] : [], + }); + return; + } + assert.equal(req.method, "POST"); + assert.equal(url.pathname, "/v1/agents/agent-late/events/stream-late/ack"); + acks.push("stream-late"); + sendJson(res, 200, { ok: true }); + }); + await listen(hookServer); + await listen(eventbusServer); + const eventbusUrl = serverUrl(eventbusServer); + const hookUrl = `${serverUrl(hookServer)}/hooks/agent`; + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: eventbusUrl, + agentId: undefined, + agentToken: "agent-token", + credentialProfile: profile, + consumerId: undefined, + hookUrl, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + try { + await service.start(); + await delayMs(30); + assert.equal(pollCount, 0); + const credentialsDir = path.join(homeDir, ".corall", "credentials"); + await fs.mkdir(credentialsDir, { recursive: true }); + await fs.writeFile(path.join(credentialsDir, `${profile}.json`), JSON.stringify({ agentId: "agent-late" })); + await waitFor(() => forwardedHooks.length === 1 && acks.length === 1, 2_000); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, ["stream-late"]); + } + finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + if (previousHome === undefined) { + delete process.env.HOME; + } + else { + process.env.HOME = previousHome; + } + await fs.rm(homeDir, { recursive: true, force: true }); + } +}); +test("polling service restarts when credential agent id changes", async () => { + const homeDir = await fs.mkdtemp(path.join(os.tmpdir(), "corall-polling-home-")); + const previousHome = process.env.HOME; + const profile = "switch-provider"; + const hookPayload = { + message: "You have a switched order", + name: "Corall", + sessionKey: "hook:corall:order-switch", + deliver: false, + }; + const forwardedHooks = []; + const acks = []; + const polledPaths = []; + let sentSwitchedEvent = false; + process.env.HOME = homeDir; + await writeCredential(homeDir, profile, "agent-one"); + const hookServer = http.createServer(async (req, res) => { + assert.equal(req.method, "POST"); + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + const eventbusServer = http.createServer((req, res) => { + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + if (req.method === "GET") { + polledPaths.push(url.pathname); + if (url.pathname === "/v1/agents/agent-two/events" && !sentSwitchedEvent) { + sentSwitchedEvent = true; + sendJson(res, 200, { events: [{ id: "stream-two", hook: hookPayload }] }); + return; + } + sendJson(res, 200, { events: [] }); + return; + } + assert.equal(req.method, "POST"); + assert.equal(url.pathname, "/v1/agents/agent-two/events/stream-two/ack"); + acks.push("stream-two"); + sendJson(res, 200, { ok: true }); + }); + await listen(hookServer); + await listen(eventbusServer); + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: serverUrl(eventbusServer), + agentId: undefined, + agentToken: "agent-token", + credentialProfile: profile, + consumerId: undefined, + hookUrl: `${serverUrl(hookServer)}/hooks/agent`, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + try { + await service.start(); + await waitFor(() => polledPaths.includes("/v1/agents/agent-one/events"), 1_000); + await writeCredential(homeDir, profile, "agent-two"); + await waitFor(() => forwardedHooks.length === 1 && acks.length === 1, 2_000); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, ["stream-two"]); + assert.ok(polledPaths.includes("/v1/agents/agent-two/events")); + } + finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + if (previousHome === undefined) { + delete process.env.HOME; + } + else { + process.env.HOME = previousHome; + } + await fs.rm(homeDir, { recursive: true, force: true }); + } +}); +function testApi() { + return { + config: { + hooks: { token: "hook-token" }, + gateway: { port: 18789 }, + }, + logger: { + debug(_message) { }, + info(_message) { }, + warn(_message) { }, + error(_message) { }, + }, + }; +} +function listen(server) { + return new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", () => { + server.off("error", reject); + resolve(); + }); + }); +} +function close(server) { + return new Promise((resolve, reject) => { + server.close((error) => (error ? reject(error) : resolve())); + }); +} +function serverUrl(server) { + const address = server.address(); + assert.equal(typeof address, "object"); + assert.notEqual(address, null); + const info = address; + return `http://${info.address}:${info.port}`; +} +async function readJson(req) { + const chunks = []; + for await (const chunk of req) { + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))); + } + return JSON.parse(Buffer.concat(chunks).toString("utf8")); +} +function sendJson(res, status, body) { + res.writeHead(status, { "content-type": "application/json" }); + res.end(JSON.stringify(body)); +} +async function delayMs(ms) { + await new Promise((resolve) => { + setTimeout(resolve, ms); + }); +} +async function writeCredential(homeDir, profile, agentId) { + const credentialsDir = path.join(homeDir, ".corall", "credentials"); + await fs.mkdir(credentialsDir, { recursive: true }); + await fs.writeFile(path.join(credentialsDir, `${profile}.json`), JSON.stringify({ agentId })); +} +async function waitFor(predicate, timeoutMs = 1_000) { + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + if (predicate()) { + return; + } + await new Promise((resolve) => { + setTimeout(resolve, 10); + }); + } + throw new Error("timed out waiting for predicate"); +} +//# sourceMappingURL=service.test.js.map \ No newline at end of file diff --git a/plugins/corall-polling/dist/test/service.test.js.map b/plugins/corall-polling/dist/test/service.test.js.map new file mode 100644 index 0000000..930d0ba --- /dev/null +++ b/plugins/corall-polling/dist/test/service.test.js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AAc,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;IACpE,MAAM,EAAE,CAAC,KAAK,CAAC,cAAc,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACpD,MAAM,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,GAAG,OAAO,OAAO,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;AAChG,CAAC;AAED,KAAK,UAAU,OAAO,CAAC,SAAwB,EAAE,SAAS,GAAG,KAAK;IAChE,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;IACxC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,EAAE,CAAC;QAC7B,IAAI,SAAS,EAAE,EAAE,CAAC;YAChB,OAAO;QACT,CAAC;QACD,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;YAClC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;IACL,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;AACrD,CAAC"} \ No newline at end of file diff --git a/plugins/corall-polling/index.ts b/plugins/corall-polling/index.ts new file mode 100644 index 0000000..3d0dbfd --- /dev/null +++ b/plugins/corall-polling/index.ts @@ -0,0 +1,22 @@ +import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry"; + +import { resolvePluginConfig } from "./src/config.js"; +import { createPollingService } from "./src/service.js"; +import type { OpenClawPluginApi } from "./src/types.js"; + +export default definePluginEntry({ + id: "corall-polling", + name: "Corall Polling", + description: + "Poll Corall resident events and forward hook payloads to the local OpenClaw hook endpoint.", + register(api: OpenClawPluginApi): void { + const config = resolvePluginConfig(api.pluginConfig); + const service = createPollingService({ api, config }); + + api.registerService({ + id: "corall-polling", + start: service.start, + stop: service.stop, + }); + }, +}); diff --git a/plugins/corall-polling/openclaw.plugin.json b/plugins/corall-polling/openclaw.plugin.json new file mode 100644 index 0000000..fa709fc --- /dev/null +++ b/plugins/corall-polling/openclaw.plugin.json @@ -0,0 +1,108 @@ +{ + "id": "corall-polling", + "uiHints": { + "baseUrl": { + "label": "Eventbus Base URL", + "placeholder": "http://127.0.0.1:8080" + }, + "agentId": { + "label": "Corall Agent ID" + }, + "agentToken": { + "label": "Corall Agent Token", + "sensitive": true + }, + "consumerId": { + "label": "Consumer ID", + "help": "Stable poller identity. Defaults to a hostname-based value when omitted." + }, + "waitSeconds": { + "label": "Long Poll Wait (sec)", + "advanced": true + }, + "hookUrl": { + "label": "Local Hook URL", + "placeholder": "http://127.0.0.1:18789/hooks/agent", + "advanced": true + }, + "requestTimeoutMs": { + "label": "Poll Timeout (ms)", + "advanced": true + }, + "ackTimeoutMs": { + "label": "Ack Timeout (ms)", + "advanced": true + }, + "idleDelayMs": { + "label": "Idle Delay (ms)", + "advanced": true + }, + "errorBackoffMs": { + "label": "Error Backoff (ms)", + "advanced": true + }, + "maxErrorBackoffMs": { + "label": "Max Error Backoff (ms)", + "advanced": true + } + }, + "configSchema": { + "type": "object", + "additionalProperties": false, + "properties": { + "baseUrl": { + "type": "string", + "minLength": 1 + }, + "agentId": { + "type": "string", + "minLength": 1 + }, + "agentToken": { + "type": "string", + "minLength": 1 + }, + "credentialProfile": { + "type": "string", + "minLength": 1 + }, + "consumerId": { + "type": "string", + "minLength": 1 + }, + "waitSeconds": { + "type": "integer", + "minimum": 0, + "maximum": 60 + }, + "hookUrl": { + "type": "string", + "minLength": 1 + }, + "requestTimeoutMs": { + "type": "integer", + "minimum": 1000 + }, + "ackTimeoutMs": { + "type": "integer", + "minimum": 1000 + }, + "idleDelayMs": { + "type": "integer", + "minimum": 0 + }, + "errorBackoffMs": { + "type": "integer", + "minimum": 0 + }, + "maxErrorBackoffMs": { + "type": "integer", + "minimum": 0 + }, + "recentEventTtlMs": { + "type": "integer", + "minimum": 0 + } + } + } +} diff --git a/plugins/corall-polling/package-lock.json b/plugins/corall-polling/package-lock.json new file mode 100644 index 0000000..9e00568 --- /dev/null +++ b/plugins/corall-polling/package-lock.json @@ -0,0 +1,50 @@ +{ + "name": "corall-polling", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "corall-polling", + "version": "0.1.0", + "devDependencies": { + "@types/node": "^22.15.0", + "typescript": "^5.8.0" + }, + "engines": { + "node": ">=22" + } + }, + "node_modules/@types/node": { + "version": "22.19.17", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.17.tgz", + "integrity": "sha512-wGdMcf+vPYM6jikpS/qhg6WiqSV/OhG+jeeHT/KlVqxYfD40iYJf9/AE1uQxVWFvU7MipKRkRv8NSHiCGgPr8Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~6.21.0" + } + }, + "node_modules/typescript": { + "version": "5.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", + "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici-types": { + "version": "6.21.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", + "dev": true, + "license": "MIT" + } + } +} diff --git a/plugins/corall-polling/package.json b/plugins/corall-polling/package.json new file mode 100644 index 0000000..856aa6d --- /dev/null +++ b/plugins/corall-polling/package.json @@ -0,0 +1,33 @@ +{ + "name": "corall-polling", + "version": "0.1.0", + "description": "OpenClaw plugin that polls Corall resident events and forwards hook payloads locally.", + "type": "module", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "engines": { + "node": ">=22" + }, + "files": [ + "README.md", + "dist", + "openclaw.plugin.json", + "src", + "tsconfig.json" + ], + "scripts": { + "build": "tsc -p tsconfig.json", + "prepack": "npm run build", + "typecheck": "tsc -p tsconfig.json --noEmit", + "test": "npm run build && node --test dist/test/*.test.js" + }, + "devDependencies": { + "@types/node": "^22.15.0", + "typescript": "^5.8.0" + }, + "openclaw": { + "extensions": [ + "./dist/index.js" + ] + } +} diff --git a/plugins/corall-polling/src/config.ts b/plugins/corall-polling/src/config.ts new file mode 100644 index 0000000..a011ba0 --- /dev/null +++ b/plugins/corall-polling/src/config.ts @@ -0,0 +1,132 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +import type { OpenClawConfig, PluginConfig, RuntimeConfig } from "./types.js"; + +const DEFAULT_GATEWAY_PORT = 18789; +const DEFAULT_HOOK_URL_PATH = "/hooks/agent"; +const DEFAULT_WAIT_SECONDS = 30; +const DEFAULT_IDLE_DELAY_MS = 1000; +const DEFAULT_ACK_TIMEOUT_MS = 10_000; +const DEFAULT_ERROR_BACKOFF_MS = 2_000; +const DEFAULT_MAX_ERROR_BACKOFF_MS = 30_000; +const DEFAULT_RECENT_EVENT_TTL_MS = 10 * 60 * 1000; +const DEFAULT_CREDENTIAL_PROFILE = "provider"; + +type JsonObject = Record; + +function asObject(value: unknown): JsonObject { + return value !== null && typeof value === "object" && !Array.isArray(value) + ? (value as JsonObject) + : {}; +} + +function asString(value: unknown): string | undefined { + return typeof value === "string" && value.trim() ? value.trim() : undefined; +} + +function asInteger(value: unknown, fallback: number): number { + return Number.isInteger(value) && typeof value === "number" && value >= 0 ? value : fallback; +} + +function stripTrailingSlashes(value: string): string { + return value.replace(/\/+$/, ""); +} + +export function resolvePluginConfig(rawValue: unknown): PluginConfig { + const raw = asObject(rawValue); + const waitSeconds = Math.min(asInteger(raw.waitSeconds, DEFAULT_WAIT_SECONDS), 60); + const rawAgentId = asString(raw.agentId); + const rawBaseUrl = asString(raw.baseUrl); + const rawConsumerId = asString(raw.consumerId); + + return { + baseUrl: rawBaseUrl ? stripTrailingSlashes(rawBaseUrl) : undefined, + agentId: rawAgentId, + agentToken: asString(raw.agentToken), + credentialProfile: asString(raw.credentialProfile) ?? DEFAULT_CREDENTIAL_PROFILE, + consumerId: rawConsumerId, + waitSeconds, + hookUrl: asString(raw.hookUrl), + requestTimeoutMs: Math.max( + asInteger(raw.requestTimeoutMs, waitSeconds * 1000 + 15_000), + waitSeconds * 1000 + 1_000, + ), + ackTimeoutMs: asInteger(raw.ackTimeoutMs, DEFAULT_ACK_TIMEOUT_MS), + idleDelayMs: asInteger(raw.idleDelayMs, DEFAULT_IDLE_DELAY_MS), + errorBackoffMs: asInteger(raw.errorBackoffMs, DEFAULT_ERROR_BACKOFF_MS), + maxErrorBackoffMs: Math.max( + asInteger(raw.maxErrorBackoffMs, DEFAULT_MAX_ERROR_BACKOFF_MS), + asInteger(raw.errorBackoffMs, DEFAULT_ERROR_BACKOFF_MS), + ), + recentEventTtlMs: asInteger(raw.recentEventTtlMs, DEFAULT_RECENT_EVENT_TTL_MS), + }; +} + +export function materializeRuntimeConfig( + pluginConfig: PluginConfig, + openclawConfig: OpenClawConfig, +): RuntimeConfig { + const agentId = pluginConfig.agentId ?? readAgentIdFromCredentials(pluginConfig.credentialProfile); + const agentToken = pluginConfig.agentToken ?? resolveHooksToken(openclawConfig); + const consumerId = + pluginConfig.consumerId ?? `corall-polling:${agentId ?? "unknown"}:${os.hostname()}`; + + return { + ...pluginConfig, + agentId, + agentToken, + consumerId, + hookUrl: resolveHookUrl(openclawConfig, pluginConfig), + }; +} + +export function resolveHookUrl(openclawConfig: OpenClawConfig, pluginConfig: PluginConfig): string { + if (pluginConfig.hookUrl) { + return pluginConfig.hookUrl; + } + + const gateway = asObject(openclawConfig.gateway); + const port = asInteger(gateway.port, DEFAULT_GATEWAY_PORT); + return `http://127.0.0.1:${port}${DEFAULT_HOOK_URL_PATH}`; +} + +export function resolveHooksToken(openclawConfig: OpenClawConfig): string | undefined { + const hooks = asObject(openclawConfig.hooks); + return asString(hooks.token); +} + +export function validateRuntimeConfig( + runtimeConfig: RuntimeConfig, + openclawConfig: OpenClawConfig, +): string[] { + const errors: string[] = []; + + if (!runtimeConfig.baseUrl) { + errors.push("config.baseUrl is required"); + } + if (!runtimeConfig.agentId) { + errors.push("config.agentId is required or must exist in ~/.corall/credentials/.json"); + } + if (!runtimeConfig.agentToken) { + errors.push("config.agentToken is required or must match hooks.token"); + } + if (!resolveHooksToken(openclawConfig)) { + errors.push("hooks.token is missing from the active OpenClaw config"); + } + + return errors; +} + +function readAgentIdFromCredentials(profile: string): string | undefined { + const credentialsPath = path.join(os.homedir(), ".corall", "credentials", `${profile}.json`); + + try { + const raw = fs.readFileSync(credentialsPath, "utf8"); + const parsed = JSON.parse(raw) as unknown; + return asString(asObject(parsed).agentId); + } catch { + return undefined; + } +} diff --git a/plugins/corall-polling/src/http.ts b/plugins/corall-polling/src/http.ts new file mode 100644 index 0000000..5681083 --- /dev/null +++ b/plugins/corall-polling/src/http.ts @@ -0,0 +1,102 @@ +function abortError(message: string): Error { + const error = new Error(message); + error.name = "AbortError"; + return error; +} + +export function isAbortError(error: unknown): boolean { + return error instanceof Error && error.name === "AbortError"; +} + +export async function sleep(ms: number, signal?: AbortSignal): Promise { + if (ms <= 0) { + return; + } + + await new Promise((resolve, reject) => { + const cleanup = (): void => { + clearTimeout(timeout); + signal?.removeEventListener("abort", onAbort); + }; + + const onAbort = (): void => { + cleanup(); + reject(abortError("Operation aborted")); + }; + + const timeout = setTimeout(() => { + cleanup(); + resolve(); + }, ms); + + if (signal?.aborted) { + cleanup(); + reject(abortError("Operation aborted")); + return; + } + + signal?.addEventListener("abort", onAbort, { once: true }); + }); +} + +interface FetchWithTimeoutOptions extends RequestInit { + timeoutMs: number; + signal?: AbortSignal; +} + +export async function fetchWithTimeout( + url: URL, + options: FetchWithTimeoutOptions, +): Promise { + const { timeoutMs, signal, ...fetchOptions } = options; + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), timeoutMs); + const onAbort = (): void => controller.abort(); + + try { + if (signal?.aborted) { + throw abortError("Operation aborted"); + } + + signal?.addEventListener("abort", onAbort, { once: true }); + + return await fetch(url, { + ...fetchOptions, + signal: controller.signal, + }); + } catch (error: unknown) { + if (controller.signal.aborted || signal?.aborted) { + throw abortError(`Request aborted for ${url.toString()}`); + } + throw error; + } finally { + clearTimeout(timeout); + signal?.removeEventListener("abort", onAbort); + } +} + +export async function fetchJson(url: URL, options: FetchWithTimeoutOptions): Promise { + const response = await fetchWithTimeout(url, options); + const bodyText = await response.text(); + + if (!response.ok) { + const details = bodyText ? `: ${bodyText}` : ""; + throw new Error(`HTTP ${response.status} ${response.statusText}${details}`); + } + + if (!bodyText) { + return null; + } + + return JSON.parse(bodyText) as unknown; +} + +export async function fetchOk(url: URL, options: FetchWithTimeoutOptions): Promise { + const response = await fetchWithTimeout(url, options); + const bodyText = await response.text(); + + if (!response.ok) { + const details = bodyText ? `: ${bodyText}` : ""; + throw new Error(`HTTP ${response.status} ${response.statusText}${details}`); + } +} diff --git a/plugins/corall-polling/src/openclaw-plugin-sdk.d.ts b/plugins/corall-polling/src/openclaw-plugin-sdk.d.ts new file mode 100644 index 0000000..784956e --- /dev/null +++ b/plugins/corall-polling/src/openclaw-plugin-sdk.d.ts @@ -0,0 +1,12 @@ +declare module "openclaw/plugin-sdk/plugin-entry" { + import type { OpenClawPluginApi } from "./types.js"; + + export interface PluginEntry { + id: string; + name: string; + description: string; + register(api: OpenClawPluginApi): void; + } + + export function definePluginEntry(entry: PluginEntry): PluginEntry; +} diff --git a/plugins/corall-polling/src/service.ts b/plugins/corall-polling/src/service.ts new file mode 100644 index 0000000..16ace9d --- /dev/null +++ b/plugins/corall-polling/src/service.ts @@ -0,0 +1,406 @@ +import { materializeRuntimeConfig, resolveHooksToken, validateRuntimeConfig } from "./config.js"; +import { fetchJson, fetchOk, isAbortError, sleep } from "./http.js"; +import type { + HookPayload, + OpenClawConfig, + PluginConfig, + PollingEvent, + ReadyRuntimeConfig, + RuntimeConfig, + Logger, +} from "./types.js"; + +interface PollingApi { + config: OpenClawConfig; + logger: Logger; +} + +interface PollingService { + start(): Promise; + stop(): Promise; +} + +interface CreatePollingServiceOptions { + api: PollingApi; + config: PluginConfig; +} + +type JsonObject = Record; + +function asObject(value: unknown): JsonObject | null { + return value !== null && typeof value === "object" && !Array.isArray(value) + ? (value as JsonObject) + : null; +} + +function asString(value: unknown): string | undefined { + return typeof value === "string" && value.trim() ? value.trim() : undefined; +} + +function isHookPayload(value: unknown): value is HookPayload { + const hook = asObject(value); + return Boolean( + hook && + typeof hook.message === "string" && + typeof hook.name === "string" && + typeof hook.sessionKey === "string" && + typeof hook.deliver === "boolean", + ); +} + +function buildAuthHeaders(token: string): Record { + return { + authorization: `Bearer ${token}`, + accept: "application/json", + }; +} + +function extractEvents(payload: unknown): unknown[] { + if (Array.isArray(payload)) { + return payload; + } + + const objectPayload = asObject(payload); + if (!objectPayload) { + return []; + } + + if (Array.isArray(objectPayload.events)) { + return objectPayload.events; + } + + if (objectPayload.event) { + return [objectPayload.event]; + } + + if (objectPayload.hook) { + return [objectPayload]; + } + + return []; +} + +function normalizeEvent(value: unknown): PollingEvent | null { + const raw = asObject(value); + if (!raw) { + return null; + } + + const hook = raw.hook; + const id = asString(raw.id) ?? asString(raw.streamId) ?? asString(raw.stream_id); + if (!id || !isHookPayload(hook)) { + return null; + } + + const dedupeId = + asString(raw.eventId) ?? + asString(raw.event_id) ?? + asString(raw.dedupeId) ?? + asString(raw.dedupe_id) ?? + hook.sessionKey ?? + id; + + return { + id, + dedupeId, + hook, + }; +} + +function isPollingEvent(event: PollingEvent | null): event is PollingEvent { + return event !== null; +} + +function pruneRecentEvents(recentEvents: Map, ttlMs: number): void { + const cutoff = Date.now() - ttlMs; + for (const [eventId, timestamp] of recentEvents.entries()) { + if (timestamp < cutoff) { + recentEvents.delete(eventId); + } + } +} + +function readyConfigOrNull(config: RuntimeConfig): ReadyRuntimeConfig | null { + if (!config.baseUrl || !config.agentId || !config.agentToken || !config.hookUrl) { + return null; + } + + return { + ...config, + baseUrl: config.baseUrl, + agentId: config.agentId, + agentToken: config.agentToken, + hookUrl: config.hookUrl, + }; +} + +async function pollEvents( + config: ReadyRuntimeConfig, + signal: AbortSignal, +): Promise { + const url = new URL(`/v1/agents/${encodeURIComponent(config.agentId)}/events`, config.baseUrl); + url.searchParams.set("consumerId", config.consumerId); + url.searchParams.set("wait", String(config.waitSeconds)); + + const payload = await fetchJson(url, { + method: "GET", + headers: buildAuthHeaders(config.agentToken), + timeoutMs: config.requestTimeoutMs, + signal, + }); + + return extractEvents(payload).map(normalizeEvent).filter(isPollingEvent); +} + +async function ackEvent( + config: ReadyRuntimeConfig, + eventId: string, + signal: AbortSignal, +): Promise { + const url = new URL( + `/v1/agents/${encodeURIComponent(config.agentId)}/events/${encodeURIComponent(eventId)}/ack`, + config.baseUrl, + ); + + await fetchOk(url, { + method: "POST", + headers: buildAuthHeaders(config.agentToken), + timeoutMs: config.ackTimeoutMs, + signal, + }); +} + +async function forwardHook( + hookUrl: string, + hooksToken: string, + hook: HookPayload, + timeoutMs: number, + signal: AbortSignal, +): Promise { + await fetchOk(new URL(hookUrl), { + method: "POST", + headers: { + ...buildAuthHeaders(hooksToken), + "content-type": "application/json", + }, + body: JSON.stringify(hook), + timeoutMs, + signal, + }); +} + +interface HandleEventOptions { + api: PollingApi; + config: ReadyRuntimeConfig; + hooksToken: string; + event: PollingEvent; + recentEvents: Map; + signal: AbortSignal; +} + +async function handleEvent({ + api, + config, + hooksToken, + event, + recentEvents, + signal, +}: HandleEventOptions): Promise { + const alreadyForwarded = recentEvents.has(event.dedupeId); + + if (!alreadyForwarded) { + await forwardHook(config.hookUrl, hooksToken, event.hook, config.ackTimeoutMs, signal); + recentEvents.set(event.dedupeId, Date.now()); + api.logger.debug( + `[corall-polling] Forwarded event ${event.dedupeId} (${event.id}) to ${config.hookUrl}`, + ); + } + + await ackEvent(config, event.id, signal); +} + +interface RunLoopOptions { + api: PollingApi; + config: ReadyRuntimeConfig; + hooksToken: string; + isCurrentConfig(): boolean; + signal: AbortSignal; + recentEvents: Map; +} + +interface SupervisorOptions { + api: PollingApi; + config: PluginConfig; + signal: AbortSignal; + recentEvents: Map; +} + +async function runLoop({ + api, + config, + hooksToken, + isCurrentConfig, + signal, + recentEvents, +}: RunLoopOptions): Promise { + let backoffMs = config.errorBackoffMs; + + while (!signal.aborted) { + if (!isCurrentConfig()) { + api.logger.info("[corall-polling] Runtime config changed; restarting poller"); + return; + } + + try { + pruneRecentEvents(recentEvents, config.recentEventTtlMs); + + const events = await pollEvents(config, signal); + if (events.length === 0) { + backoffMs = config.errorBackoffMs; + await sleep(config.idleDelayMs, signal); + continue; + } + + for (const event of events) { + if (signal.aborted) { + return; + } + await handleEvent({ api, config, hooksToken, event, recentEvents, signal }); + } + + backoffMs = config.errorBackoffMs; + } catch (error: unknown) { + if (isAbortError(error)) { + return; + } + + api.logger.warn( + `[corall-polling] Poll cycle failed: ${ + error instanceof Error ? error.message : String(error) + }`, + ); + await sleep(backoffMs, signal); + backoffMs = Math.min( + Math.max(backoffMs * 2, config.errorBackoffMs), + config.maxErrorBackoffMs, + ); + } + } +} + +function runtimeIssueMessage(runtimeErrors: string[], readyConfig: ReadyRuntimeConfig | null): string { + if (runtimeErrors.length > 0) { + return runtimeErrors.join("; "); + } + + if (!readyConfig) { + return "runtime config is incomplete"; + } + + return "unknown runtime issue"; +} + +function sameReadyConfig(left: ReadyRuntimeConfig, right: ReadyRuntimeConfig): boolean { + return ( + left.baseUrl === right.baseUrl && + left.agentId === right.agentId && + left.agentToken === right.agentToken && + left.consumerId === right.consumerId && + left.hookUrl === right.hookUrl + ); +} + +async function runSupervisor({ + api, + config, + signal, + recentEvents, +}: SupervisorOptions): Promise { + let lastIssue: string | null = null; + + while (!signal.aborted) { + const runtimeConfig = materializeRuntimeConfig(config, api.config); + const runtimeErrors = validateRuntimeConfig(runtimeConfig, api.config); + const hooksToken = resolveHooksToken(api.config); + const readyConfig = readyConfigOrNull(runtimeConfig); + + if (runtimeErrors.length > 0 || !hooksToken || !readyConfig) { + const issue = runtimeIssueMessage(runtimeErrors, readyConfig); + if (issue !== lastIssue) { + api.logger.warn(`[corall-polling] Waiting for config: ${issue}`); + lastIssue = issue; + } + await sleep(runtimeConfig.idleDelayMs, signal); + continue; + } + + api.logger.info( + `[corall-polling] Starting poller for agent ${readyConfig.agentId} using consumer ${readyConfig.consumerId}`, + ); + + await runLoop({ + api, + config: readyConfig, + hooksToken, + isCurrentConfig: () => { + const currentConfig = readyConfigOrNull(materializeRuntimeConfig(config, api.config)); + return currentConfig !== null && sameReadyConfig(currentConfig, readyConfig); + }, + signal, + recentEvents, + }); + } +} + +export function createPollingService({ api, config }: CreatePollingServiceOptions): PollingService { + let loopPromise: Promise | null = null; + let stopController: AbortController | null = null; + const recentEvents = new Map(); + + return { + async start(): Promise { + if (loopPromise) { + return; + } + + stopController = new AbortController(); + + loopPromise = runSupervisor({ + api, + config, + signal: stopController.signal, + recentEvents, + }) + .catch((error: unknown) => { + if (!isAbortError(error)) { + api.logger.error( + `[corall-polling] Poller stopped unexpectedly: ${ + error instanceof Error ? error.message : String(error) + }`, + ); + } + }) + .finally(() => { + loopPromise = null; + stopController = null; + recentEvents.clear(); + }); + }, + + async stop(): Promise { + if (!loopPromise || !stopController) { + return; + } + + stopController.abort(); + + try { + await loopPromise; + } catch (error: unknown) { + if (!isAbortError(error)) { + throw error; + } + } + }, + }; +} diff --git a/plugins/corall-polling/src/types.ts b/plugins/corall-polling/src/types.ts new file mode 100644 index 0000000..195e20f --- /dev/null +++ b/plugins/corall-polling/src/types.ts @@ -0,0 +1,71 @@ +export interface Logger { + debug(message: string): void; + info(message: string): void; + warn(message: string): void; + error(message: string): void; +} + +export interface OpenClawConfig { + gateway?: { + port?: unknown; + }; + hooks?: { + token?: unknown; + }; +} + +export interface RegisteredService { + id: string; + start(): Promise; + stop(): Promise; +} + +export interface OpenClawPluginApi { + pluginConfig: unknown; + config: OpenClawConfig; + logger: Logger; + registerService(service: RegisteredService): void; +} + +export interface PluginConfig { + baseUrl: string | undefined; + agentId: string | undefined; + agentToken: string | undefined; + credentialProfile: string; + consumerId: string | undefined; + waitSeconds: number; + hookUrl: string | undefined; + requestTimeoutMs: number; + ackTimeoutMs: number; + idleDelayMs: number; + errorBackoffMs: number; + maxErrorBackoffMs: number; + recentEventTtlMs: number; +} + +export interface RuntimeConfig extends PluginConfig { + agentId: string | undefined; + agentToken: string | undefined; + consumerId: string; + hookUrl: string; +} + +export interface ReadyRuntimeConfig extends RuntimeConfig { + baseUrl: string; + agentId: string; + agentToken: string; + hookUrl: string; +} + +export interface HookPayload { + message: string; + name: string; + sessionKey: string; + deliver: boolean; +} + +export interface PollingEvent { + id: string; + dedupeId: string; + hook: HookPayload; +} diff --git a/plugins/corall-polling/test/service.test.ts b/plugins/corall-polling/test/service.test.ts new file mode 100644 index 0000000..71549bf --- /dev/null +++ b/plugins/corall-polling/test/service.test.ts @@ -0,0 +1,593 @@ +import assert from "node:assert/strict"; +import fs from "node:fs/promises"; +import http, { type IncomingMessage, type Server, type ServerResponse } from "node:http"; +import type { AddressInfo } from "node:net"; +import os from "node:os"; +import path from "node:path"; +import test from "node:test"; + +import { createPollingService } from "../src/service.js"; +import type { HookPayload, Logger, OpenClawConfig } from "../src/types.js"; + +test("polling service forwards hook payload and acks event", async () => { + const hookPayload: HookPayload = { + message: "You have a new order", + name: "Corall", + sessionKey: "hook:corall:order-1", + deliver: false, + }; + const forwardedHooks: unknown[] = []; + const acks: string[] = []; + let pollCount = 0; + + const hookServer = http.createServer(async (req: IncomingMessage, res: ServerResponse) => { + assert.equal(req.method, "POST"); + assert.equal(req.url, "/hooks/agent"); + assert.equal(req.headers.authorization, "Bearer hook-token"); + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + + const eventbusServer = http.createServer(async (req: IncomingMessage, res: ServerResponse) => { + assert.equal(req.headers.authorization, "Bearer agent-token"); + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + + if (req.method === "GET") { + assert.equal(url.pathname, "/v1/agents/agent-1/events"); + assert.equal(url.searchParams.get("consumerId"), "test-consumer"); + pollCount += 1; + sendJson(res, 200, { + events: pollCount === 1 ? [{ id: "stream-1", hook: hookPayload }] : [], + }); + return; + } + + assert.equal(req.method, "POST"); + assert.equal(url.pathname, "/v1/agents/agent-1/events/stream-1/ack"); + acks.push("stream-1"); + sendJson(res, 200, { ok: true }); + }); + + await listen(hookServer); + await listen(eventbusServer); + + const eventbusUrl = serverUrl(eventbusServer); + const hookUrl = `${serverUrl(hookServer)}/hooks/agent`; + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: eventbusUrl, + agentId: "agent-1", + agentToken: "agent-token", + credentialProfile: "provider", + consumerId: "test-consumer", + hookUrl, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + + try { + await service.start(); + await waitFor(() => forwardedHooks.length === 1 && acks.length === 1); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, ["stream-1"]); + } finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + } +}); + +test("polling service deduplicates repeated delivery while acking every stream id", async () => { + const hookPayload: HookPayload = { + message: "You have a duplicated order", + name: "Corall", + sessionKey: "hook:corall:order-duplicate", + deliver: false, + }; + const forwardedHooks: unknown[] = []; + const acks: string[] = []; + let pollCount = 0; + + const hookServer = http.createServer(async (req: IncomingMessage, res: ServerResponse) => { + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + + const eventbusServer = http.createServer((req: IncomingMessage, res: ServerResponse) => { + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + + if (req.method === "GET") { + pollCount += 1; + sendJson(res, 200, { + events: + pollCount === 1 + ? [ + { id: "stream-1", eventId: "order.paid:order-duplicate", hook: hookPayload }, + { id: "stream-2", eventId: "order.paid:order-duplicate", hook: hookPayload }, + ] + : [], + }); + return; + } + + assert.equal(req.method, "POST"); + acks.push(url.pathname); + sendJson(res, 200, { ok: true }); + }); + + await listen(hookServer); + await listen(eventbusServer); + + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: serverUrl(eventbusServer), + agentId: "agent-1", + agentToken: "agent-token", + credentialProfile: "provider", + consumerId: "test-consumer", + hookUrl: `${serverUrl(hookServer)}/hooks/agent`, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + + try { + await service.start(); + await waitFor(() => forwardedHooks.length === 1 && acks.length === 2); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, [ + "/v1/agents/agent-1/events/stream-1/ack", + "/v1/agents/agent-1/events/stream-2/ack", + ]); + } finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + } +}); + +test("polling service recovers after poll request failure", async () => { + const hookPayload: HookPayload = { + message: "You have a recovered order", + name: "Corall", + sessionKey: "hook:corall:order-recovered", + deliver: false, + }; + const forwardedHooks: unknown[] = []; + const acks: string[] = []; + let pollCount = 0; + + const hookServer = http.createServer(async (req: IncomingMessage, res: ServerResponse) => { + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + + const eventbusServer = http.createServer((req: IncomingMessage, res: ServerResponse) => { + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + + if (req.method === "GET") { + pollCount += 1; + if (pollCount === 1) { + sendJson(res, 503, { error: "temporary redis failure" }); + return; + } + sendJson(res, 200, { + events: + pollCount === 2 + ? [{ id: "stream-recovered", eventId: "order.paid:order-recovered", hook: hookPayload }] + : [], + }); + return; + } + + assert.equal(req.method, "POST"); + acks.push(url.pathname); + sendJson(res, 200, { ok: true }); + }); + + await listen(hookServer); + await listen(eventbusServer); + + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: serverUrl(eventbusServer), + agentId: "agent-1", + agentToken: "agent-token", + credentialProfile: "provider", + consumerId: "test-consumer", + hookUrl: `${serverUrl(hookServer)}/hooks/agent`, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + + try { + await service.start(); + await waitFor(() => forwardedHooks.length === 1 && acks.length === 1); + assert.equal(pollCount >= 2, true); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, ["/v1/agents/agent-1/events/stream-recovered/ack"]); + } finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + } +}); + +test("polling service does not forward again when ack fails after hook delivery", async () => { + const hookPayload: HookPayload = { + message: "You have an ack retry order", + name: "Corall", + sessionKey: "hook:corall:order-ack-retry", + deliver: false, + }; + const forwardedHooks: unknown[] = []; + let ackAttempts = 0; + + const hookServer = http.createServer(async (req: IncomingMessage, res: ServerResponse) => { + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + + const eventbusServer = http.createServer((req: IncomingMessage, res: ServerResponse) => { + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + + if (req.method === "GET") { + sendJson(res, 200, { + events: + ackAttempts < 2 + ? [{ id: "stream-ack-retry", eventId: "order.paid:order-ack-retry", hook: hookPayload }] + : [], + }); + return; + } + + assert.equal(req.method, "POST"); + ackAttempts += 1; + if (ackAttempts === 1) { + sendJson(res, 503, { error: "temporary ack failure" }); + return; + } + sendJson(res, 200, { ok: true }); + }); + + await listen(hookServer); + await listen(eventbusServer); + + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: serverUrl(eventbusServer), + agentId: "agent-1", + agentToken: "agent-token", + credentialProfile: "provider", + consumerId: "test-consumer", + hookUrl: `${serverUrl(hookServer)}/hooks/agent`, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + + try { + await service.start(); + await waitFor(() => forwardedHooks.length === 1 && ackAttempts === 2); + assert.deepEqual(forwardedHooks[0], hookPayload); + } finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + } +}); + +test("polling service starts after credentials add agent id", async () => { + const homeDir = await fs.mkdtemp(path.join(os.tmpdir(), "corall-polling-home-")); + const previousHome = process.env.HOME; + const profile = "late-provider"; + const hookPayload: HookPayload = { + message: "You have a new order", + name: "Corall", + sessionKey: "hook:corall:order-late", + deliver: false, + }; + const forwardedHooks: unknown[] = []; + const acks: string[] = []; + let pollCount = 0; + + process.env.HOME = homeDir; + + const hookServer = http.createServer(async (req: IncomingMessage, res: ServerResponse) => { + assert.equal(req.method, "POST"); + assert.equal(req.url, "/hooks/agent"); + assert.equal(req.headers.authorization, "Bearer hook-token"); + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + + const eventbusServer = http.createServer(async (req: IncomingMessage, res: ServerResponse) => { + assert.equal(req.headers.authorization, "Bearer agent-token"); + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + + if (req.method === "GET") { + assert.equal(url.pathname, "/v1/agents/agent-late/events"); + assert.match(url.searchParams.get("consumerId") ?? "", /^corall-polling:agent-late:/); + pollCount += 1; + sendJson(res, 200, { + events: pollCount === 1 ? [{ id: "stream-late", hook: hookPayload }] : [], + }); + return; + } + + assert.equal(req.method, "POST"); + assert.equal(url.pathname, "/v1/agents/agent-late/events/stream-late/ack"); + acks.push("stream-late"); + sendJson(res, 200, { ok: true }); + }); + + await listen(hookServer); + await listen(eventbusServer); + + const eventbusUrl = serverUrl(eventbusServer); + const hookUrl = `${serverUrl(hookServer)}/hooks/agent`; + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: eventbusUrl, + agentId: undefined, + agentToken: "agent-token", + credentialProfile: profile, + consumerId: undefined, + hookUrl, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + + try { + await service.start(); + await delayMs(30); + assert.equal(pollCount, 0); + + const credentialsDir = path.join(homeDir, ".corall", "credentials"); + await fs.mkdir(credentialsDir, { recursive: true }); + await fs.writeFile( + path.join(credentialsDir, `${profile}.json`), + JSON.stringify({ agentId: "agent-late" }), + ); + + await waitFor(() => forwardedHooks.length === 1 && acks.length === 1, 2_000); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, ["stream-late"]); + } finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + if (previousHome === undefined) { + delete process.env.HOME; + } else { + process.env.HOME = previousHome; + } + await fs.rm(homeDir, { recursive: true, force: true }); + } +}); + +test("polling service restarts when credential agent id changes", async () => { + const homeDir = await fs.mkdtemp(path.join(os.tmpdir(), "corall-polling-home-")); + const previousHome = process.env.HOME; + const profile = "switch-provider"; + const hookPayload: HookPayload = { + message: "You have a switched order", + name: "Corall", + sessionKey: "hook:corall:order-switch", + deliver: false, + }; + const forwardedHooks: unknown[] = []; + const acks: string[] = []; + const polledPaths: string[] = []; + let sentSwitchedEvent = false; + + process.env.HOME = homeDir; + await writeCredential(homeDir, profile, "agent-one"); + + const hookServer = http.createServer(async (req: IncomingMessage, res: ServerResponse) => { + assert.equal(req.method, "POST"); + forwardedHooks.push(await readJson(req)); + sendJson(res, 200, { ok: true }); + }); + + const eventbusServer = http.createServer((req: IncomingMessage, res: ServerResponse) => { + const requestUrl = req.url; + assert.equal(typeof requestUrl, "string"); + if (typeof requestUrl !== "string") { + throw new Error("request URL is missing"); + } + const url = new URL(requestUrl, "http://127.0.0.1"); + + if (req.method === "GET") { + polledPaths.push(url.pathname); + if (url.pathname === "/v1/agents/agent-two/events" && !sentSwitchedEvent) { + sentSwitchedEvent = true; + sendJson(res, 200, { events: [{ id: "stream-two", hook: hookPayload }] }); + return; + } + sendJson(res, 200, { events: [] }); + return; + } + + assert.equal(req.method, "POST"); + assert.equal(url.pathname, "/v1/agents/agent-two/events/stream-two/ack"); + acks.push("stream-two"); + sendJson(res, 200, { ok: true }); + }); + + await listen(hookServer); + await listen(eventbusServer); + + const service = createPollingService({ + api: testApi(), + config: { + baseUrl: serverUrl(eventbusServer), + agentId: undefined, + agentToken: "agent-token", + credentialProfile: profile, + consumerId: undefined, + hookUrl: `${serverUrl(hookServer)}/hooks/agent`, + waitSeconds: 0, + requestTimeoutMs: 500, + ackTimeoutMs: 500, + idleDelayMs: 5, + errorBackoffMs: 5, + maxErrorBackoffMs: 10, + recentEventTtlMs: 1_000, + }, + }); + + try { + await service.start(); + await waitFor(() => polledPaths.includes("/v1/agents/agent-one/events"), 1_000); + await writeCredential(homeDir, profile, "agent-two"); + await waitFor(() => forwardedHooks.length === 1 && acks.length === 1, 2_000); + assert.deepEqual(forwardedHooks[0], hookPayload); + assert.deepEqual(acks, ["stream-two"]); + assert.ok(polledPaths.includes("/v1/agents/agent-two/events")); + } finally { + await service.stop(); + await close(eventbusServer); + await close(hookServer); + if (previousHome === undefined) { + delete process.env.HOME; + } else { + process.env.HOME = previousHome; + } + await fs.rm(homeDir, { recursive: true, force: true }); + } +}); + +function testApi(): { config: OpenClawConfig; logger: Logger } { + return { + config: { + hooks: { token: "hook-token" }, + gateway: { port: 18789 }, + }, + logger: { + debug(_message: string): void {}, + info(_message: string): void {}, + warn(_message: string): void {}, + error(_message: string): void {}, + }, + }; +} + +function listen(server: Server): Promise { + return new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", () => { + server.off("error", reject); + resolve(); + }); + }); +} + +function close(server: Server): Promise { + return new Promise((resolve, reject) => { + server.close((error) => (error ? reject(error) : resolve())); + }); +} + +function serverUrl(server: Server): string { + const address = server.address(); + assert.equal(typeof address, "object"); + assert.notEqual(address, null); + const info = address as AddressInfo; + return `http://${info.address}:${info.port}`; +} + +async function readJson(req: IncomingMessage): Promise { + const chunks: Buffer[] = []; + for await (const chunk of req) { + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))); + } + return JSON.parse(Buffer.concat(chunks).toString("utf8")) as unknown; +} + +function sendJson(res: ServerResponse, status: number, body: unknown): void { + res.writeHead(status, { "content-type": "application/json" }); + res.end(JSON.stringify(body)); +} + +async function delayMs(ms: number): Promise { + await new Promise((resolve) => { + setTimeout(resolve, ms); + }); +} + +async function writeCredential(homeDir: string, profile: string, agentId: string): Promise { + const credentialsDir = path.join(homeDir, ".corall", "credentials"); + await fs.mkdir(credentialsDir, { recursive: true }); + await fs.writeFile(path.join(credentialsDir, `${profile}.json`), JSON.stringify({ agentId })); +} + +async function waitFor(predicate: () => boolean, timeoutMs = 1_000): Promise { + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + if (predicate()) { + return; + } + await new Promise((resolve) => { + setTimeout(resolve, 10); + }); + } + throw new Error("timed out waiting for predicate"); +} diff --git a/plugins/corall-polling/tsconfig.json b/plugins/corall-polling/tsconfig.json new file mode 100644 index 0000000..6db2c31 --- /dev/null +++ b/plugins/corall-polling/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "lib": ["ES2022", "DOM"], + "types": ["node"], + "rootDir": ".", + "outDir": "dist", + "strict": true, + "noImplicitAny": true, + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + "noImplicitOverride": true, + "noFallthroughCasesInSwitch": true, + "useUnknownInCatchVariables": true, + "verbatimModuleSyntax": true, + "declaration": true, + "sourceMap": true, + "skipLibCheck": true + }, + "include": ["index.ts", "src/**/*.ts", "src/**/*.d.ts", "test/**/*.ts"] +} diff --git a/skills/corall/.claude-plugin/plugin.json b/skills/corall/.claude-plugin/plugin.json index ffff153..e658f3c 100644 --- a/skills/corall/.claude-plugin/plugin.json +++ b/skills/corall/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "corall", "version": "0.6.0", - "description": "Corall marketplace integration — set up as provider (OpenClaw webhook) or employer, handle incoming orders, and place orders on the marketplace.", + "description": "Corall marketplace integration — set up as provider with the OpenClaw polling plugin or as an employer, handle incoming orders, and place orders on the marketplace.", "author": { "name": "magine" }, "skills": ["./"] } diff --git a/skills/corall/SKILL.md b/skills/corall/SKILL.md index 10af6a0..8d6196d 100644 --- a/skills/corall/SKILL.md +++ b/skills/corall/SKILL.md @@ -31,7 +31,7 @@ corall --version | Platform | Signal | | --- | --- | -| **OpenClaw** | Running on an OpenClaw host; or user mentions OpenClaw, webhook, hook | +| **OpenClaw** | Running on an OpenClaw host; or user mentions OpenClaw, polling, eventbus, webhook, hook | | **Claude Code** | Running in Claude Code directly; no OpenClaw present | **Step 3 — load the reference:** @@ -41,14 +41,17 @@ corall --version | Provider | OpenClaw | `provider` | `references/setup-provider-openclaw.md` | | Employer | OpenClaw | `employer` | `references/setup-employer.md` | | Employer | Claude Code | `employer` | `references/setup-employer.md` | -| Handle order (webhook) | — | `provider` | `references/order-handle.md` | +| Handle order (hook/polling) | — | `provider` | `references/order-handle.md` | | Create order | — | `employer` | `references/order-create.md` | +| Browser login | — | active role profile | `references/browser-login.md` | +| Publish skill package | — | `provider` | `references/skill-package-submit.md` | | Payout | — | `provider` | `references/payout.md` | The **Profile** column is the `--profile` value to use for all `corall` commands in that mode. Pass it explicitly on every command — do not rely on the default. > Hook message with Task `Corall` or session key `hook:corall:*` → always **Handle order** with `--profile provider`. > User asks to place, create, or buy an order → always **Create order** with `--profile employer`. +> User asks to sign in to the web dashboard/browser → use **Browser login** with the role profile the browser should access. > Setup intent without clear role/platform → ask before proceeding. ## Additional References @@ -56,12 +59,15 @@ The **Profile** column is the `--profile` value to use for all `corall` commands Load these only when the active workflow calls for them: - `references/cli-reference.md` — Full CLI command listing with all flags +- `references/browser-login.md` — Browser dashboard login with Agent-approved Ed25519 challenge - `references/file-upload.md` — Presigned URL upload workflow (needed when submitting an artifact) +- `references/skill-package-submit.md` — Agent-generated form required for paid skill package submission - `references/payout.md` — Provider payout guide (Stripe Connect onboarding and transferring earnings) ## Security Notice > 1. **Dedicated accounts** — Use separate Corall accounts for provider and employer roles. Log in with `--profile provider` for agent operations and `--profile employer` for placing orders. Never mix credentials between profiles. -> 2. **Webhook verification** — OpenClaw verifies the `webhookToken` before delivering messages. Messages that reach this skill have already passed that check. -> 3. **Bounded scope** — In order-handle webhook mode, only perform the task in `inputPayload`. No pre-existing file access, no unrelated commands, no software installs. +> 2. **Hook verification** — The Corall eventbus verifies the agent token before polling delivery, and OpenClaw verifies `hooks.token` before invoking the local hook. Messages that reach this skill have already passed those checks. +> 3. **Bounded scope** — In hook-triggered order mode, only perform the task in `inputPayload`. No pre-existing file access, no unrelated commands, no software installs. > 4. **Data egress** — Artifact URLs and presigned uploads send data to external servers. In interactive sessions, confirm with the user before submitting. +> 5. **Browser login** — Approve browser login codes only in interactive user sessions. Never expose a private key, raw signature, or JWT; let the backend set the browser's HttpOnly cookie after challenge approval. diff --git a/skills/corall/evals/cases.md b/skills/corall/evals/cases.md index c6c7f6f..87279eb 100644 --- a/skills/corall/evals/cases.md +++ b/skills/corall/evals/cases.md @@ -24,13 +24,13 @@ --- -## Case 3: Incoming webhook order (hook trigger) +## Case 3: Incoming polling hook order -**Prompt (hook message):** Task: Corall — New order received. Order ID: abc123. Input: {"task": "Summarize this text", "text": "..."} +**Prompt (hook message):** name=Corall, sessionKey=hook:corall:abc123. New order received. Order ID: abc123. Input: {"task": "Summarize this text", "text": "..."} **Expected behavior:** -- Detects mode=Handle order (hook message with Task "Corall") +- Detects mode=Handle order (hook message with name "Corall" or sessionKey `hook:corall:*`) - Reads `references/order-handle.md` - Accepts the order immediately with `corall agent accept abc123` - Performs the task @@ -47,8 +47,9 @@ - Detects mode=Create order - Reads `references/order-create.md` - Runs `corall orders create agent_xyz --input '{"task": "analyze my logs"}'` -- Monitors order status until SUBMITTED -- Offers to approve or dispute +- Monitors order status until `delivered` +- Reviews the delivered result, then approves or disputes +- Leaves a factual review after approval --- @@ -60,3 +61,17 @@ - Asks the user: are you a Provider (receive orders) or Employer (place orders)? - Does not proceed until role is confirmed + +--- + +## Case 6: Publish a skill package + +**Prompt:** Publish this Skill as a paid package for my Corall agent. + +**Expected behavior:** + +- Detects mode=Publish skill package +- Reads `references/skill-package-submit.md` +- Inspects the Skill source before generating the form +- Produces a `generatedBy: "agent"` JSON form with category, description, functions, and permissions +- Asks the provider to review the form before running `corall skill-packages create` diff --git a/skills/corall/references/browser-login.md b/skills/corall/references/browser-login.md new file mode 100644 index 0000000..d337643 --- /dev/null +++ b/skills/corall/references/browser-login.md @@ -0,0 +1,26 @@ +# Browser Login + +Use this workflow when the user wants to sign in to the Corall web UI or dashboard from a browser. + +The browser starts the login and shows a short code. The Agent approves that code with the local Ed25519 key; the backend then sets the browser's HttpOnly session cookie. The Agent must never expose the private key, raw signature, or JWT to the user or to the browser. + +## Approve a Browser Code + +Use the profile that matches the account the browser should log in as: + +```bash +corall auth browser approve https://yourdomain.com \ + --code \ + --profile employer +``` + +For provider dashboard access, use `--profile provider` instead. + +The command fetches the browser challenge, signs it locally, and sends only the public key plus signature to Corall. If the command succeeds, tell the user to return to the browser tab; the page should finish the login automatically. + +## Guardrails + +- Do not approve browser login codes from hook-triggered order sessions. +- Confirm the target site before approving a code. +- If the user has not registered or logged in locally, run the relevant setup workflow first. +- If the code expired, ask the user to generate a new browser login code. diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index a2cd8e7..169f5a8 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -5,12 +5,22 @@ All commands output JSON to stdout. Errors print as `{"error": "..."}` to stderr ## Auth ```text -corall auth register --email --password --name -corall auth login --email --password +corall auth register --name +corall auth login +corall auth browser approve --code corall auth me corall auth remove ``` +Auth uses a local Ed25519 keypair saved in `~/.corall/credentials/.json`. +The optional legacy `--email` and `--password` flags are accepted for older +automation but are ignored by current public-key authentication. + +`corall auth browser approve` approves a short browser login code by fetching +the browser challenge, signing it with the local Ed25519 key, and sending the +public key plus signature to Corall. The backend sets the browser's HttpOnly +session cookie after the browser consumes the approved request. + ## Agents ```text @@ -22,7 +32,8 @@ corall agents activate corall agents delete ``` -`corall agents create` automatically saves the returned `agentId` to `~/.corall/credentials.json`. +`corall agents create` automatically saves the returned `agentId` to +`~/.corall/credentials/.json`. All `--price`, `--min-price`, `--max-price` values are in **cents** (USD). For example, `--price 500` means $5.00. @@ -63,6 +74,27 @@ Plans: `quarterly` ($29/3 months) · `yearly` ($99/year). > > Employers do not need a membership — orders can be placed on any `ACTIVE` agent without a subscription. +## Skill Packages + +```text +corall skill-packages form-template +corall skill-packages create --agent-id --skills --price +corall skill-packages mine +corall skill-packages get +corall skill-packages purchase +corall skill-packages purchased +corall skill-packages delete +``` + +Providers use `create` to publish a paid skill package for one of their agents. +The `--skills` value must be an Agent-generated form, not a loose skill list. +Use `form-template` or `references/skill-package-submit.md` for the required +shape. The form records SkillHub-style category, activation description, +functions, and permissions. +Employers use `purchase` to create a one-time Stripe Checkout session, then +`purchased` to list completed purchases after the webhook confirms payment. +All prices are in cents. + ## Connect (Stripe Connect) ```text @@ -93,22 +125,36 @@ corall reviews create --rating <1-5> [--comment ] ## OpenClaw ```text -corall openclaw setup [--webhook-token ] [--config ] +corall openclaw setup [--webhook-token ] [--eventbus-url ] [--config ] [--skip-plugin-install] +corall eventbus serve [--listen ] [--redis-url ] [--consumer-group ] [--default-wait-ms ] [--max-wait-ms ] [--default-count ] [--max-count ] [--claim-idle-ms ] ``` Merges Corall integration settings into the OpenClaw config file. Sets `hooks.enabled`, `hooks.token`, `hooks.allowRequestSessionKey`, and adds `"hook:"` to `allowedSessionKeyPrefixes` (existing prefixes are preserved). Also sets `gateway.mode="local"` and `gateway.bind="lan"` if not already set. +By default it also installs the CLI-bundled `corall-polling` OpenClaw plugin, +enables `plugins.entries.corall-polling`, sets `credentialProfile="provider"`, +and uses `--eventbus-url` or `CORALL_EVENTBUS_URL` as the plugin `baseUrl`. `--webhook-token` is optional. When omitted, a secure random token is generated. Output fields: -- `webhookToken` (string) — present only when the token was auto-generated; - pass this to `corall agents create --webhook-token` +- `webhookToken` (string) — present when the token was auto-generated or kept + from the existing OpenClaw config; pass this to + `corall agents create --webhook-token` - `tokenGenerated` (bool) — true when the token was auto-generated - `configPath` (string) — absolute path of the config file that was written - `applied` (object) — the hooks and gateway fields that were set +- `plugin` (object) — whether `corall-polling` was installed and which + eventbus URL was written + +`corall eventbus serve` starts the Redis-backed HTTP polling layer used by the +resident `corall-polling` OpenClaw plugin. The eventbus reads agent +registrations from `corall:eventbus:agent::registration`, serves +`GET /health`, `GET /v1/agents/:agent_id/events`, and +`POST /v1/agents/:agent_id/events/:event_id/ack`, and consumes agent streams +from `corall:eventbus:agent::stream`. ## Upgrade diff --git a/skills/corall/references/file-upload.md b/skills/corall/references/file-upload.md index 569c648..de974aa 100644 --- a/skills/corall/references/file-upload.md +++ b/skills/corall/references/file-upload.md @@ -1,6 +1,6 @@ # File Upload via Presigned URLs -> **Data egress warning:** `corall upload presign` returns a URL that uploads data directly to external R2 storage. In interactive sessions, confirm content with the user first. In webhook mode, only upload content produced by this task — never upload pre-existing host files. +> **Data egress warning:** `corall upload presign` returns a URL that uploads data directly to external R2 storage. In interactive sessions, confirm content with the user first. In hook-triggered mode, only upload content produced by this task — never upload pre-existing host files. ```bash # Step 1: Get a presigned URL diff --git a/skills/corall/references/order-create.md b/skills/corall/references/order-create.md index 85cea47..43ee177 100644 --- a/skills/corall/references/order-create.md +++ b/skills/corall/references/order-create.md @@ -37,10 +37,10 @@ After successful payment, the Stripe webhook will update the order status to `pa ```bash corall orders payment-status --profile employer -# { "paymentStatus": "succeeded", "orderStatus": "paid" } +# { "status": "succeeded" } ``` -> **After placing an order, you MUST actively monitor its status.** Do not stop after payment. Poll the order until it reaches a terminal state (`SUBMITTED`, `COMPLETED`, or `DISPUTED`), then take the appropriate action (approve or dispute). Leaving an order unmonitored means the task result may never be reviewed and the order will stall. +> **After placing an order, you MUST actively monitor its status.** Do not stop after payment. Poll the order until it reaches `delivered`, then approve or dispute it. `completed` and `dispute` are terminal states. Leaving an order unmonitored means the task result may never be reviewed and the order will stall. ## 4. Monitor Progress diff --git a/skills/corall/references/order-handle.md b/skills/corall/references/order-handle.md index ceea279..d979539 100644 --- a/skills/corall/references/order-handle.md +++ b/skills/corall/references/order-handle.md @@ -1,19 +1,19 @@ # Order Handling Mode (Agent Side) -This mode covers accepting an incoming order, completing the task, and submitting the result — whether triggered by webhook or interactively. +This mode covers accepting an incoming order, completing the task, and submitting the result — whether triggered by the Corall polling hook or interactively. All `corall` commands in this mode use `--profile provider`. ## Scope -In webhook mode, this skill may autonomously: +In hook-triggered mode, this skill may autonomously: - Verify credentials (`corall auth me --profile provider`) — if this fails, stop immediately; submission also requires auth, so there is nothing further to do - Accept the order - Perform the task in `inputPayload` - Submit the result -Webhook mode does **not** authorize reading or uploading pre-existing host files, running unrelated system commands, or installing software. Steps marked "interactive only" are skipped in webhook mode. +Hook-triggered mode does **not** authorize reading or uploading pre-existing host files, running unrelated system commands, or installing software. Steps marked "interactive only" are skipped in hook-triggered mode. ## 1. Parse the Notification diff --git a/skills/corall/references/setup-employer.md b/skills/corall/references/setup-employer.md index ae37850..e90e79e 100644 --- a/skills/corall/references/setup-employer.md +++ b/skills/corall/references/setup-employer.md @@ -31,21 +31,17 @@ If credentials exist for the target site, skip to **2b**. ```bash corall auth register https://yourdomain.com \ - --email your-account@example.com \ - --password \ --name "My Name" \ --profile employer ``` -Password must be at least 6 characters. On failure with "Email already registered", use login instead. +The CLI generates a local Ed25519 keypair and stores it in +`~/.corall/credentials/employer.json`. **2b. Login (existing account):** ```bash -corall auth login https://yourdomain.com \ - --email your-account@example.com \ - --password \ - --profile employer +corall auth login https://yourdomain.com --profile employer ``` Verify auth is working: @@ -56,6 +52,8 @@ corall auth me --profile employer > Before running any command that authenticates, tell the user which site you are authenticating with. Never display or log credential values. +If the user also wants browser dashboard access, use `references/browser-login.md` after local credentials are verified. + ## 3. Confirm ```bash diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index c439baa..0cea1db 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -1,6 +1,6 @@ # Setup: OpenClaw as Provider -This guide registers an OpenClaw instance as an agent on the Corall marketplace so it can receive and fulfill orders via webhook. +This guide registers an OpenClaw instance as an agent on the Corall marketplace so it can receive and fulfill orders through the resident Corall polling plugin. Walk through these steps in order. Stop and ask the user if anything looks wrong or unexpected — do not make changes to config files without confirming the current state is healthy first. @@ -14,24 +14,21 @@ openclaw status If this reports errors, stop here and ask the user to resolve them before continuing. -**Verify the machine is reachable from the internet:** +**Verify the local hook config can be used safely:** ```bash -EXTERNAL_IP=$(curl -fsSL https://api.ipify.org) -echo "External IP: $EXTERNAL_IP" -hostname -I +openclaw status +cat ~/.openclaw/openclaw.json | jq '.hooks' ``` -Show the user the output and ask: "Is this machine a cloud VM (AWS/GCP/Azure/VPS) with the webhook port open to the internet, or is it behind a home/office router?" Do not proceed until the user confirms. Home/office NAT is not supported. - -Cloud VMs typically have a private IP (e.g. `10.x.x.x`) with the public IP routed at the network level — `bind: "lan"` works, but you may need to open the webhook port in the provider's firewall or security group. +Corall no longer requires a public inbound webhook port on the OpenClaw host. The only local requirement is that the OpenClaw Gateway can accept authenticated requests on `/hooks/agent`, which `corall openclaw setup` configures in the next step. ## 2. Configure the OpenClaw Config File Run this command to merge the required hooks and gateway settings into `~/.openclaw/openclaw.json`: ```bash -corall openclaw setup +corall openclaw setup --eventbus-url http://:8787 ``` `--webhook-token` is optional. The output is JSON with one of three shapes depending on the token source: @@ -39,13 +36,13 @@ corall openclaw setup | `tokenGenerated` | `tokenKept` | `webhookToken` in output | Meaning | | --- | --- | --- | --- | | `true` | `false` | yes | New token generated — copy it now | -| `false` | `true` | no | Existing token preserved — already registered | +| `false` | `true` | yes | Existing token preserved — already registered | | `false` | `false` | no | Token was passed via `--webhook-token` — already known | **Extract the token for later use:** ```bash -WEBHOOK_TOKEN=$(corall openclaw setup | jq -r '.webhookToken') +WEBHOOK_TOKEN=$(corall openclaw setup --eventbus-url http://:8787 | jq -r '.webhookToken') ``` `webhookToken` is present whenever the token was generated or kept from the existing config. If you supplied `--webhook-token` yourself, the field is omitted (you already know it). @@ -53,11 +50,40 @@ WEBHOOK_TOKEN=$(corall openclaw setup | jq -r '.webhookToken') To force a specific token (e.g. rotating or re-registering an existing agent): ```bash -corall openclaw setup --webhook-token +corall openclaw setup \ + --webhook-token \ + --eventbus-url http://:8787 ``` If the OpenClaw config file lives elsewhere, pass `--config ` explicitly. +## 2b. Install the Resident Corall Polling Plugin + +`corall openclaw setup` installs the bundled `corall-polling` plugin from the +CLI itself and writes the matching `plugins.entries.corall-polling` config. The +plugin polls the eventbus, then forwards each order event into the local +`/hooks/agent` endpoint using the `hooks.token` from Step 2. + +Expected plugin config after setup: + +```json +{ + "plugins": { + "entries": { + "corall-polling": { + "enabled": true, + "config": { + "baseUrl": "http://:8787", + "credentialProfile": "provider" + } + } + } + } +} +``` + +The plugin can read `agentId` from `~/.corall/credentials/provider.json` after the agent is created, and it reuses `hooks.token` as the polling bearer token by default. + ## 3. Register or Login Check for existing credentials: @@ -72,21 +98,18 @@ If credentials exist for the target site, skip to **3b**. ```bash corall auth register https://yourdomain.com \ - --email your-agent@example.com \ - --password \ --name "My OpenClaw Agent" \ --profile provider ``` -Use a dedicated account for agent operations — never the employer account. Password must be at least 6 characters. On failure with "Email already registered", use login instead. +Use a dedicated account for agent operations — never the employer account. The +CLI generates a local Ed25519 keypair and stores it in +`~/.corall/credentials/provider.json`. **3b. Login (existing account):** ```bash -corall auth login https://yourdomain.com \ - --email your-agent@example.com \ - --password \ - --profile provider +corall auth login https://yourdomain.com --profile provider ``` Verify auth is working: @@ -97,6 +120,8 @@ corall auth me --profile provider > Before running any command that authenticates, tell the user which site you are authenticating with. Never display or log credential values. +If the user also wants browser dashboard access as this provider account, use `references/browser-login.md` with `--profile provider` after local credentials are verified. + ## 4. Join Developer Club (required before activating agents) Agents cannot be activated without an active Developer Club membership. Subscribe first: @@ -120,16 +145,15 @@ The response should show `"hasActiveSubscription": true`. If not, wait a few sec Check if an agent already exists: ```bash -corall agents list --mine +corall agents list --mine --profile provider ``` Look for an agent with status `ACTIVE` or `DRAFT` (skip `SUSPENDED` — they are archived). -**If an agent exists**, update its webhook config: +**If an agent exists**, update its Corall event token: ```bash corall agents update \ - --webhook-url "http://:18789/hooks/agent" \ --webhook-token "" \ --profile provider ``` @@ -141,17 +165,17 @@ corall agents create \ --name "My OpenClaw Agent" \ --description "An autonomous AI agent powered by OpenClaw" \ --tags "openclaw,automation" \ - --price 100 \ # price in cents (100 = $1.00), minimum is 50 ($0.50) + --price 100 \ --delivery-time 1 \ - --webhook-url "http://:18789/hooks/agent" \ --webhook-token "" \ --profile provider ``` -- `--webhook-url`: Your OpenClaw endpoint. Use HTTPS if you have a reverse proxy — plain HTTP sends the token unencrypted. -- `--webhook-token`: The `webhookToken` value from Step 2's JSON output. If you passed `--webhook-token` to `corall openclaw setup`, use that same value. +- `--price`: price in cents. `100` means $1.00, and the minimum is 50 ($0.50). +- `--webhook-token`: The polling bearer token Corall stores for your agent. In the current implementation this should match the `hooks.token` value from Step 2. +- `--webhook-url`: No longer required for OpenClaw polling mode. -The `agentId` is automatically saved to `~/.corall/credentials.json`. +The `agentId` is automatically saved to `~/.corall/credentials/provider.json`. ## 6. Activate @@ -170,4 +194,4 @@ corall auth me --profile provider corall agents get --profile provider ``` -Confirm with the user that the webhook URL is reachable and the firewall or security group allows inbound traffic on the webhook port. +Confirm with the user that the `corall-polling` plugin is enabled, its `baseUrl` points at the correct Corall eventbus service, and `hooks.token` still matches the agent's `--webhook-token`. diff --git a/skills/corall/references/skill-package-submit.md b/skills/corall/references/skill-package-submit.md new file mode 100644 index 0000000..7430241 --- /dev/null +++ b/skills/corall/references/skill-package-submit.md @@ -0,0 +1,125 @@ +# Skill Package Submission + +Use this guide when a provider asks to publish, submit, sell, or package a Skill on Corall. + +Skill package submission requires an Agent-generated form in the `--skills` JSON payload. Do not pass a loose list of skills. Inspect the Skill materials first, then generate the form and ask the provider to review it before publishing. + +## 1. Preconditions + +Verify provider auth and agent ownership: + +```bash +corall auth me --profile provider +corall agents list --mine --profile provider +``` + +Use an existing provider-owned agent ID. If no agent exists, complete `references/setup-provider-openclaw.md` first. + +## 2. Generate The Form + +Inspect the Skill source the provider wants to publish, including `SKILL.md`, any `references/`, `scripts/`, `assets/`, config templates, dependency notes, and examples. Then generate one JSON object with this contract: + +```json +{ + "version": 1, + "generatedBy": "agent", + "category": { + "primary": "Development", + "secondary": "CLI & Terminal" + }, + "description": { + "summary": "Generate Python hello-world scripts for test workflows.", + "activationTriggers": [ + "Use when the user asks for a small Python hello-world script." + ], + "keywords": ["python", "script", "hello-world"] + }, + "functions": [ + { + "name": "Generate script", + "description": "Produces a Python script artifact from a natural-language request." + } + ], + "permissions": { + "env": [], + "network": [], + "filesystem": [ + { + "access": "write", + "scope": "workspace", + "purpose": "Create the requested script artifact." + } + ], + "tools": [], + "install": { + "hasInstallSteps": false, + "manualReviewRequired": false + }, + "persistence": { + "requiresBackgroundService": false, + "requiresElevatedPrivileges": false + } + } +} +``` + +You can print the template with: + +```bash +corall skill-packages form-template --profile provider +``` + +## 3. Category Rules + +Use SkillHub/ClawHub-style primary categories: + +- `Development` +- `AI & Agents` +- `Productivity` +- `Communication` +- `Data & Research` +- `Business` +- `Platforms` +- `Lifestyle` +- `Education` +- `Design` +- `Other` + +Use `secondary` for the closer marketplace bucket, for example `CLI & Terminal`, `Security & Audit`, `Web Search`, `Workflow Automation`, `Email`, `CRM & Sales`, `Legal & Compliance`, `Design Tools`, or `Education & Learning`. + +## 4. Description Rules + +The description must be useful to both marketplace search and activation: + +- `summary`: concrete capability and problem solved. +- `activationTriggers`: user requests that should trigger the Skill. +- `keywords`: searchable domain, platform, and workflow terms. + +Avoid vague summaries such as "helps with APIs". Mention the actual systems, artifacts, and outputs. + +## 5. Permission Rules + +Declare the footprint the Skill actually needs: + +- `env`: environment variables and secrets, including `required`, `sensitive`, and `purpose`. +- `network`: external domains or APIs contacted by scripts or instructions. +- `filesystem`: read/write scope. Prefer `workspace`. +- `tools`: required binaries, CLIs, MCP servers, or host tools. +- `install`: whether install steps exist and whether the provider must review them manually. +- `persistence`: whether background services or elevated privileges are required. + +If nothing is needed, use an empty array or `false`. Never hide credentials, external calls, install steps, privileged operations, or background behavior. + +## 6. Publish + +After provider review, submit the package: + +```bash +corall skill-packages create \ + --agent-id \ + --skills '' \ + --price \ + --profile provider +``` + +All prices are in cents, and the minimum is 50. diff --git a/src/client.rs b/src/client.rs index e5cf03e..a4b3d87 100644 --- a/src/client.rs +++ b/src/client.rs @@ -9,6 +9,7 @@ use reqwest::StatusCode; use serde::Serialize; use serde_json::Value; +use crate::credentials; use crate::credentials::Credential; pub struct ApiClient { @@ -48,7 +49,7 @@ impl ApiClient { /// Performs a fresh login, caches the token in memory and on disk. async fn do_login(&mut self, cred: &Credential) -> Result<()> { - let token = self.login(&cred.email, &cred.password).await?; + let token = self.login_with_key(cred).await?; let expires_at = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_secs() as i64) @@ -127,18 +128,64 @@ impl ApiClient { Ok(resp) } - pub async fn login(&self, email: &str, password: &str) -> Result { - let resp = self - .request(Method::POST, "/api/auth/login") - .json(&serde_json::json!({ "email": email, "password": password })) + pub async fn login_with_key(&self, cred: &Credential) -> Result { + let challenge_resp = self + .request(Method::POST, "/api/auth/challenge") + .json(&serde_json::json!({ "publicKey": &cred.user.public_key })) + .send() + .await + .context("request failed")?; + let challenge_body = Self::handle(challenge_resp).await?; + let challenge = challenge_body + .get("challenge") + .and_then(|v| v.as_str()) + .context("no challenge in auth response")?; + let signature = credentials::sign_challenge(&cred.private_key_pkcs8, challenge)?; + + let verify_resp = self + .request(Method::POST, "/api/auth/verify") + .json(&serde_json::json!({ + "publicKey": &cred.user.public_key, + "signature": signature, + })) .send() .await .context("request failed")?; - let body = Self::handle(resp).await?; + let body = Self::handle(verify_resp).await?; body.get("token") .and_then(|v| v.as_str()) - .map(|s| s.to_string()) - .context("no token in login response") + .map(str::to_owned) + .context("no token in auth response") + } + + pub async fn approve_browser_login(&self, cred: &Credential, code: &str) -> Result { + let challenge_resp = self + .request(Method::POST, "/api/auth/browser/challenge") + .json(&serde_json::json!({ + "code": code, + "publicKey": &cred.user.public_key, + })) + .send() + .await + .context("request failed")?; + let challenge_body = Self::handle(challenge_resp).await?; + let challenge = challenge_body + .get("challenge") + .and_then(|v| v.as_str()) + .context("no challenge in browser auth response")?; + let signature = credentials::sign_challenge(&cred.private_key_pkcs8, challenge)?; + + let approve_resp = self + .request(Method::POST, "/api/auth/browser/approve") + .json(&serde_json::json!({ + "code": code, + "publicKey": &cred.user.public_key, + "signature": signature, + })) + .send() + .await + .context("request failed")?; + Self::handle(approve_resp).await } pub async fn get(&mut self, path: &str) -> Result { diff --git a/src/commands/auth.rs b/src/commands/auth.rs index 594ea42..81f1251 100644 --- a/src/commands/auth.rs +++ b/src/commands/auth.rs @@ -5,6 +5,7 @@ use serde_json::json; use crate::client::ApiClient; use crate::credentials; use crate::credentials::Credential; +use crate::credentials::CredentialUser; use crate::credentials::site_to_base_url; #[derive(Subcommand)] @@ -13,12 +14,12 @@ pub enum AuthCommand { Register { /// Site hostname (e.g. corall.example.com) site: String, - /// Email address + /// Legacy option accepted for compatibility; public-key auth does not use it. #[arg(long)] - email: String, - /// Password (min 6 characters) + email: Option, + /// Legacy option accepted for compatibility; public-key auth does not use it. #[arg(long)] - password: String, + password: Option, /// Display name #[arg(long)] name: String, @@ -27,12 +28,17 @@ pub enum AuthCommand { Login { /// Site hostname site: String, - /// Email address + /// Legacy option accepted for compatibility; public-key auth does not use it. #[arg(long)] - email: String, - /// Password + email: Option, + /// Legacy option accepted for compatibility; public-key auth does not use it. #[arg(long)] - password: String, + password: Option, + }, + /// Approve a browser login request with the local Ed25519 key + Browser { + #[command(subcommand)] + cmd: BrowserAuthCommand, }, /// Show current authenticated user info Me, @@ -40,16 +46,29 @@ pub enum AuthCommand { Remove, } +#[derive(Subcommand)] +pub enum BrowserAuthCommand { + /// Approve a browser login code shown by the web app + Approve { + /// Site hostname + site: String, + /// Browser login code + #[arg(long)] + code: String, + }, +} + pub async fn run(cmd: AuthCommand, profile: &str) -> Result<()> { match cmd { AuthCommand::Register { site, - email, - password, + email: _, + password: _, name, } => { + let key = credentials::generate_key()?; let mut client = ApiClient::new(site_to_base_url(&site)); - let body = json!({ "email": email, "password": password, "name": name }); + let body = json!({ "publicKey": &key.public_key, "name": name }); let resp = client.post("/api/auth/register", &body).await?; let user = resp.get("user").cloned().unwrap_or_default(); @@ -58,61 +77,80 @@ pub async fn run(cmd: AuthCommand, profile: &str) -> Result<()> { .and_then(|v| v.as_str()) .unwrap_or("") .to_string(); + let public_key = user + .get("publicKey") + .and_then(|v| v.as_str()) + .unwrap_or(&key.public_key) + .to_string(); let registered_at = user .get("createdAt") .and_then(|v| v.as_str()) .map(|s| s.to_string()); + let token = resp + .get("token") + .and_then(|v| v.as_str()) + .map(str::to_owned); + let token_expires_at = token.as_ref().map(|_| token_expiry_timestamp()); - credentials::save(profile, &Credential { - site, - email, - password, - user_id, - agent_id: None, - registered_at, - token: None, - token_expires_at: None, - })?; + credentials::save( + profile, + &Credential { + site, + user: CredentialUser { + id: user_id, + public_key, + }, + private_key_pkcs8: key.private_key_pkcs8, + agent_id: None, + registered_at, + token, + token_expires_at, + }, + )?; println!("{}", serde_json::to_string_pretty(&resp)?); } AuthCommand::Login { site, - email, - password, + email: _, + password: _, } => { - let mut client = ApiClient::new(site_to_base_url(&site)); - let body = json!({ "email": email, "password": password }); - let resp = client.post("/api/auth/login", &body).await?; - - let user = resp.get("user").cloned().unwrap_or_default(); - let user_id = user - .get("id") - .and_then(|v| v.as_str()) - .unwrap_or("") - .to_string(); - - // Preserve existing agentId if already set for this profile, site, and email. - let agent_id = credentials::load(profile) - .ok() - .filter(|c| c.site == site && c.email == email) - .and_then(|c| c.agent_id); + let mut cred = credentials::load(profile)?; + if cred.site != site { + anyhow::bail!( + "credentials for profile '{profile}' belong to '{}', not '{site}'", + cred.site + ); + } - credentials::save(profile, &Credential { - site, - email, - password, - user_id, - agent_id, - registered_at: None, - token: None, - token_expires_at: None, - })?; + let client = ApiClient::new(site_to_base_url(&site)); + let token = client.login_with_key(&cred).await?; + cred.token = Some(token); + cred.token_expires_at = Some(token_expiry_timestamp()); + credentials::save(profile, &cred)?; + let mut client = ApiClient::from_credential(&cred, profile).await?; + let resp = client.get("/api/auth/me").await?; println!("{}", serde_json::to_string_pretty(&resp)?); } + AuthCommand::Browser { cmd } => match cmd { + BrowserAuthCommand::Approve { site, code } => { + let cred = credentials::load(profile)?; + if cred.site != site { + anyhow::bail!( + "credentials for profile '{profile}' belong to '{}', not '{site}'", + cred.site + ); + } + + let client = ApiClient::new(site_to_base_url(&site)); + let resp = client.approve_browser_login(&cred, &code).await?; + println!("{}", serde_json::to_string_pretty(&resp)?); + } + }, + AuthCommand::Me => { let cred = credentials::load(profile)?; let mut client = ApiClient::from_credential(&cred, profile).await?; @@ -127,3 +165,11 @@ pub async fn run(cmd: AuthCommand, profile: &str) -> Result<()> { } Ok(()) } + +fn token_expiry_timestamp() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_secs() as i64) + .unwrap_or(0) + + 7 * 24 * 3600 +} diff --git a/src/commands/eventbus.rs b/src/commands/eventbus.rs new file mode 100644 index 0000000..28a74bd --- /dev/null +++ b/src/commands/eventbus.rs @@ -0,0 +1,72 @@ +use std::net::SocketAddr; + +use anyhow::Result; +use clap::Subcommand; + +use crate::eventbus::EventBusServeOptions; +use crate::eventbus::EventBusServer; + +#[derive(Subcommand, Debug)] +pub enum EventbusCommand { + /// Start the Redis-backed HTTP polling service for agent event delivery + Serve { + /// Address for the HTTP server to bind + #[arg(long, default_value = "127.0.0.1:8787")] + listen: SocketAddr, + + /// Redis connection URL used for registrations and agent streams + #[arg(long, default_value = "redis://127.0.0.1:6379/0")] + redis_url: String, + + /// Redis consumer group name used for per-agent streams + #[arg(long, default_value = "corall-eventbus")] + consumer_group: String, + + /// Default long-poll wait in milliseconds + #[arg(long, default_value_t = 25_000)] + default_wait_ms: u64, + + /// Maximum allowed long-poll wait in milliseconds + #[arg(long, default_value_t = 30_000)] + max_wait_ms: u64, + + /// Default number of events returned per poll + #[arg(long, default_value_t = 50)] + default_count: usize, + + /// Maximum number of events returned per poll + #[arg(long, default_value_t = 100)] + max_count: usize, + + /// Default idle threshold for reclaiming unacked messages; 0 disables reclaim + #[arg(long, default_value_t = 30_000)] + claim_idle_ms: u64, + }, +} + +pub async fn run(cmd: EventbusCommand) -> Result<()> { + match cmd { + EventbusCommand::Serve { + listen, + redis_url, + consumer_group, + default_wait_ms, + max_wait_ms, + default_count, + max_count, + claim_idle_ms, + } => { + let server = EventBusServer::new(EventBusServeOptions { + listen, + redis_url, + consumer_group, + default_wait_ms, + max_wait_ms, + default_count, + max_count, + claim_idle_ms: (claim_idle_ms > 0).then_some(claim_idle_ms), + })?; + server.serve().await + } + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 1704e64..3d2db45 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2,9 +2,11 @@ pub mod agent; pub mod agents; pub mod auth; pub mod connect; +pub mod eventbus; pub mod openclaw; pub mod orders; pub mod reviews; +pub mod skill_packages; pub mod subscriptions; pub mod upgrade; pub mod upload; diff --git a/src/commands/openclaw.rs b/src/commands/openclaw.rs index 96e50d8..c2cb948 100644 --- a/src/commands/openclaw.rs +++ b/src/commands/openclaw.rs @@ -1,6 +1,8 @@ use std::env; use std::fs; +use std::path::Path; use std::path::PathBuf; +use std::process::Command; use anyhow::Context; use anyhow::Result; @@ -10,6 +12,68 @@ use rand::Rng; use serde_json::Value; use serde_json::json; +const POLLING_PLUGIN_ID: &str = "corall-polling"; + +struct EmbeddedPluginFile { + path: &'static str, + bytes: &'static [u8], +} + +const EMBEDDED_POLLING_PLUGIN_FILES: &[EmbeddedPluginFile] = &[ + EmbeddedPluginFile { + path: "package.json", + bytes: include_bytes!("../../plugins/corall-polling/package.json"), + }, + EmbeddedPluginFile { + path: "openclaw.plugin.json", + bytes: include_bytes!("../../plugins/corall-polling/openclaw.plugin.json"), + }, + EmbeddedPluginFile { + path: "README.md", + bytes: include_bytes!("../../plugins/corall-polling/README.md"), + }, + EmbeddedPluginFile { + path: "dist/index.js", + bytes: include_bytes!("../../plugins/corall-polling/dist/index.js"), + }, + EmbeddedPluginFile { + path: "dist/index.d.ts", + bytes: include_bytes!("../../plugins/corall-polling/dist/index.d.ts"), + }, + EmbeddedPluginFile { + path: "dist/src/config.js", + bytes: include_bytes!("../../plugins/corall-polling/dist/src/config.js"), + }, + EmbeddedPluginFile { + path: "dist/src/config.d.ts", + bytes: include_bytes!("../../plugins/corall-polling/dist/src/config.d.ts"), + }, + EmbeddedPluginFile { + path: "dist/src/http.js", + bytes: include_bytes!("../../plugins/corall-polling/dist/src/http.js"), + }, + EmbeddedPluginFile { + path: "dist/src/http.d.ts", + bytes: include_bytes!("../../plugins/corall-polling/dist/src/http.d.ts"), + }, + EmbeddedPluginFile { + path: "dist/src/service.js", + bytes: include_bytes!("../../plugins/corall-polling/dist/src/service.js"), + }, + EmbeddedPluginFile { + path: "dist/src/service.d.ts", + bytes: include_bytes!("../../plugins/corall-polling/dist/src/service.d.ts"), + }, + EmbeddedPluginFile { + path: "dist/src/types.js", + bytes: include_bytes!("../../plugins/corall-polling/dist/src/types.js"), + }, + EmbeddedPluginFile { + path: "dist/src/types.d.ts", + bytes: include_bytes!("../../plugins/corall-polling/dist/src/types.d.ts"), + }, +]; + #[derive(Subcommand)] pub enum OpenclawCommand { /// Merge Corall integration settings into ~/.openclaw/openclaw.json @@ -37,6 +101,16 @@ pub enum OpenclawCommand { /// ~/.openclaw/openclaw.json (with legacy path fallback). #[arg(long)] config: Option, + + /// Corall eventbus base URL used by the resident corall-polling plugin. + /// Defaults to CORALL_EVENTBUS_URL when set. If omitted, the plugin is + /// still installed but waits until baseUrl is configured. + #[arg(long)] + eventbus_url: Option, + + /// Only update OpenClaw hooks/config; do not install the resident plugin. + #[arg(long)] + skip_plugin_install: bool, }, } @@ -45,6 +119,8 @@ pub async fn run(cmd: OpenclawCommand) -> Result<()> { OpenclawCommand::Setup { webhook_token, config, + eventbus_url, + skip_plugin_install, } => { let config_path = match config { Some(p) => p, @@ -91,12 +167,31 @@ pub async fn run(cmd: OpenclawCommand) -> Result<()> { }, }; + let original_cfg = cfg.clone(); apply_hooks(&mut cfg, &token); apply_gateway_defaults(&mut cfg)?; + let plugin_base_url = eventbus_url.or_else(|| env::var("CORALL_EVENTBUS_URL").ok()); + let staged_plugin = if skip_plugin_install { + None + } else { + let staged = stage_embedded_polling_plugin()?; + apply_polling_plugin_config(&mut cfg, plugin_base_url.as_deref()); + Some(staged) + }; + let changed = cfg != original_cfg; - let content = serde_json::to_string_pretty(&cfg)?; - fs::write(&config_path, &content) - .with_context(|| format!("failed to write {}", config_path.display()))?; + if changed { + let content = serde_json::to_string_pretty(&cfg)?; + fs::write(&config_path, &content) + .with_context(|| format!("failed to write {}", config_path.display()))?; + } + + let plugin_install = if let Some(staged) = staged_plugin { + install_openclaw_plugin(&staged)?; + PluginInstallResult::installed(staged) + } else { + PluginInstallResult::skipped() + }; // Report what was written. // @@ -108,6 +203,7 @@ pub async fn run(cmd: OpenclawCommand) -> Result<()> { let prefixes = cfg["hooks"]["allowedSessionKeyPrefixes"].clone(); let mut result = json!({ "configPath": config_path.display().to_string(), + "changed": changed, "tokenGenerated": generated, "tokenKept": kept, "applied": { @@ -121,6 +217,7 @@ pub async fn run(cmd: OpenclawCommand) -> Result<()> { "bind": cfg["gateway"]["bind"], }, }, + "plugin": plugin_install.to_json(plugin_base_url.as_deref()), }); if generated || kept { result["webhookToken"] = json!(token); @@ -131,6 +228,37 @@ pub async fn run(cmd: OpenclawCommand) -> Result<()> { Ok(()) } +struct PluginInstallResult { + installed: bool, + source_path: Option, +} + +impl PluginInstallResult { + const fn skipped() -> Self { + Self { + installed: false, + source_path: None, + } + } + + const fn installed(source_path: PathBuf) -> Self { + Self { + installed: true, + source_path: Some(source_path), + } + } + + fn to_json(&self, base_url: Option<&str>) -> Value { + json!({ + "id": POLLING_PLUGIN_ID, + "installed": self.installed, + "sourcePath": self.source_path.as_ref().map(|p| p.display().to_string()), + "baseUrl": base_url, + "credentialProfile": "provider", + }) + } +} + /// Merge the Corall-required hooks fields into the config. /// /// Always sets: `hooks.enabled`, `hooks.token`, `hooks.allowRequestSessionKey`. @@ -158,11 +286,90 @@ fn apply_hooks(cfg: &mut Value, webhook_token: &str) { } } +fn apply_polling_plugin_config(cfg: &mut Value, base_url: Option<&str>) { + let obj = cfg.as_object_mut().expect("cfg is an object"); + let plugins = obj.entry("plugins").or_insert_with(|| json!({})); + let plugins = plugins.as_object_mut().expect("plugins is an object"); + let entries = plugins.entry("entries").or_insert_with(|| json!({})); + let entries = entries + .as_object_mut() + .expect("plugins.entries is an object"); + + let entry = entries + .entry(POLLING_PLUGIN_ID) + .or_insert_with(|| json!({})); + let entry = entry + .as_object_mut() + .expect("plugins.entries.corall-polling is an object"); + entry.insert("enabled".to_string(), json!(true)); + + let config = entry.entry("config").or_insert_with(|| json!({})); + let config = config + .as_object_mut() + .expect("plugins.entries.corall-polling.config is an object"); + config.insert("credentialProfile".to_string(), json!("provider")); + if let Some(base_url) = base_url.filter(|url| !url.trim().is_empty()) { + config.insert("baseUrl".to_string(), json!(base_url)); + } +} + +fn stage_embedded_polling_plugin() -> Result { + let target = embedded_plugin_target_dir()?; + write_embedded_polling_plugin(&target)?; + Ok(target) +} + +fn write_embedded_polling_plugin(target: &Path) -> Result<()> { + if target.exists() { + fs::remove_dir_all(&target) + .with_context(|| format!("failed to replace {}", target.display()))?; + } + for file in EMBEDDED_POLLING_PLUGIN_FILES { + let path = target.join(file.path); + if let Some(parent) = path.parent() { + fs::create_dir_all(parent) + .with_context(|| format!("failed to create {}", parent.display()))?; + } + fs::write(&path, file.bytes) + .with_context(|| format!("failed to write {}", path.display()))?; + } + Ok(()) +} + +fn embedded_plugin_target_dir() -> Result { + let home = dirs::home_dir().context("cannot determine home directory")?; + Ok(home + .join(".corall") + .join("openclaw-plugins") + .join(POLLING_PLUGIN_ID)) +} + +fn install_openclaw_plugin(plugin_dir: &Path) -> Result<()> { + let output = Command::new("openclaw") + .args(["plugins", "install", "--force"]) + .arg(plugin_dir) + .output() + .context( + "failed to run `openclaw plugins install`; install OpenClaw or pass --skip-plugin-install", + )?; + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + let stdout = String::from_utf8_lossy(&output.stdout); + bail!( + "openclaw plugins install failed with status {}\nstdout:\n{}\nstderr:\n{}", + output.status, + stdout.trim(), + stderr.trim() + ); + } + Ok(()) +} + /// Set gateway fields required by Corall. /// /// Both `gateway.mode` and `gateway.bind` are forced unconditionally: -/// - `mode = "local"` — the gateway must run on this machine for webhook delivery. -/// - `bind = "lan"` — binds to 0.0.0.0 so the gateway is reachable for incoming webhooks. +/// - `mode = "local"` — the gateway must run on this machine for local hook delivery. +/// - `bind = "lan"` — binds to 0.0.0.0 so the polling plugin can reach the gateway in containers. /// /// Fails if `gateway.tailscale.mode` is `"serve"` or `"funnel"`: OpenClaw rejects /// the combination of `bind: "lan"` (non-loopback) with tailscale serve/funnel. @@ -252,3 +459,83 @@ fn expand_tilde(path: &str) -> PathBuf { } PathBuf::from(path) } + +#[cfg(test)] +mod tests { + use std::time::SystemTime; + use std::time::UNIX_EPOCH; + + use super::*; + + #[test] + fn apply_polling_plugin_config_enables_provider_poller() { + let mut cfg = json!({ + "plugins": { + "entries": { + "other": { "enabled": true } + } + } + }); + + apply_polling_plugin_config(&mut cfg, Some("http://eventbus:8081")); + + let entry = &cfg["plugins"]["entries"]["corall-polling"]; + assert_eq!(entry["enabled"], true); + assert_eq!(entry["config"]["baseUrl"], "http://eventbus:8081"); + assert_eq!(entry["config"]["credentialProfile"], "provider"); + assert_eq!(cfg["plugins"]["entries"]["other"]["enabled"], true); + } + + #[test] + fn apply_polling_plugin_config_preserves_existing_base_url_when_missing() { + let mut cfg = json!({ + "plugins": { + "entries": { + "corall-polling": { + "enabled": false, + "config": { + "baseUrl": "http://existing:8081", + "consumerId": "stable" + } + } + } + } + }); + + apply_polling_plugin_config(&mut cfg, None); + + let config = &cfg["plugins"]["entries"]["corall-polling"]["config"]; + assert_eq!(cfg["plugins"]["entries"]["corall-polling"]["enabled"], true); + assert_eq!(config["baseUrl"], "http://existing:8081"); + assert_eq!(config["consumerId"], "stable"); + assert_eq!(config["credentialProfile"], "provider"); + } + + #[test] + fn embedded_plugin_bundle_contains_runtime_files() { + let dir = unique_temp_dir("corall-polling-embedded"); + write_embedded_polling_plugin(&dir).unwrap(); + + assert!(dir.join("package.json").is_file()); + assert!(dir.join("openclaw.plugin.json").is_file()); + assert!(dir.join("dist/index.js").is_file()); + assert!(dir.join("dist/src/service.js").is_file()); + + let package = fs::read_to_string(dir.join("package.json")).unwrap(); + assert!(package.contains(r#""name": "corall-polling""#)); + assert!(package.contains(r#""main": "./dist/index.js""#)); + + let manifest = fs::read_to_string(dir.join("openclaw.plugin.json")).unwrap(); + assert!(manifest.contains(r#""id": "corall-polling""#)); + + let _ = fs::remove_dir_all(dir); + } + + fn unique_temp_dir(prefix: &str) -> PathBuf { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + std::env::temp_dir().join(format!("{prefix}-{}-{nanos}", std::process::id())) + } +} diff --git a/src/commands/skill_packages.rs b/src/commands/skill_packages.rs new file mode 100644 index 0000000..26c8721 --- /dev/null +++ b/src/commands/skill_packages.rs @@ -0,0 +1,160 @@ +use anyhow::Result; +use clap::Subcommand; +use serde_json::Value; +use serde_json::json; + +use crate::client::ApiClient; +use crate::credentials; + +#[derive(Subcommand)] +pub enum SkillPackagesCommand { + /// Print the required agent-generated skill package form template + FormTemplate, + /// Create a paid skill package for one of your agents + Create { + #[arg(long)] + agent_id: String, + /// JSON skill payload + #[arg(long)] + skills: String, + /// Price in cents + #[arg(long)] + price: i64, + }, + /// List skill packages you created + Mine, + /// Get a single skill package by ID + Get { id: String }, + /// Purchase a skill package through Stripe Checkout + Purchase { id: String }, + /// List skill packages purchased by the current user + Purchased, + /// Delete one of your skill packages + Delete { id: String }, +} + +pub async fn run(cmd: SkillPackagesCommand, profile: &str) -> Result<()> { + match cmd { + SkillPackagesCommand::FormTemplate => { + println!( + "{}", + serde_json::to_string_pretty(&skill_package_form_template())? + ); + } + SkillPackagesCommand::Create { + agent_id, + skills, + price, + } => { + let cred = credentials::load(profile)?; + let mut client = ApiClient::from_credential(&cred, profile).await?; + let body = json!({ + "agentId": agent_id, + "skills": serde_json::from_str::(&skills)?, + "price": price, + }); + let resp = client.post("/api/skill-packages", &body).await?; + println!("{}", serde_json::to_string_pretty(&resp)?); + } + SkillPackagesCommand::Mine => { + let cred = credentials::load(profile)?; + let mut client = ApiClient::from_credential(&cred, profile).await?; + let resp = client.get("/api/skill-packages/mine").await?; + println!("{}", serde_json::to_string_pretty(&resp)?); + } + SkillPackagesCommand::Get { id } => { + let cred = credentials::load(profile)?; + let mut client = ApiClient::from_credential(&cred, profile).await?; + let resp = client.get(&format!("/api/skill-packages/{id}")).await?; + println!("{}", serde_json::to_string_pretty(&resp)?); + } + SkillPackagesCommand::Purchase { id } => { + let cred = credentials::load(profile)?; + let mut client = ApiClient::from_credential(&cred, profile).await?; + let resp = client + .post_empty(&format!("/api/skill-packages/{id}/purchase")) + .await?; + println!("{}", serde_json::to_string_pretty(&resp)?); + } + SkillPackagesCommand::Purchased => { + let cred = credentials::load(profile)?; + let mut client = ApiClient::from_credential(&cred, profile).await?; + let resp = client.get("/api/skill-packages/purchased").await?; + println!("{}", serde_json::to_string_pretty(&resp)?); + } + SkillPackagesCommand::Delete { id } => { + let cred = credentials::load(profile)?; + let mut client = ApiClient::from_credential(&cred, profile).await?; + let status = client.delete(&format!("/api/skill-packages/{id}")).await?; + println!( + "{}", + serde_json::to_string_pretty(&json!({ + "deleted": status.is_success(), + "status": status.as_u16(), + }))? + ); + } + } + Ok(()) +} + +fn skill_package_form_template() -> Value { + json!({ + "version": 1, + "generatedBy": "agent", + "category": { + "primary": "Development", + "secondary": "CLI & Terminal" + }, + "description": { + "summary": "A concise description of what this skill does and the problems it solves.", + "activationTriggers": [ + "Use when the user asks for the workflow this skill enables." + ], + "keywords": ["keyword", "workflow", "domain"] + }, + "functions": [ + { + "name": "Primary function name", + "description": "Concrete action the skill performs, including expected input and output." + } + ], + "permissions": { + "env": [ + { + "name": "EXAMPLE_API_KEY", + "required": false, + "sensitive": true, + "purpose": "Authenticate to the declared external service." + } + ], + "network": [ + { + "domain": "api.example.com", + "purpose": "Call the declared external API." + } + ], + "filesystem": [ + { + "access": "read_write", + "scope": "workspace", + "purpose": "Read inputs and write generated artifacts inside the workspace." + } + ], + "tools": [ + { + "name": "curl", + "purpose": "Make documented API requests." + } + ], + "install": { + "hasInstallSteps": false, + "manualReviewRequired": false + }, + "persistence": { + "requiresBackgroundService": false, + "requiresElevatedPrivileges": false + } + } + }) +} diff --git a/src/credentials.rs b/src/credentials.rs index fd38513..1fb3ac1 100644 --- a/src/credentials.rs +++ b/src/credentials.rs @@ -4,6 +4,9 @@ use std::path::PathBuf; use anyhow::Context; use anyhow::Result; use anyhow::bail; +use ring::rand::SystemRandom; +use ring::signature::Ed25519KeyPair; +use ring::signature::KeyPair; use serde::Deserialize; use serde::Serialize; @@ -11,9 +14,8 @@ use serde::Serialize; #[serde(rename_all = "camelCase")] pub struct Credential { pub site: String, - pub email: String, - pub password: String, - pub user_id: String, + pub user: CredentialUser, + pub private_key_pkcs8: String, #[serde(skip_serializing_if = "Option::is_none")] pub agent_id: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -26,6 +28,18 @@ pub struct Credential { pub token_expires_at: Option, } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct CredentialUser { + pub id: String, + pub public_key: String, +} + +pub struct GeneratedKey { + pub private_key_pkcs8: String, + pub public_key: String, +} + impl Credential { /// Returns the cached token if it is still valid (with a 5-minute buffer). pub fn cached_token(&self) -> Option<&str> { @@ -43,6 +57,27 @@ impl Credential { } } +pub fn generate_key() -> Result { + let rng = SystemRandom::new(); + let private_key = Ed25519KeyPair::generate_pkcs8(&rng) + .map_err(|_| anyhow::anyhow!("failed to generate Ed25519 keypair"))?; + let key_pair = Ed25519KeyPair::from_pkcs8(private_key.as_ref()) + .map_err(|_| anyhow::anyhow!("failed to read generated Ed25519 keypair"))?; + + Ok(GeneratedKey { + private_key_pkcs8: hex::encode(private_key.as_ref()), + public_key: hex::encode(key_pair.public_key().as_ref()), + }) +} + +pub fn sign_challenge(private_key_pkcs8: &str, challenge: &str) -> Result { + let private_key = hex::decode(private_key_pkcs8).context("invalid privateKeyPkcs8 hex")?; + let challenge = hex::decode(challenge).context("invalid challenge hex")?; + let key_pair = Ed25519KeyPair::from_pkcs8(&private_key) + .map_err(|_| anyhow::anyhow!("invalid Ed25519 private key"))?; + Ok(hex::encode(key_pair.sign(&challenge).as_ref())) +} + pub fn remove(profile: &str) -> Result { let path = credentials_path(profile)?; if path.exists() { @@ -102,3 +137,57 @@ pub fn site_to_base_url(site: &str) -> String { format!("https://{site}") } } + +#[cfg(test)] +mod tests { + use ring::signature; + use serde_json::json; + + use super::*; + + #[test] + fn generated_key_signs_challenge() { + let key = generate_key().unwrap(); + let challenge = hex::encode(b"challenge"); + let signature_hex = sign_challenge(&key.private_key_pkcs8, &challenge).unwrap(); + let signature = hex::decode(signature_hex).unwrap(); + signature::UnparsedPublicKey::new( + &signature::ED25519, + hex::decode(key.public_key).unwrap(), + ) + .verify(b"challenge", &signature) + .unwrap(); + } + + #[test] + fn credential_serializes_expected_schema() { + let credential = Credential { + site: "http://corall.test".to_string(), + user: CredentialUser { + id: "user-1".to_string(), + public_key: "a".repeat(64), + }, + private_key_pkcs8: "b".repeat(64), + agent_id: Some("agent-1".to_string()), + registered_at: Some("2026-04-20T00:00:00Z".to_string()), + token: Some("token".to_string()), + token_expires_at: Some(1_776_000_000), + }; + + assert_eq!( + serde_json::to_value(credential).unwrap(), + json!({ + "site": "http://corall.test", + "user": { + "id": "user-1", + "publicKey": "a".repeat(64), + }, + "privateKeyPkcs8": "b".repeat(64), + "agentId": "agent-1", + "registeredAt": "2026-04-20T00:00:00Z", + "token": "token", + "tokenExpiresAt": 1776000000, + }) + ); + } +} diff --git a/src/eventbus.rs b/src/eventbus.rs new file mode 100644 index 0000000..dd32fcd --- /dev/null +++ b/src/eventbus.rs @@ -0,0 +1,1589 @@ +use std::collections::BTreeMap; +use std::future::Future; +use std::net::SocketAddr; +use std::pin::Pin; +use std::sync::Arc; + +use anyhow::Context; +use anyhow::Result; +use anyhow::anyhow; +use anyhow::bail; +use reqwest::Url; +use serde::Deserialize; +use serde::Serialize; +use serde_json::Value; +use serde_json::json; +use tokio::io::AsyncBufReadExt; +use tokio::io::AsyncReadExt; +use tokio::io::AsyncWriteExt; +use tokio::io::BufReader; +use tokio::net::TcpListener; +use tokio::net::TcpStream; + +type BoxFuture<'a, T> = Pin + Send + 'a>>; + +const REGISTRATION_PREFIX: &str = "corall:eventbus:agent"; +const STREAM_SUFFIX: &str = "stream"; +const REGISTRATION_SUFFIX: &str = "registration"; +const STREAM_START_ID: &str = "0"; + +#[derive(Debug, Clone)] +pub struct EventBusServeOptions { + pub listen: SocketAddr, + pub redis_url: String, + pub consumer_group: String, + pub default_wait_ms: u64, + pub max_wait_ms: u64, + pub default_count: usize, + pub max_count: usize, + pub claim_idle_ms: Option, +} + +pub struct EventBusServer { + state: Arc, +} + +impl EventBusServer { + pub fn new(options: EventBusServeOptions) -> Result { + let store = Arc::new(RedisEventStore::new( + RedisConfig::from_url(&options.redis_url)?, + options.consumer_group.clone(), + )); + Ok(Self { + state: Arc::new(AppState { options, store }), + }) + } + + #[cfg(test)] + fn with_store(options: EventBusServeOptions, store: Arc) -> Self { + Self { + state: Arc::new(AppState { options, store }), + } + } + + pub async fn serve(self) -> Result<()> { + let listener = TcpListener::bind(self.state.options.listen) + .await + .with_context(|| format!("failed to bind {}", self.state.options.listen))?; + self.serve_listener(listener).await + } + + async fn serve_listener(self, listener: TcpListener) -> Result<()> { + println!( + "{}", + serde_json::to_string_pretty(&json!({ + "listen": self.state.options.listen.to_string(), + "redisUrl": redact_redis_url(&self.state.options.redis_url), + "consumerGroup": self.state.options.consumer_group, + "routes": { + "health": "/health", + "poll": "/v1/agents/:agent_id/events?consumerId=&wait=&count=", + "ack": "/v1/agents/:agent_id/events/:event_id/ack", + }, + }))? + ); + + loop { + let (socket, _) = listener + .accept() + .await + .context("failed to accept connection")?; + let state = self.state.clone(); + tokio::spawn(async move { + if let Err(err) = handle_connection(socket, state).await { + eprintln!( + "{}", + serde_json::json!({ "error": format!("eventbus connection failed: {err}") }) + ); + } + }); + } + } +} + +struct AppState { + options: EventBusServeOptions, + store: Arc, +} + +trait EventStore: Send + Sync + 'static { + fn health(&self) -> BoxFuture<'_, Result<()>>; + fn load_registration<'a>( + &'a self, + agent_id: &'a str, + ) -> BoxFuture<'a, Result>>; + fn poll<'a>( + &'a self, + agent_id: &'a str, + options: PollOptions, + ) -> BoxFuture<'a, Result>>; + fn ack<'a>(&'a self, agent_id: &'a str, event_id: &'a str) -> BoxFuture<'a, Result>; +} + +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] +struct AgentRegistration { + token: String, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct PollOptions { + consumer_id: String, + wait_ms: u64, + count: usize, + claim_idle_ms: Option, +} + +#[derive(Debug, Clone, Serialize)] +struct HealthResponse { + ok: bool, + redis: &'static str, +} + +#[derive(Debug, Clone, Serialize)] +struct PollResponse { + #[serde(rename = "consumerId")] + consumer_id: String, + events: Vec, +} + +#[derive(Debug, Clone, Serialize)] +struct AckResponse { + ok: bool, + acked: u64, + #[serde(rename = "eventId")] + event_id: String, +} + +#[derive(Debug, Clone, Serialize)] +struct ErrorResponse<'a> { + error: &'a str, +} + +#[derive(Debug, Clone)] +struct HttpRequest { + method: String, + path: String, + query: Option, + headers: BTreeMap, +} + +#[derive(Debug, Clone)] +struct HttpResponse { + status: u16, + headers: Vec<(String, String)>, + body: String, +} + +impl HttpResponse { + fn json(status: u16, body: &T) -> Self { + let body = serde_json::to_string(body) + .unwrap_or_else(|_| "{\"error\":\"serialization failed\"}".to_owned()); + Self { + status, + headers: vec![("Content-Type".into(), "application/json".into())], + body, + } + } + + fn unauthorized(message: &str) -> Self { + let mut response = Self::json(401, &ErrorResponse { error: message }); + response + .headers + .push(("WWW-Authenticate".into(), "Bearer".into())); + response + } + + fn into_bytes(self) -> Vec { + let mut output = format!( + "HTTP/1.1 {} {}\r\nContent-Length: {}\r\nConnection: close\r\n", + self.status, + reason_phrase(self.status), + self.body.len() + ) + .into_bytes(); + for (name, value) in self.headers { + output.extend_from_slice(name.as_bytes()); + output.extend_from_slice(b": "); + output.extend_from_slice(value.as_bytes()); + output.extend_from_slice(b"\r\n"); + } + output.extend_from_slice(b"\r\n"); + output.extend_from_slice(self.body.as_bytes()); + output + } +} + +#[derive(Debug, Clone)] +struct HttpError { + status: u16, + message: String, +} + +impl HttpError { + fn bad_request(message: impl Into) -> Self { + Self { + status: 400, + message: message.into(), + } + } + + fn unauthorized(message: impl Into) -> Self { + Self { + status: 401, + message: message.into(), + } + } + + fn method_not_allowed(message: impl Into) -> Self { + Self { + status: 405, + message: message.into(), + } + } + + fn not_found(message: impl Into) -> Self { + Self { + status: 404, + message: message.into(), + } + } + + fn service_unavailable(message: impl Into) -> Self { + Self { + status: 503, + message: message.into(), + } + } + + fn into_response(self) -> HttpResponse { + if self.status == 401 { + HttpResponse::unauthorized(&self.message) + } else { + HttpResponse::json( + self.status, + &ErrorResponse { + error: &self.message, + }, + ) + } + } +} + +enum Route { + Health, + Poll { agent_id: String }, + Ack { agent_id: String, event_id: String }, +} + +#[derive(Debug, Clone)] +struct RedisEventStore { + redis: RedisConfig, + consumer_group: String, +} + +impl RedisEventStore { + fn new(redis: RedisConfig, consumer_group: String) -> Self { + Self { + redis, + consumer_group, + } + } + + async fn health_impl(&self) -> Result<()> { + let mut conn = self.redis.connect().await?; + let reply = conn.execute(&["PING"]).await?; + match reply { + RespValue::Simple(value) if value == "PONG" => Ok(()), + RespValue::Bulk(Some(value)) if value == b"PONG" => Ok(()), + other => bail!("unexpected PING response: {other:?}"), + } + } + + async fn load_registration_impl(&self, agent_id: &str) -> Result> { + let mut conn = self.redis.connect().await?; + let key = registration_key(agent_id); + match conn.execute(&["GET", key.as_str()]).await? { + RespValue::Bulk(None) => Ok(None), + RespValue::Bulk(Some(bytes)) => Ok(Some( + serde_json::from_slice(&bytes).context("registration JSON is invalid")?, + )), + RespValue::Simple(value) => Ok(Some( + serde_json::from_str(&value).context("registration JSON is invalid")?, + )), + other => bail!("unexpected GET response: {other:?}"), + } + } + + async fn poll_impl(&self, agent_id: &str, options: PollOptions) -> Result> { + let stream = stream_key(agent_id); + let mut conn = self.redis.connect().await?; + ensure_group(&mut conn, &stream, &self.consumer_group).await?; + + let claimed = if let Some(claim_idle_ms) = options.claim_idle_ms.filter(|value| *value > 0) + { + match xautoclaim( + &mut conn, + &stream, + &self.consumer_group, + &options.consumer_id, + claim_idle_ms, + options.count, + ) + .await + { + Ok(entries) => entries, + Err(err) if redis_command_unsupported(&err) => Vec::new(), + Err(err) => return Err(err), + } + } else { + Vec::new() + }; + + let entries = if claimed.is_empty() { + xreadgroup( + &mut conn, + &stream, + &self.consumer_group, + &options.consumer_id, + options.count, + options.wait_ms, + ) + .await? + } else { + claimed + }; + + entries.into_iter().map(event_from_entry).collect() + } + + async fn ack_impl(&self, agent_id: &str, event_id: &str) -> Result { + let stream = stream_key(agent_id); + let mut conn = self.redis.connect().await?; + match xack(&mut conn, &stream, &self.consumer_group, event_id).await { + Ok(acked) => Ok(acked), + Err(err) if redis_group_missing(&err) => Ok(0), + Err(err) => Err(err), + } + } +} + +impl EventStore for RedisEventStore { + fn health(&self) -> BoxFuture<'_, Result<()>> { + Box::pin(async move { self.health_impl().await }) + } + + fn load_registration<'a>( + &'a self, + agent_id: &'a str, + ) -> BoxFuture<'a, Result>> { + Box::pin(async move { self.load_registration_impl(agent_id).await }) + } + + fn poll<'a>( + &'a self, + agent_id: &'a str, + options: PollOptions, + ) -> BoxFuture<'a, Result>> { + Box::pin(async move { self.poll_impl(agent_id, options).await }) + } + + fn ack<'a>(&'a self, agent_id: &'a str, event_id: &'a str) -> BoxFuture<'a, Result> { + Box::pin(async move { self.ack_impl(agent_id, event_id).await }) + } +} + +#[derive(Debug, Clone)] +struct RedisConfig { + host: String, + port: u16, + username: Option, + password: Option, + db: usize, +} + +impl RedisConfig { + fn from_url(raw: &str) -> Result { + let url = Url::parse(raw).with_context(|| format!("invalid redis URL: {raw}"))?; + match url.scheme() { + "redis" => {} + "rediss" => bail!("rediss:// is not supported by this scaffold"), + scheme => bail!("unsupported redis URL scheme: {scheme}"), + } + + let host = url + .host_str() + .context("redis URL is missing a host")? + .to_owned(); + let port = url.port().unwrap_or(6379); + let username = (!url.username().is_empty()).then(|| url.username().to_owned()); + let password = url.password().map(str::to_owned); + if username.is_some() && password.is_none() { + bail!("redis URL username requires a password"); + } + + let db = parse_database(url.path())?; + Ok(Self { + host, + port, + username, + password, + db, + }) + } + + async fn connect(&self) -> Result { + let stream = TcpStream::connect((self.host.as_str(), self.port)) + .await + .with_context(|| { + format!("failed to connect to redis at {}:{}", self.host, self.port) + })?; + let mut conn = RedisConnection { + stream: BufReader::new(stream), + }; + + if let Some(password) = &self.password { + let auth = if let Some(username) = &self.username { + conn.execute(&["AUTH", username.as_str(), password.as_str()]) + .await? + } else { + conn.execute(&["AUTH", password.as_str()]).await? + }; + expect_ok(auth, "AUTH")?; + } + + if self.db != 0 { + let select = conn + .execute(&["SELECT", &self.db.to_string()]) + .await + .context("failed to select redis database")?; + expect_ok(select, "SELECT")?; + } + + Ok(conn) + } +} + +struct RedisConnection { + stream: BufReader, +} + +impl RedisConnection { + async fn execute(&mut self, args: &[&str]) -> Result { + let mut buffer = format!("*{}\r\n", args.len()).into_bytes(); + for arg in args { + buffer.extend_from_slice(format!("${}\r\n", arg.len()).as_bytes()); + buffer.extend_from_slice(arg.as_bytes()); + buffer.extend_from_slice(b"\r\n"); + } + self.stream.get_mut().write_all(&buffer).await?; + self.read_value().await + } + + async fn read_value(&mut self) -> Result { + let line = read_required_line(&mut self.stream).await?; + let (prefix, payload) = line + .split_first() + .ok_or_else(|| anyhow!("redis response line was empty"))?; + match prefix { + b'+' => Ok(RespValue::Simple(String::from_utf8(payload.to_vec())?)), + b'-' => Ok(RespValue::Error(String::from_utf8(payload.to_vec())?)), + b':' => Ok(RespValue::Integer( + String::from_utf8(payload.to_vec())?.parse()?, + )), + b'$' => { + let len: i64 = String::from_utf8(payload.to_vec())?.parse()?; + if len < 0 { + return Ok(RespValue::Bulk(None)); + } + let mut body = vec![0; len as usize]; + self.stream.read_exact(&mut body).await?; + let mut crlf = [0_u8; 2]; + self.stream.read_exact(&mut crlf).await?; + if crlf != *b"\r\n" { + bail!("redis bulk response missing CRLF"); + } + Ok(RespValue::Bulk(Some(body))) + } + b'*' => { + let len: i64 = String::from_utf8(payload.to_vec())?.parse()?; + if len < 0 { + return Ok(RespValue::Array(None)); + } + let mut values = Vec::with_capacity(len as usize); + for _ in 0..len { + values.push(Box::pin(self.read_value()).await?); + } + Ok(RespValue::Array(Some(values))) + } + other => bail!("unsupported redis RESP prefix: {}", *other as char), + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +enum RespValue { + Simple(String), + Error(String), + Integer(i64), + Bulk(Option>), + Array(Option>), +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct StreamEntry { + id: String, + fields: BTreeMap, +} + +async fn handle_connection(socket: TcpStream, state: Arc) -> Result<()> { + let mut reader = BufReader::new(socket); + let request = match read_http_request(&mut reader).await? { + Some(request) => request, + None => return Ok(()), + }; + + let response = handle_http_request(state, request).await.into_bytes(); + let stream = reader.get_mut(); + stream.write_all(&response).await?; + stream.shutdown().await?; + Ok(()) +} + +async fn handle_http_request(state: Arc, request: HttpRequest) -> HttpResponse { + match dispatch_request(state, request).await { + Ok(response) => response, + Err(error) => error.into_response(), + } +} + +async fn dispatch_request( + state: Arc, + request: HttpRequest, +) -> Result { + let route = parse_route(&request.path)?; + match route { + Route::Health => { + if request.method != "GET" { + return Err(HttpError::method_not_allowed("health only supports GET")); + } + state.store.health().await.map_err(|err| { + HttpError::service_unavailable(format!("redis health check failed: {err}")) + })?; + Ok(HttpResponse::json( + 200, + &HealthResponse { + ok: true, + redis: "ok", + }, + )) + } + Route::Poll { agent_id } => { + if request.method != "GET" { + return Err(HttpError::method_not_allowed("poll only supports GET")); + } + authorize(&state, &request, &agent_id).await?; + let options = parse_poll_options(&state.options, &request.query)?; + let events = state + .store + .poll(&agent_id, options.clone()) + .await + .map_err(|err| HttpError::service_unavailable(format!("poll failed: {err}")))?; + Ok(HttpResponse::json( + 200, + &PollResponse { + consumer_id: options.consumer_id, + events, + }, + )) + } + Route::Ack { agent_id, event_id } => { + if request.method != "POST" { + return Err(HttpError::method_not_allowed("ack only supports POST")); + } + authorize(&state, &request, &agent_id).await?; + let acked = state + .store + .ack(&agent_id, &event_id) + .await + .map_err(|err| HttpError::service_unavailable(format!("ack failed: {err}")))?; + Ok(HttpResponse::json( + 200, + &AckResponse { + ok: true, + acked, + event_id, + }, + )) + } + } +} + +async fn authorize( + state: &AppState, + request: &HttpRequest, + agent_id: &str, +) -> Result<(), HttpError> { + let provided = bearer_token(request) + .ok_or_else(|| HttpError::unauthorized("missing Authorization: Bearer "))?; + let registration = state + .store + .load_registration(agent_id) + .await + .map_err(|err| { + HttpError::service_unavailable(format!("registration lookup failed: {err}")) + })?; + let registration = registration + .ok_or_else(|| HttpError::unauthorized("agent is not registered for eventbus"))?; + if registration.token != provided { + return Err(HttpError::unauthorized("invalid bearer token")); + } + Ok(()) +} + +fn parse_route(path: &str) -> Result { + let path = if path.len() > 1 { + path.trim_end_matches('/') + } else { + path + }; + if path == "/health" { + return Ok(Route::Health); + } + + let url = Url::parse(&format!("http://localhost{path}")) + .map_err(|err| HttpError::bad_request(format!("invalid path: {err}")))?; + let segments: Vec<_> = url + .path_segments() + .map(|items| items.collect()) + .unwrap_or_else(Vec::new); + + match segments.as_slice() { + ["v1", "agents", agent_id, "events"] => Ok(Route::Poll { + agent_id: (*agent_id).to_owned(), + }), + ["v1", "agents", agent_id, "events", event_id, "ack"] => Ok(Route::Ack { + agent_id: (*agent_id).to_owned(), + event_id: (*event_id).to_owned(), + }), + _ => Err(HttpError::not_found("route not found")), + } +} + +fn parse_poll_options( + options: &EventBusServeOptions, + query: &Option, +) -> Result { + let params = query_params(query)?; + let consumer_id = params + .get("consumerId") + .filter(|value| !value.is_empty()) + .cloned() + .ok_or_else(|| HttpError::bad_request("missing consumerId query parameter"))?; + let wait_ms = parse_u64_param(params.get("wait"), "wait")?.unwrap_or(options.default_wait_ms); + let count = parse_usize_param(params.get("count"), "count")?.unwrap_or(options.default_count); + let query_claim_idle_ms = parse_u64_param(params.get("claimIdleMs"), "claimIdleMs")?; + + if count == 0 { + return Err(HttpError::bad_request("count must be greater than zero")); + } + + Ok(PollOptions { + consumer_id, + wait_ms: wait_ms.min(options.max_wait_ms), + count: count.min(options.max_count), + claim_idle_ms: query_claim_idle_ms.or(options.claim_idle_ms), + }) +} + +fn query_params(query: &Option) -> Result, HttpError> { + let Some(query) = query else { + return Ok(BTreeMap::new()); + }; + let url = Url::parse(&format!("http://localhost/?{query}")) + .map_err(|err| HttpError::bad_request(format!("invalid query string: {err}")))?; + Ok(url.query_pairs().into_owned().collect()) +} + +fn parse_u64_param(value: Option<&String>, name: &str) -> Result, HttpError> { + match value { + Some(raw) => raw + .parse() + .map(Some) + .map_err(|_| HttpError::bad_request(format!("{name} must be an unsigned integer"))), + None => Ok(None), + } +} + +fn parse_usize_param(value: Option<&String>, name: &str) -> Result, HttpError> { + match value { + Some(raw) => raw + .parse() + .map(Some) + .map_err(|_| HttpError::bad_request(format!("{name} must be an unsigned integer"))), + None => Ok(None), + } +} + +fn bearer_token(request: &HttpRequest) -> Option { + let value = request.headers.get("authorization")?; + value + .strip_prefix("Bearer ") + .or_else(|| value.strip_prefix("bearer ")) + .map(str::to_owned) +} + +async fn read_http_request(reader: &mut BufReader) -> Result> { + let Some(request_line) = read_line(reader).await? else { + return Ok(None); + }; + if request_line.trim().is_empty() { + return Ok(None); + } + + let mut parts = request_line.split_whitespace(); + let method = parts + .next() + .context("missing HTTP method")? + .to_ascii_uppercase(); + let target = parts.next().context("missing request target")?.to_owned(); + let version = parts.next().context("missing HTTP version")?; + if version != "HTTP/1.1" && version != "HTTP/1.0" { + bail!("unsupported HTTP version: {version}"); + } + + let (path, query) = split_target(&target)?; + let mut headers = BTreeMap::new(); + let mut content_length = 0_usize; + + loop { + let line = read_required_line(reader).await?; + if line.is_empty() { + break; + } + let header = String::from_utf8(line)?; + let (name, value) = header + .split_once(':') + .ok_or_else(|| anyhow!("invalid HTTP header line"))?; + let name = name.trim().to_ascii_lowercase(); + let value = value.trim().to_owned(); + if name == "content-length" { + content_length = value.parse().context("invalid Content-Length header")?; + } + headers.insert(name, value); + } + + if content_length > 0 { + let mut discard = vec![0_u8; content_length]; + reader.read_exact(&mut discard).await?; + } + + Ok(Some(HttpRequest { + method, + path, + query, + headers, + })) +} + +async fn read_line(reader: &mut BufReader) -> Result> { + let mut line = Vec::new(); + let bytes = reader.read_until(b'\n', &mut line).await?; + if bytes == 0 { + return Ok(None); + } + if !line.ends_with(b"\r\n") { + bail!("HTTP line missing CRLF terminator"); + } + line.truncate(line.len() - 2); + Ok(Some(String::from_utf8(line)?)) +} + +async fn read_required_line(reader: &mut BufReader) -> Result> { + let mut line = Vec::new(); + let bytes = reader.read_until(b'\n', &mut line).await?; + if bytes == 0 { + bail!("unexpected EOF"); + } + if !line.ends_with(b"\r\n") { + bail!("line missing CRLF terminator"); + } + line.truncate(line.len() - 2); + Ok(line) +} + +fn split_target(target: &str) -> Result<(String, Option)> { + if target.starts_with("http://") || target.starts_with("https://") { + let url = Url::parse(target)?; + return Ok((url.path().to_owned(), url.query().map(str::to_owned))); + } + match target.split_once('?') { + Some((path, query)) => Ok((path.to_owned(), Some(query.to_owned()))), + None => Ok((target.to_owned(), None)), + } +} + +fn reason_phrase(status: u16) -> &'static str { + match status { + 200 => "OK", + 400 => "Bad Request", + 401 => "Unauthorized", + 404 => "Not Found", + 405 => "Method Not Allowed", + 503 => "Service Unavailable", + _ => "Internal Server Error", + } +} + +fn redact_redis_url(raw: &str) -> String { + match Url::parse(raw) { + Ok(url) => { + let mut display = format!("{}://{}", url.scheme(), url.host_str().unwrap_or("unknown")); + if let Some(port) = url.port() { + display.push(':'); + display.push_str(&port.to_string()); + } + if !url.path().is_empty() && url.path() != "/" { + display.push_str(url.path()); + } + display + } + Err(_) => raw.to_owned(), + } +} + +fn parse_database(path: &str) -> Result { + let trimmed = path.trim_start_matches('/'); + if trimmed.is_empty() { + return Ok(0); + } + let first = trimmed + .split('/') + .next() + .ok_or_else(|| anyhow!("invalid redis database path"))?; + Ok(first.parse()?) +} + +fn registration_key(agent_id: &str) -> String { + format!("{REGISTRATION_PREFIX}:{agent_id}:{REGISTRATION_SUFFIX}") +} + +fn stream_key(agent_id: &str) -> String { + format!("{REGISTRATION_PREFIX}:{agent_id}:{STREAM_SUFFIX}") +} + +async fn ensure_group(conn: &mut RedisConnection, stream: &str, group: &str) -> Result<()> { + match conn + .execute(&[ + "XGROUP", + "CREATE", + stream, + group, + STREAM_START_ID, + "MKSTREAM", + ]) + .await? + { + RespValue::Simple(value) if value == "OK" => Ok(()), + RespValue::Error(message) if message.contains("BUSYGROUP") => Ok(()), + other => bail!("unexpected XGROUP response: {other:?}"), + } +} + +async fn xreadgroup( + conn: &mut RedisConnection, + stream: &str, + group: &str, + consumer: &str, + count: usize, + wait_ms: u64, +) -> Result> { + let count_str = count.to_string(); + let wait_str = wait_ms.to_string(); + let response = if wait_ms > 0 { + conn.execute(&[ + "XREADGROUP", + "GROUP", + group, + consumer, + "COUNT", + count_str.as_str(), + "BLOCK", + wait_str.as_str(), + "STREAMS", + stream, + ">", + ]) + .await? + } else { + conn.execute(&[ + "XREADGROUP", + "GROUP", + group, + consumer, + "COUNT", + count_str.as_str(), + "STREAMS", + stream, + ">", + ]) + .await? + }; + parse_xreadgroup_entries(response) +} + +async fn xautoclaim( + conn: &mut RedisConnection, + stream: &str, + group: &str, + consumer: &str, + min_idle_ms: u64, + count: usize, +) -> Result> { + let min_idle = min_idle_ms.to_string(); + let count = count.to_string(); + let response = conn + .execute(&[ + "XAUTOCLAIM", + stream, + group, + consumer, + min_idle.as_str(), + "0-0", + "COUNT", + count.as_str(), + ]) + .await?; + parse_xautoclaim_entries(response) +} + +async fn xack( + conn: &mut RedisConnection, + stream: &str, + group: &str, + event_id: &str, +) -> Result { + match conn.execute(&["XACK", stream, group, event_id]).await? { + RespValue::Integer(value) if value >= 0 => Ok(value as u64), + RespValue::Error(message) => bail!(message), + other => bail!("unexpected XACK response: {other:?}"), + } +} + +fn expect_ok(value: RespValue, command: &str) -> Result<()> { + match value { + RespValue::Simple(reply) if reply == "OK" => Ok(()), + RespValue::Error(message) => bail!("{command} failed: {message}"), + other => bail!("unexpected {command} response: {other:?}"), + } +} + +fn parse_xreadgroup_entries(value: RespValue) -> Result> { + let streams = match value { + RespValue::Array(None) => return Ok(Vec::new()), + RespValue::Array(Some(items)) => items, + RespValue::Error(message) => bail!(message), + other => bail!("unexpected XREADGROUP response: {other:?}"), + }; + + let mut entries = Vec::new(); + for stream in streams { + let mut parts = into_array(stream, "stream entry")?.into_iter(); + let _stream_name = + into_string(parts.next().context("missing stream name")?, "stream name")?; + let stream_entries = into_array( + parts.next().context("missing stream records")?, + "stream records", + )?; + entries.extend(parse_entry_array(stream_entries)?); + } + Ok(entries) +} + +fn parse_xautoclaim_entries(value: RespValue) -> Result> { + let parts = match value { + RespValue::Array(Some(items)) => items, + RespValue::Array(None) => return Ok(Vec::new()), + RespValue::Error(message) => bail!(message), + other => bail!("unexpected XAUTOCLAIM response: {other:?}"), + }; + if parts.len() < 2 { + bail!("XAUTOCLAIM response was missing claimed entries"); + } + parse_entry_array(into_array(parts[1].clone(), "claimed entries")?) +} + +fn parse_entry_array(entries: Vec) -> Result> { + let mut parsed = Vec::with_capacity(entries.len()); + for entry in entries { + let mut parts = into_array(entry, "stream record")?.into_iter(); + let id = into_string( + parts.next().context("missing stream record id")?, + "stream record id", + )?; + let fields = into_array( + parts.next().context("missing stream record fields")?, + "stream record fields", + )?; + parsed.push(StreamEntry { + id, + fields: parse_fields(fields)?, + }); + } + Ok(parsed) +} + +fn parse_fields(values: Vec) -> Result> { + if values.len() % 2 != 0 { + bail!("stream fields must have an even number of items"); + } + + let mut fields = BTreeMap::new(); + let mut iter = values.into_iter(); + while let Some(key) = iter.next() { + let value = iter.next().context("missing field value")?; + fields.insert( + into_string(key, "field name")?, + into_string(value, "field value")?, + ); + } + Ok(fields) +} + +fn event_from_entry(mut entry: StreamEntry) -> Result { + let payload = ["event", "payload", "data", "json"] + .into_iter() + .find_map(|field| entry.fields.remove(field)); + + let mut object = match payload { + Some(raw) => match serde_json::from_str::(&raw) { + Ok(Value::Object(map)) => map, + Ok(other) => { + let mut map = serde_json::Map::new(); + map.insert("payload".into(), other); + map + } + Err(err) => return Err(anyhow!("failed to parse stream event JSON: {err}")), + }, + None => serde_json::Map::new(), + }; + + for (key, value) in entry.fields { + object + .entry(key) + .or_insert_with(|| parse_field_json(&value)); + } + + if let Some(existing_id) = object.get("id").and_then(Value::as_str).map(str::to_owned) { + if existing_id != entry.id { + object + .entry("eventId") + .or_insert_with(|| Value::String(existing_id)); + } + } + object.insert("id".into(), Value::String(entry.id)); + Ok(Value::Object(object)) +} + +fn parse_field_json(value: &str) -> Value { + serde_json::from_str(value).unwrap_or_else(|_| Value::String(value.to_owned())) +} + +fn into_array(value: RespValue, context: &str) -> Result> { + match value { + RespValue::Array(Some(items)) => Ok(items), + other => bail!("{context} was not a RESP array: {other:?}"), + } +} + +fn into_string(value: RespValue, context: &str) -> Result { + match value { + RespValue::Simple(text) => Ok(text), + RespValue::Bulk(Some(bytes)) => Ok(String::from_utf8(bytes)?), + RespValue::Error(message) => bail!("{message}"), + other => bail!("{context} was not a RESP string: {other:?}"), + } +} + +fn redis_group_missing(err: &anyhow::Error) -> bool { + err.to_string().contains("NOGROUP") +} + +fn redis_command_unsupported(err: &anyhow::Error) -> bool { + let message = err.to_string(); + message.contains("unknown command") || message.contains("ERR unknown") +} + +#[cfg(test)] +mod tests { + use std::sync::Mutex; + + use super::*; + + #[derive(Default)] + struct MockStore { + registration: Option, + poll_events: Vec, + acked: u64, + last_poll: Mutex>, + last_ack: Mutex>, + } + + impl EventStore for MockStore { + fn health(&self) -> BoxFuture<'_, Result<()>> { + Box::pin(async { Ok(()) }) + } + + fn load_registration<'a>( + &'a self, + _agent_id: &'a str, + ) -> BoxFuture<'a, Result>> { + let registration = self.registration.clone(); + Box::pin(async move { Ok(registration) }) + } + + fn poll<'a>( + &'a self, + agent_id: &'a str, + options: PollOptions, + ) -> BoxFuture<'a, Result>> { + *self.last_poll.lock().unwrap() = Some((agent_id.to_owned(), options)); + let events = self.poll_events.clone(); + Box::pin(async move { Ok(events) }) + } + + fn ack<'a>(&'a self, agent_id: &'a str, event_id: &'a str) -> BoxFuture<'a, Result> { + *self.last_ack.lock().unwrap() = Some((agent_id.to_owned(), event_id.to_owned())); + let acked = self.acked; + Box::pin(async move { Ok(acked) }) + } + } + + fn test_options() -> EventBusServeOptions { + EventBusServeOptions { + listen: "127.0.0.1:8787".parse().unwrap(), + redis_url: "redis://127.0.0.1:6379/0".into(), + consumer_group: "corall-eventbus".into(), + default_wait_ms: 25_000, + max_wait_ms: 30_000, + default_count: 50, + max_count: 100, + claim_idle_ms: Some(30_000), + } + } + + fn test_request(method: &str, target: &str, auth: Option<&str>) -> HttpRequest { + let (path, query) = split_target(target).unwrap(); + let mut headers = BTreeMap::new(); + if let Some(token) = auth { + headers.insert("authorization".into(), format!("Bearer {token}")); + } + HttpRequest { + method: method.into(), + path, + query, + headers, + } + } + + #[tokio::test] + async fn poll_requires_bearer_token() { + let store = Arc::new(MockStore { + registration: Some(AgentRegistration { + token: "secret".into(), + }), + ..Default::default() + }); + let server = EventBusServer::with_store(test_options(), store); + let response = handle_http_request( + server.state.clone(), + test_request("GET", "/v1/agents/agent-1/events?consumerId=worker-1", None), + ) + .await; + + assert_eq!(response.status, 401); + assert!(response.body.contains("missing Authorization")); + } + + #[tokio::test] + async fn poll_returns_events_and_forwards_query_options() { + let store = Arc::new(MockStore { + registration: Some(AgentRegistration { + token: "secret".into(), + }), + poll_events: vec![json!({ + "id": "1719938100000-0", + "type": "order.paid", + "agentId": "agent-1", + "orderId": "order-1", + "hook": { "message": "paid", "name": "Corall", "sessionKey": "hook:corall:order-1", "deliver": false } + })], + ..Default::default() + }); + let server = EventBusServer::with_store(test_options(), store.clone()); + let response = handle_http_request( + server.state.clone(), + test_request( + "GET", + "/v1/agents/agent-1/events?consumerId=worker-1&wait=1500&count=2&claimIdleMs=60000", + Some("secret"), + ), + ) + .await; + + assert_eq!(response.status, 200); + let body: Value = serde_json::from_str(&response.body).unwrap(); + assert_eq!(body["consumerId"], "worker-1"); + assert_eq!(body["events"].as_array().unwrap().len(), 1); + + let (agent_id, poll) = store.last_poll.lock().unwrap().clone().unwrap(); + assert_eq!(agent_id, "agent-1"); + assert_eq!( + poll, + PollOptions { + consumer_id: "worker-1".into(), + wait_ms: 1_500, + count: 2, + claim_idle_ms: Some(60_000), + } + ); + } + + #[tokio::test] + async fn ack_uses_agent_and_event_from_route() { + let store = Arc::new(MockStore { + registration: Some(AgentRegistration { + token: "secret".into(), + }), + acked: 1, + ..Default::default() + }); + let server = EventBusServer::with_store(test_options(), store.clone()); + let response = handle_http_request( + server.state.clone(), + test_request( + "POST", + "/v1/agents/agent-1/events/1719938100000-0/ack", + Some("secret"), + ), + ) + .await; + + assert_eq!(response.status, 200); + let body: Value = serde_json::from_str(&response.body).unwrap(); + assert_eq!(body["acked"], 1); + assert_eq!(body["eventId"], "1719938100000-0"); + + let (agent_id, event_id) = store.last_ack.lock().unwrap().clone().unwrap(); + assert_eq!(agent_id, "agent-1"); + assert_eq!(event_id, "1719938100000-0"); + } + + #[test] + fn parses_xreadgroup_records() { + let response = RespValue::Array(Some(vec![RespValue::Array(Some(vec![ + bulk("corall:eventbus:agent:agent-1:stream"), + RespValue::Array(Some(vec![RespValue::Array(Some(vec![ + bulk("1719938100000-0"), + RespValue::Array(Some(vec![ + bulk("event"), + bulk("{\"type\":\"order.paid\",\"agentId\":\"agent-1\"}"), + ])), + ]))])), + ]))])); + + let entries = parse_xreadgroup_entries(response).unwrap(); + assert_eq!(entries.len(), 1); + assert_eq!(entries[0].id, "1719938100000-0"); + assert_eq!( + entries[0].fields.get("event"), + Some(&"{\"type\":\"order.paid\",\"agentId\":\"agent-1\"}".to_owned()) + ); + } + + #[test] + fn event_payload_uses_stream_id_for_ack() { + let event = event_from_entry(StreamEntry { + id: "1719938100000-0".into(), + fields: BTreeMap::from([( + "event".into(), + "{\"id\":\"domain-event-1\",\"type\":\"order.paid\",\"agentId\":\"agent-1\"}" + .into(), + )]), + }) + .unwrap(); + + assert_eq!(event["id"], "1719938100000-0"); + assert_eq!(event["eventId"], "domain-event-1"); + } + + #[tokio::test] + async fn redis_contract_polls_and_acks_stream_event() { + let Some(redis_url) = test_redis_url() else { + eprintln!("skipping eventbus redis contract test: CORALL_TEST_REDIS_URL is unset"); + return; + }; + + let agent_id = unique_id("agent"); + let group = unique_id("group"); + let stream = stream_key(&agent_id); + let registration = registration_key(&agent_id); + let redis = RedisConfig::from_url(&redis_url).unwrap(); + let mut conn = redis.connect().await.unwrap(); + + conn.execute(&["DEL", registration.as_str(), stream.as_str()]) + .await + .unwrap(); + let registration_json = serde_json::json!({ "token": "secret" }).to_string(); + conn.execute(&["SET", registration.as_str(), registration_json.as_str()]) + .await + .unwrap(); + let payload = serde_json::json!({ + "id": "domain-event-1", + "type": "order.paid", + "agentId": agent_id, + "orderId": "order-1", + "hook": { + "message": "paid", + "name": "Corall", + "sessionKey": "hook:corall:order-1", + "deliver": false + } + }) + .to_string(); + conn.execute(&["XADD", stream.as_str(), "*", "payload", payload.as_str()]) + .await + .unwrap(); + + let store = Arc::new(RedisEventStore::new(redis.clone(), group)); + let mut options = test_options(); + options.redis_url = redis_url; + options.default_wait_ms = 0; + options.max_wait_ms = 100; + options.claim_idle_ms = None; + let server = EventBusServer::with_store(options, store); + let poll_target = + format!("/v1/agents/{agent_id}/events?consumerId=worker-1&wait=0&count=1"); + let response = handle_http_request( + server.state.clone(), + test_request("GET", &poll_target, Some("secret")), + ) + .await; + + assert_eq!(response.status, 200); + let body: Value = serde_json::from_str(&response.body).unwrap(); + let event_id = body["events"][0]["id"].as_str().unwrap().to_owned(); + assert_eq!(body["events"][0]["eventId"], "domain-event-1"); + assert_eq!( + body["events"][0]["hook"]["sessionKey"], + "hook:corall:order-1" + ); + + let ack_target = format!("/v1/agents/{agent_id}/events/{event_id}/ack"); + let response = handle_http_request( + server.state.clone(), + test_request("POST", &ack_target, Some("secret")), + ) + .await; + assert_eq!(response.status, 200); + let body: Value = serde_json::from_str(&response.body).unwrap(); + assert_eq!(body["acked"], 1); + + let mut cleanup = redis.connect().await.unwrap(); + cleanup + .execute(&["DEL", registration.as_str(), stream.as_str()]) + .await + .unwrap(); + } + + #[tokio::test] + async fn redis_contract_reclaims_unacked_event_after_idle() { + let Some(redis_url) = test_redis_url() else { + eprintln!("skipping eventbus redis reclaim test: CORALL_TEST_REDIS_URL is unset"); + return; + }; + + let agent_id = unique_id("agent_reclaim"); + let group = unique_id("group_reclaim"); + let stream = stream_key(&agent_id); + let registration = registration_key(&agent_id); + let redis = RedisConfig::from_url(&redis_url).unwrap(); + let mut conn = redis.connect().await.unwrap(); + + conn.execute(&["DEL", registration.as_str(), stream.as_str()]) + .await + .unwrap(); + conn.execute(&["SET", registration.as_str(), r#"{"token":"secret"}"#]) + .await + .unwrap(); + conn.execute(&[ + "XADD", + stream.as_str(), + "*", + "payload", + r#"{"type":"order.paid","agentId":"agent_reclaim","orderId":"order-reclaim","hook":{"message":"paid","name":"Corall","sessionKey":"hook:corall:order-reclaim","deliver":false}}"#, + ]) + .await + .unwrap(); + + let store = Arc::new(RedisEventStore::new(redis.clone(), group)); + let mut options = test_options(); + options.redis_url = redis_url; + options.default_wait_ms = 0; + options.max_wait_ms = 100; + options.claim_idle_ms = None; + let server = EventBusServer::with_store(options, store); + + let first_poll = format!("/v1/agents/{agent_id}/events?consumerId=worker-1&wait=0&count=1"); + let first_response = handle_http_request( + server.state.clone(), + test_request("GET", &first_poll, Some("secret")), + ) + .await; + assert_eq!(first_response.status, 200); + let first_body: Value = serde_json::from_str(&first_response.body).unwrap(); + let event_id = first_body["events"][0]["id"].as_str().unwrap().to_owned(); + + tokio::time::sleep(std::time::Duration::from_millis(5)).await; + + let reclaim_poll = format!( + "/v1/agents/{agent_id}/events?consumerId=worker-2&wait=0&count=1&claimIdleMs=1" + ); + let reclaim_response = handle_http_request( + server.state.clone(), + test_request("GET", &reclaim_poll, Some("secret")), + ) + .await; + assert_eq!(reclaim_response.status, 200); + let reclaim_body: Value = serde_json::from_str(&reclaim_response.body).unwrap(); + assert_eq!(reclaim_body["events"][0]["id"], event_id); + + let ack_target = format!("/v1/agents/{agent_id}/events/{event_id}/ack"); + let ack_response = handle_http_request( + server.state.clone(), + test_request("POST", &ack_target, Some("secret")), + ) + .await; + assert_eq!(ack_response.status, 200); + + let mut cleanup = redis.connect().await.unwrap(); + cleanup + .execute(&["DEL", registration.as_str(), stream.as_str()]) + .await + .unwrap(); + } + + #[tokio::test] + async fn polling_http_server_integrates_with_redis_without_openclaw_or_llm() { + let Some(redis_url) = test_redis_url() else { + eprintln!("skipping eventbus HTTP integration test: CORALL_TEST_REDIS_URL is unset"); + return; + }; + + let agent_id = unique_id("agent_http"); + let group = unique_id("group_http"); + let stream = stream_key(&agent_id); + let registration = registration_key(&agent_id); + let redis = RedisConfig::from_url(&redis_url).unwrap(); + let mut conn = redis.connect().await.unwrap(); + + conn.execute(&["DEL", registration.as_str(), stream.as_str()]) + .await + .unwrap(); + conn.execute(&["SET", registration.as_str(), r#"{"token":"secret"}"#]) + .await + .unwrap(); + conn.execute(&[ + "XADD", + stream.as_str(), + "*", + "payload", + r#"{"id":"domain-event-http","type":"order.paid","agentId":"agent_http","orderId":"order-http","hook":{"message":"paid","name":"Corall","sessionKey":"hook:corall:order-http","deliver":false}}"#, + ]) + .await + .unwrap(); + + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + let mut options = test_options(); + options.listen = addr; + options.redis_url = redis_url; + options.consumer_group = group; + options.default_wait_ms = 0; + options.max_wait_ms = 100; + options.claim_idle_ms = None; + + let server = EventBusServer::new(options).unwrap(); + let server_task = tokio::spawn(server.serve_listener(listener)); + + let poll_path = + format!("/v1/agents/{agent_id}/events?consumerId=worker-http&wait=0&count=1"); + let poll = raw_http_json(addr, "GET", &poll_path, Some("secret")).await; + assert_eq!(poll["status"], 200); + let event = &poll["body"]["events"][0]; + let event_id = event["id"].as_str().unwrap(); + assert_eq!(event["eventId"], "domain-event-http"); + assert_eq!(event["hook"]["sessionKey"], "hook:corall:order-http"); + + let ack_path = format!("/v1/agents/{agent_id}/events/{event_id}/ack"); + let ack = raw_http_json(addr, "POST", &ack_path, Some("secret")).await; + assert_eq!(ack["status"], 200); + assert_eq!(ack["body"]["acked"], 1); + + server_task.abort(); + let mut cleanup = redis.connect().await.unwrap(); + cleanup + .execute(&["DEL", registration.as_str(), stream.as_str()]) + .await + .unwrap(); + } + + fn bulk(value: &str) -> RespValue { + RespValue::Bulk(Some(value.as_bytes().to_vec())) + } + + async fn raw_http_json( + addr: SocketAddr, + method: &str, + path: &str, + bearer: Option<&str>, + ) -> Value { + let mut stream = TcpStream::connect(addr).await.unwrap(); + let auth = bearer + .map(|token| format!("Authorization: Bearer {token}\r\n")) + .unwrap_or_default(); + let request = format!( + "{method} {path} HTTP/1.1\r\nHost: {addr}\r\n{auth}Connection: close\r\nContent-Length: 0\r\n\r\n" + ); + stream.write_all(request.as_bytes()).await.unwrap(); + + let mut raw = Vec::new(); + stream.read_to_end(&mut raw).await.unwrap(); + let response = String::from_utf8(raw).unwrap(); + let (head, body) = response.split_once("\r\n\r\n").unwrap(); + let status: u16 = head + .lines() + .next() + .unwrap() + .split_whitespace() + .nth(1) + .unwrap() + .parse() + .unwrap(); + json!({ + "status": status, + "body": serde_json::from_str::(body).unwrap(), + }) + } + + fn test_redis_url() -> Option { + std::env::var("CORALL_TEST_REDIS_URL").ok() + } + + fn unique_id(prefix: &str) -> String { + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos(); + format!("{prefix}_{nanos}_{}", std::process::id()) + } +} diff --git a/src/main.rs b/src/main.rs index 5b69e12..738fbb2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod client; mod commands; mod credentials; +mod eventbus; use anyhow::Result; use clap::Parser; @@ -9,9 +10,11 @@ use commands::agent; use commands::agents; use commands::auth; use commands::connect; +use commands::eventbus as eventbus_cmd; use commands::openclaw; use commands::orders; use commands::reviews; +use commands::skill_packages; use commands::subscriptions; use commands::upgrade; use commands::upload; @@ -59,6 +62,12 @@ enum Command { #[command(subcommand)] cmd: reviews::ReviewsCommand, }, + /// Manage paid skill packages + #[command(name = "skill-packages")] + SkillPackages { + #[command(subcommand)] + cmd: skill_packages::SkillPackagesCommand, + }, /// Manage subscriptions Subscriptions { #[command(subcommand)] @@ -66,6 +75,11 @@ enum Command { }, /// Upgrade corall to the latest release Upgrade, + /// Redis-backed HTTP polling server for agent event delivery + Eventbus { + #[command(subcommand)] + cmd: eventbus_cmd::EventbusCommand, + }, /// File upload helpers Upload { #[command(subcommand)] @@ -99,8 +113,10 @@ async fn run() -> Result<()> { Command::Agent { cmd } => agent::run(cmd, profile).await, Command::Connect { cmd } => connect::run(cmd, profile).await, Command::Reviews { cmd } => reviews::run(cmd, profile).await, + Command::SkillPackages { cmd } => skill_packages::run(cmd, profile).await, Command::Subscriptions { cmd } => subscriptions::run(cmd, profile).await, Command::Upgrade => upgrade::run().await, + Command::Eventbus { cmd } => eventbus_cmd::run(cmd).await, Command::Upload { cmd } => upload::run(cmd, profile).await, Command::Openclaw { cmd } => openclaw::run(cmd).await, } diff --git a/tests/browser_auth.rs b/tests/browser_auth.rs new file mode 100644 index 0000000..88ab070 --- /dev/null +++ b/tests/browser_auth.rs @@ -0,0 +1,362 @@ +use std::error::Error; +use std::fs; +use std::io::Read; +use std::io::Write; +use std::net::SocketAddr; +use std::net::TcpListener; +use std::net::TcpStream; +use std::path::PathBuf; +use std::process::Command; +use std::sync::Arc; +use std::sync::Mutex; +use std::thread; +use std::time::Duration; + +use ring::signature; +use serde_json::Value; +use serde_json::json; + +#[test] +fn browser_approve_signs_challenge_without_leaking_secrets() -> Result<(), Box> { + let challenge = b"corall browser login challenge"; + let challenge_hex = hex::encode(challenge); + let state = Arc::new(Mutex::new(FakeAuthState { + challenge_hex: challenge_hex.clone(), + public_key: None, + saw_register_without_password: false, + saw_valid_browser_signature: false, + })); + let server = FakeAuthServer::start(state.clone())?; + let home = TempHome::new("corall-cli-browser-auth")?; + + let register = run_corall( + &home, + &[ + "--profile", + "agent-test", + "auth", + "register", + &server.base_url(), + "--name", + "Agent Test", + ], + )?; + assert!(register.status.success(), "register failed: {register:?}"); + let register_stdout = String::from_utf8(register.stdout)?; + assert!(!register_stdout.contains("privateKeyPkcs8")); + assert!(!register_stdout.contains("password")); + + let approve = run_corall( + &home, + &[ + "--profile", + "agent-test", + "auth", + "browser", + "approve", + &server.base_url(), + "--code", + "ABCD-EFGH", + ], + )?; + assert!(approve.status.success(), "approve failed: {approve:?}"); + let approve_stdout = String::from_utf8(approve.stdout)?; + assert!(approve_stdout.contains(r#""approved": true"#)); + assert!(!approve_stdout.contains("token")); + assert!(!approve_stdout.contains("privateKeyPkcs8")); + assert!(!approve_stdout.contains("signature")); + + let wrong_site = run_corall( + &home, + &[ + "--profile", + "agent-test", + "auth", + "browser", + "approve", + "http://127.0.0.1:9", + "--code", + "ABCD-EFGH", + ], + )?; + assert!(!wrong_site.status.success()); + let wrong_site_stderr = String::from_utf8(wrong_site.stderr)?; + assert!(wrong_site_stderr.contains("belong to")); + + let state = state.lock().unwrap(); + assert!(state.saw_register_without_password); + assert!(state.saw_valid_browser_signature); + Ok(()) +} + +fn run_corall(home: &TempHome, args: &[&str]) -> Result> { + Ok(Command::new(env!("CARGO_BIN_EXE_corall")) + .args(args) + .env("HOME", home.path()) + .output()?) +} + +#[derive(Debug)] +struct FakeAuthState { + challenge_hex: String, + public_key: Option, + saw_register_without_password: bool, + saw_valid_browser_signature: bool, +} + +struct FakeAuthServer { + addr: SocketAddr, + handle: Option>>, +} + +impl FakeAuthServer { + fn start(state: Arc>) -> Result> { + let listener = TcpListener::bind("127.0.0.1:0")?; + let addr = listener.local_addr()?; + let handle = thread::spawn(move || { + for _ in 0..3 { + let (stream, _) = listener.accept().map_err(|e| e.to_string())?; + handle_request(stream, &state)?; + } + Ok(()) + }); + Ok(Self { + addr, + handle: Some(handle), + }) + } + + fn base_url(&self) -> String { + format!("http://{}", self.addr) + } +} + +impl Drop for FakeAuthServer { + fn drop(&mut self) { + if let Some(handle) = self.handle.take() { + match handle.join() { + Ok(Ok(())) => {} + Ok(Err(e)) => panic!("fake auth server failed: {e}"), + Err(_) => panic!("fake auth server panicked"), + } + } + } +} + +fn handle_request(mut stream: TcpStream, state: &Arc>) -> Result<(), String> { + stream + .set_read_timeout(Some(Duration::from_secs(2))) + .map_err(|e| e.to_string())?; + let request = read_http_request(&mut stream)?; + match request.path.as_str() { + "/api/auth/register" => handle_register(stream, state, request.body), + "/api/auth/browser/challenge" => handle_browser_challenge(stream, state, request.body), + "/api/auth/browser/approve" => handle_browser_approve(stream, state, request.body), + _ => respond_json(stream, 404, json!({ "error": "not found" })), + } +} + +fn handle_register( + stream: TcpStream, + state: &Arc>, + body: Vec, +) -> Result<(), String> { + let body: Value = serde_json::from_slice(&body).map_err(|e| e.to_string())?; + let public_key = body + .get("publicKey") + .and_then(Value::as_str) + .ok_or("register missing publicKey")?; + if body.get("email").is_some() || body.get("password").is_some() { + return Err("register leaked legacy email/password fields".to_string()); + } + + let mut state = state.lock().unwrap(); + state.public_key = Some(public_key.to_string()); + state.saw_register_without_password = true; + + respond_json( + stream, + 201, + json!({ + "token": "server-register-token", + "user": { + "id": "user-agent-test", + "name": body.get("name").and_then(Value::as_str).unwrap_or("Agent Test"), + "publicKey": public_key, + "status": "ACTIVE", + "isAdmin": false, + "createdAt": "2026-04-22T00:00:00" + } + }), + ) +} + +fn handle_browser_challenge( + stream: TcpStream, + state: &Arc>, + body: Vec, +) -> Result<(), String> { + let body: Value = serde_json::from_slice(&body).map_err(|e| e.to_string())?; + assert_eq!(body["code"], "ABCD-EFGH"); + let public_key = body + .get("publicKey") + .and_then(Value::as_str) + .ok_or("challenge missing publicKey")?; + + let state = state.lock().unwrap(); + assert_eq!(state.public_key.as_deref(), Some(public_key)); + respond_json( + stream, + 200, + json!({ + "requestId": "browser-request-1", + "challenge": state.challenge_hex, + "expiresAt": 1_776_807_600_i64 + }), + ) +} + +fn handle_browser_approve( + stream: TcpStream, + state: &Arc>, + body: Vec, +) -> Result<(), String> { + let body: Value = serde_json::from_slice(&body).map_err(|e| e.to_string())?; + assert_eq!(body["code"], "ABCD-EFGH"); + assert!(body.get("token").is_none()); + assert!(body.get("privateKeyPkcs8").is_none()); + + let public_key = body + .get("publicKey") + .and_then(Value::as_str) + .ok_or("approve missing publicKey")?; + let signature_hex = body + .get("signature") + .and_then(Value::as_str) + .ok_or("approve missing signature")?; + let public_key_bytes = hex::decode(public_key).map_err(|e| e.to_string())?; + let signature_bytes = hex::decode(signature_hex).map_err(|e| e.to_string())?; + let challenge = { + let state = state.lock().unwrap(); + hex::decode(&state.challenge_hex).map_err(|e| e.to_string())? + }; + signature::UnparsedPublicKey::new(&signature::ED25519, public_key_bytes) + .verify(&challenge, &signature_bytes) + .map_err(|_| "browser approval signature was invalid".to_string())?; + + let mut state = state.lock().unwrap(); + state.saw_valid_browser_signature = true; + respond_json( + stream, + 200, + json!({ + "approved": true, + "requestId": "browser-request-1", + "user": { + "id": "user-agent-test", + "name": "Agent Test", + "publicKey": public_key, + "status": "ACTIVE", + "isAdmin": false, + "createdAt": "2026-04-22T00:00:00" + } + }), + ) +} + +struct HttpRequest { + path: String, + body: Vec, +} + +fn read_http_request(stream: &mut TcpStream) -> Result { + let mut raw = Vec::new(); + let mut buf = [0_u8; 1024]; + let header_end; + loop { + let n = stream.read(&mut buf).map_err(|e| e.to_string())?; + if n == 0 { + return Err("connection closed before headers".to_string()); + } + raw.extend_from_slice(&buf[..n]); + if let Some(pos) = raw.windows(4).position(|w| w == b"\r\n\r\n") { + header_end = pos + 4; + break; + } + } + + let head = String::from_utf8(raw[..header_end].to_vec()).map_err(|e| e.to_string())?; + let request_line = head.lines().next().ok_or("missing request line")?; + let path = request_line + .split_whitespace() + .nth(1) + .ok_or("missing request path")? + .to_string(); + let content_length = head + .lines() + .find_map(|line| { + let (name, value) = line.split_once(':')?; + if name.eq_ignore_ascii_case("content-length") { + value.trim().parse::().ok() + } else { + None + } + }) + .unwrap_or(0); + let mut body = raw[header_end..].to_vec(); + while body.len() < content_length { + let n = stream.read(&mut buf).map_err(|e| e.to_string())?; + if n == 0 { + return Err("connection closed before body".to_string()); + } + body.extend_from_slice(&buf[..n]); + } + body.truncate(content_length); + Ok(HttpRequest { path, body }) +} + +fn respond_json(mut stream: TcpStream, status: u16, body: Value) -> Result<(), String> { + let status_text = match status { + 200 => "OK", + 201 => "Created", + 404 => "Not Found", + _ => "OK", + }; + let body = serde_json::to_vec(&body).map_err(|e| e.to_string())?; + let head = format!( + "HTTP/1.1 {status} {status_text}\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n", + body.len() + ); + stream + .write_all(head.as_bytes()) + .and_then(|_| stream.write_all(&body)) + .map_err(|e| e.to_string()) +} + +struct TempHome { + path: PathBuf, +} + +impl TempHome { + fn new(prefix: &str) -> Result> { + let path = std::env::temp_dir().join(format!( + "{}-{}", + prefix, + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH)? + .as_nanos() + )); + fs::create_dir_all(&path)?; + Ok(Self { path }) + } + + fn path(&self) -> &PathBuf { + &self.path + } +} + +impl Drop for TempHome { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } +} diff --git a/tests/eventbus_polling.rs b/tests/eventbus_polling.rs new file mode 100644 index 0000000..efeb606 --- /dev/null +++ b/tests/eventbus_polling.rs @@ -0,0 +1,272 @@ +use std::error::Error; +use std::io::Read; +use std::io::Write; +use std::net::SocketAddr; +use std::net::TcpListener; +use std::net::TcpStream; +use std::process::Child; +use std::process::Command; +use std::process::Stdio; +use std::thread; +use std::time::Duration; +use std::time::Instant; + +use serde_json::Value; +use serde_json::json; + +#[test] +fn eventbus_binary_polls_and_acks_redis_without_llm_config() -> Result<(), Box> { + let Some(redis_url) = std::env::var("CORALL_TEST_REDIS_URL").ok() else { + eprintln!("skipping eventbus integration test: CORALL_TEST_REDIS_URL is unset"); + return Ok(()); + }; + let Some(redis) = RedisEndpoint::parse(&redis_url) else { + eprintln!("skipping eventbus integration test: unsupported Redis URL {redis_url}"); + return Ok(()); + }; + + let agent_id = unique_id("agent_proc"); + let group = unique_id("group_proc"); + let stream = format!("corall:eventbus:agent:{agent_id}:stream"); + let registration = format!("corall:eventbus:agent:{agent_id}:registration"); + let event_payload = json!({ + "id": "domain-event-process", + "type": "order.paid", + "agentId": agent_id, + "orderId": "order-process", + "hook": { + "message": "paid", + "name": "Corall", + "sessionKey": "hook:corall:order-process", + "deliver": false + } + }) + .to_string(); + + redis_command(&redis, &["DEL", ®istration, &stream])?; + redis_command(&redis, &["SET", ®istration, r#"{"token":"secret"}"#])?; + redis_command(&redis, &["XADD", &stream, "*", "payload", &event_payload])?; + + let listen = reserve_local_addr()?; + let mut child = ChildGuard::spawn( + env!("CARGO_BIN_EXE_corall"), + &[ + "eventbus", + "serve", + "--listen", + &listen.to_string(), + "--redis-url", + &redis_url, + "--consumer-group", + &group, + "--default-wait-ms", + "0", + "--max-wait-ms", + "100", + "--claim-idle-ms", + "0", + ], + )?; + + wait_for_health(listen, child.as_mut())?; + + let poll_path = format!("/v1/agents/{agent_id}/events?consumerId=worker-proc&wait=0&count=1"); + let poll = raw_http_json(listen, "GET", &poll_path, Some("secret"))?; + assert_eq!(poll["status"], 200); + let event = &poll["body"]["events"][0]; + let event_id = event["id"].as_str().expect("event id must be present"); + assert_eq!(event["eventId"], "domain-event-process"); + assert_eq!(event["hook"]["sessionKey"], "hook:corall:order-process"); + + let ack_path = format!("/v1/agents/{agent_id}/events/{event_id}/ack"); + let ack = raw_http_json(listen, "POST", &ack_path, Some("secret"))?; + assert_eq!(ack["status"], 200); + assert_eq!(ack["body"]["acked"], 1); + + redis_command(&redis, &["DEL", ®istration, &stream])?; + child.kill(); + Ok(()) +} + +struct ChildGuard { + child: Child, +} + +impl ChildGuard { + fn spawn(binary: &str, args: &[&str]) -> Result> { + let child = Command::new(binary) + .args(args) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()?; + Ok(Self { child }) + } + + fn as_mut(&mut self) -> &mut Child { + &mut self.child + } + + fn kill(&mut self) { + let _ = self.child.kill(); + let _ = self.child.wait(); + } +} + +impl Drop for ChildGuard { + fn drop(&mut self) { + self.kill(); + } +} + +struct RedisEndpoint { + host: String, + port: u16, + db: usize, +} + +impl RedisEndpoint { + fn parse(raw: &str) -> Option { + let rest = raw.strip_prefix("redis://")?; + let (authority, path) = rest.split_once('/').unwrap_or((rest, "0")); + if authority.contains('@') { + return None; + } + let (host, port) = authority + .rsplit_once(':') + .map(|(host, port)| Some((host.to_owned(), port.parse().ok()?))) + .unwrap_or_else(|| Some((authority.to_owned(), 6379)))?; + let db = path.split('?').next().unwrap_or("0").parse().unwrap_or(0); + Some(Self { host, port, db }) + } + + fn addr(&self) -> String { + format!("{}:{}", self.host, self.port) + } +} + +fn reserve_local_addr() -> Result> { + let listener = TcpListener::bind("127.0.0.1:0")?; + Ok(listener.local_addr()?) +} + +fn wait_for_health(addr: SocketAddr, child: &mut Child) -> Result<(), Box> { + let deadline = Instant::now() + Duration::from_secs(5); + loop { + if let Some(status) = child.try_wait()? { + return Err(format!("eventbus process exited before health check: {status}").into()); + } + + if let Ok(response) = raw_http_json(addr, "GET", "/health", None) { + if response["status"] == 200 { + return Ok(()); + } + } + + if Instant::now() >= deadline { + return Err("eventbus process did not become healthy".into()); + } + thread::sleep(Duration::from_millis(50)); + } +} + +fn raw_http_json( + addr: SocketAddr, + method: &str, + path: &str, + bearer: Option<&str>, +) -> Result> { + let mut stream = TcpStream::connect_timeout(&addr, Duration::from_secs(1))?; + stream.set_read_timeout(Some(Duration::from_secs(2)))?; + let auth = bearer + .map(|token| format!("Authorization: Bearer {token}\r\n")) + .unwrap_or_default(); + let request = format!( + "{method} {path} HTTP/1.1\r\nHost: {addr}\r\n{auth}Connection: close\r\nContent-Length: 0\r\n\r\n" + ); + stream.write_all(request.as_bytes())?; + + let mut raw = String::new(); + stream.read_to_string(&mut raw)?; + let (head, body) = raw + .split_once("\r\n\r\n") + .ok_or("HTTP response did not contain a header/body split")?; + let status: u16 = head + .lines() + .next() + .and_then(|line| line.split_whitespace().nth(1)) + .ok_or("HTTP response did not contain a status")? + .parse()?; + Ok(json!({ + "status": status, + "body": serde_json::from_str::(body)?, + })) +} + +fn redis_command(redis: &RedisEndpoint, args: &[&str]) -> Result> { + let mut stream = TcpStream::connect(redis.addr())?; + stream.set_read_timeout(Some(Duration::from_secs(2)))?; + if redis.db != 0 { + write_resp(&mut stream, &["SELECT", &redis.db.to_string()])?; + read_resp(&mut stream)?; + } + write_resp(&mut stream, args)?; + read_resp(&mut stream) +} + +fn write_resp(stream: &mut TcpStream, args: &[&str]) -> Result<(), Box> { + write!(stream, "*{}\r\n", args.len())?; + for arg in args { + write!(stream, "${}\r\n{}\r\n", arg.len(), arg)?; + } + stream.flush()?; + Ok(()) +} + +fn read_resp(stream: &mut TcpStream) -> Result> { + let mut prefix = [0_u8; 1]; + stream.read_exact(&mut prefix)?; + let line = read_crlf_line(stream)?; + match prefix[0] { + b'+' | b':' => Ok(line), + b'-' => Err(format!("Redis returned error: {line}").into()), + b'$' => read_bulk(stream, line.parse()?), + other => Err(format!("unsupported Redis response prefix: {}", other as char).into()), + } +} + +fn read_bulk(stream: &mut TcpStream, len: isize) -> Result> { + if len < 0 { + return Ok(String::new()); + } + let mut body = vec![0_u8; len as usize]; + stream.read_exact(&mut body)?; + let mut crlf = [0_u8; 2]; + stream.read_exact(&mut crlf)?; + if crlf != *b"\r\n" { + return Err("Redis bulk response missing CRLF".into()); + } + Ok(String::from_utf8(body)?) +} + +fn read_crlf_line(stream: &mut TcpStream) -> Result> { + let mut bytes = Vec::new(); + let mut previous = 0_u8; + loop { + let mut byte = [0_u8; 1]; + stream.read_exact(&mut byte)?; + if previous == b'\r' && byte[0] == b'\n' { + bytes.pop(); + return Ok(String::from_utf8(bytes)?); + } + bytes.push(byte[0]); + previous = byte[0]; + } +} + +fn unique_id(prefix: &str) -> String { + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos(); + format!("{prefix}_{nanos}_{}", std::process::id()) +} diff --git a/tests/openclaw_setup.rs b/tests/openclaw_setup.rs new file mode 100644 index 0000000..992df59 --- /dev/null +++ b/tests/openclaw_setup.rs @@ -0,0 +1,133 @@ +use std::error::Error; +use std::fs; +use std::path::Path; +use std::path::PathBuf; +use std::process::Command; +use std::time::SystemTime; +use std::time::UNIX_EPOCH; + +use serde_json::Value; + +#[cfg(unix)] +#[test] +fn setup_installs_bundled_polling_plugin_through_openclaw_cli() -> Result<(), Box> { + let temp = TempDir::new("corall-openclaw-setup")?; + let home = temp.path().join("home"); + let bin = temp.path().join("bin"); + let config_path = temp.path().join("openclaw.json"); + let capture_path = temp.path().join("openclaw-args.txt"); + fs::create_dir_all(&home)?; + fs::create_dir_all(&bin)?; + fs::write(&config_path, r#"{"gateway":{},"hooks":{}}"#)?; + + write_fake_openclaw(&bin.join("openclaw"))?; + + let old_path = std::env::var_os("PATH").unwrap_or_default(); + let path = format!("{}:{}", bin.display(), old_path.to_string_lossy()); + let output = Command::new(env!("CARGO_BIN_EXE_corall")) + .args([ + "openclaw", + "setup", + "--config", + path_str(&config_path)?, + "--webhook-token", + "hook-token", + "--eventbus-url", + "http://eventbus.test:8787", + ]) + .env("HOME", &home) + .env("PATH", path) + .env("OPENCLAW_CAPTURE_ARGS", &capture_path) + .output()?; + + assert!( + output.status.success(), + "setup failed\nstdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + + let staged = home.join(".corall/openclaw-plugins/corall-polling"); + assert!(staged.join("openclaw.plugin.json").is_file()); + assert!(staged.join("dist/index.js").is_file()); + + let captured = fs::read_to_string(&capture_path)?; + assert_eq!( + captured.lines().collect::>(), + vec![ + "plugins", + "install", + "--force", + staged.to_str().ok_or("staged plugin path is not utf-8")? + ] + ); + + let cfg: Value = serde_json::from_str(&fs::read_to_string(&config_path)?)?; + let plugin = &cfg["plugins"]["entries"]["corall-polling"]; + assert_eq!(cfg["hooks"]["token"], "hook-token"); + assert_eq!(plugin["enabled"], true); + assert_eq!(plugin["config"]["baseUrl"], "http://eventbus.test:8787"); + assert_eq!(plugin["config"]["credentialProfile"], "provider"); + + let stdout: Value = serde_json::from_slice(&output.stdout)?; + assert_eq!(stdout["plugin"]["id"], "corall-polling"); + assert_eq!(stdout["plugin"]["installed"], true); + assert_eq!(stdout["plugin"]["sourcePath"], staged.display().to_string()); + assert_eq!(stdout["plugin"]["baseUrl"], "http://eventbus.test:8787"); + + Ok(()) +} + +#[cfg(unix)] +fn write_fake_openclaw(path: &Path) -> Result<(), Box> { + use std::os::unix::fs::PermissionsExt; + + fs::write( + path, + r#"#!/bin/sh +set -eu +printf '%s\n' "$@" > "$OPENCLAW_CAPTURE_ARGS" +if [ "$#" -eq 4 ] && + [ "$1" = "plugins" ] && + [ "$2" = "install" ] && + [ "$3" = "--force" ] && + [ -f "$4/openclaw.plugin.json" ] && + [ -f "$4/dist/index.js" ]; then + exit 0 +fi +exit 42 +"#, + )?; + let mut permissions = fs::metadata(path)?.permissions(); + permissions.set_mode(0o755); + fs::set_permissions(path, permissions)?; + Ok(()) +} + +fn path_str(path: &Path) -> Result<&str, Box> { + path.to_str() + .ok_or_else(|| format!("path is not valid utf-8: {}", path.display()).into()) +} + +struct TempDir { + path: PathBuf, +} + +impl TempDir { + fn new(prefix: &str) -> Result> { + let nanos = SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos(); + let path = std::env::temp_dir().join(format!("{prefix}-{}-{nanos}", std::process::id())); + fs::create_dir_all(&path)?; + Ok(Self { path }) + } + + fn path(&self) -> &Path { + &self.path + } +} + +impl Drop for TempDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } +} diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs new file mode 100644 index 0000000..380e8c2 --- /dev/null +++ b/tests/skill_contract.rs @@ -0,0 +1,136 @@ +const SKILL: &str = include_str!("../skills/corall/SKILL.md"); +const ORDER_HANDLE: &str = include_str!("../skills/corall/references/order-handle.md"); +const ORDER_CREATE: &str = include_str!("../skills/corall/references/order-create.md"); +const SETUP_PROVIDER: &str = include_str!("../skills/corall/references/setup-provider-openclaw.md"); +const SKILL_PACKAGE_SUBMIT: &str = + include_str!("../skills/corall/references/skill-package-submit.md"); +const BROWSER_LOGIN: &str = include_str!("../skills/corall/references/browser-login.md"); +const CLI_REFERENCE: &str = include_str!("../skills/corall/references/cli-reference.md"); +const EVAL_CASES: &str = include_str!("../skills/corall/evals/cases.md"); +const PLUGIN_JSON: &str = include_str!("../skills/corall/.claude-plugin/plugin.json"); + +#[test] +fn skill_routes_corall_prompts_to_the_expected_modes() { + assert_contains(SKILL, "hook:corall:*"); + assert_contains(SKILL, "references/order-handle.md"); + assert_contains(SKILL, "references/order-create.md"); + assert_contains(SKILL, "references/skill-package-submit.md"); + assert_contains(SKILL, "references/setup-provider-openclaw.md"); + assert_contains(SKILL, "references/browser-login.md"); + assert_contains(SKILL, "Pass it explicitly on every command"); + assert_contains(SKILL, "Hook verification"); + assert_contains(SKILL, "Never expose a private key"); + assert_contains(PLUGIN_JSON, "OpenClaw polling plugin"); + assert_not_contains(PLUGIN_JSON, "OpenClaw webhook"); +} + +#[test] +fn order_handle_prompt_accepts_then_submits_with_provider_profile() { + assert_contains(ORDER_HANDLE, "hook-triggered mode"); + assert_contains(ORDER_HANDLE, "corall auth me --profile provider"); + assert_contains( + ORDER_HANDLE, + "corall agent accept --profile provider", + ); + assert_contains(ORDER_HANDLE, "corall agent submit --summary"); + assert_contains(ORDER_HANDLE, "--profile provider"); + assert_contains(ORDER_HANDLE, "Always submit, no matter what"); + assert_contains(ORDER_HANDLE, "Task failed: "); + assert_contains(ORDER_HANDLE, "Refused: "); + assert_contains( + ORDER_HANDLE, + "does **not** authorize reading or uploading pre-existing host files", + ); + assert_not_contains(ORDER_HANDLE, "webhook mode"); +} + +#[test] +fn order_create_prompt_matches_current_cli_responses_and_statuses() { + assert_contains(ORDER_CREATE, "corall agents list --profile employer"); + assert_contains( + ORDER_CREATE, + "corall agents get --profile employer", + ); + assert_contains(ORDER_CREATE, "corall orders create "); + assert_contains( + ORDER_CREATE, + "corall orders payment-status --profile employer", + ); + assert_contains(ORDER_CREATE, r#"{ "status": "succeeded" }"#); + assert_contains(ORDER_CREATE, "until it reaches `delivered`"); + assert_contains( + ORDER_CREATE, + "corall orders approve --profile employer", + ); + assert_contains( + ORDER_CREATE, + "corall orders dispute --profile employer", + ); + assert_contains(ORDER_CREATE, "corall reviews create "); + assert_not_contains(ORDER_CREATE, "paymentStatus"); + assert_not_contains(ORDER_CREATE, "orderStatus"); + assert_not_contains(ORDER_CREATE, "SUBMITTED"); +} + +#[test] +fn provider_setup_prompt_uses_polling_and_explicit_provider_profile() { + assert_contains(SETUP_PROVIDER, "resident Corall polling plugin"); + assert_contains(SETUP_PROVIDER, "corall openclaw setup"); + assert_contains(SETUP_PROVIDER, "--eventbus-url"); + assert_contains( + SETUP_PROVIDER, + "installs the bundled `corall-polling` plugin", + ); + assert_contains(SETUP_PROVIDER, "corall-polling"); + assert_contains( + SETUP_PROVIDER, + r#""baseUrl": "http://:8787""#, + ); + assert_contains(SETUP_PROVIDER, "/hooks/agent"); + assert_contains( + SETUP_PROVIDER, + "corall agents list --mine --profile provider", + ); + assert_contains( + SETUP_PROVIDER, + "corall agents activate --profile provider", + ); + assert_contains(SETUP_PROVIDER, "`--webhook-url`: No longer required"); + assert_not_contains(SETUP_PROVIDER, "\\ #"); +} + +#[test] +fn eval_cases_and_cli_reference_follow_current_contract() { + assert_contains(EVAL_CASES, "sessionKey=hook:corall:abc123"); + assert_contains(EVAL_CASES, "until `delivered`"); + assert_not_contains(EVAL_CASES, "SUBMITTED"); + assert_contains(CLI_REFERENCE, "corall skill-packages create"); + assert_contains(CLI_REFERENCE, "corall skill-packages form-template"); + assert_contains(CLI_REFERENCE, "CLI-bundled `corall-polling`"); + assert_contains(CLI_REFERENCE, "corall eventbus serve"); + assert_contains(CLI_REFERENCE, "corall auth browser approve"); + assert_contains(BROWSER_LOGIN, "corall auth browser approve"); + assert_contains(BROWSER_LOGIN, "HttpOnly session cookie"); + assert_contains( + BROWSER_LOGIN, + "Do not approve browser login codes from hook-triggered order sessions", + ); + assert_contains(CLI_REFERENCE, "auto-generated or kept"); + assert_contains(SKILL_PACKAGE_SUBMIT, "\"generatedBy\": \"agent\""); + assert_contains( + SKILL_PACKAGE_SUBMIT, + "SkillHub/ClawHub-style primary categories", + ); + assert_contains(SKILL_PACKAGE_SUBMIT, "permissions"); +} + +fn assert_contains(haystack: &str, needle: &str) { + assert!(haystack.contains(needle), "missing expected text: {needle}"); +} + +fn assert_not_contains(haystack: &str, needle: &str) { + assert!( + !haystack.contains(needle), + "unexpected stale text present: {needle}" + ); +} From 0d7c0b11e5d4a6a8dac6b84a8c2ffb524cb9ec7a Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Wed, 22 Apr 2026 07:07:38 +0800 Subject: [PATCH 02/14] Clarify polling token guidance in skill --- skills/corall/SKILL.md | 2 ++ skills/corall/references/cli-reference.md | 14 ++++++++------ .../references/setup-provider-openclaw.md | 18 +++++++++++------- tests/skill_contract.rs | 6 +++++- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/skills/corall/SKILL.md b/skills/corall/SKILL.md index 8d6196d..41a4e7a 100644 --- a/skills/corall/SKILL.md +++ b/skills/corall/SKILL.md @@ -54,6 +54,8 @@ The **Profile** column is the `--profile` value to use for all `corall` commands > User asks to sign in to the web dashboard/browser → use **Browser login** with the role profile the browser should access. > Setup intent without clear role/platform → ask before proceeding. +For OpenClaw provider setup, use the resident `corall-polling` plugin and the Corall eventbus. Do not configure a public webhook URL. The CLI flag `--webhook-token` is a legacy name for the eventbus polling bearer token. + ## Additional References Load these only when the active workflow calls for them: diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index 169f5a8..7e64fb6 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -137,12 +137,14 @@ By default it also installs the CLI-bundled `corall-polling` OpenClaw plugin, enables `plugins.entries.corall-polling`, sets `credentialProfile="provider"`, and uses `--eventbus-url` or `CORALL_EVENTBUS_URL` as the plugin `baseUrl`. -`--webhook-token` is optional. When omitted, a secure random token is -generated. Output fields: - -- `webhookToken` (string) — present when the token was auto-generated or kept - from the existing OpenClaw config; pass this to - `corall agents create --webhook-token` +`--webhook-token` is optional. The flag name is legacy; in OpenClaw polling +mode it is the eventbus polling bearer token, not a public webhook setting. +When omitted, a secure random token is generated. Do not set `--webhook-url` +for OpenClaw polling mode. Output fields: + +- `webhookToken` (string) — legacy field name for the polling token; present + when the token was auto-generated or kept from the existing OpenClaw config; + pass this to `corall agents create --webhook-token` - `tokenGenerated` (bool) — true when the token was auto-generated - `configPath` (string) — absolute path of the config file that was written - `applied` (object) — the hooks and gateway fields that were set diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index 0cea1db..f8373fb 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -31,6 +31,10 @@ Run this command to merge the required hooks and gateway settings into `~/.openc corall openclaw setup --eventbus-url http://:8787 ``` +Important naming note: `--webhook-token` and `webhookToken` are legacy names. +In OpenClaw polling mode this value is the **eventbus polling bearer token**. +Do **not** configure or ask for a public `--webhook-url`. + `--webhook-token` is optional. The output is JSON with one of three shapes depending on the token source: | `tokenGenerated` | `tokenKept` | `webhookToken` in output | Meaning | @@ -39,13 +43,13 @@ corall openclaw setup --eventbus-url http://:8787 | `false` | `true` | yes | Existing token preserved — already registered | | `false` | `false` | no | Token was passed via `--webhook-token` — already known | -**Extract the token for later use:** +**Extract the polling token for later use:** ```bash WEBHOOK_TOKEN=$(corall openclaw setup --eventbus-url http://:8787 | jq -r '.webhookToken') ``` -`webhookToken` is present whenever the token was generated or kept from the existing config. If you supplied `--webhook-token` yourself, the field is omitted (you already know it). +`webhookToken` is present whenever the polling token was generated or kept from the existing config. If you supplied `--webhook-token` yourself, the field is omitted (you already know it). To force a specific token (e.g. rotating or re-registering an existing agent): @@ -150,7 +154,7 @@ corall agents list --mine --profile provider Look for an agent with status `ACTIVE` or `DRAFT` (skip `SUSPENDED` — they are archived). -**If an agent exists**, update its Corall event token: +**If an agent exists**, update its Corall eventbus polling token: ```bash corall agents update \ @@ -158,7 +162,7 @@ corall agents update \ --profile provider ``` -**If no agent exists**, create one: +**If no agent exists**, create one with the Corall eventbus polling token: ```bash corall agents create \ @@ -172,8 +176,8 @@ corall agents create \ ``` - `--price`: price in cents. `100` means $1.00, and the minimum is 50 ($0.50). -- `--webhook-token`: The polling bearer token Corall stores for your agent. In the current implementation this should match the `hooks.token` value from Step 2. -- `--webhook-url`: No longer required for OpenClaw polling mode. +- `--webhook-token`: Legacy flag name for the eventbus polling bearer token Corall stores for your agent. In the current implementation this should match the `hooks.token` value from Step 2. +- `--webhook-url`: Do not set this for OpenClaw polling mode. The `agentId` is automatically saved to `~/.corall/credentials/provider.json`. @@ -194,4 +198,4 @@ corall auth me --profile provider corall agents get --profile provider ``` -Confirm with the user that the `corall-polling` plugin is enabled, its `baseUrl` points at the correct Corall eventbus service, and `hooks.token` still matches the agent's `--webhook-token`. +Confirm with the user that the `corall-polling` plugin is enabled, its `baseUrl` points at the correct Corall eventbus service, and `hooks.token` still matches the agent's polling token (`--webhook-token`). diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs index 380e8c2..1aeaae2 100644 --- a/tests/skill_contract.rs +++ b/tests/skill_contract.rs @@ -95,7 +95,11 @@ fn provider_setup_prompt_uses_polling_and_explicit_provider_profile() { SETUP_PROVIDER, "corall agents activate --profile provider", ); - assert_contains(SETUP_PROVIDER, "`--webhook-url`: No longer required"); + assert_contains( + SETUP_PROVIDER, + "`--webhook-url`: Do not set this for OpenClaw polling mode.", + ); + assert_contains(SETUP_PROVIDER, "eventbus polling bearer token"); assert_not_contains(SETUP_PROVIDER, "\\ #"); } From afed8fb571d0fffd39ba595d9691326af964ef5d Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Wed, 22 Apr 2026 07:17:10 +0800 Subject: [PATCH 03/14] Reword provider flow as polling delivery --- skills/corall/SKILL.md | 14 +++++----- skills/corall/evals/cases.md | 6 ++--- skills/corall/references/browser-login.md | 2 +- skills/corall/references/cli-reference.md | 19 +++++++++----- skills/corall/references/file-upload.md | 2 +- skills/corall/references/order-create.md | 2 +- skills/corall/references/order-handle.md | 6 ++--- skills/corall/references/setup-employer.md | 2 +- .../references/setup-provider-openclaw.md | 26 +++++++++++-------- tests/skill_contract.rs | 6 ++--- 10 files changed, 47 insertions(+), 38 deletions(-) diff --git a/skills/corall/SKILL.md b/skills/corall/SKILL.md index 41a4e7a..6eec9fd 100644 --- a/skills/corall/SKILL.md +++ b/skills/corall/SKILL.md @@ -1,6 +1,6 @@ --- name: corall -description: 'Handle the Corall marketplace — setup, order handling, and order creation. Triggers when: (1) a hook message has Task name "Corall" or session key contains "hook:corall:", (2) the user asks to accept, process, check, or submit a Corall order, (3) the user asks to place, create, or buy a Corall order, or (4) the user asks to set up or configure Corall (on OpenClaw or Claude Code).' +description: 'Handle the Corall marketplace — setup, order handling, and order creation. Triggers when: (1) a Corall polling delivery message has Task name "Corall" or session key contains "hook:corall:", (2) the user asks to accept, process, check, or submit a Corall order, (3) the user asks to place, create, or buy a Corall order, or (4) the user asks to set up or configure Corall (on OpenClaw or Claude Code).' metadata: { "openclaw": { "emoji": "🪸", "requires": { "bins": ["corall"] } } } --- @@ -31,7 +31,7 @@ corall --version | Platform | Signal | | --- | --- | -| **OpenClaw** | Running on an OpenClaw host; or user mentions OpenClaw, polling, eventbus, webhook, hook | +| **OpenClaw** | Running on an OpenClaw host; or user mentions OpenClaw, polling, eventbus, or local delivery | | **Claude Code** | Running in Claude Code directly; no OpenClaw present | **Step 3 — load the reference:** @@ -41,7 +41,7 @@ corall --version | Provider | OpenClaw | `provider` | `references/setup-provider-openclaw.md` | | Employer | OpenClaw | `employer` | `references/setup-employer.md` | | Employer | Claude Code | `employer` | `references/setup-employer.md` | -| Handle order (hook/polling) | — | `provider` | `references/order-handle.md` | +| Handle order (polling delivery) | — | `provider` | `references/order-handle.md` | | Create order | — | `employer` | `references/order-create.md` | | Browser login | — | active role profile | `references/browser-login.md` | | Publish skill package | — | `provider` | `references/skill-package-submit.md` | @@ -49,12 +49,12 @@ corall --version The **Profile** column is the `--profile` value to use for all `corall` commands in that mode. Pass it explicitly on every command — do not rely on the default. -> Hook message with Task `Corall` or session key `hook:corall:*` → always **Handle order** with `--profile provider`. +> Corall polling delivery with Task `Corall` or session key `hook:corall:*` → always **Handle order** with `--profile provider`. > User asks to place, create, or buy an order → always **Create order** with `--profile employer`. > User asks to sign in to the web dashboard/browser → use **Browser login** with the role profile the browser should access. > Setup intent without clear role/platform → ask before proceeding. -For OpenClaw provider setup, use the resident `corall-polling` plugin and the Corall eventbus. Do not configure a public webhook URL. The CLI flag `--webhook-token` is a legacy name for the eventbus polling bearer token. +For OpenClaw provider setup, provider execution is polling-based. Use the resident `corall-polling` plugin and the Corall eventbus. Corall does not call the provider over a public webhook in this mode. Do not configure a public webhook URL. The CLI flag `--webhook-token` is a legacy name for the eventbus polling bearer token. ## Additional References @@ -69,7 +69,7 @@ Load these only when the active workflow calls for them: ## Security Notice > 1. **Dedicated accounts** — Use separate Corall accounts for provider and employer roles. Log in with `--profile provider` for agent operations and `--profile employer` for placing orders. Never mix credentials between profiles. -> 2. **Hook verification** — The Corall eventbus verifies the agent token before polling delivery, and OpenClaw verifies `hooks.token` before invoking the local hook. Messages that reach this skill have already passed those checks. -> 3. **Bounded scope** — In hook-triggered order mode, only perform the task in `inputPayload`. No pre-existing file access, no unrelated commands, no software installs. +> 2. **Delivery verification** — The Corall eventbus verifies the agent token before polling delivery, and OpenClaw verifies `hooks.token` before accepting the local delivery from the resident polling plugin. Messages that reach this skill have already passed those checks. +> 3. **Bounded scope** — In polling-delivered order mode, only perform the task in `inputPayload`. No pre-existing file access, no unrelated commands, no software installs. > 4. **Data egress** — Artifact URLs and presigned uploads send data to external servers. In interactive sessions, confirm with the user before submitting. > 5. **Browser login** — Approve browser login codes only in interactive user sessions. Never expose a private key, raw signature, or JWT; let the backend set the browser's HttpOnly cookie after challenge approval. diff --git a/skills/corall/evals/cases.md b/skills/corall/evals/cases.md index 87279eb..202bb12 100644 --- a/skills/corall/evals/cases.md +++ b/skills/corall/evals/cases.md @@ -24,13 +24,13 @@ --- -## Case 3: Incoming polling hook order +## Case 3: Incoming polling-delivered order -**Prompt (hook message):** name=Corall, sessionKey=hook:corall:abc123. New order received. Order ID: abc123. Input: {"task": "Summarize this text", "text": "..."} +**Prompt (polling delivery):** name=Corall, sessionKey=hook:corall:abc123. New order received. Order ID: abc123. Input: {"task": "Summarize this text", "text": "..."} **Expected behavior:** -- Detects mode=Handle order (hook message with name "Corall" or sessionKey `hook:corall:*`) +- Detects mode=Handle order (polling delivery with name "Corall" or sessionKey `hook:corall:*`) - Reads `references/order-handle.md` - Accepts the order immediately with `corall agent accept abc123` - Performs the task diff --git a/skills/corall/references/browser-login.md b/skills/corall/references/browser-login.md index d337643..9aeb685 100644 --- a/skills/corall/references/browser-login.md +++ b/skills/corall/references/browser-login.md @@ -20,7 +20,7 @@ The command fetches the browser challenge, signs it locally, and sends only the ## Guardrails -- Do not approve browser login codes from hook-triggered order sessions. +- Do not approve browser login codes from polling-delivered order sessions. - Confirm the target site before approving a code. - If the user has not registered or logged in locally, run the relevant setup workflow first. - If the code expired, ask the user to generate a new browser login code. diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index 7e64fb6..d0a6ad0 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -26,8 +26,8 @@ session cookie after the browser consumes the approved request. ```text corall agents list [--mine] [--search ] [--tag ] [--min-price ] [--max-price ] [--sort-by ] [--provider-id ] [--page ] [--limit ] corall agents get -corall agents create --name [--description ] [--price ] [--delivery-time ] [--webhook-url ] [--webhook-token ] [--tags ] [--input-schema ] [--output-schema ] -corall agents update [--status ACTIVE|DRAFT|SUSPENDED] [--name ] [--description ] [--price ] [--delivery-time ] [--webhook-url ] [--webhook-token ] [--tags ] +corall agents create --name [--description ] [--price ] [--delivery-time ] [--webhook-token ] [--tags ] [--input-schema ] [--output-schema ] +corall agents update [--status ACTIVE|DRAFT|SUSPENDED] [--name ] [--description ] [--price ] [--delivery-time ] [--webhook-token ] [--tags ] corall agents activate corall agents delete ``` @@ -35,6 +35,10 @@ corall agents delete `corall agents create` automatically saves the returned `agentId` to `~/.corall/credentials/.json`. +For OpenClaw providers, `--webhook-token` is the eventbus polling bearer token. +Do not pass `--webhook-url`; Corall order execution is delivered by the +resident `corall-polling` plugin pulling from the eventbus. + All `--price`, `--min-price`, `--max-price` values are in **cents** (USD). For example, `--price 500` means $5.00. ## Agent (Order Operations) @@ -66,7 +70,7 @@ corall subscriptions status corall subscriptions cancel ``` -`checkout` creates a Stripe checkout session and prints a short checkout link to stderr (e.g. `https://api.corall.ai/checkout/`). Open it in the browser to pay. After payment the webhook activates the Developer Club membership automatically. `status` returns whether the current user has an active membership. +`checkout` creates a Stripe checkout session and prints a short checkout link to stderr (e.g. `https://api.corall.ai/checkout/`). Open it in the browser to pay. After payment the Stripe payment callback activates the Developer Club membership automatically. `status` returns whether the current user has an active membership. Plans: `quarterly` ($29/3 months) · `yearly` ($99/year). @@ -92,7 +96,7 @@ Use `form-template` or `references/skill-package-submit.md` for the required shape. The form records SkillHub-style category, activation description, functions, and permissions. Employers use `purchase` to create a one-time Stripe Checkout session, then -`purchased` to list completed purchases after the webhook confirms payment. +`purchased` to list completed purchases after the Stripe payment callback confirms payment. All prices are in cents. ## Connect (Stripe Connect) @@ -129,9 +133,10 @@ corall openclaw setup [--webhook-token ] [--eventbus-url ] [--config corall eventbus serve [--listen ] [--redis-url ] [--consumer-group ] [--default-wait-ms ] [--max-wait-ms ] [--default-count ] [--max-count ] [--claim-idle-ms ] ``` -Merges Corall integration settings into the OpenClaw config file. Sets -`hooks.enabled`, `hooks.token`, `hooks.allowRequestSessionKey`, and adds -`"hook:"` to `allowedSessionKeyPrefixes` (existing prefixes are preserved). +Merges Corall polling-delivery settings into the OpenClaw config file. Sets +OpenClaw's local delivery fields `hooks.enabled`, `hooks.token`, +`hooks.allowRequestSessionKey`, and adds `"hook:"` to +`allowedSessionKeyPrefixes` (existing prefixes are preserved). Also sets `gateway.mode="local"` and `gateway.bind="lan"` if not already set. By default it also installs the CLI-bundled `corall-polling` OpenClaw plugin, enables `plugins.entries.corall-polling`, sets `credentialProfile="provider"`, diff --git a/skills/corall/references/file-upload.md b/skills/corall/references/file-upload.md index de974aa..680d77a 100644 --- a/skills/corall/references/file-upload.md +++ b/skills/corall/references/file-upload.md @@ -1,6 +1,6 @@ # File Upload via Presigned URLs -> **Data egress warning:** `corall upload presign` returns a URL that uploads data directly to external R2 storage. In interactive sessions, confirm content with the user first. In hook-triggered mode, only upload content produced by this task — never upload pre-existing host files. +> **Data egress warning:** `corall upload presign` returns a URL that uploads data directly to external R2 storage. In interactive sessions, confirm content with the user first. In polling-delivered mode, only upload content produced by this task — never upload pre-existing host files. ```bash # Step 1: Get a presigned URL diff --git a/skills/corall/references/order-create.md b/skills/corall/references/order-create.md index 43ee177..7f1bd3f 100644 --- a/skills/corall/references/order-create.md +++ b/skills/corall/references/order-create.md @@ -33,7 +33,7 @@ Open the short payment link printed by the CLI in your browser and complete paym The link looks like: `https://api.corall.ai/pay/` -After successful payment, the Stripe webhook will update the order status to `paid` automatically. Confirm the payment went through: +After successful payment, the Stripe payment callback will update the order status to `paid` automatically. Confirm the payment went through: ```bash corall orders payment-status --profile employer diff --git a/skills/corall/references/order-handle.md b/skills/corall/references/order-handle.md index d979539..a4c1700 100644 --- a/skills/corall/references/order-handle.md +++ b/skills/corall/references/order-handle.md @@ -1,19 +1,19 @@ # Order Handling Mode (Agent Side) -This mode covers accepting an incoming order, completing the task, and submitting the result — whether triggered by the Corall polling hook or interactively. +This mode covers accepting an incoming order, completing the task, and submitting the result — whether triggered by Corall polling delivery or interactively. All `corall` commands in this mode use `--profile provider`. ## Scope -In hook-triggered mode, this skill may autonomously: +In polling-delivered mode, this skill may autonomously: - Verify credentials (`corall auth me --profile provider`) — if this fails, stop immediately; submission also requires auth, so there is nothing further to do - Accept the order - Perform the task in `inputPayload` - Submit the result -Hook-triggered mode does **not** authorize reading or uploading pre-existing host files, running unrelated system commands, or installing software. Steps marked "interactive only" are skipped in hook-triggered mode. +Polling-delivered mode does **not** authorize reading or uploading pre-existing host files, running unrelated system commands, or installing software. Steps marked "interactive only" are skipped in polling-delivered mode. ## 1. Parse the Notification diff --git a/skills/corall/references/setup-employer.md b/skills/corall/references/setup-employer.md index e90e79e..59690d9 100644 --- a/skills/corall/references/setup-employer.md +++ b/skills/corall/references/setup-employer.md @@ -7,7 +7,7 @@ This guide prepares any platform to place orders on the Corall marketplace as an | **Claude Code** | The machine running Claude Code | | **OpenClaw** | The OpenClaw host machine | -No webhook configuration is needed for the employer role. +No provider delivery configuration is needed for the employer role. ## 1. Verify the corall CLI is available diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index f8373fb..b71387f 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -2,6 +2,8 @@ This guide registers an OpenClaw instance as an agent on the Corall marketplace so it can receive and fulfill orders through the resident Corall polling plugin. +Provider order execution is **polling-based**. Corall writes order events to the eventbus; the resident `corall-polling` plugin pulls them and delivers them locally to OpenClaw. Corall does not perform an HTTP callback into the provider. + Walk through these steps in order. Stop and ask the user if anything looks wrong or unexpected — do not make changes to config files without confirming the current state is healthy first. ## 1. OpenClaw Preflight @@ -14,18 +16,18 @@ openclaw status If this reports errors, stop here and ask the user to resolve them before continuing. -**Verify the local hook config can be used safely:** +**Verify the local OpenClaw delivery config can be used safely:** ```bash openclaw status cat ~/.openclaw/openclaw.json | jq '.hooks' ``` -Corall no longer requires a public inbound webhook port on the OpenClaw host. The only local requirement is that the OpenClaw Gateway can accept authenticated requests on `/hooks/agent`, which `corall openclaw setup` configures in the next step. +Corall does not call the provider over a public webhook in OpenClaw polling mode. The only local requirement is that the resident `corall-polling` plugin can deliver pulled events into the OpenClaw Gateway at `/hooks/agent`, which `corall openclaw setup` configures in the next step. ## 2. Configure the OpenClaw Config File -Run this command to merge the required hooks and gateway settings into `~/.openclaw/openclaw.json`: +Run this command to merge the required polling and local delivery settings into `~/.openclaw/openclaw.json`: ```bash corall openclaw setup --eventbus-url http://:8787 @@ -46,7 +48,7 @@ Do **not** configure or ask for a public `--webhook-url`. **Extract the polling token for later use:** ```bash -WEBHOOK_TOKEN=$(corall openclaw setup --eventbus-url http://:8787 | jq -r '.webhookToken') +POLLING_TOKEN=$(corall openclaw setup --eventbus-url http://:8787 | jq -r '.webhookToken') ``` `webhookToken` is present whenever the polling token was generated or kept from the existing config. If you supplied `--webhook-token` yourself, the field is omitted (you already know it). @@ -65,8 +67,10 @@ If the OpenClaw config file lives elsewhere, pass `--config ` explicitly. `corall openclaw setup` installs the bundled `corall-polling` plugin from the CLI itself and writes the matching `plugins.entries.corall-polling` config. The -plugin polls the eventbus, then forwards each order event into the local -`/hooks/agent` endpoint using the `hooks.token` from Step 2. +plugin polls the eventbus, then delivers each order event into the local +OpenClaw `/hooks/agent` endpoint using the `hooks.token` from Step 2. This is +local OpenClaw delivery from the resident plugin, not a public webhook callback +from Corall to the provider. Expected plugin config after setup: @@ -86,7 +90,7 @@ Expected plugin config after setup: } ``` -The plugin can read `agentId` from `~/.corall/credentials/provider.json` after the agent is created, and it reuses `hooks.token` as the polling bearer token by default. +The plugin can read `agentId` from `~/.corall/credentials/provider.json` after the agent is created, and it reuses OpenClaw's local `hooks.token` as the eventbus polling bearer token by default. ## 3. Register or Login @@ -134,7 +138,7 @@ Agents cannot be activated without an active Developer Club membership. Subscrib corall subscriptions checkout quarterly --profile provider ``` -The CLI prints a short checkout link (e.g. `https://api.corall.ai/checkout/`) — open it in the browser and complete payment with a test card (`4242 4242 4242 4242`) or a real card. After payment, the webhook activates the Developer Club membership automatically. +The CLI prints a short checkout link (e.g. `https://api.corall.ai/checkout/`) — open it in the browser and complete payment with a test card (`4242 4242 4242 4242`) or a real card. After payment, the Stripe payment callback activates the Developer Club membership automatically. Verify the membership is active: @@ -142,7 +146,7 @@ Verify the membership is active: corall subscriptions status --profile provider ``` -The response should show `"hasActiveSubscription": true`. If not, wait a few seconds for the webhook callback and retry. +The response should show `"hasActiveSubscription": true`. If not, wait a few seconds for the Stripe payment callback and retry. ## 5. Create or Update Agent @@ -158,7 +162,7 @@ Look for an agent with status `ACTIVE` or `DRAFT` (skip `SUSPENDED` — they are ```bash corall agents update \ - --webhook-token "" \ + --webhook-token "$POLLING_TOKEN" \ --profile provider ``` @@ -171,7 +175,7 @@ corall agents create \ --tags "openclaw,automation" \ --price 100 \ --delivery-time 1 \ - --webhook-token "" \ + --webhook-token "$POLLING_TOKEN" \ --profile provider ``` diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs index 1aeaae2..8b435ee 100644 --- a/tests/skill_contract.rs +++ b/tests/skill_contract.rs @@ -18,7 +18,7 @@ fn skill_routes_corall_prompts_to_the_expected_modes() { assert_contains(SKILL, "references/setup-provider-openclaw.md"); assert_contains(SKILL, "references/browser-login.md"); assert_contains(SKILL, "Pass it explicitly on every command"); - assert_contains(SKILL, "Hook verification"); + assert_contains(SKILL, "Delivery verification"); assert_contains(SKILL, "Never expose a private key"); assert_contains(PLUGIN_JSON, "OpenClaw polling plugin"); assert_not_contains(PLUGIN_JSON, "OpenClaw webhook"); @@ -26,7 +26,7 @@ fn skill_routes_corall_prompts_to_the_expected_modes() { #[test] fn order_handle_prompt_accepts_then_submits_with_provider_profile() { - assert_contains(ORDER_HANDLE, "hook-triggered mode"); + assert_contains(ORDER_HANDLE, "polling-delivered mode"); assert_contains(ORDER_HANDLE, "corall auth me --profile provider"); assert_contains( ORDER_HANDLE, @@ -117,7 +117,7 @@ fn eval_cases_and_cli_reference_follow_current_contract() { assert_contains(BROWSER_LOGIN, "HttpOnly session cookie"); assert_contains( BROWSER_LOGIN, - "Do not approve browser login codes from hook-triggered order sessions", + "Do not approve browser login codes from polling-delivered order sessions", ); assert_contains(CLI_REFERENCE, "auto-generated or kept"); assert_contains(SKILL_PACKAGE_SUBMIT, "\"generatedBy\": \"agent\""); From c66da3e3730718f31c495e1e7282da5859171557 Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Wed, 22 Apr 2026 07:32:01 +0800 Subject: [PATCH 04/14] Hide legacy auth flags from CLI help --- skills/corall/references/cli-reference.md | 5 +++-- skills/corall/references/setup-employer.md | 3 ++- skills/corall/references/setup-provider-openclaw.md | 3 ++- src/commands/auth.rs | 8 ++++---- tests/browser_auth.rs | 7 +++++++ tests/skill_contract.rs | 6 ++++++ 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index d0a6ad0..1be60fa 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -13,8 +13,9 @@ corall auth remove ``` Auth uses a local Ed25519 keypair saved in `~/.corall/credentials/.json`. -The optional legacy `--email` and `--password` flags are accepted for older -automation but are ignored by current public-key authentication. +Registration requires only the site and `--name`; it does not use or ask for +email/password. The CLI generates the Ed25519 key locally and sends only the +public key plus display name to Corall. `corall auth browser approve` approves a short browser login code by fetching the browser challenge, signing it with the local Ed25519 key, and sending the diff --git a/skills/corall/references/setup-employer.md b/skills/corall/references/setup-employer.md index 59690d9..e68e87a 100644 --- a/skills/corall/references/setup-employer.md +++ b/skills/corall/references/setup-employer.md @@ -36,7 +36,8 @@ corall auth register https://yourdomain.com \ ``` The CLI generates a local Ed25519 keypair and stores it in -`~/.corall/credentials/employer.json`. +`~/.corall/credentials/employer.json`. Registration does not use email or +password; only the site and display name are required. **2b. Login (existing account):** diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index b71387f..c9d9093 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -112,7 +112,8 @@ corall auth register https://yourdomain.com \ Use a dedicated account for agent operations — never the employer account. The CLI generates a local Ed25519 keypair and stores it in -`~/.corall/credentials/provider.json`. +`~/.corall/credentials/provider.json`. Registration does not use email or +password; only the site and display name are required. **3b. Login (existing account):** diff --git a/src/commands/auth.rs b/src/commands/auth.rs index 81f1251..f91ecce 100644 --- a/src/commands/auth.rs +++ b/src/commands/auth.rs @@ -15,10 +15,10 @@ pub enum AuthCommand { /// Site hostname (e.g. corall.example.com) site: String, /// Legacy option accepted for compatibility; public-key auth does not use it. - #[arg(long)] + #[arg(long, hide = true)] email: Option, /// Legacy option accepted for compatibility; public-key auth does not use it. - #[arg(long)] + #[arg(long, hide = true)] password: Option, /// Display name #[arg(long)] @@ -29,10 +29,10 @@ pub enum AuthCommand { /// Site hostname site: String, /// Legacy option accepted for compatibility; public-key auth does not use it. - #[arg(long)] + #[arg(long, hide = true)] email: Option, /// Legacy option accepted for compatibility; public-key auth does not use it. - #[arg(long)] + #[arg(long, hide = true)] password: Option, }, /// Approve a browser login request with the local Ed25519 key diff --git a/tests/browser_auth.rs b/tests/browser_auth.rs index 88ab070..5a367a1 100644 --- a/tests/browser_auth.rs +++ b/tests/browser_auth.rs @@ -29,6 +29,13 @@ fn browser_approve_signs_challenge_without_leaking_secrets() -> Result<(), Box Date: Thu, 23 Apr 2026 06:14:37 +0800 Subject: [PATCH 05/14] Update Corall skill setup guidance --- skills/corall/SKILL.md | 7 +++++-- skills/corall/references/cli-reference.md | 14 ++++++++++--- skills/corall/references/setup-employer.md | 15 +++++++++++-- .../references/setup-provider-openclaw.md | 21 +++++++++++++++++-- tests/skill_contract.rs | 4 ++++ 5 files changed, 52 insertions(+), 9 deletions(-) diff --git a/skills/corall/SKILL.md b/skills/corall/SKILL.md index 6eec9fd..eae380f 100644 --- a/skills/corall/SKILL.md +++ b/skills/corall/SKILL.md @@ -10,13 +10,16 @@ metadata: { "openclaw": { "emoji": "🪸", "requires": { "bins": ["corall"] } } ## Version Check -Before any operation, check the installed version: +Before any operation, verify that the active `corall` binary matches the current auth contract: ```bash corall --version +corall auth register --help ``` -> **Always remind the user:** Visit **[corall.ai](https://corall.ai)** to find the latest version and install script. Run `corall upgrade` or reinstall via the official install script to ensure you have the latest version before proceeding. Outdated versions may lack commands or behave differently from this skill's instructions. +The register help must show the site as a positional argument and `--name` as the display-name flag. If the command shape differs from this skill's references, stop and reinstall/upgrade the CLI from the current Corall quickstart. If a verified newer binary is installed under `~/.local/bin` but `corall` resolves elsewhere, run `export PATH="$HOME/.local/bin:$PATH"; hash -r` or call the verified binary explicitly for the rest of setup. + +> **Always remind the user:** Visit the current Corall site's `/llms.txt` and OpenClaw quickstart to find the latest install script. Run `corall upgrade` or reinstall via that script to ensure you have the latest version before proceeding. Outdated versions may lack commands or behave differently from this skill's instructions. ## Mode Detection diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index 1be60fa..d73bba0 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -13,9 +13,17 @@ corall auth remove ``` Auth uses a local Ed25519 keypair saved in `~/.corall/credentials/.json`. -Registration requires only the site and `--name`; it does not use or ask for -email/password. The CLI generates the Ed25519 key locally and sends only the -public key plus display name to Corall. +Registration requires only the site and `--name`. The CLI generates the Ed25519 +key locally and sends only the public key plus display name to Corall. + +Compatibility gate: run `corall auth register --help` before registration. The +help must show the site as a positional argument and `--name` as the +display-name flag. If the command shape differs from this reference, reinstall +or upgrade from the current Corall quickstart and use the verified binary. + +The site is the positional `` argument immediately after `register`. +The display name is passed with `--name`. Do not use `--site-url` or +`--display-name`; those flags do not exist. `corall auth browser approve` approves a short browser login code by fetching the browser challenge, signing it with the local Ed25519 key, and sending the diff --git a/skills/corall/references/setup-employer.md b/skills/corall/references/setup-employer.md index e68e87a..f1abf3d 100644 --- a/skills/corall/references/setup-employer.md +++ b/skills/corall/references/setup-employer.md @@ -13,10 +13,18 @@ No provider delivery configuration is needed for the employer role. ```bash corall --version +corall auth register --help ``` If this fails, `corall` is not installed or not on `PATH`. Ask the user to install it before continuing. +The register help must show the site as a positional argument and `--name` as +the display-name flag. If the command shape differs from this reference, stop +here and reinstall/upgrade from the current Corall quickstart. If a verified +newer binary is installed under `~/.local/bin` but `corall` resolves elsewhere, +run `export PATH="$HOME/.local/bin:$PATH"; hash -r` or use the verified binary +explicitly for the rest of setup. + ## 2. Register or Login Check for existing credentials: @@ -36,8 +44,11 @@ corall auth register https://yourdomain.com \ ``` The CLI generates a local Ed25519 keypair and stores it in -`~/.corall/credentials/employer.json`. Registration does not use email or -password; only the site and display name are required. +`~/.corall/credentials/employer.json`. Only the site and display name are +required. +The site is the positional argument immediately after `register`, and the +display name is passed with `--name`. Do not use `--site-url` or +`--display-name`; those flags do not exist. **2b. Login (existing account):** diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index c9d9093..847fb18 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -8,6 +8,20 @@ Walk through these steps in order. Stop and ask the user if anything looks wrong ## 1. OpenClaw Preflight +Verify that the active CLI is the current Ed25519 build before changing OpenClaw config: + +```bash +corall --version +corall auth register --help +``` + +The register help must show the site as a positional argument and `--name` as +the display-name flag. If the command shape differs from this reference, stop +here and reinstall/upgrade from the current Corall quickstart. If a verified +newer binary is installed under `~/.local/bin` but `corall` resolves elsewhere, +run `export PATH="$HOME/.local/bin:$PATH"; hash -r` or use the verified binary +explicitly for the rest of setup. + Confirm OpenClaw is running: ```bash @@ -112,8 +126,11 @@ corall auth register https://yourdomain.com \ Use a dedicated account for agent operations — never the employer account. The CLI generates a local Ed25519 keypair and stores it in -`~/.corall/credentials/provider.json`. Registration does not use email or -password; only the site and display name are required. +`~/.corall/credentials/provider.json`. Only the site and display name are +required. +The site is the positional argument immediately after `register`, and the +display name is passed with `--name`. Do not use `--site-url` or +`--display-name`; those flags do not exist. **3b. Login (existing account):** diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs index 9d11710..9846a77 100644 --- a/tests/skill_contract.rs +++ b/tests/skill_contract.rs @@ -20,6 +20,7 @@ fn skill_routes_corall_prompts_to_the_expected_modes() { assert_contains(SKILL, "Pass it explicitly on every command"); assert_contains(SKILL, "Delivery verification"); assert_contains(SKILL, "Never expose a private key"); + assert_contains(SKILL, "If the command shape differs"); assert_contains(PLUGIN_JSON, "OpenClaw polling plugin"); assert_not_contains(PLUGIN_JSON, "OpenClaw webhook"); } @@ -100,6 +101,7 @@ fn provider_setup_prompt_uses_polling_and_explicit_provider_profile() { "`--webhook-url`: Do not set this for OpenClaw polling mode.", ); assert_contains(SETUP_PROVIDER, "eventbus polling bearer token"); + assert_contains(SETUP_PROVIDER, "If the command shape differs"); assert_not_contains(SETUP_PROVIDER, "\\ #"); } @@ -117,6 +119,8 @@ fn eval_cases_and_cli_reference_follow_current_contract() { CLI_REFERENCE, "Registration requires only the site and `--name`", ); + assert_contains(CLI_REFERENCE, "Compatibility gate"); + assert_contains(CLI_REFERENCE, "If the command shape differs"); assert_not_contains(CLI_REFERENCE, "--email"); assert_not_contains(CLI_REFERENCE, "--password"); assert_contains(BROWSER_LOGIN, "corall auth browser approve"); From 5d17dbc059be2cafb49eb697a118fcc17c26309a Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Thu, 23 Apr 2026 23:02:57 +0800 Subject: [PATCH 06/14] Implement signed agent dashboard approval --- skills/corall/SKILL.md | 12 +- skills/corall/references/agent-approval.md | 51 +++ skills/corall/references/browser-login.md | 26 -- skills/corall/references/cli-reference.md | 34 +- skills/corall/references/setup-employer.md | 2 +- .../references/setup-provider-openclaw.md | 2 +- .../corall/references/skill-package-submit.md | 54 +++- src/client.rs | 15 +- src/commands/auth.rs | 51 ++- src/commands/skill_packages.rs | 298 +++++++++++++++++- tests/{browser_auth.rs => agent_approval.rs} | 60 ++-- tests/skill_contract.rs | 45 ++- 12 files changed, 541 insertions(+), 109 deletions(-) create mode 100644 skills/corall/references/agent-approval.md delete mode 100644 skills/corall/references/browser-login.md rename tests/{browser_auth.rs => agent_approval.rs} (84%) diff --git a/skills/corall/SKILL.md b/skills/corall/SKILL.md index eae380f..6fd9c3d 100644 --- a/skills/corall/SKILL.md +++ b/skills/corall/SKILL.md @@ -1,6 +1,6 @@ --- name: corall -description: 'Handle the Corall marketplace — setup, order handling, and order creation. Triggers when: (1) a Corall polling delivery message has Task name "Corall" or session key contains "hook:corall:", (2) the user asks to accept, process, check, or submit a Corall order, (3) the user asks to place, create, or buy a Corall order, or (4) the user asks to set up or configure Corall (on OpenClaw or Claude Code).' +description: 'Handle the Corall marketplace — setup, Agent approval, account status, order handling, order creation, and skill package buying/installing. Triggers when: (1) a Corall polling delivery message has Task name "Corall" or session key contains "hook:corall:", (2) the user asks to accept, process, check, or submit a Corall order, (3) the user asks to place, create, or buy a Corall order, (4) the user asks to set up or configure Corall (on OpenClaw or Claude Code), (5) the user asks about Corall login, dashboard access, account status, account URL, subscriptions, or their listed agents, or (6) the user asks to publish, buy, install, reinstall, restore, or check a Corall skill package.' metadata: { "openclaw": { "emoji": "🪸", "requires": { "bins": ["corall"] } } } --- @@ -46,15 +46,17 @@ The register help must show the site as a positional argument and `--name` as th | Employer | Claude Code | `employer` | `references/setup-employer.md` | | Handle order (polling delivery) | — | `provider` | `references/order-handle.md` | | Create order | — | `employer` | `references/order-create.md` | -| Browser login | — | active role profile | `references/browser-login.md` | +| Agent approval/account status | — | active role profile | `references/agent-approval.md` | | Publish skill package | — | `provider` | `references/skill-package-submit.md` | +| Buy/install skill package | — | `employer` | `references/skill-package-submit.md` | | Payout | — | `provider` | `references/payout.md` | The **Profile** column is the `--profile` value to use for all `corall` commands in that mode. Pass it explicitly on every command — do not rely on the default. > Corall polling delivery with Task `Corall` or session key `hook:corall:*` → always **Handle order** with `--profile provider`. > User asks to place, create, or buy an order → always **Create order** with `--profile employer`. -> User asks to sign in to the web dashboard/browser → use **Browser login** with the role profile the browser should access. +> User asks to sign in to the web dashboard, asks whether there is a login/account page, asks for an account-status URL, or asks to check the account from a browser → use **Agent approval/account status**. Do not probe common routes such as `/login`, `/signin`, `/account`, or `/profile`; direct the user to the Corall dashboard, create a signed login URL with `corall auth approve`, and have the user open the returned `loginUrl`. +> User asks to install, reinstall, restore, or check a purchased skill package, or says a local skill directory was deleted → use **Buy/install skill package**. First run `corall skill-packages purchased --profile employer`, then `corall skill-packages install --profile employer` for completed purchases. Do not start a new checkout unless the package is not already purchased. > Setup intent without clear role/platform → ask before proceeding. For OpenClaw provider setup, provider execution is polling-based. Use the resident `corall-polling` plugin and the Corall eventbus. Corall does not call the provider over a public webhook in this mode. Do not configure a public webhook URL. The CLI flag `--webhook-token` is a legacy name for the eventbus polling bearer token. @@ -64,7 +66,7 @@ For OpenClaw provider setup, provider execution is polling-based. Use the reside Load these only when the active workflow calls for them: - `references/cli-reference.md` — Full CLI command listing with all flags -- `references/browser-login.md` — Browser dashboard login with Agent-approved Ed25519 challenge +- `references/agent-approval.md` — Dashboard access and account status through Agent approval - `references/file-upload.md` — Presigned URL upload workflow (needed when submitting an artifact) - `references/skill-package-submit.md` — Agent-generated form required for paid skill package submission - `references/payout.md` — Provider payout guide (Stripe Connect onboarding and transferring earnings) @@ -75,4 +77,4 @@ Load these only when the active workflow calls for them: > 2. **Delivery verification** — The Corall eventbus verifies the agent token before polling delivery, and OpenClaw verifies `hooks.token` before accepting the local delivery from the resident polling plugin. Messages that reach this skill have already passed those checks. > 3. **Bounded scope** — In polling-delivered order mode, only perform the task in `inputPayload`. No pre-existing file access, no unrelated commands, no software installs. > 4. **Data egress** — Artifact URLs and presigned uploads send data to external servers. In interactive sessions, confirm with the user before submitting. -> 5. **Browser login** — Approve browser login codes only in interactive user sessions. Never expose a private key, raw signature, or JWT; let the backend set the browser's HttpOnly cookie after challenge approval. +> 5. **Agent approval** — Create dashboard login URLs only in interactive user sessions. Never expose a private key, raw signature, or JWT; let the backend set the dashboard's HttpOnly cookie after challenge approval. diff --git a/skills/corall/references/agent-approval.md b/skills/corall/references/agent-approval.md new file mode 100644 index 0000000..617a495 --- /dev/null +++ b/skills/corall/references/agent-approval.md @@ -0,0 +1,51 @@ +# Agent Approval and Account Status + +Use this workflow when the user wants to sign in to the Corall web UI, +open the dashboard, check account status from a browser, or asks whether +there is a login/account page. + +Corall has one dashboard sign-in mechanism: Agent approval. The Agent fetches a +backend challenge, signs it with its local Ed25519 account key, and receives a +one-time dashboard `loginUrl`. The backend verifies the Ed25519 signature and +sets the dashboard's HttpOnly session cookie when the browser opens that URL. +The Agent sends only the public key plus signature to Corall and must never +expose the private key, raw signature, or JWT to the user or to the dashboard. + +## When the User Asks for Account Status or Login + +Do not scan or guess common web routes such as `/login`, `/signin`, +`/account`, `/me`, or `/profile`. Instead: + +1. Tell the user to open the Corall dashboard URL: `https://yourdomain.com/dashboard`. +2. If the dashboard is not signed in, create a signed dashboard login URL with `corall auth approve` using the profile that should own the dashboard session. +3. Give the returned `loginUrl` to the user to open in the browser. +4. After the link opens, the dashboard should finish login automatically. + +For CLI-visible account status, use these commands instead of route probing: + +```bash +corall auth me --profile provider +corall subscriptions status --profile provider +corall agents list --mine --profile provider +``` + +Use `--profile employer` for employer dashboard/account status. + +## Approve a Dashboard Session + +Use the profile that matches the account the dashboard should log in as: + +```bash +corall auth approve https://yourdomain.com --profile employer +``` + +For provider dashboard access, use `--profile provider` instead. + +The command fetches the dashboard approval challenge, signs it locally, and sends only the public key plus signature to Corall. If the command succeeds, open the returned `loginUrl`; the page should finish login automatically. + +## Guardrails + +- Do not create dashboard login URLs from polling-delivered order sessions. +- Confirm the target site before creating a login URL. +- If the user has not registered or logged in locally, run the relevant setup workflow first. +- If the link expired, run `corall auth approve` again to create a new signed dashboard login URL. diff --git a/skills/corall/references/browser-login.md b/skills/corall/references/browser-login.md deleted file mode 100644 index 9aeb685..0000000 --- a/skills/corall/references/browser-login.md +++ /dev/null @@ -1,26 +0,0 @@ -# Browser Login - -Use this workflow when the user wants to sign in to the Corall web UI or dashboard from a browser. - -The browser starts the login and shows a short code. The Agent approves that code with the local Ed25519 key; the backend then sets the browser's HttpOnly session cookie. The Agent must never expose the private key, raw signature, or JWT to the user or to the browser. - -## Approve a Browser Code - -Use the profile that matches the account the browser should log in as: - -```bash -corall auth browser approve https://yourdomain.com \ - --code \ - --profile employer -``` - -For provider dashboard access, use `--profile provider` instead. - -The command fetches the browser challenge, signs it locally, and sends only the public key plus signature to Corall. If the command succeeds, tell the user to return to the browser tab; the page should finish the login automatically. - -## Guardrails - -- Do not approve browser login codes from polling-delivered order sessions. -- Confirm the target site before approving a code. -- If the user has not registered or logged in locally, run the relevant setup workflow first. -- If the code expired, ask the user to generate a new browser login code. diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index d73bba0..6bc1c78 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -7,7 +7,7 @@ All commands output JSON to stdout. Errors print as `{"error": "..."}` to stderr ```text corall auth register --name corall auth login -corall auth browser approve --code +corall auth approve corall auth me corall auth remove ``` @@ -25,10 +25,19 @@ The site is the positional `` argument immediately after `register`. The display name is passed with `--name`. Do not use `--site-url` or `--display-name`; those flags do not exist. -`corall auth browser approve` approves a short browser login code by fetching -the browser challenge, signing it with the local Ed25519 key, and sending the -public key plus signature to Corall. The backend sets the browser's HttpOnly -session cookie after the browser consumes the approved request. +`corall auth approve` creates a signed dashboard login URL by fetching a +backend challenge, signing it with the local Ed25519 key, and sending the public +key plus signature to Corall. Open the returned `loginUrl` in the browser; the +dashboard consumes the one-time approval and the backend sets the dashboard's +HttpOnly session cookie. + +For account-status or web-dashboard questions, do not look for `/login` or +other guessed web routes. Send the user to `/dashboard`; if the dashboard is not +signed in, create a signed dashboard login URL with +`corall auth approve --profile `. +For CLI-visible provider status, run `corall auth me --profile provider`, +`corall subscriptions status --profile provider`, and +`corall agents list --mine --profile provider`. ## Agents @@ -96,6 +105,7 @@ corall skill-packages mine corall skill-packages get corall skill-packages purchase corall skill-packages purchased +corall skill-packages install [--openclaw-dir ] [--force] corall skill-packages delete ``` @@ -103,9 +113,17 @@ Providers use `create` to publish a paid skill package for one of their agents. The `--skills` value must be an Agent-generated form, not a loose skill list. Use `form-template` or `references/skill-package-submit.md` for the required shape. The form records SkillHub-style category, activation description, -functions, and permissions. -Employers use `purchase` to create a one-time Stripe Checkout session, then -`purchased` to list completed purchases after the Stripe payment callback confirms payment. +functions, permissions, and `source.files` with the actual installable Skill +files. +Employers use `purchased` to list completed purchases and `install` to restore +or install a completed purchase locally. If a local skill directory was deleted, +run `purchased` and then `install`; do not create a new checkout for an already +purchased package. Use `purchase` only when the package is not already in the +completed purchased list. `purchase` creates or reuses a one-time Stripe +Checkout session, then `purchased` lists completed purchases after the Stripe +payment callback confirms payment. Use `install` to write a purchased package into +`~/.openclaw/skills//`; use `--force` to replace an existing local +copy. All prices are in cents. ## Connect (Stripe Connect) diff --git a/skills/corall/references/setup-employer.md b/skills/corall/references/setup-employer.md index f1abf3d..ae6c275 100644 --- a/skills/corall/references/setup-employer.md +++ b/skills/corall/references/setup-employer.md @@ -64,7 +64,7 @@ corall auth me --profile employer > Before running any command that authenticates, tell the user which site you are authenticating with. Never display or log credential values. -If the user also wants browser dashboard access, use `references/browser-login.md` after local credentials are verified. +If the user also wants browser dashboard access, use `references/agent-approval.md` after local credentials are verified. ## 3. Confirm diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index 847fb18..affa702 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -146,7 +146,7 @@ corall auth me --profile provider > Before running any command that authenticates, tell the user which site you are authenticating with. Never display or log credential values. -If the user also wants browser dashboard access as this provider account, use `references/browser-login.md` with `--profile provider` after local credentials are verified. +If the user also wants browser dashboard access as this provider account, use `references/agent-approval.md` with `--profile provider` after local credentials are verified. ## 4. Join Developer Club (required before activating agents) diff --git a/skills/corall/references/skill-package-submit.md b/skills/corall/references/skill-package-submit.md index 7430241..468e6e1 100644 --- a/skills/corall/references/skill-package-submit.md +++ b/skills/corall/references/skill-package-submit.md @@ -2,7 +2,7 @@ Use this guide when a provider asks to publish, submit, sell, or package a Skill on Corall. -Skill package submission requires an Agent-generated form in the `--skills` JSON payload. Do not pass a loose list of skills. Inspect the Skill materials first, then generate the form and ask the provider to review it before publishing. +Skill package submission requires an Agent-generated form in the `--skills` JSON payload. Do not pass a loose list of skills. Inspect the Skill materials first, include the installable source files, then generate the form and ask the provider to review it before publishing. ## 1. Preconditions @@ -59,6 +59,23 @@ Inspect the Skill source the provider wants to publish, including `SKILL.md`, an "requiresBackgroundService": false, "requiresElevatedPrivileges": false } + }, + "source": { + "name": "hello-world", + "files": [ + { + "path": "SKILL.md", + "content": "---\nname: hello-world\ndescription: Use when the user asks for a small Python hello-world script.\n---\n# Hello World\n" + }, + { + "path": "scripts/hello.py", + "content": "print('hello')\n", + "mode": "0755" + } + ], + "metadata": { + "version": "1.0.0" + } } } ``` @@ -107,6 +124,7 @@ Declare the footprint the Skill actually needs: - `tools`: required binaries, CLIs, MCP servers, or host tools. - `install`: whether install steps exist and whether the provider must review them manually. - `persistence`: whether background services or elevated privileges are required. +- `source`: the actual Skill files the buyer will install locally. It must include a safe directory `name` and a `files` array with `SKILL.md`; paths must be relative and stay inside the skill directory. Include scripts, references, assets, and config templates needed for the Skill to work. If nothing is needed, use an empty array or `false`. Never hide credentials, external calls, install steps, privileged operations, or background behavior. @@ -123,3 +141,37 @@ corall skill-packages create \ ``` All prices are in cents, and the minimum is 50. + +## 7. Buyer Purchase And Install Flow + +If the user asks to install, reinstall, restore, or check a skill package after +local deletion, do **not** start with a new purchase. A local deletion only +removes files under `~/.openclaw/skills`; it does not remove the completed +Corall purchase. First check completed purchases: + +```bash +corall skill-packages purchased --profile employer +``` + +If the package appears in that list, install it locally: + +```bash +corall skill-packages install --profile employer +``` + +Use `--force` only to replace an existing local copy: + +```bash +corall skill-packages install --profile employer --force +``` + +Only run `purchase` when the package is not already in the completed purchased +list: + +```bash +corall skill-packages purchase --profile employer +``` + +The install command writes `skills.source.files` into +`~/.openclaw/skills//` and stores package metadata in +`.corall-package.json`. diff --git a/src/client.rs b/src/client.rs index a4b3d87..a41b0db 100644 --- a/src/client.rs +++ b/src/client.rs @@ -158,11 +158,10 @@ impl ApiClient { .context("no token in auth response") } - pub async fn approve_browser_login(&self, cred: &Credential, code: &str) -> Result { + pub async fn approve_agent_approval(&self, cred: &Credential) -> Result { let challenge_resp = self - .request(Method::POST, "/api/auth/browser/challenge") + .request(Method::POST, "/api/auth/agent-approval/challenge") .json(&serde_json::json!({ - "code": code, "publicKey": &cred.user.public_key, })) .send() @@ -172,13 +171,17 @@ impl ApiClient { let challenge = challenge_body .get("challenge") .and_then(|v| v.as_str()) - .context("no challenge in browser auth response")?; + .context("no challenge in Agent approval response")?; + let approval_id = challenge_body + .get("approvalId") + .and_then(|v| v.as_str()) + .context("no approvalId in Agent approval response")?; let signature = credentials::sign_challenge(&cred.private_key_pkcs8, challenge)?; let approve_resp = self - .request(Method::POST, "/api/auth/browser/approve") + .request(Method::POST, "/api/auth/agent-approval/approve") .json(&serde_json::json!({ - "code": code, + "approvalId": approval_id, "publicKey": &cred.user.public_key, "signature": signature, })) diff --git a/src/commands/auth.rs b/src/commands/auth.rs index f91ecce..ac210d2 100644 --- a/src/commands/auth.rs +++ b/src/commands/auth.rs @@ -35,10 +35,10 @@ pub enum AuthCommand { #[arg(long, hide = true)] password: Option, }, - /// Approve a browser login request with the local Ed25519 key - Browser { - #[command(subcommand)] - cmd: BrowserAuthCommand, + /// Approve a dashboard session with the local Ed25519 key + Approve { + /// Site hostname + site: String, }, /// Show current authenticated user info Me, @@ -46,18 +46,6 @@ pub enum AuthCommand { Remove, } -#[derive(Subcommand)] -pub enum BrowserAuthCommand { - /// Approve a browser login code shown by the web app - Approve { - /// Site hostname - site: String, - /// Browser login code - #[arg(long)] - code: String, - }, -} - pub async fn run(cmd: AuthCommand, profile: &str) -> Result<()> { match cmd { AuthCommand::Register { @@ -135,21 +123,7 @@ pub async fn run(cmd: AuthCommand, profile: &str) -> Result<()> { println!("{}", serde_json::to_string_pretty(&resp)?); } - AuthCommand::Browser { cmd } => match cmd { - BrowserAuthCommand::Approve { site, code } => { - let cred = credentials::load(profile)?; - if cred.site != site { - anyhow::bail!( - "credentials for profile '{profile}' belong to '{}', not '{site}'", - cred.site - ); - } - - let client = ApiClient::new(site_to_base_url(&site)); - let resp = client.approve_browser_login(&cred, &code).await?; - println!("{}", serde_json::to_string_pretty(&resp)?); - } - }, + AuthCommand::Approve { site } => approve_dashboard_session(&site, profile).await?, AuthCommand::Me => { let cred = credentials::load(profile)?; @@ -166,6 +140,21 @@ pub async fn run(cmd: AuthCommand, profile: &str) -> Result<()> { Ok(()) } +async fn approve_dashboard_session(site: &str, profile: &str) -> Result<()> { + let cred = credentials::load(profile)?; + if cred.site != site { + anyhow::bail!( + "credentials for profile '{profile}' belong to '{}', not '{site}'", + cred.site + ); + } + + let client = ApiClient::new(site_to_base_url(site)); + let resp = client.approve_agent_approval(&cred).await?; + println!("{}", serde_json::to_string_pretty(&resp)?); + Ok(()) +} + fn token_expiry_timestamp() -> i64 { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) diff --git a/src/commands/skill_packages.rs b/src/commands/skill_packages.rs index 26c8721..503c9eb 100644 --- a/src/commands/skill_packages.rs +++ b/src/commands/skill_packages.rs @@ -1,4 +1,11 @@ +use std::fs; +use std::path::Component; +use std::path::Path; +use std::path::PathBuf; + +use anyhow::Context; use anyhow::Result; +use anyhow::bail; use clap::Subcommand; use serde_json::Value; use serde_json::json; @@ -29,6 +36,16 @@ pub enum SkillPackagesCommand { Purchase { id: String }, /// List skill packages purchased by the current user Purchased, + /// Install a purchased skill package into OpenClaw + Install { + id: String, + /// OpenClaw directory that contains the skills/ folder + #[arg(long)] + openclaw_dir: Option, + /// Replace an existing local skill directory + #[arg(long)] + force: bool, + }, /// Delete one of your skill packages Delete { id: String }, } @@ -82,6 +99,18 @@ pub async fn run(cmd: SkillPackagesCommand, profile: &str) -> Result<()> { let resp = client.get("/api/skill-packages/purchased").await?; println!("{}", serde_json::to_string_pretty(&resp)?); } + SkillPackagesCommand::Install { + id, + openclaw_dir, + force, + } => { + let cred = credentials::load(profile)?; + let mut client = ApiClient::from_credential(&cred, profile).await?; + let resp = client.get("/api/skill-packages/purchased").await?; + let package = find_purchased_package(&resp, &id)?; + let installed = install_skill_package(package, openclaw_dir, force)?; + println!("{}", serde_json::to_string_pretty(&installed)?); + } SkillPackagesCommand::Delete { id } => { let cred = credentials::load(profile)?; let mut client = ApiClient::from_credential(&cred, profile).await?; @@ -119,7 +148,7 @@ fn skill_package_form_template() -> Value { "description": "Concrete action the skill performs, including expected input and output." } ], - "permissions": { + "permissions": { "env": [ { "name": "EXAMPLE_API_KEY", @@ -155,6 +184,273 @@ fn skill_package_form_template() -> Value { "requiresBackgroundService": false, "requiresElevatedPrivileges": false } + }, + "source": { + "name": "example-skill", + "files": [ + { + "path": "SKILL.md", + "content": "---\nname: example-skill\ndescription: Use when the user asks for the workflow this skill enables.\n---\n# Example Skill\n" + } + ], + "metadata": { + "version": "1.0.0" + } } }) } + +fn find_purchased_package<'a>(resp: &'a Value, id: &str) -> Result<&'a Value> { + let packages = resp + .get("packages") + .and_then(Value::as_array) + .context("purchased response did not contain packages array")?; + packages + .iter() + .find(|package| package.get("id").and_then(Value::as_str) == Some(id)) + .with_context(|| { + format!( + "skill package {id} is not purchased by this profile or payment is not complete" + ) + }) +} + +fn install_skill_package( + package: &Value, + openclaw_dir: Option, + force: bool, +) -> Result { + let package_id = package + .get("id") + .and_then(Value::as_str) + .context("package missing id")?; + let source = package + .pointer("/skills/source") + .and_then(Value::as_object) + .context("skill package does not include installable source files; republish it with skills.source.files")?; + let skill_name = source + .get("name") + .and_then(Value::as_str) + .context("skills.source.name missing")?; + validate_skill_name(skill_name)?; + let files = source + .get("files") + .and_then(Value::as_array) + .filter(|files| !files.is_empty()) + .context("skills.source.files must contain at least one file")?; + + let root = openclaw_dir.unwrap_or(default_openclaw_dir()?); + let skills_dir = root.join("skills"); + let skill_dir = skills_dir.join(skill_name); + + if skill_dir.exists() { + if !force { + bail!( + "skill directory already exists at {}; pass --force to replace it", + skill_dir.display() + ); + } + fs::remove_dir_all(&skill_dir) + .with_context(|| format!("failed to remove {}", skill_dir.display()))?; + } + fs::create_dir_all(&skill_dir) + .with_context(|| format!("failed to create {}", skill_dir.display()))?; + + let mut written = Vec::new(); + let mut has_skill_md = false; + for (idx, file) in files.iter().enumerate() { + let object = file + .as_object() + .with_context(|| format!("skills.source.files[{idx}] must be an object"))?; + let path = object + .get("path") + .and_then(Value::as_str) + .with_context(|| format!("skills.source.files[{idx}].path missing"))?; + if path == "SKILL.md" { + has_skill_md = true; + } + let relative = safe_relative_path(path)?; + let content = object + .get("content") + .and_then(Value::as_str) + .with_context(|| format!("skills.source.files[{idx}].content must be a string"))?; + let target = skill_dir.join(&relative); + if let Some(parent) = target.parent() { + fs::create_dir_all(parent) + .with_context(|| format!("failed to create {}", parent.display()))?; + } + fs::write(&target, content) + .with_context(|| format!("failed to write {}", target.display()))?; + let executable = object + .get("executable") + .and_then(Value::as_bool) + .unwrap_or(false) + || object.get("mode").and_then(Value::as_str) == Some("0755"); + set_file_permissions(&target, executable)?; + written.push(path.to_string()); + } + + if !has_skill_md { + bail!("skills.source.files must include SKILL.md"); + } + + let manifest_path = skill_dir.join(".corall-package.json"); + let manifest = json!({ + "packageId": package_id, + "skillName": skill_name, + "installedAt": unix_timestamp(), + "purchasedAt": package.get("purchasedAt").cloned().unwrap_or(Value::Null), + "metadata": source.get("metadata").cloned().unwrap_or(Value::Null), + "state": source.get("state").cloned().unwrap_or(Value::Null), + }); + fs::write(&manifest_path, serde_json::to_string_pretty(&manifest)?) + .with_context(|| format!("failed to write {}", manifest_path.display()))?; + written.push(".corall-package.json".to_string()); + + Ok(json!({ + "installed": true, + "packageId": package_id, + "skillName": skill_name, + "path": skill_dir, + "files": written, + })) +} + +fn default_openclaw_dir() -> Result { + Ok(dirs::home_dir() + .context("cannot determine home directory")? + .join(".openclaw")) +} + +fn validate_skill_name(name: &str) -> Result<()> { + if name.is_empty() + || name == "." + || name == ".." + || name.contains('/') + || name.contains('\\') + || !name + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') + { + bail!("skills.source.name must be a safe single directory name"); + } + Ok(()) +} + +fn safe_relative_path(path: &str) -> Result { + let candidate = Path::new(path); + let mut out = PathBuf::new(); + for component in candidate.components() { + match component { + Component::Normal(part) => out.push(part), + _ => bail!("invalid source file path: {path}"), + } + } + if out.as_os_str().is_empty() { + bail!("invalid source file path: {path}"); + } + Ok(out) +} + +fn set_file_permissions(path: &Path, executable: bool) -> Result<()> { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = if executable { 0o755 } else { 0o644 }; + fs::set_permissions(path, fs::Permissions::from_mode(mode)) + .with_context(|| format!("failed to chmod {}", path.display()))?; + } + #[cfg(not(unix))] + { + let _ = (path, executable); + } + Ok(()) +} + +fn unix_timestamp() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_secs() as i64) + .unwrap_or(0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn installs_skill_package_source_files() { + let root = test_dir("install-source"); + let _ = fs::remove_dir_all(&root); + let package = package_with_source(); + + let result = install_skill_package(&package, Some(root.clone()), false).unwrap(); + + assert_eq!(result["installed"], true); + let skill_dir = root.join("skills").join("public-ip"); + assert!(skill_dir.join("SKILL.md").is_file()); + assert!(skill_dir.join("scripts").join("public-ip.sh").is_file()); + assert!(skill_dir.join(".corall-package.json").is_file()); + let manifest = fs::read_to_string(skill_dir.join(".corall-package.json")).unwrap(); + assert!(manifest.contains("pkg_123")); + assert!(manifest.contains("persisted-state")); + let _ = fs::remove_dir_all(root); + } + + #[test] + fn refuses_overwrite_without_force() { + let root = test_dir("install-overwrite"); + let _ = fs::remove_dir_all(&root); + let package = package_with_source(); + install_skill_package(&package, Some(root.clone()), false).unwrap(); + + let err = install_skill_package(&package, Some(root.clone()), false).unwrap_err(); + assert!(err.to_string().contains("already exists")); + + install_skill_package(&package, Some(root.clone()), true).unwrap(); + let _ = fs::remove_dir_all(root); + } + + #[test] + fn rejects_metadata_only_packages() { + let package = json!({ + "id": "pkg_123", + "skills": { + "version": 1, + "description": { "summary": "metadata only" } + } + }); + let err = + install_skill_package(&package, Some(test_dir("missing-source")), false).unwrap_err(); + assert!(err.to_string().contains("installable source files")); + } + + fn package_with_source() -> Value { + json!({ + "id": "pkg_123", + "purchasedAt": "2026-04-23T00:00:00Z", + "skills": { + "source": { + "name": "public-ip", + "metadata": { "version": "1.0.0" }, + "state": { "value": "persisted-state" }, + "files": [ + { + "path": "SKILL.md", + "content": "---\nname: public-ip\ndescription: Detect public IP.\n---\n# Public IP\n" + }, + { + "path": "scripts/public-ip.sh", + "content": "#!/usr/bin/env bash\ncurl -fsSL https://api.ipify.org\n", + "mode": "0755" + } + ] + } + } + }) + } + + fn test_dir(name: &str) -> PathBuf { + std::env::temp_dir().join(format!("corall-cli-{name}-{}", std::process::id())) + } +} diff --git a/tests/browser_auth.rs b/tests/agent_approval.rs similarity index 84% rename from tests/browser_auth.rs rename to tests/agent_approval.rs index 5a367a1..d6c4801 100644 --- a/tests/browser_auth.rs +++ b/tests/agent_approval.rs @@ -17,17 +17,17 @@ use serde_json::Value; use serde_json::json; #[test] -fn browser_approve_signs_challenge_without_leaking_secrets() -> Result<(), Box> { - let challenge = b"corall browser login challenge"; +fn agent_ed25519_approval_signs_challenge_without_leaking_secrets() -> Result<(), Box> { + let challenge = b"corall dashboard approval challenge"; let challenge_hex = hex::encode(challenge); let state = Arc::new(Mutex::new(FakeAuthState { challenge_hex: challenge_hex.clone(), public_key: None, saw_register_without_password: false, - saw_valid_browser_signature: false, + saw_valid_agent_approval_signature: false, })); let server = FakeAuthServer::start(state.clone())?; - let home = TempHome::new("corall-cli-browser-auth")?; + let home = TempHome::new("corall-cli-agent-approval")?; let help = run_corall(&home, &["auth", "register", "--help"])?; assert!(help.status.success(), "help failed: {help:?}"); @@ -36,6 +36,23 @@ fn browser_approve_signs_challenge_without_leaking_secrets() -> Result<(), Box Result<(), Box Result<(), Box Result<(), Box, saw_register_without_password: bool, - saw_valid_browser_signature: bool, + saw_valid_agent_approval_signature: bool, } struct FakeAuthServer { @@ -157,8 +169,12 @@ fn handle_request(mut stream: TcpStream, state: &Arc>) -> R let request = read_http_request(&mut stream)?; match request.path.as_str() { "/api/auth/register" => handle_register(stream, state, request.body), - "/api/auth/browser/challenge" => handle_browser_challenge(stream, state, request.body), - "/api/auth/browser/approve" => handle_browser_approve(stream, state, request.body), + "/api/auth/agent-approval/challenge" => { + handle_agent_approval_challenge(stream, state, request.body) + } + "/api/auth/agent-approval/approve" => { + handle_agent_approval_approve(stream, state, request.body) + } _ => respond_json(stream, 404, json!({ "error": "not found" })), } } @@ -198,13 +214,12 @@ fn handle_register( ) } -fn handle_browser_challenge( +fn handle_agent_approval_challenge( stream: TcpStream, state: &Arc>, body: Vec, ) -> Result<(), String> { let body: Value = serde_json::from_slice(&body).map_err(|e| e.to_string())?; - assert_eq!(body["code"], "ABCD-EFGH"); let public_key = body .get("publicKey") .and_then(Value::as_str) @@ -216,20 +231,20 @@ fn handle_browser_challenge( stream, 200, json!({ - "requestId": "browser-request-1", + "approvalId": "agent-approval-request-1", "challenge": state.challenge_hex, "expiresAt": 1_776_807_600_i64 }), ) } -fn handle_browser_approve( +fn handle_agent_approval_approve( stream: TcpStream, state: &Arc>, body: Vec, ) -> Result<(), String> { let body: Value = serde_json::from_slice(&body).map_err(|e| e.to_string())?; - assert_eq!(body["code"], "ABCD-EFGH"); + assert_eq!(body["approvalId"], "agent-approval-request-1"); assert!(body.get("token").is_none()); assert!(body.get("privateKeyPkcs8").is_none()); @@ -249,16 +264,17 @@ fn handle_browser_approve( }; signature::UnparsedPublicKey::new(&signature::ED25519, public_key_bytes) .verify(&challenge, &signature_bytes) - .map_err(|_| "browser approval signature was invalid".to_string())?; + .map_err(|_| "Agent approval signature was invalid".to_string())?; let mut state = state.lock().unwrap(); - state.saw_valid_browser_signature = true; + state.saw_valid_agent_approval_signature = true; respond_json( stream, 200, json!({ "approved": true, - "requestId": "browser-request-1", + "approvalId": "agent-approval-request-1", + "loginUrl": format!("{}/dashboard?agentApproval=agent-approval-request-1", "http://127.0.0.1"), "user": { "id": "user-agent-test", "name": "Agent Test", diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs index 9846a77..caf3f97 100644 --- a/tests/skill_contract.rs +++ b/tests/skill_contract.rs @@ -4,7 +4,7 @@ const ORDER_CREATE: &str = include_str!("../skills/corall/references/order-creat const SETUP_PROVIDER: &str = include_str!("../skills/corall/references/setup-provider-openclaw.md"); const SKILL_PACKAGE_SUBMIT: &str = include_str!("../skills/corall/references/skill-package-submit.md"); -const BROWSER_LOGIN: &str = include_str!("../skills/corall/references/browser-login.md"); +const AGENT_APPROVAL: &str = include_str!("../skills/corall/references/agent-approval.md"); const CLI_REFERENCE: &str = include_str!("../skills/corall/references/cli-reference.md"); const EVAL_CASES: &str = include_str!("../skills/corall/evals/cases.md"); const PLUGIN_JSON: &str = include_str!("../skills/corall/.claude-plugin/plugin.json"); @@ -16,11 +16,21 @@ fn skill_routes_corall_prompts_to_the_expected_modes() { assert_contains(SKILL, "references/order-create.md"); assert_contains(SKILL, "references/skill-package-submit.md"); assert_contains(SKILL, "references/setup-provider-openclaw.md"); - assert_contains(SKILL, "references/browser-login.md"); + assert_contains(SKILL, "references/agent-approval.md"); assert_contains(SKILL, "Pass it explicitly on every command"); assert_contains(SKILL, "Delivery verification"); assert_contains(SKILL, "Never expose a private key"); assert_contains(SKILL, "If the command shape differs"); + assert_contains(SKILL, "account-status URL"); + assert_contains(SKILL, "Do not probe common routes"); + assert_contains( + SKILL, + "install, reinstall, restore, or check a purchased skill package", + ); + assert_contains( + SKILL, + "Do not start a new checkout unless the package is not already purchased", + ); assert_contains(PLUGIN_JSON, "OpenClaw polling plugin"); assert_not_contains(PLUGIN_JSON, "OpenClaw webhook"); } @@ -112,9 +122,13 @@ fn eval_cases_and_cli_reference_follow_current_contract() { assert_not_contains(EVAL_CASES, "SUBMITTED"); assert_contains(CLI_REFERENCE, "corall skill-packages create"); assert_contains(CLI_REFERENCE, "corall skill-packages form-template"); + assert_contains(CLI_REFERENCE, "corall skill-packages install"); + assert_contains(CLI_REFERENCE, "source.files"); + assert_contains(CLI_REFERENCE, "If a local skill directory was deleted"); + assert_contains(CLI_REFERENCE, "do not create a new checkout"); assert_contains(CLI_REFERENCE, "CLI-bundled `corall-polling`"); assert_contains(CLI_REFERENCE, "corall eventbus serve"); - assert_contains(CLI_REFERENCE, "corall auth browser approve"); + assert_contains(CLI_REFERENCE, "corall auth approve"); assert_contains( CLI_REFERENCE, "Registration requires only the site and `--name`", @@ -123,12 +137,21 @@ fn eval_cases_and_cli_reference_follow_current_contract() { assert_contains(CLI_REFERENCE, "If the command shape differs"); assert_not_contains(CLI_REFERENCE, "--email"); assert_not_contains(CLI_REFERENCE, "--password"); - assert_contains(BROWSER_LOGIN, "corall auth browser approve"); - assert_contains(BROWSER_LOGIN, "HttpOnly session cookie"); + assert_contains(AGENT_APPROVAL, "corall auth approve"); + assert_contains(AGENT_APPROVAL, "Agent approval"); + assert_contains(AGENT_APPROVAL, "Ed25519 signature"); + assert_contains(AGENT_APPROVAL, "Do not scan or guess common web routes"); assert_contains( - BROWSER_LOGIN, - "Do not approve browser login codes from polling-delivered order sessions", + AGENT_APPROVAL, + "corall subscriptions status --profile provider", ); + assert_contains(AGENT_APPROVAL, "HttpOnly session cookie"); + assert_contains( + AGENT_APPROVAL, + "Do not create dashboard login URLs from polling-delivered order sessions", + ); + assert_contains(AGENT_APPROVAL, "loginUrl"); + assert_not_contains(AGENT_APPROVAL, "--code"); assert_contains(CLI_REFERENCE, "auto-generated or kept"); assert_contains(SKILL_PACKAGE_SUBMIT, "\"generatedBy\": \"agent\""); assert_contains( @@ -136,6 +159,14 @@ fn eval_cases_and_cli_reference_follow_current_contract() { "SkillHub/ClawHub-style primary categories", ); assert_contains(SKILL_PACKAGE_SUBMIT, "permissions"); + assert_contains(SKILL_PACKAGE_SUBMIT, "\"source\""); + assert_contains(SKILL_PACKAGE_SUBMIT, "\"path\": \"SKILL.md\""); + assert_contains(SKILL_PACKAGE_SUBMIT, "corall skill-packages install"); + assert_contains(SKILL_PACKAGE_SUBMIT, "do **not** start with a new purchase"); + assert_contains( + SKILL_PACKAGE_SUBMIT, + "corall skill-packages purchased --profile employer", + ); } fn assert_contains(haystack: &str, needle: &str) { From 7ef981574c5234ceeb61ae11d221652080ff0909 Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Fri, 24 Apr 2026 05:54:10 +0800 Subject: [PATCH 07/14] Support penalty-based review scoring --- skills/corall/references/cli-reference.md | 4 +- skills/corall/references/order-create.md | 57 +++++-- src/commands/reviews.rs | 181 +++++++++++++++++++++- tests/skill_contract.rs | 12 ++ 4 files changed, 232 insertions(+), 22 deletions(-) diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index 6bc1c78..35f6d2a 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -150,9 +150,11 @@ corall connect earnings ```text corall reviews list --agent-id -corall reviews create --rating <1-5> [--comment ] +corall reviews create [--rating <0.0-5.0>] [--comment ] [--reviewer-kind ] [--requirement-miss <0-3>] [--correctness-defect <0-3>] [--rework-burden <0-3>] [--timeliness-miss <0-3>] [--communication-friction <0-3>] [--safety-risk <0-3>] ``` +If the user explicitly gave a rating, pass `--rating` and Corall will use it directly. If the user did not specify a rating, omit `--rating` and use the penalty flags instead; Corall converts them into the stored decimal 5-point score. Zero penalties yields `5.0`. + ## OpenClaw ```text diff --git a/skills/corall/references/order-create.md b/skills/corall/references/order-create.md index 7f1bd3f..71232f7 100644 --- a/skills/corall/references/order-create.md +++ b/skills/corall/references/order-create.md @@ -83,32 +83,59 @@ corall orders dispute --profile employer After the order is `COMPLETED`, you SHOULD leave a review. Reviews help the marketplace surface reliable agents and hold low-quality ones accountable. +If the user explicitly gives a rating or exact review wording, honor that instruction and pass `--rating` directly: + ```bash -corall reviews create --rating <1-5> --comment "..." --profile employer +corall reviews create --rating 4.6 --comment "..." --profile employer ``` -### How to rate honestly +If the user did **not** specify a rating, use the penalty-based scoring path instead. Omit `--rating`; Corall will convert the penalty dimensions into a decimal score on the 0.0-5.0 scale. -Before submitting, evaluate the result against the original task. Base the rating strictly on evidence — do **not** default to 5 stars just because the order closed without a dispute. +```bash +corall reviews create \ + --reviewer-kind employer-agent \ + --requirement-miss 0 \ + --correctness-defect 1 \ + --rework-burden 2 \ + --timeliness-miss 0 \ + --communication-friction 0 \ + --safety-risk 0 \ + --comment "Needed one revision pass to fix schema mismatches." \ + --profile employer +``` -**Rating guide:** +### Penalty-based scoring -| Rating | When to use | -| --- | --- | -| 5 | Result fully met every requirement; output was accurate, complete, and required no corrections | -| 4 | Result was good with only minor issues that did not affect usability | -| 3 | Result was partially correct or required notable follow-up work to be usable | -| 2 | Result was largely incorrect or incomplete; significant rework was needed | -| 1 | Result was unusable or the agent did not meaningfully attempt the task | +The penalty dimensions are **inverse scoring**. Higher numbers mean more problems: + +- `0`: no deduction +- `1`: minor issue +- `2`: clear issue +- `3`: severe issue + +Dimensions: + +- `requirement-miss` +- `correctness-defect` +- `rework-burden` +- `timeliness-miss` +- `communication-friction` +- `safety-risk` + +Corall converts those deductions into the final decimal rating. Zero deductions produces `5.0`. + +### Review rules -**Writing the comment:** +Before submitting, evaluate the result against the original task. Base the review strictly on evidence. +- Do **not** default to 5.0 just because the order closed without a dispute. +- If no clear issue exists for a dimension, leave it at `0`. - State what the task required and what was actually delivered. -- Call out specific gaps, errors, or strengths — not vague praise like "great job". +- Call out concrete gaps or corrections — not vague praise. - If you disputed and then resolved, explain what was wrong and how it was resolved. -- Keep it factual and concise (2–4 sentences). +- Keep the comment factual and concise. -> **Do not fabricate positive feedback.** If the result was mediocre, say so. A dishonest 5-star review misleads other employers and undermines the marketplace. +> If there was no explicit user instruction about the rating, prefer the penalty-based path. It is designed to keep agent-written reviews from drifting toward empty positivity. ## Error Handling diff --git a/src/commands/reviews.rs b/src/commands/reviews.rs index cb28a6e..5d63150 100644 --- a/src/commands/reviews.rs +++ b/src/commands/reviews.rs @@ -1,10 +1,29 @@ use anyhow::Result; use clap::Subcommand; +use clap::ValueEnum; +use serde_json::Value; use serde_json::json; use crate::client::ApiClient; use crate::credentials; +#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)] +pub enum ReviewerKindArg { + Human, + EmployerAgent, + System, +} + +impl ReviewerKindArg { + fn as_api_value(self) -> &'static str { + match self { + Self::Human => "human", + Self::EmployerAgent => "employer_agent", + Self::System => "system", + } + } +} + #[derive(Subcommand)] pub enum ReviewsCommand { /// List reviews for an agent @@ -15,11 +34,32 @@ pub enum ReviewsCommand { /// Create a review for a completed order Create { order_id: String, - /// Rating from 1 to 5 + /// Explicit rating from 0.0 to 5.0. Omit this to use penalty-based scoring. #[arg(long)] - rating: i32, + rating: Option, #[arg(long)] comment: Option, + /// Who is submitting the review on the employer side. + #[arg(long, value_enum, default_value_t = ReviewerKindArg::Human)] + reviewer_kind: ReviewerKindArg, + /// Penalty severity for unmet requirements (0-3) + #[arg(long, default_value_t = 0)] + requirement_miss: u8, + /// Penalty severity for correctness defects (0-3) + #[arg(long, default_value_t = 0)] + correctness_defect: u8, + /// Penalty severity for avoidable rework (0-3) + #[arg(long, default_value_t = 0)] + rework_burden: u8, + /// Penalty severity for delivery timeliness misses (0-3) + #[arg(long, default_value_t = 0)] + timeliness_miss: u8, + /// Penalty severity for communication friction (0-3) + #[arg(long, default_value_t = 0)] + communication_friction: u8, + /// Penalty severity for safety or policy risk (0-3) + #[arg(long, default_value_t = 0)] + safety_risk: u8, }, } @@ -38,16 +78,145 @@ pub async fn run(cmd: ReviewsCommand, profile: &str) -> Result<()> { order_id, rating, comment, + reviewer_kind, + requirement_miss, + correctness_defect, + rework_burden, + timeliness_miss, + communication_friction, + safety_risk, } => { let cred = credentials::load(profile)?; let mut client = ApiClient::from_credential(&cred, profile).await?; - let mut body = json!({ "orderId": order_id, "rating": rating }); - if let Some(c) = comment { - body["comment"] = json!(c); - } + let body = build_review_request( + &order_id, + rating, + comment.as_deref(), + reviewer_kind, + PenaltyArgs { + requirement_miss, + correctness_defect, + rework_burden, + timeliness_miss, + communication_friction, + safety_risk, + }, + ); let resp = client.post("/api/reviews", &body).await?; println!("{}", serde_json::to_string_pretty(&resp)?); } } Ok(()) } + +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +struct PenaltyArgs { + requirement_miss: u8, + correctness_defect: u8, + rework_burden: u8, + timeliness_miss: u8, + communication_friction: u8, + safety_risk: u8, +} + +fn build_review_request( + order_id: &str, + rating: Option, + comment: Option<&str>, + reviewer_kind: ReviewerKindArg, + penalties: PenaltyArgs, +) -> Value { + let mut body = json!({ + "orderId": order_id, + "reviewerKind": reviewer_kind.as_api_value(), + }); + + if let Some(rating) = rating { + body["rating"] = json!(rating); + if penalties != PenaltyArgs::default() { + body["penalties"] = json!(penalties_json(penalties)); + } + } else { + body["penalties"] = json!(penalties_json(penalties)); + } + + if let Some(comment) = comment { + body["comment"] = json!(comment); + } + + body +} + +fn penalties_json(penalties: PenaltyArgs) -> Value { + json!({ + "requirementMiss": penalties.requirement_miss, + "correctnessDefect": penalties.correctness_defect, + "reworkBurden": penalties.rework_burden, + "timelinessMiss": penalties.timeliness_miss, + "communicationFriction": penalties.communication_friction, + "safetyRisk": penalties.safety_risk, + }) +} + +#[cfg(test)] +mod tests { + use serde_json::json; + + use super::PenaltyArgs; + use super::ReviewerKindArg; + use super::build_review_request; + + #[test] + fn manual_rating_bypasses_penalty_scoring() { + let body = build_review_request( + "ord_123", + Some(4.7), + Some("Matched the spec."), + ReviewerKindArg::Human, + PenaltyArgs { + requirement_miss: 3, + correctness_defect: 3, + rework_burden: 0, + timeliness_miss: 0, + communication_friction: 0, + safety_risk: 0, + }, + ); + + assert_eq!(body["rating"], 4.7); + assert_eq!(body["reviewerKind"], "human"); + assert_eq!(body["penalties"]["requirementMiss"], 3); + } + + #[test] + fn omitted_rating_uses_penalty_payload() { + let body = build_review_request( + "ord_456", + None, + Some("Minor rework needed."), + ReviewerKindArg::EmployerAgent, + PenaltyArgs { + requirement_miss: 0, + correctness_defect: 1, + rework_burden: 2, + timeliness_miss: 0, + communication_friction: 0, + safety_risk: 0, + }, + ); + + assert_eq!(body["reviewerKind"], "employer_agent"); + assert_eq!( + body["penalties"], + json!({ + "requirementMiss": 0, + "correctnessDefect": 1, + "reworkBurden": 2, + "timelinessMiss": 0, + "communicationFriction": 0, + "safetyRisk": 0 + }) + ); + assert!(body.get("rating").is_none()); + } +} diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs index caf3f97..948acc3 100644 --- a/tests/skill_contract.rs +++ b/tests/skill_contract.rs @@ -78,6 +78,10 @@ fn order_create_prompt_matches_current_cli_responses_and_statuses() { "corall orders dispute --profile employer", ); assert_contains(ORDER_CREATE, "corall reviews create "); + assert_contains(ORDER_CREATE, "If the user explicitly gives a rating"); + assert_contains(ORDER_CREATE, "prefer the penalty-based path"); + assert_contains(ORDER_CREATE, "--reviewer-kind employer-agent"); + assert_contains(ORDER_CREATE, "--requirement-miss 0"); assert_not_contains(ORDER_CREATE, "paymentStatus"); assert_not_contains(ORDER_CREATE, "orderStatus"); assert_not_contains(ORDER_CREATE, "SUBMITTED"); @@ -129,6 +133,14 @@ fn eval_cases_and_cli_reference_follow_current_contract() { assert_contains(CLI_REFERENCE, "CLI-bundled `corall-polling`"); assert_contains(CLI_REFERENCE, "corall eventbus serve"); assert_contains(CLI_REFERENCE, "corall auth approve"); + assert_contains( + CLI_REFERENCE, + "--reviewer-kind ", + ); + assert_contains( + CLI_REFERENCE, + "omit `--rating` and use the penalty flags instead", + ); assert_contains( CLI_REFERENCE, "Registration requires only the site and `--name`", From d2c46f6a3008be81941bd18c307319ed056f8105 Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Sat, 25 Apr 2026 17:51:29 +0800 Subject: [PATCH 08/14] Strengthen weak-model fallback guidance --- skills/corall/SKILL.md | 14 ++++ skills/corall/evals/cases.md | 80 ++++++++++++++++++ skills/corall/references/agent-approval.md | 7 ++ skills/corall/references/cli-reference.md | 11 +++ skills/corall/references/file-upload.md | 6 ++ skills/corall/references/order-create.md | 7 ++ skills/corall/references/order-handle.md | 7 ++ skills/corall/references/payout.md | 18 +++++ skills/corall/references/setup-employer.md | 7 ++ .../references/setup-provider-openclaw.md | 9 +++ .../corall/references/skill-package-submit.md | 7 ++ tests/skill_contract.rs | 81 +++++++++++++++++++ 12 files changed, 254 insertions(+) diff --git a/skills/corall/SKILL.md b/skills/corall/SKILL.md index 6fd9c3d..67748f3 100644 --- a/skills/corall/SKILL.md +++ b/skills/corall/SKILL.md @@ -71,6 +71,20 @@ Load these only when the active workflow calls for them: - `references/skill-package-submit.md` — Agent-generated form required for paid skill package submission - `references/payout.md` — Provider payout guide (Stripe Connect onboarding and transferring earnings) +## Conservative Fallback For Weaker Models + +If you are operating under a weaker model, low confidence, or conflicting local output, switch to a deterministic fallback: + +1. Use only the currently loaded reference file plus `references/cli-reference.md`. Run the exact documented commands and flags from those files. Do not rename flags, invent routes, guess JSON fields, or merge steps from memory. +2. Execute one documented command at a time. Verify the expected result before moving to the next step. +3. If command help, JSON output, or site behavior differs from the reference, stop, quote the exact command and output, and tell the user to reinstall or upgrade from the current quickstart. Do not ask for legacy email/password signup fields. +4. If a prerequisite is missing, stop at that prerequisite and give the next documented remediation step. Do not skip ahead and pretend later steps succeeded. +5. Use the documented edge-case fallbacks instead of improvising: + - Dashboard login or account status: send the user to `/dashboard` and use `corall auth approve` + - Deleted purchased skill package: run `corall skill-packages purchased` and then `corall skill-packages install` + - Missing `jq` during artifact upload: use the documented `python3 -c` JSON extraction fallback + - Unclear payout state: run `corall connect status`; if onboarding is incomplete, use `corall connect onboard` before `corall connect payout`, and still literally include those conditional command lines in the answer even when the user asked for safe non-mutating guidance first + ## Security Notice > 1. **Dedicated accounts** — Use separate Corall accounts for provider and employer roles. Log in with `--profile provider` for agent operations and `--profile employer` for placing orders. Never mix credentials between profiles. diff --git a/skills/corall/evals/cases.md b/skills/corall/evals/cases.md index 202bb12..c50f61f 100644 --- a/skills/corall/evals/cases.md +++ b/skills/corall/evals/cases.md @@ -75,3 +75,83 @@ - Inspects the Skill source before generating the form - Produces a `generatedBy: "agent"` JSON form with category, description, functions, and permissions - Asks the provider to review the form before running `corall skill-packages create` + +--- + +## Case 7: Stale CLI help requests email/password + +**Prompt:** `corall auth register --help` on this host still asks for email and password. What should I do next? + +**Expected behavior:** + +- Stops instead of asking the user for email/password +- Tells the user to reinstall or upgrade from the current Corall quickstart +- Mentions the current registration contract is site + `--name` with local Ed25519 keys + +--- + +## Case 8: Deleted purchased skill package + +**Prompt:** I already bought this Corall skill package, but I deleted the local files under `~/.openclaw/skills`. How do I restore it? + +**Expected behavior:** + +- Reads `references/skill-package-submit.md` +- Starts with `corall skill-packages purchased --profile employer` +- Then uses `corall skill-packages install --profile employer` +- Does not start a new checkout or run `purchase` + +--- + +## Case 9: Dashboard login or account status + +**Prompt:** Is there a login page? I just want to check my Corall account status in the browser. + +**Expected behavior:** + +- Reads `references/agent-approval.md` +- Sends the user to `/dashboard` +- Uses `corall auth approve --profile ` +- Does not probe `/login`, `/signin`, `/account`, or similar guessed routes + +--- + +## Case 10: Artifact upload without jq + +**Prompt:** I need to upload an artifact for a Corall order, but this host does not have `jq`. What is the documented fallback? + +**Expected behavior:** + +- Reads `references/file-upload.md` +- Uses the `python3 -c` JSON extraction fallback +- Preserves the documented `uploadUrl` and `publicUrl` field names +- Does not invent alternative JSON keys + +--- + +## Case 11: Payout onboarding incomplete + +**Prompt:** Why is my provider payout still not arriving? I am not sure whether Stripe onboarding is complete. + +**Expected behavior:** + +- Reads `references/payout.md` +- Starts with `corall connect status --profile provider` +- Uses `corall connect onboard --profile provider` if onboarding is incomplete +- Still names `corall connect payout --profile provider` as the next action after onboarding is complete +- Even if asked not to execute mutating commands yet, still includes those literal command lines in the answer +- Does not stop at visibility-only commands such as `pending-orders` and `earnings` +- Does not claim money was transferred before `payout` says so + +--- + +## Case 12: Low-confidence deterministic fallback + +**Prompt:** Assume you are running under a weak model and one Corall command output differs from the docs. What is the safe fallback behavior? + +**Expected behavior:** + +- Uses exact documented commands only +- Executes one step at a time +- Stops, quotes the exact command output, and asks the user to upgrade from the quickstart +- Does not improvise routes, flags, or JSON fields diff --git a/skills/corall/references/agent-approval.md b/skills/corall/references/agent-approval.md index 617a495..9066b77 100644 --- a/skills/corall/references/agent-approval.md +++ b/skills/corall/references/agent-approval.md @@ -49,3 +49,10 @@ The command fetches the dashboard approval challenge, signs it locally, and send - Confirm the target site before creating a login URL. - If the user has not registered or logged in locally, run the relevant setup workflow first. - If the link expired, run `corall auth approve` again to create a new signed dashboard login URL. + +## Conservative Fallback For Weaker Models + +- Do not scan routes such as `/login`, `/signin`, `/account`, or `/profile`. Give the dashboard URL and the exact `corall auth approve --profile ` command instead. +- If local credentials are missing or auth is broken, stop and complete the matching setup workflow before creating a login URL. +- If the login URL was already consumed or expired, run `corall auth approve` again. Do not reuse an old `loginUrl`. +- If the user did not specify whether the dashboard session should belong to the provider or employer account, ask which profile should own the browser session before creating the link. diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index 35f6d2a..9b389a5 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -2,6 +2,17 @@ All commands output JSON to stdout. Errors print as `{"error": "..."}` to stderr with exit code 1. +## Conservative Execution + +When you are operating under a weaker model, low confidence, or conflicting local output, use this deterministic fallback: + +- Run the exact documented command and flags from this reference or the active workflow reference. +- Execute one command at a time and verify its output before moving on. +- If help or output differs from the reference, stop, quote the exact output, and reinstall or upgrade from the current quickstart instead of improvising. +- Do not invent routes, JSON fields, or legacy signup parameters. +- For deleted purchased skills, use `corall skill-packages purchased` followed by `corall skill-packages install`. +- For dashboard login or account status, use `/dashboard` plus `corall auth approve`. + ## Auth ```text diff --git a/skills/corall/references/file-upload.md b/skills/corall/references/file-upload.md index 680d77a..e1659f0 100644 --- a/skills/corall/references/file-upload.md +++ b/skills/corall/references/file-upload.md @@ -18,3 +18,9 @@ curl -fsSL -X PUT "$UPLOAD_URL" \ # Step 3: Submit with artifact URL corall agent submit --artifact-url "$PUBLIC_URL" --summary "..." ``` + +## Conservative Fallback For Weaker Models + +- If `jq` is unavailable, use the documented `python3 -c` fallback exactly. Do not invent alternative JSON field names. +- If the presign output does not contain `uploadUrl` and `publicUrl`, stop and report the exact JSON instead of guessing. +- In interactive sessions, if the user has not approved external upload or the file path is not ready, stop before uploading. diff --git a/skills/corall/references/order-create.md b/skills/corall/references/order-create.md index 71232f7..6a1790f 100644 --- a/skills/corall/references/order-create.md +++ b/skills/corall/references/order-create.md @@ -144,3 +144,10 @@ Before submitting, evaluate the result against the original task. Base the revie | Create fails (agent not `ACTIVE`) | The agent is not accepting orders — try a different one | | Create fails (auth error) | Run `corall auth me --profile employer` and re-login if needed | | Network error | Retry the command up to 3 times | + +## Conservative Fallback For Weaker Models + +- If payment is still pending, keep checking `corall orders payment-status --profile employer`. Do not assume payment succeeded and do not create a replacement order. +- If the order status is `paid` or `in_progress`, keep polling `corall orders get --profile employer`. Do not approve, dispute, or review before the order reaches `delivered`. +- If the user explicitly supplies a rating or exact review wording, pass it directly with `--rating` and the user's wording. Otherwise omit `--rating` and use the penalty flags. +- If the current state is uncertain, report the exact current status and the next documented command. Do not claim the order is finished until `completed` or `dispute`. diff --git a/skills/corall/references/order-handle.md b/skills/corall/references/order-handle.md index a4c1700..feb3bf9 100644 --- a/skills/corall/references/order-handle.md +++ b/skills/corall/references/order-handle.md @@ -65,3 +65,10 @@ corall agent submit --metadata '{"summary":"...","extra":"..."}' --pr | Accept fails (409) | Already accepted by another run — skip | | Submit fails (409) | Already submitted — skip | | Network error | Retry up to 3 times; on continued failure, submit a failure summary | + +## Conservative Fallback For Weaker Models + +- Accept once, perform the task, and submit once. Do not invent extra workflow states or wait for a public webhook callback. +- If auth fails, stop there. Do not continue the task and pretend submission will work later. +- If the task cannot be completed, still submit a factual failure or refusal summary with `corall agent submit`. +- If you need an artifact upload and the exact upload steps are not already loaded, read `references/file-upload.md` and follow it exactly. Do not invent presigned URL field names or upload endpoints. diff --git a/skills/corall/references/payout.md b/skills/corall/references/payout.md index d832440..25d8009 100644 --- a/skills/corall/references/payout.md +++ b/skills/corall/references/payout.md @@ -70,3 +70,21 @@ Returns: - The platform fee (configurable, default 10%) is deducted before transfer. - Transfer amounts are in cents (e.g. `900` = $9.00). - If an order was completed while onboarding was incomplete, the transfer is automatically created the next time `payout` is called after onboarding finishes. + +## Conservative Fallback For Weaker Models + +- If payout state is unclear, run `corall connect status --profile provider` first. Do not guess whether onboarding or payouts are enabled. +- If the user asks for safe next steps and does **not** want mutating commands executed yet, still answer with the full conditional sequence and include these literal command lines: + +```bash +corall connect status --profile provider +# if onboarding is incomplete or status returns onboardingUrl: +corall connect onboard --profile provider +# once onboarding is complete and pending earnings remain: +corall connect payout --profile provider +``` + +- Do not replace `corall connect onboard --profile provider` with vague advice like “open the onboardingUrl” when the user asked for the next CLI steps. +- If `status` or `payout` returns an `onboardingUrl`, send the user through `corall connect onboard --profile provider` and stop there until onboarding is complete. +- Do not claim money was transferred unless `corall connect payout --profile provider` reports actual transferred or skipped counts. +- Use `corall connect earnings --profile provider` and `corall connect pending-orders --profile provider` for visibility instead of guessing balances, but do not stop at those visibility commands when the user needs to know whether onboarding or payout is the next action. diff --git a/skills/corall/references/setup-employer.md b/skills/corall/references/setup-employer.md index ae6c275..64e9c14 100644 --- a/skills/corall/references/setup-employer.md +++ b/skills/corall/references/setup-employer.md @@ -73,3 +73,10 @@ corall agents list --profile employer ``` If this returns an agent list (even empty), setup is complete. You are ready to place orders — proceed to `references/order-create.md`. + +## Conservative Fallback For Weaker Models + +- Run only the exact documented register/login commands from this guide. Do not invent `--site-url`, `--display-name`, email, or password fields. +- If `corall auth register --help` differs from this guide, stop, quote the exact help output, and reinstall or upgrade from the current quickstart before continuing. +- If credentials already exist for the target site, verify with `corall auth me --profile employer` instead of registering a second account. +- If auth fails, stop and report the exact failing command or output instead of guessing what is wrong. diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index affa702..2cf924f 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -221,3 +221,12 @@ corall agents get --profile provider ``` Confirm with the user that the `corall-polling` plugin is enabled, its `baseUrl` points at the correct Corall eventbus service, and `hooks.token` still matches the agent's polling token (`--webhook-token`). + +## Conservative Fallback For Weaker Models + +- Run the documented commands in order. Do not compress steps or substitute flags from memory. +- If `corall auth register --help` does not match this guide, stop, quote the exact help output, and reinstall or upgrade from the current quickstart. Do not ask the user for email/password fields. +- If `openclaw status` reports errors, stop there and ask the user to fix OpenClaw before changing config or auth state. +- If `corall openclaw setup` omits `webhookToken` because you passed `--webhook-token`, use the token you passed. Do not invent missing JSON fields. +- If `corall subscriptions status --profile provider` still shows `"hasActiveSubscription": false`, wait and retry. Do not activate or present the agent as live until the membership is active. +- If `corall agents list --mine --profile provider` already shows the provider's agent in `DRAFT` or `ACTIVE`, update that agent's polling token instead of creating a duplicate. diff --git a/skills/corall/references/skill-package-submit.md b/skills/corall/references/skill-package-submit.md index 468e6e1..b609348 100644 --- a/skills/corall/references/skill-package-submit.md +++ b/skills/corall/references/skill-package-submit.md @@ -175,3 +175,10 @@ corall skill-packages purchase --profile employer The install command writes `skills.source.files` into `~/.openclaw/skills//` and stores package metadata in `.corall-package.json`. + +## Conservative Fallback For Weaker Models + +- Before generating the package JSON, inspect the actual skill source: `SKILL.md`, `references/`, `scripts/`, assets, templates, and any install notes. If you do not have the source files yet, stop and ask for them. Do not fabricate `source.files`. +- Do not run `corall skill-packages create` until the provider has reviewed the generated form. +- If a local skill directory was deleted after purchase, start with `corall skill-packages purchased --profile employer` and then `corall skill-packages install --profile employer`. Do not open a new checkout for an already purchased package. +- Use `--force` only when there is already a local copy and the user clearly wants it replaced. diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs index 948acc3..790ff44 100644 --- a/tests/skill_contract.rs +++ b/tests/skill_contract.rs @@ -2,9 +2,12 @@ const SKILL: &str = include_str!("../skills/corall/SKILL.md"); const ORDER_HANDLE: &str = include_str!("../skills/corall/references/order-handle.md"); const ORDER_CREATE: &str = include_str!("../skills/corall/references/order-create.md"); const SETUP_PROVIDER: &str = include_str!("../skills/corall/references/setup-provider-openclaw.md"); +const SETUP_EMPLOYER: &str = include_str!("../skills/corall/references/setup-employer.md"); const SKILL_PACKAGE_SUBMIT: &str = include_str!("../skills/corall/references/skill-package-submit.md"); const AGENT_APPROVAL: &str = include_str!("../skills/corall/references/agent-approval.md"); +const FILE_UPLOAD: &str = include_str!("../skills/corall/references/file-upload.md"); +const PAYOUT: &str = include_str!("../skills/corall/references/payout.md"); const CLI_REFERENCE: &str = include_str!("../skills/corall/references/cli-reference.md"); const EVAL_CASES: &str = include_str!("../skills/corall/evals/cases.md"); const PLUGIN_JSON: &str = include_str!("../skills/corall/.claude-plugin/plugin.json"); @@ -31,6 +34,11 @@ fn skill_routes_corall_prompts_to_the_expected_modes() { SKILL, "Do not start a new checkout unless the package is not already purchased", ); + assert_contains(SKILL, "Conservative Fallback For Weaker Models"); + assert_contains(SKILL, "Run the exact documented commands and flags"); + assert_contains(SKILL, "quote the exact command and output"); + assert_contains(SKILL, "Deleted purchased skill package"); + assert_contains(SKILL, "Missing `jq` during artifact upload"); assert_contains(PLUGIN_JSON, "OpenClaw polling plugin"); assert_not_contains(PLUGIN_JSON, "OpenClaw webhook"); } @@ -52,6 +60,9 @@ fn order_handle_prompt_accepts_then_submits_with_provider_profile() { ORDER_HANDLE, "does **not** authorize reading or uploading pre-existing host files", ); + assert_contains(ORDER_HANDLE, "Conservative Fallback For Weaker Models"); + assert_contains(ORDER_HANDLE, "Do not invent extra workflow states"); + assert_contains(ORDER_HANDLE, "still submit a factual failure or refusal summary"); assert_not_contains(ORDER_HANDLE, "webhook mode"); } @@ -82,6 +93,13 @@ fn order_create_prompt_matches_current_cli_responses_and_statuses() { assert_contains(ORDER_CREATE, "prefer the penalty-based path"); assert_contains(ORDER_CREATE, "--reviewer-kind employer-agent"); assert_contains(ORDER_CREATE, "--requirement-miss 0"); + assert_contains(ORDER_CREATE, "Conservative Fallback For Weaker Models"); + assert_contains(ORDER_CREATE, "Do not assume payment succeeded"); + assert_contains( + ORDER_CREATE, + "Do not approve, dispute, or review before the order reaches `delivered`", + ); + assert_contains(ORDER_CREATE, "report the exact current status"); assert_not_contains(ORDER_CREATE, "paymentStatus"); assert_not_contains(ORDER_CREATE, "orderStatus"); assert_not_contains(ORDER_CREATE, "SUBMITTED"); @@ -116,6 +134,13 @@ fn provider_setup_prompt_uses_polling_and_explicit_provider_profile() { ); assert_contains(SETUP_PROVIDER, "eventbus polling bearer token"); assert_contains(SETUP_PROVIDER, "If the command shape differs"); + assert_contains(SETUP_PROVIDER, "Conservative Fallback For Weaker Models"); + assert_contains(SETUP_PROVIDER, "quote the exact help output"); + assert_contains(SETUP_PROVIDER, "Do not activate or present the agent as live"); + assert_contains( + SETUP_PROVIDER, + "update that agent's polling token instead of creating a duplicate", + ); assert_not_contains(SETUP_PROVIDER, "\\ #"); } @@ -147,6 +172,12 @@ fn eval_cases_and_cli_reference_follow_current_contract() { ); assert_contains(CLI_REFERENCE, "Compatibility gate"); assert_contains(CLI_REFERENCE, "If the command shape differs"); + assert_contains(CLI_REFERENCE, "Conservative Execution"); + assert_contains(CLI_REFERENCE, "Execute one command at a time"); + assert_contains( + CLI_REFERENCE, + "Do not invent routes, JSON fields, or legacy signup parameters", + ); assert_not_contains(CLI_REFERENCE, "--email"); assert_not_contains(CLI_REFERENCE, "--password"); assert_contains(AGENT_APPROVAL, "corall auth approve"); @@ -158,6 +189,8 @@ fn eval_cases_and_cli_reference_follow_current_contract() { "corall subscriptions status --profile provider", ); assert_contains(AGENT_APPROVAL, "HttpOnly session cookie"); + assert_contains(AGENT_APPROVAL, "Conservative Fallback For Weaker Models"); + assert_contains(AGENT_APPROVAL, "Do not reuse an old `loginUrl`"); assert_contains( AGENT_APPROVAL, "Do not create dashboard login URLs from polling-delivered order sessions", @@ -179,6 +212,54 @@ fn eval_cases_and_cli_reference_follow_current_contract() { SKILL_PACKAGE_SUBMIT, "corall skill-packages purchased --profile employer", ); + assert_contains(SKILL_PACKAGE_SUBMIT, "Conservative Fallback For Weaker Models"); + assert_contains(SKILL_PACKAGE_SUBMIT, "Do not fabricate `source.files`"); + assert_contains( + SKILL_PACKAGE_SUBMIT, + "Do not run `corall skill-packages create` until the provider has reviewed the generated form", + ); + assert_contains(SETUP_EMPLOYER, "Conservative Fallback For Weaker Models"); + assert_contains( + SETUP_EMPLOYER, + "Do not invent `--site-url`, `--display-name`, email, or password fields", + ); + assert_contains( + SETUP_EMPLOYER, + "verify with `corall auth me --profile employer` instead of registering a second account", + ); + assert_contains(FILE_UPLOAD, "Conservative Fallback For Weaker Models"); + assert_contains(FILE_UPLOAD, "python3 -c"); + assert_contains(FILE_UPLOAD, "stop and report the exact JSON"); + assert_contains(PAYOUT, "Conservative Fallback For Weaker Models"); + assert_contains(PAYOUT, "corall connect status --profile provider"); + assert_contains( + PAYOUT, + "still answer with the full conditional sequence and include these literal command lines", + ); + assert_contains(PAYOUT, "corall connect onboard --profile provider"); + assert_contains(PAYOUT, "corall connect payout --profile provider"); + assert_contains( + PAYOUT, + "Do not replace `corall connect onboard --profile provider` with vague advice like “open the onboardingUrl”", + ); + assert_contains( + PAYOUT, + "do not stop at those visibility commands when the user needs to know whether onboarding or payout is the next action", + ); + assert_contains(PAYOUT, "Do not claim money was transferred"); + assert_contains(EVAL_CASES, "Stale CLI help requests email/password"); + assert_contains(EVAL_CASES, "Deleted purchased skill package"); + assert_contains(EVAL_CASES, "Artifact upload without jq"); + assert_contains(EVAL_CASES, "Payout onboarding incomplete"); + assert_contains( + EVAL_CASES, + "Still names `corall connect payout --profile provider` as the next action after onboarding is complete", + ); + assert_contains( + EVAL_CASES, + "still includes those literal command lines in the answer", + ); + assert_contains(EVAL_CASES, "Low-confidence deterministic fallback"); } fn assert_contains(haystack: &str, needle: &str) { From 67ad5a230851dbe41582c2fb1cec90e3996b2677 Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Tue, 28 Apr 2026 22:29:54 +0800 Subject: [PATCH 09/14] Add eventbus polling support to corall cli --- Cargo.toml | 2 +- skills/corall/references/cli-reference.md | 36 + .../references/setup-provider-openclaw.md | 14 +- src/commands/agents.rs | 19 +- src/commands/auth.rs | 1 + src/commands/eventbus.rs | 106 +- src/commands/reviews.rs | 63 +- src/credentials.rs | 17 +- src/eventbus_poller.rs | 693 ++++++++++ src/main.rs | 3 +- tests/agent_approval.rs | 3 + tests/eventbus_poll_cli.rs | 1157 +++++++++++++++++ tests/reviews_cli.rs | 387 ++++++ tests/skill_contract.rs | 49 +- 14 files changed, 2523 insertions(+), 27 deletions(-) create mode 100644 src/eventbus_poller.rs create mode 100644 tests/eventbus_poll_cli.rs create mode 100644 tests/reviews_cli.rs diff --git a/Cargo.toml b/Cargo.toml index acae7f2..886478a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,5 +22,5 @@ rustls = { version = "0.23", default-features = false, features = ["ring", "std" serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.149" sha2 = "0.11" -tokio = { version = "1.50.0", features = ["rt-multi-thread", "macros", "net", "io-util"] } +tokio = { version = "1.50.0", features = ["rt-multi-thread", "macros", "net", "io-util", "time"] } zip = { version = "8", default-features = false, features = ["deflate"] } diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index 9b389a5..3dbac86 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -171,6 +171,7 @@ If the user explicitly gave a rating, pass `--rating` and Corall will use it dir ```text corall openclaw setup [--webhook-token ] [--eventbus-url ] [--config ] [--skip-plugin-install] corall eventbus serve [--listen ] [--redis-url ] [--consumer-group ] [--default-wait-ms ] [--max-wait-ms ] [--default-count ] [--max-count ] [--claim-idle-ms ] +corall eventbus poll [--base-url ] [--agent-id ] [--webhook-token ] [--consumer-id ] [--wait-ms ] [--request-timeout-ms ] [--ack-timeout-ms ] [--idle-delay-ms ] [--error-backoff-ms ] [--max-error-backoff-ms ] [--recent-event-ttl-ms ] [--hook-url ] [--hook-token ] [--exec ] [--exec-arg ]... ``` Merges Corall polling-delivery settings into the OpenClaw config file. Sets @@ -203,6 +204,41 @@ registrations from `corall:eventbus:agent::registration`, serves `POST /v1/agents/:agent_id/events/:event_id/ack`, and consumes agent streams from `corall:eventbus:agent::stream`. +`corall eventbus poll` is the non-OpenClaw equivalent of the resident polling +plugin. It long-polls the eventbus with the same bearer token and then delivers +each event either: + +- to a local HTTP endpoint via `--hook-url`, or +- to a local command via `--exec` / `--exec-arg`, with the event JSON envelope + written to stdin. + +For generic agents that should stay up in the background, run it under `nohup` +or another supervisor: + +```bash +nohup corall eventbus poll \ + --base-url http://:3001 \ + --profile provider \ + --webhook-token \ + --exec python3 \ + --exec-arg /opt/my-agent/corall_worker.py \ + >/var/log/corall-poll.log 2>&1 & +``` + +The executed command receives these environment variables: + +- `CORALL_AGENT_ID` +- `CORALL_EVENT_ID` +- `CORALL_EVENT_DEDUPE_ID` +- `CORALL_HOOK_NAME` +- `CORALL_HOOK_MESSAGE` +- `CORALL_HOOK_SESSION_KEY` +- `CORALL_HOOK_DELIVER` + +If you created or updated the agent with `corall agents create/update --webhook-token`, +the CLI remembers that polling token in the active credential profile, so later +`corall eventbus poll` runs can omit `--webhook-token`. + ## Upgrade ```text diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index 2cf924f..d8b2d1f 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -4,6 +4,12 @@ This guide registers an OpenClaw instance as an agent on the Corall marketplace Provider order execution is **polling-based**. Corall writes order events to the eventbus; the resident `corall-polling` plugin pulls them and delivers them locally to OpenClaw. Corall does not perform an HTTP callback into the provider. +If the provider is not using OpenClaw, do not force the OpenClaw plugin path. +Use `corall eventbus poll` instead and keep the worker alive with +`nohup` or another supervisor. In that generic mode, Corall still uses the same +eventbus polling token, but the local delivery target is either `--hook-url` or +`--exec/--exec-arg`, not `/hooks/agent`. + Walk through these steps in order. Stop and ask the user if anything looks wrong or unexpected — do not make changes to config files without confirming the current state is healthy first. ## 1. OpenClaw Preflight @@ -44,7 +50,7 @@ Corall does not call the provider over a public webhook in OpenClaw polling mode Run this command to merge the required polling and local delivery settings into `~/.openclaw/openclaw.json`: ```bash -corall openclaw setup --eventbus-url http://:8787 +corall openclaw setup --eventbus-url http://:3001 ``` Important naming note: `--webhook-token` and `webhookToken` are legacy names. @@ -62,7 +68,7 @@ Do **not** configure or ask for a public `--webhook-url`. **Extract the polling token for later use:** ```bash -POLLING_TOKEN=$(corall openclaw setup --eventbus-url http://:8787 | jq -r '.webhookToken') +POLLING_TOKEN=$(corall openclaw setup --eventbus-url http://:3001 | jq -r '.webhookToken') ``` `webhookToken` is present whenever the polling token was generated or kept from the existing config. If you supplied `--webhook-token` yourself, the field is omitted (you already know it). @@ -72,7 +78,7 @@ To force a specific token (e.g. rotating or re-registering an existing agent): ```bash corall openclaw setup \ --webhook-token \ - --eventbus-url http://:8787 + --eventbus-url http://:3001 ``` If the OpenClaw config file lives elsewhere, pass `--config ` explicitly. @@ -95,7 +101,7 @@ Expected plugin config after setup: "corall-polling": { "enabled": true, "config": { - "baseUrl": "http://:8787", + "baseUrl": "http://:3001", "credentialProfile": "provider" } } diff --git a/src/commands/agents.rs b/src/commands/agents.rs index e8ea39e..089a28b 100644 --- a/src/commands/agents.rs +++ b/src/commands/agents.rs @@ -164,7 +164,7 @@ pub async fn run(cmd: AgentsCommand, profile: &str) -> Result<()> { if let Some(v) = webhook_url { body["webhookUrl"] = json!(v); } - if let Some(v) = webhook_token { + if let Some(v) = webhook_token.as_ref() { body["webhookToken"] = json!(v); } if let Some(s) = input_schema { @@ -182,7 +182,13 @@ pub async fn run(cmd: AgentsCommand, profile: &str) -> Result<()> { .and_then(|a| a.get("id")) .and_then(|v| v.as_str()) { - credentials::set_agent_id(profile, agent_id)?; + credentials::update_agent_registration( + profile, + Some(agent_id), + webhook_token.as_deref(), + )?; + } else if webhook_token.is_some() { + credentials::update_agent_registration(profile, None, webhook_token.as_deref())?; } println!("{}", serde_json::to_string_pretty(&resp)?); @@ -224,11 +230,18 @@ pub async fn run(cmd: AgentsCommand, profile: &str) -> Result<()> { if let Some(v) = webhook_url { body["webhookUrl"] = json!(v); } - if let Some(v) = webhook_token { + if let Some(v) = webhook_token.as_ref() { body["webhookToken"] = json!(v); } let resp = client.put(&format!("/api/agents/{id}"), &body).await?; + if webhook_token.is_some() { + credentials::update_agent_registration( + profile, + Some(id.as_str()), + webhook_token.as_deref(), + )?; + } println!("{}", serde_json::to_string_pretty(&resp)?); } diff --git a/src/commands/auth.rs b/src/commands/auth.rs index ac210d2..ac7ed80 100644 --- a/src/commands/auth.rs +++ b/src/commands/auth.rs @@ -90,6 +90,7 @@ pub async fn run(cmd: AuthCommand, profile: &str) -> Result<()> { }, private_key_pkcs8: key.private_key_pkcs8, agent_id: None, + polling_token: None, registered_at, token, token_expires_at, diff --git a/src/commands/eventbus.rs b/src/commands/eventbus.rs index 28a74bd..541ded4 100644 --- a/src/commands/eventbus.rs +++ b/src/commands/eventbus.rs @@ -5,6 +5,7 @@ use clap::Subcommand; use crate::eventbus::EventBusServeOptions; use crate::eventbus::EventBusServer; +use crate::eventbus_poller::EventbusPollOptions; #[derive(Subcommand, Debug)] pub enum EventbusCommand { @@ -42,9 +43,73 @@ pub enum EventbusCommand { #[arg(long, default_value_t = 30_000)] claim_idle_ms: u64, }, + /// Long-poll agent order events from the Corall eventbus and deliver them locally + Poll { + /// Corall eventbus base URL. Falls back to CORALL_EVENTBUS_URL. + #[arg(long)] + base_url: Option, + + /// Agent ID. Defaults to agentId stored in the active credential profile. + #[arg(long)] + agent_id: Option, + + /// Eventbus polling bearer token. Falls back to CORALL_WEBHOOK_TOKEN or + /// pollingToken stored in the active credential profile. + #[arg(long, alias = "agent-token")] + webhook_token: Option, + + /// Consumer ID used for the eventbus stream group. + #[arg(long)] + consumer_id: Option, + + /// Long-poll wait in milliseconds. + #[arg(long, default_value_t = 30_000)] + wait_ms: u64, + + /// HTTP timeout for each poll request. Defaults to waitMs + 15000. + #[arg(long)] + request_timeout_ms: Option, + + /// Timeout for local delivery and ack requests. + #[arg(long, default_value_t = 10_000)] + ack_timeout_ms: u64, + + /// Delay after an empty poll result. + #[arg(long, default_value_t = 1_000)] + idle_delay_ms: u64, + + /// Initial error backoff after a failed poll cycle. + #[arg(long, default_value_t = 2_000)] + error_backoff_ms: u64, + + /// Maximum error backoff after repeated failures. + #[arg(long, default_value_t = 30_000)] + max_error_backoff_ms: u64, + + /// Deduplication window for already-forwarded events. + #[arg(long, default_value_t = 600_000)] + recent_event_ttl_ms: u64, + + /// Local HTTP endpoint that should receive the hook payload. + #[arg(long)] + hook_url: Option, + + /// Optional bearer token for the local hook endpoint. + #[arg(long)] + hook_token: Option, + + /// Local program to execute for each event. The JSON event envelope is + /// written to stdin. Use repeated --exec-arg values for arguments. + #[arg(long)] + exec: Option, + + /// Arguments passed to --exec. + #[arg(long = "exec-arg")] + exec_args: Vec, + }, } -pub async fn run(cmd: EventbusCommand) -> Result<()> { +pub async fn run(cmd: EventbusCommand, profile: &str) -> Result<()> { match cmd { EventbusCommand::Serve { listen, @@ -68,5 +133,44 @@ pub async fn run(cmd: EventbusCommand) -> Result<()> { })?; server.serve().await } + EventbusCommand::Poll { + base_url, + agent_id, + webhook_token, + consumer_id, + wait_ms, + request_timeout_ms, + ack_timeout_ms, + idle_delay_ms, + error_backoff_ms, + max_error_backoff_ms, + recent_event_ttl_ms, + hook_url, + hook_token, + exec, + exec_args, + } => { + crate::eventbus_poller::run( + EventbusPollOptions { + base_url, + agent_id, + webhook_token, + consumer_id, + wait_ms, + request_timeout_ms, + ack_timeout_ms, + idle_delay_ms, + error_backoff_ms, + max_error_backoff_ms, + recent_event_ttl_ms, + hook_url, + hook_token, + exec, + exec_args, + }, + profile, + ) + .await + } } } diff --git a/src/commands/reviews.rs b/src/commands/reviews.rs index 5d63150..70db378 100644 --- a/src/commands/reviews.rs +++ b/src/commands/reviews.rs @@ -35,7 +35,7 @@ pub enum ReviewsCommand { Create { order_id: String, /// Explicit rating from 0.0 to 5.0. Omit this to use penalty-based scoring. - #[arg(long)] + #[arg(long, value_parser = parse_rating)] rating: Option, #[arg(long)] comment: Option, @@ -43,22 +43,22 @@ pub enum ReviewsCommand { #[arg(long, value_enum, default_value_t = ReviewerKindArg::Human)] reviewer_kind: ReviewerKindArg, /// Penalty severity for unmet requirements (0-3) - #[arg(long, default_value_t = 0)] + #[arg(long, default_value_t = 0, value_parser = parse_penalty)] requirement_miss: u8, /// Penalty severity for correctness defects (0-3) - #[arg(long, default_value_t = 0)] + #[arg(long, default_value_t = 0, value_parser = parse_penalty)] correctness_defect: u8, /// Penalty severity for avoidable rework (0-3) - #[arg(long, default_value_t = 0)] + #[arg(long, default_value_t = 0, value_parser = parse_penalty)] rework_burden: u8, /// Penalty severity for delivery timeliness misses (0-3) - #[arg(long, default_value_t = 0)] + #[arg(long, default_value_t = 0, value_parser = parse_penalty)] timeliness_miss: u8, /// Penalty severity for communication friction (0-3) - #[arg(long, default_value_t = 0)] + #[arg(long, default_value_t = 0, value_parser = parse_penalty)] communication_friction: u8, /// Penalty severity for safety or policy risk (0-3) - #[arg(long, default_value_t = 0)] + #[arg(long, default_value_t = 0, value_parser = parse_penalty)] safety_risk: u8, }, } @@ -133,9 +133,6 @@ fn build_review_request( if let Some(rating) = rating { body["rating"] = json!(rating); - if penalties != PenaltyArgs::default() { - body["penalties"] = json!(penalties_json(penalties)); - } } else { body["penalties"] = json!(penalties_json(penalties)); } @@ -158,6 +155,28 @@ fn penalties_json(penalties: PenaltyArgs) -> Value { }) } +fn parse_rating(raw: &str) -> std::result::Result { + let rating = raw + .parse::() + .map_err(|_| format!("invalid rating `{raw}`"))?; + if (0.0..=5.0).contains(&rating) { + Ok(rating) + } else { + Err("rating must be between 0.0 and 5.0".to_string()) + } +} + +fn parse_penalty(raw: &str) -> std::result::Result { + let penalty = raw + .parse::() + .map_err(|_| format!("invalid penalty `{raw}`"))?; + if penalty <= 3 { + Ok(penalty) + } else { + Err("penalty values must be between 0 and 3".to_string()) + } +} + #[cfg(test)] mod tests { use serde_json::json; @@ -165,6 +184,8 @@ mod tests { use super::PenaltyArgs; use super::ReviewerKindArg; use super::build_review_request; + use super::parse_penalty; + use super::parse_rating; #[test] fn manual_rating_bypasses_penalty_scoring() { @@ -185,7 +206,7 @@ mod tests { assert_eq!(body["rating"], 4.7); assert_eq!(body["reviewerKind"], "human"); - assert_eq!(body["penalties"]["requirementMiss"], 3); + assert!(body.get("penalties").is_none()); } #[test] @@ -219,4 +240,24 @@ mod tests { ); assert!(body.get("rating").is_none()); } + + #[test] + fn parse_rating_rejects_out_of_range_values() { + assert_eq!(parse_rating("0.0").unwrap(), 0.0); + assert_eq!(parse_rating("5.0").unwrap(), 5.0); + assert_eq!( + parse_rating("5.1").unwrap_err(), + "rating must be between 0.0 and 5.0" + ); + } + + #[test] + fn parse_penalty_rejects_out_of_range_values() { + assert_eq!(parse_penalty("0").unwrap(), 0); + assert_eq!(parse_penalty("3").unwrap(), 3); + assert_eq!( + parse_penalty("4").unwrap_err(), + "penalty values must be between 0 and 3" + ); + } } diff --git a/src/credentials.rs b/src/credentials.rs index 1fb3ac1..cb6de2d 100644 --- a/src/credentials.rs +++ b/src/credentials.rs @@ -19,6 +19,8 @@ pub struct Credential { #[serde(skip_serializing_if = "Option::is_none")] pub agent_id: Option, #[serde(skip_serializing_if = "Option::is_none")] + pub polling_token: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub registered_at: Option, /// Cached JWT token from the last successful login. #[serde(skip_serializing_if = "Option::is_none")] @@ -124,9 +126,18 @@ pub fn save(profile: &str, cred: &Credential) -> Result<()> { Ok(()) } -pub fn set_agent_id(profile: &str, agent_id: &str) -> Result<()> { +pub fn update_agent_registration( + profile: &str, + agent_id: Option<&str>, + polling_token: Option<&str>, +) -> Result<()> { let mut cred = load(profile)?; - cred.agent_id = Some(agent_id.to_string()); + if let Some(agent_id) = agent_id { + cred.agent_id = Some(agent_id.to_string()); + } + if let Some(polling_token) = polling_token { + cred.polling_token = Some(polling_token.to_string()); + } save(profile, &cred) } @@ -169,6 +180,7 @@ mod tests { }, private_key_pkcs8: "b".repeat(64), agent_id: Some("agent-1".to_string()), + polling_token: Some("polling-token".to_string()), registered_at: Some("2026-04-20T00:00:00Z".to_string()), token: Some("token".to_string()), token_expires_at: Some(1_776_000_000), @@ -184,6 +196,7 @@ mod tests { }, "privateKeyPkcs8": "b".repeat(64), "agentId": "agent-1", + "pollingToken": "polling-token", "registeredAt": "2026-04-20T00:00:00Z", "token": "token", "tokenExpiresAt": 1776000000, diff --git a/src/eventbus_poller.rs b/src/eventbus_poller.rs new file mode 100644 index 0000000..e9f3c29 --- /dev/null +++ b/src/eventbus_poller.rs @@ -0,0 +1,693 @@ +use std::collections::HashMap; +use std::env; +use std::io::Write; +use std::process::Command; +use std::process::Stdio; +use std::time::Duration; +use std::time::Instant; + +use anyhow::Context; +use anyhow::Result; +use anyhow::bail; +use reqwest::Client; +use reqwest::Url; +use serde::Deserialize; +use serde::Serialize; +use serde_json::Value; +use serde_json::json; +use tokio::time::sleep; + +use crate::credentials; + +#[derive(Debug, Clone)] +pub struct EventbusPollOptions { + pub base_url: Option, + pub agent_id: Option, + pub webhook_token: Option, + pub consumer_id: Option, + pub wait_ms: u64, + pub request_timeout_ms: Option, + pub ack_timeout_ms: u64, + pub idle_delay_ms: u64, + pub error_backoff_ms: u64, + pub max_error_backoff_ms: u64, + pub recent_event_ttl_ms: u64, + pub hook_url: Option, + pub hook_token: Option, + pub exec: Option, + pub exec_args: Vec, +} + +#[derive(Debug, Clone)] +struct ResolvedPollOptions { + base_url: String, + agent_id: String, + webhook_token: String, + consumer_id: String, + wait_ms: u64, + request_timeout_ms: u64, + ack_timeout_ms: u64, + idle_delay_ms: u64, + error_backoff_ms: u64, + max_error_backoff_ms: u64, + recent_event_ttl_ms: u64, + delivery: DeliveryMode, +} + +#[derive(Debug, Clone)] +enum DeliveryMode { + Hook { + hook_url: String, + hook_token: Option, + }, + Exec { + program: String, + args: Vec, + }, +} + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] +struct HookPayload { + message: String, + name: String, + #[serde(rename = "sessionKey")] + session_key: String, + deliver: bool, +} + +#[derive(Debug, Clone, Serialize, PartialEq, Eq)] +struct PollingEvent { + id: String, + #[serde(rename = "dedupeId")] + dedupe_id: String, + hook: HookPayload, +} + +pub async fn run(options: EventbusPollOptions, profile: &str) -> Result<()> { + let resolved = resolve_options(options, profile)?; + let client = Client::new(); + let mut recent_events = HashMap::::new(); + let mut backoff_ms = resolved.error_backoff_ms.max(1); + + println!( + "{}", + serde_json::to_string_pretty(&json!({ + "mode": "eventbus-poll", + "baseUrl": resolved.base_url, + "agentId": resolved.agent_id, + "consumerId": resolved.consumer_id, + "waitMs": resolved.wait_ms, + "delivery": delivery_summary(&resolved.delivery), + }))? + ); + + loop { + prune_recent_events(&mut recent_events, resolved.recent_event_ttl_ms); + + match poll_events(&client, &resolved).await { + Ok(events) if events.is_empty() => { + backoff_ms = resolved.error_backoff_ms.max(1); + sleep(Duration::from_millis(resolved.idle_delay_ms)).await; + } + Ok(events) => { + for event in events { + handle_event(&client, &resolved, &event, &mut recent_events).await?; + } + backoff_ms = resolved.error_backoff_ms.max(1); + } + Err(err) => { + eprintln!( + "{}", + json!({ + "warning": format!("eventbus poll cycle failed: {err}"), + "backoffMs": backoff_ms, + }) + ); + sleep(Duration::from_millis(backoff_ms)).await; + backoff_ms = (backoff_ms.saturating_mul(2)) + .max(resolved.error_backoff_ms.max(1)) + .min( + resolved + .max_error_backoff_ms + .max(resolved.error_backoff_ms.max(1)), + ); + } + } + } +} + +fn resolve_options(options: EventbusPollOptions, profile: &str) -> Result { + let base_url = options + .base_url + .or_else(|| env::var("CORALL_EVENTBUS_URL").ok()) + .map(|value| value.trim_end_matches('/').to_string()) + .filter(|value| !value.is_empty()) + .context("eventbus base URL is required: pass --base-url or set CORALL_EVENTBUS_URL")?; + + let cred = credentials::load(profile).ok(); + let agent_id = options + .agent_id + .or_else(|| cred.as_ref().and_then(|cred| cred.agent_id.clone())) + .context( + "no agentId found — pass --agent-id or create/update an agent with this profile first", + )?; + + let webhook_token = options + .webhook_token + .or_else(|| env::var("CORALL_WEBHOOK_TOKEN").ok()) + .or_else(|| cred.as_ref().and_then(|cred| cred.polling_token.clone())) + .filter(|value| !value.trim().is_empty()) + .context( + "polling token is required: pass --webhook-token, set CORALL_WEBHOOK_TOKEN, or create/update the agent with --webhook-token first", + )?; + + let delivery = match ( + options.hook_url.map(|value| value.trim().to_string()), + options.exec.map(|value| value.trim().to_string()), + ) { + (Some(hook_url), None) if !hook_url.is_empty() => DeliveryMode::Hook { + hook_url, + hook_token: options.hook_token.filter(|value| !value.trim().is_empty()), + }, + (None, Some(program)) if !program.is_empty() => DeliveryMode::Exec { + program, + args: options.exec_args, + }, + (Some(_), Some(_)) => { + bail!("choose exactly one local delivery target: either --hook-url or --exec") + } + _ => bail!("missing local delivery target: pass either --hook-url or --exec"), + }; + + let consumer_id = options + .consumer_id + .filter(|value| !value.trim().is_empty()) + .unwrap_or_else(|| format!("corall-cli-poll:{agent_id}:{}", std::process::id())); + + Ok(ResolvedPollOptions { + base_url, + agent_id, + webhook_token, + consumer_id, + wait_ms: options.wait_ms, + request_timeout_ms: options + .request_timeout_ms + .unwrap_or(options.wait_ms.saturating_add(15_000)) + .max(options.wait_ms.saturating_add(1_000)), + ack_timeout_ms: options.ack_timeout_ms, + idle_delay_ms: options.idle_delay_ms, + error_backoff_ms: options.error_backoff_ms.max(1), + max_error_backoff_ms: options + .max_error_backoff_ms + .max(options.error_backoff_ms.max(1)), + recent_event_ttl_ms: options.recent_event_ttl_ms.max(1), + delivery, + }) +} + +fn delivery_summary(delivery: &DeliveryMode) -> Value { + match delivery { + DeliveryMode::Hook { hook_url, .. } => json!({ + "mode": "hook", + "hookUrl": hook_url, + }), + DeliveryMode::Exec { program, args } => json!({ + "mode": "exec", + "program": program, + "args": args, + }), + } +} + +fn prune_recent_events(recent_events: &mut HashMap, ttl_ms: u64) { + let ttl = Duration::from_millis(ttl_ms); + recent_events.retain(|_, seen_at| seen_at.elapsed() <= ttl); +} + +async fn poll_events(client: &Client, config: &ResolvedPollOptions) -> Result> { + let mut url = agent_events_url(config, None)?; + url.query_pairs_mut() + .append_pair("consumerId", &config.consumer_id) + .append_pair("wait", &config.wait_ms.to_string()); + + let response = client + .get(url.clone()) + .bearer_auth(&config.webhook_token) + .header("accept", "application/json") + .timeout(Duration::from_millis(config.request_timeout_ms)) + .send() + .await + .with_context(|| format!("failed to poll {url}"))?; + + let status = response.status(); + let body = response + .text() + .await + .context("failed to read eventbus poll response body")?; + if !status.is_success() { + bail!("eventbus poll returned HTTP {status}: {body}"); + } + + let value: Value = if body.trim().is_empty() { + Value::Null + } else { + serde_json::from_str(&body).context("eventbus poll response was not valid JSON")? + }; + + Ok(extract_events(&value) + .into_iter() + .filter_map(normalize_event) + .collect()) +} + +async fn ack_event(client: &Client, config: &ResolvedPollOptions, event_id: &str) -> Result<()> { + let url = agent_events_url(config, Some(event_id))?; + + let response = client + .post(url.clone()) + .bearer_auth(&config.webhook_token) + .header("accept", "application/json") + .timeout(Duration::from_millis(config.ack_timeout_ms)) + .send() + .await + .with_context(|| format!("failed to ack {url}"))?; + + let status = response.status(); + let body = response + .text() + .await + .context("failed to read eventbus ack response body")?; + if !status.is_success() { + bail!("eventbus ack returned HTTP {status}: {body}"); + } + Ok(()) +} + +fn agent_events_url(config: &ResolvedPollOptions, event_id: Option<&str>) -> Result { + let mut url = Url::parse(&config.base_url)?; + { + let mut segments = url + .path_segments_mut() + .map_err(|_| anyhow::anyhow!("base URL cannot be used for path segments"))?; + segments.extend(["v1", "agents", &config.agent_id, "events"]); + if let Some(event_id) = event_id { + segments.extend([event_id, "ack"]); + } + } + Ok(url) +} + +async fn handle_event( + client: &Client, + config: &ResolvedPollOptions, + event: &PollingEvent, + recent_events: &mut HashMap, +) -> Result<()> { + let already_forwarded = recent_events.contains_key(&event.dedupe_id); + if !already_forwarded { + deliver_event(client, config, event).await?; + recent_events.insert(event.dedupe_id.clone(), Instant::now()); + } + ack_event(client, config, &event.id).await +} + +async fn deliver_event( + client: &Client, + config: &ResolvedPollOptions, + event: &PollingEvent, +) -> Result<()> { + match &config.delivery { + DeliveryMode::Hook { + hook_url, + hook_token, + } => deliver_hook(client, hook_url, hook_token.as_deref(), &event.hook, config).await, + DeliveryMode::Exec { program, args } => deliver_exec(program, args, config, event), + } +} + +async fn deliver_hook( + client: &Client, + hook_url: &str, + hook_token: Option<&str>, + hook: &HookPayload, + config: &ResolvedPollOptions, +) -> Result<()> { + let mut request = client + .post(hook_url) + .header("content-type", "application/json") + .header("accept", "application/json") + .timeout(Duration::from_millis(config.ack_timeout_ms)) + .json(hook); + if let Some(hook_token) = hook_token { + request = request.bearer_auth(hook_token); + } + + let response = request + .send() + .await + .with_context(|| format!("failed to deliver hook to {hook_url}"))?; + + let status = response.status(); + let body = response + .text() + .await + .context("failed to read local hook response body")?; + if !status.is_success() { + bail!("local hook returned HTTP {status}: {body}"); + } + Ok(()) +} + +fn deliver_exec( + program: &str, + args: &[String], + config: &ResolvedPollOptions, + event: &PollingEvent, +) -> Result<()> { + let payload = serde_json::to_vec(&json!({ + "id": event.id, + "dedupeId": event.dedupe_id, + "hook": event.hook, + }))?; + + let mut child = Command::new(program) + .args(args) + .env("CORALL_AGENT_ID", &config.agent_id) + .env("CORALL_EVENT_ID", &event.id) + .env("CORALL_EVENT_DEDUPE_ID", &event.dedupe_id) + .env("CORALL_HOOK_NAME", &event.hook.name) + .env("CORALL_HOOK_MESSAGE", &event.hook.message) + .env("CORALL_HOOK_SESSION_KEY", &event.hook.session_key) + .env("CORALL_HOOK_DELIVER", event.hook.deliver.to_string()) + .stdin(Stdio::piped()) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .spawn() + .with_context(|| format!("failed to start local command `{program}`"))?; + + if let Some(stdin) = child.stdin.as_mut() { + stdin + .write_all(&payload) + .context("failed to write event payload to local command stdin")?; + } else { + bail!("local command `{program}` did not expose stdin"); + } + + let status = child + .wait() + .with_context(|| format!("failed to wait for local command `{program}`"))?; + if !status.success() { + bail!("local command `{program}` exited with status {status}"); + } + + Ok(()) +} + +fn extract_events(payload: &Value) -> Vec<&Value> { + if let Some(events) = payload.as_array() { + return events.iter().collect(); + } + + let Some(object) = payload.as_object() else { + return Vec::new(); + }; + + if let Some(events) = object.get("events").and_then(Value::as_array) { + return events.iter().collect(); + } + + if let Some(event) = object.get("event") { + return vec![event]; + } + + if object.get("hook").is_some() { + return vec![payload]; + } + + Vec::new() +} + +fn normalize_event(value: &Value) -> Option { + let object = value.as_object()?; + let id = first_string(object, &["id", "streamId", "stream_id"])?; + let hook = serde_json::from_value::(object.get("hook")?.clone()).ok()?; + let dedupe_id = first_string(object, &["eventId", "event_id", "dedupeId", "dedupe_id"]) + .unwrap_or_else(|| hook.session_key.clone()); + + Some(PollingEvent { + id, + dedupe_id, + hook, + }) +} + +fn first_string(object: &serde_json::Map, keys: &[&str]) -> Option { + keys.iter().find_map(|key| { + object + .get(*key) + .and_then(Value::as_str) + .map(str::trim) + .filter(|value| !value.is_empty()) + .map(str::to_owned) + }) +} + +#[cfg(test)] +mod tests { + use std::fs; + use std::path::PathBuf; + use std::time::SystemTime; + + use serde_json::json; + + use super::*; + + #[test] + fn normalize_event_accepts_eventbus_shapes() { + let event = normalize_event(&json!({ + "streamId": "stream-1", + "eventId": "order.paid:1", + "hook": { + "message": "paid", + "name": "Corall", + "sessionKey": "hook:corall:1", + "deliver": false + } + })) + .expect("event should normalize"); + + assert_eq!(event.id, "stream-1"); + assert_eq!(event.dedupe_id, "order.paid:1"); + assert_eq!(event.hook.session_key, "hook:corall:1"); + } + + #[test] + fn normalize_event_falls_back_to_session_key_for_dedupe_id() { + let event = normalize_event(&json!({ + "id": "stream-2", + "hook": { + "message": "paid", + "name": "Corall", + "sessionKey": "hook:corall:2", + "deliver": false + } + })) + .expect("event should normalize"); + + assert_eq!(event.id, "stream-2"); + assert_eq!(event.dedupe_id, "hook:corall:2"); + } + + #[test] + fn extract_events_accepts_single_event_shape() { + let payload = json!({ + "event": { + "id": "stream-3", + "hook": { + "message": "paid", + "name": "Corall", + "sessionKey": "hook:corall:3", + "deliver": false + } + } + }); + + let events = extract_events(&payload); + assert_eq!(events.len(), 1); + assert_eq!(events[0]["id"], "stream-3"); + } + + #[test] + fn resolve_options_rejects_missing_or_conflicting_delivery_targets() { + let base = EventbusPollOptions { + base_url: Some("http://127.0.0.1:3001".to_string()), + agent_id: Some("agent-1".to_string()), + webhook_token: Some("token".to_string()), + consumer_id: None, + wait_ms: 30_000, + request_timeout_ms: None, + ack_timeout_ms: 10_000, + idle_delay_ms: 1_000, + error_backoff_ms: 2_000, + max_error_backoff_ms: 30_000, + recent_event_ttl_ms: 600_000, + hook_url: None, + hook_token: None, + exec: None, + exec_args: Vec::new(), + }; + + let missing = resolve_options(base.clone(), "provider").unwrap_err(); + assert!( + missing + .to_string() + .contains("missing local delivery target: pass either --hook-url or --exec") + ); + + let conflicting = resolve_options( + EventbusPollOptions { + hook_url: Some("http://127.0.0.1:9000/hooks".to_string()), + exec: Some("python3".to_string()), + ..base + }, + "provider", + ) + .unwrap_err(); + assert!( + conflicting + .to_string() + .contains("choose exactly one local delivery target") + ); + } + + #[test] + fn resolve_options_uses_explicit_exec_delivery_and_defaults_consumer_id() { + let resolved = resolve_options( + EventbusPollOptions { + base_url: Some("http://127.0.0.1:3001/".to_string()), + agent_id: Some("agent-1".to_string()), + webhook_token: Some("token".to_string()), + consumer_id: None, + wait_ms: 30_000, + request_timeout_ms: None, + ack_timeout_ms: 10_000, + idle_delay_ms: 1_000, + error_backoff_ms: 2_000, + max_error_backoff_ms: 30_000, + recent_event_ttl_ms: 600_000, + hook_url: None, + hook_token: None, + exec: Some("python3".to_string()), + exec_args: vec!["worker.py".to_string()], + }, + "provider", + ) + .expect("options should resolve"); + + assert_eq!(resolved.base_url, "http://127.0.0.1:3001"); + assert_eq!(resolved.agent_id, "agent-1"); + assert_eq!(resolved.webhook_token, "token"); + assert!( + resolved.consumer_id.starts_with("corall-cli-poll:agent-1:"), + "unexpected consumer id: {}", + resolved.consumer_id + ); + assert_eq!(resolved.request_timeout_ms, 45_000); + match resolved.delivery { + DeliveryMode::Exec { program, args } => { + assert_eq!(program, "python3"); + assert_eq!(args, vec!["worker.py".to_string()]); + } + DeliveryMode::Hook { .. } => panic!("expected exec delivery"), + } + } + + #[test] + fn deliver_exec_writes_event_payload_to_stdin_and_exports_env() { + let out_path = unique_temp_file("corall-poller-event"); + let env_path = unique_temp_file("corall-poller-env"); + let config = ResolvedPollOptions { + base_url: "http://127.0.0.1:8787".to_string(), + agent_id: "agent-1".to_string(), + webhook_token: "token".to_string(), + consumer_id: "consumer".to_string(), + wait_ms: 30_000, + request_timeout_ms: 45_000, + ack_timeout_ms: 10_000, + idle_delay_ms: 10, + error_backoff_ms: 10, + max_error_backoff_ms: 100, + recent_event_ttl_ms: 60_000, + delivery: DeliveryMode::Exec { + program: "python3".to_string(), + args: vec![ + "-c".to_string(), + format!( + "import json, os, pathlib, sys; \ + pathlib.Path(r\"{}\").write_bytes(sys.stdin.buffer.read()); \ + pathlib.Path(r\"{}\").write_text(json.dumps({{\ + 'CORALL_AGENT_ID': os.environ.get('CORALL_AGENT_ID'), \ + 'CORALL_EVENT_ID': os.environ.get('CORALL_EVENT_ID'), \ + 'CORALL_EVENT_DEDUPE_ID': os.environ.get('CORALL_EVENT_DEDUPE_ID'), \ + 'CORALL_HOOK_NAME': os.environ.get('CORALL_HOOK_NAME'), \ + 'CORALL_HOOK_MESSAGE': os.environ.get('CORALL_HOOK_MESSAGE'), \ + 'CORALL_HOOK_SESSION_KEY': os.environ.get('CORALL_HOOK_SESSION_KEY'), \ + 'CORALL_HOOK_DELIVER': os.environ.get('CORALL_HOOK_DELIVER'), \ + }}))", + out_path.display(), + env_path.display() + ), + ], + }, + }; + let event = PollingEvent { + id: "stream-1".to_string(), + dedupe_id: "order-1".to_string(), + hook: HookPayload { + message: "paid".to_string(), + name: "Corall".to_string(), + session_key: "hook:corall:1".to_string(), + deliver: false, + }, + }; + + deliver_exec( + "python3", + match &config.delivery { + DeliveryMode::Exec { args, .. } => args, + DeliveryMode::Hook { .. } => unreachable!(), + }, + &config, + &event, + ) + .expect("exec delivery should succeed"); + + let written = fs::read_to_string(&out_path).expect("payload should be written"); + let value: Value = serde_json::from_str(&written).expect("payload should be valid JSON"); + assert_eq!(value["id"], "stream-1"); + assert_eq!(value["dedupeId"], "order-1"); + assert_eq!(value["hook"]["sessionKey"], "hook:corall:1"); + + let env_json = fs::read_to_string(&env_path).expect("env snapshot should be written"); + let env_value: Value = + serde_json::from_str(&env_json).expect("env snapshot should be valid JSON"); + assert_eq!(env_value["CORALL_AGENT_ID"], "agent-1"); + assert_eq!(env_value["CORALL_EVENT_ID"], "stream-1"); + assert_eq!(env_value["CORALL_EVENT_DEDUPE_ID"], "order-1"); + assert_eq!(env_value["CORALL_HOOK_NAME"], "Corall"); + assert_eq!(env_value["CORALL_HOOK_MESSAGE"], "paid"); + assert_eq!(env_value["CORALL_HOOK_SESSION_KEY"], "hook:corall:1"); + assert_eq!(env_value["CORALL_HOOK_DELIVER"], "false"); + + let _ = fs::remove_file(&out_path); + let _ = fs::remove_file(&env_path); + } + + fn unique_temp_file(prefix: &str) -> PathBuf { + let nanos = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("clock should be valid") + .as_nanos(); + std::env::temp_dir().join(format!("{prefix}-{nanos}.json")) + } +} diff --git a/src/main.rs b/src/main.rs index 738fbb2..b5808d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod client; mod commands; mod credentials; mod eventbus; +mod eventbus_poller; use anyhow::Result; use clap::Parser; @@ -116,7 +117,7 @@ async fn run() -> Result<()> { Command::SkillPackages { cmd } => skill_packages::run(cmd, profile).await, Command::Subscriptions { cmd } => subscriptions::run(cmd, profile).await, Command::Upgrade => upgrade::run().await, - Command::Eventbus { cmd } => eventbus_cmd::run(cmd).await, + Command::Eventbus { cmd } => eventbus_cmd::run(cmd, profile).await, Command::Upload { cmd } => upload::run(cmd, profile).await, Command::Openclaw { cmd } => openclaw::run(cmd).await, } diff --git a/tests/agent_approval.rs b/tests/agent_approval.rs index d6c4801..a730815 100644 --- a/tests/agent_approval.rs +++ b/tests/agent_approval.rs @@ -84,6 +84,9 @@ fn agent_ed25519_approval_signs_challenge_without_leaking_secrets() -> Result<() let approve_stdout = String::from_utf8(approve.stdout)?; assert!(approve_stdout.contains(r#""approved": true"#)); assert!(approve_stdout.contains("loginUrl")); + assert!(approve_stdout.contains("/dashboard?agentApproval=")); + assert!(!approve_stdout.contains("/login")); + assert!(!approve_stdout.contains("/signin")); assert!(!approve_stdout.contains("token")); assert!(!approve_stdout.contains("privateKeyPkcs8")); assert!(!approve_stdout.contains("signature")); diff --git a/tests/eventbus_poll_cli.rs b/tests/eventbus_poll_cli.rs new file mode 100644 index 0000000..d80f8a9 --- /dev/null +++ b/tests/eventbus_poll_cli.rs @@ -0,0 +1,1157 @@ +#[cfg(unix)] +mod unix_only { + use std::collections::HashMap; + use std::collections::VecDeque; + use std::error::Error; + use std::fs; + use std::io::Read; + use std::io::Write; + use std::net::Shutdown; + use std::net::SocketAddr; + use std::net::TcpListener; + use std::net::TcpStream; + use std::path::Path; + use std::path::PathBuf; + use std::process::Child; + use std::process::Command; + use std::process::Stdio; + use std::sync::Arc; + use std::sync::Mutex; + use std::sync::atomic::AtomicBool; + use std::sync::atomic::AtomicUsize; + use std::sync::atomic::Ordering; + use std::thread; + use std::thread::JoinHandle; + use std::time::Duration; + use std::time::Instant; + use std::time::SystemTime; + use std::time::UNIX_EPOCH; + + use serde_json::Value; + use serde_json::json; + + #[test] + fn nohup_eventbus_poll_exec_uses_saved_credentials_and_stays_alive() + -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-nohup")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let agent_id = unique_id("agent_exec"); + let polling_token = "polling-secret"; + let eventbus = FakeEventbusServer::start( + &agent_id, + polling_token, + vec![json!({ + "id": "stream-exec-1", + "eventId": "order.paid:exec-1", + "type": "order.paid", + "hook": { + "message": "exec event", + "name": "Corall", + "sessionKey": "hook:corall:exec-1", + "deliver": false + } + })], + )?; + write_credentials(&home, "provider", &agent_id, polling_token)?; + + let worker_script = temp.path().join("worker.py"); + let payload_path = temp.path().join("worker-payload.json"); + let env_path = temp.path().join("worker-env.json"); + let stdout_path = temp.path().join("poller.stdout.log"); + let stderr_path = temp.path().join("poller.stderr.log"); + write_exec_worker(&worker_script)?; + + let output = Command::new("sh") + .arg("-c") + .arg( + "nohup \"$BIN\" --profile provider eventbus poll \ + --base-url \"$BASE_URL\" \ + --exec python3 \ + --exec-arg \"$SCRIPT\" \ + --exec-arg \"$PAYLOAD\" \ + --exec-arg \"$ENVFILE\" \ + --wait-ms 5 \ + --request-timeout-ms 1000 \ + --ack-timeout-ms 1000 \ + --idle-delay-ms 50 \ + >\"$STDOUT\" 2>\"$STDERR\" & echo $!", + ) + .env("BIN", env!("CARGO_BIN_EXE_corall")) + .env("BASE_URL", eventbus.base_url()) + .env("SCRIPT", path_str(&worker_script)?) + .env("PAYLOAD", path_str(&payload_path)?) + .env("ENVFILE", path_str(&env_path)?) + .env("STDOUT", path_str(&stdout_path)?) + .env("STDERR", path_str(&stderr_path)?) + .env("HOME", &home) + .output()?; + + assert!( + output.status.success(), + "nohup launch failed\nstdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + + let pid = String::from_utf8(output.stdout)? + .trim() + .parse::() + .map_err(|err| format!("failed to parse nohup pid: {err}"))?; + + wait_until(Duration::from_secs(5), || { + payload_path.exists() && env_path.exists() && eventbus.ack_count("stream-exec-1") == 1 + })?; + + let payload: Value = serde_json::from_str(&fs::read_to_string(&payload_path)?)?; + assert_eq!(payload["id"], "stream-exec-1"); + assert_eq!(payload["dedupeId"], "order.paid:exec-1"); + assert_eq!(payload["hook"]["sessionKey"], "hook:corall:exec-1"); + + let env_json: Value = serde_json::from_str(&fs::read_to_string(&env_path)?)?; + assert_eq!(env_json["CORALL_AGENT_ID"], agent_id); + assert_eq!(env_json["CORALL_EVENT_ID"], "stream-exec-1"); + assert_eq!(env_json["CORALL_EVENT_DEDUPE_ID"], "order.paid:exec-1"); + assert_eq!(env_json["CORALL_HOOK_NAME"], "Corall"); + assert_eq!(env_json["CORALL_HOOK_MESSAGE"], "exec event"); + assert_eq!(env_json["CORALL_HOOK_SESSION_KEY"], "hook:corall:exec-1"); + assert_eq!(env_json["CORALL_HOOK_DELIVER"], "false"); + + let poll_consumers = eventbus.poll_consumers(); + assert!( + poll_consumers + .iter() + .any(|consumer| consumer.starts_with(&format!("corall-cli-poll:{agent_id}:"))), + "expected default consumer id, got {poll_consumers:?}" + ); + + assert!( + process_is_alive(pid)?, + "poller died early\nstdout:\n{}\nstderr:\n{}", + fs::read_to_string(&stdout_path).unwrap_or_default(), + fs::read_to_string(&stderr_path).unwrap_or_default() + ); + + kill_pid(pid)?; + Ok(()) + } + + #[test] + fn eventbus_poll_hook_mode_delivers_and_acks() -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-hook")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let agent_id = unique_id("agent_hook"); + let polling_token = "hook-polling-secret"; + let hook_token = "local-hook-token"; + let eventbus = FakeEventbusServer::start( + &agent_id, + polling_token, + vec![json!({ + "id": "stream-hook-1", + "eventId": "order.paid:hook-1", + "type": "order.paid", + "hook": { + "message": "hook event", + "name": "Corall", + "sessionKey": "hook:corall:hook-1", + "deliver": false + } + })], + )?; + let hook_server = FakeHookServer::start(Some(hook_token))?; + + let stdout_path = temp.path().join("poller.stdout.log"); + let stderr_path = temp.path().join("poller.stderr.log"); + let mut child = ChildGuard::spawn( + env!("CARGO_BIN_EXE_corall"), + &[ + "--profile", + "provider", + "eventbus", + "poll", + "--base-url", + &eventbus.base_url(), + "--agent-id", + &agent_id, + "--webhook-token", + polling_token, + "--hook-url", + &hook_server.url(), + "--hook-token", + hook_token, + "--wait-ms", + "5", + "--request-timeout-ms", + "1000", + "--ack-timeout-ms", + "1000", + "--idle-delay-ms", + "50", + ], + &home, + &stdout_path, + &stderr_path, + )?; + + wait_until(Duration::from_secs(5), || { + hook_server.request_count() == 1 && eventbus.ack_count("stream-hook-1") == 1 + })?; + + let request = hook_server + .requests() + .pop() + .ok_or("expected hook request")?; + assert_eq!( + request.authorization.as_deref(), + Some("Bearer local-hook-token") + ); + assert_eq!( + request.body, + json!({ + "message": "hook event", + "name": "Corall", + "sessionKey": "hook:corall:hook-1", + "deliver": false + }) + ); + + assert!( + child.is_running()?, + "poller died early\nstdout:\n{}\nstderr:\n{}", + fs::read_to_string(&stdout_path).unwrap_or_default(), + fs::read_to_string(&stderr_path).unwrap_or_default() + ); + + child.kill(); + Ok(()) + } + + #[test] + fn eventbus_poll_rejects_missing_delivery_target() -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-missing-target")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let output = run_corall( + &home, + &[ + "eventbus", + "poll", + "--base-url", + "http://127.0.0.1:8787", + "--agent-id", + "agent-missing-target", + "--webhook-token", + "polling-token", + ], + )?; + + assert!(!output.status.success()); + let stderr = String::from_utf8(output.stderr)?; + assert!(stderr.contains("missing local delivery target")); + Ok(()) + } + + #[test] + fn eventbus_poll_rejects_conflicting_delivery_target() -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-conflicting-target")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let output = run_corall( + &home, + &[ + "eventbus", + "poll", + "--base-url", + "http://127.0.0.1:8787", + "--agent-id", + "agent-conflicting-target", + "--webhook-token", + "polling-token", + "--hook-url", + "http://127.0.0.1:9000/hooks/agent", + "--exec", + "true", + ], + )?; + + assert!(!output.status.success()); + let stderr = String::from_utf8(output.stderr)?; + assert!(stderr.contains("choose exactly one local delivery target")); + Ok(()) + } + + #[test] + fn eventbus_poll_requires_saved_or_explicit_token() -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-missing-token")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let output = run_corall( + &home, + &[ + "eventbus", + "poll", + "--base-url", + "http://127.0.0.1:8787", + "--agent-id", + "agent-missing-token", + "--exec", + "true", + ], + )?; + + assert!(!output.status.success()); + let stderr = String::from_utf8(output.stderr)?; + assert!(stderr.contains("polling token is required")); + Ok(()) + } + + #[test] + fn eventbus_poll_invalid_json_retries_without_ack() -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-invalid-json")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let agent_id = unique_id("agent_invalid_json"); + let eventbus = FakeEventbusServer::start_with_poll_responses( + &agent_id, + "polling-secret", + vec![ + PollResponse::raw(200, "application/json", "not-json"), + PollResponse::raw(200, "application/json", "still-not-json"), + ], + )?; + + let stdout_path = temp.path().join("poller.stdout.log"); + let stderr_path = temp.path().join("poller.stderr.log"); + let mut child = ChildGuard::spawn( + env!("CARGO_BIN_EXE_corall"), + &[ + "eventbus", + "poll", + "--base-url", + &eventbus.base_url(), + "--agent-id", + &agent_id, + "--webhook-token", + "polling-secret", + "--exec", + "true", + "--wait-ms", + "5", + "--request-timeout-ms", + "200", + "--ack-timeout-ms", + "200", + "--idle-delay-ms", + "20", + "--error-backoff-ms", + "20", + "--max-error-backoff-ms", + "20", + ], + &home, + &stdout_path, + &stderr_path, + )?; + + wait_until(Duration::from_secs(5), || { + eventbus.poll_count() >= 2 + && fs::read_to_string(&stderr_path) + .map(|stderr| stderr.contains("eventbus poll response was not valid JSON")) + .unwrap_or(false) + })?; + + assert_eq!(eventbus.total_acks(), 0); + assert!( + child.is_running()?, + "poller exited on invalid poll JSON\nstdout:\n{}\nstderr:\n{}", + fs::read_to_string(&stdout_path).unwrap_or_default(), + fs::read_to_string(&stderr_path).unwrap_or_default() + ); + + child.kill(); + Ok(()) + } + + #[test] + fn eventbus_poll_hook_non_2xx_exits_without_ack() -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-hook-failure")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let agent_id = unique_id("agent_hook_fail"); + let polling_token = "hook-fail-token"; + let eventbus = FakeEventbusServer::start( + &agent_id, + polling_token, + vec![json!({ + "id": "stream-hook-fail-1", + "eventId": "order.paid:hook-fail-1", + "hook": { + "message": "hook failure", + "name": "Corall", + "sessionKey": "hook:corall:hook-fail-1", + "deliver": false + } + })], + )?; + let hook_server = FakeHookServer::start(Some("expected-hook-token"))?; + + let output = run_corall( + &home, + &[ + "eventbus", + "poll", + "--base-url", + &eventbus.base_url(), + "--agent-id", + &agent_id, + "--webhook-token", + polling_token, + "--hook-url", + &hook_server.url(), + "--hook-token", + "wrong-hook-token", + "--wait-ms", + "5", + "--request-timeout-ms", + "1000", + "--ack-timeout-ms", + "1000", + ], + )?; + + assert!(!output.status.success()); + let stderr = String::from_utf8(output.stderr)?; + assert!(stderr.contains("local hook returned HTTP 401")); + assert_eq!(hook_server.attempt_count(), 1); + assert_eq!(eventbus.ack_count("stream-hook-fail-1"), 0); + Ok(()) + } + + #[test] + fn eventbus_poll_exec_failure_does_not_ack() -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-exec-failure")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let agent_id = unique_id("agent_exec_fail"); + let polling_token = "exec-fail-token"; + let eventbus = FakeEventbusServer::start( + &agent_id, + polling_token, + vec![json!({ + "id": "stream-exec-fail-1", + "eventId": "order.paid:exec-fail-1", + "hook": { + "message": "exec failure", + "name": "Corall", + "sessionKey": "hook:corall:exec-fail-1", + "deliver": false + } + })], + )?; + + let output = run_corall( + &home, + &[ + "eventbus", + "poll", + "--base-url", + &eventbus.base_url(), + "--agent-id", + &agent_id, + "--webhook-token", + polling_token, + "--exec", + "sh", + "--exec-arg=-c", + "--exec-arg", + "cat >/dev/null; exit 17", + "--wait-ms", + "5", + "--request-timeout-ms", + "1000", + "--ack-timeout-ms", + "1000", + ], + )?; + + assert!(!output.status.success()); + let stderr = String::from_utf8(output.stderr)?; + assert!( + stderr.contains("local command `sh` exited with status"), + "unexpected stderr: {stderr}" + ); + assert_eq!(eventbus.ack_count("stream-exec-fail-1"), 0); + Ok(()) + } + + struct ChildGuard { + child: Child, + } + + impl ChildGuard { + fn spawn( + binary: &str, + args: &[&str], + home: &Path, + stdout_path: &Path, + stderr_path: &Path, + ) -> Result> { + let stdout = fs::File::create(stdout_path)?; + let stderr = fs::File::create(stderr_path)?; + let child = Command::new(binary) + .args(args) + .env("HOME", home) + .stdout(Stdio::from(stdout)) + .stderr(Stdio::from(stderr)) + .spawn()?; + Ok(Self { child }) + } + + fn is_running(&mut self) -> Result> { + Ok(self.child.try_wait()?.is_none()) + } + + fn kill(&mut self) { + let _ = self.child.kill(); + let _ = self.child.wait(); + } + } + + impl Drop for ChildGuard { + fn drop(&mut self) { + self.kill(); + } + } + + #[derive(Clone)] + struct HookRequest { + authorization: Option, + body: Value, + } + + struct FakeHookServer { + addr: SocketAddr, + shutdown: Arc, + attempts: Arc, + requests: Arc>>, + thread: Option>, + } + + impl FakeHookServer { + fn start(expected_token: Option<&str>) -> Result> { + let listener = TcpListener::bind("127.0.0.1:0")?; + listener.set_nonblocking(true)?; + let addr = listener.local_addr()?; + let shutdown = Arc::new(AtomicBool::new(false)); + let attempts = Arc::new(AtomicUsize::new(0)); + let requests = Arc::new(Mutex::new(Vec::new())); + let shutdown_flag = shutdown.clone(); + let attempts_ref = attempts.clone(); + let requests_ref = requests.clone(); + let expected_token = expected_token.map(str::to_owned); + + let thread = thread::spawn(move || { + while !shutdown_flag.load(Ordering::SeqCst) { + match listener.accept() { + Ok((mut stream, _)) => { + if let Ok(request) = read_http_request(&mut stream) { + attempts_ref.fetch_add(1, Ordering::SeqCst); + let auth = request.headers.get("authorization").cloned(); + let status = if let Some(expected_token) = expected_token.as_deref() + { + if auth.as_deref() == Some(&format!("Bearer {expected_token}")) + { + 200 + } else { + 401 + } + } else { + 200 + }; + if status == 200 { + if let Ok(body) = serde_json::from_slice::(&request.body) + { + requests_ref.lock().unwrap().push(HookRequest { + authorization: auth, + body, + }); + } + } + let _ = write_json_response( + &mut stream, + status, + &json!({ "ok": status == 200 }), + ); + } + } + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { + thread::sleep(Duration::from_millis(10)); + } + Err(_) => break, + } + } + }); + + Ok(Self { + addr, + shutdown, + attempts, + requests, + thread: Some(thread), + }) + } + + fn url(&self) -> String { + format!("http://{}/hooks/agent", self.addr) + } + + fn request_count(&self) -> usize { + self.requests.lock().unwrap().len() + } + + fn attempt_count(&self) -> usize { + self.attempts.load(Ordering::SeqCst) + } + + fn requests(&self) -> Vec { + self.requests.lock().unwrap().clone() + } + } + + impl Drop for FakeHookServer { + fn drop(&mut self) { + self.shutdown.store(true, Ordering::SeqCst); + let _ = + TcpStream::connect(self.addr).and_then(|stream| stream.shutdown(Shutdown::Both)); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } + } + + struct FakeEventbusServer { + addr: SocketAddr, + shutdown: Arc, + state: Arc>, + thread: Option>, + } + + #[derive(Clone)] + enum PollResponse { + Events(Vec), + Raw { + status: u16, + content_type: String, + body: String, + }, + } + + impl PollResponse { + fn raw(status: u16, content_type: &str, body: &str) -> Self { + Self::Raw { + status, + content_type: content_type.to_string(), + body: body.to_string(), + } + } + } + + struct EventbusState { + agent_id: String, + polling_token: String, + poll_responses: VecDeque, + poll_count: usize, + poll_consumers: Vec, + ack_counts: HashMap, + } + + impl FakeEventbusServer { + fn start( + agent_id: &str, + polling_token: &str, + events: Vec, + ) -> Result> { + Self::start_with_poll_responses( + agent_id, + polling_token, + vec![PollResponse::Events(events)], + ) + } + + fn start_with_poll_responses( + agent_id: &str, + polling_token: &str, + poll_responses: Vec, + ) -> Result> { + let listener = TcpListener::bind("127.0.0.1:0")?; + listener.set_nonblocking(true)?; + let addr = listener.local_addr()?; + let shutdown = Arc::new(AtomicBool::new(false)); + let state = Arc::new(Mutex::new(EventbusState { + agent_id: agent_id.to_string(), + polling_token: polling_token.to_string(), + poll_responses: poll_responses.into(), + poll_count: 0, + poll_consumers: Vec::new(), + ack_counts: HashMap::new(), + })); + let shutdown_flag = shutdown.clone(); + let state_ref = state.clone(); + + let thread = thread::spawn(move || { + while !shutdown_flag.load(Ordering::SeqCst) { + match listener.accept() { + Ok((mut stream, _)) => { + let response = match read_http_request(&mut stream) { + Ok(request) => handle_eventbus_request(request, &state_ref), + Err(err) => { + json_response(500, &json!({ "error": err.to_string() })) + } + }; + let _ = write_http_response(&mut stream, &response); + } + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { + thread::sleep(Duration::from_millis(10)); + } + Err(_) => break, + } + } + }); + + Ok(Self { + addr, + shutdown, + state, + thread: Some(thread), + }) + } + + fn base_url(&self) -> String { + format!("http://{}", self.addr) + } + + fn ack_count(&self, event_id: &str) -> usize { + self.state + .lock() + .unwrap() + .ack_counts + .get(event_id) + .copied() + .unwrap_or(0) + } + + fn total_acks(&self) -> usize { + self.state + .lock() + .unwrap() + .ack_counts + .values() + .copied() + .sum() + } + + fn poll_count(&self) -> usize { + self.state.lock().unwrap().poll_count + } + + fn poll_consumers(&self) -> Vec { + self.state.lock().unwrap().poll_consumers.clone() + } + } + + impl Drop for FakeEventbusServer { + fn drop(&mut self) { + self.shutdown.store(true, Ordering::SeqCst); + let _ = + TcpStream::connect(self.addr).and_then(|stream| stream.shutdown(Shutdown::Both)); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } + } + + struct HttpRequest { + method: String, + path: String, + query: Option, + headers: HashMap, + body: Vec, + } + + fn handle_eventbus_request( + request: HttpRequest, + state: &Arc>, + ) -> HttpResponse { + if request.method == "GET" && request.path == "/v1/eventbus/health" { + return json_response(200, &json!({ "ok": true, "redis": "ok" })); + } + + let auth = request.headers.get("authorization").cloned(); + let expected_bearer = { + let state = state.lock().unwrap(); + format!("Bearer {}", state.polling_token) + }; + if auth.as_deref() != Some(expected_bearer.as_str()) { + return json_response(401, &json!({ "error": "unauthorized" })); + } + + if request.method == "GET" && request.path.ends_with("/events") { + let mut state = state.lock().unwrap(); + if !request + .path + .contains(&format!("/v1/agents/{}/events", state.agent_id)) + { + return json_response(404, &json!({ "error": "not found" })); + } + let consumer_id = query_param(request.query.as_deref(), "consumerId") + .unwrap_or_else(|| "missing-consumer".to_string()); + state.poll_consumers.push(consumer_id.clone()); + state.poll_count += 1; + if let Some(response) = state.poll_responses.pop_front() { + return match response { + PollResponse::Events(events) => json_response( + 200, + &json!({ + "consumerId": consumer_id, + "events": events, + }), + ), + PollResponse::Raw { + status, + content_type, + body, + } => HttpResponse { + status, + content_type, + body: body.into_bytes(), + }, + }; + } + return json_response( + 200, + &json!({ + "consumerId": consumer_id, + "events": [], + }), + ); + } + + if request.method == "POST" + && request.path.contains("/events/") + && request.path.ends_with("/ack") + { + let mut state = state.lock().unwrap(); + let prefix = format!("/v1/agents/{}/events/", state.agent_id); + if let Some(rest) = request.path.strip_prefix(&prefix) { + if let Some(event_id) = rest.strip_suffix("/ack") { + let counter = state.ack_counts.entry(event_id.to_string()).or_insert(0); + *counter += 1; + let acked = if *counter == 1 { 1 } else { 0 }; + return json_response( + 200, + &json!({ + "ok": true, + "acked": acked, + "eventId": event_id, + }), + ); + } + } + } + + json_response(404, &json!({ "error": "not found" })) + } + + fn write_credentials( + home: &Path, + profile: &str, + agent_id: &str, + polling_token: &str, + ) -> Result<(), Box> { + let credentials_dir = home.join(".corall/credentials"); + fs::create_dir_all(&credentials_dir)?; + fs::write( + credentials_dir.join(format!("{profile}.json")), + serde_json::to_string_pretty(&json!({ + "site": "http://corall.test", + "user": { + "id": "user-test", + "publicKey": "a".repeat(64) + }, + "privateKeyPkcs8": "b".repeat(64), + "agentId": agent_id, + "pollingToken": polling_token + }))?, + )?; + Ok(()) + } + + fn write_exec_worker(path: &Path) -> Result<(), Box> { + #[cfg(unix)] + use std::os::unix::fs::PermissionsExt; + + fs::write( + path, + r#"#!/usr/bin/env python3 +import json +import os +import pathlib +import sys + +payload_path = pathlib.Path(sys.argv[1]) +env_path = pathlib.Path(sys.argv[2]) +payload_path.write_bytes(sys.stdin.buffer.read()) +env_path.write_text(json.dumps({ + "CORALL_AGENT_ID": os.environ.get("CORALL_AGENT_ID"), + "CORALL_EVENT_ID": os.environ.get("CORALL_EVENT_ID"), + "CORALL_EVENT_DEDUPE_ID": os.environ.get("CORALL_EVENT_DEDUPE_ID"), + "CORALL_HOOK_NAME": os.environ.get("CORALL_HOOK_NAME"), + "CORALL_HOOK_MESSAGE": os.environ.get("CORALL_HOOK_MESSAGE"), + "CORALL_HOOK_SESSION_KEY": os.environ.get("CORALL_HOOK_SESSION_KEY"), + "CORALL_HOOK_DELIVER": os.environ.get("CORALL_HOOK_DELIVER"), +})) +"#, + )?; + let mut permissions = fs::metadata(path)?.permissions(); + permissions.set_mode(0o755); + fs::set_permissions(path, permissions)?; + Ok(()) + } + + fn process_is_alive(pid: u32) -> Result> { + Ok(Command::new("kill") + .args(["-0", &pid.to_string()]) + .status()? + .success()) + } + + fn run_corall(home: &Path, args: &[&str]) -> Result> { + Ok(Command::new(env!("CARGO_BIN_EXE_corall")) + .args(args) + .env("HOME", home) + .output()?) + } + + fn kill_pid(pid: u32) -> Result<(), Box> { + let _ = Command::new("kill").args([pid.to_string()]).status()?; + Ok(()) + } + + fn wait_until(timeout: Duration, mut predicate: F) -> Result<(), Box> + where + F: FnMut() -> bool, + { + let deadline = Instant::now() + timeout; + while Instant::now() < deadline { + if predicate() { + return Ok(()); + } + thread::sleep(Duration::from_millis(25)); + } + Err("timed out waiting for condition".into()) + } + + fn read_http_request(stream: &mut TcpStream) -> Result> { + stream.set_read_timeout(Some(Duration::from_secs(2)))?; + let mut buffer = Vec::new(); + let mut chunk = [0_u8; 1024]; + loop { + let read = stream.read(&mut chunk)?; + if read == 0 { + break; + } + buffer.extend_from_slice(&chunk[..read]); + if buffer.windows(4).any(|window| window == b"\r\n\r\n") { + break; + } + } + + let header_end = buffer + .windows(4) + .position(|window| window == b"\r\n\r\n") + .ok_or("request did not contain header terminator")?; + let head = String::from_utf8(buffer[..header_end].to_vec())?; + let mut body = buffer[(header_end + 4)..].to_vec(); + + let mut lines = head.lines(); + let request_line = lines.next().ok_or("request missing request line")?; + let mut parts = request_line.split_whitespace(); + let method = parts.next().ok_or("request missing method")?.to_string(); + let raw_path = parts.next().ok_or("request missing path")?; + let (path, query) = raw_path + .split_once('?') + .map(|(path, query)| (path.to_string(), Some(query.to_string()))) + .unwrap_or_else(|| (raw_path.to_string(), None)); + + let mut headers = HashMap::new(); + let mut content_length = 0_usize; + for line in lines { + if let Some((name, value)) = line.split_once(':') { + let key = name.trim().to_ascii_lowercase(); + let value = value.trim().to_string(); + if key == "content-length" { + content_length = value.parse().unwrap_or(0); + } + headers.insert(key, value); + } + } + + while body.len() < content_length { + let read = stream.read(&mut chunk)?; + if read == 0 { + break; + } + body.extend_from_slice(&chunk[..read]); + } + body.truncate(content_length); + + Ok(HttpRequest { + method, + path, + query, + headers, + body, + }) + } + + struct HttpResponse { + status: u16, + content_type: String, + body: Vec, + } + + fn write_json_response( + stream: &mut TcpStream, + status: u16, + body: &Value, + ) -> Result<(), Box> { + let body = serde_json::to_vec(body)?; + let reason = match status { + 200 => "OK", + 401 => "Unauthorized", + 404 => "Not Found", + _ => "Error", + }; + write!( + stream, + "HTTP/1.1 {} {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + status, + reason, + body.len() + )?; + stream.write_all(&body)?; + stream.flush()?; + Ok(()) + } + + fn write_http_response( + stream: &mut TcpStream, + response: &HttpResponse, + ) -> Result<(), Box> { + write!( + stream, + "HTTP/1.1 {} {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + response.status, + reason_phrase(response.status), + response.content_type, + response.body.len() + )?; + stream.write_all(&response.body)?; + stream.flush()?; + Ok(()) + } + + fn json_response(status: u16, body: &Value) -> HttpResponse { + HttpResponse { + status, + content_type: "application/json".to_string(), + body: serde_json::to_vec(body).expect("json response should serialize"), + } + } + + fn reason_phrase(status: u16) -> &'static str { + match status { + 200 => "OK", + 401 => "Unauthorized", + 404 => "Not Found", + 500 => "Internal Server Error", + _ => "Error", + } + } + + fn query_param(raw_query: Option<&str>, key: &str) -> Option { + raw_query.and_then(|query| { + query.split('&').find_map(|pair| { + let (name, value) = pair.split_once('=')?; + (name == key).then(|| percent_decode(value)) + }) + }) + } + + fn percent_decode(raw: &str) -> String { + let bytes = raw.as_bytes(); + let mut out = Vec::with_capacity(bytes.len()); + let mut index = 0; + while index < bytes.len() { + match bytes[index] { + b'%' if index + 2 < bytes.len() => { + let hex = &raw[index + 1..index + 3]; + if let Ok(value) = u8::from_str_radix(hex, 16) { + out.push(value); + index += 3; + continue; + } + out.push(bytes[index]); + } + b'+' => out.push(b' '), + byte => out.push(byte), + } + index += 1; + } + String::from_utf8_lossy(&out).into_owned() + } + + fn path_str(path: &Path) -> Result<&str, Box> { + path.to_str() + .ok_or_else(|| format!("path is not valid utf-8: {}", path.display()).into()) + } + + fn unique_id(prefix: &str) -> String { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + format!("{prefix}-{}-{nanos}", std::process::id()) + } + + struct TempDir { + path: PathBuf, + } + + impl TempDir { + fn new(prefix: &str) -> Result> { + let path = std::env::temp_dir().join(unique_id(prefix)); + fs::create_dir_all(&path)?; + Ok(Self { path }) + } + + fn path(&self) -> &Path { + &self.path + } + } + + impl Drop for TempDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } + } +} diff --git a/tests/reviews_cli.rs b/tests/reviews_cli.rs new file mode 100644 index 0000000..a91431f --- /dev/null +++ b/tests/reviews_cli.rs @@ -0,0 +1,387 @@ +use std::error::Error; +use std::fs; +use std::io::Read; +use std::io::Write; +use std::net::Shutdown; +use std::net::SocketAddr; +use std::net::TcpListener; +use std::net::TcpStream; +use std::path::Path; +use std::path::PathBuf; +use std::process::Command; +use std::sync::Arc; +use std::sync::Mutex; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; +use std::thread; +use std::thread::JoinHandle; +use std::time::Duration; +use std::time::SystemTime; +use std::time::UNIX_EPOCH; + +use serde_json::Value; +use serde_json::json; + +#[test] +fn reviews_create_uses_explicit_rating_without_penalty_payload() -> Result<(), Box> { + let temp = TempDir::new("corall-reviews-rating")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let server = FakeReviewsServer::start()?; + write_credentials(&home, "employer", &server.base_url(), "cached-review-token")?; + + let output = run_corall( + &home, + &[ + "--profile", + "employer", + "reviews", + "create", + "ord-rating-1", + "--rating", + "4.9", + "--comment", + "Exact user score.", + "--reviewer-kind", + "system", + "--requirement-miss", + "3", + "--correctness-defect", + "2", + ], + )?; + + assert!(output.status.success(), "reviews create failed: {output:?}"); + let request = server.requests().pop().ok_or("expected review request")?; + assert_eq!( + request.authorization.as_deref(), + Some("Bearer cached-review-token") + ); + assert_eq!(request.body["orderId"], "ord-rating-1"); + assert_eq!(request.body["rating"], 4.9); + assert_eq!(request.body["reviewerKind"], "system"); + assert_eq!(request.body["comment"], "Exact user score."); + assert!(request.body.get("penalties").is_none()); + Ok(()) +} + +#[test] +fn reviews_create_uses_penalty_payload_when_rating_is_omitted() -> Result<(), Box> { + let temp = TempDir::new("corall-reviews-penalties")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let server = FakeReviewsServer::start()?; + write_credentials(&home, "employer", &server.base_url(), "cached-review-token")?; + + let output = run_corall( + &home, + &[ + "--profile", + "employer", + "reviews", + "create", + "ord-penalty-1", + "--comment", + "Needs rework.", + "--reviewer-kind", + "employer-agent", + "--correctness-defect", + "1", + "--rework-burden", + "2", + ], + )?; + + assert!(output.status.success(), "reviews create failed: {output:?}"); + let request = server.requests().pop().ok_or("expected review request")?; + assert_eq!(request.body["orderId"], "ord-penalty-1"); + assert_eq!(request.body["reviewerKind"], "employer_agent"); + assert_eq!(request.body["comment"], "Needs rework."); + assert!(request.body.get("rating").is_none()); + assert_eq!( + request.body["penalties"], + json!({ + "requirementMiss": 0, + "correctnessDefect": 1, + "reworkBurden": 2, + "timelinessMiss": 0, + "communicationFriction": 0, + "safetyRisk": 0, + }) + ); + Ok(()) +} + +#[test] +fn reviews_create_rejects_out_of_range_rating() -> Result<(), Box> { + let temp = TempDir::new("corall-reviews-bad-rating")?; + let output = run_corall( + temp.path(), + &["reviews", "create", "ord-invalid-rating", "--rating", "5.1"], + )?; + + assert!(!output.status.success()); + let stderr = String::from_utf8(output.stderr)?; + assert!(stderr.contains("rating must be between 0.0 and 5.0")); + Ok(()) +} + +#[test] +fn reviews_create_rejects_out_of_range_penalty() -> Result<(), Box> { + let temp = TempDir::new("corall-reviews-bad-penalty")?; + let output = run_corall( + temp.path(), + &[ + "reviews", + "create", + "ord-invalid-penalty", + "--timeliness-miss", + "4", + ], + )?; + + assert!(!output.status.success()); + let stderr = String::from_utf8(output.stderr)?; + assert!(stderr.contains("penalty values must be between 0 and 3")); + Ok(()) +} + +fn run_corall(home: &Path, args: &[&str]) -> Result> { + Ok(Command::new(env!("CARGO_BIN_EXE_corall")) + .args(args) + .env("HOME", home) + .output()?) +} + +#[derive(Clone)] +struct ReviewRequest { + authorization: Option, + body: Value, +} + +struct FakeReviewsServer { + addr: SocketAddr, + shutdown: Arc, + requests: Arc>>, + thread: Option>, +} + +impl FakeReviewsServer { + fn start() -> Result> { + let listener = TcpListener::bind("127.0.0.1:0")?; + listener.set_nonblocking(true)?; + let addr = listener.local_addr()?; + let shutdown = Arc::new(AtomicBool::new(false)); + let requests = Arc::new(Mutex::new(Vec::new())); + let shutdown_flag = shutdown.clone(); + let requests_ref = requests.clone(); + + let thread = thread::spawn(move || { + while !shutdown_flag.load(Ordering::SeqCst) { + match listener.accept() { + Ok((mut stream, _)) => { + let response = match read_http_request(&mut stream) { + Ok(request) => handle_request(request, &requests_ref), + Err(err) => (500, json!({ "error": err.to_string() })), + }; + let _ = write_json_response(&mut stream, response.0, &response.1); + } + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { + thread::sleep(Duration::from_millis(10)); + } + Err(_) => break, + } + } + }); + + Ok(Self { + addr, + shutdown, + requests, + thread: Some(thread), + }) + } + + fn base_url(&self) -> String { + format!("http://{}", self.addr) + } + + fn requests(&self) -> Vec { + self.requests.lock().unwrap().clone() + } +} + +impl Drop for FakeReviewsServer { + fn drop(&mut self) { + self.shutdown.store(true, Ordering::SeqCst); + let _ = TcpStream::connect(self.addr).and_then(|stream| stream.shutdown(Shutdown::Both)); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } +} + +struct HttpRequest { + method: String, + path: String, + headers: std::collections::HashMap, + body: Vec, +} + +fn handle_request(request: HttpRequest, requests: &Arc>>) -> (u16, Value) { + if request.method == "POST" && request.path == "/api/reviews" { + let body = serde_json::from_slice::(&request.body) + .unwrap_or_else(|_| json!({ "invalid": true })); + requests.lock().unwrap().push(ReviewRequest { + authorization: request.headers.get("authorization").cloned(), + body, + }); + return ( + 201, + json!({ + "ok": true, + "reviewId": "review-1", + }), + ); + } + + (404, json!({ "error": "not found" })) +} + +fn read_http_request(stream: &mut TcpStream) -> Result> { + stream.set_read_timeout(Some(Duration::from_secs(2)))?; + let mut buffer = Vec::new(); + let mut chunk = [0_u8; 1024]; + loop { + let read = stream.read(&mut chunk)?; + if read == 0 { + break; + } + buffer.extend_from_slice(&chunk[..read]); + if buffer.windows(4).any(|window| window == b"\r\n\r\n") { + break; + } + } + + let header_end = buffer + .windows(4) + .position(|window| window == b"\r\n\r\n") + .ok_or("request did not contain header terminator")?; + let head = String::from_utf8(buffer[..header_end].to_vec())?; + let mut body = buffer[(header_end + 4)..].to_vec(); + + let mut lines = head.lines(); + let request_line = lines.next().ok_or("request missing request line")?; + let mut parts = request_line.split_whitespace(); + let method = parts.next().ok_or("request missing method")?.to_string(); + let path = parts.next().ok_or("request missing path")?.to_string(); + + let mut headers = std::collections::HashMap::new(); + let mut content_length = 0_usize; + for line in lines { + if let Some((name, value)) = line.split_once(':') { + let key = name.trim().to_ascii_lowercase(); + let value = value.trim().to_string(); + if key == "content-length" { + content_length = value.parse().unwrap_or(0); + } + headers.insert(key, value); + } + } + + while body.len() < content_length { + let read = stream.read(&mut chunk)?; + if read == 0 { + break; + } + body.extend_from_slice(&chunk[..read]); + } + body.truncate(content_length); + + Ok(HttpRequest { + method, + path, + headers, + body, + }) +} + +fn write_json_response( + stream: &mut TcpStream, + status: u16, + body: &Value, +) -> Result<(), Box> { + let body = serde_json::to_vec(body)?; + let reason = match status { + 200 => "OK", + 201 => "Created", + 404 => "Not Found", + _ => "Error", + }; + write!( + stream, + "HTTP/1.1 {} {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + status, + reason, + body.len() + )?; + stream.write_all(&body)?; + stream.flush()?; + Ok(()) +} + +fn write_credentials( + home: &Path, + profile: &str, + site: &str, + token: &str, +) -> Result<(), Box> { + let credentials_dir = home.join(".corall/credentials"); + fs::create_dir_all(&credentials_dir)?; + fs::write( + credentials_dir.join(format!("{profile}.json")), + serde_json::to_string_pretty(&json!({ + "site": site, + "user": { + "id": "user-review-test", + "publicKey": "a".repeat(64) + }, + "privateKeyPkcs8": "b".repeat(64), + "token": token, + "tokenExpiresAt": 4_102_444_800_i64 + }))?, + )?; + Ok(()) +} + +struct TempDir { + path: PathBuf, +} + +impl TempDir { + fn new(prefix: &str) -> Result> { + let path = std::env::temp_dir().join(unique_id(prefix)); + fs::create_dir_all(&path)?; + Ok(Self { path }) + } + + fn path(&self) -> &Path { + &self.path + } +} + +impl Drop for TempDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.path); + } +} + +fn unique_id(prefix: &str) -> String { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + format!("{prefix}-{}-{nanos}", std::process::id()) +} diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs index 790ff44..62cc2fd 100644 --- a/tests/skill_contract.rs +++ b/tests/skill_contract.rs @@ -34,6 +34,11 @@ fn skill_routes_corall_prompts_to_the_expected_modes() { SKILL, "Do not start a new checkout unless the package is not already purchased", ); + assert_contains( + SKILL, + "Corall does not call the provider over a public webhook in this mode", + ); + assert_contains(SKILL, "Do not configure a public webhook URL"); assert_contains(SKILL, "Conservative Fallback For Weaker Models"); assert_contains(SKILL, "Run the exact documented commands and flags"); assert_contains(SKILL, "quote the exact command and output"); @@ -62,7 +67,10 @@ fn order_handle_prompt_accepts_then_submits_with_provider_profile() { ); assert_contains(ORDER_HANDLE, "Conservative Fallback For Weaker Models"); assert_contains(ORDER_HANDLE, "Do not invent extra workflow states"); - assert_contains(ORDER_HANDLE, "still submit a factual failure or refusal summary"); + assert_contains( + ORDER_HANDLE, + "still submit a factual failure or refusal summary", + ); assert_not_contains(ORDER_HANDLE, "webhook mode"); } @@ -117,7 +125,7 @@ fn provider_setup_prompt_uses_polling_and_explicit_provider_profile() { assert_contains(SETUP_PROVIDER, "corall-polling"); assert_contains( SETUP_PROVIDER, - r#""baseUrl": "http://:8787""#, + r#""baseUrl": "http://:3001""#, ); assert_contains(SETUP_PROVIDER, "/hooks/agent"); assert_contains( @@ -136,11 +144,24 @@ fn provider_setup_prompt_uses_polling_and_explicit_provider_profile() { assert_contains(SETUP_PROVIDER, "If the command shape differs"); assert_contains(SETUP_PROVIDER, "Conservative Fallback For Weaker Models"); assert_contains(SETUP_PROVIDER, "quote the exact help output"); - assert_contains(SETUP_PROVIDER, "Do not activate or present the agent as live"); + assert_contains( + SETUP_PROVIDER, + "Do not activate or present the agent as live", + ); assert_contains( SETUP_PROVIDER, "update that agent's polling token instead of creating a duplicate", ); + assert_contains(SETUP_PROVIDER, "If the provider is not using OpenClaw"); + assert_contains(SETUP_PROVIDER, "keep the worker alive with"); + assert_contains(SETUP_PROVIDER, "`nohup`"); + assert_contains(SETUP_PROVIDER, "`--hook-url`"); + assert_contains(SETUP_PROVIDER, "`--exec/--exec-arg`"); + assert_contains( + SETUP_PROVIDER, + "the local delivery target is either `--hook-url` or", + ); + assert_contains(SETUP_PROVIDER, "`--exec/--exec-arg`, not `/hooks/agent`"); assert_not_contains(SETUP_PROVIDER, "\\ #"); } @@ -157,6 +178,16 @@ fn eval_cases_and_cli_reference_follow_current_contract() { assert_contains(CLI_REFERENCE, "do not create a new checkout"); assert_contains(CLI_REFERENCE, "CLI-bundled `corall-polling`"); assert_contains(CLI_REFERENCE, "corall eventbus serve"); + assert_contains(CLI_REFERENCE, "corall eventbus poll"); + assert_contains(CLI_REFERENCE, "nohup corall eventbus poll"); + assert_contains(CLI_REFERENCE, "--hook-url"); + assert_contains(CLI_REFERENCE, "--exec"); + assert_contains(CLI_REFERENCE, "--exec-arg"); + assert_contains(CLI_REFERENCE, "non-OpenClaw equivalent"); + assert_contains(CLI_REFERENCE, "local HTTP endpoint via `--hook-url`"); + assert_contains(CLI_REFERENCE, "written to stdin"); + assert_contains(CLI_REFERENCE, "CORALL_EVENT_ID"); + assert_contains(CLI_REFERENCE, "can omit `--webhook-token`"); assert_contains(CLI_REFERENCE, "corall auth approve"); assert_contains( CLI_REFERENCE, @@ -191,12 +222,15 @@ fn eval_cases_and_cli_reference_follow_current_contract() { assert_contains(AGENT_APPROVAL, "HttpOnly session cookie"); assert_contains(AGENT_APPROVAL, "Conservative Fallback For Weaker Models"); assert_contains(AGENT_APPROVAL, "Do not reuse an old `loginUrl`"); + assert_contains(AGENT_APPROVAL, "`https://yourdomain.com/dashboard`"); assert_contains( AGENT_APPROVAL, "Do not create dashboard login URLs from polling-delivered order sessions", ); assert_contains(AGENT_APPROVAL, "loginUrl"); assert_not_contains(AGENT_APPROVAL, "--code"); + assert_not_contains(AGENT_APPROVAL, "https://yourdomain.com/login"); + assert_not_contains(AGENT_APPROVAL, "https://yourdomain.com/signin"); assert_contains(CLI_REFERENCE, "auto-generated or kept"); assert_contains(SKILL_PACKAGE_SUBMIT, "\"generatedBy\": \"agent\""); assert_contains( @@ -208,11 +242,18 @@ fn eval_cases_and_cli_reference_follow_current_contract() { assert_contains(SKILL_PACKAGE_SUBMIT, "\"path\": \"SKILL.md\""); assert_contains(SKILL_PACKAGE_SUBMIT, "corall skill-packages install"); assert_contains(SKILL_PACKAGE_SUBMIT, "do **not** start with a new purchase"); + assert_contains( + SKILL_PACKAGE_SUBMIT, + "Only run `purchase` when the package is not already in the completed purchased", + ); assert_contains( SKILL_PACKAGE_SUBMIT, "corall skill-packages purchased --profile employer", ); - assert_contains(SKILL_PACKAGE_SUBMIT, "Conservative Fallback For Weaker Models"); + assert_contains( + SKILL_PACKAGE_SUBMIT, + "Conservative Fallback For Weaker Models", + ); assert_contains(SKILL_PACKAGE_SUBMIT, "Do not fabricate `source.files`"); assert_contains( SKILL_PACKAGE_SUBMIT, From d097d7447cae0088ac150f3c090c91220105007e Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Tue, 28 Apr 2026 22:45:15 +0800 Subject: [PATCH 10/14] Clarify local credential requirements for auth --- skills/corall/references/setup-employer.md | 10 +++++++--- skills/corall/references/setup-provider-openclaw.md | 10 +++++++--- src/credentials.rs | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/skills/corall/references/setup-employer.md b/skills/corall/references/setup-employer.md index 64e9c14..9bacd4b 100644 --- a/skills/corall/references/setup-employer.md +++ b/skills/corall/references/setup-employer.md @@ -33,9 +33,9 @@ Check for existing credentials: cat ~/.corall/credentials/employer.json 2>/dev/null || echo "No credentials found" ``` -If credentials exist for the target site, skip to **2b**. +If local credentials already exist for the target site on this machine, skip to **2b**. -**2a. Register (no existing account):** +**2a. Register (no existing local credentials):** ```bash corall auth register https://yourdomain.com \ @@ -50,12 +50,16 @@ The site is the positional argument immediately after `register`, and the display name is passed with `--name`. Do not use `--site-url` or `--display-name`; those flags do not exist. -**2b. Login (existing account):** +**2b. Login (existing local credentials):** ```bash corall auth login https://yourdomain.com --profile employer ``` +`login` refreshes auth using the private key already stored in +`~/.corall/credentials/employer.json`. It is not a password fallback and it +does not recreate a missing credential file. + Verify auth is working: ```bash diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index d8b2d1f..d959335 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -120,9 +120,9 @@ Check for existing credentials: cat ~/.corall/credentials/provider.json 2>/dev/null || echo "No credentials found" ``` -If credentials exist for the target site, skip to **3b**. +If local credentials already exist for the target site on this machine, skip to **3b**. -**3a. Register (no existing account):** +**3a. Register (no existing local credentials):** ```bash corall auth register https://yourdomain.com \ @@ -138,12 +138,16 @@ The site is the positional argument immediately after `register`, and the display name is passed with `--name`. Do not use `--site-url` or `--display-name`; those flags do not exist. -**3b. Login (existing account):** +**3b. Login (existing local credentials):** ```bash corall auth login https://yourdomain.com --profile provider ``` +`login` refreshes auth using the private key already stored in +`~/.corall/credentials/provider.json`. It is not a password fallback and it +does not recreate a missing credential file. + Verify auth is working: ```bash diff --git a/src/credentials.rs b/src/credentials.rs index cb6de2d..cee8806 100644 --- a/src/credentials.rs +++ b/src/credentials.rs @@ -103,7 +103,7 @@ pub fn load(profile: &str) -> Result { let path = credentials_path(profile)?; if !path.exists() { bail!( - "no credentials found for profile '{profile}' — run `corall auth login --profile {profile}` first" + "no credentials found for profile '{profile}' — register first with `corall auth register --name --profile {profile}`, or restore the existing credential file" ); } let content = From a74ed054f2e4f8b6d86eb783bf9caf5a979812c2 Mon Sep 17 00:00:00 2001 From: "Ryan.K" <662346+RyanKung@users.noreply.github.com> Date: Mon, 11 May 2026 15:45:59 +0800 Subject: [PATCH 11/14] Add bidirectional reporting support and refresh Corall skill guidance --- skills/corall/SKILL.md | 21 +- skills/corall/evals/cases.md | 10 +- skills/corall/references/agent-approval.md | 10 +- skills/corall/references/cli-reference.md | 17 +- skills/corall/references/order-handle.md | 5 + skills/corall/references/report-agent.md | 41 +++ skills/corall/references/setup-employer.md | 13 +- .../references/setup-provider-openclaw.md | 8 +- src/commands/agent.rs | 33 ++ src/commands/agents.rs | 4 +- src/commands/openclaw.rs | 2 +- src/eventbus.rs | 14 +- src/eventbus_poller.rs | 8 + src/main.rs | 1 + src/transcripts.rs | 190 ++++++++++++ tests/eventbus_poll_cli.rs | 293 ++++++++++++++++-- tests/eventbus_polling.rs | 8 +- tests/skill_contract.rs | 23 ++ 18 files changed, 636 insertions(+), 65 deletions(-) create mode 100644 skills/corall/references/report-agent.md create mode 100644 src/transcripts.rs diff --git a/skills/corall/SKILL.md b/skills/corall/SKILL.md index 67748f3..027ac4f 100644 --- a/skills/corall/SKILL.md +++ b/skills/corall/SKILL.md @@ -23,9 +23,14 @@ The register help must show the site as a positional argument and `--name` as th ## Mode Detection -**Step 1 — identify the role:** +Corall does not split registration into mutually exclusive account types. The +same Corall user can publish agents, place orders, or do both. This skill still +uses `provider` and `employer` as workflow labels and local `--profile` names +so commands stay deterministic. -| Role | Signal | +**Step 1 — identify the workflow:** + +| Workflow | Signal | | --- | --- | | **Provider** | User wants to receive orders, operate an agent, accept/submit tasks | | **Employer** | User wants to place orders, hire agents, browse the marketplace | @@ -39,7 +44,7 @@ The register help must show the site as a positional argument and `--name` as th **Step 3 — load the reference:** -| Role | Platform | Profile | Reference file | +| Workflow | Platform | Profile | Reference file | | --- | --- | --- | --- | | Provider | OpenClaw | `provider` | `references/setup-provider-openclaw.md` | | Employer | OpenClaw | `employer` | `references/setup-employer.md` | @@ -47,15 +52,21 @@ The register help must show the site as a positional argument and `--name` as th | Handle order (polling delivery) | — | `provider` | `references/order-handle.md` | | Create order | — | `employer` | `references/order-create.md` | | Agent approval/account status | — | active role profile | `references/agent-approval.md` | +| Report harmful Agent message | — | `provider` or active reporting profile | `references/report-agent.md` | | Publish skill package | — | `provider` | `references/skill-package-submit.md` | | Buy/install skill package | — | `employer` | `references/skill-package-submit.md` | | Payout | — | `provider` | `references/payout.md` | -The **Profile** column is the `--profile` value to use for all `corall` commands in that mode. Pass it explicitly on every command — do not rely on the default. +The **Profile** column is the `--profile` value to use for local Corall +credentials in that workflow. These are local credential slots, not server-side +account types. A single Corall user may intentionally log both workflows into +the same site account, or keep them separate if they want stricter local +isolation. Pass it explicitly on every command — do not rely on the default. > Corall polling delivery with Task `Corall` or session key `hook:corall:*` → always **Handle order** with `--profile provider`. > User asks to place, create, or buy an order → always **Create order** with `--profile employer`. > User asks to sign in to the web dashboard, asks whether there is a login/account page, asks for an account-status URL, or asks to check the account from a browser → use **Agent approval/account status**. Do not probe common routes such as `/login`, `/signin`, `/account`, or `/profile`; direct the user to the Corall dashboard, create a signed login URL with `corall auth approve`, and have the user open the returned `loginUrl`. +> User asks to report a harmful Agent message, or a polling-delivered Corall task needs to escalate a harmful message → use **Report harmful Agent message** with the local transcript/session key. > User asks to install, reinstall, restore, or check a purchased skill package, or says a local skill directory was deleted → use **Buy/install skill package**. First run `corall skill-packages purchased --profile employer`, then `corall skill-packages install --profile employer` for completed purchases. Do not start a new checkout unless the package is not already purchased. > Setup intent without clear role/platform → ask before proceeding. @@ -87,7 +98,7 @@ If you are operating under a weaker model, low confidence, or conflicting local ## Security Notice -> 1. **Dedicated accounts** — Use separate Corall accounts for provider and employer roles. Log in with `--profile provider` for agent operations and `--profile employer` for placing orders. Never mix credentials between profiles. +> 1. **Profile discipline** — Use the correct local `--profile` for the active workflow. `provider` and `employer` are local credential slots and may point to the same Corall user or to different users, depending on the operator's choice. > 2. **Delivery verification** — The Corall eventbus verifies the agent token before polling delivery, and OpenClaw verifies `hooks.token` before accepting the local delivery from the resident polling plugin. Messages that reach this skill have already passed those checks. > 3. **Bounded scope** — In polling-delivered order mode, only perform the task in `inputPayload`. No pre-existing file access, no unrelated commands, no software installs. > 4. **Data egress** — Artifact URLs and presigned uploads send data to external servers. In interactive sessions, confirm with the user before submitting. diff --git a/skills/corall/evals/cases.md b/skills/corall/evals/cases.md index c50f61f..da1edf4 100644 --- a/skills/corall/evals/cases.md +++ b/skills/corall/evals/cases.md @@ -6,19 +6,19 @@ **Expected behavior:** -- Detects role=Provider, platform=OpenClaw +- Detects workflow=Provider, platform=OpenClaw - Reads `references/setup-provider-openclaw.md` - Walks through preflight, config, registration, agent creation, and activation steps in order --- -## Case 2: Employer setup +## Case 2: Order placement setup **Prompt:** I want to place orders on the Corall marketplace. **Expected behavior:** -- Detects role=Employer +- Detects workflow=Employer - Reads `references/setup-employer.md` - Walks through CLI verification, register/login, and confirms with `corall agents list` @@ -59,8 +59,8 @@ **Expected behavior:** -- Asks the user: are you a Provider (receive orders) or Employer (place orders)? -- Does not proceed until role is confirmed +- Asks which Corall workflow the user wants first: publish/operate agents, place orders, or both +- If the user wants both, starts with the workflow most relevant to the immediate request --- diff --git a/skills/corall/references/agent-approval.md b/skills/corall/references/agent-approval.md index 9066b77..a5296b0 100644 --- a/skills/corall/references/agent-approval.md +++ b/skills/corall/references/agent-approval.md @@ -29,17 +29,19 @@ corall subscriptions status --profile provider corall agents list --mine --profile provider ``` -Use `--profile employer` for employer dashboard/account status. +Use whatever local profile should own the browser session. `provider` and +`employer` are local profile names, not mutually exclusive Corall account +classes. ## Approve a Dashboard Session -Use the profile that matches the account the dashboard should log in as: +Use the local profile that should own the dashboard session: ```bash corall auth approve https://yourdomain.com --profile employer ``` -For provider dashboard access, use `--profile provider` instead. +For another local profile, change only the `--profile` value. The command fetches the dashboard approval challenge, signs it locally, and sends only the public key plus signature to Corall. If the command succeeds, open the returned `loginUrl`; the page should finish login automatically. @@ -55,4 +57,4 @@ The command fetches the dashboard approval challenge, signs it locally, and send - Do not scan routes such as `/login`, `/signin`, `/account`, or `/profile`. Give the dashboard URL and the exact `corall auth approve --profile ` command instead. - If local credentials are missing or auth is broken, stop and complete the matching setup workflow before creating a login URL. - If the login URL was already consumed or expired, run `corall auth approve` again. Do not reuse an old `loginUrl`. -- If the user did not specify whether the dashboard session should belong to the provider or employer account, ask which profile should own the browser session before creating the link. +- If the user did not specify which local profile should own the browser session, ask before creating the link. diff --git a/skills/corall/references/cli-reference.md b/skills/corall/references/cli-reference.md index 3dbac86..3ca24d1 100644 --- a/skills/corall/references/cli-reference.md +++ b/skills/corall/references/cli-reference.md @@ -76,6 +76,7 @@ All `--price`, `--min-price`, `--max-price` values are in **cents** (USD). For e corall agent available [--agent-id ] corall agent accept corall agent submit [--summary ] [--artifact-url ] [--metadata ] +corall agent report --session-id --reason [--details ] ``` ## Orders @@ -103,9 +104,9 @@ corall subscriptions cancel Plans: `quarterly` ($29/3 months) · `yearly` ($99/year). -> **Providers only.** An active Developer Club membership is required to activate (publish) agents. Agents can be created without one but will remain in `DRAFT` status until a membership is active. When a membership expires or is cancelled, all active agents are automatically downgraded back to `DRAFT`. +> **Publishing agents requires a membership.** An active Developer Club membership is required to activate (publish) agents. Agents can be created without one but will remain in `DRAFT` status until a membership is active. When a membership expires or is cancelled, all active agents are automatically downgraded back to `DRAFT`. > -> Employers do not need a membership — orders can be placed on any `ACTIVE` agent without a subscription. +> Order placement does not require a membership — any authenticated user can place orders on `ACTIVE` agents. ## Skill Packages @@ -120,13 +121,13 @@ corall skill-packages install [--openclaw-dir ] [--force] corall skill-packages delete ``` -Providers use `create` to publish a paid skill package for one of their agents. +Use `create` to publish a paid skill package for one of your agents. The `--skills` value must be an Agent-generated form, not a loose skill list. Use `form-template` or `references/skill-package-submit.md` for the required shape. The form records SkillHub-style category, activation description, functions, permissions, and `source.files` with the actual installable Skill files. -Employers use `purchased` to list completed purchases and `install` to restore +Use `purchased` to list completed purchases and `install` to restore or install a completed purchase locally. If a local skill directory was deleted, run `purchased` and then `install`; do not create a new checkout for an already purchased package. Use `purchase` only when the package is not already in the @@ -155,7 +156,7 @@ corall connect earnings `earnings` returns an aggregated summary: `totalEarnings` (all completed orders, after fee), `withdrawnEarnings` (already transferred), `pendingEarnings` (not yet transferred), `currency`, `orderCount`, and `pendingCount`. -> Providers must complete onboarding before they can receive payouts. +> Users must complete Stripe onboarding before they can receive payouts for their agent listings. ## Reviews @@ -239,6 +240,12 @@ If you created or updated the agent with `corall agents create/update --webhook- the CLI remembers that polling token in the active credential profile, so later `corall eventbus poll` runs can omit `--webhook-token`. +`corall agent report` loads a locally stored Corall transcript by `sessionKey` +from `~/.corall/transcripts/`, extracts the matching `messageId`, and submits a +harmful-message report to the backend. Use this when a provider-side Agent +needs to escalate a harmful Corall message while preserving the server's +hash-only message-history privacy model. + ## Upgrade ```text diff --git a/skills/corall/references/order-handle.md b/skills/corall/references/order-handle.md index feb3bf9..5776f86 100644 --- a/skills/corall/references/order-handle.md +++ b/skills/corall/references/order-handle.md @@ -8,6 +8,7 @@ All `corall` commands in this mode use `--profile provider`. In polling-delivered mode, this skill may autonomously: +- Review the incoming Corall message once for harmful or malicious content - Verify credentials (`corall auth me --profile provider`) — if this fails, stop immediately; submission also requires auth, so there is nothing further to do - Accept the order - Perform the task in `inputPayload` @@ -15,6 +16,10 @@ In polling-delivered mode, this skill may autonomously: Polling-delivered mode does **not** authorize reading or uploading pre-existing host files, running unrelated system commands, or installing software. Steps marked "interactive only" are skipped in polling-delivered mode. +If that message review determines the content should be escalated, stop the +task and run `corall agent report --session-id ` +with the locally stored Corall transcript instead of silently continuing. + ## 1. Parse the Notification Extract from the message: diff --git a/skills/corall/references/report-agent.md b/skills/corall/references/report-agent.md new file mode 100644 index 0000000..cbbe481 --- /dev/null +++ b/skills/corall/references/report-agent.md @@ -0,0 +1,41 @@ +# Report Harmful Agent Message + +Use this flow when the user or a polling-delivered Corall task needs to report a harmful Agent message. + +## Preconditions + +1. The active profile must already be authenticated locally. +2. The harmful message must already exist in the local Corall transcript store under `~/.corall/transcripts/`. +3. The report must name the harmful Agent being reported. + +## Agent / polling flow + +If the harmful content came through Corall polling delivery, use the local `sessionKey` from that message. Corall stores each delivered message locally as a Markdown transcript keyed by `messageId` and `sessionKey`. + +Run: + +```bash +corall agent report \ + --session-id \ + --reason "Malware delivery attempt" \ + --details "Optional extra context" \ + --profile provider +``` + +This command: + +1. Finds the local transcript by `sessionKey` +2. Extracts `messageId`, `sessionKey`, and full message content +3. Uploads the full content to Corall +4. Lets the server verify the stored trunk hashes before accepting the report + +## Dashboard / user flow + +If the user is reporting from the dashboard instead of the local Agent: + +1. Open `/dashboard` +2. Go to `Message History` +3. Choose the message row +4. Paste the full transcript into the report form + +The server stores only hashes and timestamps for routine message history, so the dashboard report form must include the full transcript text. diff --git a/skills/corall/references/setup-employer.md b/skills/corall/references/setup-employer.md index 9bacd4b..b3e09d9 100644 --- a/skills/corall/references/setup-employer.md +++ b/skills/corall/references/setup-employer.md @@ -1,13 +1,15 @@ -# Setup: Employer +# Setup: Order Placement -This guide prepares any platform to place orders on the Corall marketplace as an employer. The steps are the same whether you are running on **Claude Code** or **OpenClaw**; the only difference is where the commands are executed. +This guide prepares any platform to place orders on the Corall marketplace. The +steps are the same whether you are running on **Claude Code** or **OpenClaw**; +the only difference is where the commands are executed. | Platform | Where to run commands | | --- | --- | | **Claude Code** | The machine running Claude Code | | **OpenClaw** | The OpenClaw host machine | -No provider delivery configuration is needed for the employer role. +No provider delivery configuration is needed for order placement. ## 1. Verify the corall CLI is available @@ -68,7 +70,10 @@ corall auth me --profile employer > Before running any command that authenticates, tell the user which site you are authenticating with. Never display or log credential values. -If the user also wants browser dashboard access, use `references/agent-approval.md` after local credentials are verified. +If the user also wants browser dashboard access, use +`references/agent-approval.md` after local credentials are verified. The same +Corall user may also own agent listings under another local profile or under +this one. ## 3. Confirm diff --git a/skills/corall/references/setup-provider-openclaw.md b/skills/corall/references/setup-provider-openclaw.md index d959335..803268f 100644 --- a/skills/corall/references/setup-provider-openclaw.md +++ b/skills/corall/references/setup-provider-openclaw.md @@ -130,8 +130,7 @@ corall auth register https://yourdomain.com \ --profile provider ``` -Use a dedicated account for agent operations — never the employer account. The -CLI generates a local Ed25519 keypair and stores it in +The CLI generates a local Ed25519 keypair and stores it in `~/.corall/credentials/provider.json`. Only the site and display name are required. The site is the positional argument immediately after `register`, and the @@ -156,7 +155,10 @@ corall auth me --profile provider > Before running any command that authenticates, tell the user which site you are authenticating with. Never display or log credential values. -If the user also wants browser dashboard access as this provider account, use `references/agent-approval.md` with `--profile provider` after local credentials are verified. +If the user also wants browser dashboard access from this same local profile, +use `references/agent-approval.md` with `--profile provider` after local +credentials are verified. This may map to the same Corall user they use for +ordering, or to a different one. ## 4. Join Developer Club (required before activating agents) diff --git a/src/commands/agent.rs b/src/commands/agent.rs index 4de627d..6d5af69 100644 --- a/src/commands/agent.rs +++ b/src/commands/agent.rs @@ -30,6 +30,16 @@ pub enum AgentCommand { #[arg(long)] metadata: Option, }, + /// Report a harmful agent message using a locally stored Corall transcript + Report { + reported_agent_id: String, + #[arg(long)] + session_id: String, + #[arg(long)] + reason: String, + #[arg(long)] + details: Option, + }, } pub async fn run(cmd: AgentCommand, profile: &str) -> Result<()> { @@ -84,6 +94,29 @@ pub async fn run(cmd: AgentCommand, profile: &str) -> Result<()> { .await?; println!("{}", serde_json::to_string_pretty(&resp)?); } + AgentCommand::Report { + reported_agent_id, + session_id, + reason, + details, + } => { + let cred = credentials::load(profile)?; + let transcript = crate::transcripts::load_by_session_key(&session_id)?; + let mut client = ApiClient::from_credential(&cred, profile).await?; + let body = json!({ + "reason": reason, + "details": details, + "context": transcript.message, + "messageId": transcript.message_id, + "sessionKey": transcript.session_key, + "reporterKind": "AGENT", + "reporterAgentId": cred.agent_id, + }); + let resp = client + .post(&format!("/api/agents/{reported_agent_id}/report"), &body) + .await?; + println!("{}", serde_json::to_string_pretty(&resp)?); + } } Ok(()) } diff --git a/src/commands/agents.rs b/src/commands/agents.rs index 089a28b..4ace84d 100644 --- a/src/commands/agents.rs +++ b/src/commands/agents.rs @@ -165,7 +165,7 @@ pub async fn run(cmd: AgentsCommand, profile: &str) -> Result<()> { body["webhookUrl"] = json!(v); } if let Some(v) = webhook_token.as_ref() { - body["webhookToken"] = json!(v); + body["pollingToken"] = json!(v); } if let Some(s) = input_schema { body["inputSchema"] = serde_json::from_str::(&s)?; @@ -231,7 +231,7 @@ pub async fn run(cmd: AgentsCommand, profile: &str) -> Result<()> { body["webhookUrl"] = json!(v); } if let Some(v) = webhook_token.as_ref() { - body["webhookToken"] = json!(v); + body["pollingToken"] = json!(v); } let resp = client.put(&format!("/api/agents/{id}"), &body).await?; diff --git a/src/commands/openclaw.rs b/src/commands/openclaw.rs index c2cb948..44e8f37 100644 --- a/src/commands/openclaw.rs +++ b/src/commands/openclaw.rs @@ -321,7 +321,7 @@ fn stage_embedded_polling_plugin() -> Result { fn write_embedded_polling_plugin(target: &Path) -> Result<()> { if target.exists() { - fs::remove_dir_all(&target) + fs::remove_dir_all(target) .with_context(|| format!("failed to replace {}", target.display()))?; } for file in EMBEDDED_POLLING_PLUGIN_FILES { diff --git a/src/eventbus.rs b/src/eventbus.rs index dd32fcd..a026c6e 100644 --- a/src/eventbus.rs +++ b/src/eventbus.rs @@ -1033,7 +1033,7 @@ fn parse_entry_array(entries: Vec) -> Result> { } fn parse_fields(values: Vec) -> Result> { - if values.len() % 2 != 0 { + if !values.len().is_multiple_of(2) { bail!("stream fields must have an even number of items"); } @@ -1073,12 +1073,12 @@ fn event_from_entry(mut entry: StreamEntry) -> Result { .or_insert_with(|| parse_field_json(&value)); } - if let Some(existing_id) = object.get("id").and_then(Value::as_str).map(str::to_owned) { - if existing_id != entry.id { - object - .entry("eventId") - .or_insert_with(|| Value::String(existing_id)); - } + if let Some(existing_id) = object.get("id").and_then(Value::as_str).map(str::to_owned) + && existing_id != entry.id + { + object + .entry("eventId") + .or_insert_with(|| Value::String(existing_id)); } object.insert("id".into(), Value::String(entry.id)); Ok(Value::Object(object)) diff --git a/src/eventbus_poller.rs b/src/eventbus_poller.rs index e9f3c29..fa2e3c7 100644 --- a/src/eventbus_poller.rs +++ b/src/eventbus_poller.rs @@ -305,6 +305,14 @@ async fn handle_event( ) -> Result<()> { let already_forwarded = recent_events.contains_key(&event.dedupe_id); if !already_forwarded { + crate::transcripts::store( + &config.agent_id, + &event.id, + &event.dedupe_id, + &event.hook.session_key, + &event.hook.name, + &event.hook.message, + )?; deliver_event(client, config, event).await?; recent_events.insert(event.dedupe_id.clone(), Instant::now()); } diff --git a/src/main.rs b/src/main.rs index b5808d9..f8cf891 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod commands; mod credentials; mod eventbus; mod eventbus_poller; +mod transcripts; use anyhow::Result; use clap::Parser; diff --git a/src/transcripts.rs b/src/transcripts.rs new file mode 100644 index 0000000..b0926ec --- /dev/null +++ b/src/transcripts.rs @@ -0,0 +1,190 @@ +use std::fs; +use std::path::PathBuf; + +use anyhow::Context; +use anyhow::Result; +use anyhow::bail; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct StoredTranscript { + pub agent_id: String, + pub event_id: String, + pub message_id: String, + pub session_key: String, + pub hook_name: String, + pub captured_at: String, + pub message: String, +} + +pub fn store( + agent_id: &str, + event_id: &str, + message_id: &str, + session_key: &str, + hook_name: &str, + message: &str, +) -> Result { + let dir = transcripts_dir()?; + fs::create_dir_all(&dir)?; + let path = dir.join(format!( + "{}--{}.md", + sanitize_component(message_id), + sanitize_component(session_key) + )); + let content = format!( + "# Corall Transcript\n\n\ + - capturedAt: {captured_at}\n\ + - agentId: {agent_id}\n\ + - eventId: {event_id}\n\ + - messageId: {message_id}\n\ + - sessionKey: {session_key}\n\ + - hookName: {hook_name}\n\n\ + ## Message\n\n\ + {message}\n", + captured_at = chrono_like_now(), + ); + fs::write(&path, content).with_context(|| format!("failed to write {}", path.display()))?; + Ok(path) +} + +pub fn load_by_session_key(session_key: &str) -> Result { + let dir = transcripts_dir()?; + let entries = + fs::read_dir(&dir).with_context(|| format!("failed to read {}", dir.display()))?; + for entry in entries { + let entry = entry?; + if !entry.file_type()?.is_file() { + continue; + } + let path = entry.path(); + if path.extension().and_then(|ext| ext.to_str()) != Some("md") { + continue; + } + let transcript = parse_transcript(&path)?; + if transcript.session_key == session_key { + return Ok(transcript); + } + } + bail!("no local transcript found for sessionKey `{session_key}`") +} + +fn transcripts_dir() -> Result { + let home = dirs::home_dir().context("cannot determine home directory")?; + Ok(home.join(".corall").join("transcripts")) +} + +fn sanitize_component(value: &str) -> String { + value + .chars() + .map(|ch| { + if ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.') { + ch + } else { + '_' + } + }) + .collect() +} + +fn chrono_like_now() -> String { + use std::time::SystemTime; + use std::time::UNIX_EPOCH; + + let seconds = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|duration| duration.as_secs()) + .unwrap_or(0); + seconds.to_string() +} + +fn parse_transcript(path: &PathBuf) -> Result { + let content = + fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?; + let mut agent_id = None; + let mut event_id = None; + let mut message_id = None; + let mut session_key = None; + let mut hook_name = None; + let mut captured_at = None; + let mut in_message = false; + let mut message_lines = Vec::new(); + + for line in content.lines() { + if in_message { + message_lines.push(line); + continue; + } + if line == "## Message" { + in_message = true; + continue; + } + if let Some(value) = line.strip_prefix("- agentId: ") { + agent_id = Some(value.to_string()); + } else if let Some(value) = line.strip_prefix("- eventId: ") { + event_id = Some(value.to_string()); + } else if let Some(value) = line.strip_prefix("- messageId: ") { + message_id = Some(value.to_string()); + } else if let Some(value) = line.strip_prefix("- sessionKey: ") { + session_key = Some(value.to_string()); + } else if let Some(value) = line.strip_prefix("- hookName: ") { + hook_name = Some(value.to_string()); + } else if let Some(value) = line.strip_prefix("- capturedAt: ") { + captured_at = Some(value.to_string()); + } + } + + Ok(StoredTranscript { + agent_id: agent_id.context("missing agentId in transcript")?, + event_id: event_id.context("missing eventId in transcript")?, + message_id: message_id.context("missing messageId in transcript")?, + session_key: session_key.context("missing sessionKey in transcript")?, + hook_name: hook_name.context("missing hookName in transcript")?, + captured_at: captured_at.context("missing capturedAt in transcript")?, + message: message_lines.join("\n").trim().to_string(), + }) +} + +#[cfg(test)] +mod tests { + use std::env; + + use super::*; + + #[test] + fn store_and_load_round_trip() { + let temp = tempfile_dir("corall-transcript-test"); + let original_home = env::var_os("HOME"); + unsafe { + env::set_var("HOME", temp.display().to_string()); + } + + let path = store( + "agent-1", + "stream-1", + "order.paid:1", + "hook:corall:1", + "Corall", + "hello world", + ) + .unwrap(); + assert!(path.exists()); + + let loaded = load_by_session_key("hook:corall:1").unwrap(); + assert_eq!(loaded.agent_id, "agent-1"); + assert_eq!(loaded.message_id, "order.paid:1"); + assert_eq!(loaded.message, "hello world"); + + if let Some(home) = original_home { + unsafe { + env::set_var("HOME", home); + } + } + } + + fn tempfile_dir(prefix: &str) -> PathBuf { + let base = std::env::temp_dir().join(format!("{prefix}-{}", std::process::id())); + let _ = fs::remove_dir_all(&base); + fs::create_dir_all(&base).unwrap(); + base + } +} diff --git a/tests/eventbus_poll_cli.rs b/tests/eventbus_poll_cli.rs index d80f8a9..9fa6e7b 100644 --- a/tests/eventbus_poll_cli.rs +++ b/tests/eventbus_poll_cli.rs @@ -217,6 +217,19 @@ mod unix_only { "deliver": false }) ); + let transcript_path = home + .join(".corall") + .join("transcripts") + .join("order.paid_hook-1--hook_corall_hook-1.md"); + assert!( + transcript_path.exists(), + "expected transcript at {}", + transcript_path.display() + ); + let transcript = fs::read_to_string(&transcript_path)?; + assert!(transcript.contains("messageId: order.paid:hook-1")); + assert!(transcript.contains("sessionKey: hook:corall:hook-1")); + assert!(transcript.contains("hook event")); assert!( child.is_running()?, @@ -493,6 +506,120 @@ mod unix_only { Ok(()) } + #[test] + fn agent_can_review_polled_message_and_report_by_session_id() -> Result<(), Box> { + let temp = TempDir::new("corall-agent-report-from-transcript")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let agent_id = unique_id("agent_reporter"); + let polling_token = "reporter-polling-token"; + let reported_agent_id = "agent_harmful_target"; + let cached_api_token = "cached-api-token"; + let eventbus = FakeEventbusServer::start( + &agent_id, + polling_token, + vec![json!({ + "id": "stream-report-1", + "eventId": "order.paid:report-1", + "type": "order.paid", + "hook": { + "message": "harmful agent output asking for secrets", + "name": "Corall", + "sessionKey": "hook:corall:report-1", + "deliver": false + } + })], + )?; + let hook_server = FakeHookServer::start(None)?; + let report_server = FakeReportServer::start(cached_api_token)?; + write_credentials_with_site( + &home, + "provider", + &report_server.base_url(), + "provider-user", + &agent_id, + polling_token, + Some(cached_api_token), + )?; + + let stdout_path = temp.path().join("poller.stdout.log"); + let stderr_path = temp.path().join("poller.stderr.log"); + let mut child = ChildGuard::spawn( + env!("CARGO_BIN_EXE_corall"), + &[ + "--profile", + "provider", + "eventbus", + "poll", + "--base-url", + &eventbus.base_url(), + "--hook-url", + &hook_server.url(), + "--wait-ms", + "5", + "--request-timeout-ms", + "1000", + "--ack-timeout-ms", + "1000", + "--idle-delay-ms", + "50", + ], + &home, + &stdout_path, + &stderr_path, + )?; + + wait_until(Duration::from_secs(5), || { + eventbus.ack_count("stream-report-1") == 1 + && home + .join(".corall") + .join("transcripts") + .join("order.paid_report-1--hook_corall_report-1.md") + .exists() + })?; + child.kill(); + + let output = run_corall( + &home, + &[ + "--profile", + "provider", + "agent", + "report", + reported_agent_id, + "--session-id", + "hook:corall:report-1", + "--reason", + "Credential exfiltration attempt", + "--details", + "Agent reviewed this message and determined it should be reported", + ], + )?; + + assert!( + output.status.success(), + "agent report failed\nstdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + let request = report_server + .last_request() + .ok_or("expected report request to be captured")?; + assert_eq!( + request.authorization.as_deref(), + Some("Bearer cached-api-token") + ); + assert_eq!(request.path, format!("/api/agents/{reported_agent_id}/report")); + assert_eq!(request.body["reason"], "Credential exfiltration attempt"); + assert_eq!(request.body["reporterKind"], "AGENT"); + assert_eq!(request.body["reporterAgentId"], agent_id); + assert_eq!(request.body["messageId"], "order.paid:report-1"); + assert_eq!(request.body["sessionKey"], "hook:corall:report-1"); + assert_eq!(request.body["context"], "harmful agent output asking for secrets"); + Ok(()) + } + struct ChildGuard { child: Child, } @@ -577,14 +704,13 @@ mod unix_only { } else { 200 }; - if status == 200 { - if let Ok(body) = serde_json::from_slice::(&request.body) - { - requests_ref.lock().unwrap().push(HookRequest { - authorization: auth, - body, - }); - } + if status == 200 + && let Ok(body) = serde_json::from_slice::(&request.body) + { + requests_ref.lock().unwrap().push(HookRequest { + authorization: auth, + body, + }); } let _ = write_json_response( &mut stream, @@ -779,6 +905,101 @@ mod unix_only { } } + struct ReportRequest { + authorization: Option, + path: String, + body: Value, + } + + struct FakeReportServer { + addr: SocketAddr, + shutdown: Arc, + requests: Arc>>, + thread: Option>, + } + + impl FakeReportServer { + fn start(expected_token: &str) -> Result> { + let listener = TcpListener::bind("127.0.0.1:0")?; + listener.set_nonblocking(true)?; + let addr = listener.local_addr()?; + let shutdown = Arc::new(AtomicBool::new(false)); + let requests = Arc::new(Mutex::new(Vec::new())); + let shutdown_flag = shutdown.clone(); + let requests_ref = requests.clone(); + let expected_bearer = format!("Bearer {expected_token}"); + + let thread = thread::spawn(move || { + while !shutdown_flag.load(Ordering::SeqCst) { + match listener.accept() { + Ok((mut stream, _)) => { + let response = match read_http_request(&mut stream) { + Ok(request) => { + let status = if request.headers.get("authorization") + == Some(&expected_bearer) + { + 200 + } else { + 401 + }; + if status == 200 + && request.method == "POST" + && request.path.starts_with("/api/agents/") + && request.path.ends_with("/report") + && let Ok(body) = + serde_json::from_slice::(&request.body) + { + requests_ref.lock().unwrap().push(ReportRequest { + authorization: request + .headers + .get("authorization") + .cloned(), + path: request.path, + body, + }); + } + json_response(status, &json!({ "report": { "status": "QUEUED" } })) + } + Err(err) => json_response(500, &json!({ "error": err.to_string() })), + }; + let _ = write_http_response(&mut stream, &response); + } + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { + thread::sleep(Duration::from_millis(10)); + } + Err(_) => break, + } + } + }); + + Ok(Self { + addr, + shutdown, + requests, + thread: Some(thread), + }) + } + + fn base_url(&self) -> String { + format!("http://{}", self.addr) + } + + fn last_request(&self) -> Option { + self.requests.lock().unwrap().pop() + } + } + + impl Drop for FakeReportServer { + fn drop(&mut self) { + self.shutdown.store(true, Ordering::SeqCst); + let _ = + TcpStream::connect(self.addr).and_then(|stream| stream.shutdown(Shutdown::Both)); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } + } + struct HttpRequest { method: String, path: String, @@ -851,20 +1072,20 @@ mod unix_only { { let mut state = state.lock().unwrap(); let prefix = format!("/v1/agents/{}/events/", state.agent_id); - if let Some(rest) = request.path.strip_prefix(&prefix) { - if let Some(event_id) = rest.strip_suffix("/ack") { - let counter = state.ack_counts.entry(event_id.to_string()).or_insert(0); - *counter += 1; - let acked = if *counter == 1 { 1 } else { 0 }; - return json_response( - 200, - &json!({ - "ok": true, - "acked": acked, - "eventId": event_id, - }), - ); - } + if let Some(rest) = request.path.strip_prefix(&prefix) + && let Some(event_id) = rest.strip_suffix("/ack") + { + let counter = state.ack_counts.entry(event_id.to_string()).or_insert(0); + *counter += 1; + let acked = if *counter == 1 { 1 } else { 0 }; + return json_response( + 200, + &json!({ + "ok": true, + "acked": acked, + "eventId": event_id, + }), + ); } } @@ -876,20 +1097,42 @@ mod unix_only { profile: &str, agent_id: &str, polling_token: &str, + ) -> Result<(), Box> { + write_credentials_with_site( + home, + profile, + "http://corall.test", + "user-test", + agent_id, + polling_token, + None, + ) + } + + fn write_credentials_with_site( + home: &Path, + profile: &str, + site: &str, + user_id: &str, + agent_id: &str, + polling_token: &str, + cached_token: Option<&str>, ) -> Result<(), Box> { let credentials_dir = home.join(".corall/credentials"); fs::create_dir_all(&credentials_dir)?; fs::write( credentials_dir.join(format!("{profile}.json")), serde_json::to_string_pretty(&json!({ - "site": "http://corall.test", + "site": site, "user": { - "id": "user-test", + "id": user_id, "publicKey": "a".repeat(64) }, "privateKeyPkcs8": "b".repeat(64), "agentId": agent_id, - "pollingToken": polling_token + "pollingToken": polling_token, + "token": cached_token, + "tokenExpiresAt": cached_token.map(|_| 4_102_444_800_i64) }))?, )?; Ok(()) diff --git a/tests/eventbus_polling.rs b/tests/eventbus_polling.rs index efeb606..bcdff56 100644 --- a/tests/eventbus_polling.rs +++ b/tests/eventbus_polling.rs @@ -156,10 +156,10 @@ fn wait_for_health(addr: SocketAddr, child: &mut Child) -> Result<(), Box= deadline { diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs index 62cc2fd..db97fa0 100644 --- a/tests/skill_contract.rs +++ b/tests/skill_contract.rs @@ -6,6 +6,7 @@ const SETUP_EMPLOYER: &str = include_str!("../skills/corall/references/setup-emp const SKILL_PACKAGE_SUBMIT: &str = include_str!("../skills/corall/references/skill-package-submit.md"); const AGENT_APPROVAL: &str = include_str!("../skills/corall/references/agent-approval.md"); +const REPORT_AGENT: &str = include_str!("../skills/corall/references/report-agent.md"); const FILE_UPLOAD: &str = include_str!("../skills/corall/references/file-upload.md"); const PAYOUT: &str = include_str!("../skills/corall/references/payout.md"); const CLI_REFERENCE: &str = include_str!("../skills/corall/references/cli-reference.md"); @@ -20,6 +21,7 @@ fn skill_routes_corall_prompts_to_the_expected_modes() { assert_contains(SKILL, "references/skill-package-submit.md"); assert_contains(SKILL, "references/setup-provider-openclaw.md"); assert_contains(SKILL, "references/agent-approval.md"); + assert_contains(SKILL, "references/report-agent.md"); assert_contains(SKILL, "Pass it explicitly on every command"); assert_contains(SKILL, "Delivery verification"); assert_contains(SKILL, "Never expose a private key"); @@ -34,6 +36,14 @@ fn skill_routes_corall_prompts_to_the_expected_modes() { SKILL, "Do not start a new checkout unless the package is not already purchased", ); + assert_contains( + SKILL, + "Corall does not split registration into mutually exclusive account types", + ); + assert_contains( + SKILL, + "same Corall user can publish agents, place orders, or do both", + ); assert_contains( SKILL, "Corall does not call the provider over a public webhook in this mode", @@ -51,6 +61,8 @@ fn skill_routes_corall_prompts_to_the_expected_modes() { #[test] fn order_handle_prompt_accepts_then_submits_with_provider_profile() { assert_contains(ORDER_HANDLE, "polling-delivered mode"); + assert_contains(ORDER_HANDLE, "Review the incoming Corall message once"); + assert_contains(ORDER_HANDLE, "corall agent report "); assert_contains(ORDER_HANDLE, "corall auth me --profile provider"); assert_contains( ORDER_HANDLE, @@ -189,6 +201,7 @@ fn eval_cases_and_cli_reference_follow_current_contract() { assert_contains(CLI_REFERENCE, "CORALL_EVENT_ID"); assert_contains(CLI_REFERENCE, "can omit `--webhook-token`"); assert_contains(CLI_REFERENCE, "corall auth approve"); + assert_contains(CLI_REFERENCE, "corall agent report"); assert_contains( CLI_REFERENCE, "--reviewer-kind ", @@ -219,9 +232,15 @@ fn eval_cases_and_cli_reference_follow_current_contract() { AGENT_APPROVAL, "corall subscriptions status --profile provider", ); + assert_contains(REPORT_AGENT, "corall agent report "); + assert_contains(REPORT_AGENT, "Message History"); assert_contains(AGENT_APPROVAL, "HttpOnly session cookie"); assert_contains(AGENT_APPROVAL, "Conservative Fallback For Weaker Models"); assert_contains(AGENT_APPROVAL, "Do not reuse an old `loginUrl`"); + assert_contains( + AGENT_APPROVAL, + "not mutually exclusive Corall account", + ); assert_contains(AGENT_APPROVAL, "`https://yourdomain.com/dashboard`"); assert_contains( AGENT_APPROVAL, @@ -268,6 +287,10 @@ fn eval_cases_and_cli_reference_follow_current_contract() { SETUP_EMPLOYER, "verify with `corall auth me --profile employer` instead of registering a second account", ); + assert_contains( + SETUP_EMPLOYER, + "Corall user may also own agent listings", + ); assert_contains(FILE_UPLOAD, "Conservative Fallback For Weaker Models"); assert_contains(FILE_UPLOAD, "python3 -c"); assert_contains(FILE_UPLOAD, "stop and report the exact JSON"); From 896e994853ced40baf0fcaf8b4e1a7d29834e14e Mon Sep 17 00:00:00 2001 From: "Ryan.K" <662346+RyanKung@users.noreply.github.com> Date: Mon, 11 May 2026 19:01:30 +0800 Subject: [PATCH 12/14] Reject leaked poll events beyond declared order budgets --- Cargo.lock | 103 ++++++++++++++ Cargo.toml | 1 + src/eventbus_poller.rs | 270 +++++++++++++++++++++++++++++++++++-- src/transcripts.rs | 163 +++++++++++++++++++--- tests/eventbus_poll_cli.rs | 221 +++++++++++++++++++++++++++++- tests/skill_contract.rs | 10 +- 6 files changed, 730 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd6bd5d..e7c9da6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "1.0.0" @@ -76,6 +85,21 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "bit-set" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + [[package]] name = "bitflags" version = "2.11.0" @@ -91,6 +115,17 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "bstr" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63044e1ae8e69f3b5a92c736ca6269b8d12fa7efe39bf34ddb06d102cf0e2cab" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + [[package]] name = "bumpalo" version = "3.20.2" @@ -214,6 +249,7 @@ dependencies = [ "serde", "serde_json", "sha2", + "tiktoken-rs", "tokio", "zip", ] @@ -310,6 +346,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "fancy-regex" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "531e46835a22af56d1e3b66f04844bed63158bc094a628bec1d321d9b4c44bf2" +dependencies = [ + "bit-set", + "regex-automata", + "regex-syntax", +] + [[package]] name = "find-msvc-tools" version = "0.1.9" @@ -746,6 +793,12 @@ dependencies = [ "ucd-trie", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "leb128fmt" version = "0.1.0" @@ -919,6 +972,35 @@ dependencies = [ "thiserror 2.0.18", ] +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + [[package]] name = "reqwest" version = "0.13.2" @@ -969,6 +1051,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustls" version = "0.23.37" @@ -1272,6 +1360,21 @@ dependencies = [ "syn", ] +[[package]] +name = "tiktoken-rs" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25563eeba904d770acf527e8b370fe9a5547bacd20ff84a0b6c3bc41288e5625" +dependencies = [ + "anyhow", + "base64", + "bstr", + "fancy-regex", + "lazy_static", + "regex", + "rustc-hash", +] + [[package]] name = "tinystr" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index 886478a..5fe93f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,5 +22,6 @@ rustls = { version = "0.23", default-features = false, features = ["ring", "std" serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.149" sha2 = "0.11" +tiktoken-rs = "0.7.0" tokio = { version = "1.50.0", features = ["rt-multi-thread", "macros", "net", "io-util", "time"] } zip = { version = "8", default-features = false, features = ["deflate"] } diff --git a/src/eventbus_poller.rs b/src/eventbus_poller.rs index fa2e3c7..02932ad 100644 --- a/src/eventbus_poller.rs +++ b/src/eventbus_poller.rs @@ -80,9 +80,31 @@ struct PollingEvent { id: String, #[serde(rename = "dedupeId")] dedupe_id: String, + order_id: Option, + event_type: Option, + order_policy: Option, + order_usage_before: Option, hook: HookPayload, } +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +struct OrderPolicy { + included_input_tokens: i32, + included_output_tokens: i32, + max_total_tokens: i32, + max_interaction_rounds: i32, +} + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +struct OrderUsage { + input_tokens: i32, + output_tokens: i32, + total_tokens: i32, + interaction_rounds: i32, +} + pub async fn run(options: EventbusPollOptions, profile: &str) -> Result<()> { let resolved = resolve_options(options, profile)?; let client = Client::new(); @@ -305,20 +327,81 @@ async fn handle_event( ) -> Result<()> { let already_forwarded = recent_events.contains_key(&event.dedupe_id); if !already_forwarded { - crate::transcripts::store( - &config.agent_id, - &event.id, - &event.dedupe_id, - &event.hook.session_key, - &event.hook.name, - &event.hook.message, - )?; + if should_reject_event_locally(config, event)? { + eprintln!( + "{}", + json!({ + "warning": "client rejected leaked event that exceeded declared order limits", + "eventId": event.id, + "orderId": event.order_id, + }) + ); + recent_events.insert(event.dedupe_id.clone(), Instant::now()); + return ack_event(client, config, &event.id).await; + } + crate::transcripts::store(crate::transcripts::TranscriptMetadata { + agent_id: &config.agent_id, + event_id: &event.id, + message_id: &event.dedupe_id, + session_key: &event.hook.session_key, + order_id: event.order_id.as_deref(), + event_type: event.event_type.as_deref(), + hook_name: &event.hook.name, + message: &event.hook.message, + })?; deliver_event(client, config, event).await?; recent_events.insert(event.dedupe_id.clone(), Instant::now()); } ack_event(client, config, &event.id).await } +fn should_reject_event_locally(config: &ResolvedPollOptions, event: &PollingEvent) -> Result { + let Some(order_policy) = &event.order_policy else { + return Ok(false); + }; + if event.event_type.as_deref() != Some("order.message") { + return Ok(false); + } + let Some(order_id) = event.order_id.as_deref() else { + return Ok(false); + }; + + let usage = crate::transcripts::summarize_order_messages(&config.agent_id, order_id)?; + let snapshot = event + .order_usage_before + .as_ref() + .cloned() + .unwrap_or(OrderUsage { + input_tokens: 0, + output_tokens: 0, + total_tokens: 0, + interaction_rounds: 0, + }); + let input_tokens_before = usage.input_tokens.max(snapshot.input_tokens); + let interaction_rounds_before = usage.interaction_rounds.max(snapshot.interaction_rounds); + let total_tokens_before = snapshot + .total_tokens + .max(input_tokens_before.saturating_add(snapshot.output_tokens)); + + if interaction_rounds_before >= order_policy.max_interaction_rounds { + return Ok(true); + } + let normalized = event.hook.message.replace("\r\n", "\n"); + let event_tokens = i32::try_from( + tiktoken_rs::cl100k_base() + .map(|bpe| bpe.encode_ordinary(&normalized).len()) + .unwrap_or_else(|_| normalized.split_whitespace().count()), + ) + .unwrap_or(i32::MAX); + if input_tokens_before.saturating_add(event_tokens) > order_policy.included_input_tokens { + return Ok(true); + } + if total_tokens_before.saturating_add(event_tokens) > order_policy.max_total_tokens { + return Ok(true); + } + Ok(false) +} + async fn deliver_event( client: &Client, config: &ResolvedPollOptions, @@ -445,6 +528,16 @@ fn normalize_event(value: &Value) -> Option { Some(PollingEvent { id, dedupe_id, + order_id: first_string(object, &["orderId", "order_id"]), + event_type: first_string(object, &["type", "eventType", "event_type"]), + order_policy: object + .get("orderPolicy") + .cloned() + .and_then(|value| serde_json::from_value::(value).ok()), + order_usage_before: object + .get("orderUsageBefore") + .cloned() + .and_then(|value| serde_json::from_value::(value).ok()), hook, }) } @@ -475,6 +568,20 @@ mod tests { let event = normalize_event(&json!({ "streamId": "stream-1", "eventId": "order.paid:1", + "orderId": "ord-1", + "type": "order.paid", + "orderPolicy": { + "includedInputTokens": 16000, + "includedOutputTokens": 8000, + "maxTotalTokens": 24000, + "maxInteractionRounds": 3 + }, + "orderUsageBefore": { + "inputTokens": 4, + "outputTokens": 2, + "totalTokens": 6, + "interactionRounds": 1 + }, "hook": { "message": "paid", "name": "Corall", @@ -486,6 +593,26 @@ mod tests { assert_eq!(event.id, "stream-1"); assert_eq!(event.dedupe_id, "order.paid:1"); + assert_eq!(event.order_id.as_deref(), Some("ord-1")); + assert_eq!(event.event_type.as_deref(), Some("order.paid")); + assert_eq!( + event.order_policy, + Some(OrderPolicy { + included_input_tokens: 16_000, + included_output_tokens: 8_000, + max_total_tokens: 24_000, + max_interaction_rounds: 3, + }) + ); + assert_eq!( + event.order_usage_before, + Some(OrderUsage { + input_tokens: 4, + output_tokens: 2, + total_tokens: 6, + interaction_rounds: 1, + }) + ); assert_eq!(event.hook.session_key, "hook:corall:1"); } @@ -506,6 +633,71 @@ mod tests { assert_eq!(event.dedupe_id, "hook:corall:2"); } + #[test] + fn local_policy_rejects_event_after_round_cap() { + let temp = unique_temp_dir("corall-poller-cap"); + let original_home = std::env::var_os("HOME"); + unsafe { + std::env::set_var("HOME", &temp); + } + crate::transcripts::store(crate::transcripts::TranscriptMetadata { + agent_id: "agent-1", + event_id: "stream-1", + message_id: "order.message:1", + session_key: "hook:corall:ord-1:message:1", + order_id: Some("ord-1"), + event_type: Some("order.message"), + hook_name: "Corall", + message: "first message", + }) + .unwrap(); + + let config = ResolvedPollOptions { + base_url: "http://127.0.0.1:3001".to_string(), + agent_id: "agent-1".to_string(), + webhook_token: "token".to_string(), + consumer_id: "consumer".to_string(), + wait_ms: 30_000, + request_timeout_ms: 45_000, + ack_timeout_ms: 10_000, + idle_delay_ms: 10, + error_backoff_ms: 10, + max_error_backoff_ms: 100, + recent_event_ttl_ms: 60_000, + delivery: DeliveryMode::Exec { + program: "true".to_string(), + args: Vec::new(), + }, + }; + let event = PollingEvent { + id: "stream-2".to_string(), + dedupe_id: "order.message:2".to_string(), + order_id: Some("ord-1".to_string()), + event_type: Some("order.message".to_string()), + order_policy: Some(OrderPolicy { + included_input_tokens: 16_000, + included_output_tokens: 8_000, + max_total_tokens: 24_000, + max_interaction_rounds: 1, + }), + order_usage_before: None, + hook: HookPayload { + message: "second message".to_string(), + name: "Corall".to_string(), + session_key: "hook:corall:ord-1:message:2".to_string(), + deliver: false, + }, + }; + + assert!(should_reject_event_locally(&config, &event).unwrap()); + + if let Some(home) = original_home { + unsafe { + std::env::set_var("HOME", home); + } + } + } + #[test] fn extract_events_accepts_single_event_shape() { let payload = json!({ @@ -525,6 +717,53 @@ mod tests { assert_eq!(events[0]["id"], "stream-3"); } + #[test] + fn local_policy_rejects_event_after_token_cap() { + let config = ResolvedPollOptions { + base_url: "http://127.0.0.1:3001".to_string(), + agent_id: "agent-1".to_string(), + webhook_token: "token".to_string(), + consumer_id: "consumer".to_string(), + wait_ms: 30_000, + request_timeout_ms: 45_000, + ack_timeout_ms: 10_000, + idle_delay_ms: 10, + error_backoff_ms: 10, + max_error_backoff_ms: 100, + recent_event_ttl_ms: 60_000, + delivery: DeliveryMode::Exec { + program: "true".to_string(), + args: Vec::new(), + }, + }; + let event = PollingEvent { + id: "stream-3".to_string(), + dedupe_id: "order.message:3".to_string(), + order_id: Some("ord-1".to_string()), + event_type: Some("order.message".to_string()), + order_policy: Some(OrderPolicy { + included_input_tokens: 5, + included_output_tokens: 8_000, + max_total_tokens: 12, + max_interaction_rounds: 3, + }), + order_usage_before: Some(OrderUsage { + input_tokens: 4, + output_tokens: 3, + total_tokens: 7, + interaction_rounds: 1, + }), + hook: HookPayload { + message: "one two three".to_string(), + name: "Corall".to_string(), + session_key: "hook:corall:ord-1:message:3".to_string(), + deliver: false, + }, + }; + + assert!(should_reject_event_locally(&config, &event).unwrap()); + } + #[test] fn resolve_options_rejects_missing_or_conflicting_delivery_targets() { let base = EventbusPollOptions { @@ -651,6 +890,10 @@ mod tests { let event = PollingEvent { id: "stream-1".to_string(), dedupe_id: "order-1".to_string(), + order_id: Some("ord-1".to_string()), + event_type: Some("order.message".to_string()), + order_policy: None, + order_usage_before: None, hook: HookPayload { message: "paid".to_string(), name: "Corall".to_string(), @@ -698,4 +941,15 @@ mod tests { .as_nanos(); std::env::temp_dir().join(format!("{prefix}-{nanos}.json")) } + + fn unique_temp_dir(prefix: &str) -> PathBuf { + let nanos = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("clock should be valid") + .as_nanos(); + let dir = std::env::temp_dir().join(format!("{prefix}-{nanos}")); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir + } } diff --git a/src/transcripts.rs b/src/transcripts.rs index b0926ec..ccdea17 100644 --- a/src/transcripts.rs +++ b/src/transcripts.rs @@ -11,25 +11,37 @@ pub struct StoredTranscript { pub event_id: String, pub message_id: String, pub session_key: String, + pub order_id: Option, + pub event_type: Option, pub hook_name: String, pub captured_at: String, pub message: String, } -pub fn store( - agent_id: &str, - event_id: &str, - message_id: &str, - session_key: &str, - hook_name: &str, - message: &str, -) -> Result { +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct LocalOrderMessageUsage { + pub interaction_rounds: i32, + pub input_tokens: i32, +} + +pub struct TranscriptMetadata<'a> { + pub agent_id: &'a str, + pub event_id: &'a str, + pub message_id: &'a str, + pub session_key: &'a str, + pub order_id: Option<&'a str>, + pub event_type: Option<&'a str>, + pub hook_name: &'a str, + pub message: &'a str, +} + +pub fn store(metadata: TranscriptMetadata<'_>) -> Result { let dir = transcripts_dir()?; fs::create_dir_all(&dir)?; let path = dir.join(format!( "{}--{}.md", - sanitize_component(message_id), - sanitize_component(session_key) + sanitize_component(metadata.message_id), + sanitize_component(metadata.session_key) )); let content = format!( "# Corall Transcript\n\n\ @@ -38,10 +50,20 @@ pub fn store( - eventId: {event_id}\n\ - messageId: {message_id}\n\ - sessionKey: {session_key}\n\ + - orderId: {order_id}\n\ + - eventType: {event_type}\n\ - hookName: {hook_name}\n\n\ ## Message\n\n\ {message}\n", captured_at = chrono_like_now(), + agent_id = metadata.agent_id, + event_id = metadata.event_id, + message_id = metadata.message_id, + session_key = metadata.session_key, + order_id = metadata.order_id.unwrap_or(""), + event_type = metadata.event_type.unwrap_or(""), + hook_name = metadata.hook_name, + message = metadata.message, ); fs::write(&path, content).with_context(|| format!("failed to write {}", path.display()))?; Ok(path) @@ -104,6 +126,8 @@ fn parse_transcript(path: &PathBuf) -> Result { let mut event_id = None; let mut message_id = None; let mut session_key = None; + let mut order_id = None; + let mut event_type = None; let mut hook_name = None; let mut captured_at = None; let mut in_message = false; @@ -126,6 +150,14 @@ fn parse_transcript(path: &PathBuf) -> Result { message_id = Some(value.to_string()); } else if let Some(value) = line.strip_prefix("- sessionKey: ") { session_key = Some(value.to_string()); + } else if let Some(value) = line.strip_prefix("- orderId: ") { + if !value.is_empty() { + order_id = Some(value.to_string()); + } + } else if let Some(value) = line.strip_prefix("- eventType: ") { + if !value.is_empty() { + event_type = Some(value.to_string()); + } } else if let Some(value) = line.strip_prefix("- hookName: ") { hook_name = Some(value.to_string()); } else if let Some(value) = line.strip_prefix("- capturedAt: ") { @@ -138,12 +170,62 @@ fn parse_transcript(path: &PathBuf) -> Result { event_id: event_id.context("missing eventId in transcript")?, message_id: message_id.context("missing messageId in transcript")?, session_key: session_key.context("missing sessionKey in transcript")?, + order_id, + event_type, hook_name: hook_name.context("missing hookName in transcript")?, captured_at: captured_at.context("missing capturedAt in transcript")?, message: message_lines.join("\n").trim().to_string(), }) } +pub fn summarize_order_messages(agent_id: &str, order_id: &str) -> Result { + let dir = transcripts_dir()?; + if !dir.exists() { + return Ok(LocalOrderMessageUsage { + interaction_rounds: 0, + input_tokens: 0, + }); + } + + let entries = + fs::read_dir(&dir).with_context(|| format!("failed to read {}", dir.display()))?; + let mut usage = LocalOrderMessageUsage { + interaction_rounds: 0, + input_tokens: 0, + }; + for entry in entries { + let entry = entry?; + if !entry.file_type()?.is_file() { + continue; + } + let path = entry.path(); + if path.extension().and_then(|ext| ext.to_str()) != Some("md") { + continue; + } + let transcript = parse_transcript(&path)?; + if transcript.agent_id != agent_id || transcript.order_id.as_deref() != Some(order_id) { + continue; + } + if transcript.event_type.as_deref() == Some("order.message") { + usage.interaction_rounds = usage.interaction_rounds.saturating_add(1); + usage.input_tokens = usage + .input_tokens + .saturating_add(token_count(&transcript.message)); + } + } + Ok(usage) +} + +fn token_count(content: &str) -> i32 { + let normalized = content.replace("\r\n", "\n"); + i32::try_from( + tiktoken_rs::cl100k_base() + .map(|bpe| bpe.encode_ordinary(&normalized).len()) + .unwrap_or_else(|_| normalized.split_whitespace().count()), + ) + .unwrap_or(i32::MAX) +} + #[cfg(test)] mod tests { use std::env; @@ -158,20 +240,23 @@ mod tests { env::set_var("HOME", temp.display().to_string()); } - let path = store( - "agent-1", - "stream-1", - "order.paid:1", - "hook:corall:1", - "Corall", - "hello world", - ) + let path = store(TranscriptMetadata { + agent_id: "agent-1", + event_id: "stream-1", + message_id: "order.paid:1", + session_key: "hook:corall:1", + order_id: Some("ord-1"), + event_type: Some("order.paid"), + hook_name: "Corall", + message: "hello world", + }) .unwrap(); assert!(path.exists()); let loaded = load_by_session_key("hook:corall:1").unwrap(); assert_eq!(loaded.agent_id, "agent-1"); assert_eq!(loaded.message_id, "order.paid:1"); + assert_eq!(loaded.order_id.as_deref(), Some("ord-1")); assert_eq!(loaded.message, "hello world"); if let Some(home) = original_home { @@ -181,6 +266,48 @@ mod tests { } } + #[test] + fn summarize_order_messages_counts_only_message_events() { + let temp = tempfile_dir("corall-transcript-summary"); + let original_home = env::var_os("HOME"); + unsafe { + env::set_var("HOME", temp.display().to_string()); + } + + store(TranscriptMetadata { + agent_id: "agent-1", + event_id: "stream-1", + message_id: "order.paid:1", + session_key: "hook:corall:ord-1", + order_id: Some("ord-1"), + event_type: Some("order.paid"), + hook_name: "Corall", + message: "initial payload", + }) + .unwrap(); + store(TranscriptMetadata { + agent_id: "agent-1", + event_id: "stream-2", + message_id: "order.message:1", + session_key: "hook:corall:ord-1:message:1", + order_id: Some("ord-1"), + event_type: Some("order.message"), + hook_name: "Corall", + message: "follow-up question", + }) + .unwrap(); + + let usage = summarize_order_messages("agent-1", "ord-1").unwrap(); + assert_eq!(usage.interaction_rounds, 1); + assert!(usage.input_tokens > 0); + + if let Some(home) = original_home { + unsafe { + env::set_var("HOME", home); + } + } + } + fn tempfile_dir(prefix: &str) -> PathBuf { let base = std::env::temp_dir().join(format!("{prefix}-{}", std::process::id())); let _ = fs::remove_dir_all(&base); diff --git a/tests/eventbus_poll_cli.rs b/tests/eventbus_poll_cli.rs index 9fa6e7b..0e2a7be 100644 --- a/tests/eventbus_poll_cli.rs +++ b/tests/eventbus_poll_cli.rs @@ -242,6 +242,208 @@ mod unix_only { Ok(()) } + #[test] + fn eventbus_poll_rejects_leaked_order_message_beyond_declared_round_limit() + -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-round-cap")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let agent_id = unique_id("agent_round_cap"); + let polling_token = "round-cap-token"; + let hook_token = "local-hook-token"; + let eventbus = FakeEventbusServer::start( + &agent_id, + polling_token, + vec![ + json!({ + "id": "stream-hook-round-1", + "eventId": "order.message:hook-round-1", + "orderId": "ord-round-1", + "type": "order.message", + "orderPolicy": { + "includedInputTokens": 16000, + "includedOutputTokens": 8000, + "maxTotalTokens": 24000, + "maxInteractionRounds": 1 + }, + "hook": { + "message": "first round message", + "name": "Corall", + "sessionKey": "hook:corall:ord-round-1:message:1", + "deliver": false + } + }), + json!({ + "id": "stream-hook-round-2", + "eventId": "order.message:hook-round-2", + "orderId": "ord-round-1", + "type": "order.message", + "orderPolicy": { + "includedInputTokens": 16000, + "includedOutputTokens": 8000, + "maxTotalTokens": 24000, + "maxInteractionRounds": 1 + }, + "hook": { + "message": "second leaked message", + "name": "Corall", + "sessionKey": "hook:corall:ord-round-1:message:2", + "deliver": false + } + }), + ], + )?; + let hook_server = FakeHookServer::start(Some(hook_token))?; + + let stdout_path = temp.path().join("poller.stdout.log"); + let stderr_path = temp.path().join("poller.stderr.log"); + let mut child = ChildGuard::spawn( + env!("CARGO_BIN_EXE_corall"), + &[ + "--profile", + "provider", + "eventbus", + "poll", + "--base-url", + &eventbus.base_url(), + "--agent-id", + &agent_id, + "--webhook-token", + polling_token, + "--hook-url", + &hook_server.url(), + "--hook-token", + hook_token, + "--wait-ms", + "5", + "--request-timeout-ms", + "1000", + "--ack-timeout-ms", + "1000", + "--idle-delay-ms", + "50", + ], + &home, + &stdout_path, + &stderr_path, + )?; + + wait_until(Duration::from_secs(5), || { + hook_server.request_count() == 1 + && eventbus.ack_count("stream-hook-round-1") == 1 + && eventbus.ack_count("stream-hook-round-2") == 1 + })?; + + let requests = hook_server.requests(); + assert_eq!(requests.len(), 1); + assert_eq!(requests[0].body["message"], "first round message"); + let stderr = fs::read_to_string(&stderr_path).unwrap_or_default(); + assert!(stderr.contains("client rejected leaked event")); + + assert!( + child.is_running()?, + "poller died early\nstdout:\n{}\nstderr:\n{}", + fs::read_to_string(&stdout_path).unwrap_or_default(), + stderr + ); + + child.kill(); + Ok(()) + } + + #[test] + fn eventbus_poll_rejects_leaked_order_message_beyond_declared_token_limit() + -> Result<(), Box> { + let temp = TempDir::new("corall-eventbus-poll-token-cap")?; + let home = temp.path().join("home"); + fs::create_dir_all(&home)?; + + let agent_id = unique_id("agent_token_cap"); + let polling_token = "token-cap-token"; + let hook_token = "local-hook-token"; + let eventbus = FakeEventbusServer::start( + &agent_id, + polling_token, + vec![json!({ + "id": "stream-hook-token-1", + "eventId": "order.message:hook-token-1", + "orderId": "ord-token-1", + "type": "order.message", + "orderPolicy": { + "includedInputTokens": 5, + "includedOutputTokens": 8000, + "maxTotalTokens": 10, + "maxInteractionRounds": 3 + }, + "orderUsageBefore": { + "inputTokens": 4, + "outputTokens": 3, + "totalTokens": 7, + "interactionRounds": 1 + }, + "hook": { + "message": "one two three", + "name": "Corall", + "sessionKey": "hook:corall:ord-token-1:message:1", + "deliver": false + } + })], + )?; + let hook_server = FakeHookServer::start(Some(hook_token))?; + + let stdout_path = temp.path().join("poller.stdout.log"); + let stderr_path = temp.path().join("poller.stderr.log"); + let mut child = ChildGuard::spawn( + env!("CARGO_BIN_EXE_corall"), + &[ + "--profile", + "provider", + "eventbus", + "poll", + "--base-url", + &eventbus.base_url(), + "--agent-id", + &agent_id, + "--webhook-token", + polling_token, + "--hook-url", + &hook_server.url(), + "--hook-token", + hook_token, + "--wait-ms", + "5", + "--request-timeout-ms", + "1000", + "--ack-timeout-ms", + "1000", + "--idle-delay-ms", + "50", + ], + &home, + &stdout_path, + &stderr_path, + )?; + + wait_until(Duration::from_secs(5), || { + hook_server.request_count() == 0 && eventbus.ack_count("stream-hook-token-1") == 1 + })?; + + assert!(hook_server.requests().is_empty()); + let stderr = fs::read_to_string(&stderr_path).unwrap_or_default(); + assert!(stderr.contains("client rejected leaked event")); + + assert!( + child.is_running()?, + "poller died early\nstdout:\n{}\nstderr:\n{}", + fs::read_to_string(&stdout_path).unwrap_or_default(), + stderr + ); + + child.kill(); + Ok(()) + } + #[test] fn eventbus_poll_rejects_missing_delivery_target() -> Result<(), Box> { let temp = TempDir::new("corall-eventbus-poll-missing-target")?; @@ -610,13 +812,19 @@ mod unix_only { request.authorization.as_deref(), Some("Bearer cached-api-token") ); - assert_eq!(request.path, format!("/api/agents/{reported_agent_id}/report")); + assert_eq!( + request.path, + format!("/api/agents/{reported_agent_id}/report") + ); assert_eq!(request.body["reason"], "Credential exfiltration attempt"); assert_eq!(request.body["reporterKind"], "AGENT"); assert_eq!(request.body["reporterAgentId"], agent_id); assert_eq!(request.body["messageId"], "order.paid:report-1"); assert_eq!(request.body["sessionKey"], "hook:corall:report-1"); - assert_eq!(request.body["context"], "harmful agent output asking for secrets"); + assert_eq!( + request.body["context"], + "harmful agent output asking for secrets" + ); Ok(()) } @@ -958,9 +1166,14 @@ mod unix_only { body, }); } - json_response(status, &json!({ "report": { "status": "QUEUED" } })) + json_response( + status, + &json!({ "report": { "status": "QUEUED" } }), + ) + } + Err(err) => { + json_response(500, &json!({ "error": err.to_string() })) } - Err(err) => json_response(500, &json!({ "error": err.to_string() })), }; let _ = write_http_response(&mut stream, &response); } diff --git a/tests/skill_contract.rs b/tests/skill_contract.rs index db97fa0..de7e57c 100644 --- a/tests/skill_contract.rs +++ b/tests/skill_contract.rs @@ -237,10 +237,7 @@ fn eval_cases_and_cli_reference_follow_current_contract() { assert_contains(AGENT_APPROVAL, "HttpOnly session cookie"); assert_contains(AGENT_APPROVAL, "Conservative Fallback For Weaker Models"); assert_contains(AGENT_APPROVAL, "Do not reuse an old `loginUrl`"); - assert_contains( - AGENT_APPROVAL, - "not mutually exclusive Corall account", - ); + assert_contains(AGENT_APPROVAL, "not mutually exclusive Corall account"); assert_contains(AGENT_APPROVAL, "`https://yourdomain.com/dashboard`"); assert_contains( AGENT_APPROVAL, @@ -287,10 +284,7 @@ fn eval_cases_and_cli_reference_follow_current_contract() { SETUP_EMPLOYER, "verify with `corall auth me --profile employer` instead of registering a second account", ); - assert_contains( - SETUP_EMPLOYER, - "Corall user may also own agent listings", - ); + assert_contains(SETUP_EMPLOYER, "Corall user may also own agent listings"); assert_contains(FILE_UPLOAD, "Conservative Fallback For Weaker Models"); assert_contains(FILE_UPLOAD, "python3 -c"); assert_contains(FILE_UPLOAD, "stop and report the exact JSON"); From dfbd2e84f606d97b31a009c4f03730a90cd1e1dc Mon Sep 17 00:00:00 2001 From: "Ryan.K" <662346+RyanKung@users.noreply.github.com> Date: Thu, 14 May 2026 19:05:09 +0800 Subject: [PATCH 13/14] Update auth register to signed challenge flow --- src/client.rs | 32 ++++++++++++++++++++++++++++++++ src/commands/auth.rs | 7 ++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/client.rs b/src/client.rs index a41b0db..420a529 100644 --- a/src/client.rs +++ b/src/client.rs @@ -158,6 +158,38 @@ impl ApiClient { .context("no token in auth response") } + pub async fn register_with_key( + &self, + public_key: &str, + private_key_pkcs8: &str, + name: &str, + ) -> Result { + let challenge_resp = self + .request(Method::POST, "/api/auth/challenge") + .json(&serde_json::json!({ "publicKey": public_key })) + .send() + .await + .context("request failed")?; + let challenge_body = Self::handle(challenge_resp).await?; + let challenge = challenge_body + .get("challenge") + .and_then(|v| v.as_str()) + .context("no challenge in auth response")?; + let signature = credentials::sign_challenge(private_key_pkcs8, challenge)?; + + let register_resp = self + .request(Method::POST, "/api/auth/register") + .json(&serde_json::json!({ + "publicKey": public_key, + "name": name, + "signature": signature, + })) + .send() + .await + .context("request failed")?; + Self::handle(register_resp).await + } + pub async fn approve_agent_approval(&self, cred: &Credential) -> Result { let challenge_resp = self .request(Method::POST, "/api/auth/agent-approval/challenge") diff --git a/src/commands/auth.rs b/src/commands/auth.rs index ac7ed80..e435c51 100644 --- a/src/commands/auth.rs +++ b/src/commands/auth.rs @@ -55,9 +55,10 @@ pub async fn run(cmd: AuthCommand, profile: &str) -> Result<()> { name, } => { let key = credentials::generate_key()?; - let mut client = ApiClient::new(site_to_base_url(&site)); - let body = json!({ "publicKey": &key.public_key, "name": name }); - let resp = client.post("/api/auth/register", &body).await?; + let client = ApiClient::new(site_to_base_url(&site)); + let resp = client + .register_with_key(&key.public_key, &key.private_key_pkcs8, &name) + .await?; let user = resp.get("user").cloned().unwrap_or_default(); let user_id = user From 70042c701cd4859023ca05375b42a952d385229f Mon Sep 17 00:00:00 2001 From: "Ryan.K" <662346+RyanKung@users.noreply.github.com> Date: Thu, 4 Jun 2026 15:07:25 +0800 Subject: [PATCH 14/14] Fix PR formatting checks --- src/commands/auth.rs | 27 +-- src/eventbus.rs | 59 ++--- tests/agent_approval.rs | 55 ++--- tests/eventbus_poll_cli.rs | 471 ++++++++++++++++--------------------- tests/eventbus_polling.rs | 35 ++- tests/openclaw_setup.rs | 15 +- tests/reviews_cli.rs | 98 ++++---- 7 files changed, 334 insertions(+), 426 deletions(-) diff --git a/src/commands/auth.rs b/src/commands/auth.rs index e435c51..24a5334 100644 --- a/src/commands/auth.rs +++ b/src/commands/auth.rs @@ -81,22 +81,19 @@ pub async fn run(cmd: AuthCommand, profile: &str) -> Result<()> { .map(str::to_owned); let token_expires_at = token.as_ref().map(|_| token_expiry_timestamp()); - credentials::save( - profile, - &Credential { - site, - user: CredentialUser { - id: user_id, - public_key, - }, - private_key_pkcs8: key.private_key_pkcs8, - agent_id: None, - polling_token: None, - registered_at, - token, - token_expires_at, + credentials::save(profile, &Credential { + site, + user: CredentialUser { + id: user_id, + public_key, }, - )?; + private_key_pkcs8: key.private_key_pkcs8, + agent_id: None, + polling_token: None, + registered_at, + token, + token_expires_at, + })?; println!("{}", serde_json::to_string_pretty(&resp)?); } diff --git a/src/eventbus.rs b/src/eventbus.rs index a026c6e..08ab6de 100644 --- a/src/eventbus.rs +++ b/src/eventbus.rs @@ -259,12 +259,9 @@ impl HttpError { if self.status == 401 { HttpResponse::unauthorized(&self.message) } else { - HttpResponse::json( - self.status, - &ErrorResponse { - error: &self.message, - }, - ) + HttpResponse::json(self.status, &ErrorResponse { + error: &self.message, + }) } } } @@ -569,13 +566,10 @@ async fn dispatch_request( state.store.health().await.map_err(|err| { HttpError::service_unavailable(format!("redis health check failed: {err}")) })?; - Ok(HttpResponse::json( - 200, - &HealthResponse { - ok: true, - redis: "ok", - }, - )) + Ok(HttpResponse::json(200, &HealthResponse { + ok: true, + redis: "ok", + })) } Route::Poll { agent_id } => { if request.method != "GET" { @@ -588,13 +582,10 @@ async fn dispatch_request( .poll(&agent_id, options.clone()) .await .map_err(|err| HttpError::service_unavailable(format!("poll failed: {err}")))?; - Ok(HttpResponse::json( - 200, - &PollResponse { - consumer_id: options.consumer_id, - events, - }, - )) + Ok(HttpResponse::json(200, &PollResponse { + consumer_id: options.consumer_id, + events, + })) } Route::Ack { agent_id, event_id } => { if request.method != "POST" { @@ -606,14 +597,11 @@ async fn dispatch_request( .ack(&agent_id, &event_id) .await .map_err(|err| HttpError::service_unavailable(format!("ack failed: {err}")))?; - Ok(HttpResponse::json( - 200, - &AckResponse { - ok: true, - acked, - event_id, - }, - )) + Ok(HttpResponse::json(200, &AckResponse { + ok: true, + acked, + event_id, + })) } } } @@ -1237,15 +1225,12 @@ mod tests { let (agent_id, poll) = store.last_poll.lock().unwrap().clone().unwrap(); assert_eq!(agent_id, "agent-1"); - assert_eq!( - poll, - PollOptions { - consumer_id: "worker-1".into(), - wait_ms: 1_500, - count: 2, - claim_idle_ms: Some(60_000), - } - ); + assert_eq!(poll, PollOptions { + consumer_id: "worker-1".into(), + wait_ms: 1_500, + count: 2, + claim_idle_ms: Some(60_000), + }); } #[tokio::test] diff --git a/tests/agent_approval.rs b/tests/agent_approval.rs index a730815..aafcb53 100644 --- a/tests/agent_approval.rs +++ b/tests/agent_approval.rs @@ -53,33 +53,27 @@ fn agent_ed25519_approval_signs_challenge_without_leaking_secrets() -> Result<() let approve_help_stdout = String::from_utf8(approve_help.stdout)?; assert!(!approve_help_stdout.contains("--code")); - let register = run_corall( - &home, - &[ - "--profile", - "agent-test", - "auth", - "register", - &server.base_url(), - "--name", - "Agent Test", - ], - )?; + let register = run_corall(&home, &[ + "--profile", + "agent-test", + "auth", + "register", + &server.base_url(), + "--name", + "Agent Test", + ])?; assert!(register.status.success(), "register failed: {register:?}"); let register_stdout = String::from_utf8(register.stdout)?; assert!(!register_stdout.contains("privateKeyPkcs8")); assert!(!register_stdout.contains("password")); - let approve = run_corall( - &home, - &[ - "--profile", - "agent-test", - "auth", - "approve", - &server.base_url(), - ], - )?; + let approve = run_corall(&home, &[ + "--profile", + "agent-test", + "auth", + "approve", + &server.base_url(), + ])?; assert!(approve.status.success(), "approve failed: {approve:?}"); let approve_stdout = String::from_utf8(approve.stdout)?; assert!(approve_stdout.contains(r#""approved": true"#)); @@ -91,16 +85,13 @@ fn agent_ed25519_approval_signs_challenge_without_leaking_secrets() -> Result<() assert!(!approve_stdout.contains("privateKeyPkcs8")); assert!(!approve_stdout.contains("signature")); - let wrong_site = run_corall( - &home, - &[ - "--profile", - "agent-test", - "auth", - "approve", - "http://127.0.0.1:9", - ], - )?; + let wrong_site = run_corall(&home, &[ + "--profile", + "agent-test", + "auth", + "approve", + "http://127.0.0.1:9", + ])?; assert!(!wrong_site.status.success()); let wrong_site_stderr = String::from_utf8(wrong_site.stderr)?; assert!(wrong_site_stderr.contains("belong to")); diff --git a/tests/eventbus_poll_cli.rs b/tests/eventbus_poll_cli.rs index 0e2a7be..00c1aa4 100644 --- a/tests/eventbus_poll_cli.rs +++ b/tests/eventbus_poll_cli.rs @@ -39,21 +39,17 @@ mod unix_only { let agent_id = unique_id("agent_exec"); let polling_token = "polling-secret"; - let eventbus = FakeEventbusServer::start( - &agent_id, - polling_token, - vec![json!({ - "id": "stream-exec-1", - "eventId": "order.paid:exec-1", - "type": "order.paid", - "hook": { - "message": "exec event", - "name": "Corall", - "sessionKey": "hook:corall:exec-1", - "deliver": false - } - })], - )?; + let eventbus = FakeEventbusServer::start(&agent_id, polling_token, vec![json!({ + "id": "stream-exec-1", + "eventId": "order.paid:exec-1", + "type": "order.paid", + "hook": { + "message": "exec event", + "name": "Corall", + "sessionKey": "hook:corall:exec-1", + "deliver": false + } + })])?; write_credentials(&home, "provider", &agent_id, polling_token)?; let worker_script = temp.path().join("worker.py"); @@ -146,21 +142,17 @@ mod unix_only { let agent_id = unique_id("agent_hook"); let polling_token = "hook-polling-secret"; let hook_token = "local-hook-token"; - let eventbus = FakeEventbusServer::start( - &agent_id, - polling_token, - vec![json!({ - "id": "stream-hook-1", - "eventId": "order.paid:hook-1", - "type": "order.paid", - "hook": { - "message": "hook event", - "name": "Corall", - "sessionKey": "hook:corall:hook-1", - "deliver": false - } - })], - )?; + let eventbus = FakeEventbusServer::start(&agent_id, polling_token, vec![json!({ + "id": "stream-hook-1", + "eventId": "order.paid:hook-1", + "type": "order.paid", + "hook": { + "message": "hook event", + "name": "Corall", + "sessionKey": "hook:corall:hook-1", + "deliver": false + } + })])?; let hook_server = FakeHookServer::start(Some(hook_token))?; let stdout_path = temp.path().join("poller.stdout.log"); @@ -252,48 +244,44 @@ mod unix_only { let agent_id = unique_id("agent_round_cap"); let polling_token = "round-cap-token"; let hook_token = "local-hook-token"; - let eventbus = FakeEventbusServer::start( - &agent_id, - polling_token, - vec![ - json!({ - "id": "stream-hook-round-1", - "eventId": "order.message:hook-round-1", - "orderId": "ord-round-1", - "type": "order.message", - "orderPolicy": { - "includedInputTokens": 16000, - "includedOutputTokens": 8000, - "maxTotalTokens": 24000, - "maxInteractionRounds": 1 - }, - "hook": { - "message": "first round message", - "name": "Corall", - "sessionKey": "hook:corall:ord-round-1:message:1", - "deliver": false - } - }), - json!({ - "id": "stream-hook-round-2", - "eventId": "order.message:hook-round-2", - "orderId": "ord-round-1", - "type": "order.message", - "orderPolicy": { - "includedInputTokens": 16000, - "includedOutputTokens": 8000, - "maxTotalTokens": 24000, - "maxInteractionRounds": 1 - }, - "hook": { - "message": "second leaked message", - "name": "Corall", - "sessionKey": "hook:corall:ord-round-1:message:2", - "deliver": false - } - }), - ], - )?; + let eventbus = FakeEventbusServer::start(&agent_id, polling_token, vec![ + json!({ + "id": "stream-hook-round-1", + "eventId": "order.message:hook-round-1", + "orderId": "ord-round-1", + "type": "order.message", + "orderPolicy": { + "includedInputTokens": 16000, + "includedOutputTokens": 8000, + "maxTotalTokens": 24000, + "maxInteractionRounds": 1 + }, + "hook": { + "message": "first round message", + "name": "Corall", + "sessionKey": "hook:corall:ord-round-1:message:1", + "deliver": false + } + }), + json!({ + "id": "stream-hook-round-2", + "eventId": "order.message:hook-round-2", + "orderId": "ord-round-1", + "type": "order.message", + "orderPolicy": { + "includedInputTokens": 16000, + "includedOutputTokens": 8000, + "maxTotalTokens": 24000, + "maxInteractionRounds": 1 + }, + "hook": { + "message": "second leaked message", + "name": "Corall", + "sessionKey": "hook:corall:ord-round-1:message:2", + "deliver": false + } + }), + ])?; let hook_server = FakeHookServer::start(Some(hook_token))?; let stdout_path = temp.path().join("poller.stdout.log"); @@ -362,34 +350,30 @@ mod unix_only { let agent_id = unique_id("agent_token_cap"); let polling_token = "token-cap-token"; let hook_token = "local-hook-token"; - let eventbus = FakeEventbusServer::start( - &agent_id, - polling_token, - vec![json!({ - "id": "stream-hook-token-1", - "eventId": "order.message:hook-token-1", - "orderId": "ord-token-1", - "type": "order.message", - "orderPolicy": { - "includedInputTokens": 5, - "includedOutputTokens": 8000, - "maxTotalTokens": 10, - "maxInteractionRounds": 3 - }, - "orderUsageBefore": { - "inputTokens": 4, - "outputTokens": 3, - "totalTokens": 7, - "interactionRounds": 1 - }, - "hook": { - "message": "one two three", - "name": "Corall", - "sessionKey": "hook:corall:ord-token-1:message:1", - "deliver": false - } - })], - )?; + let eventbus = FakeEventbusServer::start(&agent_id, polling_token, vec![json!({ + "id": "stream-hook-token-1", + "eventId": "order.message:hook-token-1", + "orderId": "ord-token-1", + "type": "order.message", + "orderPolicy": { + "includedInputTokens": 5, + "includedOutputTokens": 8000, + "maxTotalTokens": 10, + "maxInteractionRounds": 3 + }, + "orderUsageBefore": { + "inputTokens": 4, + "outputTokens": 3, + "totalTokens": 7, + "interactionRounds": 1 + }, + "hook": { + "message": "one two three", + "name": "Corall", + "sessionKey": "hook:corall:ord-token-1:message:1", + "deliver": false + } + })])?; let hook_server = FakeHookServer::start(Some(hook_token))?; let stdout_path = temp.path().join("poller.stdout.log"); @@ -450,19 +434,16 @@ mod unix_only { let home = temp.path().join("home"); fs::create_dir_all(&home)?; - let output = run_corall( - &home, - &[ - "eventbus", - "poll", - "--base-url", - "http://127.0.0.1:8787", - "--agent-id", - "agent-missing-target", - "--webhook-token", - "polling-token", - ], - )?; + let output = run_corall(&home, &[ + "eventbus", + "poll", + "--base-url", + "http://127.0.0.1:8787", + "--agent-id", + "agent-missing-target", + "--webhook-token", + "polling-token", + ])?; assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr)?; @@ -476,23 +457,20 @@ mod unix_only { let home = temp.path().join("home"); fs::create_dir_all(&home)?; - let output = run_corall( - &home, - &[ - "eventbus", - "poll", - "--base-url", - "http://127.0.0.1:8787", - "--agent-id", - "agent-conflicting-target", - "--webhook-token", - "polling-token", - "--hook-url", - "http://127.0.0.1:9000/hooks/agent", - "--exec", - "true", - ], - )?; + let output = run_corall(&home, &[ + "eventbus", + "poll", + "--base-url", + "http://127.0.0.1:8787", + "--agent-id", + "agent-conflicting-target", + "--webhook-token", + "polling-token", + "--hook-url", + "http://127.0.0.1:9000/hooks/agent", + "--exec", + "true", + ])?; assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr)?; @@ -506,19 +484,16 @@ mod unix_only { let home = temp.path().join("home"); fs::create_dir_all(&home)?; - let output = run_corall( - &home, - &[ - "eventbus", - "poll", - "--base-url", - "http://127.0.0.1:8787", - "--agent-id", - "agent-missing-token", - "--exec", - "true", - ], - )?; + let output = run_corall(&home, &[ + "eventbus", + "poll", + "--base-url", + "http://127.0.0.1:8787", + "--agent-id", + "agent-missing-token", + "--exec", + "true", + ])?; assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr)?; @@ -533,14 +508,11 @@ mod unix_only { fs::create_dir_all(&home)?; let agent_id = unique_id("agent_invalid_json"); - let eventbus = FakeEventbusServer::start_with_poll_responses( - &agent_id, - "polling-secret", - vec![ + let eventbus = + FakeEventbusServer::start_with_poll_responses(&agent_id, "polling-secret", vec![ PollResponse::raw(200, "application/json", "not-json"), PollResponse::raw(200, "application/json", "still-not-json"), - ], - )?; + ])?; let stdout_path = temp.path().join("poller.stdout.log"); let stderr_path = temp.path().join("poller.stderr.log"); @@ -602,45 +574,38 @@ mod unix_only { let agent_id = unique_id("agent_hook_fail"); let polling_token = "hook-fail-token"; - let eventbus = FakeEventbusServer::start( - &agent_id, - polling_token, - vec![json!({ - "id": "stream-hook-fail-1", - "eventId": "order.paid:hook-fail-1", - "hook": { - "message": "hook failure", - "name": "Corall", - "sessionKey": "hook:corall:hook-fail-1", - "deliver": false - } - })], - )?; + let eventbus = FakeEventbusServer::start(&agent_id, polling_token, vec![json!({ + "id": "stream-hook-fail-1", + "eventId": "order.paid:hook-fail-1", + "hook": { + "message": "hook failure", + "name": "Corall", + "sessionKey": "hook:corall:hook-fail-1", + "deliver": false + } + })])?; let hook_server = FakeHookServer::start(Some("expected-hook-token"))?; - let output = run_corall( - &home, - &[ - "eventbus", - "poll", - "--base-url", - &eventbus.base_url(), - "--agent-id", - &agent_id, - "--webhook-token", - polling_token, - "--hook-url", - &hook_server.url(), - "--hook-token", - "wrong-hook-token", - "--wait-ms", - "5", - "--request-timeout-ms", - "1000", - "--ack-timeout-ms", - "1000", - ], - )?; + let output = run_corall(&home, &[ + "eventbus", + "poll", + "--base-url", + &eventbus.base_url(), + "--agent-id", + &agent_id, + "--webhook-token", + polling_token, + "--hook-url", + &hook_server.url(), + "--hook-token", + "wrong-hook-token", + "--wait-ms", + "5", + "--request-timeout-ms", + "1000", + "--ack-timeout-ms", + "1000", + ])?; assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr)?; @@ -658,45 +623,38 @@ mod unix_only { let agent_id = unique_id("agent_exec_fail"); let polling_token = "exec-fail-token"; - let eventbus = FakeEventbusServer::start( + let eventbus = FakeEventbusServer::start(&agent_id, polling_token, vec![json!({ + "id": "stream-exec-fail-1", + "eventId": "order.paid:exec-fail-1", + "hook": { + "message": "exec failure", + "name": "Corall", + "sessionKey": "hook:corall:exec-fail-1", + "deliver": false + } + })])?; + + let output = run_corall(&home, &[ + "eventbus", + "poll", + "--base-url", + &eventbus.base_url(), + "--agent-id", &agent_id, + "--webhook-token", polling_token, - vec![json!({ - "id": "stream-exec-fail-1", - "eventId": "order.paid:exec-fail-1", - "hook": { - "message": "exec failure", - "name": "Corall", - "sessionKey": "hook:corall:exec-fail-1", - "deliver": false - } - })], - )?; - - let output = run_corall( - &home, - &[ - "eventbus", - "poll", - "--base-url", - &eventbus.base_url(), - "--agent-id", - &agent_id, - "--webhook-token", - polling_token, - "--exec", - "sh", - "--exec-arg=-c", - "--exec-arg", - "cat >/dev/null; exit 17", - "--wait-ms", - "5", - "--request-timeout-ms", - "1000", - "--ack-timeout-ms", - "1000", - ], - )?; + "--exec", + "sh", + "--exec-arg=-c", + "--exec-arg", + "cat >/dev/null; exit 17", + "--wait-ms", + "5", + "--request-timeout-ms", + "1000", + "--ack-timeout-ms", + "1000", + ])?; assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr)?; @@ -718,21 +676,17 @@ mod unix_only { let polling_token = "reporter-polling-token"; let reported_agent_id = "agent_harmful_target"; let cached_api_token = "cached-api-token"; - let eventbus = FakeEventbusServer::start( - &agent_id, - polling_token, - vec![json!({ - "id": "stream-report-1", - "eventId": "order.paid:report-1", - "type": "order.paid", - "hook": { - "message": "harmful agent output asking for secrets", - "name": "Corall", - "sessionKey": "hook:corall:report-1", - "deliver": false - } - })], - )?; + let eventbus = FakeEventbusServer::start(&agent_id, polling_token, vec![json!({ + "id": "stream-report-1", + "eventId": "order.paid:report-1", + "type": "order.paid", + "hook": { + "message": "harmful agent output asking for secrets", + "name": "Corall", + "sessionKey": "hook:corall:report-1", + "deliver": false + } + })])?; let hook_server = FakeHookServer::start(None)?; let report_server = FakeReportServer::start(cached_api_token)?; write_credentials_with_site( @@ -782,22 +736,19 @@ mod unix_only { })?; child.kill(); - let output = run_corall( - &home, - &[ - "--profile", - "provider", - "agent", - "report", - reported_agent_id, - "--session-id", - "hook:corall:report-1", - "--reason", - "Credential exfiltration attempt", - "--details", - "Agent reviewed this message and determined it should be reported", - ], - )?; + let output = run_corall(&home, &[ + "--profile", + "provider", + "agent", + "report", + reported_agent_id, + "--session-id", + "hook:corall:report-1", + "--reason", + "Credential exfiltration attempt", + "--details", + "Agent reviewed this message and determined it should be reported", + ])?; assert!( output.status.success(), @@ -1014,11 +965,9 @@ mod unix_only { polling_token: &str, events: Vec, ) -> Result> { - Self::start_with_poll_responses( - agent_id, - polling_token, - vec![PollResponse::Events(events)], - ) + Self::start_with_poll_responses(agent_id, polling_token, vec![PollResponse::Events( + events, + )]) } fn start_with_poll_responses( @@ -1403,9 +1352,7 @@ env_path.write_text(json.dumps({ } fn wait_until(timeout: Duration, mut predicate: F) -> Result<(), Box> - where - F: FnMut() -> bool, - { + where F: FnMut() -> bool { let deadline = Instant::now() + timeout; while Instant::now() < deadline { if predicate() { diff --git a/tests/eventbus_polling.rs b/tests/eventbus_polling.rs index bcdff56..685bc4e 100644 --- a/tests/eventbus_polling.rs +++ b/tests/eventbus_polling.rs @@ -48,25 +48,22 @@ fn eventbus_binary_polls_and_acks_redis_without_llm_config() -> Result<(), Box Result<(), Bo assert!(staged.join("dist/index.js").is_file()); let captured = fs::read_to_string(&capture_path)?; - assert_eq!( - captured.lines().collect::>(), - vec![ - "plugins", - "install", - "--force", - staged.to_str().ok_or("staged plugin path is not utf-8")? - ] - ); + assert_eq!(captured.lines().collect::>(), vec![ + "plugins", + "install", + "--force", + staged.to_str().ok_or("staged plugin path is not utf-8")? + ]); let cfg: Value = serde_json::from_str(&fs::read_to_string(&config_path)?)?; let plugin = &cfg["plugins"]["entries"]["corall-polling"]; diff --git a/tests/reviews_cli.rs b/tests/reviews_cli.rs index a91431f..ba9d3e4 100644 --- a/tests/reviews_cli.rs +++ b/tests/reviews_cli.rs @@ -31,26 +31,23 @@ fn reviews_create_uses_explicit_rating_without_penalty_payload() -> Result<(), B let server = FakeReviewsServer::start()?; write_credentials(&home, "employer", &server.base_url(), "cached-review-token")?; - let output = run_corall( - &home, - &[ - "--profile", - "employer", - "reviews", - "create", - "ord-rating-1", - "--rating", - "4.9", - "--comment", - "Exact user score.", - "--reviewer-kind", - "system", - "--requirement-miss", - "3", - "--correctness-defect", - "2", - ], - )?; + let output = run_corall(&home, &[ + "--profile", + "employer", + "reviews", + "create", + "ord-rating-1", + "--rating", + "4.9", + "--comment", + "Exact user score.", + "--reviewer-kind", + "system", + "--requirement-miss", + "3", + "--correctness-defect", + "2", + ])?; assert!(output.status.success(), "reviews create failed: {output:?}"); let request = server.requests().pop().ok_or("expected review request")?; @@ -75,24 +72,21 @@ fn reviews_create_uses_penalty_payload_when_rating_is_omitted() -> Result<(), Bo let server = FakeReviewsServer::start()?; write_credentials(&home, "employer", &server.base_url(), "cached-review-token")?; - let output = run_corall( - &home, - &[ - "--profile", - "employer", - "reviews", - "create", - "ord-penalty-1", - "--comment", - "Needs rework.", - "--reviewer-kind", - "employer-agent", - "--correctness-defect", - "1", - "--rework-burden", - "2", - ], - )?; + let output = run_corall(&home, &[ + "--profile", + "employer", + "reviews", + "create", + "ord-penalty-1", + "--comment", + "Needs rework.", + "--reviewer-kind", + "employer-agent", + "--correctness-defect", + "1", + "--rework-burden", + "2", + ])?; assert!(output.status.success(), "reviews create failed: {output:?}"); let request = server.requests().pop().ok_or("expected review request")?; @@ -117,10 +111,13 @@ fn reviews_create_uses_penalty_payload_when_rating_is_omitted() -> Result<(), Bo #[test] fn reviews_create_rejects_out_of_range_rating() -> Result<(), Box> { let temp = TempDir::new("corall-reviews-bad-rating")?; - let output = run_corall( - temp.path(), - &["reviews", "create", "ord-invalid-rating", "--rating", "5.1"], - )?; + let output = run_corall(temp.path(), &[ + "reviews", + "create", + "ord-invalid-rating", + "--rating", + "5.1", + ])?; assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr)?; @@ -131,16 +128,13 @@ fn reviews_create_rejects_out_of_range_rating() -> Result<(), Box> { #[test] fn reviews_create_rejects_out_of_range_penalty() -> Result<(), Box> { let temp = TempDir::new("corall-reviews-bad-penalty")?; - let output = run_corall( - temp.path(), - &[ - "reviews", - "create", - "ord-invalid-penalty", - "--timeliness-miss", - "4", - ], - )?; + let output = run_corall(temp.path(), &[ + "reviews", + "create", + "ord-invalid-penalty", + "--timeliness-miss", + "4", + ])?; assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr)?;