podster 0.0.1-test.1 → 0.0.1-test.2
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/README.md +21 -1
- package/build/core/capabilities.js +7 -0
- package/build/core/lifecycle.js +4 -3
- package/build/core/nudge.js +340 -0
- package/build/core/poller.js +11 -3
- package/build/core/runtime.js +1 -1
- package/build/core/storage.js +21 -1
- package/build/node/cli.js +26 -2
- package/build/node/nudge-command.js +132 -0
- package/build/node/nudge-listener.js +137 -0
- package/build/node/run-command.js +2 -2
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,6 +62,26 @@ Runner refuses a key file that's a link, that someone else can read, or that hol
|
|
|
62
62
|
|
|
63
63
|
Runner's two container images, one for each task and one for Claude Code, are pinned by image ID in Runner's home. The first time `run --provider-key-file` finds one missing, Runner builds it with Docker from the Dockerfile in this package, says so first, and pins what it built. A build takes a few minutes, and the Claude Code image downloads Claude Code from npm's registry. If a build fails, `run` stops before running anything.
|
|
64
64
|
|
|
65
|
+
## Nudges (optional, off by default)
|
|
66
|
+
|
|
67
|
+
Runner polls, and that's enough to get every offer. If you want offers picked up sooner, you can also let Podster nudge Runner. A nudge only makes Runner poll sooner: it never carries a task, never starts a run by itself, and polling stays on, so a missed nudge loses nothing. **Proposed** (M7 step 14), not security-reviewed.
|
|
68
|
+
|
|
69
|
+
You need a public HTTPS address you control that forwards to your machine: a reverse proxy, or a tunnel you run (for example `cloudflared`). It ends TLS and forwards to Runner's port on `127.0.0.1` with the path unchanged. Podster sends only to `https://` at a DNS name on the default port. Runner listens only on `127.0.0.1`, never on your network.
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npx --ignore-scripts podster@test run --nudge-listen 8787 --nudge-url https://runner.example.com/podster/nudge
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Runner then:
|
|
76
|
+
|
|
77
|
+
1. listens on `127.0.0.1:8787`;
|
|
78
|
+
2. registers the address with Podster as this agent's nudge endpoint, and keeps the signing secret Podster gives it in the key store with its other keys, never in a file, a log or an argument;
|
|
79
|
+
3. asks Podster to check it: Podster sends one signed challenge to the address, and Runner answers with a proof only the secret can make.
|
|
80
|
+
|
|
81
|
+
Runner prints the endpoint's version. An admin of the agent's organization switches the nudge on for that version in Podster; the agent can't. Starting Runner again with the same address keeps the registration and the switch. A new address, or a secret Runner no longer holds, needs the check and the switch again.
|
|
82
|
+
|
|
83
|
+
Runner takes a request only if its signature checks out under the secret and its time is within 5 minutes, before reading anything in it. It refuses replays, bodies over 8 KB and anything but a nudge or the challenge, and it limits how many requests it takes a minute. Stopping Runner closes the port. Podster switches the endpoint off after 3 failed nudges in a row; polling goes on either way.
|
|
84
|
+
|
|
65
85
|
## Linux
|
|
66
86
|
|
|
67
87
|
**Validated so far only by CI's Linux job,** on GitHub's Ubuntu runner with Docker Engine (S-47). That job builds both images, starts the containers with the relay, and checks that the provider key is in none of them, that the relay refuses a request without its per-run token, and that nothing is left afterwards. It calls no provider. No task has run end to end with Claude Code on Linux yet.
|
|
@@ -94,4 +114,4 @@ A WSL2 distro usually has no Secret Service, so Runner uses its file store there
|
|
|
94
114
|
|
|
95
115
|
## Where it comes from
|
|
96
116
|
|
|
97
|
-
Each version is built and published by the `runner-prerelease` workflow on `main` of `github.com/thachp/podster`, with npm provenance. The one
|
|
117
|
+
Each version is built and published by the `runner-prerelease` workflow on `main` of `github.com/thachp/podster`, with npm provenance. The one exception is the first, `0.0.1-test.1`, which the owner published once from that workflow's own build, without provenance, on 2026-11-19 (see `docs/runner-prerelease.md` in the repository). The package refuses a plain `npm publish` from a machine, but that check only catches mistakes: what limits who can publish is npm's own trusted-publisher and package settings. MIT licensed.
|
|
@@ -9,6 +9,11 @@
|
|
|
9
9
|
* the default one, a fixed set of GitHub calls with no merge, and the token kept in Runner's broker on the host. Only
|
|
10
10
|
* `src/node/repo-path.ts` calls it; a test holds that.
|
|
11
11
|
*
|
|
12
|
+
* `set_a2a_endpoint` and `verify_a2a_endpoint` (M7 step 14) are called only when the owner starts `run` with the inbound
|
|
13
|
+
* nudge switched on, and only with `delivery: "nudge"`: they register the owner's HTTPS address and prove Runner holds
|
|
14
|
+
* its signing secret. Neither grants anything in a room, and the nudge sends nothing until a person who administers the
|
|
15
|
+
* agent's organization switches it on in Podster. Only `src/core/nudge.ts` names them; a test holds that.
|
|
16
|
+
*
|
|
12
17
|
* Step 6 calls only the reads and the inbox. The lifecycle calls are named now so the registered agent is the one later
|
|
13
18
|
* steps use: Runner registers once per pairing and never again by itself.
|
|
14
19
|
*/
|
|
@@ -31,6 +36,8 @@ export const RUNNER_CAPABILITIES = [
|
|
|
31
36
|
"get_asset_link",
|
|
32
37
|
"list_assets",
|
|
33
38
|
"get_repo_access",
|
|
39
|
+
"set_a2a_endpoint",
|
|
40
|
+
"verify_a2a_endpoint",
|
|
34
41
|
];
|
|
35
42
|
/**
|
|
36
43
|
* Capabilities a deployment lists only while a feature is switched on: `get_repo_access` only where managed repositories
|
package/build/core/lifecycle.js
CHANGED
|
@@ -4,7 +4,7 @@ import { pullRequestUrl, RepoPathError } from "./repository.js";
|
|
|
4
4
|
import { readTaskDetail, sha256Hex } from "./prompt.js";
|
|
5
5
|
import { cleanResult, composeNote } from "./results.js";
|
|
6
6
|
import { parseRunResult } from "./tool-policy.js";
|
|
7
|
-
import { AuthorityEnded } from "./poller.js";
|
|
7
|
+
import { AuthorityEnded, waitForNext } from "./poller.js";
|
|
8
8
|
import { MAX_PROMPT_BYTES } from "./run-interface.js";
|
|
9
9
|
import { budgetOf, clockWentBack, dayOf, newEntry, owedSubmission, reserve, settle } from "./runs.js";
|
|
10
10
|
/** The check-in's fixed maximum interval during a run (S-33, S-43). **Proposed** figure. */
|
|
@@ -1178,7 +1178,7 @@ function askView(entry, decision, detail) {
|
|
|
1178
1178
|
* Runner's loop with the lifecycle: a poll, then a lifecycle pass, then the wait. A local stop (`signal`) ends it at once,
|
|
1179
1179
|
* a run included, and nothing is called after it; a revoked agent ends it too.
|
|
1180
1180
|
*/
|
|
1181
|
-
export async function runWithLifecycle(poller, lifecycle, clock, signal) {
|
|
1181
|
+
export async function runWithLifecycle(poller, lifecycle, clock, signal, bell) {
|
|
1182
1182
|
while (!signal.aborted) {
|
|
1183
1183
|
const polled = await poller.pollOnce();
|
|
1184
1184
|
if (polled === "revoked")
|
|
@@ -1190,7 +1190,8 @@ export async function runWithLifecycle(poller, lifecycle, clock, signal) {
|
|
|
1190
1190
|
if (outcome !== "idle")
|
|
1191
1191
|
return outcome;
|
|
1192
1192
|
}
|
|
1193
|
-
|
|
1193
|
+
// A nudge (M7 step 14) only ends this wait early: the next poll and tick are the ordinary ones.
|
|
1194
|
+
await waitForNext(clock, poller.nextWaitMs(polled), polled, signal, bell);
|
|
1194
1195
|
}
|
|
1195
1196
|
return "stopped";
|
|
1196
1197
|
}
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { isId } from "./state.js";
|
|
2
|
+
export const NUDGE_KIND = "podster.dev/nudge";
|
|
3
|
+
export const VERIFY_METHOD = "podster.dev/verifyEndpoint";
|
|
4
|
+
/** What the proof is an HMAC of, before the agent, version and challenge (`podster_egress::nudge::PROOF_LABEL`). */
|
|
5
|
+
export const PROOF_LABEL = "podster-endpoint-proof/1";
|
|
6
|
+
/** **Proposed:** the largest body read. A nudge is about 600 bytes and a challenge about 250. */
|
|
7
|
+
export const MAX_NUDGE_BODY_BYTES = 8 * 1024;
|
|
8
|
+
/** **Proposed:** how far a signature's time may be from Runner's clock, either way. */
|
|
9
|
+
export const SIGNATURE_WINDOW_S = 300;
|
|
10
|
+
/** **Proposed:** requests taken at once, and refilled per minute, for the whole endpoint. */
|
|
11
|
+
export const RATE_BURST = 30;
|
|
12
|
+
export const RATE_PER_MINUTE = 60;
|
|
13
|
+
/** **Proposed:** signatures, attempts and challenges remembered, each for twice the window. */
|
|
14
|
+
export const SEEN_LIMIT = 512;
|
|
15
|
+
/**
|
|
16
|
+
* **Proposed:** the longest public URL taken, under Podster's 2,048. The key-store item holding it with the secret must fit
|
|
17
|
+
* one line of macOS's keychain tool, which a test measures.
|
|
18
|
+
*/
|
|
19
|
+
export const MAX_NUDGE_URL_BYTES = 1024;
|
|
20
|
+
/** The shortest wait after a poll, nudged or not: Runner's shortest poll interval, so nudges never poll faster. */
|
|
21
|
+
export const NUDGE_GAP_MS = 5_000;
|
|
22
|
+
const SIGNATURE = /^t=(\d{1,12}),v1=([0-9a-f]{64})$/;
|
|
23
|
+
const CHALLENGE = /^[0-9a-f]{64}$/;
|
|
24
|
+
const ATTEMPT_KINDS = new Set(["offer", "change_request"]);
|
|
25
|
+
const encoder = new TextEncoder();
|
|
26
|
+
export function isRegistration(value) {
|
|
27
|
+
const v = value;
|
|
28
|
+
return !!v && typeof v === "object" && typeof v.url === "string" && isId(v.agent_id) && Number.isSafeInteger(v.version) && v.version > 0 && typeof v.secret === "string" && v.secret.length > 0;
|
|
29
|
+
}
|
|
30
|
+
const hex = (bytes) => [...new Uint8Array(bytes)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
31
|
+
const hmacKey = (secret, usage) => crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, [usage]);
|
|
32
|
+
/** Lowercase hex HMAC-SHA256 of `text` under `secret`. */
|
|
33
|
+
export async function hmacHex(secret, text) {
|
|
34
|
+
return hex(await crypto.subtle.sign("HMAC", await hmacKey(secret, "sign"), encoder.encode(text)));
|
|
35
|
+
}
|
|
36
|
+
/** The answer to Podster's challenge: `podster_egress::nudge::proof`. */
|
|
37
|
+
export function proofFor(secret, agent, version, challenge) {
|
|
38
|
+
return hmacHex(secret, `${PROOF_LABEL}.${agent}.${version}.${challenge}`);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether `header` signs `body` under `secret` at a time within the window of `nowMs`. The HMAC is checked first, by
|
|
42
|
+
* Web Crypto's own constant-time verify, so the time is only read from a signature that's genuine.
|
|
43
|
+
*/
|
|
44
|
+
export async function checkSignature(secret, header, body, nowMs) {
|
|
45
|
+
if (header === null || header === "")
|
|
46
|
+
return { ok: false, code: "unsigned" };
|
|
47
|
+
const match = SIGNATURE.exec(header);
|
|
48
|
+
if (!match)
|
|
49
|
+
return { ok: false, code: "forged" };
|
|
50
|
+
const [, t, digest] = match;
|
|
51
|
+
const given = new Uint8Array(digest.match(/../g).map((pair) => parseInt(pair, 16)));
|
|
52
|
+
const genuine = await crypto.subtle.verify("HMAC", await hmacKey(secret, "verify"), given, encoder.encode(`${t}.${body}`));
|
|
53
|
+
if (!genuine)
|
|
54
|
+
return { ok: false, code: "forged" };
|
|
55
|
+
if (Math.abs(Number(t) - Math.floor(nowMs / 1000)) > SIGNATURE_WINDOW_S)
|
|
56
|
+
return { ok: false, code: "stale" };
|
|
57
|
+
return { ok: true, digest };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The public URL the owner registers: HTTPS, a DNS name with a dot, the default port, no credentials, query or fragment.
|
|
61
|
+
* Podster checks it again under its own egress rules. Answers the URL as written out, or null.
|
|
62
|
+
*/
|
|
63
|
+
export function checkNudgeUrl(value) {
|
|
64
|
+
if (encoder.encode(value).byteLength > MAX_NUDGE_URL_BYTES)
|
|
65
|
+
return null;
|
|
66
|
+
let url;
|
|
67
|
+
try {
|
|
68
|
+
url = new URL(value);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const host = url.hostname;
|
|
74
|
+
if (url.protocol !== "https:" || url.username || url.password || url.port || url.search || url.hash)
|
|
75
|
+
return null;
|
|
76
|
+
// The URL parser writes every IPv4 form (hex, octal, short) as dotted digits, and IPv6 in brackets.
|
|
77
|
+
if (!host.includes(".") || host.startsWith("[") || /^[0-9.]+$/.test(host))
|
|
78
|
+
return null;
|
|
79
|
+
return encoder.encode(url.href).byteLength > MAX_NUDGE_URL_BYTES ? null : url.href;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The poll loop's wait, cut short by a nudge. Each wait after a good poll first waits `NUDGE_GAP_MS`, so nudges never make
|
|
83
|
+
* Runner poll faster than its shortest interval; a ring before or during that ends the wait there, and a ring later ends
|
|
84
|
+
* it at once. A ring is used once. Only a good poll's wait is ever shortened, so a nudge never cuts a backoff.
|
|
85
|
+
*/
|
|
86
|
+
export class NudgeBell {
|
|
87
|
+
pending = false;
|
|
88
|
+
wake = null;
|
|
89
|
+
gapMs;
|
|
90
|
+
constructor(gapMs = NUDGE_GAP_MS) {
|
|
91
|
+
this.gapMs = gapMs;
|
|
92
|
+
}
|
|
93
|
+
ring() {
|
|
94
|
+
this.pending = true;
|
|
95
|
+
this.wake?.();
|
|
96
|
+
}
|
|
97
|
+
take() {
|
|
98
|
+
const was = this.pending;
|
|
99
|
+
this.pending = false;
|
|
100
|
+
return was;
|
|
101
|
+
}
|
|
102
|
+
async idle(clock, ms, signal) {
|
|
103
|
+
const gap = Math.min(ms, this.gapMs);
|
|
104
|
+
await clock.idle(gap, signal);
|
|
105
|
+
if (signal.aborted || gap >= ms || this.take())
|
|
106
|
+
return;
|
|
107
|
+
const woken = new AbortController();
|
|
108
|
+
const stop = () => woken.abort();
|
|
109
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
110
|
+
this.wake = stop;
|
|
111
|
+
try {
|
|
112
|
+
await clock.idle(ms - gap, woken.signal);
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
this.wake = null;
|
|
116
|
+
signal.removeEventListener("abort", stop);
|
|
117
|
+
this.take();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const refused = (status) => new Response(null, { status, headers: { connection: "close" } });
|
|
122
|
+
const rpc = (id, answer) => new Response(JSON.stringify({ jsonrpc: "2.0", id: typeof id === "string" || typeof id === "number" ? id : null, ...answer }), { status: 200, headers: { "content-type": "application/json", connection: "close" } });
|
|
123
|
+
const isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
124
|
+
/** Reads at most `max` bytes of a request's body as UTF-8. Null if it's longer, unreadable or not UTF-8. */
|
|
125
|
+
async function readUpTo(request, max) {
|
|
126
|
+
const body = request.body;
|
|
127
|
+
if (body === null)
|
|
128
|
+
return "";
|
|
129
|
+
const reader = body.getReader();
|
|
130
|
+
const chunks = [];
|
|
131
|
+
let size = 0;
|
|
132
|
+
try {
|
|
133
|
+
for (;;) {
|
|
134
|
+
const { done, value } = await reader.read();
|
|
135
|
+
if (done)
|
|
136
|
+
break;
|
|
137
|
+
size += value.byteLength;
|
|
138
|
+
if (size > max) {
|
|
139
|
+
void reader.cancel().catch(() => undefined);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
chunks.push(value);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
void reader.cancel().catch(() => undefined);
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const bytes = new Uint8Array(size);
|
|
150
|
+
let at = 0;
|
|
151
|
+
for (const chunk of chunks) {
|
|
152
|
+
bytes.set(chunk, at);
|
|
153
|
+
at += chunk.byteLength;
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
export class NudgeEndpoint {
|
|
163
|
+
deps;
|
|
164
|
+
/** Each key seen, with when it may be forgotten. Insertion order is age order. */
|
|
165
|
+
seen = new Map();
|
|
166
|
+
tokens = RATE_BURST;
|
|
167
|
+
refilledAt;
|
|
168
|
+
constructor(deps) {
|
|
169
|
+
this.deps = deps;
|
|
170
|
+
this.refilledAt = deps.clock.now();
|
|
171
|
+
}
|
|
172
|
+
/** How many keys the replay memory holds now, for tests. */
|
|
173
|
+
get remembered() {
|
|
174
|
+
return this.seen.size;
|
|
175
|
+
}
|
|
176
|
+
log(outcome, code, ids = {}) {
|
|
177
|
+
this.deps.log("nudge.request", outcome, { ...ids, ...(code ? { code } : {}) });
|
|
178
|
+
}
|
|
179
|
+
refuse(status, code) {
|
|
180
|
+
this.log("refused", code);
|
|
181
|
+
return refused(status);
|
|
182
|
+
}
|
|
183
|
+
admit() {
|
|
184
|
+
const now = this.deps.clock.now();
|
|
185
|
+
this.tokens = Math.min(RATE_BURST, this.tokens + ((now - this.refilledAt) * RATE_PER_MINUTE) / 60_000);
|
|
186
|
+
this.refilledAt = now;
|
|
187
|
+
if (this.tokens < 1)
|
|
188
|
+
return false;
|
|
189
|
+
this.tokens -= 1;
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
/** Forgets what's past its time. Answers whether there's room for `count` more. */
|
|
193
|
+
room(count) {
|
|
194
|
+
const now = this.deps.clock.now();
|
|
195
|
+
for (const [key, until] of this.seen) {
|
|
196
|
+
if (until > now)
|
|
197
|
+
break;
|
|
198
|
+
this.seen.delete(key);
|
|
199
|
+
}
|
|
200
|
+
return this.seen.size + count <= SEEN_LIMIT;
|
|
201
|
+
}
|
|
202
|
+
remember(...keys) {
|
|
203
|
+
const until = this.deps.clock.now() + 2 * SIGNATURE_WINDOW_S * 1000;
|
|
204
|
+
for (const key of keys) {
|
|
205
|
+
this.seen.delete(key);
|
|
206
|
+
this.seen.set(key, until);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async handle(request) {
|
|
210
|
+
if (!this.admit())
|
|
211
|
+
return this.refuse(429, "rate_limited");
|
|
212
|
+
if (request.headers.get("origin") !== null)
|
|
213
|
+
return this.refuse(403, "origin_refused");
|
|
214
|
+
if (request.method !== "POST")
|
|
215
|
+
return this.refuse(405, "wrong_method");
|
|
216
|
+
if (new URL(request.url).pathname !== this.deps.path)
|
|
217
|
+
return this.refuse(404, "wrong_path");
|
|
218
|
+
const registration = this.deps.registration();
|
|
219
|
+
if (registration === null)
|
|
220
|
+
return this.refuse(503, "no_registration");
|
|
221
|
+
const declared = request.headers.get("content-length");
|
|
222
|
+
if (declared !== null && !(Number(declared) <= MAX_NUDGE_BODY_BYTES))
|
|
223
|
+
return this.refuse(413, "too_large");
|
|
224
|
+
const body = await readUpTo(request, MAX_NUDGE_BODY_BYTES);
|
|
225
|
+
if (body === null)
|
|
226
|
+
return this.refuse(413, "too_large");
|
|
227
|
+
// Nothing in the body is looked at until its signature and time pass.
|
|
228
|
+
const signature = await checkSignature(registration.secret, request.headers.get("x-podster-signature"), body, this.deps.clock.now());
|
|
229
|
+
if (!signature.ok)
|
|
230
|
+
return this.refuse(401, signature.code);
|
|
231
|
+
if (!this.room(0))
|
|
232
|
+
return this.refuse(503, "memory_full");
|
|
233
|
+
if (this.seen.has(`sig:${signature.digest}`))
|
|
234
|
+
return this.refuse(409, "replayed");
|
|
235
|
+
let parsed;
|
|
236
|
+
try {
|
|
237
|
+
parsed = JSON.parse(body);
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return this.refuse(400, "bad_request");
|
|
241
|
+
}
|
|
242
|
+
if (!isObject(parsed) || parsed.jsonrpc !== "2.0" || !isObject(parsed.params))
|
|
243
|
+
return this.refuse(400, "bad_request");
|
|
244
|
+
const { id, method, params } = parsed;
|
|
245
|
+
if (method === VERIFY_METHOD)
|
|
246
|
+
return this.verify(registration, id, params, signature.digest);
|
|
247
|
+
if (method === "SendMessage")
|
|
248
|
+
return this.nudge(id, params, signature.digest);
|
|
249
|
+
this.log("refused", "wrong_kind");
|
|
250
|
+
return rpc(id, { error: { code: -32601, message: "Only a Podster nudge is taken here." } });
|
|
251
|
+
}
|
|
252
|
+
async verify(registration, id, params, digest) {
|
|
253
|
+
const { agent_id: agent, version, challenge } = params;
|
|
254
|
+
if (agent !== registration.agent_id || version !== registration.version || typeof challenge !== "string" || !CHALLENGE.test(challenge) || id !== challenge) {
|
|
255
|
+
this.log("refused", "wrong_registration");
|
|
256
|
+
return rpc(id, { error: { code: -32602, message: "Not this endpoint's registration." } });
|
|
257
|
+
}
|
|
258
|
+
if (this.seen.has(`challenge:${challenge}`)) {
|
|
259
|
+
// Refused, and not remembered: a repeat is harmless, and remembering it could fill the memory.
|
|
260
|
+
this.log("refused", "replayed");
|
|
261
|
+
return rpc(id, { error: { code: -32602, message: "This challenge was answered already." } });
|
|
262
|
+
}
|
|
263
|
+
if (!this.room(2))
|
|
264
|
+
return this.refuse(503, "memory_full");
|
|
265
|
+
this.remember(`sig:${digest}`, `challenge:${challenge}`);
|
|
266
|
+
this.log("verified");
|
|
267
|
+
return rpc(id, { result: { proof: await proofFor(registration.secret, registration.agent_id, registration.version, challenge) } });
|
|
268
|
+
}
|
|
269
|
+
nudge(id, params, digest) {
|
|
270
|
+
const message = params.message;
|
|
271
|
+
const parts = isObject(message) ? message.parts : undefined;
|
|
272
|
+
const part = Array.isArray(parts) && parts.length === 1 && isObject(parts[0]) ? parts[0] : null;
|
|
273
|
+
const data = part && isObject(part.data) ? part.data : null;
|
|
274
|
+
const keys = data ? Object.keys(data).sort().join(",") : "";
|
|
275
|
+
if (!isObject(message) ||
|
|
276
|
+
!data ||
|
|
277
|
+
data.kind !== NUDGE_KIND ||
|
|
278
|
+
keys !== "attempt_id,attempt_kind,kind,task_id,workspace_id" ||
|
|
279
|
+
!isId(data.workspace_id) ||
|
|
280
|
+
!isId(data.task_id) ||
|
|
281
|
+
!isId(data.attempt_id) ||
|
|
282
|
+
!ATTEMPT_KINDS.has(data.attempt_kind) ||
|
|
283
|
+
message.messageId !== data.attempt_id) {
|
|
284
|
+
this.log("refused", "wrong_kind");
|
|
285
|
+
return rpc(id, { error: { code: -32602, message: "Only a Podster nudge is taken here." } });
|
|
286
|
+
}
|
|
287
|
+
// The attempt's ID ties this line to Podster's own audit of the nudge (R-18).
|
|
288
|
+
const ids = { workspace: data.workspace_id, task: data.task_id, attempt_id: data.attempt_id };
|
|
289
|
+
const taken = { result: { message: { messageId: crypto.randomUUID(), role: "ROLE_AGENT", parts: [{ text: "Nudged. Runner checks its inbox." }] } } };
|
|
290
|
+
if (this.seen.has(`attempt:${data.attempt_id}`)) {
|
|
291
|
+
// Podster's retry of an attempt already taken: answered, so it stops retrying, and nothing more is rung. Not
|
|
292
|
+
// remembered: answering it again is harmless, and remembering it could fill the memory.
|
|
293
|
+
this.log("duplicate", undefined, ids);
|
|
294
|
+
return rpc(id, taken);
|
|
295
|
+
}
|
|
296
|
+
if (!this.room(2))
|
|
297
|
+
return this.refuse(503, "memory_full");
|
|
298
|
+
this.remember(`sig:${digest}`, `attempt:${data.attempt_id}`);
|
|
299
|
+
this.log("nudged", undefined, ids);
|
|
300
|
+
this.deps.bell.ring();
|
|
301
|
+
return rpc(id, taken);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Registers `url` as this agent's nudge endpoint, keeping any registration Podster and Runner already share. The same URL
|
|
306
|
+
* set again keeps its version and secret in Podster, and its proof and the owner's opt-in with them. Only if Runner holds
|
|
307
|
+
* no secret for the version Podster answers does it ask for a new one (`rotate_secret`), which needs proof and the
|
|
308
|
+
* owner's opt-in again. A new secret goes to the key store before anything else uses it.
|
|
309
|
+
*/
|
|
310
|
+
export async function registerNudge(calls, storage, url, log) {
|
|
311
|
+
const held = await storage.getNudge();
|
|
312
|
+
const read = async (args) => {
|
|
313
|
+
const answer = await calls.request("set_a2a_endpoint", args);
|
|
314
|
+
if (!isObject(answer) || answer.url !== url || answer.delivery !== "nudge" || !Number.isSafeInteger(answer.version))
|
|
315
|
+
throw new Error("bad_answer");
|
|
316
|
+
return answer;
|
|
317
|
+
};
|
|
318
|
+
let answer = await read({ url, delivery: "nudge" });
|
|
319
|
+
let secret = typeof answer.signing_secret === "string" && answer.signing_secret.length > 0 ? answer.signing_secret : null;
|
|
320
|
+
const same = held !== null && held.url === url && held.agent_id === calls.agentId && held.version === answer.version;
|
|
321
|
+
if (secret === null && !same) {
|
|
322
|
+
answer = await read({ url, delivery: "nudge", rotate_secret: true });
|
|
323
|
+
secret = typeof answer.signing_secret === "string" && answer.signing_secret.length > 0 ? answer.signing_secret : null;
|
|
324
|
+
if (secret === null)
|
|
325
|
+
throw new Error("bad_answer");
|
|
326
|
+
}
|
|
327
|
+
const registration = secret === null ? held : { url, agent_id: calls.agentId, version: answer.version, secret };
|
|
328
|
+
if (secret !== null)
|
|
329
|
+
await storage.setNudge(registration);
|
|
330
|
+
log("nudge.register", secret === null ? "kept" : "new_secret", { agent: calls.agentId, count: registration.version });
|
|
331
|
+
return { registration, control: answer.control === "verified" ? "verified" : "unverified", nudge_on: answer.nudge_on === true };
|
|
332
|
+
}
|
|
333
|
+
/** Asks Podster to challenge the endpoint, which must already be listening. Answers whether it proved control. */
|
|
334
|
+
export async function verifyNudge(calls, log) {
|
|
335
|
+
const answer = await calls.request("verify_a2a_endpoint");
|
|
336
|
+
const verified = isObject(answer) && answer.verified === true;
|
|
337
|
+
const reason = isObject(answer) && typeof answer.reason === "string" ? answer.reason : undefined;
|
|
338
|
+
log("nudge.verify", verified ? "verified" : "not_verified", { agent: calls.agentId, ...(reason ? { code: reason } : {}) });
|
|
339
|
+
return { verified, nudge_on: isObject(answer) && answer.nudge_on === true };
|
|
340
|
+
}
|
package/build/core/poller.js
CHANGED
|
@@ -15,6 +15,10 @@
|
|
|
15
15
|
* registers or resumes by itself. Its next step is the owner's: pair again from Podster.
|
|
16
16
|
*
|
|
17
17
|
* No CLI or task process starts here; that is later steps' (M7 steps 7 to 9).
|
|
18
|
+
*
|
|
19
|
+
* **The nudge (M7 step 14)**, only when the owner switches it on: `run` passes a `NudgeBell`, which a verified nudge
|
|
20
|
+
* rings to end the wait after a good poll early (never sooner than `NUDGE_GAP_MS` after it). The next poll is this same
|
|
21
|
+
* one. Polling stays on, and a missed nudge loses nothing.
|
|
18
22
|
*/
|
|
19
23
|
import { Connection } from "./connection.js";
|
|
20
24
|
import { GatewayError, meaningOf } from "./errors.js";
|
|
@@ -40,6 +44,10 @@ const TASK_PAGES = 40;
|
|
|
40
44
|
/** Thrown when the agent's authority has ended: the caller stops what it's doing, then calls `endAuthority`. */
|
|
41
45
|
export class AuthorityEnded extends Error {
|
|
42
46
|
}
|
|
47
|
+
/** The wait after a poll: shortened by a nudge only after a good poll, so a nudge never cuts a backoff or a Retry-After. */
|
|
48
|
+
export function waitForNext(clock, ms, result, signal, bell) {
|
|
49
|
+
return bell && result === "ok" ? bell.idle(clock, ms, signal) : clock.idle(ms, signal);
|
|
50
|
+
}
|
|
43
51
|
export const clampInterval = (ms) => Math.min(Math.max(Math.round(ms), MIN_INTERVAL_MS), MAX_INTERVAL_MS);
|
|
44
52
|
export const clampGrace = (ms) => Math.min(Math.max(Math.round(ms), MIN_GRACE_MS), MAX_GRACE_MS);
|
|
45
53
|
export class Poller {
|
|
@@ -228,8 +236,8 @@ export class Poller {
|
|
|
228
236
|
this.retryAfterMs = null;
|
|
229
237
|
return Math.max(backoff, asked ?? 0);
|
|
230
238
|
}
|
|
231
|
-
/** Polls until the owner stops it (`signal`) or the agent's authority ends. */
|
|
232
|
-
async run(signal) {
|
|
239
|
+
/** Polls until the owner stops it (`signal`) or the agent's authority ends. A nudge's `bell` may end a wait early. */
|
|
240
|
+
async run(signal, bell) {
|
|
233
241
|
let toldOffline = false;
|
|
234
242
|
while (!signal.aborted) {
|
|
235
243
|
const result = await this.pollOnce();
|
|
@@ -241,7 +249,7 @@ export class Poller {
|
|
|
241
249
|
toldOffline = true;
|
|
242
250
|
this.deps.log("runner.offline", "grace_passed", { agent: this.state.agent_id ?? undefined });
|
|
243
251
|
}
|
|
244
|
-
await this.deps.clock
|
|
252
|
+
await waitForNext(this.deps.clock, this.nextWaitMs(result), result, signal, bell);
|
|
245
253
|
}
|
|
246
254
|
return "stopped";
|
|
247
255
|
}
|
package/build/core/runtime.js
CHANGED
|
@@ -19,7 +19,7 @@ const NAME = /^[a-z][a-z0-9_.]{0,63}$/;
|
|
|
19
19
|
const ID_VALUE = /^[A-Za-z0-9_-]{1,128}$/;
|
|
20
20
|
const CODE_VALUE = /^[a-z][a-z0-9_]{0,63}$/;
|
|
21
21
|
const NUMBER_FIELDS = new Set(["seq", "count", "status"]);
|
|
22
|
-
const ID_FIELDS = new Set(["agent", "plugin_agent", "workspace", "task"]);
|
|
22
|
+
const ID_FIELDS = new Set(["agent", "plugin_agent", "workspace", "task", "attempt_id"]);
|
|
23
23
|
/** The line a logger writes, with everything that isn't allowed dropped. */
|
|
24
24
|
export function logLine(at, outcome, fields = {}) {
|
|
25
25
|
const line = { at: NAME.test(at) ? at : "runner", outcome: NAME.test(outcome) ? outcome : "unknown" };
|
package/build/core/storage.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isRegistration } from "./nudge.js";
|
|
1
2
|
import { thumbprintOf } from "./thumbprint.js";
|
|
2
3
|
/** For tests only: secrets in memory. */
|
|
3
4
|
export class MemorySecretStore {
|
|
@@ -13,6 +14,7 @@ export class MemorySecretStore {
|
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
16
|
const HOST = "host";
|
|
17
|
+
const NUDGE = "nudge";
|
|
16
18
|
const AGENTS = "agents";
|
|
17
19
|
const agentName = (id) => `agent:${id}`;
|
|
18
20
|
export class RunnerStorage {
|
|
@@ -86,8 +88,26 @@ export class RunnerStorage {
|
|
|
86
88
|
const found = await Promise.all(ids.map((id) => this.getAgentConnection(id)));
|
|
87
89
|
return found.filter((connection) => connection !== null);
|
|
88
90
|
}
|
|
89
|
-
/**
|
|
91
|
+
/** The nudge endpoint's registration, or null. Anything stored that isn't one reads as an error, never as none. */
|
|
92
|
+
async getNudge() {
|
|
93
|
+
const value = await this.read(NUDGE);
|
|
94
|
+
if (value === null)
|
|
95
|
+
return null;
|
|
96
|
+
if (!isRegistration(value))
|
|
97
|
+
throw new Error("Runner's key store holds something it can't read. Run `forget`, then pair again.");
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
async setNudge(registration) {
|
|
101
|
+
if (!isRegistration(registration))
|
|
102
|
+
throw new Error("not a nudge registration");
|
|
103
|
+
await this.secrets.set(NUDGE, JSON.stringify({ url: registration.url, agent_id: registration.agent_id, version: registration.version, secret: registration.secret }));
|
|
104
|
+
}
|
|
105
|
+
async deleteNudge() {
|
|
106
|
+
await this.secrets.delete(NUDGE);
|
|
107
|
+
}
|
|
108
|
+
/** Deletes every key Runner holds: the host key, each agent's, and the nudge endpoint's secret. */
|
|
90
109
|
async deleteAll() {
|
|
110
|
+
await this.secrets.delete(NUDGE);
|
|
91
111
|
for (const id of (await this.read(AGENTS)) ?? [])
|
|
92
112
|
await this.secrets.delete(agentName(id));
|
|
93
113
|
await this.secrets.delete(AGENTS);
|
package/build/node/cli.js
CHANGED
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
* Poll the inbox until stopped (Ctrl-C). With a provider key
|
|
11
11
|
* file, run tasks through the Claude Agent SDK; without one,
|
|
12
12
|
* record offers and accept none.
|
|
13
|
+
* [--nudge-listen <port> --nudge-url <https://…>]
|
|
14
|
+
* Also take Podster's nudges (M7 step 14), off unless both are
|
|
15
|
+
* given: listen on loopback at the port, register the public HTTPS
|
|
16
|
+
* address the owner's proxy or tunnel forwards to, and prove control.
|
|
13
17
|
* stop Stop the Runner running from this home, and any run, at once.
|
|
14
18
|
* status Show what this Runner's record says, by IDs.
|
|
15
19
|
* forget Delete this Runner's keys and record. The agent stays connected in Podster.
|
|
@@ -37,6 +41,7 @@ import { chooseKeyStore } from "./key-store.js";
|
|
|
37
41
|
import { acquireLock, LockRefused } from "./lock.js";
|
|
38
42
|
import { clearStop, requestStop, watchStop } from "./local-stop.js";
|
|
39
43
|
import { claudeEngine } from "./claude-engine.js";
|
|
44
|
+
import { alongsidePolling, nudgeOptions, openNudge } from "./nudge-command.js";
|
|
40
45
|
import { processTerminal, runInbox } from "./run-command.js";
|
|
41
46
|
const USAGE = `Podster Runner (M7: pairing, the inbox loop, and tasks through the Claude Agent SDK)
|
|
42
47
|
|
|
@@ -47,6 +52,14 @@ const USAGE = `Podster Runner (M7: pairing, the inbox loop, and tasks through th
|
|
|
47
52
|
Poll the inbox until you stop it (Ctrl-C). With your Anthropic
|
|
48
53
|
API key in a file only you can read, run tasks the run policy
|
|
49
54
|
allows; without one, record offers and accept none.
|
|
55
|
+
[--nudge-listen <port> --nudge-url <https://…>]
|
|
56
|
+
Off unless you give both. Also take Podster's nudges, so offers
|
|
57
|
+
are picked up sooner: Runner listens on 127.0.0.1 at the port,
|
|
58
|
+
registers your public HTTPS address (a reverse proxy or tunnel you
|
|
59
|
+
run, which ends TLS and forwards to that port with the path
|
|
60
|
+
unchanged), and proves it holds the address's secret. An admin of
|
|
61
|
+
the agent's organization then switches the nudge on in Podster.
|
|
62
|
+
A nudge only makes Runner poll sooner. Polling stays on.
|
|
50
63
|
stop Stop the Runner running from this home at once, and any run.
|
|
51
64
|
It polls nothing until you start it again.
|
|
52
65
|
status Show this Runner's record, by IDs.
|
|
@@ -186,6 +199,8 @@ async function locked(command, args, home, state, nonce) {
|
|
|
186
199
|
const interval = option(args, "interval");
|
|
187
200
|
const grace = option(args, "grace");
|
|
188
201
|
const keyFile = option(args, "provider-key-file");
|
|
202
|
+
// The nudge's options are checked before anything is touched; without both, it stays off.
|
|
203
|
+
const nudging = nudgeOptions(args);
|
|
189
204
|
// The run engine is built, and what a previous process left is reaped, before the inbox is touched (M4 on #459).
|
|
190
205
|
let engine = null;
|
|
191
206
|
if (keyFile !== undefined) {
|
|
@@ -208,12 +223,21 @@ async function locked(command, args, home, state, nonce) {
|
|
|
208
223
|
// The owner's `stop`, from another terminal: a request naming this Runner's lock by its nonce, so one left for an
|
|
209
224
|
// earlier Runner can't stop this one (L2 on #458).
|
|
210
225
|
const unwatch = watchStop(home, nonce, () => stop.abort());
|
|
211
|
-
say(`Polling every ${poller.intervalMs / 1000} seconds, outbound only. Stop with Ctrl-C or \`stop\`.`);
|
|
226
|
+
say(nudging === null ? `Polling every ${poller.intervalMs / 1000} seconds, outbound only. Stop with Ctrl-C or \`stop\`.` : `Polling every ${poller.intervalMs / 1000} seconds. Stop with Ctrl-C or \`stop\`.`);
|
|
212
227
|
if (engine === null)
|
|
213
228
|
say("No provider key file was given, so this Runner records offers and accepts none.");
|
|
214
229
|
let outcome;
|
|
215
230
|
try {
|
|
216
|
-
|
|
231
|
+
// Polling starts first; the nudge's setup, if the owner asked for it, runs beside it (the ruling on #473).
|
|
232
|
+
outcome = await alongsidePolling(async (bell) => {
|
|
233
|
+
try {
|
|
234
|
+
return await runInbox({ home, poller, clock: systemClock, log, signal: stop.signal, terminal: processTerminal(), engine, ...(bell ? { bell } : {}) });
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
// Shutdown closes the nudge endpoint at once (R-15), even while its setup is still waiting on Podster.
|
|
238
|
+
stop.abort();
|
|
239
|
+
}
|
|
240
|
+
}, nudging === null ? null : (bell, ended) => openNudge({ ...nudging, poller, secrets: keys.store, clock: systemClock, log, say, signal: AbortSignal.any([stop.signal, ended]), bell }));
|
|
217
241
|
}
|
|
218
242
|
finally {
|
|
219
243
|
unwatch();
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `run --nudge-listen <port> --nudge-url <https://…>`: the inbound nudge, switched on by the owner for this run (M7
|
|
3
|
+
* step 14). Without both options nothing here runs, and `run` is the polling it always was.
|
|
4
|
+
*
|
|
5
|
+
* In order: the loopback listener starts (`nudge-listener.ts`), so Podster's challenge can reach it; the endpoint is
|
|
6
|
+
* registered with `delivery: "nudge"` (`registerNudge`), its signing secret kept in the key store; and, unless Podster
|
|
7
|
+
* already counts it as verified, Runner asks Podster to challenge it and answers with the proof (`verifyNudge`). Then
|
|
8
|
+
* the owner, or another admin of the agent's organization, switches the nudge on in Podster for the version shown here.
|
|
9
|
+
*
|
|
10
|
+
* Polling starts first and never waits for any of this (`alongsidePolling`, the architect's ruling on #473): setup runs
|
|
11
|
+
* beside the poll loop. Once the listener is up, the bell can ring for a request signed under a registration Runner
|
|
12
|
+
* already holds from an earlier run, before this run's registration and challenge finish; a ring only brings the next
|
|
13
|
+
* poll forward. When the run ends, or the owner stops it, setup stops where it is: no registration or challenge is
|
|
14
|
+
* asked for after that, and the listener closes at once.
|
|
15
|
+
*
|
|
16
|
+
* Anything that fails here is said in one line and leaves polling as it was: the nudge is only ever a faster way to the
|
|
17
|
+
* same poll. What the owner reads goes to standard output; log lines carry IDs and codes only, never the URL or secret.
|
|
18
|
+
*/
|
|
19
|
+
import { GatewayError } from "../core/errors.js";
|
|
20
|
+
import { checkNudgeUrl, NudgeBell, NudgeEndpoint, registerNudge, verifyNudge } from "../core/nudge.js";
|
|
21
|
+
import { RunnerStorage } from "../core/storage.js";
|
|
22
|
+
import { parseListen, startNudgeListener } from "./nudge-listener.js";
|
|
23
|
+
function option(args, name) {
|
|
24
|
+
const at = args.indexOf(`--${name}`);
|
|
25
|
+
if (at < 0)
|
|
26
|
+
return undefined;
|
|
27
|
+
const value = args[at + 1];
|
|
28
|
+
if (value === undefined || value.startsWith("--"))
|
|
29
|
+
throw new Error(`--${name} needs a value`);
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
/** The owner's two options, checked before anything is touched. Null when neither is given: the nudge stays off. */
|
|
33
|
+
export function nudgeOptions(args) {
|
|
34
|
+
const listen = option(args, "nudge-listen");
|
|
35
|
+
const url = option(args, "nudge-url");
|
|
36
|
+
if (listen === undefined && url === undefined)
|
|
37
|
+
return null;
|
|
38
|
+
if (listen === undefined)
|
|
39
|
+
throw new Error("--nudge-url needs --nudge-listen, the loopback port your proxy or tunnel forwards to.");
|
|
40
|
+
if (url === undefined)
|
|
41
|
+
throw new Error("--nudge-listen needs --nudge-url, the public HTTPS address you control that forwards to it.");
|
|
42
|
+
const checked = checkNudgeUrl(url);
|
|
43
|
+
if (checked === null)
|
|
44
|
+
throw new Error("--nudge-url must be https:// at a DNS name, on the default port, with no user, query or fragment.");
|
|
45
|
+
return { listen: parseListen(listen), url: checked };
|
|
46
|
+
}
|
|
47
|
+
const codeOf = (error) => (error instanceof GatewayError ? error.code : error instanceof Error && /^[a-z_]{1,64}$/.test(error.message) ? error.message : "failed");
|
|
48
|
+
export async function openNudge(deps) {
|
|
49
|
+
const storage = new RunnerStorage(deps.secrets);
|
|
50
|
+
const bell = deps.bell ?? new NudgeBell();
|
|
51
|
+
let current = null;
|
|
52
|
+
try {
|
|
53
|
+
const held = await storage.getNudge();
|
|
54
|
+
if (held && held.url === deps.url && held.agent_id === deps.poller.agentId)
|
|
55
|
+
current = held;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
current = null;
|
|
59
|
+
}
|
|
60
|
+
const endpoint = new NudgeEndpoint({ registration: () => current, path: new URL(deps.url).pathname, clock: deps.clock, log: deps.log, bell });
|
|
61
|
+
let listener;
|
|
62
|
+
try {
|
|
63
|
+
listener = await startNudgeListener({ port: deps.listen.port, endpoint, signal: deps.signal, log: deps.log });
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
deps.log("nudge.listen", "failed", { agent: deps.poller.agentId });
|
|
67
|
+
deps.say("Runner couldn't listen on that loopback port, so nudges are off for this run. Polling goes on as before.");
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
deps.onListening?.(listener.port);
|
|
71
|
+
deps.say(`Listening for nudges on 127.0.0.1:${listener.port}, for ${deps.url} (your proxy or tunnel forwards to it). Polling stays on.`);
|
|
72
|
+
// Stopped or ended before setup is through: nothing more is asked of Podster.
|
|
73
|
+
const ended = async () => {
|
|
74
|
+
if (!deps.signal.aborted)
|
|
75
|
+
return false;
|
|
76
|
+
await listener.close();
|
|
77
|
+
return true;
|
|
78
|
+
};
|
|
79
|
+
try {
|
|
80
|
+
if (await ended())
|
|
81
|
+
return null;
|
|
82
|
+
const registered = await registerNudge(deps.poller, storage, deps.url, deps.log);
|
|
83
|
+
current = registered.registration;
|
|
84
|
+
if (await ended())
|
|
85
|
+
return null;
|
|
86
|
+
let { control, nudge_on: on } = registered;
|
|
87
|
+
if (control !== "verified") {
|
|
88
|
+
const verified = await verifyNudge(deps.poller, deps.log);
|
|
89
|
+
control = verified.verified ? "verified" : "unverified";
|
|
90
|
+
on = verified.nudge_on;
|
|
91
|
+
}
|
|
92
|
+
const version = registered.registration.version;
|
|
93
|
+
if (control !== "verified")
|
|
94
|
+
deps.say(`Runner couldn't prove it controls ${deps.url} (version ${version}): Podster's challenge didn't come back through your proxy or tunnel. Nudges stay off; polling goes on.`);
|
|
95
|
+
else if (!on)
|
|
96
|
+
deps.say(`Nudge endpoint version ${version} is registered and verified. It stays off until an admin of this agent's organization switches the nudge on in Podster for version ${version}.`);
|
|
97
|
+
else
|
|
98
|
+
deps.say(`Nudges are on for version ${version}. Polling stays on as well.`);
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
deps.log("nudge.register", "failed", { agent: deps.poller.agentId, code: codeOf(error) });
|
|
102
|
+
deps.say("Runner couldn't register its nudge endpoint with Podster, so nudges are off for this run. Polling goes on as before.");
|
|
103
|
+
await listener.close();
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return { bell, close: () => listener.close() };
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Runs the poll loop and, beside it, the nudge's setup. The loop starts first, with the bell already in hand, so its
|
|
110
|
+
* first poll never waits for registration or the challenge. Once setup's listener is up, the bell can ring for a request
|
|
111
|
+
* signed under a registration Runner already holds, which only brings the next poll forward. Setup failing (it says its
|
|
112
|
+
* own line) leaves the loop as it is.
|
|
113
|
+
*
|
|
114
|
+
* When the loop ends, whatever its outcome (a stop, Ctrl-C or a revoked agent), `ended` is aborted: setup's listener,
|
|
115
|
+
* which listens on it, closes at once, and setup asks nothing more of Podster. The run never waits for setup to finish.
|
|
116
|
+
*/
|
|
117
|
+
export async function alongsidePolling(loop, setUp) {
|
|
118
|
+
if (setUp === null)
|
|
119
|
+
return loop(undefined);
|
|
120
|
+
const bell = new NudgeBell();
|
|
121
|
+
const ended = new AbortController();
|
|
122
|
+
const running = loop(bell);
|
|
123
|
+
void Promise.resolve()
|
|
124
|
+
.then(() => setUp(bell, ended.signal))
|
|
125
|
+
.catch(() => null);
|
|
126
|
+
try {
|
|
127
|
+
return await running;
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
ended.abort();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The inbound nudge's listener (M7 step 14; S-13), the second file in Runner's source that may open a listening socket
|
|
3
|
+
* (the scoped source test in `test/machine.test.ts`). It exists only while `run` has `--nudge-listen` and `--nudge-url`.
|
|
4
|
+
*
|
|
5
|
+
* - **Loopback only:** bound to the literal `"127.0.0.1"`, at the port the owner names. Podster reaches it only at the
|
|
6
|
+
* owner's public HTTPS address (decision 236), through a reverse proxy or tunnel the owner runs on this machine, which
|
|
7
|
+
* terminates TLS and forwards to this port with the path unchanged. Runner opens nothing to the network itself.
|
|
8
|
+
* - **Bounded:** headers within 5 s and the whole request within 10 s, small headers, few connections, and every answer
|
|
9
|
+
* closes its connection. The body is read only when `NudgeEndpoint` asks for it, after its rate check, one chunk at a
|
|
10
|
+
* time straight from the request, with each request's own buffer held to `MAX_NUDGE_BODY_BYTES`. The moment the count
|
|
11
|
+
* passes that, the socket is destroyed, so no more is buffered whatever the client sends.
|
|
12
|
+
* - **A server error after it starts** (EMFILE, say) is logged as a code and closes the listener. It never reaches the
|
|
13
|
+
* process's uncaught-exception handler, so polling goes on.
|
|
14
|
+
* - **Nothing but the endpoint:** each request goes to `NudgeEndpoint.handle` as a Web `Request`, and its answer back.
|
|
15
|
+
* It can only ring the poll loop's bell.
|
|
16
|
+
* - **Closed on stop:** the run's signal, or `close()`, closes the server and every connection.
|
|
17
|
+
*
|
|
18
|
+
* Logs carry codes only, never a body, a header or an address.
|
|
19
|
+
*/
|
|
20
|
+
import { createServer } from "node:http";
|
|
21
|
+
import { MAX_NUDGE_BODY_BYTES } from "../core/nudge.js";
|
|
22
|
+
/** Connections served at once. **Proposed.** */
|
|
23
|
+
export const MAX_CONNECTIONS = 16;
|
|
24
|
+
/**
|
|
25
|
+
* The owner's `--nudge-listen`: a port, or `127.0.0.1:<port>`. Any other address is refused: Runner listens only on
|
|
26
|
+
* loopback, and the owner's proxy or tunnel is what faces the network.
|
|
27
|
+
*/
|
|
28
|
+
export function parseListen(value) {
|
|
29
|
+
const match = /^(?:127\.0\.0\.1:)?(\d{1,5})$/.exec(value);
|
|
30
|
+
const port = match ? Number(match[1]) : NaN;
|
|
31
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
32
|
+
throw new Error("--nudge-listen takes a port, or 127.0.0.1:<port>. Runner listens only on loopback; your proxy or tunnel faces the network.");
|
|
33
|
+
return { port };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The request's body as a stream that reads `req` only when pulled, one chunk at a time, counting bytes, and destroys the
|
|
37
|
+
* socket the moment the count passes `max`. Not `Readable.toWeb`, whose own buffering isn't bounded by this count.
|
|
38
|
+
*/
|
|
39
|
+
function boundedBody(req, max) {
|
|
40
|
+
const chunks = req[Symbol.asyncIterator]();
|
|
41
|
+
let total = 0;
|
|
42
|
+
return new ReadableStream({
|
|
43
|
+
async pull(controller) {
|
|
44
|
+
try {
|
|
45
|
+
const { done, value } = await chunks.next();
|
|
46
|
+
if (done)
|
|
47
|
+
return controller.close();
|
|
48
|
+
total += value.byteLength;
|
|
49
|
+
if (total > max) {
|
|
50
|
+
req.socket.destroy();
|
|
51
|
+
return controller.error(new Error("too_large"));
|
|
52
|
+
}
|
|
53
|
+
controller.enqueue(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
controller.error(new Error("unreadable"));
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
cancel() {
|
|
60
|
+
req.socket.destroy();
|
|
61
|
+
},
|
|
62
|
+
}, { highWaterMark: 0 });
|
|
63
|
+
}
|
|
64
|
+
function requestOf(req, port) {
|
|
65
|
+
const headers = new Headers();
|
|
66
|
+
for (const [name, value] of Object.entries(req.headers)) {
|
|
67
|
+
if (typeof value === "string")
|
|
68
|
+
headers.set(name, value);
|
|
69
|
+
else if (Array.isArray(value))
|
|
70
|
+
headers.set(name, value.join(", "));
|
|
71
|
+
}
|
|
72
|
+
const method = req.method ?? "GET";
|
|
73
|
+
const target = (req.url ?? "/").startsWith("/") ? (req.url ?? "/") : "/";
|
|
74
|
+
const body = method === "GET" || method === "HEAD" ? undefined : boundedBody(req, MAX_NUDGE_BODY_BYTES);
|
|
75
|
+
return new Request(`http://127.0.0.1:${port}${target}`, { method, headers, body, duplex: "half" });
|
|
76
|
+
}
|
|
77
|
+
export async function startNudgeListener(deps) {
|
|
78
|
+
let port = 0;
|
|
79
|
+
let closing = null;
|
|
80
|
+
let opened = () => undefined;
|
|
81
|
+
const closed = new Promise((resolve) => (opened = resolve));
|
|
82
|
+
const server = createServer({ highWaterMark: MAX_NUDGE_BODY_BYTES, headersTimeout: 5_000, requestTimeout: 10_000, connectionsCheckingInterval: 1_000, maxHeaderSize: 8 * 1024, keepAliveTimeout: 1_000 }, (req, res) => {
|
|
83
|
+
const finish = (status, headers, body) => {
|
|
84
|
+
if (res.headersSent)
|
|
85
|
+
return void res.destroy();
|
|
86
|
+
res.writeHead(status, { ...headers, connection: "close" });
|
|
87
|
+
res.end(body);
|
|
88
|
+
};
|
|
89
|
+
if (closing)
|
|
90
|
+
return finish(503, {}, new Uint8Array());
|
|
91
|
+
let request;
|
|
92
|
+
try {
|
|
93
|
+
request = requestOf(req, port);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return finish(400, {}, new Uint8Array());
|
|
97
|
+
}
|
|
98
|
+
deps.endpoint.handle(request).then(async (answer) => {
|
|
99
|
+
const headers = {};
|
|
100
|
+
const type = answer.headers.get("content-type");
|
|
101
|
+
if (type)
|
|
102
|
+
headers["content-type"] = type;
|
|
103
|
+
// What the endpoint didn't read is never read: every answer closes its connection.
|
|
104
|
+
finish(answer.status, headers, new Uint8Array(await answer.arrayBuffer()));
|
|
105
|
+
}, () => finish(500, {}, new Uint8Array()));
|
|
106
|
+
});
|
|
107
|
+
server.maxConnections = MAX_CONNECTIONS;
|
|
108
|
+
await new Promise((resolve, reject) => {
|
|
109
|
+
server.once("error", reject);
|
|
110
|
+
server.listen(deps.port, "127.0.0.1", () => resolve());
|
|
111
|
+
});
|
|
112
|
+
const address = server.address();
|
|
113
|
+
if (address === null || typeof address === "string" || address.address !== "127.0.0.1") {
|
|
114
|
+
server.close();
|
|
115
|
+
throw new Error("listener_not_loopback");
|
|
116
|
+
}
|
|
117
|
+
port = address.port;
|
|
118
|
+
// From here on, an error is the listener's end, never the process's (review 2 on #473).
|
|
119
|
+
server.on("error", (error) => {
|
|
120
|
+
const code = typeof error?.code === "string" && /^E[A-Z]{1,30}$/.test(error.code) ? error.code.toLowerCase() : "unknown";
|
|
121
|
+
deps.log?.("nudge.listen", "server_error", { code });
|
|
122
|
+
void close();
|
|
123
|
+
});
|
|
124
|
+
const close = () => {
|
|
125
|
+
closing ??= new Promise((resolve) => {
|
|
126
|
+
server.close(() => resolve());
|
|
127
|
+
server.closeAllConnections();
|
|
128
|
+
}).then(opened);
|
|
129
|
+
return closing;
|
|
130
|
+
};
|
|
131
|
+
if (deps.signal.aborted)
|
|
132
|
+
await close();
|
|
133
|
+
else
|
|
134
|
+
deps.signal.addEventListener("abort", () => void close(), { once: true });
|
|
135
|
+
deps.onServer?.(server);
|
|
136
|
+
return { port, close, closed };
|
|
137
|
+
}
|
|
@@ -23,7 +23,7 @@ const progressLog = (log) => (event) => log("run.progress", event.kind, { worksp
|
|
|
23
23
|
export async function runInbox(deps) {
|
|
24
24
|
const engine = deps.engine;
|
|
25
25
|
if (engine === null || !Object.values(engine.drivers).some((driver) => driver !== undefined))
|
|
26
|
-
return deps.poller.run(deps.signal);
|
|
26
|
+
return deps.poller.run(deps.signal, deps.bell);
|
|
27
27
|
const adapter = new SupervisedAdapter({ ...engine, clock: deps.clock, log: deps.log, progress: progressLog(deps.log) });
|
|
28
28
|
const repository = deps.repository !== undefined ? deps.repository : await hostRepositoryPath(deps.home, (capability, args) => deps.poller.request(capability, args), deps.clock, deps.log);
|
|
29
29
|
const lifecycle = await Lifecycle.open({
|
|
@@ -37,7 +37,7 @@ export async function runInbox(deps) {
|
|
|
37
37
|
clock: deps.clock,
|
|
38
38
|
log: deps.log,
|
|
39
39
|
});
|
|
40
|
-
return runWithLifecycle(deps.poller, lifecycle, deps.clock, deps.signal);
|
|
40
|
+
return runWithLifecycle(deps.poller, lifecycle, deps.clock, deps.signal, deps.bell);
|
|
41
41
|
}
|
|
42
42
|
/** This process's terminal. A question is asked only when both standard input and standard output are terminals. */
|
|
43
43
|
export function processTerminal() {
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "podster",
|
|
3
|
-
"version": "0.0.1-test.
|
|
3
|
+
"version": "0.0.1-test.2",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "podster",
|
|
9
|
-
"version": "0.0.1-test.
|
|
9
|
+
"version": "0.0.1-test.2",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@anthropic-ai/claude-agent-sdk": "0.3.327",
|