Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
8195c3d
Record serve daemon transition worksheets and behavioral baselines
sambitcreate Nov 5, 2026
ac563a5
Make daemon chat custody and generation settlement explicit
sambitcreate Nov 5, 2026
7a9a258
Keep the serve lease until accepted control commands settle
sambitcreate Nov 5, 2026
7849e45
Stop serve startup when a signal arrives
sambitcreate Nov 5, 2026
1d3fd97
Finish every cleanup step of a daemon run
sambitcreate Nov 5, 2026
31028aa
Decide serve stop from one table and never blame a foreign PID
sambitcreate Nov 5, 2026
10ddf10
Check for a stop after the last startup step
sambitcreate Nov 5, 2026
0095d3a
Enforce the daemon response ceiling on the final chunk
sambitcreate Nov 5, 2026
75881d8
Read only the first frame on the daemon socket
sambitcreate Nov 5, 2026
a7480c7
Move daemon tool approval onto the shared coordinator
sambitcreate Nov 5, 2026
f65f266
Finish interrupted chat deletions on every server
sambitcreate Nov 5, 2026
91de97e
Stop the double-start test's server before its directory goes
sambitcreate Nov 5, 2026
92dfecc
Apply the control socket ceilings to the frame
sambitcreate Nov 5, 2026
755864f
Note the approval publish race owned by the coordinator
sambitcreate Nov 5, 2026
7c5a7de
Give schedule consent one phase and behavioral coverage
sambitcreate Nov 5, 2026
24e47ee
Record C15 and triage the remaining cli-native entries
sambitcreate Nov 5, 2026
8678c5a
Bind schedule consent to the reviewed input, once
sambitcreate Nov 5, 2026
3bb8964
Remove a schedule receipt when its command ends
sambitcreate Nov 5, 2026
67eaa64
Document that every server finishes interrupted deletions
sambitcreate Nov 5, 2026
0dadb47
Record Lane D review lessons
sambitcreate Nov 5, 2026
8cc674c
Advise no kill when a stop cannot confirm its owner
sambitcreate Nov 5, 2026
e9308d2
Keep a command tracked until its reply has flushed
sambitcreate Nov 5, 2026
9b59998
Merge branch 'feature/sm-serve-lifetime' into feature/sm-serve-ownership
sambitcreate Nov 5, 2026
29ee09d
Merge branch 'feature/sm-serve-approval' into feature/sm-serve-framing
sambitcreate Nov 5, 2026
6357eeb
Merge branch 'feature/sm-serve-ownership' into feature/sm-serve-approval
sambitcreate Nov 5, 2026
799a7a4
Merge branch 'feature/sm-serve-framing' into feature/sm-serve-chat-re…
sambitcreate Nov 5, 2026
443dbd3
Stop reading the daemon socket after the first frame
sambitcreate Nov 5, 2026
50a02d2
Refuse to start when the deletion record is unreadable
sambitcreate Nov 5, 2026
37c7311
Merge branch 'feature/sm-serve-chat-recovery' into feature/sm-serve-s…
sambitcreate Nov 5, 2026
abd6235
Assert the real approval attention field
sambitcreate Nov 5, 2026
8e06a9e
Merge branch 'feature/sm-serve-daemon' into feature/sm-serve-turns
sambitcreate Nov 5, 2026
b981b16
Merge branch 'feature/sm-serve-framing' into feature/sm-serve-chat-re…
sambitcreate Nov 5, 2026
cce1de6
Merge branch 'feature/sm-serve-approval' into feature/sm-serve-framing
sambitcreate Nov 5, 2026
55bd5f5
Merge branch 'feature/sm-serve-ownership' into feature/sm-serve-approval
sambitcreate Nov 5, 2026
3f35155
Merge branch 'feature/sm-serve-chat-recovery' into feature/sm-serve-s…
sambitcreate Nov 5, 2026
007688d
Merge branch 'feature/sm-serve-lifetime' into feature/sm-serve-ownership
sambitcreate Nov 5, 2026
40e27b9
Merge branch 'feature/sm-serve-turns' into feature/sm-serve-lifetime
sambitcreate Nov 5, 2026
127ac6f
Record a failed final save as failed
sambitcreate Nov 5, 2026
da59192
Merge branch 'feature/sm-serve-framing' into feature/sm-serve-chat-re…
sambitcreate Nov 5, 2026
313d834
Merge branch 'feature/sm-serve-approval' into feature/sm-serve-framing
sambitcreate Nov 5, 2026
f085afa
Report each raised approval once, from its outcome
sambitcreate Nov 5, 2026
c1cafd2
Merge branch 'feature/sm-serve-chat-recovery' into feature/sm-serve-s…
sambitcreate Nov 5, 2026
37fd8a8
Update the Lane D approval note
sambitcreate Nov 5, 2026
0138204
Consume schedule consent at its first write
sambitcreate Nov 5, 2026
89590a8
Treat a deletion record of non-ids as unreadable
sambitcreate Nov 5, 2026
e99cd4b
Merge branch 'feature/sm-serve-chat-recovery' into feature/sm-serve-s…
sambitcreate Nov 5, 2026
f210556
Merge branch 'feature/sm-serve-chat-recovery' into feature/sm-serve-s…
sambitcreate Nov 5, 2026
1b4fb7c
Merge branch 'feature/sm-serve-framing' into feature/sm-serve-chat-re…
sambitcreate Nov 5, 2026
33f4c1b
Close the talkative test peer during cleanup
sambitcreate Nov 5, 2026
09f48d7
Let a revocation win the race for the receipt
sambitcreate Nov 5, 2026
741a95d
Merge pull request #765 from sambitcreate/feature/sm-serve-schedule-c…
sambitcreate Nov 6, 2026
3d362d5
Merge pull request #764 from sambitcreate/feature/sm-serve-chat-recovery
sambitcreate Nov 6, 2026
b779065
Merge pull request #762 from sambitcreate/feature/sm-serve-approval
sambitcreate Nov 6, 2026
17c26d7
Merge pull request #760 from sambitcreate/feature/sm-serve-lifetime
sambitcreate Nov 6, 2026
4f27106
Merge pull request #763 from sambitcreate/feature/sm-serve-framing
sambitcreate Nov 6, 2026
72c267e
Merge pull request #761 from sambitcreate/feature/sm-serve-ownership
sambitcreate Nov 6, 2026
f6edce3
Merge pull request #759 from sambitcreate/feature/sm-serve-turns
sambitcreate Nov 6, 2026
16befe8
Merge remote-tracking branch 'origin/main' into feature/sm-serve-daemon
sambitcreate Nov 6, 2026
cd41e1f
Put the daemon chat store's doc comments on the members they describe
sambitcreate Nov 6, 2026
a98f057
Record the tombstone store ordering and a load-sensitive parity test
sambitcreate Nov 6, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .memory/explicit-state-machines-lane-d.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Explicit state machines, Lane D: `aiden serve` owners

Started 2026-11-04. Scope: `cli-native` C09, C10, C03, C01, C02, C11, C04, then triage of the other C entries. Worksheets: `docs/state-machines/serve-daemon.md`.

- `feature/subagent-desktop-serve-upgrade` (uncommitted and local-only in `~/projects/aiden-agent`) rewrites `serve.ts`, `daemon-chats.ts` and `serve-lifecycle.ts`, and replaces the daemon approval map with `HeadlessInteractionHub`. Keep extracted logic in new files with small adapter edits so that branch merges cleanly.
- Pi's `extensionRunner.emit` catches handler errors, so a throwing `session_shutdown` handler cannot skip closers or usage in `perform`.
- A fresh worktree needs `build/native` for `tests/extensions.test.mjs`: copy `packages/cli/dist/app/native` after a CLI build.
- `DaemonTurnOwner` (`packages/cli/src/daemon-turn-lifecycle.ts`) is the only writer of chat custody. Lane A's #735 keeps the lease API, so this does not stack on it; its doc home is `docs/state-machines/`.
- Never await `cancelChat` from inside a tool_call or before_agent_start hook: `session.abort()` waits for the very hook that called it.
- `.memory/` is gitignored but tracked: new files need `git add -f`.
- C15 converted to one `phase`. Triage of the other `C*` entries is at the end of `docs/state-machines/serve-daemon.md`.
- C18 and C19 are blocked on the subagent upgrade branch, which rewrites those files. C05 waits on #733. C12, C16, C28 and C32 are P6.
- No file outside `packages/cli`, `docs/state-machines` and `.memory` changed, so no mobile contract moved.
- Cleanup hooks in `node:test` (`t.after`) run in registration order here, so stop a daemon before removing its directory. `box.stopServer` in `tests/serve-lifetime.test.mjs` does that; an earlier `t.after(serve stop)` orphaned real daemons.
- The local shell is zsh: an unquoted `$files` is one argument, so a looped `node --test $files` silently runs nothing. Check that a pass count came back.
- Control-socket chunks stop near 64 KiB, so a ceiling test needs a frame near the limit plus a later write, not one big write.
- `createDaemonApprovals` reports each raised prompt once from the settled outcome, so it does not rely on the coordinator's published flag, on `main` or after Lane B's #738.
- The two `JsonStore` instances on `daemon-chat-deletions.json` (daemon `tombstones`, remote `deleting`) do not share a queue, and `acquireLease` throws on contention rather than waiting. That is safe only because daemon recovery finishes before Remote serves its first delete; keep that order.
- Under heavy host load `durable CLI Bots can message each other` in `tests/parity.test.mjs` can miss its 10-second reply deadline (seen 2026-11-06 at load 14, passes alone). The ledger showed the reply on its way, not lost.
193 changes: 193 additions & 0 deletions docs/state-machines/serve-daemon.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Every setup step is a short command you run once over SSH.
aiden serve
```

`aiden serve` starts the server in the foreground: the scheduler, configured Telegram polling, Bots and Remote, the authenticated HTTPS/SSE API that phones and Macs use (port 49320 by default). Remote is on by default; `aiden serve --no-remote` runs without it (`--remote` is still accepted).
`aiden serve` starts the server in the foreground: the scheduler, configured Telegram polling, Bots and Remote, the authenticated HTTPS/SSE API that phones and Macs use (port 49320 by default). Remote is on by default; `aiden serve --no-remote` runs without it (`--remote` is still accepted). Either way, the server finishes any chat deletion a crash interrupted, and that chat takes no turns until it has.

When `aiden serve` runs in the foreground in an interactive terminal, it prints a pairing QR code and manual code at startup, so you can scan it from the phone app right away. It pairs over the local network by default (over Tailscale when the server is set to Tailscale only, `remote mode tailscale`); `--pair tailscale` pairs over Tailscale instead, and `--no-pair` skips the code. No code is printed under `--daemon` or when output is not a terminal (a log, a pipe, a service manager), because the code is a single-use secret.

Expand All @@ -62,7 +62,7 @@ When `aiden serve` runs in the foreground in an interactive terminal, it prints
| `aiden remote devices`, `remote revoke <id>` | List or revoke paired devices. Revoking a device closes its open Bot session streams before the command returns. |
| `aiden remote roots`, `remote approve-root <path>` | Folders that paired devices may open |

**Stopping.** Once shutdown starts, the server takes no new connections, requests or chat turns, cancels running ones and drains. `aiden serve stop` force-kills a server still running 15 seconds after SIGTERM, after checking the PID is still that server: a `--daemon` server together with its process group (its Remote advertiser), a foreground `aiden serve` on its own so the rest of its shell job (`aiden serve | tee`) survives. Either way, its stdio MCP servers (each in a process group of its own, recorded in the lease directory) are killed too, but only groups that still contain a process the server started. A foreground server logs a stray unhandled rejection instead of exiting. Aiden commands exit 0 quietly when their reader closes early (`aiden schedule list | head -1`).
**Stopping.** Once shutdown starts, the server takes no new connections, requests or chat turns, cancels running ones and drains. Control commands it already accepted finish and get their reply first. A stop during startup ends startup at the next step. `aiden serve stop` force-kills a server still running 15 seconds after SIGTERM, after checking the PID is still that server: a `--daemon` server together with its process group (its Remote advertiser), a foreground `aiden serve` on its own so the rest of its shell job (`aiden serve | tee`) survives. Either way, its stdio MCP servers (each in a process group of its own, recorded in the lease directory) are killed too, but only groups that still contain a process the server started. A foreground server logs a stray unhandled rejection instead of exiting. Aiden commands exit 0 quietly when their reader closes early (`aiden schedule list | head -1`).

`remote` and `bots` commands talk to the running server over its control socket and fail if it is not running. `schedule` and `workspace` commands also work without the server, through a one-shot lease. `telegram status` works anytime; `telegram connect|configure|disable|disconnect` take the server's lease, so stop `aiden serve` before changing Telegram configuration.

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"scripts": {
"build": "node scripts/patch-branding.mjs && node scripts/build.mjs",
"test": "node --test tests/bundle.test.mjs tests/runtime.test.mjs tests/extensions.test.mjs tests/parity.test.mjs tests/serve-startup.test.mjs tests/service.test.mjs tests/reachability.test.mjs tests/inbox-host.test.mjs",
"test": "node --test tests/bundle.test.mjs tests/runtime.test.mjs tests/extensions.test.mjs tests/parity.test.mjs tests/serve-startup.test.mjs tests/service.test.mjs tests/reachability.test.mjs tests/inbox-host.test.mjs tests/daemon-turns.test.mjs tests/serve-lifetime.test.mjs tests/schedule-consent.test.mjs",
"type-check": "tsc --noEmit"
},
"dependencies": {
Expand Down
68 changes: 68 additions & 0 deletions packages/cli/src/daemon-approvals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* The daemon's tool approvals, on the shared `ToolApprovalCoordinator` that owns
* one-shot settlement, owner scoping and abort. This adds what is the daemon's:
* the prompt's expiry and the decision its run journal records.
* See docs/state-machines/serve-daemon.md.
*/
import { randomUUID } from "node:crypto";
import { ToolApprovalCoordinator, type ToolApprovalOutcome, type ToolApprovalPrompt } from "../../../main/services/tool-approval.js";

export type DaemonApprovalDecision = "allow" | "deny" | "expired" | "cancelled";

interface OpenPrompt {
publish(prompt: ToolApprovalPrompt): void;
expiry: AbortController;
raised: boolean;
reported: boolean;
}

export interface DaemonApprovalsOptions {
/** A prompt is about to reach its owner. */
raised(prompt: ToolApprovalPrompt): void;
/** A raised prompt ended, however it ended. */
resolved(approvalId: string, decision: DaemonApprovalDecision): void;
expiryMs?: number;
}

export function createDaemonApprovals(options: DaemonApprovalsOptions) {
const open = new Map<string, OpenPrompt>();
const decisionFor = (outcome: ToolApprovalOutcome, prompt?: OpenPrompt): DaemonApprovalDecision => {
if (outcome === "allowed") return "allow";
if (outcome === "denied") return "deny";
return prompt?.expiry.signal.aborted ? "expired" : "cancelled";
};
/** One report per raised prompt, from the outcome that settled it. */
const report = (approvalId: string, outcome: ToolApprovalOutcome, prompt?: OpenPrompt) => {
if (!prompt?.raised || prompt.reported) return;
prompt.reported = true;
options.resolved(approvalId, decisionFor(outcome, prompt));
};
const coordinator = new ToolApprovalCoordinator(
(prompt) => {
const entry = open.get(prompt.approvalId);
// Marked first: a `raised` that throws may already have changed attention.
if (entry) entry.raised = true;
options.raised(prompt);
entry?.publish(prompt);
},
(approvalId, outcome) => { report(approvalId, outcome, open.get(approvalId)); },
);
return {
/** Waits for one answer. `publish` delivers the prompt to this run's owner. */
async request(descriptor: Omit<ToolApprovalPrompt, "approvalId">, signal: AbortSignal, ownerId: string, publish: OpenPrompt["publish"]): Promise<ToolApprovalOutcome> {
const approvalId = `approval_${randomUUID()}`, expiry = new AbortController();
const timer = setTimeout(() => expiry.abort(), options.expiryMs ?? 300_000);
const prompt: OpenPrompt = { publish, expiry, raised: false, reported: false };
open.set(approvalId, prompt);
try {
const outcome = await coordinator.request({ ...descriptor, approvalId }, AbortSignal.any([signal, expiry.signal]), ownerId);
// The coordinator withdraws only what it saw published, so a failed or re-entrant publish lands here.
report(approvalId, outcome, prompt);
return outcome;
} finally { clearTimeout(timer); open.delete(approvalId); }
},
approve: (id: string, decision: "allow" | "deny", ownerId: string) => coordinator.decide(id, decision === "allow", ownerId),
/** A paired desktop holding `runs:control` answers any run's approval, as the desktop host's own controls do. */
approveAsHost: (id: string, decision: "allow" | "deny") => coordinator.decideAsHost(id, decision === "allow"),
};
}
Loading
Loading