Author SHA1 Message Date
jiang 004c9bb72d fix(agent): stabilize large tool results
Generic Container CI/CD / test-build-publish (push) Successful in 1m53s
Agent CI/CD v2 / build-test-publish-and-deploy (push) Successful in 1m53s
2026-08-25 12:02:48 +08:00
jiang 774f39cbbe fix(chat): restore tool execution details
Generic Container CI/CD / test-build-publish (push) Successful in 2m4s
Agent CI/CD v2 / build-test-publish-and-deploy (push) Successful in 2m4s
2026-08-24 18:45:45 +08:00
jiang c6efccb88a fix(chat): only expose final agent response
Generic Container CI/CD / test-build-publish (push) Successful in 1m4s
Agent CI/CD v2 / build-test-publish-and-deploy (push) Successful in 1m4s
2026-08-24 18:30:24 +08:00
jiang 11ebf428bb merge: integrate tjwater-cli into main
Merge PR #1 after CLI, contract, test, and container gates passed.
2026-08-18 17:56:44 +08:00
jiang 18e8b25f48 fix(agent): bound CLI subprocess execution 2026-08-18 17:00:23 +08:00
jiang 9aa5a96e60 merge(agent): integrate main into tjwater-cli 2026-08-18 16:42:15 +08:00
jiang a5f6474be5 fix(health): align Agent readiness contract
Generic Container CI/CD / test-build-publish (push) Successful in 35s
Agent CI/CD v2 / build-test-publish-and-deploy (push) Successful in 35s
2026-08-11 11:20:40 +08:00
TJWater CI 0a64de89bb ci: replace Agent webhook workflow with v2 deployment
Generic Container CI/CD / test-build-publish (push) Successful in 34s
Agent CI/CD v2 / build-test-publish-and-deploy (push) Successful in 34s
2026-08-11 09:26:46 +08:00
TJWater CI 2f267af7a3 revert: remove unused Agent PostgreSQL persistence 2026-08-07 18:06:08 +08:00
TJWater CI 649af949c5 feat(deploy): include Agent storage migration script in image 2026-08-07 18:00:55 +08:00
TJWater CI 9c9e31c570 fix(ci): provide dependencies to Agent build test stage 2026-08-07 17:58:49 +08:00
TJWater CI c0c54e238d fix(ci): include package manifest in Agent build test stage 2026-08-07 17:54:04 +08:00
TJWater CI 99f5a0b823 ci: run Agent Docker build test target 2026-08-07 17:52:08 +08:00
TJWater CI 4a9681c148 ci: use internal offline build cache 2026-08-07 17:35:56 +08:00
TJWater CI 8530793882 ci: add reusable container deployment workflow 2026-08-07 17:23:21 +08:00
jiang cb3aa3a150 ci: add Dev deployment transport canary 2026-08-07 17:15:27 +08:00
jiang 5ac50bfeaa 切换到使用pg数据库 2026-05-28 18:22:39 +08:00
23 changed files with 1372 additions and 182 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ jobs:
dockerfile: Dockerfile dockerfile: Dockerfile
build_context: . build_context: .
cache_image: gitea.waternetwork.cn/orgtjwater/tjwateragent:ci-cache cache_image: gitea.waternetwork.cn/orgtjwater/tjwateragent:ci-cache
test_target: build test_target: test
deploy_service: agent deploy_service: agent
deploy_host: 192.168.1.114 deploy_host: 192.168.1.114
secrets: secrets:
+5 -1
View File
@@ -2,10 +2,14 @@
description: TJWater Agent,用于供水网络分析和操作员工作流 description: TJWater Agent,用于供水网络分析和操作员工作流
mode: primary mode: primary
model: deepseek/deepseek-v4-flash model: deepseek/deepseek-v4-flash
temperature: 0.2
--- ---
你是 TJWater 供水管网分析 Agent,运用水力专业知识,回复用户时使用简体中文,内容要求简洁准确。 你是 TJWater 供水管网分析 Agent,运用水力专业知识,回复用户时使用简体中文,内容要求简洁准确。
## 回复要求
- 工具执行期间不输出过程说明,全部完成后只回复最终结果
- 直接给出结论、关键数据和可执行建议,默认仅展示最重要的 Top 5;数据不足或任务失败时简要说明影响和下一步
## 工作流生命周期 ## 工作流生命周期
Skills 树是**动态生长的**——工作流不是预置的,而是从实际任务中沉淀出来的: Skills 树是**动态生长的**——工作流不是预置的,而是从实际任务中沉淀出来的:
+2
View File
@@ -37,3 +37,5 @@ PRs should describe runtime behavior changes, list `bun run check` and any test
## Security & Configuration Tips ## Security & Configuration Tips
Do not commit `.env`, logs, session transcripts, generated result references, or `node_modules/`. Keep registry and deploy credentials in Gitea secrets. Do not commit `.env`, logs, session transcripts, generated result references, or `node_modules/`. Keep registry and deploy credentials in Gitea secrets.
Automatic approval for `glob` and `grep` must remain limited to canonical paths inside an explicit safe workspace subtree. Broad workspace-root searches, symlink escapes, external paths, and `.env`, `data/`, or `logs/` targets must stay interactive.
+9
View File
@@ -65,6 +65,15 @@ COPY cli ./cli
COPY .opencode ./.opencode COPY .opencode ./.opencode
RUN bun run check RUN bun run check
FROM build AS test
RUN apt-get update && apt-get install -y --no-install-recommends nodejs && \
rm -rf /var/lib/apt/lists/*
COPY contracts ./contracts
COPY node-tests ./node-tests
COPY scripts ./scripts
COPY tests ./tests
RUN bun run test:ci
FROM build AS runner FROM build AS runner
WORKDIR /app WORKDIR /app
+4 -2
View File
@@ -86,11 +86,13 @@ TJWATER_API_BASE_URL=http://127.0.0.1:8000
`opencode.json` 已启用 `experimental.continue_loop_on_deny`。用户拒绝权限请求后,OpenCode V1 会把拒绝结果交还给 Agent,让其尝试无需该权限的替代方案,而不是直接结束本轮执行。 `opencode.json` 已启用 `experimental.continue_loop_on_deny`。用户拒绝权限请求后,OpenCode V1 会把拒绝结果交还给 Agent,让其尝试无需该权限的替代方案,而不是直接结束本轮执行。
前端提供三种整体权限模式:“请求批准”只执行 OpenCode 明确允许的白名单,其他权限请求逐次交给用户确认;“自动批准”额外自动放行低风险业务工具,其他请求仍需确认“始终允许”自动放行当前对话中所有未被 OpenCode 明确禁止的权限请求。自动放行统一使用单次批准,切换整体模式后立即恢复对应策略,不会写入持久授权。 前端提供三种整体权限模式:“请求批准”只执行 OpenCode 明确允许的白名单,其他权限请求逐次交给用户确认;“自动批准”额外自动放行低风险业务工具、skill,以及真实路径位于工作区安全子树且不涉及 `.env``data/``logs/` 的 glob/grep;工作区根目录的宽泛搜索仍需确认“始终允许”自动放行当前对话中所有未被 OpenCode 明确禁止的权限请求。自动放行统一使用单次批准,切换整体模式后立即恢复对应策略,不会写入持久授权。
单次权限请求支持“允许一次”“保存授权”和“拒绝”。“保存授权”使用 OpenCode 的 `always` 回复,仅保存 OpenCode 为本次请求建议的权限范围,并只在当前 OpenCode 会话内生效。外部目录以及 `.env``data/``logs/` 路径仍由静态配置明确禁止,三种整体模式都不能绕过这些拒绝规则。 单次权限请求支持“允许一次”“保存授权”和“拒绝”。“保存授权”使用 OpenCode 的 `always` 回复,仅保存 OpenCode 为本次请求建议的权限范围,并只在当前 OpenCode 会话内生效。外部目录以及 `.env``data/``logs/` 路径仍由静态配置明确禁止,三种整体模式都不能绕过这些拒绝规则。
`store_render_ref` 只会从 `RESULT_REF_IMPORT_DIR`(默认 `./data/result-imports`)导入包装格式 JSON。文件必须包含 `metadata``location.file_path``data`,且真实路径不能越出导入目录;单文件默认上限为 64 MiB,成功导入后源包装文件会被删除。 `store_render_ref` 只会从 `RESULT_REF_IMPORT_DIR`(默认 `./data/result-imports`)导入包装格式 JSON。文件必须包含 `metadata``location.file_path``data`,且真实路径不能越出导入目录;单文件默认上限为 128 MiB,成功导入后源包装文件会被删除。
CLI 桥接层对 stdout 设置独立的 128 MiB 硬上限(`MAX_CLI_OUTPUT_BYTES`);stderr 最多保留 256 KiB`MAX_CLI_STDERR_BYTES`),超出后截断但不会终止 CLI。`MAX_INLINE_RESULT_BYTES`(默认 12000 字节)只控制 OpenCode 的内联阈值,较大结果由 OpenCode 写入标准 `tool-output` 目录。Agent 启动时及后续定期清理其中超过 `RESULT_REF_TTL_HOURS`(默认 7 天)的 `tool_*` 文件。
## 配置与安全 ## 配置与安全
+18 -9
View File
@@ -1,5 +1,6 @@
import { emitApi } from "../core/http.js"; import { emitApi, requestAllPages } from "../core/http.js";
import { parseOptions, requiredString } from "../core/options.js"; import { optionalNumber, parseOptions, requiredString } from "../core/options.js";
import { success } from "../core/output.js";
import type { HandlerMap, RuntimeContext } from "../core/types.js"; import type { HandlerMap, RuntimeContext } from "../core/types.js";
function apiGet(ctx: RuntimeContext, argv: string[], summary: string, path: string, key: string): Promise<void> { function apiGet(ctx: RuntimeContext, argv: string[], summary: string, path: string, key: string): Promise<void> {
@@ -7,20 +8,28 @@ function apiGet(ctx: RuntimeContext, argv: string[], summary: string, path: stri
return emitApi(ctx, summary, { method: "GET", path, params: { [key]: requiredString(values, key) }, requireProject: true }); return emitApi(ctx, summary, { method: "GET", path, params: { [key]: requiredString(values, key) }, requireProject: true });
} }
function apiGetAll(ctx: RuntimeContext, summary: string, path: string): Promise<void> { async function apiGetAll(ctx: RuntimeContext, argv: string[], summary: string, path: string): Promise<void> {
return emitApi(ctx, summary, { method: "GET", path, requireProject: true }); const { values } = parseOptions(argv, { limit: "integer", "page-size": "integer" });
const requestedPageSize = optionalNumber(values, "page-size") ?? optionalNumber(values, "limit") ?? 1000;
const pageSize = Math.min(1000, Math.max(1, requestedPageSize));
const [data, durationMs] = await requestAllPages(
ctx,
{ method: "GET", path, requireProject: true },
pageSize,
);
success(summary, data, ctx, durationMs);
} }
export const networkHandlers: HandlerMap = { export const networkHandlers: HandlerMap = {
"network get-junction-properties": (ctx, argv) => apiGet(ctx, argv, "读取节点属性成功", "/junctions/properties", "junction"), "network get-junction-properties": (ctx, argv) => apiGet(ctx, argv, "读取节点属性成功", "/junctions/properties", "junction"),
"network get-pipe-properties": (ctx, argv) => apiGet(ctx, argv, "读取管道属性成功", "/pipes/properties", "pipe"), "network get-pipe-properties": (ctx, argv) => apiGet(ctx, argv, "读取管道属性成功", "/pipes/properties", "pipe"),
"network get-all-pipes-properties": (ctx) => apiGetAll(ctx, "读取全部管道属性成功", "/pipes"), "network get-all-pipes-properties": (ctx, argv) => apiGetAll(ctx, argv, "读取全部管道属性成功", "/pipes"),
"network get-reservoir-properties": (ctx, argv) => apiGet(ctx, argv, "读取水库属性成功", "/reservoirs/properties", "reservoir"), "network get-reservoir-properties": (ctx, argv) => apiGet(ctx, argv, "读取水库属性成功", "/reservoirs/properties", "reservoir"),
"network get-all-reservoirs-properties": (ctx) => apiGetAll(ctx, "读取全部水库属性成功", "/reservoirs"), "network get-all-reservoirs-properties": (ctx, argv) => apiGetAll(ctx, argv, "读取全部水库属性成功", "/reservoirs"),
"network get-tank-properties": (ctx, argv) => apiGet(ctx, argv, "读取水箱属性成功", "/tanks/properties", "tank"), "network get-tank-properties": (ctx, argv) => apiGet(ctx, argv, "读取水箱属性成功", "/tanks/properties", "tank"),
"network get-all-tanks-properties": (ctx) => apiGetAll(ctx, "读取全部水箱属性成功", "/tanks"), "network get-all-tanks-properties": (ctx, argv) => apiGetAll(ctx, argv, "读取全部水箱属性成功", "/tanks"),
"network get-pump-properties": (ctx, argv) => apiGet(ctx, argv, "读取水泵属性成功", "/pumps/properties", "pump"), "network get-pump-properties": (ctx, argv) => apiGet(ctx, argv, "读取水泵属性成功", "/pumps/properties", "pump"),
"network get-all-pumps-properties": (ctx) => apiGetAll(ctx, "读取全部水泵属性成功", "/pumps"), "network get-all-pumps-properties": (ctx, argv) => apiGetAll(ctx, argv, "读取全部水泵属性成功", "/pumps"),
"network get-valve-properties": (ctx, argv) => apiGet(ctx, argv, "读取阀门属性成功", "/valves/properties", "valve"), "network get-valve-properties": (ctx, argv) => apiGet(ctx, argv, "读取阀门属性成功", "/valves/properties", "valve"),
"network get-all-valves-properties": (ctx) => apiGetAll(ctx, "读取全部阀门属性成功", "/valves"), "network get-all-valves-properties": (ctx, argv) => apiGetAll(ctx, argv, "读取全部阀门属性成功", "/valves"),
}; };
+94
View File
@@ -76,6 +76,100 @@ export async function requestJson(ctx: RuntimeContext, request: RequestOptions):
return [payload, durationMs]; return [payload, durationMs];
} }
export async function requestAllPages(
ctx: RuntimeContext,
request: RequestOptions,
pageSize: number,
): Promise<[unknown[], number]> {
const items: unknown[] = [];
let durationMs = 0;
let offset = 0;
let expectedTotal: number | null = null;
while (expectedTotal === null || offset < expectedTotal) {
const [payload, pageDurationMs] = await requestJson(ctx, {
...request,
params: {
...request.params,
limit: pageSize,
offset,
},
});
durationMs += pageDurationMs;
const page = normalizePage(payload);
if (!page) {
throw new CliError(
"服务端错误",
"INVALID_PAGINATION_RESPONSE",
"backend collection response must contain items, total, limit, and offset",
7,
false,
payload,
);
}
if (expectedTotal === null) {
expectedTotal = page.total;
} else if (page.total !== expectedTotal) {
throw new CliError(
"服务端错误",
"PAGINATION_TOTAL_CHANGED",
`backend collection total changed from ${expectedTotal} to ${page.total}`,
7,
true,
);
}
if (page.offset !== offset) {
throw new CliError(
"服务端错误",
"PAGINATION_OFFSET_MISMATCH",
`backend collection returned offset ${page.offset}, expected ${offset}`,
7,
true,
);
}
if (page.items.length === 0 && offset < expectedTotal) {
throw new CliError(
"服务端错误",
"PAGINATION_STALLED",
`backend collection returned an empty page at offset ${offset} before total ${expectedTotal}`,
7,
true,
);
}
items.push(...page.items);
offset += page.items.length;
}
return [items.slice(0, expectedTotal ?? 0), durationMs];
}
function normalizePage(
payload: unknown,
): { items: unknown[]; limit: number; offset: number; total: number } | null {
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return null;
const page = payload as Record<string, unknown>;
if (
!Array.isArray(page.items) ||
typeof page.limit !== "number" ||
!Number.isInteger(page.limit) ||
typeof page.offset !== "number" ||
!Number.isInteger(page.offset) ||
typeof page.total !== "number" ||
!Number.isInteger(page.total) ||
page.limit <= 0 ||
page.offset < 0 ||
page.total < 0
) {
return null;
}
return {
items: page.items,
limit: page.limit,
offset: page.offset,
total: page.total,
};
}
function mapStatus(status: number): number { function mapStatus(status: number): number {
if (status === 400 || status === 422) return 2; if (status === 400 || status === 422) return 2;
if (status === 401) return 3; if (status === 401) return 3;
+5 -5
View File
@@ -32,15 +32,15 @@ type CommandSpec = readonly [path: string, summary: string, options: readonly st
const commandSpecs: readonly CommandSpec[] = [ const commandSpecs: readonly CommandSpec[] = [
["network get-junction-properties", "读取节点属性", ["--junction <JUNCTION>"], ["tjwater-cli network get-junction-properties --junction J1"]], ["network get-junction-properties", "读取节点属性", ["--junction <JUNCTION>"], ["tjwater-cli network get-junction-properties --junction J1"]],
["network get-pipe-properties", "读取管道属性", ["--pipe <PIPE>"], ["tjwater-cli network get-pipe-properties --pipe P1"]], ["network get-pipe-properties", "读取管道属性", ["--pipe <PIPE>"], ["tjwater-cli network get-pipe-properties --pipe P1"]],
["network get-all-pipes-properties", "读取全部管道属性", [], ["tjwater-cli network get-all-pipes-properties"]], ["network get-all-pipes-properties", "读取全部管道属性", ["[--page-size <PAGE_SIZE>]"], ["tjwater-cli network get-all-pipes-properties"]],
["network get-reservoir-properties", "读取水库属性", ["--reservoir <RESERVOIR>"], ["tjwater-cli network get-reservoir-properties --reservoir R1"]], ["network get-reservoir-properties", "读取水库属性", ["--reservoir <RESERVOIR>"], ["tjwater-cli network get-reservoir-properties --reservoir R1"]],
["network get-all-reservoirs-properties", "读取全部水库属性", [], ["tjwater-cli network get-all-reservoirs-properties"]], ["network get-all-reservoirs-properties", "读取全部水库属性", ["[--page-size <PAGE_SIZE>]"], ["tjwater-cli network get-all-reservoirs-properties"]],
["network get-tank-properties", "读取水箱属性", ["--tank <TANK>"], ["tjwater-cli network get-tank-properties --tank T1"]], ["network get-tank-properties", "读取水箱属性", ["--tank <TANK>"], ["tjwater-cli network get-tank-properties --tank T1"]],
["network get-all-tanks-properties", "读取全部水箱属性", [], ["tjwater-cli network get-all-tanks-properties"]], ["network get-all-tanks-properties", "读取全部水箱属性", ["[--page-size <PAGE_SIZE>]"], ["tjwater-cli network get-all-tanks-properties"]],
["network get-pump-properties", "读取水泵属性", ["--pump <PUMP>"], ["tjwater-cli network get-pump-properties --pump PU1"]], ["network get-pump-properties", "读取水泵属性", ["--pump <PUMP>"], ["tjwater-cli network get-pump-properties --pump PU1"]],
["network get-all-pumps-properties", "读取全部水泵属性", [], ["tjwater-cli network get-all-pumps-properties"]], ["network get-all-pumps-properties", "读取全部水泵属性", ["[--page-size <PAGE_SIZE>]"], ["tjwater-cli network get-all-pumps-properties"]],
["network get-valve-properties", "读取阀门属性", ["--valve <VALVE>"], ["tjwater-cli network get-valve-properties --valve V1"]], ["network get-valve-properties", "读取阀门属性", ["--valve <VALVE>"], ["tjwater-cli network get-valve-properties --valve V1"]],
["network get-all-valves-properties", "读取全部阀门属性", [], ["tjwater-cli network get-all-valves-properties"]], ["network get-all-valves-properties", "读取全部阀门属性", ["[--page-size <PAGE_SIZE>]"], ["tjwater-cli network get-all-valves-properties"]],
["component option schema", "读取选项 schema", ["--kind <KIND>", "[--pump <PUMP>]"], ["tjwater-cli component option schema --kind time", "tjwater-cli component option schema --kind energy", "tjwater-cli component option schema --kind pump-energy --pump PUMP1", "tjwater-cli component option schema --kind network"]], ["component option schema", "读取选项 schema", ["--kind <KIND>", "[--pump <PUMP>]"], ["tjwater-cli component option schema --kind time", "tjwater-cli component option schema --kind energy", "tjwater-cli component option schema --kind pump-energy --pump PUMP1", "tjwater-cli component option schema --kind network"]],
["component option get", "读取选项属性", ["--kind <KIND>", "[--pump <PUMP>]"], ["tjwater-cli component option get --kind time", "tjwater-cli component option get --kind energy", "tjwater-cli component option get --kind pump-energy --pump PUMP1", "tjwater-cli component option get --kind network"]], ["component option get", "读取选项属性", ["--kind <KIND>", "[--pump <PUMP>]"], ["tjwater-cli component option get --kind time", "tjwater-cli component option get --kind energy", "tjwater-cli component option get --kind pump-energy --pump PUMP1", "tjwater-cli component option get --kind network"]],
["simulation run", "触发指定绝对时间的模拟运行", ["--start-time <START_TIME>", "--duration <DURATION>"], ["tjwater-cli simulation run --start-time 2025-01-02T03:04:05+08:00 --duration 30"], ["tjwater-cli data timeseries realtime links --start-time 2025-01-02T03:04:05+08:00 --end-time 2025-01-02T03:34:05+08:00", "tjwater-cli data timeseries realtime nodes --start-time 2025-01-02T03:04:05+08:00 --end-time 2025-01-02T03:34:05+08:00"]], ["simulation run", "触发指定绝对时间的模拟运行", ["--start-time <START_TIME>", "--duration <DURATION>"], ["tjwater-cli simulation run --start-time 2025-01-02T03:04:05+08:00 --duration 30"], ["tjwater-cli data timeseries realtime links --start-time 2025-01-02T03:04:05+08:00 --end-time 2025-01-02T03:34:05+08:00", "tjwater-cli data timeseries realtime nodes --start-time 2025-01-02T03:04:05+08:00 --end-time 2025-01-02T03:34:05+08:00"]],
+65 -2
View File
@@ -104,7 +104,11 @@ async function startJsonServer(responseData) {
url: req.url, url: req.url,
}); });
res.setHeader("content-type", "application/json"); res.setHeader("content-type", "application/json");
res.end(JSON.stringify(responseData)); res.end(
JSON.stringify(
typeof responseData === "function" ? responseData(req) : responseData,
),
);
}); });
await new Promise((resolveListen, reject) => { await new Promise((resolveListen, reject) => {
@@ -138,7 +142,20 @@ function normalizeSeenRequest(request) {
}; };
} }
async function runAgainstServer(name, runner, args, auth, responseData = { accepted: true }) { function defaultContractResponse(req) {
const url = new URL(req.url, "http://127.0.0.1");
if (["/api/v1/pipes", "/api/v1/reservoirs", "/api/v1/tanks", "/api/v1/pumps", "/api/v1/valves"].includes(url.pathname)) {
return {
items: [],
limit: Number(url.searchParams.get("limit")),
offset: Number(url.searchParams.get("offset")),
total: 0,
};
}
return { accepted: true };
}
async function runAgainstServer(name, runner, args, auth, responseData = defaultContractResponse) {
const server = await startJsonServer(responseData); const server = await startJsonServer(responseData);
try { try {
const result = await runner(["--auth-stdin", ...args], { ...auth, server: server.url }); const result = await runner(["--auth-stdin", ...args], { ...auth, server: server.url });
@@ -232,6 +249,52 @@ test("sends auth headers and simulation body through the backend API contract",
} }
}); });
test("get-all network commands collect every backend page", async () => {
const items = Array.from({ length: 2_005 }, (_, index) => ({
id: `P${index + 1}`,
node1: `N${index + 1}`,
node2: `N${index + 2}`,
}));
const server = await startJsonServer((req) => {
const url = new URL(req.url, "http://127.0.0.1");
const limit = Number(url.searchParams.get("limit") ?? 100);
const offset = Number(url.searchParams.get("offset") ?? 0);
return {
items: items.slice(offset, offset + limit),
limit,
offset,
total: items.length,
};
});
try {
const result = await runCli(
["--auth-stdin", "network", "get-all-pipes-properties"],
{
server: server.url,
access_token: "token-1",
project_id: "project-1",
},
);
assert.equal(result.exitCode, 0, result.stderr);
const payload = parseJsonResult(result);
assert.equal(payload.data.length, items.length);
assert.deepEqual(payload.data[0], items[0]);
assert.deepEqual(payload.data.at(-1), items.at(-1));
assert.deepEqual(
server.seen.map((request) => normalizeSeenRequest(request).query),
[
{ limit: "1000", offset: "0" },
{ limit: "1000", offset: "1000" },
{ limit: "1000", offset: "2000" },
],
);
} finally {
await server.close();
}
});
test("uses project scoped headers for realtime data commands", async () => { test("uses project scoped headers for realtime data commands", async () => {
const server = await startJsonServer([{ id: "P1" }]); const server = await startJsonServer([{ id: "P1" }]);
try { try {
+2
View File
@@ -8,7 +8,9 @@
"install:opencode": "bun install --cwd .opencode", "install:opencode": "bun install --cwd .opencode",
"typecheck": "tsc --noEmit -p tsconfig.json", "typecheck": "tsc --noEmit -p tsconfig.json",
"typecheck:opencode": "bun run --cwd .opencode typecheck", "typecheck:opencode": "bun run --cwd .opencode typecheck",
"test": "bun test tests",
"test:cli": "node --test node-tests/cli/*.node.mjs", "test:cli": "node --test node-tests/cli/*.node.mjs",
"test:ci": "bun run contract:check && /usr/bin/node --test node-tests/cli/*.node.mjs && bun test tests",
"dev": "bun --watch src/server.ts", "dev": "bun --watch src/server.ts",
"build": "bun run check", "build": "bun run check",
"check": "bun run typecheck && bun run typecheck:opencode", "check": "bun run typecheck && bun run typecheck:opencode",
+215
View File
@@ -0,0 +1,215 @@
import { spawn } from "node:child_process";
import { type RuntimeSessionContext } from "../runtime/sessionContext.js";
type OutputStream = "stdout" | "stderr";
export type CliExecutionResult = {
outcome: "completed" | "timeout" | "output_limit";
exitCode: number | null;
signal: NodeJS.Signals | null;
status: number;
stderr: string;
stderrTruncated: boolean;
stdout: string;
exceededStream?: OutputStream;
};
type ExecuteCliCommandOptions = {
apiBaseUrl: string;
cliPath: string;
maxStderrBytes: number;
maxStdoutBytes: number;
terminationGraceMs?: number;
};
const getCompletedStatus = (exitCode: number | null, stdout: string) => {
let errorCode = "";
try {
const payload = JSON.parse(stdout) as { error?: { code?: unknown } };
errorCode =
typeof payload.error?.code === "string" ? payload.error.code : "";
} catch {
errorCode = "";
}
if (errorCode === "HTTP_401" || errorCode === "UNAUTHENTICATED") {
return 401;
}
if (errorCode === "HTTP_403") {
return 403;
}
return exitCode === 0 ? 200 : 502;
};
export const executeCliCommand = async (
context: RuntimeSessionContext,
command: string,
timeoutSec: number,
options: ExecuteCliCommandOptions,
): Promise<CliExecutionResult> => {
if (!Number.isSafeInteger(options.maxStdoutBytes) || options.maxStdoutBytes <= 0) {
throw new Error("maxStdoutBytes must be a positive safe integer");
}
if (!Number.isSafeInteger(options.maxStderrBytes) || options.maxStderrBytes <= 0) {
throw new Error("maxStderrBytes must be a positive safe integer");
}
const child = spawn(
options.cliPath,
["--auth-stdin", ...command.split(/\s+/).filter(Boolean)],
{ stdio: ["pipe", "pipe", "pipe"] },
);
const stdoutChunks: Buffer[] = [];
const stderrChunks: Buffer[] = [];
let stdoutBytes = 0;
let stderrBytes = 0;
let stderrTruncated = false;
let terminationReason:
| "timeout"
| "output_limit"
| "execution_error"
| null = null;
let exceededStream: OutputStream | undefined;
let terminationStarted = false;
let settled = false;
let executionError: Error | null = null;
let forceKillTimer: ReturnType<typeof setTimeout> | undefined;
const result = await new Promise<CliExecutionResult>((resolve, reject) => {
const cleanup = () => {
clearTimeout(timeoutTimer);
if (forceKillTimer) {
clearTimeout(forceKillTimer);
}
};
const terminate = (
reason: "timeout" | "output_limit" | "execution_error",
) => {
if (terminationStarted) {
return;
}
terminationStarted = true;
terminationReason = reason;
if (child.exitCode === null && child.signalCode === null) {
child.kill("SIGTERM");
}
forceKillTimer = setTimeout(() => {
if (child.exitCode === null && child.signalCode === null) {
child.kill("SIGKILL");
}
}, options.terminationGraceMs ?? 1500);
};
const captureStdout = (data: Buffer) => {
if (terminationReason) {
return;
}
if (stdoutBytes + data.length > options.maxStdoutBytes) {
exceededStream = "stdout";
terminate("output_limit");
return;
}
stdoutChunks.push(data);
stdoutBytes += data.length;
};
const captureStderr = (data: Buffer) => {
if (terminationReason || stderrTruncated) {
return;
}
const remainingBytes = options.maxStderrBytes - stderrBytes;
if (data.length > remainingBytes) {
if (remainingBytes > 0) {
stderrChunks.push(data.subarray(0, remainingBytes));
stderrBytes += remainingBytes;
}
stderrTruncated = true;
return;
}
stderrChunks.push(data);
stderrBytes += data.length;
};
const timeoutTimer = setTimeout(() => {
if (child.exitCode === null && child.signalCode === null) {
terminate("timeout");
}
}, timeoutSec * 1000);
child.stdout.on("data", captureStdout);
child.stderr.on("data", captureStderr);
child.stdin.on("error", (error) => {
if (terminationReason === null) {
executionError = error;
terminate("execution_error");
}
});
child.on("error", (error) => {
if (terminationReason === null) {
executionError = error;
terminate("execution_error");
}
});
child.on("close", (exitCode, signal) => {
if (settled) {
return;
}
settled = true;
cleanup();
if (terminationReason === "timeout") {
resolve({
outcome: "timeout",
exitCode,
signal,
status: 504,
stderr: "",
stderrTruncated,
stdout: "",
});
return;
}
if (executionError) {
reject(executionError);
return;
}
if (terminationReason === "output_limit") {
resolve({
outcome: "output_limit",
exceededStream,
exitCode,
signal,
status: 502,
stderr: "",
stderrTruncated,
stdout: "",
});
return;
}
const stdout = Buffer.concat(stdoutChunks, stdoutBytes).toString("utf-8");
const stderr = Buffer.concat(stderrChunks, stderrBytes).toString("utf-8");
resolve({
outcome: "completed",
exitCode,
signal,
status: getCompletedStatus(exitCode, stdout),
stderr,
stderrTruncated,
stdout,
});
});
child.stdin.end(
JSON.stringify({
server: options.apiBaseUrl,
access_token: context.accessToken,
project_id: context.projectId,
}),
);
});
return result;
};
+14 -2
View File
@@ -61,8 +61,20 @@ const envSchema = z
TJWATER_API_BASE_URL: z.string().default("http://127.0.0.1:8000"), TJWATER_API_BASE_URL: z.string().default("http://127.0.0.1:8000"),
// 代理调用 TJWater 后端 API 的超时时间(毫秒)。 // 代理调用 TJWater 后端 API 的超时时间(毫秒)。
TJWATER_API_TIMEOUT_MS: z.coerce.number().int().positive().default(30000), TJWATER_API_TIMEOUT_MS: z.coerce.number().int().positive().default(30000),
// 后端结果在直接内联返回给模型前允许的最大字节数 // OpenCode 工具结果以内联形式返回给模型的阈值;更大的结果由 OpenCode 落盘
MAX_INLINE_RESULT_BYTES: z.coerce.number().int().positive().default(12000), MAX_INLINE_RESULT_BYTES: z.coerce.number().int().positive().default(12000),
// 单次 tjwater-cli stdout 的硬上限;超过后终止子进程。
MAX_CLI_OUTPUT_BYTES: z.coerce
.number()
.int()
.positive()
.default(128 * 1024 * 1024),
// 单次 tjwater-cli stderr 最多保留的字节数;超过后截断但不终止进程。
MAX_CLI_STDERR_BYTES: z.coerce
.number()
.int()
.positive()
.default(256 * 1024),
// 生成结果 preview 时最多抽样的条目数。 // 生成结果 preview 时最多抽样的条目数。
MAX_PREVIEW_SAMPLE_ITEMS: z.coerce.number().int().positive().default(3), MAX_PREVIEW_SAMPLE_ITEMS: z.coerce.number().int().positive().default(3),
// memory 持久化存储目录。 // memory 持久化存储目录。
@@ -110,7 +122,7 @@ const envSchema = z
.number() .number()
.int() .int()
.positive() .positive()
.default(64 * 1024 * 1024), .default(128 * 1024 * 1024),
// result_ref 保留时长(小时)。 // result_ref 保留时长(小时)。
RESULT_REF_TTL_HOURS: z.coerce.number().int().positive().default(168), RESULT_REF_TTL_HOURS: z.coerce.number().int().positive().default(168),
// 定时清理过期 result_ref 的扫描周期(毫秒)。 // 定时清理过期 result_ref 的扫描周期(毫秒)。
+168 -2
View File
@@ -1,5 +1,14 @@
import { lstatSync, readdirSync, realpathSync } from "node:fs";
import { isAbsolute, relative, resolve, sep } from "node:path";
export type ApprovalMode = "request" | "auto" | "always"; export type ApprovalMode = "request" | "auto" | "always";
export type PermissionApprovalContext = {
metadata?: Record<string, unknown>;
patterns?: readonly string[];
workspaceRoot?: string;
};
const lowRiskToolPermissions = new Set([ const lowRiskToolPermissions = new Set([
"apply_layer_style", "apply_layer_style",
"geocode", "geocode",
@@ -12,10 +21,31 @@ const lowRiskToolPermissions = new Set([
"zoom_to_map", "zoom_to_map",
]); ]);
const lowRiskSearchRootNames = new Set([
".opencode",
"cli",
"contracts",
"node-tests",
"scripts",
"src",
"tests",
]);
const normalizePermission = (permission: string) => permission.trim().toLowerCase(); const normalizePermission = (permission: string) => permission.trim().toLowerCase();
export const canAutoApprovePermission = (permission: string): boolean => { export const canAutoApprovePermission = (
permission: string,
context: PermissionApprovalContext = {},
): boolean => {
const normalized = normalizePermission(permission); const normalized = normalizePermission(permission);
if (normalized === "skill") {
return true;
}
if (normalized === "glob" || normalized === "grep") {
return isSafeWorkspaceSearch(normalized, context);
}
if (lowRiskToolPermissions.has(normalized)) { if (lowRiskToolPermissions.has(normalized)) {
return true; return true;
} }
@@ -30,6 +60,7 @@ export const canAutoApprovePermission = (permission: string): boolean => {
export const resolvePermissionApproval = ( export const resolvePermissionApproval = (
approvalMode: ApprovalMode, approvalMode: ApprovalMode,
permission: string, permission: string,
context: PermissionApprovalContext = {},
) => { ) => {
if (approvalMode === "always") { if (approvalMode === "always") {
return { return {
@@ -40,7 +71,7 @@ export const resolvePermissionApproval = (
} as const; } as const;
} }
if (approvalMode === "auto" && canAutoApprovePermission(permission)) { if (approvalMode === "auto" && canAutoApprovePermission(permission, context)) {
return { return {
autoApprove: true, autoApprove: true,
title: "已自动批准低风险权限", title: "已自动批准低风险权限",
@@ -54,3 +85,138 @@ export const resolvePermissionApproval = (
detail: undefined, detail: undefined,
} as const; } as const;
}; };
const isSafeWorkspaceSearch = (
permission: "glob" | "grep",
context: PermissionApprovalContext,
): boolean => {
const workspaceRoot = context.workspaceRoot?.trim();
if (!workspaceRoot) {
return false;
}
const requestedPath =
typeof context.metadata?.path === "string" && context.metadata.path.trim()
? context.metadata.path
: workspaceRoot;
let root: string;
let searchRoot: string;
try {
root = realpathSync.native(resolve(workspaceRoot));
searchRoot = realpathSync.native(resolve(root, requestedPath));
} catch {
return false;
}
let relativePath = relative(root, searchRoot);
if (
relativePath === ".." ||
relativePath.startsWith(`..${sep}`) ||
isAbsolute(relativePath)
) {
return false;
}
const expressions: string[] = [];
if (permission === "glob" && typeof context.metadata?.pattern === "string") {
expressions.push(context.metadata.pattern);
}
if (permission === "glob") {
expressions.push(...(context.patterns ?? []));
}
if (typeof context.metadata?.include === "string") {
expressions.push(context.metadata.include);
}
if (
expressions.some(
(expression) =>
isAbsolute(expression) ||
containsParentTraversal(expression) ||
containsAmbiguousGlobSyntax(expression) ||
containsProtectedPath(expression),
)
) {
return false;
}
if (!relativePath) {
if (permission !== "glob") {
return false;
}
const literalPrefix = getLiteralGlobPrefix(expressions[0]);
if (!literalPrefix) {
return false;
}
try {
searchRoot = realpathSync.native(resolve(root, literalPrefix));
} catch {
return false;
}
relativePath = relative(root, searchRoot);
}
return (
relativePath !== "" &&
relativePath !== ".." &&
!relativePath.startsWith(`..${sep}`) &&
!isAbsolute(relativePath) &&
!containsProtectedPath(relativePath) &&
isSafeSearchTarget(searchRoot, relativePath)
);
};
const isSafeSearchTarget = (searchRoot: string, relativePath: string): boolean => {
try {
const target = lstatSync(searchRoot);
if (target.isFile()) {
return true;
}
if (!target.isDirectory()) {
return false;
}
const topLevelName = relativePath.split(sep)[0];
if (!topLevelName || !lowRiskSearchRootNames.has(topLevelName)) {
return false;
}
const pending = [searchRoot];
while (pending.length > 0) {
const directory = pending.pop()!;
for (const entry of readdirSync(directory, { withFileTypes: true })) {
if (entry.isSymbolicLink() || containsProtectedPath(entry.name)) {
return false;
}
if (entry.isDirectory()) {
pending.push(resolve(directory, entry.name));
}
}
}
return true;
} catch {
return false;
}
};
const getLiteralGlobPrefix = (expression: string | undefined): string | null => {
const firstSegment = expression
?.replaceAll("\\", "/")
.replace(/^\.\//, "")
.split("/")[0];
return firstSegment && !/[*?[\]{}()!+@]/.test(firstSegment)
? firstSegment
: null;
};
const containsParentTraversal = (value: string): boolean =>
value.replaceAll("\\", "/").split("/").includes("..");
const containsAmbiguousGlobSyntax = (value: string): boolean =>
/[?[\]{}()!+@\\]/.test(value);
const containsProtectedPath = (value: string): boolean => {
const normalized = value.replaceAll("\\", "/").toLowerCase();
return (
normalized.includes(".env") ||
/(?:^|[^a-z0-9_-])(?:data|logs)(?:$|[^a-z0-9_-])/.test(normalized)
);
};
+66 -67
View File
@@ -111,24 +111,40 @@ const toRuntimeModel = (model?: SupportedModel) => {
}; };
}; };
const emitFallbackMessage = async ( const emitFinalMessage = async (
runtime: OpencodeRuntimeAdapter, runtime: OpencodeRuntimeAdapter,
sessionId: string, sessionId: string,
clientSessionId: string, clientSessionId: string,
currentAssistantMessageIds: Set<string>,
assistantTextParts: Map<string, Map<string, string>>,
write: (event: string, data: Record<string, unknown>) => void, write: (event: string, data: Record<string, unknown>) => void,
) => { ) => {
const messages = await runtime.messages(sessionId); let text = [...currentAssistantMessageIds]
const assistantMessage = [...messages]
.reverse() .reverse()
.find((message) => message.info.role === "assistant"); .map((messageId) => [...(assistantTextParts.get(messageId)?.values() ?? [])].join(""))
const parts = assistantMessage?.parts ?? []; .find((content) => content.length > 0) ?? "";
const text = collectTextContent(parts);
if (!text) {
const messages = await runtime.messages(sessionId);
const assistantMessage = [...messages]
.reverse()
.find(
(message) =>
message.info.role === "assistant" &&
(currentAssistantMessageIds.size === 0 ||
currentAssistantMessageIds.has(message.info.id)),
);
text = collectTextContent(assistantMessage?.parts ?? []);
}
if (text) { if (text) {
write("token", { write("token", {
session_id: clientSessionId, session_id: clientSessionId,
content: text, content: text,
}); });
return true;
} }
return false;
}; };
export const streamPromptResponse = async ({ export const streamPromptResponse = async ({
@@ -156,15 +172,14 @@ export const streamPromptResponse = async ({
const emittedToolParts = new Set<string>(); const emittedToolParts = new Set<string>();
const emittedQuestionToolParts = new Set<string>(); const emittedQuestionToolParts = new Set<string>();
const emittedQuestionRequestIds = new Set<string>(); const emittedQuestionRequestIds = new Set<string>();
const currentAssistantMessageIds = new Set<string>();
const assistantTextParts = new Map<string, Map<string, string>>();
const partTypes = new Map<string, Part["type"]>(); const partTypes = new Map<string, Part["type"]>();
const pendingPartTextDeltas = new Map<string, string[]>(); const pendingTextDeltas = new Map<string, string[]>();
const reasoningDeltas = new Map<string, string[]>();
const reasoningStatuses = new Map<string, "running" | "completed">(); const reasoningStatuses = new Map<string, "running" | "completed">();
const toolStatuses = new Map<string, string>(); const toolStatuses = new Map<string, string>();
let firstSessionEventLogged = false; let firstSessionEventLogged = false;
let firstNonStatusEventLogged = false; let firstNonStatusEventLogged = false;
let firstTokenLogged = false;
let firstReasoningLogged = false;
let firstToolEventLogged = false; let firstToolEventLogged = false;
let lastSessionStatus: string | null = null; let lastSessionStatus: string | null = null;
let lastSessionStatusMessage: string | null = null; let lastSessionStatusMessage: string | null = null;
@@ -379,6 +394,11 @@ export const streamPromptResponse = async ({
const permissionApproval = resolvePermissionApproval( const permissionApproval = resolvePermissionApproval(
approvalMode, approvalMode,
event.properties.permission, event.properties.permission,
{
metadata: event.properties.metadata,
patterns: event.properties.patterns,
workspaceRoot: process.cwd(),
},
); );
logDevelopmentDebug("permission request received", { logDevelopmentDebug("permission request received", {
...debugContext, ...debugContext,
@@ -425,6 +445,11 @@ export const streamPromptResponse = async ({
const permissionApproval = resolvePermissionApproval( const permissionApproval = resolvePermissionApproval(
approvalMode, approvalMode,
event.properties.action, event.properties.action,
{
metadata: event.properties.metadata,
patterns: event.properties.resources,
workspaceRoot: process.cwd(),
},
); );
logDevelopmentDebug("permission v2 request received", { logDevelopmentDebug("permission v2 request received", {
...debugContext, ...debugContext,
@@ -626,45 +651,26 @@ export const streamPromptResponse = async ({
if (event.type === "message.updated") { if (event.type === "message.updated") {
if (event.properties.info.role === "assistant") { if (event.properties.info.role === "assistant") {
sawResponseActivity = true; sawResponseActivity = true;
currentAssistantMessageIds.add(event.properties.info.id);
} }
continue; continue;
} }
if (event.type === "message.part.delta" && event.properties.field === "text") { if (event.type === "message.part.delta" && event.properties.field === "text") {
sawResponseActivity = true; sawResponseActivity = true;
currentAssistantMessageIds.add(event.properties.messageID);
const partType = partTypes.get(event.properties.partID); const partType = partTypes.get(event.properties.partID);
if (partType === "text") { if (partType === "text") {
if (!firstTokenLogged) { const messageParts = assistantTextParts.get(event.properties.messageID) ?? new Map();
firstTokenLogged = true; messageParts.set(
logDevelopmentDebug("first response token emitted", { event.properties.partID,
...debugContext, `${messageParts.get(event.properties.partID) ?? ""}${event.properties.delta}`,
partId: event.properties.partID, );
elapsedMs: Math.max(0, Date.now() - requestStartedAt), assistantTextParts.set(event.properties.messageID, messageParts);
sincePromptDispatchMs: Math.max(0, Date.now() - promptStartedAt),
});
}
emittedText = true;
write("token", {
session_id: clientSessionId,
content: event.properties.delta,
});
} else if (partType === "reasoning") {
if (!firstReasoningLogged) {
firstReasoningLogged = true;
logDevelopmentDebug("first reasoning delta received", {
...debugContext,
partId: event.properties.partID,
elapsedMs: Math.max(0, Date.now() - requestStartedAt),
sincePromptDispatchMs: Math.max(0, Date.now() - promptStartedAt),
});
}
const pending = reasoningDeltas.get(event.properties.partID) ?? [];
pending.push(event.properties.delta);
reasoningDeltas.set(event.properties.partID, pending);
} else if (!partType) { } else if (!partType) {
const pending = pendingPartTextDeltas.get(event.properties.partID) ?? []; const pending = pendingTextDeltas.get(event.properties.partID) ?? [];
pending.push(event.properties.delta); pending.push(event.properties.delta);
pendingPartTextDeltas.set(event.properties.partID, pending); pendingTextDeltas.set(event.properties.partID, pending);
} }
continue; continue;
} }
@@ -673,23 +679,19 @@ export const streamPromptResponse = async ({
sawResponseActivity = true; sawResponseActivity = true;
const part = event.properties.part; const part = event.properties.part;
partTypes.set(part.id, part.type); partTypes.set(part.id, part.type);
if (part.type === "text" || part.type === "reasoning" || part.type === "tool") {
currentAssistantMessageIds.add(part.messageID);
}
if (part.type === "text") { if (part.type === "text") {
const pending = pendingPartTextDeltas.get(part.id) ?? []; const pendingText = (pendingTextDeltas.get(part.id) ?? []).join("");
pendingPartTextDeltas.delete(part.id); pendingTextDeltas.delete(part.id);
for (const content of pending) { const messageParts = assistantTextParts.get(part.messageID) ?? new Map();
emittedText = true; messageParts.set(part.id, part.text || pendingText);
write("token", { assistantTextParts.set(part.messageID, messageParts);
session_id: clientSessionId, } else {
content, pendingTextDeltas.delete(part.id);
}); }
} if (part.type === "reasoning") {
} else if (part.type === "reasoning") {
const pending = pendingPartTextDeltas.get(part.id) ?? [];
if (pending.length > 0) {
const existing = reasoningDeltas.get(part.id) ?? [];
reasoningDeltas.set(part.id, existing.concat(pending));
}
pendingPartTextDeltas.delete(part.id);
const reasoningStatus = part.time.end ? "completed" : "running"; const reasoningStatus = part.time.end ? "completed" : "running";
if (reasoningStatuses.get(part.id) !== reasoningStatus) { if (reasoningStatuses.get(part.id) !== reasoningStatus) {
reasoningStatuses.set(part.id, reasoningStatus); reasoningStatuses.set(part.id, reasoningStatus);
@@ -697,14 +699,10 @@ export const streamPromptResponse = async ({
...debugContext, ...debugContext,
partId: part.id, partId: part.id,
status: reasoningStatus, status: reasoningStatus,
chunkCount: (reasoningDeltas.get(part.id) ?? []).length,
elapsedMs: Math.max(0, Date.now() - requestStartedAt), elapsedMs: Math.max(0, Date.now() - requestStartedAt),
}); });
} }
const reasoningDetail = buildReasoningProgressDetail( const reasoningDetail = buildReasoningProgressDetail(part.time.end);
reasoningDeltas.get(part.id) ?? [],
part.time.end,
);
emitProgress({ emitProgress({
id: part.id, id: part.id,
phase: "planning", phase: "planning",
@@ -932,13 +930,14 @@ export const streamPromptResponse = async ({
} }
await promptPromise; await promptPromise;
if (!emittedText) { emittedText = await emitFinalMessage(
logDevelopmentDebug("no streamed text emitted, falling back to messages()", { runtime,
...debugContext, sessionId,
elapsedMs: Math.max(0, Date.now() - requestStartedAt), clientSessionId,
}); currentAssistantMessageIds,
await emitFallbackMessage(runtime, sessionId, clientSessionId, write); assistantTextParts,
} write,
);
emitProgress({ emitProgress({
id: "request-received", id: "request-received",
phase: "start", phase: "start",
+1 -18
View File
@@ -376,12 +376,6 @@ const formatProgressValue = (value: unknown): string => {
} }
}; };
const normalizeProgressText = (chunks: string[]) =>
chunks.join("").replace(/\s+/g, " ").trim();
const truncateProgressText = (text: string, maxLength: number) =>
text.length > maxLength ? `${text.slice(0, maxLength - 3)}...` : text;
const summarizeToolParams = (params: Record<string, unknown>) => { const summarizeToolParams = (params: Record<string, unknown>) => {
const ignoredKeys = new Set(["reason", "request_reason", "why", "purpose", "rationale"]); const ignoredKeys = new Set(["reason", "request_reason", "why", "purpose", "rationale"]);
const summary = Object.entries(params) const summary = Object.entries(params)
@@ -413,19 +407,8 @@ export const buildSessionStatusDetail = (status: { type: string; message?: strin
}; };
export const buildReasoningProgressDetail = ( export const buildReasoningProgressDetail = (
chunks: string[],
ended?: string | number | Date | null, ended?: string | number | Date | null,
) => { ) => ended ? "分析步骤已整理完成。" : "Agent 正在分析问题。";
const reasoningText = truncateProgressText(normalizeProgressText(chunks), 800);
if (ended) {
return reasoningText
? `推理过程:${reasoningText}`
: "当前推理阶段已完成,Agent 将继续输出答案或进入工具执行。";
}
return reasoningText
? `正在推理:${reasoningText}`
: "Agent 正在拆解问题、梳理执行步骤并判断是否需要调用工具。";
};
export const buildToolProgressDetail = ( export const buildToolProgressDetail = (
tool: string, tool: string,
+45
View File
@@ -7,6 +7,10 @@ import { resolve } from "node:path";
import { config } from "../config.js"; import { config } from "../config.js";
import { logger } from "../logger.js"; import { logger } from "../logger.js";
import {
cleanupExpiredToolOutputs,
resolveOpencodeToolOutputDirectory,
} from "./opencodeToolOutputCleanup.js";
const isDevelopmentDebugLoggingEnabled = process.env.NODE_ENV === "development"; const isDevelopmentDebugLoggingEnabled = process.env.NODE_ENV === "development";
@@ -46,6 +50,7 @@ const getRuntimeMessageId = (message: RuntimeMessage) => message.info.id;
export class OpencodeRuntimeAdapter { export class OpencodeRuntimeAdapter {
private clientPromise: Promise<OpencodeClient> | null = null; private clientPromise: Promise<OpencodeClient> | null = null;
private closeServer: (() => void) | null = null; private closeServer: (() => void) | null = null;
private toolOutputCleanupTimer: ReturnType<typeof setInterval> | null = null;
async ensureClient(): Promise<OpencodeClient> { async ensureClient(): Promise<OpencodeClient> {
if (!this.clientPromise) { if (!this.clientPromise) {
@@ -380,12 +385,18 @@ export class OpencodeRuntimeAdapter {
} }
async dispose(): Promise<void> { async dispose(): Promise<void> {
if (this.toolOutputCleanupTimer) {
clearInterval(this.toolOutputCleanupTimer);
this.toolOutputCleanupTimer = null;
}
this.closeServer?.(); this.closeServer?.();
this.closeServer = null; this.closeServer = null;
this.clientPromise = null; this.clientPromise = null;
} }
private async bootstrapClient(): Promise<OpencodeClient> { private async bootstrapClient(): Promise<OpencodeClient> {
await this.cleanupToolOutputs();
// embedded 模式下,把服务内工具桥地址注入到 opencode 进程环境里, // embedded 模式下,把服务内工具桥地址注入到 opencode 进程环境里,
// 这样 .opencode/tools 下的自定义工具可以回调本服务。 // 这样 .opencode/tools 下的自定义工具可以回调本服务。
process.env.TJWATER_AGENT_INTERNAL_BASE_URL = `http://127.0.0.1:${config.PORT}`; process.env.TJWATER_AGENT_INTERNAL_BASE_URL = `http://127.0.0.1:${config.PORT}`;
@@ -436,9 +447,39 @@ export class OpencodeRuntimeAdapter {
this.closeServer = () => { this.closeServer = () => {
runtime.server.close(); runtime.server.close();
}; };
this.startToolOutputCleanupLoop();
return runtime.client; return runtime.client;
} }
private async cleanupToolOutputs(): Promise<void> {
const directory = resolveOpencodeToolOutputDirectory();
const ttlMs = config.RESULT_REF_TTL_HOURS * 60 * 60 * 1000;
try {
const result = await cleanupExpiredToolOutputs(directory, ttlMs);
if (result.removed > 0) {
logger.info(
{ directory, ...result },
"removed expired opencode tool output files",
);
}
} catch (error) {
logger.warn(
{ err: error, directory },
"failed to clean expired opencode tool output files",
);
}
}
private startToolOutputCleanupLoop(): void {
if (this.toolOutputCleanupTimer) {
return;
}
this.toolOutputCleanupTimer = setInterval(() => {
void this.cleanupToolOutputs();
}, config.RESULT_REF_CLEANUP_INTERVAL_MS);
this.toolOutputCleanupTimer.unref();
}
} }
export const opencodeRuntime = new OpencodeRuntimeAdapter(); export const opencodeRuntime = new OpencodeRuntimeAdapter();
@@ -448,6 +489,10 @@ function buildOpencodeConfig(): Record<string, unknown> {
deepMerge(readProjectOpencodeConfig(), readEnvOpencodeConfig()), deepMerge(readProjectOpencodeConfig(), readEnvOpencodeConfig()),
{ {
model: config.OPENCODE_MODEL, model: config.OPENCODE_MODEL,
tool_output: {
max_bytes: config.MAX_INLINE_RESULT_BYTES,
max_lines: 2000,
},
}, },
); );
} }
+59
View File
@@ -0,0 +1,59 @@
import { readdir, rm, stat } from "node:fs/promises";
import { homedir } from "node:os";
import { join } from "node:path";
export type ToolOutputCleanupResult = {
removed: number;
scanned: number;
};
export const resolveOpencodeToolOutputDirectory = (): string => {
const dataRoot = process.env.XDG_DATA_HOME?.trim() || join(homedir(), ".local", "share");
return join(dataRoot, "opencode", "tool-output");
};
export const cleanupExpiredToolOutputs = async (
directory: string,
ttlMs: number,
now = Date.now(),
): Promise<ToolOutputCleanupResult> => {
if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
throw new Error("tool output cleanup ttlMs must be a positive number");
}
let entries;
try {
entries = await readdir(directory, { withFileTypes: true });
} catch (error) {
if (isNodeError(error, "ENOENT")) {
return { removed: 0, scanned: 0 };
}
throw error;
}
let removed = 0;
for (const entry of entries) {
if (!entry.isFile() || !entry.name.startsWith("tool_")) {
continue;
}
const path = join(directory, entry.name);
try {
const file = await stat(path);
if (now - file.mtimeMs <= ttlMs) {
continue;
}
await rm(path);
removed += 1;
} catch (error) {
if (!isNodeError(error, "ENOENT")) {
throw error;
}
}
}
return { removed, scanned: entries.length };
};
const isNodeError = (error: unknown, code: string): error is NodeJS.ErrnoException =>
error instanceof Error && "code" in error && error.code === code;
+32 -73
View File
@@ -1,5 +1,4 @@
import { randomUUID } from "node:crypto"; import { randomUUID } from "node:crypto";
import { spawn } from "node:child_process";
import cors from "cors"; import cors from "cors";
import express from "express"; import express from "express";
@@ -11,6 +10,7 @@ import {
runWithCredentialRefresh, runWithCredentialRefresh,
} from "./auth/credentialRefresh.js"; } from "./auth/credentialRefresh.js";
import { SessionTranscriptStore } from "./sessions/transcriptStore.js"; import { SessionTranscriptStore } from "./sessions/transcriptStore.js";
import { executeCliCommand } from "./cli/executeCliCommand.js";
import { ChatSessionBridge } from "./chat/sessionBridge.js"; import { ChatSessionBridge } from "./chat/sessionBridge.js";
import { config } from "./config.js"; import { config } from "./config.js";
import { SessionUiStateStore } from "./sessions/uiStateStore.js"; import { SessionUiStateStore } from "./sessions/uiStateStore.js";
@@ -229,7 +229,13 @@ app.post("/internal/tools/tjwater-cli-call", async (req, res) => {
result = await runWithCredentialRefresh( result = await runWithCredentialRefresh(
credentialRefreshCoordinator, credentialRefreshCoordinator,
context, context,
(activeContext) => executeCliCommand(activeContext, command, timeoutSec), (activeContext) =>
executeCliCommand(activeContext, command, timeoutSec, {
apiBaseUrl: config.TJWATER_API_BASE_URL,
cliPath: config.TJWATER_CLI_PATH,
maxStderrBytes: config.MAX_CLI_STDERR_BYTES,
maxStdoutBytes: config.MAX_CLI_OUTPUT_BYTES,
}),
); );
} catch (error) { } catch (error) {
if (!(error instanceof CredentialRefreshError)) { if (!(error instanceof CredentialRefreshError)) {
@@ -266,6 +272,20 @@ app.post("/internal/tools/tjwater-cli-call", async (req, res) => {
return; return;
} }
if (result.outcome === "output_limit") {
res.status(502).json({
ok: false,
schema_version: "tjwater-cli/v1",
summary: "CLI 输出超过安全限制",
error: {
code: "OUTPUT_LIMIT_EXCEEDED",
message: `${result.exceededStream ?? "output"} exceeded ${config.MAX_CLI_OUTPUT_BYTES} bytes`,
retryable: false,
},
});
return;
}
if (result.status === 401) { if (result.status === 401) {
markAuthExpired( markAuthExpired(
getRuntimeSessionContext(sessionId) ?? context, getRuntimeSessionContext(sessionId) ?? context,
@@ -288,80 +308,19 @@ app.post("/internal/tools/tjwater-cli-call", async (req, res) => {
return; return;
} }
try { if (result.stdout.trim()) {
res.json(JSON.parse(result.stdout)); res.status(200).type("application/json").send(result.stdout);
} catch { return;
res.json({
ok: true,
schema_version: "tjwater-cli/v1",
raw: result.stdout,
stderr: result.stderr || undefined,
});
} }
res.json({
ok: true,
schema_version: "tjwater-cli/v1",
raw: "",
stderr: result.stderr || undefined,
stderr_truncated: result.stderrTruncated || undefined,
});
}); });
const executeCliCommand = async (
context: RuntimeSessionContext,
command: string,
timeoutSec: number,
) => {
const child = spawn(
config.TJWATER_CLI_PATH,
["--auth-stdin", ...command.split(/\s+/).filter(Boolean)],
{ stdio: ["pipe", "pipe", "pipe"] },
);
let stdout = "";
let stderr = "";
child.stdout.on("data", (data: Buffer) => {
stdout += data.toString("utf-8");
});
child.stderr.on("data", (data: Buffer) => {
stderr += data.toString("utf-8");
});
child.stdin.write(
JSON.stringify({
server: config.TJWATER_API_BASE_URL,
access_token: context.accessToken,
project_id: context.projectId,
}),
);
child.stdin.end();
const exitCode = await new Promise<number | null>((resolve, reject) => {
const timer = setTimeout(() => {
child.kill("SIGTERM");
resolve(-1);
}, timeoutSec * 1000);
child.on("close", (code) => {
clearTimeout(timer);
resolve(code);
});
child.on("error", (error) => {
clearTimeout(timer);
reject(error);
});
});
let errorCode = "";
try {
const payload = JSON.parse(stdout) as { error?: { code?: unknown } };
errorCode =
typeof payload.error?.code === "string" ? payload.error.code : "";
} catch {
errorCode = "";
}
const status =
exitCode === -1
? 504
: errorCode === "HTTP_401" || errorCode === "UNAUTHENTICATED"
? 401
: errorCode === "HTTP_403"
? 403
: exitCode === 0
? 200
: 502;
return { exitCode, status, stderr, stdout };
};
app.post("/internal/tools/store-render-ref", async (req, res) => { app.post("/internal/tools/store-render-ref", async (req, res) => {
if (req.header("x-agent-internal-token") !== internalToken) { if (req.header("x-agent-internal-token") !== internalToken) {
res.status(403).json({ message: "forbidden" }); res.status(403).json({ message: "forbidden" });
+137
View File
@@ -0,0 +1,137 @@
import { describe, expect, test } from "bun:test";
import { fileURLToPath } from "node:url";
import { executeCliCommand } from "../../src/cli/executeCliCommand.js";
import { type RuntimeSessionContext } from "../../src/runtime/sessionContext.js";
const cliPath = fileURLToPath(
new URL("../fixtures/fakeCli.mjs", import.meta.url),
);
const context: RuntimeSessionContext = {
accessToken: "test-token",
actorKey: "actor-1",
clientSessionId: "client-1",
projectId: "project-1",
projectKey: "project-1",
sessionId: "session-1",
traceId: "trace-1",
};
const run = (
command: string,
options: {
maxStderrBytes?: number;
maxStdoutBytes?: number;
terminationGraceMs?: number;
timeoutSec?: number;
} = {},
) =>
executeCliCommand(context, command, options.timeoutSec ?? 1, {
apiBaseUrl: "http://127.0.0.1:8000",
cliPath,
maxStderrBytes: options.maxStderrBytes ?? 8,
maxStdoutBytes: options.maxStdoutBytes ?? 64,
terminationGraceMs: options.terminationGraceMs ?? 20,
});
describe("executeCliCommand", () => {
test("accepts output at the byte limit", async () => {
await expect(run("stdout 123456", { maxStdoutBytes: 6 })).resolves.toMatchObject({
outcome: "completed",
exitCode: 0,
status: 200,
stdout: "123456",
});
});
test("does not treat the OpenCode 12000-byte inline threshold as a CLI limit", async () => {
const stdout = "x".repeat(12_001);
const result = await run(`stdout ${stdout}`, { maxStdoutBytes: 128 * 1024 * 1024 });
expect(result.outcome).toBe("completed");
expect(result.stdout).toBe(stdout);
});
test("rejects multibyte output above the byte limit without returning a partial body", async () => {
await expect(run("stdout 水水", { maxStdoutBytes: 5 })).resolves.toMatchObject({
outcome: "output_limit",
exceededStream: "stdout",
status: 502,
stderr: "",
stdout: "",
});
});
test("truncates stderr independently without terminating a successful command", async () => {
await expect(
run("stderr-success 123456789", { maxStderrBytes: 6 }),
).resolves.toMatchObject({
outcome: "completed",
exitCode: 0,
status: 200,
stderr: "123456",
stderrTruncated: true,
stdout: '{"ok":true}',
});
});
test("waits for a SIGTERM-aware process to close after timeout", async () => {
const startedAt = Date.now();
const result = await run("term", {
terminationGraceMs: 100,
timeoutSec: 0.25,
});
expect(result).toMatchObject({ outcome: "timeout", status: 504 });
expect(Date.now() - startedAt).toBeGreaterThanOrEqual(270);
});
test("uses SIGKILL when a timed-out process ignores SIGTERM", async () => {
const result = await run("ignore-term", { timeoutSec: 0.25 });
expect(result).toMatchObject({
outcome: "timeout",
signal: "SIGKILL",
status: 504,
});
});
test("keeps the timeout outcome when closing stdin also errors", async () => {
const largeContext = {
...context,
accessToken: "x".repeat(1024 * 1024),
};
await expect(
executeCliCommand(largeContext, "ignore-term", 0.25, {
apiBaseUrl: "http://127.0.0.1:8000",
cliPath,
maxStderrBytes: 8,
maxStdoutBytes: 64,
terminationGraceMs: 20,
}),
).resolves.toMatchObject({
outcome: "timeout",
signal: "SIGKILL",
status: 504,
});
});
test("rejects a deterministic stdin pipe error without crashing", async () => {
const largeContext = {
...context,
accessToken: "x".repeat(1024 * 1024),
};
await expect(
executeCliCommand(largeContext, "closed-stdin", 1, {
apiBaseUrl: "http://127.0.0.1:8000",
cliPath,
maxStderrBytes: 8,
maxStdoutBytes: 64,
terminationGraceMs: 20,
}),
).rejects.toBeInstanceOf(Error);
});
});
Vendored Executable
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env node
import { closeSync } from "node:fs";
const command = process.argv[3];
const value = process.argv[4] ?? "";
if (command === "stdout") {
process.stdout.write(value);
process.exit(0);
}
if (command === "stderr") {
process.stderr.write(value);
process.exit(1);
}
if (command === "stderr-success") {
process.stderr.write(value);
process.stdout.write('{"ok":true}');
process.exit(0);
}
if (command === "term") {
process.on("SIGTERM", () => {
setTimeout(() => process.exit(0), 30);
});
setInterval(() => undefined, 1000);
}
if (command === "ignore-term") {
process.on("SIGTERM", () => undefined);
setInterval(() => undefined, 1000);
}
if (command === "closed-stdin") {
closeSync(0);
setInterval(() => undefined, 1000);
}
+93
View File
@@ -1,4 +1,7 @@
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { mkdtemp, mkdir, rm, symlink } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { import {
canAutoApprovePermission, canAutoApprovePermission,
@@ -9,10 +12,100 @@ describe("permission approval policy", () => {
it.each([ it.each([
"show_chart", "show_chart",
"web_search", "web_search",
"skill",
])("allows low-risk permission %s", (permission) => { ])("allows low-risk permission %s", (permission) => {
expect(canAutoApprovePermission(permission)).toBe(true); expect(canAutoApprovePermission(permission)).toBe(true);
}); });
it("allows structured searches within the workspace", () => {
const workspaceRoot = process.cwd();
const context = {
workspaceRoot,
metadata: { path: join(workspaceRoot, "src"), include: "*.ts" },
patterns: ["*.ts"],
};
expect(canAutoApprovePermission("glob", context)).toBe(true);
expect(canAutoApprovePermission("grep", context)).toBe(true);
});
it("allows a glob with an explicit safe prefix from the workspace root", () => {
expect(
canAutoApprovePermission("glob", {
workspaceRoot: process.cwd(),
metadata: { path: process.cwd(), pattern: "src/**/*.ts" },
patterns: ["src/**/*.ts"],
}),
).toBe(true);
});
it.each([
{ metadata: { path: dirname(process.cwd()) }, patterns: ["*"] },
{ metadata: { path: join(process.cwd(), ".local.env") }, patterns: ["*"] },
{ metadata: { path: join(process.cwd(), "data") }, patterns: ["*"] },
{ metadata: { path: join(process.cwd(), "src") }, patterns: ["../logs/**"] },
{ metadata: { path: process.cwd() }, patterns: ["**/*.env"] },
{ metadata: { path: process.cwd() }, patterns: ["**/*"] },
{ metadata: { path: join(process.cwd(), "src") }, patterns: [".[e]nv"] },
])("keeps protected or external searches interactive", (request) => {
expect(
canAutoApprovePermission("glob", {
workspaceRoot: process.cwd(),
...request,
}),
).toBe(false);
});
it("keeps grep from the workspace root interactive", () => {
expect(
canAutoApprovePermission("grep", {
workspaceRoot: process.cwd(),
metadata: { path: process.cwd(), include: "*.ts" },
patterns: ["secret"],
}),
).toBe(false);
});
it("rejects a workspace symlink that resolves outside the workspace", async () => {
const workspaceRoot = await mkdtemp(join(tmpdir(), "permission-workspace-"));
const externalRoot = await mkdtemp(join(tmpdir(), "permission-external-"));
try {
await mkdir(join(externalRoot, "src"));
const linkedPath = join(workspaceRoot, "linked");
await symlink(join(externalRoot, "src"), linkedPath, "dir");
expect(
canAutoApprovePermission("grep", {
workspaceRoot,
metadata: { path: linkedPath, include: "*.ts" },
patterns: ["secret"],
}),
).toBe(false);
} finally {
await Promise.all([
rm(workspaceRoot, { force: true, recursive: true }),
rm(externalRoot, { force: true, recursive: true }),
]);
}
});
it("rejects protected descendants below an otherwise safe search root", async () => {
const workspaceRoot = await mkdtemp(join(tmpdir(), "permission-descendant-"));
try {
const sourceRoot = join(workspaceRoot, "src");
await mkdir(join(sourceRoot, "data"), { recursive: true });
expect(
canAutoApprovePermission("grep", {
workspaceRoot,
metadata: { path: sourceRoot, include: "*.ts" },
patterns: ["secret"],
}),
).toBe(false);
} finally {
await rm(workspaceRoot, { force: true, recursive: true });
}
});
it.each([ it.each([
"bash", "bash",
"edit", "edit",
+236
View File
@@ -15,6 +15,242 @@ const createEventStream = (events: unknown[]) => ({
}); });
describe("streamPromptResponse", () => { describe("streamPromptResponse", () => {
it("emits only the final assistant text after tool-driven intermediate messages", async () => {
const runtime = {
subscribeEvents: async () =>
createEventStream([
{
type: "message.part.delta",
properties: {
sessionID: "runtime-session-1",
messageID: "assistant-intermediate",
partID: "text-part-intermediate",
field: "text",
delta: "正在加载工作流并尝试分页参数。",
},
},
{
type: "message.part.updated",
properties: {
sessionID: "runtime-session-1",
part: {
id: "text-part-intermediate",
sessionID: "runtime-session-1",
messageID: "assistant-intermediate",
type: "text",
text: "正在加载工作流并尝试分页参数。",
time: { start: 1, end: 2 },
},
time: 2,
},
},
{
type: "message.part.delta",
properties: {
sessionID: "runtime-session-1",
messageID: "assistant-final",
partID: "text-part-final",
field: "text",
delta: "共识别 56 条瓶颈管段,建议优先改造 Top 5。",
},
},
{
type: "message.part.updated",
properties: {
sessionID: "runtime-session-1",
part: {
id: "text-part-final",
sessionID: "runtime-session-1",
messageID: "assistant-final",
type: "text",
text: "共识别 56 条瓶颈管段,建议优先改造 Top 5。",
time: { start: 3, end: 4 },
},
time: 4,
},
},
{
type: "session.idle",
properties: { sessionID: "runtime-session-1" },
},
]),
prompt: async () => undefined,
messages: async () => [
{
info: { id: "assistant-intermediate", role: "assistant" },
parts: [
{
id: "text-part-intermediate",
sessionID: "runtime-session-1",
messageID: "assistant-intermediate",
type: "text",
text: "正在加载工作流并尝试分页参数。",
},
],
},
{
info: { id: "assistant-final", role: "assistant" },
parts: [
{
id: "text-part-final",
sessionID: "runtime-session-1",
messageID: "assistant-final",
type: "text",
text: "共识别 56 条瓶颈管段,建议优先改造 Top 5。",
},
],
},
],
} as unknown as OpencodeRuntimeAdapter;
const events: Array<{ event: string; data: Record<string, unknown> }> = [];
await streamPromptResponse({
runtime,
sessionId: "runtime-session-1",
clientSessionId: "client-session-1",
message: "分析管网瓶颈",
write: (event, data) => events.push({ event, data }),
});
expect(
events
.filter((item) => item.event === "token")
.map((item) => item.data.content)
.join(""),
).toBe("共识别 56 条瓶颈管段,建议优先改造 Top 5。");
});
it("uses the final text event cache when the messages lookup fails", async () => {
const runtime = {
subscribeEvents: async () =>
createEventStream([
{
type: "message.part.updated",
properties: {
sessionID: "runtime-session-1",
part: {
id: "text-part-final",
sessionID: "runtime-session-1",
messageID: "assistant-final",
type: "text",
text: "最终分析结果。",
time: { start: 1, end: 2 },
},
time: 2,
},
},
{
type: "session.idle",
properties: { sessionID: "runtime-session-1" },
},
]),
prompt: async () => undefined,
messages: async () => {
throw new Error("transient messages lookup failure");
},
} as unknown as OpencodeRuntimeAdapter;
const events: Array<{ event: string; data: Record<string, unknown> }> = [];
const result = await streamPromptResponse({
runtime,
sessionId: "runtime-session-1",
clientSessionId: "client-session-1",
message: "分析管网瓶颈",
write: (event, data) => events.push({ event, data }),
});
expect(result.failed).toBe(false);
expect(
events
.filter((item) => item.event === "token")
.map((item) => item.data.content)
.join(""),
).toBe("最终分析结果。");
});
it("keeps reasoning generic while preserving tool execution details", async () => {
const runtime = {
subscribeEvents: async () =>
createEventStream([
{
type: "message.part.updated",
properties: {
sessionID: "runtime-session-1",
part: {
id: "reasoning-part-1",
sessionID: "runtime-session-1",
messageID: "assistant-1",
type: "reasoning",
text: "内部推理:尝试 limit=5000 并读取临时路径。",
time: { start: 1, end: 2 },
},
time: 2,
},
},
{
type: "message.part.delta",
properties: {
sessionID: "runtime-session-1",
messageID: "assistant-1",
partID: "reasoning-part-1",
field: "text",
delta: "内部推理:尝试 limit=5000 并读取临时路径。",
},
},
{
type: "message.part.updated",
properties: {
sessionID: "runtime-session-1",
part: {
id: "tool-part-1",
sessionID: "runtime-session-1",
messageID: "assistant-1",
type: "tool",
callID: "call-1",
tool: "tjwater_cli",
state: {
status: "error",
input: {
command: "network get-all-pipes-properties --limit 5000",
reason: "尝试突破分页限制",
},
error: "HTTP_422 raw backend payload with trace_id=secret-trace",
time: { start: 1, end: 2 },
},
},
time: 2,
},
},
{
type: "session.idle",
properties: { sessionID: "runtime-session-1" },
},
]),
prompt: async () => undefined,
messages: async () => [],
} as unknown as OpencodeRuntimeAdapter;
const events: Array<{ event: string; data: Record<string, unknown> }> = [];
await streamPromptResponse({
runtime,
sessionId: "runtime-session-1",
clientSessionId: "client-session-1",
message: "分析管网瓶颈",
write: (event, data) => events.push({ event, data }),
});
const reasoningProgress = events.find(
(item) => item.event === "progress" && item.data.id === "reasoning-part-1",
);
const toolProgress = events.find(
(item) => item.event === "progress" && item.data.id === "tool-part-1",
);
expect(reasoningProgress?.data.detail).toBe("分析步骤已整理完成。");
expect(toolProgress?.data.detail).toBe(
"tjwater_cli 调用失败;调用原因:尝试突破分页限制;关键参数:command=network get-all-pipes-properties --limit 5000;错误:HTTP_422 raw backend payload with trace_id=secret-trace",
);
});
it("forwards opencode permission requests as SSE payloads", async () => { it("forwards opencode permission requests as SSE payloads", async () => {
const runtime = { const runtime = {
subscribeEvents: async () => subscribeEvents: async () =>
@@ -0,0 +1,62 @@
import { describe, expect, test } from "bun:test";
import {
lstat,
mkdir,
mkdtemp,
readFile,
rm,
stat,
symlink,
utimes,
writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { cleanupExpiredToolOutputs } from "../../src/runtime/opencodeToolOutputCleanup.js";
describe("cleanupExpiredToolOutputs", () => {
test("removes only expired regular tool output files", async () => {
const directory = await mkdtemp(join(tmpdir(), "opencode-tool-output-cleanup-"));
const expired = join(directory, "tool_expired");
const current = join(directory, "tool_current");
const unrelated = join(directory, "keep.txt");
const toolDirectory = join(directory, "tool_directory");
const toolSymlink = join(directory, "tool_symlink");
try {
await Promise.all([
writeFile(expired, "expired"),
writeFile(current, "current"),
writeFile(unrelated, "unrelated"),
mkdir(toolDirectory),
]);
await symlink(unrelated, toolSymlink);
const now = Date.now();
const old = new Date(now - 8 * 24 * 60 * 60 * 1000);
await utimes(expired, old, old);
const result = await cleanupExpiredToolOutputs(
directory,
7 * 24 * 60 * 60 * 1000,
now,
);
expect(result).toEqual({ removed: 1, scanned: 5 });
await expect(stat(expired)).rejects.toMatchObject({ code: "ENOENT" });
expect(await readFile(current, "utf8")).toBe("current");
expect(await readFile(unrelated, "utf8")).toBe("unrelated");
expect((await stat(toolDirectory)).isDirectory()).toBe(true);
expect((await lstat(toolSymlink)).isSymbolicLink()).toBe(true);
} finally {
await rm(directory, { force: true, recursive: true });
}
});
test("treats a missing tool output directory as empty", async () => {
const directory = join(tmpdir(), `missing-tool-output-${crypto.randomUUID()}`);
await expect(cleanupExpiredToolOutputs(directory, 1_000)).resolves.toEqual({
removed: 0,
scanned: 0,
});
});
});