Files
CatoandClaude Fable 5.2 137ff5d1fb fix(ai): Loki reads and writes the box's AI usage log; fleet-env usage-log points every app at it (#1709)
* ops(fleet-env): usage-log points every app at one AI usage log through the fleet file

launch.sh.tmpl sets AI_USAGE_DIR, but a launch script is only rewritten by
a site sync, so after #1704 no running app had it and Settings > AI said
the server is not writing the log. Every launch script already sources
/opt/fleet/env with export on. usage-log makes the directory, writes the
one line there once, and restarts the apps one at a time.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq

* fix(ai): Loki reads and writes the box's usage log without the launch variable

Loki's launch script predates /opt/fleet/env, so after the box was pointed
at one usage log Loki still had no AI_USAGE_DIR: the card said the server
was not writing a log. lib/ai/usage-dir takes the box's directory when the
variable is unset and it exists; instrumentation adopts it at boot (and
names the app LOKI). Loki walks its own chain, so the package cannot see
its calls: recordUsage reports each answered call to the log.

scripts/test/usage-dir.ts.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq

* docs(env): AI_APP_ID in .env.example

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq

* ops(fleet-env): where and retire look beyond the app layout

The platform's Google key was retired from /opt/fleet/env and the app env
files on 2026-11-13, but nothing had looked at the builder's env
(/opt/loki/runner/.env), the named runners' (/etc/loki/runners/*.env) or the
Telegram gateway's (~openclaw/.openclaw/.env), where a copy of the same key
could still be read by a process that spends. where and retire now also
sweep /opt, /etc/loki, /home and /root for env files (not examples, not
node_modules, not releases), reading root-owned ones through sudo.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-13 13:41:20 +01:00

40 lines
1.2 KiB
TypeScript

/**
* Where the server keeps its AI usage log (lib/ai/usage-dir).
* Run: npx tsx scripts/test/usage-dir.ts
*/
import assert from "node:assert/strict";
import { adoptFleetUsageLog, BOX_USAGE_DIR, fleetUsageDir } from "@/lib/ai/usage-dir";
const yes = () => true;
const no = () => false;
assert.equal(fleetUsageDir({ AI_USAGE_DIR: " /x " }, no), "/x", "the variable wins");
assert.equal(
fleetUsageDir({}, yes),
BOX_USAGE_DIR,
"unset, and the box has the directory: that is the log",
);
assert.equal(
fleetUsageDir({}, no),
null,
"unset and no directory: there is no log, not an empty one",
);
const env: Record<string, string | undefined> = {};
adoptFleetUsageLog(env, yes);
assert.deepEqual(
env,
{ AI_USAGE_DIR: BOX_USAGE_DIR, AI_APP_ID: "LOKI" },
"Loki's own calls are named and logged",
);
const own: Record<string, string | undefined> = { AI_USAGE_DIR: "/mine", AI_APP_ID: "DEV" };
adoptFleetUsageLog(own, yes);
assert.deepEqual(own, { AI_USAGE_DIR: "/mine", AI_APP_ID: "DEV" }, "what is already set is kept");
const none: Record<string, string | undefined> = {};
adoptFleetUsageLog(none, no);
assert.deepEqual(none, {}, "a machine without the directory is left alone");
console.log("usage-dir: ok");