@alook/daemon 0.0.152
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +342 -0
- package/dist/cli/index.js +4773 -0
- package/dist/index.js +5146 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Alook
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing permissions
|
|
201
|
+
and limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
# @alook/daemon
|
|
2
|
+
|
|
3
|
+
A **pure, host-neutral agent-runtime "driver" abstraction** in TypeScript.
|
|
4
|
+
|
|
5
|
+
It documents — as compilable code — how a host adapts many different AI coding
|
|
6
|
+
runtimes (Claude Code, Codex, Gemini, Kimi, Pi, Copilot, Cursor, OpenCode,
|
|
7
|
+
Antigravity) behind one uniform interface, and how it delivers messages into a
|
|
8
|
+
running agent (the "steering" mechanism).
|
|
9
|
+
|
|
10
|
+
The backend hardcodes **no specific host platform**. The agent always invokes a
|
|
11
|
+
stable CLI name (`alook`); `cliTransport.ts` writes a small wrapper by that
|
|
12
|
+
name into a per-launch state dir, prepends it to `PATH`, and forwards to the
|
|
13
|
+
host's real `targetCommand` — **decoupling the agent-facing name from the host's
|
|
14
|
+
real binary** (the host can rename/relocate its CLI without touching the agent
|
|
15
|
+
surface). It also injects an `ALOOK_*` env contract. The default
|
|
16
|
+
`MOCK_CLI_CONFIG` wires no `targetCommand` (the wrapper errors if invoked); a
|
|
17
|
+
real deployment passes its own `CliTransportConfig` (CLI name, env prefix,
|
|
18
|
+
`targetCommand`) and its own system-prompt communication guide. The abstraction
|
|
19
|
+
is what's reusable — plug in whatever host you like.
|
|
20
|
+
|
|
21
|
+
> **Provenance.** The shapes, protocols, flag strings, and control flow here were
|
|
22
|
+
> derived by studying how production agent-runtime daemons drive these CLIs, then
|
|
23
|
+
> re-expressed as a tidy, generic abstraction. It is original code, not a copy of
|
|
24
|
+
> any vendor source, and carries no platform-specific glue.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## The big idea
|
|
29
|
+
|
|
30
|
+
The daemon never speaks a runtime's native protocol directly. Each runtime is
|
|
31
|
+
wrapped by a **`Driver`** (`src/types.ts`) that knows how to:
|
|
32
|
+
|
|
33
|
+
1. **`spawn`** (or `createSession`) — launch the runtime,
|
|
34
|
+
2. **`encodeStdinMessage`** — encode an outgoing user message for the runtime's
|
|
35
|
+
input channel,
|
|
36
|
+
3. **`parseLine`** — normalize the runtime's output into a tiny shared event
|
|
37
|
+
vocabulary (`ParsedEvent`: `session_init`, `thinking`, `text`, `tool_call`,
|
|
38
|
+
`tool_output`, `compaction_*`, `turn_end`, `error`, `telemetry`).
|
|
39
|
+
|
|
40
|
+
A generic **session host** then runs the process and fans `ParsedEvent`s out to
|
|
41
|
+
the daemon:
|
|
42
|
+
|
|
43
|
+
- `ChildProcessRuntimeSession` (`src/runtime/runtimeSession.ts`) for CLI runtimes.
|
|
44
|
+
- `SdkRuntimeSession` (`src/runtime/sdkRuntimeSession.ts`) for in-process SDK
|
|
45
|
+
runtimes (pi).
|
|
46
|
+
|
|
47
|
+
Because everything downstream consumes the same `ParsedEvent` stream and the
|
|
48
|
+
same `Driver` capability flags, the rest of the daemon is transport-agnostic.
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Two delivery models (the heart of it)
|
|
53
|
+
|
|
54
|
+
When a message arrives, what happens depends on the runtime's lifecycle:
|
|
55
|
+
|
|
56
|
+
### Persistent runtimes (claude, codex, kimi, pi)
|
|
57
|
+
One long-lived process spans many turns. A new message is **written onto the
|
|
58
|
+
still-open input channel** — no restart. This is "steering."
|
|
59
|
+
|
|
60
|
+
- **`direct`** (codex, kimi, pi): write immediately; the runtime
|
|
61
|
+
tolerates injection any time.
|
|
62
|
+
- **`gated`** (claude): a raw write mid-stream could collide with an active
|
|
63
|
+
signed thinking block, so writes are **held until a safe boundary**. Two state
|
|
64
|
+
machines implement this:
|
|
65
|
+
- `RuntimeTurnState` (`src/runtime/turnState.ts`) — the instantaneous gate:
|
|
66
|
+
`canSteerBusy = currentTurnId && !steeringGateActive`. Tool boundary closes
|
|
67
|
+
the gate; progress / turn-start reopens it.
|
|
68
|
+
- `apmStateMachine` (`src/runtime/apmStateMachine.ts`) — the policy reducers
|
|
69
|
+
that decide *when* to flush queued inbox notices and emit the concrete
|
|
70
|
+
`notify_stdin` / `deliver_stdin` effects (clause `SMR-002`). Flushing is
|
|
71
|
+
blocked while `outstanding_tool_uses > 0`, while `compacting`, while
|
|
72
|
+
`reviewing` (Codex review mode), or when `tool_boundary_flush_disabled`.
|
|
73
|
+
Compaction *and* review exit are treated as safe flush boundaries.
|
|
74
|
+
|
|
75
|
+
For Claude specifically, the input channel is **stream-json**: the process is
|
|
76
|
+
launched with `--input-format stream-json --output-format stream-json
|
|
77
|
+
--include-partial-messages`, and each message is one NDJSON line
|
|
78
|
+
`{"type":"user","message":{"role":"user","content":[{"type":"text","text":…}]}}`.
|
|
79
|
+
|
|
80
|
+
### Per-turn runtimes (gemini, copilot, cursor, opencode, antigravity)
|
|
81
|
+
The process handles exactly one turn and exits. `supportsStdinNotification` is
|
|
82
|
+
`false` and `encodeStdinMessage` returns `null`. A new message means a **brand-new
|
|
83
|
+
process**; the agent re-checks the inbox each wake. This lifecycle
|
|
84
|
+
(`lifecycle.kind: "per_turn"`) drives the `## Message Notifications` section
|
|
85
|
+
generated by `buildCliSystemPrompt` — no driver hand-writes this reminder.
|
|
86
|
+
`opencode` is a special per-turn case: it defers spawning until a concrete
|
|
87
|
+
message and terminates the process on turn end.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Runtime comparison
|
|
92
|
+
|
|
93
|
+
| Runtime | Lifecycle | Transport / protocol | Steering | Initial input | Output format |
|
|
94
|
+
|---|---|---|---|---|---|
|
|
95
|
+
| **claude** | persistent | child process, stream-json NDJSON | `gated` | `{type:"user",…}` line on stdin | stream-json |
|
|
96
|
+
| **codex** | persistent | child process, JSON-RPC 2.0 (`app-server --listen stdio://`) | `direct` (`turn/steer`) | `initialize` → `thread/start`/`resume` | JSON-RPC notifications |
|
|
97
|
+
| **kimi** | persistent | child process, JSON-RPC "wire" | `direct` (`steer`) | `initialize` → `prompt` | JSON-RPC events |
|
|
98
|
+
| **pi** | persistent | in-process SDK (`@earendil-works/pi-coding-agent`), multi-provider | `direct` | `session.prompt()` | SDK event callback |
|
|
99
|
+
| **gemini** | per-turn | child process, stream-json | none | prompt on stdin, then close | stream-json |
|
|
100
|
+
| **copilot** | per-turn | child process, JSON | none | prompt as `-p` arg | JSON events |
|
|
101
|
+
| **cursor** | per-turn | child process, stream-json | none | prompt as trailing arg | stream-json |
|
|
102
|
+
| **opencode** | per-turn (defer-spawn, terminate-on-end) | child process, JSON | none | prompt as `-- <arg>` | JSON events |
|
|
103
|
+
| **antigravity** | per-turn | child process, **plain text** | none | prompt on stdin, then close | plain text lines |
|
|
104
|
+
|
|
105
|
+
(Each driver's exact launch flags live in its file under `src/drivers/`.)
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Layout
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
src/
|
|
113
|
+
types.ts # Driver interface + ParsedEvent + lifecycle/capability types
|
|
114
|
+
index.ts # public entry point
|
|
115
|
+
logger.ts # tiny structured logger (ALOOK_LOG_LEVEL); wired through
|
|
116
|
+
# wsControlChannel.ts/agentRouter.ts/managerRuntime.ts too, not just cli/daemonStart.ts
|
|
117
|
+
drivers/
|
|
118
|
+
index.ts # getDriver(runtimeId) registry ← start here
|
|
119
|
+
cliTransport.ts # shared: state dir, token, decoupling cliName wrapper -> targetCommand, env
|
|
120
|
+
systemPrompt.ts # shared: standing-prompt assembly
|
|
121
|
+
probe.ts # CLI/binary detection + version
|
|
122
|
+
claude.ts # Claude Code driver
|
|
123
|
+
claudeLaunch.ts # args / command / spawn-spec / prompt-file
|
|
124
|
+
claudeProviderIsolation.ts # custom-provider HOME/config isolation
|
|
125
|
+
claudeEventNormalizer.ts # stream-json → ParsedEvent
|
|
126
|
+
codex.ts / codexEventNormalizer.ts / codexTelemetrySidecar.ts
|
|
127
|
+
gemini.ts copilot.ts cursor.ts opencode.ts antigravity.ts
|
|
128
|
+
kimi.ts # child-process Kimi (JSON-RPC wire)
|
|
129
|
+
pi.ts # in-process Pi SDK (multi-provider)
|
|
130
|
+
runtime/
|
|
131
|
+
runtimeSession.ts # ChildProcessRuntimeSession + descriptor
|
|
132
|
+
sdkRuntimeSession.ts # SdkRuntimeSession (in-process)
|
|
133
|
+
turnState.ts # instantaneous steering gate
|
|
134
|
+
apmStateMachine.ts # gated-delivery policy reducers (SMR-002)
|
|
135
|
+
progressState.ts # liveness / stall detection
|
|
136
|
+
notificationState.ts # inbox-notice de-dup + batching
|
|
137
|
+
errorDiagnostics.ts # runtime-error classification + scrubbing
|
|
138
|
+
inbox/
|
|
139
|
+
projection.ts # bucket pending messages by target → notice snapshots
|
|
140
|
+
stateMachine.ts # pre-action freshness guard (forward / hold / bypass)
|
|
141
|
+
manager/
|
|
142
|
+
managerPolicy.ts # pure reducer: single-flight, wake/sleep, queue+coalesce, stalled
|
|
143
|
+
managerRuntime.ts # AgentProcessManager: applies effects to real sessions
|
|
144
|
+
server/
|
|
145
|
+
contract.ts # ServerApi (data) + EnrollmentApi (machine→runner key) + HostControlChannel (control) + AdminApi
|
|
146
|
+
wsControlServer.ts # server end of the control plane over ws (machine-key authed)
|
|
147
|
+
wsControlChannel.ts # host end: WebSocket HostControlChannel (injectable socket, reconnect + heartbeat + resync)
|
|
148
|
+
daemon/
|
|
149
|
+
createDaemon.ts # runtime-agnostic daemon factory (driver/sessionFactory/runtimes INJECTED; no test code)
|
|
150
|
+
cli/
|
|
151
|
+
index.ts # the `alook` agent CLI skeleton (message send / inbox pull)
|
|
152
|
+
proxyServerApi.ts # agent data-plane client: voucher → proxy → server (identity from the voucher)
|
|
153
|
+
credentials/
|
|
154
|
+
credentialProxy.ts # CredentialBroker (mint/revoke/check per-voucher vouchers) + local key-swapping proxy
|
|
155
|
+
scripts/
|
|
156
|
+
daemon.ts # DAEMON process: thin launcher — wraps `alook daemon start` for local dev
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Host orchestration (manager + server)
|
|
160
|
+
|
|
161
|
+
Beyond driving a single runtime, the backend includes the **host-side**
|
|
162
|
+
orchestration so it can run agents end-to-end:
|
|
163
|
+
|
|
164
|
+
- **`manager/`** — `AgentProcessManager` schedules processes. Decisions live in a
|
|
165
|
+
pure reducer (`managerPolicy.ts`: single-flight one-process-per-agent, wake/sleep,
|
|
166
|
+
message queue + coalesce, stalled detection → `spawn`/`send`/`stop`/`terminate_stalled`
|
|
167
|
+
effects); a thin executor (`managerRuntime.ts`) applies them to real sessions and
|
|
168
|
+
feeds runtime events back in. `AgentRouter` consumes the control plane (below).
|
|
169
|
+
- **`server/`** — the agent ⇄ server boundary, split into two planes that share
|
|
170
|
+
one contract (`contract.ts`):
|
|
171
|
+
- **data plane** (`ServerApi`): what the `alook` CLI calls — `send` / `inboxPull` /
|
|
172
|
+
`ack` / `read` / `listChannels`, addressed by path-style `ChannelRef`s.
|
|
173
|
+
- **control plane** (`HostCommand` + `HostControlChannel`): server → host commands
|
|
174
|
+
(`agent:wake` / `agent:stop`; plus `bot:*` lifecycle frames). `agent:wake` carries a
|
|
175
|
+
bodiless `UnreadNotice` (channel + latest seq, no message content) — the DAEMON
|
|
176
|
+
(`AgentRouter`/`AgentProcessManager`), not the server, decides whether to spawn a
|
|
177
|
+
process, notify an already-running one, or coalesce the notice for the next turn.
|
|
178
|
+
There is no in-process fixture standing in for the server: `wsControlServer.ts`
|
|
179
|
+
(server end) and `wsControlChannel.ts` (host end, injectable socket +
|
|
180
|
+
exponential-backoff reconnect + heartbeat) carry the `HostCommand` frames over
|
|
181
|
+
a real WebSocket (`ws://127.0.0.1` in local dev, a real `ws-do` URL in
|
|
182
|
+
production). A real server connection is the same `WsControlChannel` pointed
|
|
183
|
+
at a real URL — nothing upstream changes.
|
|
184
|
+
|
|
185
|
+
`tests/integration/daemon/control-plane.test.ts` wires a real `WsControlChannel`
|
|
186
|
+
against a running `ws-do dev` server into a complete loop: seed a server/bot/
|
|
187
|
+
machine, post a message as a human owner over real HTTP, and assert the daemon's
|
|
188
|
+
control channel receives the resulting `agent:wake` `HostCommand` — then, acting
|
|
189
|
+
as the agent (no CLI spawned), replies over the real credential chain
|
|
190
|
+
(enroll → voucher → proxy → X-Agent-Id) and asserts the reply is readable back
|
|
191
|
+
over real HTTP. See "Point a daemon at real infra locally" below for how to run
|
|
192
|
+
the pieces this test drives by hand.
|
|
193
|
+
|
|
194
|
+
## Credentials (zero-trust isolation)
|
|
195
|
+
|
|
196
|
+
A spawned runtime needs *some* credential to call back to the server, but handing
|
|
197
|
+
it the real API key means the agent process (and any tool it runs) can read it,
|
|
198
|
+
can't be scoped, and a leak forces a rotation. `credentials/credentialProxy.ts`
|
|
199
|
+
implements the **voucher** pattern instead — the same shape a production daemon
|
|
200
|
+
uses:
|
|
201
|
+
|
|
202
|
+
1. The host starts one local key-swapping proxy on `127.0.0.1` (loopback only).
|
|
203
|
+
2. For each launch, a `CredentialBroker` **mints a per-launch voucher** (`vch_…`)
|
|
204
|
+
into a 0600 file. The child is given `<PREFIX>_PROXY_URL` +
|
|
205
|
+
`<PREFIX>_PROXY_TOKEN_FILE` + its capability set — **never the real key**.
|
|
206
|
+
3. The child's CLI calls the proxy with `Authorization: Bearer vch_…`.
|
|
207
|
+
4. The proxy validates the voucher, **swaps in the real `Bearer <hostApiKey>`**,
|
|
208
|
+
stamps `X-Agent-Id` / `X-Client` / `X-Agent-Active-Capabilities`, and forwards
|
|
209
|
+
to the host-supplied upstream.
|
|
210
|
+
|
|
211
|
+
This buys **credential isolation** (the agent only ever holds a voucher),
|
|
212
|
+
**capability scoping** (the proxy rejects endpoints outside the voucher's caps),
|
|
213
|
+
and **revocability** (vouchers are per-launch and individually revocable; rotating
|
|
214
|
+
a leaked voucher never touches the real key). This is the **only** credential path
|
|
215
|
+
— `cliTransport` requires `ctx.credentialProxy` (a running broker + proxy URL) and
|
|
216
|
+
throws without it; there is no plaintext fallback that would hand the agent a real
|
|
217
|
+
key. Everything is host-neutral: the real key, upstream URL, voucher prefix, and
|
|
218
|
+
header names all come from the host via `CredentialBrokerConfig`.
|
|
219
|
+
|
|
220
|
+
`src/credentials/credentialProxy.test.ts` covers the swap, voucher rejection
|
|
221
|
+
(`invalid local agent proxy token`), capability scoping, and revocation against a
|
|
222
|
+
throwaway upstream.
|
|
223
|
+
|
|
224
|
+
### Three key tiers (and where each lives)
|
|
225
|
+
|
|
226
|
+
A production daemon keeps **three** credential tiers, not two — important for
|
|
227
|
+
understanding what `hostApiKey` should be:
|
|
228
|
+
|
|
229
|
+
1. **Machine master key** — authenticates the daemon to the server and is used to
|
|
230
|
+
**mint** per-agent credentials. Held in daemon memory only (not persisted to
|
|
231
|
+
disk or keychain). ⚠️ In the current daemon it is passed as a process **argv**
|
|
232
|
+
(`--api-key …`), so any local process can read its full value via `ps aux`.
|
|
233
|
+
Mitigation: source it from an env var, a 0600 file, or the OS keychain instead
|
|
234
|
+
of argv. This proxy **never forwards the master key**.
|
|
235
|
+
2. **Per-agent runner credential** — the server mints one per agent (scoped to
|
|
236
|
+
that agent, individually revocable) when the daemon asks with the master key.
|
|
237
|
+
This is what the proxy actually swaps in and forwards upstream, and what
|
|
238
|
+
belongs in `CredentialBrokerConfig.hostApiKey`. Minting it from the master key
|
|
239
|
+
is **host-side orchestration** and is intentionally out of scope for this
|
|
240
|
+
backend.
|
|
241
|
+
3. **`vch_` voucher** — minted by `CredentialBroker` per launch, written to a 0600
|
|
242
|
+
file, and the only credential the agent process ever sees.
|
|
243
|
+
|
|
244
|
+
## Inbox layer (freshness)
|
|
245
|
+
|
|
246
|
+
Two pieces govern how messages reach the agent and how outgoing actions are
|
|
247
|
+
gated:
|
|
248
|
+
|
|
249
|
+
- **`inbox/projection.ts`** — `projectAgentInboxSnapshot` buckets pending
|
|
250
|
+
messages by target (channel / DM / thread) and projects each into the
|
|
251
|
+
metadata-only summary surfaced as an `[inbox notice: …]` (count, first,
|
|
252
|
+
latest, sender, flags). No bodies — the agent pulls those with
|
|
253
|
+
`alook inbox pull`.
|
|
254
|
+
- **`inbox/stateMachine.ts`** — `planAgentInboxSideEffect` is the "don't reply on
|
|
255
|
+
stale context" guard. Before an outward action (`send` / `task_claim` /
|
|
256
|
+
`task_update`) it compares the model's seen `seq` boundary against pending
|
|
257
|
+
messages and returns **forward** (let it through), **held** (hold the action,
|
|
258
|
+
surface the latest few unseen messages as held context to reconcile first —
|
|
259
|
+
this is the "Freshness hold → saved as a draft" you can hit when messaging a
|
|
260
|
+
busy agent), or **bypass** (explicit `continueAnyway`). It is a pure function
|
|
261
|
+
producing a stable `producerFactId`; clause ids `SMR-002` (consume) and
|
|
262
|
+
`SMR-006` (held envelope) are preserved.
|
|
263
|
+
|
|
264
|
+
## Build & run
|
|
265
|
+
|
|
266
|
+
This project uses **pnpm** (see the `packageManager` field).
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
pnpm install
|
|
270
|
+
pnpm run typecheck # tsc --noEmit (passes clean)
|
|
271
|
+
pnpm test # vitest — unit tests only (drivers/manager/inbox/credentials/server)
|
|
272
|
+
pnpm run test:integration # real infra: requires wrangler dev + ws-do dev + wake-worker dev (see below)
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Unit behavior is covered by `src/**/*.test.ts`. Full control-plane and
|
|
276
|
+
credential-chain behavior against real infra is covered by
|
|
277
|
+
[`tests/integration/daemon/`](../../tests/integration/daemon) — see that
|
|
278
|
+
directory for the executable reference of how the pieces fit together over
|
|
279
|
+
the network (real `WsControlChannel` over a real WebSocket, real
|
|
280
|
+
`enroll-agent`/credential-proxy HTTP calls, no in-process shortcuts).
|
|
281
|
+
|
|
282
|
+
### Point a daemon at real infra locally
|
|
283
|
+
|
|
284
|
+
There's no mock server to stand in for `src/web`/`src/ws-do` — a daemon
|
|
285
|
+
always talks to the real thing. To drive one by hand in local dev, run the
|
|
286
|
+
real servers and walk the real credential chain (`cmt_` pairing token →
|
|
287
|
+
`cmk_` daemon credential → `crk_` per-agent runner key):
|
|
288
|
+
|
|
289
|
+
**Terminal 1 — the web app + control-plane DO:**
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
pnpm --filter @alook/web dev # http://localhost:3000
|
|
293
|
+
pnpm --filter @alook/ws-do dev # ws://localhost:8789
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
**Terminal 2 — pair + activate a machine**, then start a daemon with the
|
|
297
|
+
resulting `cmk_...` credential:
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
# 1. As a signed-in user, create a pairing token:
|
|
301
|
+
curl -s -X POST http://localhost:3000/api/community/machines/pair \
|
|
302
|
+
-H "Cookie: <session-cookie>" | tee /tmp/pair.json
|
|
303
|
+
# → { "tokenId": "cmt_…", "expiresAt": "…" }
|
|
304
|
+
|
|
305
|
+
# 2. Exchange it for a daemon credential:
|
|
306
|
+
curl -s -X POST http://localhost:3000/api/community/daemon/activate \
|
|
307
|
+
-H "Authorization: Bearer $(jq -r .tokenId /tmp/pair.json)" \
|
|
308
|
+
-H "content-type: application/json" \
|
|
309
|
+
-d '{"hostname":"my-laptop","platform":"darwin","arch":"arm64"}'
|
|
310
|
+
# → { "credential": "cmk_…", "machineId": "cm_…", "expiresAt": null }
|
|
311
|
+
|
|
312
|
+
# 3. Start the daemon with that credential:
|
|
313
|
+
ALOOK_MACHINE_KEY=cmk_… \
|
|
314
|
+
ALOOK_SERVER_URL=http://localhost:3000 \
|
|
315
|
+
ALOOK_SERVER_WS_URL=ws://localhost:8789 \
|
|
316
|
+
pnpm run daemon
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
A bot bound to this machine (via the community UI/API) can now be woken by
|
|
320
|
+
posting a message in a channel it's a member of — the real wake-producer path
|
|
321
|
+
(`src/web` → `src/wake-worker` → `src/ws-do`) delivers `agent:wake` over the
|
|
322
|
+
daemon's real `WsControlChannel`.
|
|
323
|
+
|
|
324
|
+
> **Run the source, not `dist/` (yet).** `pnpm run build` emits JS, but the
|
|
325
|
+
> current Bundler-resolution emit produces extensionless relative imports that
|
|
326
|
+
> Node's native ESM rejects (`Cannot find module './types'`). Until that's
|
|
327
|
+
> switched to NodeNext or bundled, consume the library through a TS toolchain
|
|
328
|
+
> (tsx / ts-node / vite / esbuild / webpack).
|
|
329
|
+
|
|
330
|
+
## Usage sketch
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
import { getDriver, createChildProcessRuntimeSession } from "@alook/daemon";
|
|
334
|
+
|
|
335
|
+
const driver = getDriver("claude");
|
|
336
|
+
const session = createChildProcessRuntimeSession(driver, ctx);
|
|
337
|
+
session.on("runtime_event", (e) => handleParsedEvent(e));
|
|
338
|
+
await session.start({ text: initialPrompt });
|
|
339
|
+
|
|
340
|
+
// Later, a message arrives while the agent is working:
|
|
341
|
+
session.send({ text: "new message from #general", mode: "busy" }); // steer
|
|
342
|
+
```
|