openany-cli-dev 0.1.4 → 0.1.5
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/package.json +1 -1
- package/skills/openany-provider/references/hermes-auto-files.md +121 -0
- package/skills/openany-provider/references/hermes-files.md +3 -1
- package/src/artifact-collector.js +72 -0
- package/src/artifact-files.js +45 -0
- package/src/cli.js +4 -0
- package/src/hermes-delivery.js +228 -0
- package/src/poll-artifacts.js +15 -44
- package/src/provider.js +4 -0
package/package.json
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# Hermes 自动收集文件并交付 OpenAny
|
|
2
|
+
|
|
3
|
+
本功能在 Hermes 原生异步轮询和 CLI 文件上传之上增加自动收集。本文描述本地实现;尚未部署、发布 npm 包或在服务商服务器启用。
|
|
4
|
+
|
|
5
|
+
## 运行链路
|
|
6
|
+
|
|
7
|
+
OpenAny 网关向执行端发送原生 `POST /v1/runs`,携带平台生成的任务 ID、截止时间和自动交付标记。本机 CLI 适配服务持久化准确的请求与任务映射,再调用 Hermes。执行完成后,从明确的成果清单或最终回复中的文件路径收集文件,冻结最终内容,复用 `seller tasks upload` 的实现上传。
|
|
8
|
+
|
|
9
|
+
平台继续轮询 `GET /v1/runs/{run_id}`。Hermes 成功、结果摘要非空、必交文件已通过存储校验时才结算。文件可以在运行结束后上传;截止时间沿用原任务,不自动延长。模型执行应预留上传和重试时间。上传本身不改变业务状态或金额。
|
|
10
|
+
|
|
11
|
+
这是独立的本机自动交付适配服务,采用 Deliverable Mode 的路径声明约定;不是简单打开 Hermes 的一个原生开关。无需修改 Hermes 源码。适配服务应使用与 Hermes 相同的系统运行用户,并与 Hermes 工具运行环境共享任务目录,并能读取该目录中的成果;容器运行时也必须保持路径一致。
|
|
12
|
+
|
|
13
|
+
## 商品配置
|
|
14
|
+
|
|
15
|
+
继续使用前端已有的输出合同 JSON 配置,增加 `artifact_delivery`:
|
|
16
|
+
|
|
17
|
+
```json
|
|
18
|
+
{
|
|
19
|
+
"format": "json",
|
|
20
|
+
"artifacts_required": true,
|
|
21
|
+
"artifact_delivery": "auto_cli"
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
完整异步配置示例:
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"adapter_type": "http_json",
|
|
30
|
+
"delivery_mode": "poll",
|
|
31
|
+
"timeout_secs": 600,
|
|
32
|
+
"output_contract": {
|
|
33
|
+
"format": "json",
|
|
34
|
+
"artifacts_required": true,
|
|
35
|
+
"artifact_delivery": "auto_cli"
|
|
36
|
+
},
|
|
37
|
+
"poll": {"interval_secs": 2}
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
服务接口仍为 `/v1/runs`,固定字段 `fixed_body` 继续指定业务 Skill。不同商品可以共享该服务接口、分别配置 Skill 和价格。无需为每个 Skill 增加上传命令。
|
|
42
|
+
|
|
43
|
+
省略 `artifact_delivery` 或明确使用 `manual_cli` 时,保留原有模型执行上传命令的方式。只有 Hermes 原生轮询且 `artifacts_required=true` 才允许设置交付策略。切换自动模式时应移除卖家自己曾加入 Skill 的手动上传规则,避免两个执行者同时上传。
|
|
44
|
+
|
|
45
|
+
## 服务商一次性配置
|
|
46
|
+
|
|
47
|
+
使用包含本功能的 CLI,检查 `--help` 中有 `seller hermes serve`。仓库源码可运行:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
node /path/to/openany-cli/bin/openany.js seller hermes serve -j
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
准备权限为 `0700` 的绝对路径目录,例如 `/home/ubuntu/openany-tasks` 和 `/home/ubuntu/openany-delivery-state`。两者不能相互包含。为该进程配置下列环境变量;密钥放在权限为 `0600` 的进程环境文件中,不写入 Skill、命令参数、固定字段或日志。
|
|
54
|
+
|
|
55
|
+
| 环境变量 | 含义 |
|
|
56
|
+
| --- | --- |
|
|
57
|
+
| `OPENANY_TASK_ROOT` | Hermes 和收集器共享的任务目录 |
|
|
58
|
+
| `OPENANY_HERMES_STATE_DIR` | 独立的私有持久化状态目录 |
|
|
59
|
+
| `OPENANY_HERMES_URL` | 固定的 Hermes API 根地址,例如 `http://127.0.0.1:8642` |
|
|
60
|
+
| `OPENANY_HERMES_API_KEY` | Hermes API 的 Bearer 密钥 |
|
|
61
|
+
| `OPENANY_DEV_SELLER_KEY` | 对应环境的服务商文件上传密钥 |
|
|
62
|
+
| `OPENANY_HERMES_PORT` | 本机监听端口,默认 `8790` |
|
|
63
|
+
|
|
64
|
+
Dev 包运行 `openany-dev seller hermes serve -j`,服务商凭据使用 `OPENANY_DEV_SELLER_KEY`;其余以上变量名不变。Dev 包固定访问 Dev 网关,正式包固定访问正式网关,不能混用。
|
|
65
|
+
|
|
66
|
+
适配服务只监听 `127.0.0.1`。准备启用时,将已有 HTTPS 反向代理的 `/v1/runs` 及其状态查询路由到该本机端口;如需枚举 Skill,`/v1/skills` 也可转发。适配服务仍校验 Hermes Bearer 密钥,使用同一密钥访问固定的 Hermes 地址;不接受请求指定其他目标地址。已有人机界面等其他路径应保持各自路由。服务路径包含额外前缀时由反向代理明确去除前缀。
|
|
67
|
+
|
|
68
|
+
首次运行会检查 `/v1/capabilities` 中 `features.runs_idempotency`:必须支持持久化幂等,保留时长覆盖任务截止时间,否则拒绝自动任务,避免接单不确定时重复执行模型。这里要求 Hermes API 工具在本机执行;远程工具沙箱需挂载同一任务目录。
|
|
69
|
+
|
|
70
|
+
本文只提供待启用的配置说明,本次不修改反向代理、服务进程或服务器配置。
|
|
71
|
+
|
|
72
|
+
## 成果声明
|
|
73
|
+
|
|
74
|
+
适配服务会向指令追加本次真实目录:`OPENANY_TASK_ROOT/<OpenAny任务UUID>/`。Agent 只需生成最终成果、提供非空业务摘要,并采用以下任一方式声明文件:
|
|
75
|
+
|
|
76
|
+
1. 推荐:在本任务目录写 `openany-artifacts.json`:
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{"files":["report.pdf","tables/data.csv"]}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
清单最多 32 项,支持目录内的相对路径或绝对路径,适合有空格的文件名。
|
|
83
|
+
2. 最终回复以普通文本列出成果的绝对路径。代码块和反引号中的路径会被忽略,避免将示例误当成果。带空格的路径应使用清单。
|
|
84
|
+
|
|
85
|
+
清单存在时以清单为准,清单错误或任一文件缺失不会回退为部分交付。只收集明确声明的成果,不扫描整个目录;拒绝其他任务目录、逃逸软链接、内部交付目录和不安全文件名。声明路径错误不会上传其他位置的文件。
|
|
86
|
+
|
|
87
|
+
单个支持的文件保留原格式;多个文件,或单个图片、HTML 等当前上传接口不直接接受的格式,会打成 ZIP。最终一个文件必须在 1–10,000,000 字节内,打包开销也计入限制。读取时检查文件身份、长度、修改时间及路径边界;生成尚未结束导致文件变化时,在截止时间内重试收集。
|
|
88
|
+
|
|
89
|
+
## 持久化、重试与完整性
|
|
90
|
+
|
|
91
|
+
- 精确请求在调用 Hermes 之前落盘并同步;OpenAny 任务 UUID 同时作为原生幂等键。丢失接单回执后仅重放同一请求,不生成新任务。
|
|
92
|
+
- 任务映射和状态写入独立状态目录。进程重启后加载未完成任务;禁止两个收集器同时使用该状态目录。
|
|
93
|
+
- 收集完整文件后,将快照原子发布到本任务的 `.openany-delivery/`,记录文件大小、类型和 SHA-256。ZIP 只创建一次,后续上传全部使用同一份快照。
|
|
94
|
+
- 上传复用 CLI 代码和服务商鉴权,校验服务端回执的任务 ID、附件 ID、大小、类型与 SHA-256。服务端另做对象存储读取与哈希校验。
|
|
95
|
+
- 网络错误、503、丢失回执等按退避重试,最长间隔 30 秒,截止时间后停止上传。相同文件重传由平台幂等处理,不新增扣费。
|
|
96
|
+
- 已冻结文件被篡改、内容冲突、权限拒绝等不会被当成成功;错误状态可供排查。文件缺失不会静默跳过。
|
|
97
|
+
- 状态目录绑定 Hermes 地址、API 密钥摘要、任务根目录和 OpenAny 环境。更换这些配置前需要先核对未完成任务;密钥轮换改变原生幂等作用域,不能盲目重新接单。
|
|
98
|
+
|
|
99
|
+
查看本地状态,不需要买家登录:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
openany-dev seller hermes status --id '<OPENANY_TASK_UUID>' -j
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
返回 `task_id`、`run_id`、`state`、`attempts`、`last_code` 和毫秒时间戳 `deadline`;`state=stored` 仅代表文件上传确认,平台任务及计费状态仍通过买家的 `status_url` 查询。`attempts` 是接单和交付处理的尝试计数,不是模型执行次数。
|
|
106
|
+
|
|
107
|
+
`stored`、`failed`、`expired` 为收集器终态。失败或超时不自动重新执行业务 Skill。对尚未到期的可恢复上传错误会持续重试;平台根据执行状态、文件登记和截止时间决定结算或释放预留。
|
|
108
|
+
|
|
109
|
+
宿主机文件与终态状态记录由服务商按自己的保留策略清理;清理前确认任务终态且无需排查,不能删除仍在等待上传的快照。不要把买家保留期内的私有下载地址变成公开对象地址。
|
|
110
|
+
|
|
111
|
+
## 买家和计费
|
|
112
|
+
|
|
113
|
+
买家接口保持现状:提交后读取 `status_url`,使用同一买家 API Key 查询,完成后按 `artifacts[].artifact_id` 下载。文件上传接口仍是 `POST /v1/agent-api-tasks/{task_id}/artifacts`,不使用 poll 任务不兼容的结果回调入口。
|
|
114
|
+
|
|
115
|
+
Hermes 完成但缺文件、上传失败或存储校验失败时,不满足成功结算条件。执行失败、取消、中断或缺文件超时释放预留;重复提交相同幂等请求和重复上传相同文件都不增加费用。字节完整性校验不代替业务内容质量验收。
|
|
116
|
+
|
|
117
|
+
## 本地验证范围
|
|
118
|
+
|
|
119
|
+
CLI 测试覆盖稳定读取、回执不一致、路径隔离、并发接单去重、确定性 ZIP、上传重试、接单回执丢失及重启恢复。跨仓库联调使用真实本机 HTTP、PostgreSQL、Redis、钱包账本、CLI 适配服务和上传器,Hermes 执行及 S3 协议由测试服务模拟,校验下载原始字节和 SHA-256、上传失败不扣费、恢复后只结算一次、缺文件和执行失败零扣费。
|
|
120
|
+
|
|
121
|
+
这不代表已经在真实服务商 Hermes 上安装、产生真实模型调用或修改 Dev 钱包。
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Hermes 文件类 Skill 接入 OpenAny 托管交付
|
|
2
2
|
|
|
3
|
+
推荐自动收集:参见 [Hermes 自动文件交付](hermes-auto-files.md)。它复用本文上传接口和轮询计费,逐个 Skill 无需编写上传命令。本文保留 `manual_cli` 接入方式供兼容。
|
|
4
|
+
|
|
3
5
|
本文示例使用 Dev 环境和 `openany-dev`。2026-11-16 本次交付为本地代码与测试,没有部署、发布 npm 包或修改服务商 Hermes;现网旧 CLI 不包含新命令。接入前应确认目标环境已支持下述接口,且所安装 CLI 的 `--help` 包含 `seller tasks upload`。
|
|
4
6
|
|
|
5
7
|
## 责任和工作方式
|
|
@@ -48,7 +50,7 @@ Hermes 由服务商运行和管理。OpenAny 提供上传接口、CLI、私有
|
|
|
48
50
|
2. 在 $OPENANY_TASK_ROOT/<task_id>/ 中生成该任务的交付文件。
|
|
49
51
|
3. 多个文件打包成一个 ZIP。准备最终、完整且不再修改的文件。
|
|
50
52
|
4. 调用 openany-dev seller tasks upload --id <task_id> --file <文件绝对路径> --content-type <文件MIME> -j。
|
|
51
|
-
5. 只有返回 task_id
|
|
53
|
+
5. 只有返回 task_id 匹配、status=stored,且文件大小、类型和 SHA-256 均与本地一致才确认上传成功。stored 只代表文件暂存成功,不代表平台已结算。
|
|
52
54
|
6. 网络错误或返回不确定时,保留文件并对同一任务重试同一内容;不要另建任务,也不要覆盖为另一份文件。
|
|
53
55
|
7. 上传完成后,再结束 Hermes 运行并给出非空的业务结果摘要。上传一直失败时明确报告失败,不要谎报成功。
|
|
54
56
|
8. 不读取、不打印、不输出任何凭据。不要处理其他任务目录。
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { extname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { inside, MAX_FILE_BYTES, readStableArtifact, sha256 } from './artifact-files.js';
|
|
5
|
+
import { CLIError } from './errors.js';
|
|
6
|
+
|
|
7
|
+
const mime = {'.pdf':'application/pdf','.zip':'application/zip','.docx':'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
8
|
+
'.xlsx':'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','.xls':'application/vnd.ms-excel',
|
|
9
|
+
'.json':'application/json','.txt':'text/plain','.csv':'text/csv'};
|
|
10
|
+
const extensions = /\.(?:pdf|zip|docx?|xlsx?|csv|txt|json|png|jpe?g|gif|webp|bmp|tiff|svg|mp[34]|mov|avi|mkv|webm|wav|ogg|m4a|flac|odt|rtf|md|tsv|xml|ya?ml|pptx?|odp|tar|gz|tgz|bz2|7z|html?)$/i;
|
|
11
|
+
|
|
12
|
+
export function replyArtifactPaths(output) {
|
|
13
|
+
// Code examples are not declarations of final artifacts (same convention as Hermes).
|
|
14
|
+
const text = String(output || '').replace(/(```|~~~)[\s\S]*?\1/g, '').replace(/`+[^`\n]*`+/g, '');
|
|
15
|
+
return [...new Set((text.match(/(?:~\/|\/)[^\s<>"'`,。;!?()【】]+/g) || [])
|
|
16
|
+
.map(p => p.replace(/[)\],;:!?]+$/, '')).filter(p => extensions.test(p)))];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// STORE-only ZIP: fixed timestamps, safe relative names and no external archiver dependency.
|
|
20
|
+
function crc32(bytes) {
|
|
21
|
+
let crc = 0xffffffff;
|
|
22
|
+
for (const byte of bytes) { crc ^= byte; for (let n=0;n<8;n++) crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1)); }
|
|
23
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
24
|
+
}
|
|
25
|
+
export function zipArtifacts(files) {
|
|
26
|
+
const local = [], central = []; let offset = 0;
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
const name = Buffer.from(file.name); const crc = crc32(file.bytes);
|
|
29
|
+
const h = Buffer.alloc(30); h.writeUInt32LE(0x04034b50); h.writeUInt16LE(20,4); h.writeUInt16LE(0x800,6);
|
|
30
|
+
h.writeUInt16LE(33,12); h.writeUInt32LE(crc,14); h.writeUInt32LE(file.bytes.length,18); h.writeUInt32LE(file.bytes.length,22); h.writeUInt16LE(name.length,26);
|
|
31
|
+
const c = Buffer.alloc(46); c.writeUInt32LE(0x02014b50); c.writeUInt16LE(20,4); c.writeUInt16LE(20,6); c.writeUInt16LE(0x800,8);
|
|
32
|
+
c.writeUInt16LE(33,14); c.writeUInt32LE(crc,16); c.writeUInt32LE(file.bytes.length,20); c.writeUInt32LE(file.bytes.length,24); c.writeUInt16LE(name.length,28); c.writeUInt32LE(offset,42);
|
|
33
|
+
local.push(h,name,file.bytes); central.push(c,name); offset += h.length + name.length + file.bytes.length;
|
|
34
|
+
}
|
|
35
|
+
const directory = Buffer.concat(central); const end = Buffer.alloc(22);
|
|
36
|
+
end.writeUInt32LE(0x06054b50); end.writeUInt16LE(files.length,8); end.writeUInt16LE(files.length,10); end.writeUInt32LE(directory.length,12); end.writeUInt32LE(offset,16);
|
|
37
|
+
return Buffer.concat([...local,directory,end]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function collectArtifacts(taskDir, output) {
|
|
41
|
+
let paths;
|
|
42
|
+
const manifest = join(taskDir, 'openany-artifacts.json');
|
|
43
|
+
try {
|
|
44
|
+
await fs.lstat(manifest);
|
|
45
|
+
const value = JSON.parse((await readStableArtifact(taskDir, manifest, 65536)).toString('utf8'));
|
|
46
|
+
if (!Array.isArray(value.files) || !value.files.length || value.files.length > 32 || value.files.some(p => typeof p !== 'string' || !p))
|
|
47
|
+
throw new CLIError('INVALID_MANIFEST', 'Expected a nonempty files array with at most 32 paths');
|
|
48
|
+
paths = value.files;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (error.code !== 'ENOENT') throw error;
|
|
51
|
+
paths = replyArtifactPaths(output);
|
|
52
|
+
}
|
|
53
|
+
if (!paths.length || paths.length > 32) throw new CLIError('ARTIFACTS_MISSING', 'No final artifact declaration found');
|
|
54
|
+
const files = []; const seen = new Set(); let size = 0;
|
|
55
|
+
for (const path of paths) {
|
|
56
|
+
if (/[\\\x00-\x1f\x7f]/.test(path)) throw new CLIError('UNSAFE_PATH', 'Artifact paths cannot contain backslashes or control characters');
|
|
57
|
+
const absolute = path.startsWith('~/') ? resolve(homedir(),path.slice(2)) : isAbsolute(path) ? path : resolve(taskDir,path);
|
|
58
|
+
if (!inside(taskDir, absolute) || relative(taskDir,absolute).split(/[\\/]/).some(p => p.startsWith('.openany-')) || absolute === manifest)
|
|
59
|
+
throw new CLIError('UNSAFE_PATH', 'Only declared files inside the task directory can be delivered');
|
|
60
|
+
const canonical = await fs.realpath(absolute);
|
|
61
|
+
if (seen.has(canonical)) continue;
|
|
62
|
+
seen.add(canonical);
|
|
63
|
+
const bytes = await readStableArtifact(taskDir, absolute); size += bytes.length;
|
|
64
|
+
if (size > MAX_FILE_BYTES) throw new CLIError('INVALID_INPUT', 'Combined artifacts exceed the file limit');
|
|
65
|
+
files.push({name:relative(taskDir,absolute).split('\\').join('/'),bytes});
|
|
66
|
+
}
|
|
67
|
+
files.sort((a,b) => a.name.localeCompare(b.name,'en'));
|
|
68
|
+
const singleType = files.length === 1 && mime[extname(files[0].name).toLowerCase()];
|
|
69
|
+
const bytes = singleType ? files[0].bytes : zipArtifacts(files);
|
|
70
|
+
if (bytes.length > MAX_FILE_BYTES) throw new CLIError('INVALID_INPUT', 'Delivery archive exceeds the file limit');
|
|
71
|
+
return {bytes, content_type: singleType || 'application/zip', name:singleType ? 'delivery'+extname(files[0].name).toLowerCase() : 'delivery.zip', size:bytes.length, sha256:sha256(bytes)};
|
|
72
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import fs, { realpath } from 'node:fs/promises';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { isAbsolute, relative, sep } from 'node:path';
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
import { CLIError } from './errors.js';
|
|
6
|
+
|
|
7
|
+
export const MAX_FILE_BYTES = 10_000_000;
|
|
8
|
+
export const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
9
|
+
export const inside = (root, file) => {
|
|
10
|
+
const rel = relative(root, file);
|
|
11
|
+
return rel !== '' && rel !== '..' && !rel.startsWith('..' + sep) && !isAbsolute(rel);
|
|
12
|
+
};
|
|
13
|
+
const unchanged = (a, b) => ['dev','ino','size','mtimeNs','ctimeNs'].every(k => a[k] === b[k]);
|
|
14
|
+
|
|
15
|
+
// File contents and the resolved task boundary must remain stable for the whole read.
|
|
16
|
+
// Process/UID isolation remains necessary against a hostile process with the same UID.
|
|
17
|
+
export async function readStableArtifact(root, file, limit = MAX_FILE_BYTES) {
|
|
18
|
+
let handle;
|
|
19
|
+
try {
|
|
20
|
+
const boundary = await realpath(root);
|
|
21
|
+
const resolved = await realpath(file);
|
|
22
|
+
if (boundary !== root || !inside(root, resolved)) throw new CLIError('UNSAFE_PATH', 'File must remain inside its task directory');
|
|
23
|
+
const before = await fs.stat(resolved, {bigint:true});
|
|
24
|
+
handle = await fs.open(resolved, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
25
|
+
const opened = await handle.stat({bigint:true});
|
|
26
|
+
if (!unchanged(before, opened) || !opened.isFile()) throw new CLIError('FILE_CHANGED', 'File changed while opening');
|
|
27
|
+
if (opened.size < 1 || opened.size > BigInt(limit)) throw new CLIError('INVALID_INPUT', 'Invalid artifact size');
|
|
28
|
+
const buffer = Buffer.alloc(Number(opened.size) + 1);
|
|
29
|
+
let size = 0;
|
|
30
|
+
while (size < buffer.length) {
|
|
31
|
+
const part = await handle.read(buffer, size, buffer.length - size, size);
|
|
32
|
+
if (!part.bytesRead) break;
|
|
33
|
+
size += part.bytesRead;
|
|
34
|
+
}
|
|
35
|
+
const after = await handle.stat({bigint:true});
|
|
36
|
+
const pathAfter = await fs.stat(resolved, {bigint:true});
|
|
37
|
+
if (BigInt(size) !== opened.size || !unchanged(opened, after) || !unchanged(after, pathAfter) ||
|
|
38
|
+
await realpath(root) !== boundary || await realpath(file) !== resolved)
|
|
39
|
+
throw new CLIError('FILE_CHANGED', 'File changed during reading; retry after generation has finished');
|
|
40
|
+
return buffer.subarray(0, size);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error instanceof CLIError) throw error;
|
|
43
|
+
throw new CLIError('FILE_UNAVAILABLE', 'Cannot read the task file');
|
|
44
|
+
} finally { await handle?.close(); }
|
|
45
|
+
}
|
package/src/cli.js
CHANGED
|
@@ -43,6 +43,10 @@ Model execution uses the OpenAny gateway with the path and query from curlExampl
|
|
|
43
43
|
Multiple providers require --provider; multiple requests require --example.
|
|
44
44
|
Supply the complete JSON body, including model; route.provider is bound from the catalog.
|
|
45
45
|
Audio transcription is not supported. Task polling is explicit, never automatic.
|
|
46
|
+
seller hermes serve [-j] (loopback native Runs relay with durable automatic file delivery)
|
|
47
|
+
seller hermes status --id TASK_ID [-j] (local delivery state; no credentials or business output)
|
|
48
|
+
Requires OPENANY_HERMES_URL, OPENANY_HERMES_API_KEY, OPENANY_HERMES_STATE_DIR,
|
|
49
|
+
OPENANY_TASK_ROOT, OPENANY_DEV_SELLER_KEY; optional OPENANY_HERMES_PORT (default 8790).
|
|
46
50
|
seller tasks upload --id TASK_ID --file FILE --content-type MIME [-j]
|
|
47
51
|
Requires OPENANY_DEV_SELLER_KEY and absolute OPENANY_TASK_ROOT; does not settle the task.
|
|
48
52
|
seller status [--application ID]
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { isAbsolute, join, resolve } from 'node:path';
|
|
4
|
+
import { randomUUID, timingSafeEqual } from 'node:crypto';
|
|
5
|
+
import { GATEWAY_ORIGIN } from './http.js';
|
|
6
|
+
import { CLIError } from './errors.js';
|
|
7
|
+
import { inside, readStableArtifact, sha256 } from './artifact-files.js';
|
|
8
|
+
import { collectArtifacts } from './artifact-collector.js';
|
|
9
|
+
import { uploadPollArtifact } from './poll-artifacts.js';
|
|
10
|
+
|
|
11
|
+
const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
12
|
+
const runId = /^[A-Za-z0-9_-]{1,128}$/;
|
|
13
|
+
const done = new Set(['stored','failed','expired']);
|
|
14
|
+
const fail = (code, status=422) => { throw new CLIError(code, code, {status}); };
|
|
15
|
+
const MAX_JSON = 1_048_576;
|
|
16
|
+
|
|
17
|
+
async function syncDir(path) { const h = await fs.open(path,'r'); try { await h.sync(); } finally { await h.close(); } }
|
|
18
|
+
async function atomicWrite(path, bytes) {
|
|
19
|
+
const temp = path+'.'+randomUUID()+'.tmp';
|
|
20
|
+
const h = await fs.open(temp,'wx',0o600);
|
|
21
|
+
try { await h.writeFile(bytes); await h.sync(); } finally { await h.close(); }
|
|
22
|
+
await fs.rename(temp,path); await syncDir(resolve(path,'..'));
|
|
23
|
+
}
|
|
24
|
+
async function jsonFile(path) { return JSON.parse(await fs.readFile(path,'utf8')); }
|
|
25
|
+
async function readBounded(stream, limit=MAX_JSON) {
|
|
26
|
+
const chunks=[]; let size=0;
|
|
27
|
+
for await (const chunk of stream) { size+=chunk.length; if (size>limit) fail('BODY_TOO_LARGE',413); chunks.push(chunk); }
|
|
28
|
+
return Buffer.concat(chunks);
|
|
29
|
+
}
|
|
30
|
+
function equal(a,b) { const x=Buffer.from(a || ''); const y=Buffer.from(b); return x.length===y.length && timingSafeEqual(x,y); }
|
|
31
|
+
|
|
32
|
+
export class HermesDelivery {
|
|
33
|
+
constructor({ctx, fetch=globalThis.fetch, now=Date.now}) {
|
|
34
|
+
this.ctx=ctx; this.env=ctx.env; this.fetch=fetch; this.now=now; this.jobs=new Map(); this.serial=Promise.resolve();
|
|
35
|
+
this.key=this.env.OPENANY_HERMES_API_KEY?.trim();
|
|
36
|
+
if (!this.key || !/^[\x21-\x7e]+$/.test(this.key) || !this.env.OPENANY_DEV_SELLER_KEY?.trim()) fail('CONFIG_REQUIRED');
|
|
37
|
+
ctx.secrets.push(this.key,this.env.OPENANY_DEV_SELLER_KEY.trim());
|
|
38
|
+
for (const name of ['OPENANY_TASK_ROOT','OPENANY_HERMES_STATE_DIR']) if (!isAbsolute(this.env[name] || '')) fail('CONFIG_REQUIRED');
|
|
39
|
+
this.root=resolve(this.env.OPENANY_TASK_ROOT); this.stateDir=resolve(this.env.OPENANY_HERMES_STATE_DIR);
|
|
40
|
+
if (this.root===this.stateDir || inside(this.root,this.stateDir) || inside(this.stateDir,this.root)) fail('UNSAFE_STATE_DIRECTORY');
|
|
41
|
+
try { this.url=new URL(this.env.OPENANY_HERMES_URL); } catch { fail('INVALID_HERMES_URL'); }
|
|
42
|
+
if (this.url.username || this.url.password || this.url.search || this.url.hash || this.url.pathname !== '/' ||
|
|
43
|
+
!(this.url.protocol==='https:' || this.url.protocol==='http:' && ['127.0.0.1','[::1]','localhost'].includes(this.url.hostname))) fail('INVALID_HERMES_URL');
|
|
44
|
+
this.ctx.env={...this.env,OPENANY_TASK_ROOT:this.root};
|
|
45
|
+
}
|
|
46
|
+
locked(fn) { const next=this.serial.then(fn); this.serial=next.catch(()=>{}); return next; }
|
|
47
|
+
async init() {
|
|
48
|
+
for (const path of [this.root,this.stateDir]) {
|
|
49
|
+
await fs.mkdir(path,{recursive:true,mode:0o700});
|
|
50
|
+
const stat=await fs.lstat(path);
|
|
51
|
+
if (stat.isSymbolicLink() || stat.mode & 0o077 || stat.uid!==process.getuid?.()) fail('UNSAFE_STATE_DIRECTORY');
|
|
52
|
+
}
|
|
53
|
+
this.root=await fs.realpath(this.root); this.stateDir=await fs.realpath(this.stateDir);
|
|
54
|
+
this.ctx.env.OPENANY_TASK_ROOT=this.root;
|
|
55
|
+
if (this.root===this.stateDir || inside(this.root,this.stateDir) || inside(this.stateDir,this.root)) fail('UNSAFE_STATE_DIRECTORY');
|
|
56
|
+
this.lockPath=join(this.stateDir,'worker.lock');
|
|
57
|
+
for (let n=0;n<2;n++) {
|
|
58
|
+
try { this.lock=await fs.open(this.lockPath,'wx',0o600); await this.lock.writeFile(String(process.pid)); await this.lock.sync(); break; }
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error.code!=='EEXIST' || n) fail('COLLECTOR_ALREADY_RUNNING');
|
|
61
|
+
const pid=Number(await fs.readFile(this.lockPath,'utf8'));
|
|
62
|
+
if (!Number.isSafeInteger(pid) || pid<1) fail('COLLECTOR_LOCK_UNCERTAIN');
|
|
63
|
+
try { process.kill(pid,0); fail('COLLECTOR_ALREADY_RUNNING'); }
|
|
64
|
+
catch (e) { if (e.code!=='ESRCH') throw e; await fs.unlink(this.lockPath); }
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
// Changing a Hermes credential changes its idempotency scope. Require reconciliation first.
|
|
69
|
+
const binding={root:this.root,hermes:this.url.href,key_hash:sha256(this.key),environment:GATEWAY_ORIGIN};
|
|
70
|
+
const path=join(this.stateDir,'binding.json');
|
|
71
|
+
try { if (JSON.stringify(await jsonFile(path))!==JSON.stringify(binding)) fail('COLLECTOR_BINDING_CHANGED'); }
|
|
72
|
+
catch (e) { if(e.code!=='ENOENT') throw e; await atomicWrite(path,JSON.stringify(binding)); }
|
|
73
|
+
for (const entry of await fs.readdir(this.stateDir)) if (/^[0-9a-f-]{36}\.json$/.test(entry)) {
|
|
74
|
+
const job=await jsonFile(join(this.stateDir,entry));
|
|
75
|
+
if (!uuid.test(job.task_id) || entry!==job.task_id+'.json' || job.version!==1 || !Number.isFinite(job.deadline)) fail('INVALID_DELIVERY_STATE');
|
|
76
|
+
this.jobs.set(job.task_id,job);
|
|
77
|
+
}
|
|
78
|
+
return this;
|
|
79
|
+
} catch(e) { await this.close(); throw e; }
|
|
80
|
+
}
|
|
81
|
+
async close() { await this.serial; if(this.lock) { await this.lock.close(); this.lock=null; await fs.unlink(this.lockPath); } }
|
|
82
|
+
async save(job) { await atomicWrite(join(this.stateDir,job.task_id+'.json'),JSON.stringify(job)); this.jobs.set(job.task_id,job); }
|
|
83
|
+
async request(path,{method='GET',body,id,deadline=this.now()+10000}={}) {
|
|
84
|
+
const response=await this.fetch(new URL(path,this.url),{method,headers:{authorization:'Bearer '+this.key,'content-type':'application/json',...(id?{'idempotency-key':id}:{})},
|
|
85
|
+
body,redirect:'error',signal:AbortSignal.timeout(Math.max(1,Math.min(10000,deadline-this.now())))});
|
|
86
|
+
const bytes=await readBounded(response.body);
|
|
87
|
+
let value; try { value=JSON.parse(bytes.toString()); } catch { fail('INVALID_HERMES_RESPONSE',502); }
|
|
88
|
+
if (!response.ok) fail('HERMES_HTTP_'+response.status,response.status);
|
|
89
|
+
return {status:response.status,body:value};
|
|
90
|
+
}
|
|
91
|
+
async checkCapabilities(deadline) {
|
|
92
|
+
const {body}=await this.request('/v1/capabilities',{deadline}); const idem=body.features?.runs_idempotency;
|
|
93
|
+
if (idem?.supported!==true || idem?.durable!==true || !(idem.retention_seconds*1000 >= deadline-this.now()+10000)) fail('DURABLE_RUNS_REQUIRED',503);
|
|
94
|
+
}
|
|
95
|
+
async admit(id,deadlineText,body,idem) { return this.locked(async()=>{
|
|
96
|
+
const deadline=Date.parse(deadlineText);
|
|
97
|
+
if (!uuid.test(id || '') || idem!==id || !Number.isFinite(deadline) || deadline<=this.now() || !body || typeof body!=='object' || Array.isArray(body)) fail('INVALID_TASK_CONTEXT');
|
|
98
|
+
const original=JSON.stringify(body); let job=this.jobs.get(id);
|
|
99
|
+
if (job && (job.request_hash!==sha256(original) || job.deadline!==deadline)) fail('TASK_CONTEXT_CONFLICT',409);
|
|
100
|
+
if (!job) {
|
|
101
|
+
await this.checkCapabilities(deadline);
|
|
102
|
+
const taskDir=join(this.root,id); await fs.mkdir(taskDir,{recursive:true,mode:0o700});
|
|
103
|
+
if (await fs.realpath(taskDir)!==taskDir || (await fs.lstat(taskDir)).mode & 0o077) fail('UNSAFE_TASK_DIRECTORY');
|
|
104
|
+
const native={...body,instructions:(body.instructions || '')+'\n\nOpenAny 自动文件交付:仅在 '+taskDir+' 中生成本任务最终成果。完成后在回复中以纯文本列出成果的绝对路径(不放在代码块或反引号中),并提供非空摘要;或者写入 '+join(taskDir,'openany-artifacts.json')+',格式为 {"files":["report.pdf"]}。只声明最终文件,勿列出中间产物。不要自行执行上传命令,自动收集器负责上传。'};
|
|
105
|
+
job={version:1,task_id:id,deadline,request_hash:sha256(original),native:JSON.stringify(native),state:'dispatching',attempts:0,next_at:0};
|
|
106
|
+
// Persist the exact admitted payload BEFORE dispatch. Unknown outcomes replay only this payload/key.
|
|
107
|
+
await this.save(job);
|
|
108
|
+
}
|
|
109
|
+
if (!job.run_id && !done.has(job.state)) await this.dispatch(job);
|
|
110
|
+
if (!job.run_id) fail('ADMISSION_UNCONFIRMED',503);
|
|
111
|
+
return {status:202,body:{run_id:job.run_id,status:'queued',replayed:job.attempts>1}};
|
|
112
|
+
}); }
|
|
113
|
+
async dispatch(job) {
|
|
114
|
+
if (this.now()>=job.deadline) { job.state='expired'; await this.save(job); return; }
|
|
115
|
+
// Recheck durable admission after a restart before replaying an uncertain POST.
|
|
116
|
+
await this.checkCapabilities(job.deadline);
|
|
117
|
+
const response=await this.request('/v1/runs',{method:'POST',body:job.native,id:job.task_id,deadline:job.deadline});
|
|
118
|
+
if (!runId.test(response.body.run_id || '') || response.status!==202) fail('INVALID_HERMES_ADMISSION',502);
|
|
119
|
+
job.run_id=response.body.run_id; job.state='running'; job.attempts++; await this.save(job);
|
|
120
|
+
}
|
|
121
|
+
async freeze(job,output) {
|
|
122
|
+
const taskDir=join(this.root,job.task_id); const dir=join(taskDir,'.openany-delivery');
|
|
123
|
+
try {
|
|
124
|
+
const meta=await jsonFile(join(dir,'snapshot.json'));
|
|
125
|
+
if (!/^delivery\.[a-z0-9]+$/.test(meta.name) || !Number.isSafeInteger(meta.size) || !/^[0-9a-f]{64}$/.test(meta.sha256)) fail('INVALID_SNAPSHOT');
|
|
126
|
+
const bytes=await readStableArtifact(taskDir,join(dir,meta.name));
|
|
127
|
+
if (sha256(bytes)!==meta.sha256 || bytes.length!==meta.size) fail('SNAPSHOT_CORRUPT');
|
|
128
|
+
return {...meta,file:join(dir,meta.name)};
|
|
129
|
+
} catch(e) { if(e.code!=='ENOENT') throw e; }
|
|
130
|
+
// Atomic directory publication makes recovery reuse the same immutable ZIP after a crash.
|
|
131
|
+
const artifact=await collectArtifacts(taskDir,output); const staging=join(taskDir,'.openany-staging-'+randomUUID());
|
|
132
|
+
await fs.mkdir(staging,{mode:0o700});
|
|
133
|
+
const {bytes,...meta}=artifact;
|
|
134
|
+
try {
|
|
135
|
+
await atomicWrite(join(staging,meta.name),bytes); await atomicWrite(join(staging,'snapshot.json'),JSON.stringify(meta));
|
|
136
|
+
await fs.rename(staging,dir); await syncDir(taskDir);
|
|
137
|
+
} catch(e) { await fs.rm(staging,{recursive:true,force:true}); throw e; }
|
|
138
|
+
return {...meta,file:join(dir,meta.name)};
|
|
139
|
+
}
|
|
140
|
+
async step() { return this.locked(async()=>{
|
|
141
|
+
for (const job of this.jobs.values()) {
|
|
142
|
+
if (done.has(job.state) || job.next_at>this.now() && this.now()<job.deadline) continue;
|
|
143
|
+
try {
|
|
144
|
+
if (this.now()>=job.deadline) { job.state='expired'; job.last_code='DEADLINE_EXCEEDED'; await this.save(job); continue; }
|
|
145
|
+
if (!job.run_id) await this.dispatch(job);
|
|
146
|
+
if (!job.run_id) continue;
|
|
147
|
+
if (!job.frozen) {
|
|
148
|
+
const {body}=await this.request('/v1/runs/'+job.run_id,{deadline:job.deadline});
|
|
149
|
+
if (body.run_id!==job.run_id) fail('RUN_ID_MISMATCH');
|
|
150
|
+
if (['failed','cancelled','interrupted'].includes(body.status)) { job.state='failed'; job.last_code='HERMES_RUN_FAILED'; await this.save(job); continue; }
|
|
151
|
+
if (body.status!=='completed') { job.next_at=this.now()+2000; await this.save(job); continue; }
|
|
152
|
+
if (typeof body.output!=='string' || !body.output.trim()) fail('RESULT_MISSING');
|
|
153
|
+
job.frozen=await this.freeze(job,body.output); job.state='uploading'; await this.save(job);
|
|
154
|
+
}
|
|
155
|
+
if(this.now()>=job.deadline) fail('DEADLINE_EXCEEDED');
|
|
156
|
+
// Reuse the very same uploader as `seller tasks upload`, including scope and receipt checks.
|
|
157
|
+
const receipt=await uploadPollArtifact(['seller','tasks','upload'],{...this.ctx,
|
|
158
|
+
options:{id:job.task_id,file:job.frozen.file,'content-type':job.frozen.content_type},expectedArtifact:job.frozen,
|
|
159
|
+
http:{gateway:(path,options)=>this.ctx.http.gateway(path,{...options,timeoutMs:Math.max(1,Math.min(10000,job.deadline-this.now()))})}});
|
|
160
|
+
job.receipt=receipt; job.state='stored'; job.last_code=null; delete job.native; await this.save(job);
|
|
161
|
+
} catch(e) {
|
|
162
|
+
job.attempts++; job.last_code=/^[A-Z0-9_]+$/.test(e.code || '')?e.code:'DELIVERY_RETRY';
|
|
163
|
+
const permanent=['UNSAFE_PATH','INVALID_INPUT','INVALID_MANIFEST','SNAPSHOT_CORRUPT','INVALID_SNAPSHOT','FILE_CHANGED'].includes(e.code) && job.frozen ||
|
|
164
|
+
[400,401,403,409,410,413,422].includes(e.details?.status);
|
|
165
|
+
job.state=this.now()>=job.deadline?'expired':permanent?'failed':job.run_id?'retry':'dispatching';
|
|
166
|
+
job.next_at=this.now()+Math.min(30000,1000*2**Math.min(job.attempts,5)); await this.save(job);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}); }
|
|
170
|
+
publicStatus(id) {
|
|
171
|
+
const job=this.jobs.get(id); if(!job) fail('TASK_NOT_FOUND',404);
|
|
172
|
+
return Object.fromEntries(['task_id','run_id','state','attempts','last_code','deadline'].map(k=>[k,job[k] ?? null]));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function createHermesRelay(options) {
|
|
177
|
+
const delivery=await new HermesDelivery(options).init();
|
|
178
|
+
const send=(res,status,body)=>{res.writeHead(status,{'content-type':'application/json','cache-control':'no-store'});res.end(JSON.stringify(body));};
|
|
179
|
+
const server=createServer(async(req,res)=>{
|
|
180
|
+
try {
|
|
181
|
+
if (!equal(req.headers.authorization,'Bearer '+delivery.key)) fail('AUTHENTICATION_INVALID',401);
|
|
182
|
+
if (req.url==='/health' && req.method==='GET') return send(res,200,{ok:true});
|
|
183
|
+
const status=/^\/openany\/delivery\/([0-9a-f-]{36})$/.exec(req.url);
|
|
184
|
+
if (status && req.method==='GET') return send(res,200,delivery.publicStatus(status[1]));
|
|
185
|
+
if (req.url==='/v1/runs' && req.method==='POST') {
|
|
186
|
+
let body; try { body=JSON.parse((await readBounded(req)).toString()); } catch(e) { if(e instanceof CLIError) throw e; fail('INVALID_JSON',400); }
|
|
187
|
+
if (req.headers['x-openany-artifact-delivery']==='auto_cli') {
|
|
188
|
+
const r=await delivery.admit(req.headers['x-openany-task-id'],req.headers['x-openany-deadline'],body,req.headers['idempotency-key']);
|
|
189
|
+
return send(res,r.status,r.body);
|
|
190
|
+
}
|
|
191
|
+
if (Object.keys(req.headers).some(k=>k.startsWith('x-openany-'))) fail('INVALID_TASK_CONTEXT');
|
|
192
|
+
const r=await delivery.request('/v1/runs',{method:'POST',body:JSON.stringify(body),id:req.headers['idempotency-key']}); return send(res,r.status,r.body);
|
|
193
|
+
}
|
|
194
|
+
if (req.method==='GET' && (/^\/v1\/runs\/[A-Za-z0-9_-]{1,128}$/.test(req.url) || ['/v1/skills','/v1/capabilities'].includes(req.url))) {
|
|
195
|
+
const r=await delivery.request(req.url); return send(res,r.status,r.body);
|
|
196
|
+
}
|
|
197
|
+
fail('NOT_FOUND',404);
|
|
198
|
+
} catch(e) { send(res,e.details?.status || 503,{error:{code:/^[A-Z0-9_]+$/.test(e.code || '')?e.code:'DELIVERY_UNAVAILABLE'}}); }
|
|
199
|
+
});
|
|
200
|
+
server.requestTimeout=15000; server.headersTimeout=10000;
|
|
201
|
+
let ticking=false;
|
|
202
|
+
const tick=()=>{if(ticking)return;ticking=true;delivery.step().catch(()=>options.ctx.stderr?.write('{"error":"DELIVERY_STATE_WRITE_FAILED"}\n')).finally(()=>ticking=false);};
|
|
203
|
+
const timer=setInterval(tick,1000); timer.unref(); tick();
|
|
204
|
+
return {server,delivery,async close(){clearInterval(timer);server.closeIdleConnections();await new Promise((resolve,reject)=>server.close(e=>e?reject(e):resolve()));await delivery.close();}};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export async function runHermesDelivery(args,ctx) {
|
|
208
|
+
if (args.length!==3 || !['serve','status'].includes(args[2]) || Object.keys(ctx.options).some(k=>!['id','json'].includes(k)))
|
|
209
|
+
throw new CLIError('INVALID_INPUT','Use seller hermes serve [-j] or seller hermes status --id TASK_ID [-j]');
|
|
210
|
+
if (args[2]==='status') {
|
|
211
|
+
const id=ctx.options.id;
|
|
212
|
+
if (!uuid.test(id || '') || !isAbsolute(ctx.env.OPENANY_HERMES_STATE_DIR || '')) fail('CONFIG_REQUIRED');
|
|
213
|
+
const job=await jsonFile(join(ctx.env.OPENANY_HERMES_STATE_DIR,id+'.json'));
|
|
214
|
+
return Object.fromEntries(['task_id','run_id','state','attempts','last_code','deadline'].map(k=>[k,job[k] ?? null]));
|
|
215
|
+
}
|
|
216
|
+
const port=Number(ctx.env.OPENANY_HERMES_PORT || 8790);
|
|
217
|
+
if (!Number.isInteger(port) || port<1024 || port>65535 || ctx.options.id) fail('INVALID_INPUT');
|
|
218
|
+
const relay=await createHermesRelay({ctx});
|
|
219
|
+
try {
|
|
220
|
+
await new Promise((resolve,reject)=>{relay.server.once('error',reject);relay.server.listen(port,'127.0.0.1',resolve);});
|
|
221
|
+
ctx.stdout.write(JSON.stringify({ready:true,address:'127.0.0.1',port})+'\n');
|
|
222
|
+
await new Promise(resolve=>{
|
|
223
|
+
const stop=()=>{process.off('SIGTERM',stop);process.off('SIGINT',stop);resolve();};
|
|
224
|
+
process.once('SIGTERM',stop);process.once('SIGINT',stop);
|
|
225
|
+
});
|
|
226
|
+
} finally { await relay.close(); }
|
|
227
|
+
return {stopped:true};
|
|
228
|
+
}
|
package/src/poll-artifacts.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { basename, isAbsolute, join, relative, sep } from 'node:path';
|
|
1
|
+
import { realpath } from 'node:fs/promises';
|
|
2
|
+
import { basename, isAbsolute, join, relative } from 'node:path';
|
|
4
3
|
import { CLIError } from './errors.js';
|
|
4
|
+
import { inside, readStableArtifact, sha256 } from './artifact-files.js';
|
|
5
5
|
|
|
6
|
-
const types = new Set(['application/pdf', 'application/zip',
|
|
6
|
+
export const types = new Set(['application/pdf', 'application/zip',
|
|
7
7
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
8
8
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
9
9
|
'application/vnd.ms-excel', 'application/json', 'text/plain', 'text/csv']);
|
|
10
|
-
const inside = (root, file) => {
|
|
11
|
-
const rel = relative(root, file);
|
|
12
|
-
return rel !== '' && rel !== '..' && !rel.startsWith('..' + sep) && !isAbsolute(rel);
|
|
13
|
-
};
|
|
14
10
|
|
|
15
11
|
export async function uploadPollArtifact(args, ctx) {
|
|
16
12
|
const { options, env, http, secrets } = ctx;
|
|
@@ -29,41 +25,14 @@ export async function uploadPollArtifact(args, ctx) {
|
|
|
29
25
|
throw new CLIError('INVALID_INPUT', 'Unsupported file MIME type; use ZIP for multiple files');
|
|
30
26
|
if (!env.OPENANY_TASK_ROOT || !isAbsolute(env.OPENANY_TASK_ROOT) || !options.file)
|
|
31
27
|
throw new CLIError('INVALID_INPUT', 'Configure absolute OPENANY_TASK_ROOT and provide --file');
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const checked = await fs.stat(file, {bigint:true});
|
|
41
|
-
handle = await fs.open(file, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
42
|
-
const stat = await handle.stat({bigint:true});
|
|
43
|
-
// Pin reads to the checked inode and recheck parent resolution after open.
|
|
44
|
-
// Host process/permission isolation is still required against hostile same-UID mutation.
|
|
45
|
-
if (checked.dev !== stat.dev || checked.ino !== stat.ino ||
|
|
46
|
-
await realpath(file) !== file || await realpath(join(root,id)) !== taskDir)
|
|
47
|
-
throw new CLIError('UNSAFE_PATH', 'Task directory changed while opening the file');
|
|
48
|
-
const current = await fs.stat(file, {bigint:true});
|
|
49
|
-
if (current.dev !== stat.dev || current.ino !== stat.ino)
|
|
50
|
-
throw new CLIError('UNSAFE_PATH', 'Task file changed while opening');
|
|
51
|
-
if (!stat.isFile() || stat.size < 1 || stat.size > 10_000_000)
|
|
52
|
-
throw new CLIError('INVALID_INPUT', 'File must contain 1–10000000 bytes');
|
|
53
|
-
// A bounded positional read also rejects a file that grows after stat().
|
|
54
|
-
const buffer = Buffer.alloc(10_000_001);
|
|
55
|
-
let size = 0;
|
|
56
|
-
while (size < buffer.length) {
|
|
57
|
-
const part = await handle.read(buffer, size, buffer.length - size, size);
|
|
58
|
-
if (!part.bytesRead) break;
|
|
59
|
-
size += part.bytesRead;
|
|
60
|
-
}
|
|
61
|
-
if (size < 1 || size > 10_000_000) throw new CLIError('INVALID_INPUT', 'File size changed or exceeds limit');
|
|
62
|
-
bytes = buffer.subarray(0, size);
|
|
63
|
-
} catch (error) {
|
|
64
|
-
if (error instanceof CLIError) throw error;
|
|
65
|
-
throw new CLIError('FILE_UNAVAILABLE', 'Cannot read the task file; no upload was submitted');
|
|
66
|
-
} finally { await handle?.close(); }
|
|
28
|
+
const root = await realpath(env.OPENANY_TASK_ROOT);
|
|
29
|
+
const taskDir = await realpath(join(root, id));
|
|
30
|
+
if (!inside(root, taskDir) || relative(root, taskDir) !== id)
|
|
31
|
+
throw new CLIError('UNSAFE_PATH', 'Task directory must remain inside OPENANY_TASK_ROOT');
|
|
32
|
+
const bytes = await readStableArtifact(taskDir, options.file);
|
|
33
|
+
const hash = sha256(bytes);
|
|
34
|
+
if (ctx.expectedArtifact && (ctx.expectedArtifact.sha256 !== hash || ctx.expectedArtifact.size !== bytes.length))
|
|
35
|
+
throw new CLIError('FILE_CHANGED', 'Frozen artifact no longer matches its delivery record');
|
|
67
36
|
const body = new FormData();
|
|
68
37
|
body.set('metadata', JSON.stringify({status:'completed'}));
|
|
69
38
|
body.set('files', new Blob([bytes], {type:options['content-type']}), basename(options.file));
|
|
@@ -71,7 +40,9 @@ export async function uploadPollArtifact(args, ctx) {
|
|
|
71
40
|
method:'POST', body, sellerKey:key,
|
|
72
41
|
});
|
|
73
42
|
if (response.body?.task_id !== id || response.body?.status !== 'stored' ||
|
|
74
|
-
typeof response.body?.artifact?.artifact_id !== 'string'
|
|
43
|
+
typeof response.body?.artifact?.artifact_id !== 'string' ||
|
|
44
|
+
response.body.artifact.size !== bytes.length || response.body.artifact.sha256 !== hash ||
|
|
45
|
+
response.body.artifact.content_type !== options['content-type'])
|
|
75
46
|
throw new CLIError('INVALID_RESPONSE', 'Upload outcome is uncertain; retry the same task and unchanged file');
|
|
76
47
|
return response.body;
|
|
77
48
|
}
|
package/src/provider.js
CHANGED
|
@@ -8,6 +8,10 @@ import { runReviews } from "./provider-reviews.js";
|
|
|
8
8
|
import { runImports } from "./provider-imports.js";
|
|
9
9
|
import { parseProviderJSON, readProviderJSON } from "./provider-json.js";
|
|
10
10
|
export async function runProvider(args, ctx) {
|
|
11
|
+
if (args[1] === "hermes") {
|
|
12
|
+
const { runHermesDelivery } = await import("./hermes-delivery.js");
|
|
13
|
+
return runHermesDelivery(args, ctx);
|
|
14
|
+
}
|
|
11
15
|
if (args[1] === "tasks") {
|
|
12
16
|
const { uploadPollArtifact } = await import("./poll-artifacts.js");
|
|
13
17
|
return uploadPollArtifact(args, ctx);
|