Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(okx-chat-bot): keep the worker reporting health from its own process instead of dying silent

nirholas79bdb690fe16
diff --git a/tests/okx-chat-bot.test.js b/tests/okx-chat-bot.test.js
index a4e7789f8..ef3a3bca4 100644
--- a/tests/okx-chat-bot.test.js
+++ b/tests/okx-chat-bot.test.js
@@ -16,7 +16,7 @@ import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterAll, describe, it, expect, vi } from 'vitest';
import { classify, loginInstructions } from '../workers/okx-chat-bot/session.js';
-import { resolveProvider, loadConfig, paths } from '../workers/okx-chat-bot/config.js';
+import { resolveProvider, resolveHost, loadConfig, paths } from '../workers/okx-chat-bot/config.js';
import { createHealthHandler } from '../workers/okx-chat-bot/health-server.js';
import { createSupervisor } from '../workers/okx-chat-bot/supervisor.js';
import { STATE_ROOTS, STATE_EXCLUDES } from '../workers/okx-chat-bot/state.js';
@@ -129,6 +129,31 @@ describe('okx-chat-bot provider selection', () => {
});
});
+describe('okx-chat-bot host reporting', () => {
+ it('calls Cloud Run the durable host and names the revision', () => {
+ const h = resolveHost({ K_SERVICE: 'okx-chat-bot', K_REVISION: 'okx-chat-bot-00001-abc' });
+ expect(h.durable).toBe(true);
+ expect(h.label).toContain('okx-chat-bot-00001-abc');
+ });
+
+ // The stopgap this worker replaces runs on a codespace. It must never be able
+ // to report itself the way the always-on host does.
+ it('never lets a codespace claim durability', () => {
+ expect(resolveHost({ CODESPACE_NAME: 'fluffy-space-fiesta' })).toMatchObject({
+ label: 'codespace:fluffy-space-fiesta',
+ durable: false,
+ });
+ expect(resolveHost({ OKX_BOT_HOST_LABEL: 'vm:box-under-a-desk' }).durable).toBe(false);
+ expect(resolveHost({ OKX_BOT_HOST_LABEL: 'vm:box-under-a-desk', OKX_BOT_HOST_DURABLE: '1' }).durable).toBe(true);
+ });
+
+ it('puts the host on the status body a human reads', () => {
+ const cfg = loadConfig({ CODESPACE_NAME: 'fluffy-space-fiesta', PORT: '0' });
+ expect(cfg.host).toBe('codespace:fluffy-space-fiesta');
+ expect(cfg.hostDurable).toBe(false);
+ });
+});
+
describe('okx-chat-bot state contract', () => {
it('carries the identity files a session survives on', () => {
expect(STATE_ROOTS).toContain('.onchainos');
@@ -394,6 +419,28 @@ describe('okx_chat_bot subsystem health', () => {
expect(s.hint).toContain('/readyz');
});
+ it('names the host on a healthy beat', () => {
+ const s = classifyOkxChatBotBeat(beat(20_000, { host: 'cloudrun:okx-chat-bot', hostDurable: true }), now);
+ expect(s.status).toBe('ok');
+ expect(s.detail).toContain('cloudrun:okx-chat-bot');
+ });
+
+ // An online stopgap and an online always-on host are not the same news.
+ // Painting both green is the false-green this worker exists to kill.
+ it('refuses to call a stopgap host green, and points at the deploy', () => {
+ const s = classifyOkxChatBotBeat(beat(20_000, { host: 'codespace:fluffy', hostDurable: false }), now);
+ expect(s.status).toBe('degraded');
+ expect(s.detail).toContain('codespace:fluffy');
+ expect(s.detail).toMatch(/stopgap/);
+ expect(s.hint).toContain('workers/okx-chat-bot/cloudbuild.yaml');
+ });
+
+ // Beats predating the host field must keep reading as ok, not degrade on a
+ // missing key.
+ it('stays ok for a beat that never reported a host', () => {
+ expect(classifyOkxChatBotBeat(beat(20_000), now).status).toBe('ok');
+ });
+
it('passes a degraded self-report through without escalating it', () => {
expect(classifyOkxChatBotBeat(beat(20_000, { health: 'degraded', detail: '0 XMTP clients' }), now).status).toBe(
'degraded',
diff --git a/workers/okx-chat-bot/config.js b/workers/okx-chat-bot/config.js
index 21bce85a1..bf3d6444c 100644
--- a/workers/okx-chat-bot/config.js
+++ b/workers/okx-chat-bot/config.js
@@ -4,7 +4,7 @@
// VM, and locally with no code change. The defaults are the production posture:
// state persisted to GCS, heartbeat on, session probed every minute.
-import { homedir } from 'node:os';
+import { homedir, hostname } from 'node:os';
import { join } from 'node:path';
const num = (v, fallback) => {
@@ -49,8 +49,36 @@ export function resolveProvider(env = process.env) {
};
}
+/**
+ * Name the host this process is beating from, and say whether that host survives
+ * without a human.
+ *
+ * The heartbeat is the fleet's only view of this bot, and "online" from a
+ * developer codespace means something completely different from "online" on
+ * Cloud Run: the first dies with the workspace and takes marketplace chat with
+ * it. Reporting both the same way would rebuild, one level up, exactly the
+ * false-green this worker exists to kill.
+ *
+ * K_SERVICE set by Cloud Run, the durable posture
+ * OKX_BOT_HOST_LABEL name another host (a VM, a box under a desk)
+ * OKX_BOT_HOST_DURABLE=1 claim that host survives on its own
+ *
+ * @param {NodeJS.ProcessEnv} env
+ * @returns {{ label: string, durable: boolean }}
+ */
+export function resolveHost(env = process.env) {
+ if (env.K_SERVICE) {
+ return { label: `cloudrun:${env.K_SERVICE}${env.K_REVISION ? ` (${env.K_REVISION})` : ''}`, durable: true };
+ }
+ const explicit = (env.OKX_BOT_HOST_LABEL || '').trim();
+ if (explicit) return { label: explicit, durable: env.OKX_BOT_HOST_DURABLE === '1' };
+ if (env.CODESPACE_NAME) return { label: `codespace:${env.CODESPACE_NAME}`, durable: false };
+ return { label: `local:${hostname()}`, durable: false };
+}
+
export function loadConfig(env = process.env) {
const provider = resolveProvider(env);
+ const host = resolveHost(env);
return {
// HOME for both CLIs. Everything durable lands under it:
// $home/.okx-agent-task/ daemon state, sqlite, XMTP db, AI workspace
@@ -75,6 +103,12 @@ export function loadConfig(env = process.env) {
providerReason: provider.reason,
providerCredentialed: provider.credentialed,
+ // Where this process runs, and whether that place stays up on its own.
+ // Reported on every beat so /api/healthz can tell a durable host from a
+ // stopgap instead of calling both "online".
+ host: host.label,
+ hostDurable: host.durable,
+
// Where the AI subsession's briefing and skills come from. Baked into the
// image at /app; regenerated at boot so a redeploy always ships the live
// catalog rather than whatever the last snapshot happened to contain.
diff --git a/workers/okx-chat-bot/health-server.js b/workers/okx-chat-bot/health-server.js
index a250e2e67..f8d525b69 100644
--- a/workers/okx-chat-bot/health-server.js
+++ b/workers/okx-chat-bot/health-server.js
@@ -33,6 +33,7 @@ export function statusBody(cfg, live, daemonStats, bootAt) {
worker: 'okx-chat-bot',
agentId: cfg.agentId,
bootAt,
+ host: { label: cfg.host, durable: cfg.hostDurable },
provider: { name: cfg.provider, credentialed: cfg.providerCredentialed, reason: cfg.providerReason },
daemon: { status: live.daemon, ...daemonStats },
session: live.wallet ? { loggedIn: live.wallet.loggedIn, email: live.wallet.email } : null,
diff --git a/workers/okx-chat-bot/index.js b/workers/okx-chat-bot/index.js
index 8cee55735..6a3f8cb89 100644
--- a/workers/okx-chat-bot/index.js
+++ b/workers/okx-chat-bot/index.js
@@ -50,6 +50,8 @@ async function heartbeat(cfg, supervisor) {
const meta = {
bootAt: BOOT_AT,
agentId: cfg.agentId,
+ host: cfg.host,
+ hostDurable: cfg.hostDurable,
health: live.verdict.status,
ready: live.verdict.ready,
reason: live.verdict.reason,
@@ -162,6 +164,8 @@ async function main() {
log.info('boot', {
home: cfg.home,
agentId: cfg.agentId,
+ host: cfg.host,
+ hostDurable: cfg.hostDurable,
provider: cfg.provider,
providerReason: cfg.providerReason,
stateBucket: cfg.stateBucket || null,