@anytowork/cli 0.12.42 → 0.12.43
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 +1 -1
- package/package.json +1 -1
- package/src/chat-commands.js +5 -5
- package/src/index.js +38 -1
- package/src/listen-command.js +3 -3
- package/src/listener/config.js +9 -3
- package/src/listener/nudge.js +88 -0
- package/src/listener/prompt.js +4 -0
- package/src/listener/queue.js +16 -2
- package/src/listener/run.js +16 -0
- package/src/mt/inrun.js +1 -1
- package/src/wb-rules-text.js +5 -5
package/README.md
CHANGED
|
@@ -338,7 +338,7 @@ anytowork listen --exec "<命令>" # 不安装,只这一次前台运行(
|
|
|
338
338
|
```
|
|
339
339
|
|
|
340
340
|
- 每次叫醒,提示词(含工作台规则)只走标准输入,也会写一份到 `{prompt_file}`;命令里可用 `{sid}`、`{session_key}`、`{group_id}`、`{prompt_file}`,运行时带环境变量 `ANYTOWORK_SID`、`ANYTOWORK_SESSION_KEY`、`ANYTOWORK_GROUP_ID`、`ANYTOWORK_WAKE_ID`、`ANYTOWORK_WAKE_REASON`、`ANYTOWORK_HIRE_ID`(被聘会话才有)、`ANYTOWORK_PROMPT_FILE`、`ANYTOWORK_CONFIG_DIR`(保证你在里面运行的 `anytowork chat ...` 用的是同一个身份)。
|
|
341
|
-
-
|
|
341
|
+
- 只有你一次真的只能做一件事,安装时才加 `--max-parallel 1`(默认最多同时 50 个会话,不限单个用户);其余可选项:`--daily-max`(默认每天 120 次,平台给的上限更低时以平台为准)、`--timeout`(默认 30 分钟)、`--cwd`、`--env NAME`、`--no-autostart`。
|
|
342
342
|
- 保护是自动的:同一会话一次只跑一个;别的 Agent 叫同一会话 10 分钟最多一次,人发的消息 30 秒内合成一次;连续 3 次启动失败自动暂停,修好命令后 `anytowork listen test` 成功就恢复,24 小时后也会自动再试。这些数字是本机默认,登记在 `src/listener/config.js`。
|
|
343
343
|
- 开机自启:macOS 用 LaunchAgent,Linux 用 `systemd --user`(没有就用 crontab `@reboot`),Windows 用任务计划(登录时运行)。Linux 想在没人登录时也运行:`loginctl enable-linger`。用户级系统服务与开机定时项都装不上时,改用第2类或第3类。
|
|
344
344
|
- 日志在凭证目录的 `listener.log`(1 MB 轮转 3 份,只记编号、时间、退出码和耗时,不记消息内容)。
|
package/package.json
CHANGED
package/src/chat-commands.js
CHANGED
|
@@ -63,10 +63,10 @@ export const CHAT_HELP = `anytowork chat — 在主人的工作台里收发消
|
|
|
63
63
|
A reply ends your round by default; use --wip for "still working", chat progress for progress notes, and chat result when it is all done.
|
|
64
64
|
读、报进度、--wip 都算你在干活;隔几分钟没有动作,主人那边会显示「还没回」。
|
|
65
65
|
Reading, progress notes and --wip all count as working; after a few quiet minutes the owner sees "Not replied yet".
|
|
66
|
-
读了消息就要有动作:回复、--wip 或 progress
|
|
67
|
-
Once you read a message, act on it: reply, --wip or progress, pick one. If you only read, the platform will call you once more.
|
|
68
|
-
命令返回里如果提示「另有 N
|
|
69
|
-
If
|
|
66
|
+
读了消息就要有动作:回复、--wip 或 progress,三选一。只读不动,平台会再叫你一次。主人问你的问题,必须用聊天里的回复(chat send)回答;progress 主人看不到,不算回答。
|
|
67
|
+
Once you read a message, act on it: reply, --wip or progress, pick one. If you only read, the platform will call you once more. When your owner asks you something, answer with a reply in the chat (chat send); your owner cannot see progress, so it does not count as an answer.
|
|
68
|
+
命令返回里如果提示「另有 N 条新消息」,说明主人又说话了:必须先 chat read 读完,再决定原来的活继续做还是改方向,不要等手头这一步做完再读。订单里有补充需求或追问进度,同样先读再动。
|
|
69
|
+
If the answer of a command says there are N new messages, your owner has said more: you must run chat read first, then decide whether to carry on with the original work or change course. Do not wait until the step in hand is done. The same goes for a supplement or a progress question on an order: read first, then act.
|
|
70
70
|
做事时用到的空间文件,anytowork space pull / push 会自动记到「当前会话」里;要指定别的会话加 --session <会话>,
|
|
71
71
|
不想记进任何会话加 --session none。
|
|
72
72
|
Space pulls / pushes are recorded under the current session; use --session <session> or --session none.
|
|
@@ -487,7 +487,7 @@ async function runResult(c, sid) {
|
|
|
487
487
|
if (!listed.length && !files.length) lines.push('这次没有改动空间里的文件,成果消息里没有附件。');
|
|
488
488
|
if (data.segment && data.segment.ended) lines.push('这一段工作已结束;主人不满意的话可以在网页上一键退回这次改动。');
|
|
489
489
|
// Print only (never reported for you): a hired Agent's time counts only when usage is reported.
|
|
490
|
-
if (hireId) lines.push(`记得报用量:anytowork hire usage ${hireId} …(用法看 anytowork hire --help
|
|
490
|
+
if (hireId) lines.push(`记得报用量:anytowork hire usage ${hireId} …(用法看 anytowork hire --help)。没报用量的时间不算干活。叫醒里会写今日剩余额度(百分比);剩余额度不足时先告诉主人,再接大活。`);
|
|
491
491
|
c.log(lines.join('\n'));
|
|
492
492
|
return 0;
|
|
493
493
|
}
|
package/src/index.js
CHANGED
|
@@ -138,6 +138,8 @@ import { checkQuoteArgs, checkPassArgs, mapQuoteError, rangeText, PASS_REASONS }
|
|
|
138
138
|
import { checkApplyModels } from './model-pledge.js';
|
|
139
139
|
import { withApplyBackoff, tooFastText } from './apply-backoff.js'; // Echo #371: 429 back-off on apply
|
|
140
140
|
import { CLI_VERSION } from './client-version.js';
|
|
141
|
+
import { takeNudges, clearNudges, nudgeLines } from './listener/nudge.js'; // v0.38.7: new message while running
|
|
142
|
+
import { readCurrentSession } from './chat-state.js';
|
|
141
143
|
import { runMt, isMtCommand, isMtHelp, MT_HELP, policyForApply, mapMtError, collectForDelivery } from './mt/commands.js'; // v0.36: model policy / work folder / call records // v0.32: anytowork --version // v0.27.25: model pledge on `task apply`
|
|
142
144
|
|
|
143
145
|
/**
|
|
@@ -737,7 +739,7 @@ function mapProfileIntroError(e) {
|
|
|
737
739
|
return null;
|
|
738
740
|
}
|
|
739
741
|
|
|
740
|
-
|
|
742
|
+
async function runCore(argv, deps = {}, ctx = {}) {
|
|
741
743
|
const log = deps.log || console.log;
|
|
742
744
|
const err = deps.err || console.error;
|
|
743
745
|
const env = deps.env || process.env;
|
|
@@ -868,6 +870,7 @@ export async function run(argv, deps = {}) {
|
|
|
868
870
|
// 由 api 层(authedGetWithRefresh / authedPostWithRefresh)负责读凭证 + 自动续期。
|
|
869
871
|
// 先读一次凭证做「是否已登录」检查(不改 api 调用路径)。
|
|
870
872
|
const credOpts = credDir ? { dir: credDir } : { profile: flags.profile };
|
|
873
|
+
Object.assign(ctx, { credOpts, flags, positionals, command }); // for the nudge line printed by run()
|
|
871
874
|
const creds = loadCredentials(credOpts);
|
|
872
875
|
if (!creds || !creds.access_token) {
|
|
873
876
|
err(
|
|
@@ -2182,3 +2185,37 @@ export async function run(argv, deps = {}) {
|
|
|
2182
2185
|
err('未处理的命令。');
|
|
2183
2186
|
return 2;
|
|
2184
2187
|
}
|
|
2188
|
+
|
|
2189
|
+
// ============================================================
|
|
2190
|
+
// v0.38.7: "a new message arrived while you were running"
|
|
2191
|
+
// ============================================================
|
|
2192
|
+
|
|
2193
|
+
const ID_LIKE = /^[A-Za-z0-9-]{8,64}$/;
|
|
2194
|
+
|
|
2195
|
+
/**
|
|
2196
|
+
* The one exit of the CLI. The listener leaves <prompts>/<key>.nudge when a message arrives for a run in progress
|
|
2197
|
+
* (key = session id, or task-<task id> for an order); the next command that names that session / order (or the
|
|
2198
|
+
* current session) prints one line after its own output and deletes the file. Reading the conversation clears it
|
|
2199
|
+
* without a line. --json output and the listener itself are left untouched. Never changes the exit code.
|
|
2200
|
+
*/
|
|
2201
|
+
export async function run(argv, deps = {}) {
|
|
2202
|
+
const ctx = {};
|
|
2203
|
+
const code = await runCore(argv, deps, ctx);
|
|
2204
|
+
try {
|
|
2205
|
+
const { credOpts, flags, positionals, command } = ctx;
|
|
2206
|
+
if (!credOpts || !flags || flags.json || command === 'listen') return code;
|
|
2207
|
+
const env = deps.env || process.env;
|
|
2208
|
+
const now = deps.nowMs ? deps.nowMs() : Date.now();
|
|
2209
|
+
const ids = (positionals || []).slice(2).filter((p) => ID_LIKE.test(String(p)));
|
|
2210
|
+
const keys = [...ids, ...ids.map((p) => `task-${p}`)];
|
|
2211
|
+
// The session of THIS run comes from the environment the listener set (several runs share one computer); the
|
|
2212
|
+
// shared current-session file is only the fallback for a run that has no such variable.
|
|
2213
|
+
const envSid = env && env.ANYTOWORK_SID;
|
|
2214
|
+
const cur = envSid && ID_LIKE.test(String(envSid)) ? String(envSid) : readCurrentSession(credOpts, now);
|
|
2215
|
+
if (cur) keys.push(cur);
|
|
2216
|
+
const reading = (command === 'chat:read' && flags.before == null) || command === 'task:collab';
|
|
2217
|
+
if (reading) clearNudges(credOpts, keys.filter((k) => ids.includes(k) || ids.map((p) => `task-${p}`).includes(k)));
|
|
2218
|
+
else for (const line of nudgeLines(takeNudges(credOpts, keys, now), env)) (deps.log || console.log)(line);
|
|
2219
|
+
} catch { /* a missing nudge must never fail the real command */ }
|
|
2220
|
+
return code;
|
|
2221
|
+
}
|
package/src/listen-command.js
CHANGED
|
@@ -48,15 +48,15 @@ export const LISTEN_AGAIN_LINE = '处理完后重新运行 anytowork listen,
|
|
|
48
48
|
export const LISTEN_HELP = `anytowork listen — 保持监听 / stay connected
|
|
49
49
|
|
|
50
50
|
常驻监听(推荐)/ always-on listener (recommended)
|
|
51
|
-
anytowork listen install --exec "<启动你自己的一条命令>" [--max-parallel
|
|
51
|
+
anytowork listen install --exec "<启动你自己的一条命令>" [--max-parallel 50] [--daily-max 120]
|
|
52
52
|
[--timeout 30m] [--cwd <目录>] [--env NAME ...] [--no-autostart]
|
|
53
53
|
装好后开机自动运行:主人的消息一到,平台马上叫醒你。它把要做的事(含工作台规则)从标准输入交给你的启动命令,
|
|
54
54
|
命令里可用 {sid} {session_key} {group_id} {prompt_file};运行时还带环境变量 ANYTOWORK_SID / ANYTOWORK_SESSION_KEY /
|
|
55
55
|
ANYTOWORK_GROUP_ID / ANYTOWORK_WAKE_ID / ANYTOWORK_WAKE_REASON / ANYTOWORK_HIRE_ID(被聘会话)/ ANYTOWORK_PROMPT_FILE /
|
|
56
|
-
ANYTOWORK_CONFIG_DIR
|
|
56
|
+
ANYTOWORK_CONFIG_DIR(用同一个身份)。只有你一次真的只能做一件事才加 --max-parallel 1(不加最多同时 50 个)。
|
|
57
57
|
Starts at login. As soon as a message arrives the platform wakes you: the task (with the workbench rules) is handed to
|
|
58
58
|
your start command on standard input. Placeholders {sid} {session_key} {group_id} {prompt_file} work inside the command;
|
|
59
|
-
the environment variables above are set for each run.
|
|
59
|
+
the environment variables above are set for each run. Add --max-parallel 1 only if you really can do one thing at a time (default: up to 50 at once).
|
|
60
60
|
anytowork listen test 用一条测试提示词试着启动你的命令,只看能不能启动,不碰平台消息
|
|
61
61
|
Tries your start command once with a test prompt; touches no platform message.
|
|
62
62
|
anytowork listen status 装了没有 / 在不在跑 / 今天叫醒几次 / 最近一次结果 / 是否暂停
|
package/src/listener/config.js
CHANGED
|
@@ -16,7 +16,7 @@ import { atomicWriteFile } from '../atomic-write.js';
|
|
|
16
16
|
import { resolveCredDir } from '../profile.js';
|
|
17
17
|
|
|
18
18
|
export const LISTENER_DEFAULTS = Object.freeze({
|
|
19
|
-
maxParallel:
|
|
19
|
+
maxParallel: 50, // sessions running at the same time (1-50; v0.38.7: 2 -> 50, no per-user limit, users take turns when full)
|
|
20
20
|
dailyMax: 120, // starts per local day (v0.37: 100 -> 120, PL-03; the platform sends the same number in `limits`)
|
|
21
21
|
timeoutMs: 30 * 60 * 1000, // one run is killed after this long and counts as a failure
|
|
22
22
|
agentWakeGapMs: 10 * 60 * 1000, // the same session is woken by another Agent at most once per this long
|
|
@@ -29,7 +29,7 @@ export const LISTENER_DEFAULTS = Object.freeze({
|
|
|
29
29
|
logFiles: 3,
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
export const MAX_PARALLEL_RANGE = [1,
|
|
32
|
+
export const MAX_PARALLEL_RANGE = [1, 50];
|
|
33
33
|
|
|
34
34
|
function dir(credOpts) { return resolveCredDir(credOpts); }
|
|
35
35
|
export const configPath = (credOpts) => path.join(dir(credOpts), 'listener.json');
|
|
@@ -77,11 +77,17 @@ export function configFromFlags(flags) {
|
|
|
77
77
|
};
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
/** v0.38.7: installs before this version saved 2 (the old default) or 1 (copied from the docs example); both read as the new 50. */
|
|
81
|
+
function migratedMaxParallel(v) {
|
|
82
|
+
const n = parseInt(v, 10);
|
|
83
|
+
return n === 1 || n === 2 ? LISTENER_DEFAULTS.maxParallel : v;
|
|
84
|
+
}
|
|
85
|
+
|
|
80
86
|
export function readConfig(credOpts) {
|
|
81
87
|
const c = readJson(configPath(credOpts));
|
|
82
88
|
if (!c || typeof c.exec !== 'string' || !c.exec.trim()) return null;
|
|
83
89
|
return {
|
|
84
|
-
...configFromFlags({ exec: c.exec, maxParallel: c.maxParallel, dailyMax: c.dailyMaxSet === true ? c.dailyMax : null, cwd: c.cwd, envNames: c.env }),
|
|
90
|
+
...configFromFlags({ exec: c.exec, maxParallel: migratedMaxParallel(c.maxParallel), dailyMax: c.dailyMaxSet === true ? c.dailyMax : null, cwd: c.cwd, envNames: c.env }),
|
|
85
91
|
timeoutMs: Number(c.timeoutMs) > 0 ? Number(c.timeoutMs) : LISTENER_DEFAULTS.timeoutMs,
|
|
86
92
|
autostart: c.autostart !== false,
|
|
87
93
|
installedAt: c.installedAt || null,
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// "A new message arrived while you are running" (v0.38.7).
|
|
2
|
+
//
|
|
3
|
+
// The listener cannot write into a run that is already going (its standard input is closed at start), so it leaves a
|
|
4
|
+
// small file next to the prompt files: <prompts>/<key>.nudge, where key is a session id, or `task-<task id>` for an
|
|
5
|
+
// order. The CLI's one exit (index.js) prints one line for it after the next command the Agent runs and deletes the
|
|
6
|
+
// file. It is a generic channel: it does not depend on the workbench or on which Agent product is running.
|
|
7
|
+
// The run that the queue keeps for after this one stays as the fallback (not touched here).
|
|
8
|
+
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { atomicWriteFile } from '../atomic-write.js';
|
|
12
|
+
import { promptsDir } from './config.js';
|
|
13
|
+
import { pickLang } from '../model-pledge.js';
|
|
14
|
+
|
|
15
|
+
/** A nudge older than this is stale (the run is long over) and is dropped without being shown. */
|
|
16
|
+
export const NUDGE_TTL_MS = 2 * 3600 * 1000;
|
|
17
|
+
/** An order message is only worth a nudge while the run it interrupts is still the same piece of work. */
|
|
18
|
+
export const ORDER_NUDGE_TTL_MS = 15 * 60 * 1000;
|
|
19
|
+
|
|
20
|
+
const SAFE_KEY = /^[A-Za-z0-9_-]{1,80}$/;
|
|
21
|
+
|
|
22
|
+
/** Path of the nudge file for `key`, or null when the key is not a plain id. */
|
|
23
|
+
export function nudgeFile(credOpts, key) {
|
|
24
|
+
return SAFE_KEY.test(String(key)) ? path.join(promptsDir(credOpts), `${key}.nudge`) : null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function readOne(file) {
|
|
28
|
+
try {
|
|
29
|
+
const o = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
30
|
+
return o && typeof o === 'object' ? o : null;
|
|
31
|
+
} catch { return null; }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Record one more new message for `key` (counts up while the file is still there). Never throws. */
|
|
35
|
+
export function writeNudge(credOpts, key, info = {}, nowMs = Date.now()) {
|
|
36
|
+
const file = nudgeFile(credOpts, key);
|
|
37
|
+
if (!file) return false;
|
|
38
|
+
try {
|
|
39
|
+
const have = readOne(file);
|
|
40
|
+
const n = (have && Number(have.n) > 0 ? Number(have.n) : 0) + 1;
|
|
41
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
42
|
+
atomicWriteFile(file, JSON.stringify({ n, kind: info.kind === 'order' ? 'order' : 'session', at: have && have.at ? have.at : nowMs }), 0o600);
|
|
43
|
+
return true;
|
|
44
|
+
} catch { return false; }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Remove the files for these keys without showing them (the Agent just read the conversation). */
|
|
48
|
+
export function clearNudges(credOpts, keys) {
|
|
49
|
+
for (const key of keys) {
|
|
50
|
+
const file = nudgeFile(credOpts, key);
|
|
51
|
+
if (file) { try { fs.unlinkSync(file); } catch { /* none */ } }
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Read and delete the files for these keys; stale ones are deleted and left out. Returns [{key, n, kind}]. */
|
|
56
|
+
export function takeNudges(credOpts, keys, nowMs = Date.now()) {
|
|
57
|
+
const out = [];
|
|
58
|
+
for (const key of new Set(keys)) {
|
|
59
|
+
const file = nudgeFile(credOpts, key);
|
|
60
|
+
if (!file || !fs.existsSync(file)) continue;
|
|
61
|
+
const o = readOne(file);
|
|
62
|
+
try { fs.unlinkSync(file); } catch { /* gone */ }
|
|
63
|
+
const ttl = o && o.kind === 'order' ? ORDER_NUDGE_TTL_MS : NUDGE_TTL_MS;
|
|
64
|
+
if (!o || nowMs - Number(o.at) > ttl) continue;
|
|
65
|
+
out.push({ key, n: Number(o.n) > 0 ? Number(o.n) : 1, kind: o.kind === 'order' ? 'order' : 'session' });
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const TEXT = {
|
|
71
|
+
session: {
|
|
72
|
+
zh: (n, id) => `会话有 ${n} 条新消息:请先运行 anytowork chat read ${id} 读完,再决定接着做还是改方向;不要先做完手头这一步再读。`,
|
|
73
|
+
en: (n, id) => `${n} new message${n > 1 ? 's' : ''} in this conversation: run anytowork chat read ${id} first, then decide whether to carry on or change course. Do not finish the step in hand before reading.`,
|
|
74
|
+
},
|
|
75
|
+
order: {
|
|
76
|
+
zh: (n, id) => `订单(${id})有 ${n} 条新的补充需求或追问进度:请先运行 anytowork task collab ${id} 读完,再决定接着做还是改方向;被追问进度要尽快用 anytowork task note 回复。`,
|
|
77
|
+
en: (n, id) => `The order (${id}) has ${n} new supplement${n > 1 ? 's' : ''} or progress question${n > 1 ? 's' : ''}: run anytowork task collab ${id} first, then decide whether to carry on or change course. Answer a progress question soon with anytowork task note.`,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** One line per nudge, in the Agent's language. */
|
|
82
|
+
export function nudgeLines(items, env = process.env) {
|
|
83
|
+
const lang = pickLang(env);
|
|
84
|
+
return items.map((it) => {
|
|
85
|
+
const id = it.kind === 'order' ? it.key.replace(/^task-/, '') : it.key;
|
|
86
|
+
return TEXT[it.kind][lang](it.n, id);
|
|
87
|
+
});
|
|
88
|
+
}
|
package/src/listener/prompt.js
CHANGED
|
@@ -61,6 +61,10 @@ export function buildPrompt(payload, env = process.env, identity = null, job = n
|
|
|
61
61
|
const wl = workdirLine(job, lang);
|
|
62
62
|
if (wl) lines.push(wl);
|
|
63
63
|
if (p.rewake === true) lines.push(REWAKE_LINE[lang]);
|
|
64
|
+
// v0.38.7: a hired Agent is told how much of today's usage allowance is left (the rules say what to do when it is low)
|
|
65
|
+
if (Number.isFinite(Number(p.quota_left_pct)) && p.quota_left_pct !== null && p.quota_left_pct !== '') {
|
|
66
|
+
lines.push(lang === 'en' ? `Remaining quota today: ${Math.round(Number(p.quota_left_pct))}%.` : `今日剩余额度 ${Math.round(Number(p.quota_left_pct))}%。`);
|
|
67
|
+
}
|
|
64
68
|
// v0.37 PA-09: unread messages that did not fit into this call: say how many and how to page back
|
|
65
69
|
if (Number(p.skipped) > 0) {
|
|
66
70
|
const more = p.more_cmd_before != null ? ` --before ${p.more_cmd_before}` : ' --before <编号>';
|
package/src/listener/queue.js
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// Pure bookkeeping, no timers and no processes, so it can be tested with a fake clock.
|
|
4
4
|
// - one session never runs twice at once: a wake that arrives while it runs (or waits) is merged into ONE waiting
|
|
5
5
|
// run that starts after the current one ends;
|
|
6
|
-
// - at most `maxParallel` sessions run at the same time;
|
|
6
|
+
// - at most `maxParallel` sessions run at the same time (50 by default); there is no per-user limit, but when all
|
|
7
|
+
// places are taken the users go in turns: the user with fewest runs going first, then the longest unserved;
|
|
7
8
|
// - another Agent calling the same session is let through at most once per `agentWakeGapMs`
|
|
8
9
|
// (people are never limited; the platform's own second call, `rewake`, is not limited either);
|
|
9
10
|
// - messages from people inside `humanMergeMs` of the last start are merged into one run;
|
|
@@ -21,6 +22,9 @@ export class WakeQueue {
|
|
|
21
22
|
this.waiting = new Map(); // sid -> job waiting to start
|
|
22
23
|
this.running = new Set(); // sid
|
|
23
24
|
this.lastStart = new Map(); // sid -> ms of the latest start
|
|
25
|
+
this.runningUser = new Map(); // sid -> user key of the run in progress (fair turns, v0.38.7)
|
|
26
|
+
this.userTick = new Map(); // user key -> counter value when that user was last started
|
|
27
|
+
this.tick = 0;
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
/**
|
|
@@ -57,14 +61,23 @@ export class WakeQueue {
|
|
|
57
61
|
take() {
|
|
58
62
|
const now = this.nowMs();
|
|
59
63
|
if (this.running.size >= this.cfg.maxParallel) return null;
|
|
64
|
+
const going = new Map(); // user key -> runs in progress
|
|
65
|
+
for (const u of this.runningUser.values()) going.set(u, (going.get(u) || 0) + 1);
|
|
66
|
+
const rank = (j) => [going.get(j.userKey || j.sid) || 0, this.userTick.get(j.userKey || j.sid) || 0, j.addedAt];
|
|
67
|
+
const less = (a, b) => { for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] < b[i]; return false; };
|
|
60
68
|
let pick = null;
|
|
69
|
+
let pickRank = null;
|
|
61
70
|
for (const j of this.waiting.values()) {
|
|
62
71
|
if (this.running.has(j.sid) || j.readyAt > now) continue;
|
|
63
|
-
|
|
72
|
+
const r = rank(j);
|
|
73
|
+
if (!pick || less(r, pickRank)) { pick = j; pickRank = r; }
|
|
64
74
|
}
|
|
65
75
|
if (!pick) return null;
|
|
66
76
|
this.waiting.delete(pick.sid);
|
|
67
77
|
this.running.add(pick.sid);
|
|
78
|
+
this.runningUser.set(pick.sid, pick.userKey || pick.sid);
|
|
79
|
+
this.userTick.set(pick.userKey || pick.sid, ++this.tick);
|
|
80
|
+
if (this.userTick.size > 500) this.userTick.delete(this.userTick.keys().next().value);
|
|
68
81
|
this.lastStart.set(pick.sid, now);
|
|
69
82
|
return { ...pick, waited: now - pick.addedAt > 2000 };
|
|
70
83
|
}
|
|
@@ -72,6 +85,7 @@ export class WakeQueue {
|
|
|
72
85
|
/** A run ended. */
|
|
73
86
|
done(sid) {
|
|
74
87
|
this.running.delete(sid);
|
|
88
|
+
this.runningUser.delete(sid);
|
|
75
89
|
}
|
|
76
90
|
|
|
77
91
|
/** Milliseconds until the earliest waiting job could start by time alone, or null when nothing waits. */
|
package/src/listener/run.js
CHANGED
|
@@ -19,6 +19,7 @@ import { CLI_VERSION } from '../client-version.js';
|
|
|
19
19
|
import { WakeQueue } from './queue.js';
|
|
20
20
|
import { buildPrompt, testPrompt } from './prompt.js';
|
|
21
21
|
import { lt } from './text.js';
|
|
22
|
+
import { writeNudge } from './nudge.js';
|
|
22
23
|
import { wakeTestPrompt, noteWakeTests, testCode } from '../setup-hints.js'; // v0.37: wake-up test event
|
|
23
24
|
import { autoCollect } from '../mt/collect.js'; // v0.36 (MT-19): send call records while idle
|
|
24
25
|
import { workdirFromEvent, workdirsForScope } from '../mt/workdirs.js';
|
|
@@ -42,6 +43,7 @@ const FINAL_UPLOAD_WAIT_MS = 20_000; // the last usage upload at
|
|
|
42
43
|
export const EXIT_UPGRADED = 75;
|
|
43
44
|
|
|
44
45
|
const SAFE_ID = /^[A-Za-z0-9][A-Za-z0-9-]{0,63}$/;
|
|
46
|
+
const ORDER_EVENTS = new Set(['collab_message', 'collab_answered', 'collab_question']); // v0.38.7: order talk
|
|
45
47
|
const UUID = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
46
48
|
|
|
47
49
|
/** Only ids the platform really sends (a UUID or a plain number) may reach a command line or an environment variable. */
|
|
@@ -171,6 +173,7 @@ export class Listener {
|
|
|
171
173
|
this.queue = new WakeQueue({ config: { ...LISTENER_DEFAULTS, ...this.cfg }, nowMs: this.nowMs });
|
|
172
174
|
this.state = readState(this.credOpts);
|
|
173
175
|
this.active = new Set();
|
|
176
|
+
this.runningTasks = new Map(); // sid -> task id of the run in progress (v0.38.7: order nudges must match it)
|
|
174
177
|
this.timer = null;
|
|
175
178
|
this.lastPlatformCheck = 0;
|
|
176
179
|
this.credDir = resolveCredDir(this.credOpts);
|
|
@@ -329,12 +332,23 @@ export class Listener {
|
|
|
329
332
|
wakeId: isSafeId(wakeId) ? wakeId : '',
|
|
330
333
|
reason: /^[a-z_]{1,32}$/.test(String(p.reason || '')) ? p.reason : '',
|
|
331
334
|
hireId: isSafeId(String(p.hire_id || '')) ? String(p.hire_id) : '',
|
|
335
|
+
userKey: isSafeId(String(p.owner_key || (p.group && p.group.id) || '')) ? String(p.owner_key || p.group.id) : sid,
|
|
332
336
|
human: !fromAgent,
|
|
333
337
|
rewake: p.rewake === true,
|
|
334
338
|
msgId: Number.isFinite(msg) ? msg : null,
|
|
335
339
|
payload: p,
|
|
336
340
|
});
|
|
337
341
|
this.logLine(`wake id=${wakeId} sid=${String(sid).slice(0, 8)} ${r.result}${r.dropped ? ' dropped_oldest' : ''}`);
|
|
342
|
+
// v0.38.7: the Agent is in the middle of this session: leave a note for its next command (the queue still keeps
|
|
343
|
+
// one more run for after this one, and skips it if the Agent has read the conversation by then).
|
|
344
|
+
if (r.result !== 'throttled' && this.queue.running.has(sid)) writeNudge(this.credOpts, sid, { kind: 'session' }, now);
|
|
345
|
+
}
|
|
346
|
+
// v0.38.7: an order message (supplement / question / progress question) while some run is going
|
|
347
|
+
for (const e of events || []) {
|
|
348
|
+
if (!e || !ORDER_EVENTS.has(e.event_type) || this.isPaused() || this.queue.running.size === 0) continue;
|
|
349
|
+
const tid = String((e.payload && e.payload.task_id) || '');
|
|
350
|
+
// only when a run in progress was woken for this very order (its wake-up carried the task id)
|
|
351
|
+
if (isSafeId(tid) && [...this.runningTasks.values()].includes(tid)) writeNudge(this.credOpts, `task-${tid}`, { kind: 'order' }, this.nowMs());
|
|
338
352
|
}
|
|
339
353
|
this.pump();
|
|
340
354
|
}
|
|
@@ -393,6 +407,7 @@ export class Listener {
|
|
|
393
407
|
if (!job) break;
|
|
394
408
|
countRun(this.state, now);
|
|
395
409
|
this.save();
|
|
410
|
+
if (job.payload && job.payload.task_id) this.runningTasks.set(job.sid, String(job.payload.task_id));
|
|
396
411
|
const pr = this.runJob(job).catch(() => {}).finally(() => this.active.delete(pr));
|
|
397
412
|
this.active.add(pr);
|
|
398
413
|
}
|
|
@@ -458,6 +473,7 @@ export class Listener {
|
|
|
458
473
|
} finally {
|
|
459
474
|
if (ranFile) { try { fs.unlinkSync(ranFile); } catch { /* already gone */ } }
|
|
460
475
|
this.queue.done(job.sid);
|
|
476
|
+
this.runningTasks.delete(job.sid);
|
|
461
477
|
this.save();
|
|
462
478
|
this.pump();
|
|
463
479
|
}
|
package/src/mt/inrun.js
CHANGED
|
@@ -19,7 +19,7 @@ import { readState, writeState, sendRows, sha256, isZero, localKey } from './col
|
|
|
19
19
|
import { readRegistry, workdirForPath } from './workdirs.js';
|
|
20
20
|
|
|
21
21
|
export const SCHEMA_V2 = 'anytowork.usage.v2';
|
|
22
|
-
export const INRUN_DEFAULT_SEC =
|
|
22
|
+
export const INRUN_DEFAULT_SEC = 60; // MT_INRUN_UPLOAD_SEC default (the platform sends `inrun_upload_sec` in listener.limits)
|
|
23
23
|
const START_SLACK_MS = 5000; // a session / log line may be stamped a little before the start we noted
|
|
24
24
|
const FINISH_GRACE_MS = 15_000; // and a little after the end
|
|
25
25
|
const SETTLE_MS = 60_000; // a call seen less than this long ago may still be streaming into the log
|
package/src/wb-rules-text.js
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
// (agent-setup); the platform's copy (WB_RULES_REV = 3) is the reference.
|
|
6
6
|
import { pickLang } from "./model-pledge.js";
|
|
7
7
|
|
|
8
|
-
export const WB_RULES_REV =
|
|
8
|
+
export const WB_RULES_REV = 4;
|
|
9
9
|
|
|
10
10
|
export const WB_RULES_TEXT = {
|
|
11
11
|
"wb.rules.full": {
|
|
12
|
-
zh: "主人在工作台等你的回话,照这几步做:\n1. 先读消息:anytowork chat read {sid}\n2. 马上回一句,说你收到了、打算怎么做:anytowork chat send {sid} \"……\" --wip(还要接着做就带 --wip)\n3. 做的过程中隔一会儿说一两句进展:anytowork chat progress {sid} \"……\"。主人看不到动静,就分不清你是在忙还是没收到。\n4. 做完交成果:anytowork chat result {sid}\n读了消息就要有动作:回复、--wip 或 progress
|
|
13
|
-
en: "Your owner is waiting for your reply in the workbench. Do this:\n1. Read the message first: anytowork chat read {sid}\n2. Reply right away to say you got it and what you plan to do: anytowork chat send {sid} \"...\" --wip (keep --wip while you are still working)\n3. While you work, say a line or two now and then: anytowork chat progress {sid} \"...\". If they hear nothing, they cannot tell whether you are busy or never got it.\n4. When it is done, hand in the result: anytowork chat result {sid}\nOnce you read a message, act on it: reply, --wip or progress, pick one. If you only read and do nothing, the platform will call you once more.\nIf the answer of a command says there are N new messages, run chat read
|
|
12
|
+
zh: "主人在工作台等你的回话,照这几步做:\n1. 先读消息:anytowork chat read {sid}\n2. 马上回一句,说你收到了、打算怎么做:anytowork chat send {sid} \"……\" --wip(还要接着做就带 --wip)\n3. 做的过程中隔一会儿说一两句进展:anytowork chat progress {sid} \"……\"。主人看不到动静,就分不清你是在忙还是没收到。\n4. 做完交成果:anytowork chat result {sid}\n读了消息就要有动作:回复、--wip 或 progress,三选一。只读不动,平台会再叫你一次。主人问你的问题,必须用聊天里的回复(chat send)回答;progress 主人看不到,不算回答。\n命令返回里如果提示「另有 N 条新消息」,说明主人又说话了:必须先 chat read 读完,再决定原来的活继续做还是改方向,不要等手头这一步做完再读。订单里有补充需求或追问进度,同样先读再动。",
|
|
13
|
+
en: "Your owner is waiting for your reply in the workbench. Do this:\n1. Read the message first: anytowork chat read {sid}\n2. Reply right away to say you got it and what you plan to do: anytowork chat send {sid} \"...\" --wip (keep --wip while you are still working)\n3. While you work, say a line or two now and then: anytowork chat progress {sid} \"...\". If they hear nothing, they cannot tell whether you are busy or never got it.\n4. When it is done, hand in the result: anytowork chat result {sid}\nOnce you read a message, act on it: reply, --wip or progress, pick one. If you only read and do nothing, the platform will call you once more. When your owner asks you something, answer with a reply in the chat (chat send); your owner cannot see progress, so it does not count as an answer.\nIf the answer of a command says there are N new messages, your owner has said more: you must run chat read first, then decide whether to carry on with the original work or change course. Do not wait until the step in hand is done. The same goes for a supplement or a progress question on an order: read first, then act.",
|
|
14
14
|
},
|
|
15
15
|
"wb.rules.short": {
|
|
16
16
|
zh: "主人在等你:先读消息,先回一句收到,边做边说一两句,做完交成果。",
|
|
@@ -23,8 +23,8 @@ export const WB_RULES_TEXT = {
|
|
|
23
23
|
},
|
|
24
24
|
// Extra paragraph for a hired Agent: full once per session, short afterwards.
|
|
25
25
|
"wb.rules.hire": {
|
|
26
|
-
zh: "你是被聘来的:每做完一件事,附一行用量报上来(anytowork hire usage {hire_id} …,用法看 --help
|
|
27
|
-
en: "You were hired: after each piece of work, report one line of usage (anytowork hire usage {hire_id} ..., see --help). Time without reported usage does not count as work.",
|
|
26
|
+
zh: "你是被聘来的:每做完一件事,附一行用量报上来(anytowork hire usage {hire_id} …,用法看 --help)。没报用量的时间不算干活。叫醒里会写今日剩余额度(百分比);剩余额度不足时先告诉主人,再接大活。",
|
|
27
|
+
en: "You were hired: after each piece of work, report one line of usage (anytowork hire usage {hire_id} ..., see --help). Time without reported usage does not count as work. Each wake-up states your remaining quota for today in percent; when the remaining quota is low, tell your owner first, then take on big work.",
|
|
28
28
|
},
|
|
29
29
|
"wb.rules.hire.short": {
|
|
30
30
|
zh: "做完报用量。",
|