@saltpub/agent-autoloop 0.1.5 → 0.1.8
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/CHANGELOG.md +26 -0
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/SECURITY.md +1 -1
- package/dist/bin/agent-autoloop.js +67 -1
- package/dist/cli/dashboard.js +43 -2
- package/dist/cli/explain.js +2 -0
- package/dist/cli/main.js +27 -8
- package/dist/host/global-shim.js +79 -0
- package/dist/host/runtime.js +1 -1
- package/dist/host/state.js +19 -6
- package/dist/host/up.js +8 -0
- package/dist/kernel/decide.js +7 -6
- package/dist/kernel/query.js +2 -1
- package/dist/plugins/codex/agent.js +43 -6
- package/dist/plugins/delivery/project.js +3 -1
- package/dist/plugins/delivery/review.js +5 -2
- package/dist/plugins/feishu/channel.js +44 -6
- package/dist/protocol/rules.js +1 -0
- package/dist/protocol/schema.json +6 -0
- package/dist/worker/agents.js +4 -0
- package/dist/worker/contract.js +1 -0
- package/dist/worker/worker.js +107 -32
- package/docs/auto-update.md +2 -0
- package/docs/protocol.md +8 -2
- package/examples/plugins/hello-source/source.ts +4 -3
- package/package.json +1 -1
- package/plugins/codex/agent.ts +40 -6
- package/plugins/delivery/project.ts +2 -1
- package/plugins/delivery/review.ts +5 -2
- package/plugins/feishu/channel.ts +50 -6
- package/skills/agent-autoloop/SKILL.md +3 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @saltpub/agent-autoloop
|
|
2
2
|
|
|
3
|
+
## 0.1.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 781cc95: npm 全局安装的 `agent-autoloop` 命令改为版本 shim:读 `update/current.json` 指针,把命令转给正在跑的版本执行。以前 `update` 之后中台是新版、敲命令还是旧全局包的行为(help、子命令都停在装的那版);指针缺失、损坏、指向自己或源码检出时回退包自身。launcher 拉起的中台启动时一次性把 shim 之前的旧全局包升级为 shim 版(只升级已装的全局包,失败只告警不挡启动)。
|
|
8
|
+
|
|
9
|
+
## 0.1.7
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- a99d4d2: `agent-autoloop dashboard` 在已有看板占着端口时不再报端口占用:再敲一次会认出是本程序的看板,打印地址并打开它(别的程序占着端口仍然报错)。新增 `dashboard --stop` 关掉在跑的看板,不用再找进程杀;看板相应多了 `POST /stop`,只关看板自己,碰不到中台。
|
|
14
|
+
|
|
15
|
+
## 0.1.6
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- bdc9242: 合并上游修复与规则标签:
|
|
20
|
+
|
|
21
|
+
- 规则可以写可选的 `tags`(文字的列表),中台不解释,原样带进 `/v1/state` 的 `lines`、`runs`、`cards`、`recentCards`、`/v1/runs` 和这条规则每一轮的资料文件。老中台不认识这个字段会拒收规则文件,先升中台再写。
|
|
22
|
+
- worker 轮询只检查还在跑的任务,不再扫历史目录,目录多了也不拖住中台。
|
|
23
|
+
- worker 自身的失败文字按字符截断,不再把半个字留成替换符。
|
|
24
|
+
- Windows 上 Codex 桌面版更新换了安装目录也能找到 `codex`,不再轮轮报找不到。
|
|
25
|
+
- 交付:路径模式末尾整段 `**` 才覆盖它下面的所有文件;审查器提前退出不再把整轮结束掉。
|
|
26
|
+
- 共享用量限制的卡片不再误报已恢复,并说明该试哪条规则。
|
|
27
|
+
- 飞书卡片发不出去时说清原因和下一步(bot 缺权限指向开发者后台),不再贴 lark-cli 的原始 JSON。
|
|
28
|
+
|
|
3
29
|
## 0.1.5
|
|
4
30
|
|
|
5
31
|
### Patch Changes
|
package/README.en.md
CHANGED
|
@@ -101,7 +101,7 @@ Changes take effect within seconds of saving. Run `agent-autoloop check` to vali
|
|
|
101
101
|
|
|
102
102
|
### See what's going on
|
|
103
103
|
|
|
104
|
-
`agent-autoloop dashboard` serves a read-only board on this machine (`http://127.0.0.1:3351` by default; `--port` to move it, `--no-open` to keep the browser shut): rules and the last 24 hours on the left, what's coming and what just finished on the right. The four counts on top — running, queued, waiting on you, finished today — open a detail list, and any round opens its full story. It sends only `GET /v1/state`, `GET /v1/stream`, and `GET /v1/runs/<id>/why`, follows the log live, and the hub keeps running if you close it.
|
|
104
|
+
`agent-autoloop dashboard` serves a read-only board on this machine (`http://127.0.0.1:3351` by default; `--port` to move it, `--no-open` to keep the browser shut; run it again while one is up to print the address and open it, `--stop` to close it): rules and the last 24 hours on the left, what's coming and what just finished on the right. The four counts on top — running, queued, waiting on you, finished today — open a detail list, and any round opens its full story. It sends only `GET /v1/state`, `GET /v1/stream`, and `GET /v1/runs/<id>/why`, follows the log live, and the hub keeps running if you close it.
|
|
105
105
|
|
|
106
106
|
### How agents talk back to the hub
|
|
107
107
|
|
package/README.md
CHANGED
|
@@ -99,7 +99,7 @@ Agent 结束了这一轮,问了你一个问题。它现在不占进程、不
|
|
|
99
99
|
|
|
100
100
|
### 看现在的情况
|
|
101
101
|
|
|
102
|
-
`agent-autoloop dashboard` 在这台电脑上开一个只读看板(默认 `http://127.0.0.1:3351`,`--port` 换端口,`--no-open`
|
|
102
|
+
`agent-autoloop dashboard` 在这台电脑上开一个只读看板(默认 `http://127.0.0.1:3351`,`--port` 换端口,`--no-open` 不打开浏览器;已经在跑时再敲一次会打印地址并打开它,`--stop` 关掉它):左边是规则和最近 24 小时的时间轴,右边是接下去要跑的、最近跑完的;顶上四个数是“在跑”“排队”“待你”“今天跑完”,点开是明细,点任意一轮能看它的来龙去脉。它只发 `GET /v1/state`、`GET /v1/stream`、`GET /v1/runs/<id>/why` 三个请求,靠日志实时更新,关掉它不影响中台。
|
|
103
103
|
|
|
104
104
|
### Agent 怎么跟中台配合?
|
|
105
105
|
|
package/SECURITY.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
- `access` 由专用 Agent 插件映射到 CLI 权限;`command` 不替任意程序限制权限。普通 Agent 的 `read` 不等于读不到这台电脑上的密钥。要隔离密钥,用单独的系统账号或容器。
|
|
15
15
|
- 普通访问密钥只存 SHA-256,另有本机 `master.key` 推导轮次和卡片链接的 token。`master.key` 为空或不完整时中台拒绝启动,不会拿它签链接,也不会自己换一把新的。接单程序不通过提示词、资料或环境变量传递中台凭据;同一系统账号下能读哪些文件仍由 Agent 沙箱决定。回执能影响的范围见 `docs/protocol.md`。
|
|
16
16
|
- 卡片链接谁拿到谁就能回答这一张卡片,从卡片创建起 7 天内有效,过期后也不再显示卡片内容,只发给配了 `"links": true` 的通道。
|
|
17
|
-
- `agent-autoloop dashboard` 只听 127.0.0.1,只把 `/v1/state`、`/v1/stream`、`/v1/runs/<id>/why` 连同本人密钥转给中台,密钥不进页面;只认发给 `127.0.0.1:<端口>` 和 `localhost:<端口>`
|
|
17
|
+
- `agent-autoloop dashboard` 只听 127.0.0.1,只把 `/v1/state`、`/v1/stream`、`/v1/runs/<id>/why` 连同本人密钥转给中台,密钥不进页面;只认发给 `127.0.0.1:<端口>` 和 `localhost:<端口>` 的请求,别的主机名(包括被解析到本机的域名)一律拒绝;除 `POST /stop`(只关掉看板自己,碰不到中台和本机别的任何东西)外别的方法一律拒绝。它只读,不写日志、不回答问题。
|
|
18
18
|
- 本项目只启动你自己安装、自己登录的官方命令行工具,不接触、不保存、不中转它们的登录凭据。
|
|
19
19
|
- 浏览器能力由 Agent 自己的工具配置提供;专用入口只从可信规则或 Skill 取得,不从事件正文取命令。中台不自动授予浏览器权限或接管桌面会话。需本人恢复的连接或验证用 `ask` 交回,保留未验收项。
|
|
20
20
|
|
|
@@ -1,2 +1,68 @@
|
|
|
1
1
|
#!/usr/bin/env -S node --disable-warning=ExperimentalWarning
|
|
2
|
-
|
|
2
|
+
// AGENT_AUTOLOOP_SHIM_V1 — version shim, rustup-style. The npm global install resolves the self-update pointer and
|
|
3
|
+
// re-runs the versioned copy, so a command follows the version the hub runs after an update instead of the stale
|
|
4
|
+
// global package. Every failure mode (no pointer, corrupt JSON, missing target, a pointer naming this very
|
|
5
|
+
// install, a git checkout) falls back to this package's own CLI — the behaviour before the shim.
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
const HERE = import.meta.dirname;
|
|
11
|
+
const COMPILED = path.extname(import.meta.filename) === ".js";
|
|
12
|
+
/** The folder with package.json: above dist/bin when installed, above bin/ in a checkout. */
|
|
13
|
+
const ROOT = COMPILED ? path.dirname(path.dirname(HERE)) : path.dirname(HERE);
|
|
14
|
+
const HOME = process.env.AGENT_AUTOLOOP_HOME?.trim() || path.join(homedir(), ".agent-autoloop");
|
|
15
|
+
/** The managed copy's entry, or undefined to fall back to this package. */
|
|
16
|
+
function pointedEntry() {
|
|
17
|
+
let pointer;
|
|
18
|
+
try {
|
|
19
|
+
pointer = JSON.parse(readFileSync(path.join(HOME, "update", "current.json"), "utf8"));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return undefined; // no managed install, or a pointer file half-written
|
|
23
|
+
}
|
|
24
|
+
const dir = pointer?.dir;
|
|
25
|
+
if (typeof dir !== "string" || dir.length === 0)
|
|
26
|
+
return undefined;
|
|
27
|
+
// A pointer naming this very install: re-running it would fork-bomb.
|
|
28
|
+
if (path.resolve(dir) === path.resolve(ROOT))
|
|
29
|
+
return undefined;
|
|
30
|
+
const entry = path.join(dir, "dist", "bin", "agent-autoloop.js");
|
|
31
|
+
return existsSync(entry) ? entry : undefined;
|
|
32
|
+
}
|
|
33
|
+
// A checkout runs its own sources, pointer or not. The fallback imports are dynamic on purpose: a static import
|
|
34
|
+
// would run this package's CLI even when the command belongs to the managed copy.
|
|
35
|
+
if (existsSync(path.join(ROOT, ".git"))) {
|
|
36
|
+
await import("../cli/main.js");
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const entry = pointedEntry();
|
|
40
|
+
if (!entry) {
|
|
41
|
+
await import("../cli/main.js");
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const child = spawn(process.execPath, ["--disable-warning=ExperimentalWarning", entry, ...process.argv.slice(2)], {
|
|
45
|
+
stdio: "inherit",
|
|
46
|
+
});
|
|
47
|
+
// POSIX: forward termination so Ctrl-C / SIGTERM reach the real CLI.
|
|
48
|
+
const forward = process.platform === "win32" ? undefined : (signal) => void child.kill(signal);
|
|
49
|
+
if (forward) {
|
|
50
|
+
process.on("SIGINT", forward);
|
|
51
|
+
process.on("SIGTERM", forward);
|
|
52
|
+
}
|
|
53
|
+
child.on("error", (error) => {
|
|
54
|
+
console.error(`agent-autoloop: 没能启动 ${entry}:${error.message}`);
|
|
55
|
+
process.exit(1);
|
|
56
|
+
});
|
|
57
|
+
child.on("exit", (code, signal) => {
|
|
58
|
+
if (signal && process.platform !== "win32" && forward) {
|
|
59
|
+
// Die the way the child did: drop the handlers (default action is terminate) and re-raise.
|
|
60
|
+
process.off("SIGINT", forward);
|
|
61
|
+
process.off("SIGTERM", forward);
|
|
62
|
+
process.kill(process.pid, signal);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
process.exit(code ?? 1);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
package/dist/cli/dashboard.js
CHANGED
|
@@ -18,6 +18,7 @@ export async function startDashboard({ url, key, port, folder = path.join(import
|
|
|
18
18
|
const pages = new Map(PAGES.map(([route, file, type]) => [route, { body: readFileSync(path.join(folder, file)), type }]));
|
|
19
19
|
const owner = { authorization: `Bearer ${key}` };
|
|
20
20
|
let allowed = new Set();
|
|
21
|
+
const stopping = Promise.withResolvers();
|
|
21
22
|
async function forward(answer, address) {
|
|
22
23
|
const upstream = await fetch(address, { headers: owner });
|
|
23
24
|
if (!upstream.ok)
|
|
@@ -50,9 +51,14 @@ export async function startDashboard({ url, key, port, folder = path.join(import
|
|
|
50
51
|
async function serve(request, answer) {
|
|
51
52
|
if (!allowed.has(request.headers.host ?? ""))
|
|
52
53
|
return send(answer, 403, { error: "这个地址不是本机的看板" });
|
|
54
|
+
const here = new URL(request.url ?? "/", "http://127.0.0.1");
|
|
55
|
+
// The one thing past read-only: it stops the board itself, and nothing behind it.
|
|
56
|
+
if (here.pathname === "/stop" && request.method === "POST") {
|
|
57
|
+
send(answer, 200, { stopped: true });
|
|
58
|
+
return stopping.resolve();
|
|
59
|
+
}
|
|
53
60
|
if (request.method !== "GET" && request.method !== "HEAD")
|
|
54
61
|
return send(answer, 405, { error: "看板只读" });
|
|
55
|
-
const here = new URL(request.url ?? "/", "http://127.0.0.1");
|
|
56
62
|
const page = pages.get(here.pathname);
|
|
57
63
|
if (page) {
|
|
58
64
|
answer.writeHead(200, { "content-type": page.type, "cache-control": "no-store" });
|
|
@@ -72,6 +78,8 @@ export async function startDashboard({ url, key, port, folder = path.join(import
|
|
|
72
78
|
return send(answer, 404, { error: "看板没有这个地址" });
|
|
73
79
|
}
|
|
74
80
|
const server = createServer((request, answer) => {
|
|
81
|
+
// Every answer carries the board's name, so a rerun of the command can tell it from a stranger on the same port.
|
|
82
|
+
answer.setHeader("x-agent-autoloop", "dashboard");
|
|
75
83
|
serve(request, answer).catch((error) => send(answer, 502, { error: error.message }));
|
|
76
84
|
});
|
|
77
85
|
const listening = Promise.withResolvers();
|
|
@@ -79,10 +87,43 @@ export async function startDashboard({ url, key, port, folder = path.join(import
|
|
|
79
87
|
server.listen(port, "127.0.0.1", () => {
|
|
80
88
|
const bound = server.address().port;
|
|
81
89
|
allowed = new Set([`127.0.0.1:${bound}`, `localhost:${bound}`]);
|
|
82
|
-
listening.resolve({
|
|
90
|
+
listening.resolve({
|
|
91
|
+
url: `http://127.0.0.1:${bound}/`,
|
|
92
|
+
// The page holds a live stream open; without cutting those sockets close() would wait on them forever.
|
|
93
|
+
close: () => {
|
|
94
|
+
server.closeAllConnections();
|
|
95
|
+
server.close();
|
|
96
|
+
},
|
|
97
|
+
stopped: stopping.promise,
|
|
98
|
+
});
|
|
83
99
|
});
|
|
84
100
|
return listening.promise;
|
|
85
101
|
}
|
|
102
|
+
/** The address of the board already on this port, or null when the port is free or belongs to something else. */
|
|
103
|
+
export async function probeDashboard(port) {
|
|
104
|
+
try {
|
|
105
|
+
const answer = await fetch(`http://127.0.0.1:${port}/`, { signal: AbortSignal.timeout(2000) });
|
|
106
|
+
await answer.body?.cancel();
|
|
107
|
+
return answer.headers.get("x-agent-autoloop") === "dashboard" ? `http://127.0.0.1:${port}/` : null;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** Asks the board on this port to close. True only when a board said yes; a stranger's server is left alone. */
|
|
114
|
+
export async function stopDashboard(port) {
|
|
115
|
+
try {
|
|
116
|
+
const answer = await fetch(`http://127.0.0.1:${port}/stop`, {
|
|
117
|
+
method: "POST",
|
|
118
|
+
signal: AbortSignal.timeout(2000),
|
|
119
|
+
});
|
|
120
|
+
await answer.body?.cancel();
|
|
121
|
+
return answer.headers.get("x-agent-autoloop") === "dashboard" && answer.ok;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
86
127
|
function send(answer, status, body) {
|
|
87
128
|
if (answer.headersSent)
|
|
88
129
|
return answer.end();
|
package/dist/cli/explain.js
CHANGED
|
@@ -20,6 +20,8 @@ function details(rule, rules, now) {
|
|
|
20
20
|
parts.push("接着上一轮的会话");
|
|
21
21
|
if (rule.title)
|
|
22
22
|
parts.push(`会话标题 ${rule.title}`);
|
|
23
|
+
if (rule.tags?.length)
|
|
24
|
+
parts.push(`标签 ${rule.tags.join("、")}`);
|
|
23
25
|
if (rule.group !== rule.name)
|
|
24
26
|
parts.push(`组 ${rule.group}`);
|
|
25
27
|
if (rule.tell !== "needed")
|
package/dist/cli/main.js
CHANGED
|
@@ -118,12 +118,13 @@ const STATUS_USAGE = `用法:agent-autoloop status [--json]
|
|
|
118
118
|
|
|
119
119
|
现在什么情况:谁在线、等你什么、在做什么、做过什么、花了多少。
|
|
120
120
|
--json 输出 /v1/state 的原始结构,给脚本和看板用`;
|
|
121
|
-
const DASHBOARD_USAGE = `用法:agent-autoloop dashboard [--port 3351] [--no-open]
|
|
121
|
+
const DASHBOARD_USAGE = `用法:agent-autoloop dashboard [--port 3351] [--no-open] [--stop]
|
|
122
122
|
|
|
123
123
|
在这台电脑上打开看板:最近 24 小时的轨道、每条规则、在跑和排着的活、等你决定的卡片。
|
|
124
|
-
只读;回答卡片仍然走通道或 agent-autoloop answer
|
|
124
|
+
只读;回答卡片仍然走通道或 agent-autoloop answer。已经在跑时再敲一次会打印地址并打开它。
|
|
125
125
|
--port 看板占用的端口,默认 3351
|
|
126
|
-
--no-open
|
|
126
|
+
--no-open 不发开浏览器,只打印地址
|
|
127
|
+
--stop 关掉这个端口上在跑的看板`;
|
|
127
128
|
const RUNS_USAGE = `用法:agent-autoloop runs [规则] [--before <第几轮>] [--json]
|
|
128
129
|
|
|
129
130
|
列出做完的轮次,新的在前,一次 20 轮:编号、结束时间、怎么样了、规则和结果。
|
|
@@ -186,7 +187,7 @@ rules.json 里这个定时任务要写 cwd;你答了,中台叫醒它的 Agen
|
|
|
186
187
|
runs: { flags: ["before", "json"], usage: RUNS_USAGE },
|
|
187
188
|
run: { flags: ["subject", "reason"], usage: "用法:agent-autoloop run <规则> [--subject 对象] [--reason 说明]" },
|
|
188
189
|
service: { flags: [], usage: "用法:agent-autoloop service install|remove" },
|
|
189
|
-
dashboard: { flags: ["port", "no-open"], usage: DASHBOARD_USAGE },
|
|
190
|
+
dashboard: { flags: ["port", "no-open", "stop"], usage: DASHBOARD_USAGE },
|
|
190
191
|
status: { flags: ["json"], usage: STATUS_USAGE },
|
|
191
192
|
stop: { flags: [], usage: "用法:agent-autoloop stop <第几轮>" },
|
|
192
193
|
tell: { flags: ["task"], usage: "用法:agent-autoloop tell <编号> [--task 定时任务名],内容从标准输入读" },
|
|
@@ -557,20 +558,38 @@ async function dashboard(args) {
|
|
|
557
558
|
const port = given === undefined ? 3351 : Number(given);
|
|
558
559
|
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
559
560
|
return fail(`端口要写 1 到 65535 之间的数字:${given}`);
|
|
561
|
+
const { probeDashboard, startDashboard, stopDashboard } = await import("./dashboard.js");
|
|
562
|
+
if (named.stop !== undefined) {
|
|
563
|
+
const stopped = await stopDashboard(port);
|
|
564
|
+
return console.log(stopped ? "看板关了。" : `127.0.0.1:${port} 没有在跑的看板。`);
|
|
565
|
+
}
|
|
560
566
|
const config = readConfig();
|
|
561
|
-
const { startDashboard } = await import("./dashboard.js");
|
|
562
567
|
const board = await startDashboard({
|
|
563
568
|
url: config.url,
|
|
564
569
|
key: config.owner ?? fail("这台电脑上没有本人的密钥(只接活的电脑看不了)"),
|
|
565
570
|
port,
|
|
566
|
-
}).catch((error) =>
|
|
567
|
-
|
|
571
|
+
}).catch(async (error) => {
|
|
572
|
+
// A rerun while one is up: point at it instead of dying on the busy port. A stranger's port still fails.
|
|
573
|
+
if (error.code === "EADDRINUSE") {
|
|
574
|
+
const running = await probeDashboard(port);
|
|
575
|
+
if (running) {
|
|
576
|
+
console.log(`看板已经在 ${running} 跑着`);
|
|
577
|
+
if (named["no-open"] === undefined)
|
|
578
|
+
openPage(running);
|
|
579
|
+
return null;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return fail(`看板起不来:${error.message}`);
|
|
583
|
+
});
|
|
584
|
+
if (!board)
|
|
585
|
+
return;
|
|
586
|
+
console.log(`看板在 ${board.url} · 按 Ctrl-C 或 agent-autoloop dashboard --stop 结束`);
|
|
568
587
|
if (named["no-open"] === undefined)
|
|
569
588
|
openPage(board.url);
|
|
570
589
|
const stop = Promise.withResolvers();
|
|
571
590
|
process.once("SIGINT", stop.resolve);
|
|
572
591
|
process.once("SIGTERM", stop.resolve);
|
|
573
|
-
await stop.promise;
|
|
592
|
+
await Promise.race([stop.promise, board.stopped]);
|
|
574
593
|
board.close();
|
|
575
594
|
}
|
|
576
595
|
async function status(named) {
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// A one-time heal for global installs from before the bin became a version shim: without it, commands typed at the
|
|
2
|
+
// global bin keep running the old package's own code while the hub and --version follow the update pointer. The
|
|
3
|
+
// launcher child upgrades the global package once, so its bin becomes the shim, and writes a marker so every later
|
|
4
|
+
// boot is a no-op. It never installs a global package the machine doesn't have, and never breaks startup: every
|
|
5
|
+
// failure is a result, not a throw.
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { commandLine } from "./command.js";
|
|
10
|
+
import { HOME } from "./home.js";
|
|
11
|
+
import { PACKAGE } from "./npm.js";
|
|
12
|
+
import { REGISTRY, updateDir } from "./update.js";
|
|
13
|
+
import { versionName } from "./version.js";
|
|
14
|
+
/** Written into the shim's bin; an old global package's bin doesn't have it. */
|
|
15
|
+
export const SHIM_MARKER = "AGENT_AUTOLOOP_SHIM_V1";
|
|
16
|
+
const runNpm = (args) => {
|
|
17
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
18
|
+
const [program, rest] = commandLine(["npm", ...args]);
|
|
19
|
+
const child = spawn(program, rest, { stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
|
|
20
|
+
let stdout = "";
|
|
21
|
+
let stderr = "";
|
|
22
|
+
child.stdout.on("data", (chunk) => (stdout += chunk));
|
|
23
|
+
child.stderr.on("data", (chunk) => (stderr += chunk));
|
|
24
|
+
child.on("error", reject);
|
|
25
|
+
child.on("close", (code) => resolve({ code: code ?? 1, stdout, stderr }));
|
|
26
|
+
return promise;
|
|
27
|
+
};
|
|
28
|
+
export async function healGlobalShim(options = {}) {
|
|
29
|
+
const home = options.home ?? HOME;
|
|
30
|
+
const markerFile = options.markerFile ?? path.join(updateDir(home), "global-shim.json");
|
|
31
|
+
const run = options.run ?? runNpm;
|
|
32
|
+
const exists = options.exists ?? existsSync;
|
|
33
|
+
const read = options.read ?? ((file) => readFileSync(file, "utf8"));
|
|
34
|
+
const version = options.version ?? versionName();
|
|
35
|
+
const now = options.now ?? (() => new Date());
|
|
36
|
+
const mark = () => {
|
|
37
|
+
mkdirSync(path.dirname(markerFile), { recursive: true });
|
|
38
|
+
writeFileSync(markerFile, JSON.stringify({ healedAt: now().toISOString(), version }), { mode: 0o600 });
|
|
39
|
+
};
|
|
40
|
+
try {
|
|
41
|
+
if (exists(markerFile))
|
|
42
|
+
return { healed: false, reason: "already-healed" };
|
|
43
|
+
const root = await run(["root", "-g"]);
|
|
44
|
+
const globalRoot = root.stdout.trim();
|
|
45
|
+
if (root.code !== 0 || !globalRoot)
|
|
46
|
+
return { healed: false, reason: "failed" };
|
|
47
|
+
const globalPackage = path.join(globalRoot, ...PACKAGE.split("/"));
|
|
48
|
+
// No global package: nothing to heal, and never install one ourselves.
|
|
49
|
+
if (!exists(path.join(globalPackage, "package.json"))) {
|
|
50
|
+
mark();
|
|
51
|
+
return { healed: false, reason: "no-global-package" };
|
|
52
|
+
}
|
|
53
|
+
let bin = "";
|
|
54
|
+
try {
|
|
55
|
+
bin = read(path.join(globalPackage, "dist", "bin", "agent-autoloop.js"));
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// A bin that can't be read can't carry the marker either; installing repairs it.
|
|
59
|
+
}
|
|
60
|
+
if (bin.includes(SHIM_MARKER)) {
|
|
61
|
+
mark();
|
|
62
|
+
return { healed: false, reason: "already-shim" };
|
|
63
|
+
}
|
|
64
|
+
const install = await run([
|
|
65
|
+
"install",
|
|
66
|
+
"-g",
|
|
67
|
+
`${PACKAGE}@latest`,
|
|
68
|
+
`--registry=${REGISTRY}`,
|
|
69
|
+
`--${PACKAGE.split("/")[0]}:registry=${REGISTRY}`,
|
|
70
|
+
]);
|
|
71
|
+
if (install.code !== 0)
|
|
72
|
+
return { healed: false, reason: "failed" };
|
|
73
|
+
mark();
|
|
74
|
+
return { healed: true };
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return { healed: false, reason: "failed" };
|
|
78
|
+
}
|
|
79
|
+
}
|
package/dist/host/runtime.js
CHANGED
|
@@ -134,7 +134,7 @@ export const view = (card, ref = null, links = false) => query.cardView(card, en
|
|
|
134
134
|
export const activeRuns = () => Number(one("SELECT count(*) AS n FROM runs WHERE state IN ('queued', 'claimed', 'unknown')")?.n ?? 0);
|
|
135
135
|
export const unrouted = (limit = 20) => query.unrouted(getView(), limit);
|
|
136
136
|
export const skipped = (rule, since, everything = false) => skippedOf(all, rule, since, everything);
|
|
137
|
-
export const runsOfRule = (rule, before) => snapshot(() => ruleHistory(all, rule, before));
|
|
137
|
+
export const runsOfRule = (rule, before) => snapshot(() => ruleHistory(all, rules, rule, before));
|
|
138
138
|
/** Read in one snapshot: its seq is where the log stood for all of it, for a client to go on in /v1/stream from. */
|
|
139
139
|
export function stateOf(now = Date.now(), before) {
|
|
140
140
|
return snapshot(() => {
|
package/dist/host/state.js
CHANGED
|
@@ -91,10 +91,16 @@ export function skippedOf(all, rule, since, everything = false) {
|
|
|
91
91
|
const events = all(`${columns} ${SKIPPED_BY} ${order}`, [since, rule, rule]);
|
|
92
92
|
return { count, events: events.map(skippedView) };
|
|
93
93
|
}
|
|
94
|
-
|
|
94
|
+
/** A rule's tags by its name, as the rules are now: none for one without them or no longer there. */
|
|
95
|
+
function tagsOf(rules) {
|
|
96
|
+
const tags = new Map(rules.rules.map((rule) => [rule.name, rule.tags ?? []]));
|
|
97
|
+
return (rule) => tags.get(rule) ?? [];
|
|
98
|
+
}
|
|
99
|
+
function runView(run, all, tags) {
|
|
95
100
|
return {
|
|
96
101
|
id: run.id,
|
|
97
102
|
rule: run.rule,
|
|
103
|
+
tags,
|
|
98
104
|
agent: run.agent,
|
|
99
105
|
state: run.state,
|
|
100
106
|
attempts: run.attempts,
|
|
@@ -112,12 +118,12 @@ function runView(run, all) {
|
|
|
112
118
|
events: all(EVENTS_OF_RUN, [run.id]),
|
|
113
119
|
};
|
|
114
120
|
}
|
|
115
|
-
export function ruleHistory(all, rule, before) {
|
|
121
|
+
export function ruleHistory(all, rules, rule, before) {
|
|
116
122
|
const limit = 20;
|
|
117
123
|
const rows = all(`SELECT * FROM runs WHERE rule = ? AND state NOT IN ('queued', 'claimed') ${before === undefined ? "" : "AND id < ?"}
|
|
118
124
|
ORDER BY id DESC LIMIT ?`, before === undefined ? [rule, limit + 1] : [rule, before, limit + 1]);
|
|
119
125
|
return {
|
|
120
|
-
runs: rows.slice(0, limit).map((run) => runView(run, all)),
|
|
126
|
+
runs: rows.slice(0, limit).map((run) => runView(run, all, tagsOf(rules)(rule))),
|
|
121
127
|
nextBefore: rows.length > limit ? rows[limit - 1].id : null,
|
|
122
128
|
};
|
|
123
129
|
}
|
|
@@ -157,6 +163,7 @@ export function stateOf(now, before, rules, { all, view, unrouted }) {
|
|
|
157
163
|
const { by, reason, since, until, skipped } = pause;
|
|
158
164
|
return { by, reason, since, until, skipped };
|
|
159
165
|
};
|
|
166
|
+
const tags = tagsOf(rules);
|
|
160
167
|
const skippedAt = new Map(all("SELECT rule, skipped_at FROM schedules").map((row) => [row.rule, row.skipped_at]));
|
|
161
168
|
return {
|
|
162
169
|
now: iso(now),
|
|
@@ -164,8 +171,13 @@ export function stateOf(now, before, rules, { all, view, unrouted }) {
|
|
|
164
171
|
spending: spendingOf(all, now, rules.timezone),
|
|
165
172
|
totals: { ...tally(counts), attention: openCards.length },
|
|
166
173
|
// The owner's own: which matter a card is about, and what its asker left for whoever carries on. Channels don't get them.
|
|
167
|
-
cards: openCards.map((card) => ({ ...view(card), lane: card.lane, about: card.about })),
|
|
168
|
-
recentCards: recentCards.map((card) => ({
|
|
174
|
+
cards: openCards.map((card) => ({ ...view(card), tags: tags(card.rule), lane: card.lane, about: card.about })),
|
|
175
|
+
recentCards: recentCards.map((card) => ({
|
|
176
|
+
...view(card),
|
|
177
|
+
tags: tags(card.rule),
|
|
178
|
+
lane: card.lane,
|
|
179
|
+
about: card.about,
|
|
180
|
+
})),
|
|
169
181
|
timers: timers.map(({ rule, lane, key, about, at }) => ({ rule, lane, key, about, at })),
|
|
170
182
|
sources: Object.entries(rules.sources).map(([id, { name }]) => {
|
|
171
183
|
const events = all("SELECT COUNT(*) AS n FROM events WHERE source = ?", [id])[0].n;
|
|
@@ -175,6 +187,7 @@ export function stateOf(now, before, rules, { all, view, unrouted }) {
|
|
|
175
187
|
id: rule.name,
|
|
176
188
|
name: rule.name,
|
|
177
189
|
group: rule.group,
|
|
190
|
+
tags: rule.tags ?? [],
|
|
178
191
|
agent: rule.to,
|
|
179
192
|
source: eventRule(rule) ? rule.when.source : SCHEDULE,
|
|
180
193
|
types: eventRule(rule) ? rule.when.types : [],
|
|
@@ -211,7 +224,7 @@ export function stateOf(now, before, rules, { all, view, unrouted }) {
|
|
|
211
224
|
: undefined;
|
|
212
225
|
return { id, name, ...presence(`channel:${id}`), ...(lastFailure && { lastFailure }) };
|
|
213
226
|
}),
|
|
214
|
-
runs: visible.map((run) => runView(run, all)),
|
|
227
|
+
runs: visible.map((run) => runView(run, all, tags(run.rule))),
|
|
215
228
|
hasMore,
|
|
216
229
|
nextBefore: hasMore ? page[PAGE - 1].id : null,
|
|
217
230
|
unrouted: unrouted(),
|
package/dist/host/up.js
CHANGED
|
@@ -5,6 +5,7 @@ import { existsSync, mkdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { loadChannel } from "./channels.js";
|
|
7
7
|
import { cantListen, parseListen, readConfig } from "./config.js";
|
|
8
|
+
import { healGlobalShim } from "./global-shim.js";
|
|
8
9
|
import { HOME } from "./home.js";
|
|
9
10
|
import { runningUp } from "./service.js";
|
|
10
11
|
import { commitOf } from "./version.js";
|
|
@@ -131,6 +132,13 @@ export async function up() {
|
|
|
131
132
|
jobs.push(keepChannelRunning(id, channel, config.url, stop.signal));
|
|
132
133
|
}
|
|
133
134
|
refreshLauncher();
|
|
135
|
+
// The npm-global bin became a version shim in 0.1.8: upgrade a pre-shim global install once, never blocking startup.
|
|
136
|
+
if (managedAt(HOME) && process.env.AGENT_AUTOLOOP_LAUNCHED === "1") {
|
|
137
|
+
void healGlobalShim().then((result) => {
|
|
138
|
+
if (result.healed)
|
|
139
|
+
log("update")("全局命令已换成跟随中台版本的 shim");
|
|
140
|
+
});
|
|
141
|
+
}
|
|
134
142
|
// A hub that starts matters more than the link to its manual: a folder the user's home won't take is worth a line, not
|
|
135
143
|
// refusing to run.
|
|
136
144
|
try {
|
package/dist/kernel/decide.js
CHANGED
|
@@ -119,11 +119,13 @@ function closeOpen(draft, matches, result) {
|
|
|
119
119
|
/** Once it's stopped, nothing should still say it will try again, or that it may still be going. */
|
|
120
120
|
const closeStopped = (draft, run, result) => closeOpen(draft, (key) => key === `run:${run}:failing` || key === `run:${run}:unknown`, result);
|
|
121
121
|
const closeOffline = (draft, worker) => closeOpen(draft, (key) => key.startsWith(`worker:${worker}:offline:`), "连上了");
|
|
122
|
-
/** An agent's usage limit is over
|
|
122
|
+
/** An agent's usage limit is over; their shared card closes when nobody is waiting for that reset. */
|
|
123
123
|
function lift(draft, agent, result) {
|
|
124
124
|
const { until } = draft.view.get("limits", agent);
|
|
125
125
|
set(draft, "limits", agent, null);
|
|
126
|
-
|
|
126
|
+
if (rows(draft.view, "limits", { until }).length === 0) {
|
|
127
|
+
closeOpen(draft, (key) => key === `limit:${until}`, result);
|
|
128
|
+
}
|
|
127
129
|
}
|
|
128
130
|
/** Whether a new stamp would tell nothing this one doesn't. One in the future means a clock was set back: it gets redone. */
|
|
129
131
|
function justSeen({ now }, row) {
|
|
@@ -397,7 +399,6 @@ function limit(draft, run, until, error) {
|
|
|
397
399
|
reason: error,
|
|
398
400
|
since: held?.since ?? draft.at,
|
|
399
401
|
});
|
|
400
|
-
const agent = configuration(draft.view, run.rules_version).agents[run.agent]?.name ?? run.agent;
|
|
401
402
|
const time = new Intl.DateTimeFormat("zh-CN", {
|
|
402
403
|
timeZone: draft.rules.timezone,
|
|
403
404
|
month: "numeric",
|
|
@@ -406,10 +407,10 @@ function limit(draft, run, until, error) {
|
|
|
406
407
|
minute: "2-digit",
|
|
407
408
|
hourCycle: "h23",
|
|
408
409
|
}).format(until);
|
|
409
|
-
const words =
|
|
410
|
-
|
|
410
|
+
const words = `有 Agent 用量到上限了,${time} 前不再派活给仍受限的 Agent。排着的和这期间新来的活都留着,到时自动接着做。` +
|
|
411
|
+
`运行 agent-autoloop status 可以查看当前受限项;想提前试,手动运行一条用到受限 Agent 的规则(agent-autoloop run),跑通就接着给它派活。`;
|
|
411
412
|
// Agents on one account are told the same moment: keyed by it, the owner hears of it once.
|
|
412
|
-
notify(draft, `limit:${iso(until)}`,
|
|
413
|
+
notify(draft, `limit:${iso(until)}`, "Agent 用量到上限", words);
|
|
413
414
|
}
|
|
414
415
|
function scheduleReview(draft, run, later) {
|
|
415
416
|
const { rule, lane } = run;
|
package/dist/kernel/query.js
CHANGED
|
@@ -132,7 +132,7 @@ export function cardView(card, env, ref = null, links = false) {
|
|
|
132
132
|
}
|
|
133
133
|
export function jobOf(view, run, env) {
|
|
134
134
|
const rules = configuration(view, run.rules_version);
|
|
135
|
-
const { name, cwd, skill, prompt, title, emits } = ruleOf(rules, run.rule);
|
|
135
|
+
const { name, cwd, skill, prompt, title, emits, tags } = ruleOf(rules, run.rule);
|
|
136
136
|
const { maxActive: _maxActive, ...agent } = rules.agents[run.agent];
|
|
137
137
|
const events = eventsOf(view, run.id);
|
|
138
138
|
const eventIds = events.map((event) => event.id);
|
|
@@ -194,6 +194,7 @@ export function jobOf(view, run, env) {
|
|
|
194
194
|
.map(({ id, title, choice, answer, options, decided_at }) => {
|
|
195
195
|
return { card: id, title, choice: choice === null ? answer : JSON.parse(options)[choice], at: decided_at };
|
|
196
196
|
}),
|
|
197
|
+
...(tags && { tags }),
|
|
197
198
|
},
|
|
198
199
|
};
|
|
199
200
|
}
|
|
@@ -1,13 +1,50 @@
|
|
|
1
1
|
// Codex: the user's own, installed and signed in by them. A run is a `codex exec` in the background; its thread is kept
|
|
2
2
|
// on disk, so a retry or an answer can go on in it, and the link opens it in the app.
|
|
3
3
|
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { readdirSync, statSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
4
6
|
import { commandLine, powershell, psQuote } from "../../host/command.js";
|
|
5
7
|
import { findProgram, tail } from "../../worker/agents.js";
|
|
6
8
|
const SANDBOX = { read: "read-only", edit: "workspace-write", full: "danger-full-access" };
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
/** The Codex the desktop app installs, for those without one of their own. */
|
|
10
|
+
export function inTheApp() {
|
|
11
|
+
const mac = [
|
|
12
|
+
"/Applications/ChatGPT.app/Contents/Resources/codex-cli/CodexCLI.app/Contents/MacOS/codex",
|
|
13
|
+
"/Applications/Codex.app/Contents/Resources/codex",
|
|
14
|
+
];
|
|
15
|
+
// The Windows installer puts codex.exe in %LOCALAPPDATA%\OpenAI\Codex\bin\<id>; an update adds a new <id> beside the
|
|
16
|
+
// old one and deletes the old one, leaving PATH naming the folder it deleted. So read that folder for the newest
|
|
17
|
+
// codex.exe rather than one path an update rewrites: on 2026-11-20 and 2026-11-21 both morning rules failed with
|
|
18
|
+
// 找不到 codex while the program sat in the folder beside the one PATH named.
|
|
19
|
+
const local = process.env.LOCALAPPDATA;
|
|
20
|
+
const root = local ? path.join(local, "OpenAI", "Codex", "bin") : null;
|
|
21
|
+
if (!root)
|
|
22
|
+
return mac;
|
|
23
|
+
// An update is emptying these folders while this runs, so each is read on its own and one that has gone is left out:
|
|
24
|
+
// a throw here reaches the round as whichever path vanished rather than as 找不到 codex.
|
|
25
|
+
const folders = (() => {
|
|
26
|
+
try {
|
|
27
|
+
return readdirSync(root, { withFileTypes: true });
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// No Codex desktop install, or a bin this account can't read as a folder.
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
})();
|
|
34
|
+
const found = [];
|
|
35
|
+
for (const entry of folders) {
|
|
36
|
+
if (!entry.isDirectory())
|
|
37
|
+
continue;
|
|
38
|
+
const file = path.join(root, entry.name, "codex.exe");
|
|
39
|
+
try {
|
|
40
|
+
found.push({ file, at: statSync(file).mtimeMs });
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// A folder holding another of the app's programs, or one this update is still emptying.
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return [...found.sort((a, b) => b.at - a.at).map((codex) => codex.file), ...mac];
|
|
47
|
+
}
|
|
11
48
|
export default {
|
|
12
49
|
command: (turn) => {
|
|
13
50
|
// The elevated Windows sandbox runs as CodexSandboxUsers. Share only the supplied data, never the run token or key.
|
|
@@ -23,7 +60,7 @@ export default {
|
|
|
23
60
|
[IO.File]::SetAccessControl($file, $acl)
|
|
24
61
|
`);
|
|
25
62
|
return [
|
|
26
|
-
turn.program ?? findProgram("codex",
|
|
63
|
+
turn.program ?? findProgram("codex", inTheApp),
|
|
27
64
|
"exec",
|
|
28
65
|
...(turn.resume ? ["resume"] : []),
|
|
29
66
|
"--json",
|
|
@@ -90,7 +127,7 @@ export default {
|
|
|
90
127
|
},
|
|
91
128
|
link: (session) => `codex://threads/${session}`,
|
|
92
129
|
check(program) {
|
|
93
|
-
const exe = program ?? findProgram("codex",
|
|
130
|
+
const exe = program ?? findProgram("codex", inTheApp);
|
|
94
131
|
try {
|
|
95
132
|
execFileSync(...commandLine([exe, "login", "status"]), {
|
|
96
133
|
encoding: "utf8",
|
|
@@ -94,9 +94,11 @@ export function readMainSettings(root, testLocalHealth = false, ref = "origin/ma
|
|
|
94
94
|
return settings(JSON.parse(git(root, "show", `${ref}:ops/delivery.json`)), testLocalHealth);
|
|
95
95
|
}
|
|
96
96
|
function globToRegExp(pattern) {
|
|
97
|
-
const parts = pattern.split(/(
|
|
97
|
+
const parts = pattern.split(/(\*\*\/|(?<=^|\/)\*\*$|\*)/).map((part) => {
|
|
98
98
|
if (part === "**/")
|
|
99
99
|
return "(?:.*/)?";
|
|
100
|
+
if (part === "**")
|
|
101
|
+
return ".*";
|
|
100
102
|
if (part === "*")
|
|
101
103
|
return "[^/]*";
|
|
102
104
|
return part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|