diff --git a/modules/anthropic-consumer/Dockerfile b/modules/anthropic-consumer/Dockerfile deleted file mode 100644 index 8445007..0000000 --- a/modules/anthropic-consumer/Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -# anthropic-consumer's runtime: the tool runtime, carrying this module's compiled code. -# -# **Built from this module's own directory and nothing else.** The sdk and the tool runtime are in -# the base images, published like any other artifact — which is what makes this buildable by the -# mesh from a repository and a path (novox/hq ADR 0069) rather than only on a workstation that -# happens to have the siblings. -# -# Two bases, named rather than pinned (novox/hq issue 044): the image this is COMPILED in and the -# image it RUNS in — the second must not carry a compiler. Declared in module.json's `build.on`. -ARG BUILD_BASE -ARG RUNTIME_BASE - -FROM ${BUILD_BASE} AS build -WORKDIR /app/modules/anthropic-consumer -COPY . . -RUN node /app/node_modules/typescript/bin/tsc apply/index.ts usage/index.ts \ - --module NodeNext --moduleResolution NodeNext --target ES2022 --outDir dist - -FROM ${RUNTIME_BASE} -COPY --from=build /app/modules/anthropic-consumer/dist /app/modules/anthropic-consumer/dist -# No serve-time entrypoints: every container of this module names its command (`run` on a -# schedule), so nothing here serves — deliberately no MESH_TOOL_MODULES. diff --git a/modules/anthropic-consumer/module.json b/modules/anthropic-consumer/module.json index 7b4c3ad..b2379cc 100644 --- a/modules/anthropic-consumer/module.json +++ b/modules/anthropic-consumer/module.json @@ -14,19 +14,10 @@ "secrets": { "model-access": "${dir:state}/access-token" }, - "own-secrets": { - "broker": "${dir:mesh-state}/broker" - }, "emits": [ "usage.session" ], "resources": [ - { - "id": "mesh-state", - "type": "directory", - "mode": "0700", - "place": "mesh" - }, { "id": "state", "type": "directory", @@ -46,66 +37,39 @@ }, { "id": "apply", - "type": "container", - "name": "mesh-anthropic-consumer-apply", - "network": "host", + "type": "process", + "name": "anthropic-consumer-apply", + "artifact": "code", + "run": [ + "node", + "apply/index.js" + ], "schedule": "*/5 * * * *", - "args": [ - "run", - "/app/modules/anthropic-consumer/dist/apply/index.js" - ], - "volumes": [ - "${dir:state}:/run/state" - ], "env": { - "MESH_MODEL_ACCESS_SECRET_FILE": "/run/state/access-token", - "MESH_MODEL_ACCESS_BIND_FILE": "/run/state/model.json", - "MESH_CLAUDE_CREDENTIALS_FILE": "/run/state/claude/.credentials.json", - "MESH_CLAUDE_IDENTITY_FILE": "/run/state/claude/.claude.json" - }, - "artifact": "runtime" - }, - { - "id": "usage", - "type": "container", - "name": "mesh-anthropic-consumer-usage", - "network": "host", - "schedule": "*/5 * * * *", - "args": [ - "run", - "/app/modules/anthropic-consumer/dist/usage/index.js" - ], - "volumes": [ - "${dir:mesh-state}/broker:/run/secrets/broker:ro", - "${dir:state}:/run/state" - ], - "env": { - "MESH_BROKER_FILE": "/run/secrets/broker", - "MESH_CLAUDE_PROJECTS_DIR": "/run/state/claude/projects", - "MESH_ANTHROPIC_USAGE_OUT": "/run/state/out/session-usage.json", - "MESH_TOOLS_MAIN": "/app/dist/main.js" - }, - "artifact": "runtime" + "MESH_MODEL_ACCESS_SECRET_FILE": "${dir:state}/access-token", + "MESH_MODEL_ACCESS_BIND_FILE": "${dir:state}/model.json", + "MESH_CLAUDE_CREDENTIALS_FILE": "${dir:state}/claude/.credentials.json", + "MESH_CLAUDE_IDENTITY_FILE": "${dir:state}/claude/.claude.json" + } } ], "build": { - "on": [ - { - "arg": "BUILD_BASE", - "module": "mesh-tools", - "artifact": "build" - }, - { - "arg": "RUNTIME_BASE", - "module": "mesh-tools", - "artifact": "runtime" - } - ], "artifacts": [ { - "name": "runtime", - "kind": "image", - "from": "Dockerfile" + "name": "code", + "kind": "bundle", + "language": "typescript", + "entrypoints": [ + "apply/index.js", + "usage/index.js" + ], + "loads": [ + "usage/index.js" + ], + "env": { + "MESH_CLAUDE_PROJECTS_DIR": "${dir:state}/claude/projects", + "MESH_ANTHROPIC_USAGE_OUT": "${dir:state}/out/session-usage.json" + } } ] } diff --git a/modules/anthropic-consumer/usage/index.ts b/modules/anthropic-consumer/usage/index.ts index 9dae62d..71a9662 100644 --- a/modules/anthropic-consumer/usage/index.ts +++ b/modules/anthropic-consumer/usage/index.ts @@ -3,12 +3,15 @@ // per session. The consumer IS the (node,module) session's fixed binding, so no per-message account // attribution is done — just the totals (port map "don't-map" #3). // -// Runs as `mesh-tools run` (no broker), so events are emitted best-effort via the sibling mesh-tools -// `emit` primitive; the totals are also written to a file so the reading is observable without one. +// Runs in the node's runtime (novox/hq ADR 0198), every five minutes, so events are emitted through +// the runtime as this module; the totals are also written to a file so the reading is observable +// without one. import { readdirSync, statSync, readFileSync, writeFileSync, renameSync, mkdirSync } from "node:fs"; import { join, dirname } from "node:path"; +import { emit } from "@novox/mesh-sdk/events"; + import { readSessionFile, type SessionUsage } from "../transcript.js"; /** The vendor-neutral usage row ADR 0054 fixes — the shape the model-usage store upserts. Kept local @@ -116,22 +119,20 @@ function atomicWrite(path: string, content: string): void { renameSync(tmp, path); } -/** Emit best-effort via the sibling mesh-tools `emit`, which wires a broker a run step has none. */ +/** Emit best-effort through the runtime: a reading that could not be announced is still in the file. */ async function emitUsage(body: Record): Promise { - const main = process.env.MESH_TOOLS_MAIN ?? "/app/dist/main.js"; - const { spawn } = await import("node:child_process"); - await new Promise((resolve) => { - const child = spawn( - process.execPath, - [main, "emit", "usage.session", JSON.stringify(body)], - { stdio: "inherit" }, - ); - child.on("exit", () => resolve()); - child.on("error", (err) => { - console.error(`[anthropic-consumer] could not emit usage: ${err}`); - resolve(); - }); - }); + try { + await emit("usage.session", body); + } catch (err) { + console.error(`[anthropic-consumer] could not emit usage: ${err}`); + } } -await main(); +// The cadence the scheduled container had: once at start, then every five minutes. Not awaited, so the +// runtime's handshake is answered while a long first reading is still under way. +const EVERY_MS = 5 * 60 * 1000; +const tick = (): void => { + void main().catch((err) => console.error(`[anthropic-consumer] usage reading failed: ${err}`)); +}; +tick(); +setInterval(tick, EVERY_MS);