iterate.com / kernel

git clone https://git.iterate.com/repos/kernel

cli: a program disposes of the daemon's itx() after each use

Measured: a stub that is asked for and never disposed of is held at the daemon until
the program's socket closes. The readme, the comment and the test say and do so.

Session: claude 344f881f-d8ca-4d42-99c3-42f7b2516e6b on beelink

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

Jonas Templestein, 2026-11-28 20:55 UTC · af80fc9e810a02413660853550556f65a122ff03 · parent 02d5288

3 files changed

M core/lib/src/cli/README.md
@@ -738,8 +738,12 @@
   { deliverEvent: async (event) => {/* … */} },
   { description: "Codex sessions on beelink" },
 );
-// the mind's own API, over the daemon's connection, in one pipeline
-await daemon.itx().cd("/agents/codex/abc").append({ type: "codex/record-received", payload });
+// the mind's own API, over the daemon's connection: asked for at each use, disposed of after
+{
+  using itx = daemon.itx();
+  using session = itx.cd("/agents/codex/abc");
+  await session.append({ type: "codex/record-received", payload });
+}
 await daemon.closed; // the daemon ended: connect and provide again
 ```
 
@@ -753,8 +757,10 @@
   connection first, so a name is never without a lender. It reconnects for as long as it runs
   (1 s, doubling, at most 60 s). A program does nothing at any of that.
 - **A program that goes away takes its lends along**, at once. The newest lend of a name wins.
-- **Ask for `itx()` at each use.** What it answers lives as long as the daemon's current connection
-  to the mind. A call waits up to 30 s for a connection, then fails.
+- **Ask for `itx()` at each use, and dispose of it after.** What it answers lives as long as the
+  daemon's current connection to the mind, and the calls on it go with the call that asks for it:
+  one round trip to the daemon. One that is never disposed of is held at the daemon until the
+  program goes away. A call waits up to 30 s for a connection, then fails.
 - **Who may connect: this computer's user.** Whoever can read `daemon.json` can read the stored
   sign-in beside it. The port alone opens nothing, and a request with an `Origin` header (a
   browser's page) is refused. A program that connects acts as the sign-in's person, with all that
M core/lib/src/cli/daemon-client.ts
@@ -56,9 +56,10 @@
 export type DaemonConnection = Disposable & {
   status(): Promise<DaemonStatus>;
   /** THE MIND'S ROOT CONTEXT over the daemon's connection (`mind`: another mind than the
-   *  daemon's own). Ask for it at each use, as a pipeline costs no round trip:
-   *  `await daemon.itx().cd("/notes").append(event)`. A copy that is kept ends when the daemon
-   *  replaces its connection to the mind, about once an hour. */
+   *  daemon's own). Ask for it at each use and dispose of it after (`using`): the calls on it
+   *  go with the call that asks for it, so a use costs one round trip to the daemon. A copy
+   *  that is kept ends when the daemon replaces its connection to the mind, about once an
+   *  hour, and one that is never disposed of is held at the daemon until this program goes. */
   itx(mind?: string): MindItx;
   /** LEND `provided` TO THE MIND as `itx.<name>` until the answer is disposed of or this
    *  connection closes: a plain object of functions (wrapped as `iterate provide` wraps a file's,
M core/lib/src/cli/daemon.test.ts
@@ -128,7 +128,9 @@
     );
     expect(new TextDecoder().decode(await lent.files.read("a.txt"))).toBe("a.txt");
     // the mind's itx, in one pipeline: no connection to the mind of the program's own
-    const [event] = await program.itx().cd("/agents/codex/one").append({ type: "codex/noted" });
+    using itx = program.itx();
+    using session = itx.cd("/agents/codex/one");
+    const [event] = await session.append({ type: "codex/noted" });
     expect(event).toMatchObject({ path: "/agents/codex/one", type: "codex/noted" });
     expect(mind.connections()).toBe(1);
     expect(await program.status()).toMatchObject({