@jacoblee93/agent-overwatch 0.0.0-stage → 0.1.0
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 +45 -0
- package/README.md +33 -2
- package/config.example.json +44 -0
- package/dist/acp/connection.js +175 -0
- package/dist/acp/types.js +23 -0
- package/dist/acp-session.js +212 -0
- package/dist/agents.js +111 -0
- package/dist/approvals.js +70 -0
- package/dist/auth.js +71 -0
- package/dist/bridge.js +103 -0
- package/dist/cli.js +612 -0
- package/dist/config.js +171 -0
- package/dist/daemon.js +191 -0
- package/dist/devices.js +82 -0
- package/dist/dirs.js +107 -0
- package/dist/errors.js +8 -0
- package/dist/events.js +38 -0
- package/dist/github.js +244 -0
- package/dist/http.js +63 -0
- package/dist/linear.js +105 -0
- package/dist/mock-agent.js +242 -0
- package/dist/notify.js +32 -0
- package/dist/pairing.js +166 -0
- package/dist/pending.js +108 -0
- package/dist/pki.js +94 -0
- package/dist/push.js +287 -0
- package/dist/qr.js +304 -0
- package/dist/questions.js +117 -0
- package/dist/relay.js +80 -0
- package/dist/server.js +256 -0
- package/dist/servers.js +92 -0
- package/dist/sessions.js +691 -0
- package/dist/summaries.js +70 -0
- package/dist/tailscale.js +51 -0
- package/dist/thread-store.js +74 -0
- package/dist/transcript.js +167 -0
- package/dist/version.js +4 -0
- package/dist/worktrees.js +157 -0
- package/dist/x509.js +144 -0
- package/package.json +63 -3
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export const DEFAULT_AUTO_ALLOW = { kinds: ["read", "search", "think"], commands: [] };
|
|
2
|
+
const SHELL_META = /[;&|$`<>\n\r()\\]/;
|
|
3
|
+
const normalize = (s) => s.trim().replace(/\s+/g, " ");
|
|
4
|
+
export function commandMatches(command, patterns) {
|
|
5
|
+
if (!command || SHELL_META.test(command))
|
|
6
|
+
return false;
|
|
7
|
+
const cmd = normalize(command);
|
|
8
|
+
return patterns.some((raw) => {
|
|
9
|
+
const p = normalize(raw);
|
|
10
|
+
if (!p)
|
|
11
|
+
return false;
|
|
12
|
+
if (p.endsWith("*")) {
|
|
13
|
+
const prefix = p.slice(0, -1);
|
|
14
|
+
// "git diff*" matches "git diff" and "git diff --stat", not "git difftool".
|
|
15
|
+
return prefix.endsWith(" ") ? cmd.startsWith(prefix) : cmd === prefix || cmd.startsWith(prefix + " ");
|
|
16
|
+
}
|
|
17
|
+
return cmd === p;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/** The shell command an execute tool call wants to run, if we can tell. */
|
|
21
|
+
export function commandOf(rawInput) {
|
|
22
|
+
const ri = rawInput;
|
|
23
|
+
const c = ri?.command ?? ri?.cmd;
|
|
24
|
+
if (typeof c === "string")
|
|
25
|
+
return c;
|
|
26
|
+
if (Array.isArray(c) && c.every((x) => typeof x === "string")) {
|
|
27
|
+
// ["bash", "-lc", "npm test"] → the script; otherwise the argv joined.
|
|
28
|
+
if (c.length === 3 && /(^|\/)(ba|z)?sh$/.test(c[0]) && /^-l?c$/.test(c[1]))
|
|
29
|
+
return c[2];
|
|
30
|
+
return c.join(" ");
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
/** Why a tool call is auto-approved, or undefined if it needs you. */
|
|
35
|
+
export function autoAllowReason(config, kind, rawInput) {
|
|
36
|
+
const rules = config.approvals?.autoAllow ?? DEFAULT_AUTO_ALLOW;
|
|
37
|
+
if (kind && (rules.kinds ?? []).includes(kind))
|
|
38
|
+
return `${kind} tools are auto-approved`;
|
|
39
|
+
const command = commandOf(rawInput);
|
|
40
|
+
if (command && (kind === "execute" || kind === undefined) && commandMatches(command, rules.commands ?? []))
|
|
41
|
+
return "command is auto-approved";
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The command "Always allow" on the watch would add to `autoAllow.commands`, if
|
|
46
|
+
* this call can be remembered: a shell command (not an edit or fetch), short,
|
|
47
|
+
* with no shell metacharacters (so it matches exactly that command and nothing
|
|
48
|
+
* chained onto it), and not already allowed.
|
|
49
|
+
*/
|
|
50
|
+
export function rememberableCommand(config, kind, rawInput) {
|
|
51
|
+
if (kind !== "execute" && kind !== undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
const command = commandOf(rawInput);
|
|
54
|
+
if (!command || SHELL_META.test(command))
|
|
55
|
+
return undefined;
|
|
56
|
+
const cmd = normalize(command);
|
|
57
|
+
if (!cmd || cmd.length > 200 || cmd.includes("*"))
|
|
58
|
+
return undefined;
|
|
59
|
+
if (autoAllowReason(config, kind, rawInput))
|
|
60
|
+
return undefined;
|
|
61
|
+
return cmd;
|
|
62
|
+
}
|
|
63
|
+
/** `autoAllow` with `command` added (the defaults stay when there was no `approvals` section yet). */
|
|
64
|
+
export function withRememberedCommand(config, command) {
|
|
65
|
+
const current = config.approvals?.autoAllow ?? DEFAULT_AUTO_ALLOW;
|
|
66
|
+
const commands = [...(current.commands ?? [])];
|
|
67
|
+
if (!commands.includes(command))
|
|
68
|
+
commands.push(command);
|
|
69
|
+
return { ...config.approvals, autoAllow: { ...current, kinds: [...(current.kinds ?? [])], commands } };
|
|
70
|
+
}
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { overwatchHome } from "./config.js";
|
|
5
|
+
function digest(s) {
|
|
6
|
+
return createHash("sha256").update(s, "utf8").digest();
|
|
7
|
+
}
|
|
8
|
+
/** Constant-time comparison of a presented bearer token against the configured one. */
|
|
9
|
+
export function tokenMatches(presented, expected) {
|
|
10
|
+
if (!presented)
|
|
11
|
+
return false;
|
|
12
|
+
return timingSafeEqual(digest(presented), digest(expected));
|
|
13
|
+
}
|
|
14
|
+
export function bearerFrom(req) {
|
|
15
|
+
const h = req.headers.get("authorization");
|
|
16
|
+
if (!h)
|
|
17
|
+
return null;
|
|
18
|
+
const m = /^Bearer\s+(.+)$/i.exec(h.trim());
|
|
19
|
+
return m?.[1] ?? null;
|
|
20
|
+
}
|
|
21
|
+
// ---- Pairing codes -----------------------------------------------------------
|
|
22
|
+
// The watch makes up a 12-digit code and shows it; you type it into
|
|
23
|
+
// `overwatch pair`, which writes it, short-lived and single-use, to
|
|
24
|
+
// ~/.agent-overwatch/pairing.json (0600). The code never crosses the network:
|
|
25
|
+
// both sides prove they know it with HMACs bound to the TLS certificate the
|
|
26
|
+
// watch actually saw (see pairing.ts). 12 digits (~40 bits) keeps a
|
|
27
|
+
// man-in-the-middle from brute-forcing it within a pairing request's lifetime.
|
|
28
|
+
export const PAIRING_TTL_MS = 5 * 60 * 1000;
|
|
29
|
+
export const PAIRING_CODE_DIGITS = 12;
|
|
30
|
+
export const MAX_PAIRING_ATTEMPTS = 5;
|
|
31
|
+
function pairingPath() {
|
|
32
|
+
return join(overwatchHome(), "pairing.json");
|
|
33
|
+
}
|
|
34
|
+
/** Normalize what was typed ("1234 5678-9012") to 12 digits, or undefined if it isn't a code. */
|
|
35
|
+
export function parsePairingCode(typed) {
|
|
36
|
+
const digits = typed.replace(/[\s-]/g, "");
|
|
37
|
+
return new RegExp(`^\\d{${PAIRING_CODE_DIGITS}}$`).test(digits) ? digits : undefined;
|
|
38
|
+
}
|
|
39
|
+
/** Arm pairing with the code shown on the watch. */
|
|
40
|
+
export function setPairingCode(code) {
|
|
41
|
+
if (!parsePairingCode(code))
|
|
42
|
+
throw new Error(`a pairing code is ${PAIRING_CODE_DIGITS} digits`);
|
|
43
|
+
const file = { code, expiresAt: Date.now() + PAIRING_TTL_MS };
|
|
44
|
+
writeFileSync(pairingPath(), JSON.stringify(file), { mode: 0o600 });
|
|
45
|
+
return { code, expiresAt: new Date(file.expiresAt) };
|
|
46
|
+
}
|
|
47
|
+
export function formatPairingCode(code) {
|
|
48
|
+
return code.replace(/(\d{4})(?=\d)/g, "$1 ");
|
|
49
|
+
}
|
|
50
|
+
export function activePairingCode() {
|
|
51
|
+
const path = pairingPath();
|
|
52
|
+
if (!existsSync(path))
|
|
53
|
+
return { ok: false, reason: "no_code" };
|
|
54
|
+
try {
|
|
55
|
+
const file = JSON.parse(readFileSync(path, "utf8"));
|
|
56
|
+
if (Date.now() > file.expiresAt) {
|
|
57
|
+
rmSync(path, { force: true });
|
|
58
|
+
return { ok: false, reason: "expired" };
|
|
59
|
+
}
|
|
60
|
+
if (!/^\d+$/.test(file.code))
|
|
61
|
+
throw new Error("bad code");
|
|
62
|
+
return { ok: true, code: file.code };
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
rmSync(path, { force: true });
|
|
66
|
+
return { ok: false, reason: "no_code" };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export function consumePairingCode() {
|
|
70
|
+
rmSync(pairingPath(), { force: true });
|
|
71
|
+
}
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { watchFile } from "node:fs";
|
|
2
|
+
import { configPath, loadConfig, overwatchHome } from "./config.js";
|
|
3
|
+
import { DeviceRegistry } from "./devices.js";
|
|
4
|
+
import { ChangeFeed } from "./events.js";
|
|
5
|
+
import { makeNotifier } from "./notify.js";
|
|
6
|
+
import { GitHubSource } from "./github.js";
|
|
7
|
+
import { LinearSource } from "./linear.js";
|
|
8
|
+
import { makePushNotifier, PushRegistry } from "./push.js";
|
|
9
|
+
import { loadRelayIdentity, RelayClient, relayURL } from "./relay.js";
|
|
10
|
+
import { loadOrCreatePki } from "./pki.js";
|
|
11
|
+
import { startServers } from "./servers.js";
|
|
12
|
+
import { SessionManager } from "./sessions.js";
|
|
13
|
+
import { funnelURLs } from "./tailscale.js";
|
|
14
|
+
/** Runs the bridge in the foreground until SIGINT/SIGTERM. */
|
|
15
|
+
export async function runBridge() {
|
|
16
|
+
const log = (m) => console.log(`${new Date().toISOString()} ${m}`);
|
|
17
|
+
const { config, created } = loadConfig();
|
|
18
|
+
if (created)
|
|
19
|
+
log(`created ${configPath()} — edit it to add your agents and directories`);
|
|
20
|
+
// No publicURL configured? Use whatever Tailscale Funnel forwards to the mTLS port.
|
|
21
|
+
if (!config.secure.publicURL) {
|
|
22
|
+
const found = funnelURLs(config.secure.port, config.secure.pairPort).api;
|
|
23
|
+
if (found) {
|
|
24
|
+
config.secure.publicURL = found;
|
|
25
|
+
log(`public URL from Tailscale Funnel: ${found}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const publicHost = config.secure.publicURL ? [new URL(config.secure.publicURL).hostname] : [];
|
|
29
|
+
const pki = loadOrCreatePki(overwatchHome(), config.hostName, [...publicHost, ...(config.secure.publicHostnames ?? [])]);
|
|
30
|
+
const devices = new DeviceRegistry(overwatchHome());
|
|
31
|
+
const feed = new ChangeFeed();
|
|
32
|
+
const sessions = new SessionManager(config, feed, log);
|
|
33
|
+
const linear = new LinearSource();
|
|
34
|
+
const github = new GitHubSource(() => config.directories);
|
|
35
|
+
await github.checkAuth();
|
|
36
|
+
const ntfy = makeNotifier(config, log);
|
|
37
|
+
const pushRegistry = new PushRegistry(overwatchHome());
|
|
38
|
+
const relayAt = relayURL(config);
|
|
39
|
+
const relay = relayAt ? new RelayClient(relayAt, loadRelayIdentity(overwatchHome())) : undefined;
|
|
40
|
+
const push = makePushNotifier(config, pushRegistry, (id) => devices.list().some((d) => d.id === id), log, undefined, relay);
|
|
41
|
+
sessions.onNeedsInput((e) => {
|
|
42
|
+
ntfy(e);
|
|
43
|
+
push.notify(e);
|
|
44
|
+
});
|
|
45
|
+
const servers = await startServers({
|
|
46
|
+
config,
|
|
47
|
+
pki,
|
|
48
|
+
devices,
|
|
49
|
+
api: {
|
|
50
|
+
config,
|
|
51
|
+
feed,
|
|
52
|
+
sessions,
|
|
53
|
+
log,
|
|
54
|
+
linear,
|
|
55
|
+
github,
|
|
56
|
+
push: { registry: pushRegistry, enabled: push.enabled },
|
|
57
|
+
// The watch registers its push token with the relay only when the relay will actually send.
|
|
58
|
+
...(push.via === "relay" && relay
|
|
59
|
+
? { relay: { url: relay.url, bridgeId: relay.identity.bridgeId }, claimRelayHandle: (h) => relay.claim(h) }
|
|
60
|
+
: {}),
|
|
61
|
+
},
|
|
62
|
+
log,
|
|
63
|
+
});
|
|
64
|
+
log(`Agent Overwatch bridge "${config.hostName}"`);
|
|
65
|
+
log(` local API (admin token): http://${config.listen.hostname}:${servers.ports.local}`);
|
|
66
|
+
log(` secure API (mTLS): https://${config.secure.hostname}:${servers.ports.secure}`);
|
|
67
|
+
log(` pairing: https://${config.secure.hostname}:${servers.ports.pairing}`);
|
|
68
|
+
log(` server cert SHA-256: ${pki.serverFingerprint.slice(0, 16)}…`);
|
|
69
|
+
log(`agents: ${config.agents.map((a) => a.id).join(", ") || "(none)"} · directories: ${config.directories.map((w) => w.id).join(", ") || "(none)"}`);
|
|
70
|
+
log(`linear: ${linear.enabled ? "on" : "off (overwatch linear login)"} · github: ${github.enabled ? "on" : "off (gh auth login)"}`);
|
|
71
|
+
log(`push: ${push.enabled ? `on via ${push.via === "key" ? "your APNs key" : "the relay"} (${pushRegistry.list().length} watch${pushRegistry.list().length === 1 ? "" : "es"})` : "off"} · ntfy: ${config.notify?.ntfyUrl ? "on" : "off"} · paired devices: ${devices.list().length}`);
|
|
72
|
+
if (config.secure.hostname === "127.0.0.1") {
|
|
73
|
+
log("reach it from anywhere, e.g. with Tailscale Funnel (raw TCP, so TLS stays end-to-end):");
|
|
74
|
+
log(` tailscale funnel --bg --tcp=443 tcp://localhost:${servers.ports.secure}`);
|
|
75
|
+
log(` tailscale funnel --bg --tcp=8443 tcp://localhost:${servers.ports.pairing}`);
|
|
76
|
+
}
|
|
77
|
+
log("pair a watch with: overwatch pair");
|
|
78
|
+
// Agents, directories and approval rules reload when the config changes (e.g. `overwatch dirs add`,
|
|
79
|
+
// or "Always allow" on the watch), so they apply without a restart. Other settings need a restart.
|
|
80
|
+
watchFile(configPath(), { interval: 1000 }, (cur, prev) => {
|
|
81
|
+
if (cur.mtimeMs === prev.mtimeMs)
|
|
82
|
+
return;
|
|
83
|
+
try {
|
|
84
|
+
const next = loadConfig().config;
|
|
85
|
+
config.agents = next.agents;
|
|
86
|
+
config.directories = next.directories;
|
|
87
|
+
config.approvals = next.approvals;
|
|
88
|
+
feed.bump();
|
|
89
|
+
log(`config reloaded · agents: ${config.agents.map((a) => a.id).join(", ") || "(none)"} · directories: ${config.directories.map((d) => d.id).join(", ") || "(none)"}`);
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
log(`config change ignored: ${e.message}`);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
async function shutdown() {
|
|
96
|
+
log("shutting down");
|
|
97
|
+
sessions.shutdown();
|
|
98
|
+
await servers.close();
|
|
99
|
+
process.exit(0);
|
|
100
|
+
}
|
|
101
|
+
process.on("SIGINT", shutdown);
|
|
102
|
+
process.on("SIGTERM", shutdown);
|
|
103
|
+
}
|