Skip to content

Experience-Driven Black-Box Cost Control for Coding Agents

opencode plugin: fixes from a live run on 1.19.5

Zaid Ahmed6612809e1771
diff --git a/plugin/.DS_Store b/plugin/.DS_Store
new file mode 100644
index 0000000..9d92963
Binary files /dev/null and b/plugin/.DS_Store differ
diff --git a/plugin/README.md b/plugin/README.md
index 7e06278..5d026e2 100644
--- a/plugin/README.md
+++ b/plugin/README.md
@@ -286,9 +286,11 @@ from); `pi config` turns it off without removing it. Optional: `rm -rf ~/.tokene
## opencode
-**Experimental; not yet verified against a running opencode.** The plugin is written against the
-v1 plugin API (`@opencode-ai/plugin`, opencode 1.19.x) and is tested only through the shared
-dispatcher and a recorded session export.
+**Experimental.** Written against the v1 plugin API (`@opencode-ai/plugin`) and run live on
+opencode 1.19.5 with `opencode run` (free `opencode/big-pickle` model): the plugin loads, each
+session is handed over and folded with real token usage, facts are learned over five sessions
+(`microskills=envfacts`), and the next session receives them through the system prompt. Not yet
+exercised: the interactive TUI (toasts, quitting), the loop guard's redirect, and the v2 plugin API.
**1. Install.** Copy or link the plugin directory, and keep it next to `scripts/` and `lib/`:
@@ -299,8 +301,8 @@ ln -s /path/to/TokenEconomizer/plugin/opencode/tokeneconomizer.ts ~/.config/open
**2. Check it.** Run a session, then `~/.tokeneconomizer/bin/te --host opencode status` in the
repository. opencode has no plugin slash commands here, so every command runs from a terminal.
-Tool pruning does not apply yet. The plugin hands a session over when opencode deletes it or the
-process exits, and a headless `opencode run` after its one turn. Model calls of the plugin's own
+Tool pruning does not apply yet. The plugin hands a session over when opencode deletes it, when the process
+exits, and after the one turn of a headless `opencode run`, exporting it with `opencode export`. Model calls of the plugin's own
(micro-skills' proposer, the loop guard's judge) are not wired up for opencode yet, so it learns
from repeated failures only.
diff --git a/plugin/opencode/tokeneconomizer.ts b/plugin/opencode/tokeneconomizer.ts
index e67eccf..4c5d99c 100644
--- a/plugin/opencode/tokeneconomizer.ts
+++ b/plugin/opencode/tokeneconomizer.ts
@@ -19,6 +19,8 @@
* -> SessionEnd hands the session, exported as JSON, to
* a detached fold
*
+ * opencode calls every export of a plugin module as a plugin, so `TokenEconomizer` is the only one.
+ *
* Like the hooks on the other agents it fails open: a dispatcher that is missing, slow or
* broken answers "no change". It does nothing under TE_SIDECALL=1 (the plugin's own side calls)
* or TE_DISABLE=1.
@@ -49,7 +51,7 @@ const mode = () => (process.env.TE_OPENCODE_MODE ?? (process.argv.includes("run"
/** The program and arguments that run a launcher. The launchers are POSIX `sh`, which Windows
* cannot start; there the bootstrap they run is started with Python itself (`TE_PYTHON`, else
* `python`). */
-export function launch(
+function launch(
script: string,
args: string[],
platform: string = process.platform,
@@ -116,15 +118,25 @@ function hookSync(payload: Record<string, unknown>) {
const stateRoot = () => resolve(process.env.TOKENECONOMIZER_HOME || join(homedir(), ".tokeneconomizer"));
-/** What the model said and did so far, as the JSON the fold reads (`trace/opencode.py`). */
-async function exportSession(client: any, sessionID: string): Promise<string> {
+/** The session so far, as the JSON the fold reads (`trace/opencode.py`), from `opencode export`.
+ * A child process rather than the plugin client: `opencode run` exits as soon as its turn goes
+ * idle, without waiting for a plugin to finish, so only a blocking call is sure to complete. The
+ * running binary is opencode itself (`process.execPath`), or `opencode` on PATH under Node. */
+function exportSession(sessionID: string, cwd: string): string {
const file = join(stateRoot(), "opencode-sessions", `${sessionID}.json`);
try {
- const res = await client.session.messages({ path: { id: sessionID } });
- const messages = res?.data ?? res;
- if (!Array.isArray(messages)) return "";
+ const exe = /(^|[\\/])(node|bun)(\.exe)?$/i.test(process.execPath) ? "opencode" : process.execPath;
+ const res = spawnSync(exe, ["export", sessionID], {
+ cwd,
+ encoding: "utf8",
+ timeout: HOOK_TIMEOUT_MS,
+ maxBuffer: 256 * 1024 * 1024,
+ stdio: ["ignore", "pipe", "ignore"],
+ });
+ const json = res.status === 0 ? res.stdout : "";
+ if (!json.trimStart().startsWith("{")) return "";
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
- writeFileSync(file, JSON.stringify({ info: { id: sessionID }, messages }), { mode: 0o600 });
+ writeFileSync(file, json, { mode: 0o600 });
return file;
} catch {
return "";
@@ -165,7 +177,7 @@ export const TokenEconomizer: Plugin = async ({ client, directory }) => {
const end = async (sessionID: string, reason: string) => {
if (!open.delete(sessionID)) return;
- const file = await exportSession(client, sessionID);
+ const file = exportSession(sessionID, directory);
await hook({ ...base(sessionID, file), hook_event_name: "SessionEnd", reason });
};
@@ -173,7 +185,11 @@ export const TokenEconomizer: Plugin = async ({ client, directory }) => {
// finishing), the sessions still open are handed over, once, synchronously.
process.once("exit", () => {
for (const sessionID of open) {
- hookSync({ ...base(sessionID), hook_event_name: "SessionEnd", reason: "exit" });
+ hookSync({
+ ...base(sessionID, exportSession(sessionID, directory)),
+ hook_event_name: "SessionEnd",
+ reason: "exit",
+ });
}
open.clear();
});
@@ -189,7 +205,7 @@ export const TokenEconomizer: Plugin = async ({ client, directory }) => {
if (!id) return;
if (!open.has(id)) await start(id);
await hook({ ...base(id), hook_event_name: "Stop", prompt_id: turns.get(id) ?? "", stop_hook_active: false });
- // A headless run ends right after its one turn; export now, while the client is up.
+ // A headless run ends right after its one turn, so the session is handed over now.
if (mode() === "run") await end(id, "run");
} else if (event?.type === "session.deleted") {
const id = props.info?.id ?? props.sessionID;
@@ -245,5 +261,3 @@ export const TokenEconomizer: Plugin = async ({ client, directory }) => {
},
};
};
-
-export default TokenEconomizer;