From 401c0080b3fd738daed3ebe5b29ac7967503a416 Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 14:43:40 +0800 Subject: [PATCH 1/2] feat(config): use one private JSON credential file --- .scratch/agent-loop-v1/spec.md | 2 +- .scratch/single-json-config/spec.md | 41 ++++++ README.md | 25 +++- config/example.json | 6 +- docs/agents/operations.md | 4 +- docs/feishu-setup.md | 23 ++-- package.json | 6 +- scripts/pair-feishu.ts | 73 ++++++----- scripts/verify-feishu.ts | 195 ++++++++++++++-------------- src/adapters/feishu.ts | 11 +- src/cli.ts | 25 ++-- src/config.ts | 68 ++++++---- src/credentials.ts | 38 ++++++ src/dashboard.ts | 7 +- src/domain.ts | 3 +- src/store.ts | 33 ++--- test/cli.test.ts | 29 +++-- test/credential-redaction.test.ts | 89 +++++++++++++ test/execution-config.test.ts | 8 +- test/feishu-config-helpers.test.ts | 83 ++++++++++++ test/single-config-cli.test.ts | 94 ++++++++++++++ test/workspace.test.ts | 4 +- 22 files changed, 633 insertions(+), 234 deletions(-) create mode 100644 .scratch/single-json-config/spec.md create mode 100644 src/credentials.ts create mode 100644 test/credential-redaction.test.ts create mode 100644 test/feishu-config-helpers.test.ts create mode 100644 test/single-config-cli.test.ts diff --git a/.scratch/agent-loop-v1/spec.md b/.scratch/agent-loop-v1/spec.md index 1334e78..2b812d2 100644 --- a/.scratch/agent-loop-v1/spec.md +++ b/.scratch/agent-loop-v1/spec.md @@ -112,7 +112,7 @@ Status: ready-for-agent 24. **离线补偿。** 恢复在线后补偿同步来源和可取得的飞书消息,并去重。具体平台权限和历史可获取范围需要验证;无法完整补回时显示缺口,而不是承诺已经无损恢复。 25. **失败处理。** 网络同步、消息投递和执行失败分别记录。可重试的传输错误按配置重试;有外部副作用的动作先核对结果。不得以失败触发无限重跑模型,也不得将未启动或投递失败的操作记为成功。 26. **交付与后续修改。** 核对草稿 PR 与任务仓库、分支和提交的关联后展示待验收状态,并保留验证结果和限制。后续反馈沿用原会话、分支和 PR。首版不自动清理群、任务数据或工作目录。 -27. **运行配置。** 仓库映射、基准分支、模型与执行参数、轮询和重试设置、存储目录及凭证引用使用配置机制;密钥使用环境变量或已有凭证提供器。运行参数和项目事实不写入全局说明。Freedom 环境信息由任务侧提供,核心循环不定义其专用配置模型。 +27. **运行配置。** 仓库映射、基准分支、模型与执行参数、轮询和重试设置、存储目录及凭证引用使用配置机制;应用凭证要求现由[单 JSON 配置规格 v1](../single-json-config/spec.md)取代,密钥直接存入被忽略的私有 JSON;普通系统和执行 provider 环境机制保留。运行参数和项目事实不写入全局说明。Freedom 环境信息由任务侧提供,核心循环不定义其专用配置模型。 28. **类型扩展。** 模板注册、来源分类和采集条件分别维护。未来新增类型无需重写通用会话、消息和恢复逻辑;仅修改采集类型不能让没有模板的 Feature 任务误用 Bug prompt。 ## Testing Decisions diff --git a/.scratch/single-json-config/spec.md b/.scratch/single-json-config/spec.md new file mode 100644 index 0000000..b8e1a5c --- /dev/null +++ b/.scratch/single-json-config/spec.md @@ -0,0 +1,41 @@ +Status: ready-for-agent + +# Single JSON configuration + +Version: 1 + +## Goal and authorization + +Users maintain one private JSON configuration file. The repository commits only a placeholder example. Application credentials live directly in that file. + +The coordinator delegated implementation in event `single-config-implementation-v1` from task `c9edaf14-f18a-42de-841e-d963e1f520cc`. The delegation identifies the user's explicit authorization as message `om_x100b60ca1e1928a0c05c23ea7b9df56`, chat `oc_586b1866902382e0db4dff8d25c0f3f7`, sender `ou_5c280b16780ac4016c7455a276b2d95b`. This document records that relayed authorization, not a new product decision by the implementer. + +## Confirmed scope + +- Store `feishu.appId`, `feishu.appSecret`, and `github.token` directly in JSON. Remove their `*Env` selectors and credential environment lookup. +- Remove application dotenv loading and `--env-file`. Do not retain an optional compatibility path. +- Migrate CLI doctor, collect, run, and owned Feishu pairing and verification helpers together with examples, documentation, and tests. +- Keep normal system and provider environment mechanisms. Keep the existing conditional GitHub token requirement when collection repositories are configured. +- Keep execution hot reload. Valid edits update only execution settings. Invalid edits retain the last valid execution settings. Other settings, including credentials, require restart. +- Ignore private configuration and track only placeholder examples. Do not expose application credentials in validation errors, logs, dashboard or configuration APIs, snapshots, or reports. + +## Acceptance + +1. A user copies `config/example.json` to ignored `config.json`, fills direct credentials, and runs doctor without a dotenv file or application credential environment variables. +2. Collection and runtime clients receive credentials from JSON. Missing GitHub token is rejected when repositories are configured and allowed when none are configured. +3. Legacy selectors and `--env-file` fail clearly without echoing credential values. An existing dotenv file has no application effect. +4. Pair and verify helpers use the same private JSON file and do not print credentials. Pairing changes only the owner binding in that file. +5. Synthetic secrets do not appear in diagnostic output, validation failures, serialized configuration, dashboard responses, snapshots, or persisted reports. +6. Execution reload behavior and ordinary provider environment inheritance remain covered. Relevant tests, type checking, and build pass. + +## Excluded scope + +No live configuration or dotenv migration, deletion, or reading of actual credentials. No changes to live property lists. No deployment, service restart, merge, or unrelated coordinator intake work. No alternate credential stores or dotenv compatibility. + +## Open questions + +None at handoff. New scope changes require coordinator review. Network acceptance using real credentials remains outside this task's authorization. + +## Comments + +Implementation and independent verification are separate. The PR owner delivers the branch and evidence. The coordinator arranges an independent verifier before a merge decision. diff --git a/README.md b/README.md index ad91f20..8832bf2 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Agent Loop 保存项目和任务的持续上下文。用户在项目协调群交 cp config/example.json config.json ``` -按下面的说明修改本机路径,并通过环境变量提供凭证。然后检查配置。 +按下面的说明修改本机路径,并在私有 `config.json` 中填写凭证。然后检查配置。 ```sh npm start -- doctor --config config.json @@ -29,7 +29,9 @@ npm install cp config/example.json config.json ``` -编辑 `config.json`,将 `feishu.userOpenId` 改为应用下的所有者 Open ID。相对路径均以配置文件所在目录为基准。 +编辑私有 `config.json`,填写 `feishu.appId`、`feishu.appSecret` 和应用下的所有者 `feishu.userOpenId`。配置了采集仓库时,还需填写 `github.token`。相对路径均以配置文件所在目录为基准。 + +仓库只提交带占位值的 `config/example.json`。`config.json` 与 `config.json.*` 已被 Git 忽略。将私人副本保存在这些路径,或自行为其他文件名设置忽略规则,并限制文件读取权限,例如 `chmod 600 config.json`。不要把私人配置复制进日志、报告、prompt 或版本库。 正式任务共用本机 Codex daemon。Agent Loop 通过官方 `daemon version` 发现服务,只在服务未运行时调用 `daemon start`。连接失败会报告错误,断线后重新连接并核对原 Thread。Agent Loop 不停止、重启或更新 daemon,也不回退私有 App Server。 @@ -69,7 +71,24 @@ Agent Loop 不再使用模型选择目录。任务必须先归属到已配置项 新 Task 先保存原始任务与附件。项目协调者负责确认工程归属并通过 `claim_intake` 或 `dispatch` 派发 Worker;未归属 intake 和历史任务保持等待,不创建执行 Thread。确认归属后,协调者和 Worker 直接使用项目配置的目录,所有项目共用实例级角色执行配置。后续讨论、澄清、工程工作和恢复沿用正式 Thread。 -凭证通过环境变量提供。默认名称为 `GITHUB_TOKEN`、`FEISHU_APP_ID`、`FEISHU_APP_SECRET`。配置仅保存变量名。程序加载当前目录的 `.env`,已有进程变量优先。可用 `--env-file` 指定其他文件。不要将真实凭证写入 JSON、prompt 或全局说明。 +应用只从 `--config` 指定的 JSON 文件读取凭证。凭证字段直接填写值,例如: + +```json +{ + "github": { "token": "REPLACE_WITH_GITHUB_TOKEN" }, + "feishu": { + "appId": "REPLACE_WITH_FEISHU_APP_ID", + "appSecret": "REPLACE_WITH_FEISHU_APP_SECRET", + "userOpenId": "REPLACE_WITH_YOUR_OPEN_ID" + } +} +``` + +这些占位值不会被视为已配置凭证。`doctor` 仅报告字段是否已填写。`collect` 只在配置采集仓库时要求 GitHub token,不要求飞书凭证;`run` 还要求飞书凭证和已绑定所有者。`status` 可在未填写凭证时读取本地状态。 + +应用不读取 `.env`,不接受 `--env-file`,也不支持 `tokenEnv`、`appIdEnv` 或 `appSecretEnv`。升级已有配置时,在私有 JSON 中填写直接凭证字段并移除旧字段,从服务启动参数中移除 `--env-file`。完成迁移和验收后,可由配置所有者另行处理旧环境文件。程序不会迁移或删除它。普通系统环境和 Codex provider 的环境与登录机制继续保留,JSON 凭证不会注入子进程环境。 + +只有 `execution` 配置会热更新。修改凭证或其他设置后,按[服务操作说明](docs/agents/operations.md)重启服务。无效编辑会保留上一次有效 execution 配置。解析后的凭证在配置导出、诊断和看板输出中被遮蔽,私有 JSON 文件本身仍保存原值。 ## 飞书接入 diff --git a/config/example.json b/config/example.json index 0a54448..4f2f3f6 100644 --- a/config/example.json +++ b/config/example.json @@ -34,13 +34,13 @@ } ], "github": { - "tokenEnv": "GITHUB_TOKEN", + "token": "REPLACE_WITH_GITHUB_TOKEN", "apiUrl": "https://api.github.com", "requestTimeoutMs": 30000 }, "feishu": { - "appIdEnv": "FEISHU_APP_ID", - "appSecretEnv": "FEISHU_APP_SECRET", + "appId": "REPLACE_WITH_FEISHU_APP_ID", + "appSecret": "REPLACE_WITH_FEISHU_APP_SECRET", "userOpenId": "REPLACE_WITH_YOUR_OPEN_ID" }, "execution": { diff --git a/docs/agents/operations.md b/docs/agents/operations.md index b32aec3..4272669 100644 --- a/docs/agents/operations.md +++ b/docs/agents/operations.md @@ -2,7 +2,7 @@ 本机常驻执行服务和调试网页由同一个 `run` 进程提供,统一由 launchd 管理。后续启动、停止和重启均使用本文命令,避免另起独立后台进程争用同一数据目录。前台调试前先停止 launchd 服务。 -服务定义位于 `~/Library/LaunchAgents/com.local.agent-loop.run.plist`,标签为 `com.local.agent-loop.run`。工作目录和入口以定义中的 `WorkingDirectory`、`ProgramArguments` 为准;独立 worktree 部署可以使用该 worktree 的 `dist/src/cli.js`。应用配置继续使用绝对路径 `/Users/developer/IdeaProjects/agent-loop/config.json`,凭证文件通过 `--env-file` 指定,避免因工作目录变化而读取错误位置。运行参数和环境变量保留在服务定义与应用配置中。 +服务定义位于 `~/Library/LaunchAgents/com.local.agent-loop.run.plist`,标签为 `com.local.agent-loop.run`。工作目录和入口以定义中的 `WorkingDirectory`、`ProgramArguments` 为准;独立 worktree 部署可以使用该 worktree 的 `dist/src/cli.js`。应用配置继续使用绝对路径 `/Users/developer/IdeaProjects/agent-loop/config.json`,凭证直接保存在该私有 JSON 的 `github.token`、`feishu.appId` 和 `feishu.appSecret` 中。程序不读取环境凭证文件。升级服务定义时移除旧 `--env-file` 参数,并按 [README 配置说明](../../README.md#项目归属与正式任务)迁移旧字段。运行参数和普通系统或 provider 环境变量保留在服务定义中。 服务随当前用户登录加载。断开 SSH 不影响运行,进程退出后由 launchd 自动拉起。机器重启后,服务在该用户登录时启动。 @@ -16,7 +16,7 @@ launchctl print "gui/$(id -u)/com.local.agent-loop.run" | rg 'state =|pid =|last ## 修改配置后重启 -修改 `config.json` 或应用代码后,执行: +只有 `execution` 配置支持热更新。修改凭证、其他配置或应用代码后,在获得对应服务操作授权后执行: ```sh launchctl kickstart -k "gui/$(id -u)/com.local.agent-loop.run" diff --git a/docs/feishu-setup.md b/docs/feishu-setup.md index debc3b8..0486ac5 100644 --- a/docs/feishu-setup.md +++ b/docs/feishu-setup.md @@ -4,27 +4,26 @@ ## 确认当前实例与飞书应用 -1. 确认目标服务的工作目录、启动参数中的 `--config` 路径和 `--env-file` 路径。未指定 `--env-file` 时,程序读取工作目录的 `.env`。本机 launchd 实例的位置与检查方法见[服务操作说明](agents/operations.md)。 -2. 在该配置中找到 `feishu.appIdEnv`,读取它所指环境变量的 App ID。默认变量名为 `FEISHU_APP_ID`,`appIdEnv` 本身填写变量名。进程已有环境变量优先于 `.env`。不要用另一个终端或另一个实例的环境代替服务实际环境。 -3. 登录[飞书开放平台开发者后台](https://open.feishu.cn/app),打开该 App ID 对应的应用,在“凭证与基础信息”中核对 App ID。后续权限操作均在这个应用中完成。另一实例的机器人能发文档,不代表当前应用也有权限。 +1. 确认目标服务的工作目录和启动参数中的 `--config` 路径。本机 launchd 实例的位置与检查方法见[服务操作说明](agents/operations.md)。 +2. 在该私有配置中找到 `feishu.appId`,在本机核对当前应用。`feishu.appSecret` 也直接保存在同一文件。不要将凭证复制到排障记录或截图中。 +3. 登录[飞书开放平台开发者后台](https://open.feishu.cn/app),打开对应应用,在“凭证与基础信息”中核对 App ID。后续权限操作均在这个应用中完成。另一实例的机器人能发文档,不代表当前应用也有权限。 -在项目根目录使用以下只读命令查看磁盘配置与当前终端的 App ID。将配置路径、环境文件路径换成目标实例的路径。命令只输出 App ID 和相关配置,不输出 App Secret。 +在项目根目录运行以下只读命令,查看磁盘配置的凭证存在性和进度设置。将配置路径换成目标实例的路径。命令不输出凭证值。 ```sh node --import tsx --input-type=module <<'NODE' import { loadConfig } from './src/config.ts'; -process.loadEnvFile('.env'); const config = await loadConfig('config.json'); console.log(JSON.stringify({ - appIdEnv: config.feishu.appIdEnv, - appId: process.env[config.feishu.appIdEnv] ?? null, + appIdConfigured: config.feishu.appId.configured, + appSecretConfigured: config.feishu.appSecret.configured, dataDir: config.dataDir, progress: config.feishu.progress, }, null, 2)); NODE ``` -此命令不读取运行中服务的内存配置。若凭证由服务管理器注入,需在服务定义中核对变量来源。不要将真实 App Secret 或访问令牌写入文档、JSON 配置或排障记录。 +此命令不读取运行中服务的内存配置。应用不再读取 `.env` 或应用凭证环境变量,不接受 `--env-file` 与旧 `*Env` 字段。真实凭证仅写入被 Git 忽略的私有 JSON,不写入示例、文档或排障记录。 ## 配置文档进度模式 @@ -33,8 +32,8 @@ NODE ```json { "feishu": { - "appIdEnv": "FEISHU_APP_ID", - "appSecretEnv": "FEISHU_APP_SECRET", + "appId": "REPLACE_WITH_FEISHU_APP_ID", + "appSecret": "REPLACE_WITH_FEISHU_APP_SECRET", "userOpenId": "REPLACE_WITH_YOUR_OPEN_ID", "progress": { "enabled": true, @@ -100,7 +99,7 @@ NODE ## 重载配置并确认目标实例 -1. 使用目标配置运行 `npm start -- doctor --config config.json`。若环境文件不同,追加 `--env-file /absolute/path/to/.env`。先修正配置解析错误或缺失凭证。 +1. 使用目标配置运行 `npm start -- doctor --config config.json`。应用凭证均来自该 JSON 文件。先修正配置解析错误或缺失凭证。 2. 确认服务定义中的工作目录和配置路径与刚修改的文件一致,按[服务操作说明的重启步骤](agents/operations.md#修改配置后重启)重启目标实例。不要另开一个 `run` 进程共用同一数据目录。 3. 重启后核对服务 PID、调试网页的执行服务心跳 PID 和持续更新的心跳时间。核对最新 `started` 日志的 `dataDir`、调试网页地址与目标实例一致。日志位置和状态命令见[服务操作说明](agents/operations.md)。 4. 再次运行本指南的配置查看命令,确认磁盘中的 `enabled`、`surface` 和 `documentBaseUrl`。随后用新任务群的实际文档和 Tab 完成验收。仅看到磁盘新配置或一个旧心跳,不能证明目标服务已加载新配置。 @@ -164,7 +163,7 @@ sqlite3 -readonly '/absolute/path/to/data/tasks.sqlite' \ | --- | --- | | 群消息正常,但顶部没有“执行进度”Tab | 先确认 `feishu.progress.enabled: true`、`surface: "document"` 和正确的 HTTPS `documentBaseUrl`,再核对目标服务是否重载。`card` 模式只发聊天卡片。继续查文档诊断、文档进度权限及群设置是否允许机器人管理标签页。 | | 文档或标签页接口返回 `99991672` | 这是权限不足的诊断。根据失败接口和响应中要求的权限,在当前 App ID 下添加应用身份权限,再回读实际授权。文档创建需 `docx:document`,标签页读取需 `im:chat.tabs:read` 或覆盖权限,不能用基础群信息读取成功作证明。 | -| 当前应用与正常实例的 App ID 不同 | 重新确认目标服务的 `appIdEnv`、环境文件和进程环境来源。分别进入各自应用配置权限,另一应用的已授权状态不能复用。若修改了目标实例的环境或配置,按服务操作说明重启后重新验收。 | +| 当前应用与正常实例的 App ID 不同 | 重新确认目标服务实际使用的 JSON 文件及其中的 `feishu.appId`。分别进入各自应用配置权限,另一应用的已授权状态不能复用。若修改了目标实例的凭证或其他非 execution 配置,按服务操作说明重启后重新验收。 | | 权限已添加,但接口仍拒权 | 检查是否误选用户身份权限,以及是否尚需发布版本或管理员审批。用当前应用的应用身份回读实际授权;没有出现在已授权范围内时,返回权限管理处理,不能仅凭已添加列表判定生效。 | | “向管理员申请授权”接口返回 `212002` | 此接口表示可申请的未授权范围为空,不能据此证明所需能力已授权。先检查所需权限是否已加入当前应用配置,再查授权状态。本次接入在尚未添加权限时遇到此码,回到权限管理添加并按后台提示开通后才恢复。接口说明见[向管理员申请授权](https://open.feishu.cn/document/application-v6/scope/apply)。 | | 文档或 Tab 已创建,但群成员不能阅读 | 检查 `docs:permission.member:create` 与 `docs:permission.member:retrieve`,并回读文档协作者列表,确认当前 `chatId` 作为 `openchat` 成员具有 `view` 阅读权限。核对 Tab 绑定的文档 URL,使用普通群成员账号实际打开。Tab 创建成功不能证明协作者授权成功。 | diff --git a/package.json b/package.json index 585c77a..43393dc 100644 --- a/package.json +++ b/package.json @@ -11,13 +11,13 @@ "check": "tsc --noEmit", "test": "tsx --test test/*.test.ts", "benchmark:dashboard": "tsx scripts/benchmark-dashboard.ts", - "start": "tsx src/cli.ts", + "start": "node --import tsx -- src/cli.ts", "verify:shared-codex": "npm run build && tsx scripts/verify-shared-codex.ts", "verify:closeout": "tsx --test test/task-closeout.test.ts", "verify:codex": "tsx scripts/verify-codex.ts", "verify:inputs": "tsx scripts/verify-inputs.ts", - "pair:feishu": "tsx scripts/pair-feishu.ts", - "verify:feishu": "tsx scripts/verify-feishu.ts", + "pair:feishu": "node --import tsx -- scripts/pair-feishu.ts", + "verify:feishu": "node --import tsx -- scripts/verify-feishu.ts", "verify:runtime": "tsx scripts/verify-runtime.ts", "verify:workspace-selection": "tsx scripts/verify-workspace-selection.ts" }, diff --git a/scripts/pair-feishu.ts b/scripts/pair-feishu.ts index bfb0b98..1d0d687 100644 --- a/scripts/pair-feishu.ts +++ b/scripts/pair-feishu.ts @@ -1,39 +1,44 @@ import { randomBytes } from 'node:crypto'; -import { readFile, writeFile } from 'node:fs/promises'; +import { writeFile } from 'node:fs/promises'; import { parseArgs } from 'node:util'; import { Client, WSClient, EventDispatcher, Domain, LoggerLevel } from '@larksuiteoapi/node-sdk'; import { z } from 'zod'; +import { loadConfig, readConfigInput } from '../src/config.js'; +import { credentialSafeLogger, publicJson } from '../src/credentials.js'; +import { errorMessage } from '../src/domain.js'; -const args = parseArgs({ options: { config: { type: 'string', default: 'config.json' } } }); -process.loadEnvFile('.env'); -const appId = process.env.FEISHU_APP_ID; -const appSecret = process.env.FEISHU_APP_SECRET; -if (!appId || !appSecret) throw new Error('缺少飞书应用环境变量'); -const code = randomBytes(4).toString('hex'); -const command = `绑定 ${code}`; -const client = new Client({ appId, appSecret, domain: Domain.Feishu, loggerLevel: LoggerLevel.error }); -const socket = new WSClient({ appId, appSecret, domain: Domain.Feishu, loggerLevel: LoggerLevel.error }); -const eventSchema = z.object({ sender: z.object({ sender_type: z.literal('user'), sender_id: z.object({ open_id: z.string().min(1) }) }), message: z.object({ message_type: z.literal('text'), content: z.string() }) }); -let bound = false; -const timer = setTimeout(() => { process.stderr.write('绑定超时,请重新运行。\n'); socket.close({ force: true }); process.exitCode = 1; }, 20 * 60_000); -const stop = () => { clearTimeout(timer); socket.close({ force: true }); }; -process.once('SIGINT', stop); process.once('SIGTERM', stop); -await socket.start({ eventDispatcher: new EventDispatcher({ loggerLevel: LoggerLevel.error }).register({ - 'im.message.receive_v1': async raw => { - const result = eventSchema.safeParse(raw); - if (bound || !result.success) return; - const text = z.object({ text: z.string() }).parse(JSON.parse(result.data.message.content)).text; - if (text.trim() !== command) return; - bound = true; - try { - const config = z.record(z.string(), z.unknown()).parse(JSON.parse(await readFile(args.values.config, 'utf8'))); - const feishu = z.record(z.string(), z.unknown()).parse(config.feishu); - config.feishu = { ...feishu, userOpenId: result.data.sender.sender_id.open_id }; - await writeFile(args.values.config, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 }); - const reply = await client.im.message.create({ params: { receive_id_type: 'open_id' }, data: { receive_id: result.data.sender.sender_id.open_id, msg_type: 'text', content: JSON.stringify({ text: 'Agent Loop 已绑定你的账号。后续每个任务使用独立群,文字、截图、日志和确认都会回到对应任务会话。' }) } }); - if (reply.code !== 0) throw new Error(`飞书回复失败 ${reply.code}`); - process.stdout.write(JSON.stringify({ paired: true, config: args.values.config }) + '\n'); - } finally { stop(); } - }, -}) }); -process.stdout.write(JSON.stringify({ pairingCommand: command, appId, instruction: '在飞书中打开 Agent Loop 小助手,发送 pairingCommand。' }) + '\n'); +async function main(): Promise { + const args = parseArgs({ options: { config: { type: 'string', default: 'config.json' } } }); + const loaded = await loadConfig(args.values.config); + const appId = loaded.feishu.appId.require('feishu.appId'); + const appSecret = loaded.feishu.appSecret.require('feishu.appSecret'); + const code = randomBytes(4).toString('hex'); + const command = `绑定 ${code}`; + const client = new Client({ appId, appSecret, domain: Domain.Feishu, loggerLevel: LoggerLevel.error, logger: credentialSafeLogger }); + const socket = new WSClient({ appId, appSecret, domain: Domain.Feishu, loggerLevel: LoggerLevel.error, logger: credentialSafeLogger }); + const eventSchema = z.object({ sender: z.object({ sender_type: z.literal('user'), sender_id: z.object({ open_id: z.string().min(1) }) }), message: z.object({ message_type: z.literal('text'), content: z.string() }) }); + let bound = false; + const timer = setTimeout(() => { process.stderr.write('绑定超时,请重新运行。\n'); socket.close({ force: true }); process.exitCode = 1; }, 20 * 60_000); + const stop = () => { clearTimeout(timer); socket.close({ force: true }); }; + process.once('SIGINT', stop); process.once('SIGTERM', stop); + await socket.start({ eventDispatcher: new EventDispatcher({ loggerLevel: LoggerLevel.error, logger: credentialSafeLogger }).register({ + 'im.message.receive_v1': async raw => { + const result = eventSchema.safeParse(raw); + if (bound || !result.success) return; + const text = z.object({ text: z.string() }).parse(JSON.parse(result.data.message.content)).text; + if (text.trim() !== command) return; + bound = true; + try { + const config = z.record(z.string(), z.unknown()).parse(await readConfigInput(args.values.config)); + const feishu = z.record(z.string(), z.unknown()).parse(config.feishu); + config.feishu = { ...feishu, userOpenId: result.data.sender.sender_id.open_id }; + await writeFile(args.values.config, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 }); + const reply = await client.im.message.create({ params: { receive_id_type: 'open_id' }, data: { receive_id: result.data.sender.sender_id.open_id, msg_type: 'text', content: JSON.stringify({ text: 'Agent Loop 已绑定你的账号。后续每个任务使用独立群,文字、截图、日志和确认都会回到对应任务会话。' }) } }); + if (reply.code !== 0) throw new Error(`飞书回复失败 ${reply.code}`); + process.stdout.write(publicJson({ paired: true, config: args.values.config }) + '\n'); + } finally { stop(); } + }, + }) }); + process.stdout.write(publicJson({ pairingCommand: command, instruction: '在飞书中打开 Agent Loop 小助手,发送 pairingCommand。' }) + '\n'); +} +main().catch(error => { process.stderr.write(errorMessage(error) + '\n'); process.exitCode = 1; }); diff --git a/scripts/verify-feishu.ts b/scripts/verify-feishu.ts index 7eb9dfe..6ee21c4 100644 --- a/scripts/verify-feishu.ts +++ b/scripts/verify-feishu.ts @@ -8,106 +8,109 @@ import { Client, LoggerLevel } from '@larksuiteoapi/node-sdk'; import { z } from 'zod'; import { FeishuAdapter } from '../src/adapters/feishu.js'; import { loadConfig } from '../src/config.js'; -import { UncertainOperationError } from '../src/domain.js'; +import { credentialSafeLogger, publicJson } from '../src/credentials.js'; +import { UncertainOperationError, errorMessage } from '../src/domain.js'; -const args = parseArgs({ options: { config: { type: 'string', default: 'config.json' }, listen: { type: 'boolean', default: false } } }); -process.loadEnvFile('.env'); -const config = await loadConfig(args.values.config); -assert.ok(!config.feishu.userOpenId.startsWith('REPLACE_'), 'Pair the owner first'); -const appId = z.string().min(1).parse(process.env[config.feishu.appIdEnv]); -const appSecret = z.string().min(1).parse(process.env[config.feishu.appSecretEnv]); -const api = new Client({ appId, appSecret, loggerLevel: LoggerLevel.error }); -const adapter = new FeishuAdapter({ appId, appSecret, userOpenId: config.feishu.userOpenId }); -const runId = randomUUID(); -const directory = resolve('.scratch/verification', `feishu-${Date.now()}`); -await mkdir(directory, { recursive: true }); -const evidence: Record = { startedAt: new Date().toISOString(), runId, limitations: ['Resource round trips use bot-sent synthetic fixtures; they do not prove owner-uploaded screenshot or log handling through Codex.'] }; -const save = () => writeFile(`${directory}/result.json`, `${JSON.stringify(evidence, null, 2)}\n`, { mode: 0o600 }); -const sha256 = (bytes: Buffer) => createHash('sha256').update(bytes).digest('hex'); -try { - const group = { taskId: 'connectivity-check-v1', title: 'Agent Loop 联通验证', sourceUrl: 'https://github.com/crysound/OpenTest-Internal' }; - const chatId = await adapter.ensureGroup(group); - evidence.chatId = chatId; - await adapter.verifyGroup(chatId); - const listDeadline = Date.now() + 60_000; - while (true) { - try { - assert.equal(await adapter.ensureGroup({ ...group, reconcileOnly: true }), chatId); - break; - } catch (error) { - if (!(error instanceof UncertainOperationError) || Date.now() >= listDeadline) throw error; - await delay(2000); +async function main(): Promise { + const args = parseArgs({ options: { config: { type: 'string', default: 'config.json' }, listen: { type: 'boolean', default: false } } }); + const config = await loadConfig(args.values.config); + assert.ok(!config.feishu.userOpenId.startsWith('REPLACE_'), 'Pair the owner first'); + const appId = config.feishu.appId.require('feishu.appId'); + const appSecret = config.feishu.appSecret.require('feishu.appSecret'); + const api = new Client({ appId, appSecret, loggerLevel: LoggerLevel.error, logger: credentialSafeLogger }); + const adapter = new FeishuAdapter({ appId, appSecret, userOpenId: config.feishu.userOpenId }); + const runId = randomUUID(); + const directory = resolve('.scratch/verification', `feishu-${Date.now()}`); + await mkdir(directory, { recursive: true }); + const evidence: Record = { startedAt: new Date().toISOString(), runId, limitations: ['Resource round trips use bot-sent synthetic fixtures; they do not prove owner-uploaded screenshot or log handling through Codex.'] }; + const save = () => writeFile(`${directory}/result.json`, `${publicJson(evidence, 2)}\n`, { mode: 0o600 }); + const sha256 = (bytes: Buffer) => createHash('sha256').update(bytes).digest('hex'); + try { + const group = { taskId: 'connectivity-check-v1', title: 'Agent Loop 联通验证', sourceUrl: 'https://github.com/crysound/OpenTest-Internal' }; + const chatId = await adapter.ensureGroup(group); + evidence.chatId = chatId; + await adapter.verifyGroup(chatId); + const listDeadline = Date.now() + 60_000; + while (true) { + try { + assert.equal(await adapter.ensureGroup({ ...group, reconcileOnly: true }), chatId); + break; + } catch (error) { + if (!(error instanceof UncertainOperationError) || Date.now() >= listDeadline) throw error; + await delay(2000); + } + } + evidence.groupVerifiedAndReused = true; + await adapter.send({ chatId, id: `${runId}:start`, text: '这是 Agent Loop 联通验证群,仅用于验证消息和附件,不会启动业务任务。接下来发送一张测试图片和一份不含业务数据的测试日志。' }); + const image = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+a9mQAAAAASUVORK5CYII=', 'base64'); + const log = Buffer.from(`Agent Loop 联通验证\nnonce=${runId}\n`, 'utf8'); + const imageKey = z.object({ image_key: z.string().min(1) }).parse(await api.im.image.create({ data: { image_type: 'message', image } })).image_key; + const fileKey = z.object({ file_key: z.string().min(1) }).parse(await api.im.file.create({ data: { file_type: 'stream', file_name: 'agent-loop-test.log', file: log } })).file_key; + const resources = [ + { kind: 'image' as const, key: imageKey, name: 'fixture.png', original: image }, + { kind: 'file' as const, key: fileKey, name: 'fixture.log', original: log }, + ]; + const checks = []; + for (const resource of resources) { + const content = resource.kind === 'image' ? { image_key: resource.key } : { file_key: resource.key }; + const response = await api.im.message.create({ params: { receive_id_type: 'chat_id' }, data: { receive_id: chatId, msg_type: resource.kind, content: publicJson(content), uuid: randomUUID() } }); + const sent = z.object({ code: z.literal(0), data: z.object({ message_id: z.string().min(1), body: z.object({ content: z.string() }) }) }).parse(response).data; + const messageId = sent.message_id; + const sentContent: unknown = JSON.parse(sent.body.content); + const key = resource.kind === 'image' + ? z.object({ image_key: z.string().min(1) }).parse(sentContent).image_key + : z.object({ file_key: z.string().min(1) }).parse(sentContent).file_key; + const destination = `${directory}/${resource.name}`; + await adapter.download({ messageId, attachment: { ...resource, key }, destination, maxBytes: config.maxAttachmentBytes }); + const actual = await readFile(destination); + assert.deepEqual(actual, resource.original, `${resource.kind} bytes must survive the round trip`); + checks.push({ kind: resource.kind, messageId, bytes: actual.length, sha256: sha256(actual), exactMatch: true }); + evidence.resources = checks; + await save(); } - } - evidence.groupVerifiedAndReused = true; - await adapter.send({ chatId, id: `${runId}:start`, text: '这是 Agent Loop 联通验证群,仅用于验证消息和附件,不会启动业务任务。接下来发送一张测试图片和一份不含业务数据的测试日志。' }); - const image = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+a9mQAAAAASUVORK5CYII=', 'base64'); - const log = Buffer.from(`Agent Loop 联通验证\nnonce=${runId}\n`, 'utf8'); - const imageKey = z.object({ image_key: z.string().min(1) }).parse(await api.im.image.create({ data: { image_type: 'message', image } })).image_key; - const fileKey = z.object({ file_key: z.string().min(1) }).parse(await api.im.file.create({ data: { file_type: 'stream', file_name: 'agent-loop-test.log', file: log } })).file_key; - const resources = [ - { kind: 'image' as const, key: imageKey, name: 'fixture.png', original: image }, - { kind: 'file' as const, key: fileKey, name: 'fixture.log', original: log }, - ]; - const checks = []; - for (const resource of resources) { - const content = resource.kind === 'image' ? { image_key: resource.key } : { file_key: resource.key }; - const response = await api.im.message.create({ params: { receive_id_type: 'chat_id' }, data: { receive_id: chatId, msg_type: resource.kind, content: JSON.stringify(content), uuid: randomUUID() } }); - const sent = z.object({ code: z.literal(0), data: z.object({ message_id: z.string().min(1), body: z.object({ content: z.string() }) }) }).parse(response).data; - const messageId = sent.message_id; - const sentContent: unknown = JSON.parse(sent.body.content); - const key = resource.kind === 'image' - ? z.object({ image_key: z.string().min(1) }).parse(sentContent).image_key - : z.object({ file_key: z.string().min(1) }).parse(sentContent).file_key; - const destination = `${directory}/${resource.name}`; - await adapter.download({ messageId, attachment: { ...resource, key }, destination, maxBytes: config.maxAttachmentBytes }); - const actual = await readFile(destination); - assert.deepEqual(actual, resource.original, `${resource.kind} bytes must survive the round trip`); - checks.push({ kind: resource.kind, messageId, bytes: actual.length, sha256: sha256(actual), exactMatch: true }); evidence.resources = checks; + evidence.ownerHistoryCount = (await adapter.history({ chatId, since: 0 })).length; + evidence.apiResult = 'passed'; await save(); - } - evidence.resources = checks; - evidence.ownerHistoryCount = (await adapter.history({ chatId, since: 0 })).length; - evidence.apiResult = 'passed'; - await save(); - if (args.values.listen) { - const command = `验证 ${runId.slice(0, 8)}`; - evidence.ownerCommand = command; - evidence.ownerMessage = 'pending'; + if (args.values.listen) { + const command = `验证 ${runId.slice(0, 8)}`; + evidence.ownerCommand = command; + evidence.ownerMessage = 'pending'; + await save(); + let finish: () => void = () => {}; + const received = new Promise(resolve => { finish = resolve; }); + const timer = setTimeout(finish, 10 * 60_000); + let matched = false; + try { + await adapter.start(async message => { + if (message.chatId !== chatId || message.text.trim() !== command || matched) return; + matched = true; + try { + const history = await adapter.history({ chatId, since: message.createdAt - 1000 }); + assert.ok(history.some(item => item.id === message.id), 'Live owner message must appear in history'); + await adapter.send({ chatId, id: `${runId}:owner`, text: '验证通过:已实时收到你的消息,历史补收也能找到同一条消息。测试图片和日志下载后的内容与原文件完全一致。' }); + evidence.ownerMessage = { id: message.id, liveReceived: true, foundInHistory: true, replySent: true }; + } catch (error) { evidence.ownerMessageError = errorMessage(error); } + finally { finish(); } + }, error => { evidence.connectionError = errorMessage(error); finish(); }); + await adapter.send({ chatId, id: `${runId}:command`, text: `图片和日志传输验证已通过。请在本群发送「${command}」,完成你的群消息接收验证。` }); + process.stdout.write(`${publicJson({ chatId, ownerCommand: command, evidence: `${directory}/result.json` })}\n`); + await received; + assert.equal(typeof evidence.ownerMessage, 'object', 'Owner message verification was not completed'); + assert.equal(evidence.ownerMessageError, undefined); + assert.equal(evidence.connectionError, undefined); + } finally { clearTimeout(timer); } + } + evidence.result = 'passed'; + } catch (error) { + evidence.result = 'failed'; + evidence.error = errorMessage(error); + process.exitCode = 1; + } finally { + await adapter.close(); + evidence.finishedAt = new Date().toISOString(); await save(); - let finish: () => void = () => {}; - const received = new Promise(resolve => { finish = resolve; }); - const timer = setTimeout(finish, 10 * 60_000); - let matched = false; - try { - await adapter.start(async message => { - if (message.chatId !== chatId || message.text.trim() !== command || matched) return; - matched = true; - try { - const history = await adapter.history({ chatId, since: message.createdAt - 1000 }); - assert.ok(history.some(item => item.id === message.id), 'Live owner message must appear in history'); - await adapter.send({ chatId, id: `${runId}:owner`, text: '验证通过:已实时收到你的消息,历史补收也能找到同一条消息。测试图片和日志下载后的内容与原文件完全一致。' }); - evidence.ownerMessage = { id: message.id, liveReceived: true, foundInHistory: true, replySent: true }; - } catch (error) { evidence.ownerMessageError = String(error); } - finally { finish(); } - }, error => { evidence.connectionError = String(error); finish(); }); - await adapter.send({ chatId, id: `${runId}:command`, text: `图片和日志传输验证已通过。请在本群发送「${command}」,完成你的群消息接收验证。` }); - process.stdout.write(`${JSON.stringify({ chatId, ownerCommand: command, evidence: `${directory}/result.json` })}\n`); - await received; - assert.equal(typeof evidence.ownerMessage, 'object', 'Owner message verification was not completed'); - assert.equal(evidence.ownerMessageError, undefined); - assert.equal(evidence.connectionError, undefined); - } finally { clearTimeout(timer); } + process.stdout.write(`${publicJson({ result: evidence.result, evidence: `${directory}/result.json`, error: evidence.error })}\n`); } - evidence.result = 'passed'; -} catch (error) { - evidence.result = 'failed'; - evidence.error = String(error); - process.exitCode = 1; -} finally { - await adapter.close(); - evidence.finishedAt = new Date().toISOString(); - await save(); - process.stdout.write(`${JSON.stringify({ result: evidence.result, evidence: `${directory}/result.json`, error: evidence.error })}\n`); } +main().catch(error => { process.stderr.write(errorMessage(error) + '\n'); process.exitCode = 1; }); diff --git a/src/adapters/feishu.ts b/src/adapters/feishu.ts index 9f04b7a..9b6b139 100644 --- a/src/adapters/feishu.ts +++ b/src/adapters/feishu.ts @@ -6,6 +6,7 @@ import { pipeline } from 'node:stream/promises'; import { setTimeout as delay } from 'node:timers/promises'; import { Client, WSClient, EventDispatcher, Domain, LoggerLevel, defaultHttpInstance, type HttpInstance, type HttpRequestOptions } from '@larksuiteoapi/node-sdk'; import { z } from 'zod'; +import { credentialSafeLogger } from '../credentials.js'; import { UncertainOperationError, errorMessage, projectDecisionActionSchema, type ApprovalAction, type Attachment, type IncomingMessage, type PendingRequest, type ProjectDecisionAction, type ReplyTarget } from '../domain.js'; import { FeishuApiError, GroupCreationRejected, TaskGroupUnavailable, type FeishuCardDelivery, type FeishuDecisionCardInput, type FeishuPort, type FeishuProgressPort, type FeishuReconnectEvent, type ProgressWriteGuard } from '../ports.js'; import { FeishuDocumentAdapter, type FeishuDocumentApi } from './feishu-document.js'; @@ -252,7 +253,7 @@ export class FeishuAdapter implements FeishuPort, FeishuProgressPort { this.documentProgress = new FeishuDocumentAdapter(() => { if (!documentApi) { const client = new Client({ appId: this.options.appId, appSecret: this.options.appSecret, - domain: Domain.Feishu, httpInstance: this.http, loggerLevel: LoggerLevel.error }); + domain: Domain.Feishu, httpInstance: this.http, loggerLevel: LoggerLevel.error, logger: credentialSafeLogger }); documentApi = { document: client.docx.v1.document, children: client.docx.v1.documentBlockChildren, block: client.docx.v1.documentBlock, members: client.drive.v1.permissionMember, tabs: client.im.v1.chatTab }; } @@ -261,12 +262,12 @@ export class FeishuAdapter implements FeishuPort, FeishuProgressPort { } private get api(): FeishuApi { this.apiInstance ??= new Client({ appId: this.options.appId, appSecret: this.options.appSecret, - domain: Domain.Feishu, httpInstance: this.http, loggerLevel: LoggerLevel.error }).im.v1; + domain: Domain.Feishu, httpInstance: this.http, loggerLevel: LoggerLevel.error, logger: credentialSafeLogger }).im.v1; return this.apiInstance; } private get cardkit(): NonNullable { this.cardkitInstance ??= new Client({ appId: this.options.appId, appSecret: this.options.appSecret, - domain: Domain.Feishu, httpInstance: this.http, loggerLevel: LoggerLevel.error }).cardkit.v1; + domain: Domain.Feishu, httpInstance: this.http, loggerLevel: LoggerLevel.error, logger: credentialSafeLogger }).cardkit.v1; return this.cardkitInstance; } private async paceProgressRequest(): Promise { @@ -546,7 +547,7 @@ export class FeishuAdapter implements FeishuPort, FeishuProgressPort { return [...messages.values()].sort((a, b) => a.createdAt - b.createdAt || a.id.localeCompare(b.id)); } async start(onMessage: (message: IncomingMessage) => Promise, onError: (error: unknown) => void, onAction?: (action: ApprovalAction) => Promise, onReconnect?: (event: FeishuReconnectEvent) => void, onDecision?: (action: ProjectDecisionAction) => Promise): Promise { - const eventDispatcher = new EventDispatcher({ loggerLevel: LoggerLevel.error }).register({ + const eventDispatcher = new EventDispatcher({ loggerLevel: LoggerLevel.error, logger: credentialSafeLogger }).register({ 'card.action.trigger': async (raw: unknown) => { const receivedAt = Date.now(); let action: ApprovalAction | ProjectDecisionAction | null = null; @@ -605,7 +606,7 @@ export class FeishuAdapter implements FeishuPort, FeishuProgressPort { reject(new Error('Feishu long connection did not become ready before its timeout')); }, this.options.requestTimeoutMs ?? 30_000); this.socket = new WSClient({ appId: this.options.appId, appSecret: this.options.appSecret, - domain: Domain.Feishu, httpInstance: this.http, loggerLevel: LoggerLevel.error, + domain: Domain.Feishu, httpInstance: this.http, loggerLevel: LoggerLevel.error, logger: credentialSafeLogger, handshakeTimeoutMs: this.options.requestTimeoutMs ?? 30_000, onReady: () => { clearTimeout(timer); resolve(); }, onError: error => { clearTimeout(timer); onError(error); reject(error); }, diff --git a/src/cli.ts b/src/cli.ts index 13b85bc..bb56dbd 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -14,9 +14,9 @@ import { discoverProjects } from './project-discovery.js'; import { Client } from '@larksuiteoapi/node-sdk'; import { FeishuArtifactAdapter } from './adapters/feishu-artifact.js'; import { StoreArtifactDeliveryLedger } from './feishu-artifact-ledger.js'; +import { credentialSafeLogger, publicJson } from './credentials.js'; -function output(value: unknown): void { process.stdout.write(`${JSON.stringify(value, null, 2)}\n`); } -function secret(name: string): string { const value = process.env[name]; if (!value) throw new Error(`缺少环境变量 ${name}`); return value; } +function output(value: unknown): void { process.stdout.write(`${publicJson(value, 2)}\n`); } const usage = `Agent Loop npm start -- doctor --config config.json @@ -33,7 +33,11 @@ Send new work only in the project group. Only coordinators dispatch Workers. `; async function doctor(config: Config): Promise { - const credentials = [...(config.repositories.length ? [config.github.tokenEnv] : []), config.feishu.appIdEnv, config.feishu.appSecretEnv].map(name => ({ name, present: Boolean(process.env[name]) })); + const credentials = [ + ...(config.repositories.length ? [{ name: 'github.token', present: config.github.token.configured }] : []), + { name: 'feishu.appId', present: config.feishu.appId.configured }, + { name: 'feishu.appSecret', present: config.feishu.appSecret.configured }, + ]; const repositories = await Promise.all(config.repositories.map(async repository => { if (!('path' in repository)) return { name: repository.name, criteria: repository.criteria, sourceOnly: true, exists: true }; const details = { name: repository.name, path: repository.path, baseBranch: repository.baseBranch, @@ -56,7 +60,7 @@ async function doctor(config: Config): Promise { if (!ready) process.exitCode = 1; } async function main(): Promise { - const args = parseArgs({ options: { config: { type: 'string', default: 'config.json' }, 'env-file': { type: 'string', default: '.env' }, help: { type: 'boolean', short: 'h' }, + const args = parseArgs({ options: { config: { type: 'string', default: 'config.json' }, help: { type: 'boolean', short: 'h' }, 'request-id': { type: 'string' } }, allowPositionals: true }); const command = args.positionals[0]; if (args.values.help || !command) { process.stdout.write(usage); return; } @@ -91,24 +95,21 @@ async function main(): Promise { finally { store.close(); } return; } - const envFile = resolve(args.values['env-file']); - try { await access(envFile); process.loadEnvFile(envFile); } - catch (error) { if (!(error instanceof Error && 'code' in error && error.code === 'ENOENT' && args.values['env-file'] === '.env')) throw error; } if (command === 'doctor') { await doctor(config); return; } - const token = config.repositories.length ? secret(config.github.tokenEnv) : ''; + const token = config.repositories.length ? config.github.token.require('github.token') : ''; if (command === 'run' && config.feishu.userOpenId.startsWith('REPLACE_')) throw new Error('请先配置飞书用户 open_id'); - const appId = command === 'run' ? secret(config.feishu.appIdEnv) : ''; - const appSecret = command === 'run' ? secret(config.feishu.appSecretEnv) : ''; + const appId = command === 'run' ? config.feishu.appId.require('feishu.appId') : ''; + const appSecret = command === 'run' ? config.feishu.appSecret.require('feishu.appSecret') : ''; const store = new Store(config.dataDir); const feishuArtifacts = new FeishuArtifactAdapter(() => { - const artifactClient = new Client({ appId, appSecret }); + const artifactClient = new Client({ appId, appSecret, logger: credentialSafeLogger }); return { image: artifactClient.im.v1.image, file: artifactClient.im.v1.file, message: artifactClient.im.v1.message }; }, { maxImageBytes: config.maxAttachmentBytes, maxFileBytes: config.maxAttachmentBytes }, new StoreArtifactDeliveryLedger(store)); const loop = new AgentLoop(config, { github: new GitHubAdapter({ token, apiUrl: config.github.apiUrl, requestTimeoutMs: config.github.requestTimeoutMs }), feishu: new FeishuAdapter({ appId, appSecret, userOpenId: config.feishu.userOpenId }), feishuArtifacts, - codex: new CodexAppServer({ ...config.execution.backends.codex, env: Object.fromEntries(Object.entries(process.env).filter(([name]) => ![config.feishu.appIdEnv, config.feishu.appSecretEnv].includes(name))) }), workspace: new WorkspaceProvider(config.dataDir), + codex: new CodexAppServer(config.execution.backends.codex), workspace: new WorkspaceProvider(config.dataDir), }, store); if (command === 'collect') { try { await loop.collect(); output({ collectedTasks: loop.tasks().filter(task => loop.projects.ledger.membership(task.id)?.role !== 'coordinator').length, diagnostics: loop.store.diagnostics() }); if (loop.store.diagnostics().length) process.exitCode = 1; } diff --git a/src/config.ts b/src/config.ts index f9b4c5e..8518e78 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,8 +6,10 @@ import { collectionRepositorySchema } from './domain.js'; import { projectConfigInputSchema } from './project.js'; import { daemonSettingsSchema, normalizeDaemonSettings } from './adapters/codex-daemon.js'; import { assertSupportedExecutionBackends, executionConfigSchema } from './execution.js'; +import { ConfigCredential, redactCredentials } from './credentials.js'; -const environmentName = z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/, 'Use an environment variable name, not a credential'); +const credential = z.preprocess(value => value instanceof ConfigCredential ? value.rawValue : value, + z.string().trim().default('').transform(value => new ConfigCredential(value))); const positiveMilliseconds = z.number().int().positive(); export const documentMaxLinesSchema = z.number().int().positive().max(100_000).default(3000); export const documentBaseUrlSchema = z.url().max(2048).refine(value => { @@ -38,13 +40,13 @@ export const configSchema = z.strictObject({ }), recovery: z.strictObject({ maxAttempts: z.number().int().nonnegative().default(3), retryDelayMs: positiveMilliseconds.default(1000) }).default({ maxAttempts: 3, retryDelayMs: 1000 }), github: z.strictObject({ - tokenEnv: environmentName.default('GITHUB_TOKEN'), + token: credential, apiUrl: z.url().refine(value => ['http:', 'https:'].includes(new URL(value).protocol)).default('https://api.github.com'), requestTimeoutMs: positiveMilliseconds.default(30_000), - }).default(() => ({ tokenEnv: 'GITHUB_TOKEN', apiUrl: 'https://api.github.com', requestTimeoutMs: 30_000 })), + }).prefault({}), feishu: z.strictObject({ - appIdEnv: environmentName.default('FEISHU_APP_ID'), - appSecretEnv: environmentName.default('FEISHU_APP_SECRET'), + appId: credential, + appSecret: credential, userOpenId: z.string().min(1), progress: z.strictObject({ enabled: z.boolean().default(false), @@ -80,32 +82,46 @@ export const configSchema = z.strictObject({ export type Config = z.infer; +export async function readConfigInput(path: string): Promise { + const text = await readFile(path, 'utf8'); + let raw: unknown; + try { raw = JSON.parse(text); } + catch { throw new Error('Invalid configuration JSON'); } + return raw; +} + export async function loadConfig(path: string): Promise { const configPath = resolve(path); const directory = dirname(configPath); - const raw: unknown = JSON.parse(await readFile(configPath, 'utf8')); - const config = configSchema.parse(raw); - config.dataDir = resolve(directory, config.dataDir); - for (const project of config.projects) { - project.directory = resolve(directory, project.directory); - if (!(await stat(project.directory)).isDirectory()) throw new Error(`Project directory must exist: ${project.directory}`); - } - config.repositories = config.repositories.map(repository => !('path' in repository) ? repository : ({ ...repository, path: resolve(directory, repository.path), - ...(repository.workspace?.kind === 'project-directory' - ? { workspace: { ...repository.workspace, path: resolve(directory, repository.workspace.path) } } : {}), - })); - const guidance = config.engineering; - if (guidance) { - if (guidance.projectRoot) guidance.projectRoot = resolve(directory, guidance.projectRoot); - if (guidance.promptPath) guidance.promptPath = resolve(directory, guidance.promptPath); - if (guidance.skillPath) guidance.skillPath = resolve(directory, guidance.skillPath); - for (const file of [guidance.promptPath, guidance.skillPath]) { - if (!file) continue; - await access(file, constants.R_OK); - if (!(await stat(file)).isFile() || !(await readFile(file, 'utf8')).trim()) throw new Error(`Engineering guidance must be a readable, nonempty file: ${file}`); + const raw = await readConfigInput(configPath); + const result = configSchema.safeParse(raw); + if (!result.success) throw new Error('Invalid configuration fields or types. Credential *Env selectors are unsupported.'); + const config = result.data; + try { + config.dataDir = resolve(directory, config.dataDir); + for (const project of config.projects) { + project.directory = resolve(directory, project.directory); + if (!(await stat(project.directory)).isDirectory()) throw new Error(`Project directory must exist: ${project.directory}`); + } + config.repositories = config.repositories.map(repository => !('path' in repository) ? repository : ({ ...repository, path: resolve(directory, repository.path), + ...(repository.workspace?.kind === 'project-directory' + ? { workspace: { ...repository.workspace, path: resolve(directory, repository.workspace.path) } } : {}), + })); + const guidance = config.engineering; + if (guidance) { + if (guidance.projectRoot) guidance.projectRoot = resolve(directory, guidance.projectRoot); + if (guidance.promptPath) guidance.promptPath = resolve(directory, guidance.promptPath); + if (guidance.skillPath) guidance.skillPath = resolve(directory, guidance.skillPath); + for (const file of [guidance.promptPath, guidance.skillPath]) { + if (!file) continue; + await access(file, constants.R_OK); + if (!(await stat(file)).isFile() || !(await readFile(file, 'utf8')).trim()) throw new Error(`Engineering guidance must be a readable, nonempty file: ${file}`); + } } + return config; + } catch (error) { + throw new Error(redactCredentials(error instanceof Error ? error.message : String(error))); } - return config; } export class ExecutionConfigWatcher { diff --git a/src/credentials.ts b/src/credentials.ts new file mode 100644 index 0000000..8a4fde4 --- /dev/null +++ b/src/credentials.ts @@ -0,0 +1,38 @@ +import { inspect } from 'node:util'; + +const credentialValues = new Set(); +const redacted = '[REDACTED]'; + +export class ConfigCredential { + readonly #value: string; + constructor(value: string) { + this.#value = value; + if (this.configured) { + for (const variant of [value, JSON.stringify(value).slice(1, -1), new URLSearchParams({ value }).toString().slice(6).replaceAll('+', '%20')]) credentialValues.add(variant); + } + } + get rawValue(): string { return this.#value; } + get configured(): boolean { return this.#value.length > 0 && !this.#value.startsWith('REPLACE_'); } + require(field: 'github.token' | 'feishu.appId' | 'feishu.appSecret'): string { + if (!this.configured) throw new Error(`Missing configuration credential: ${field}`); + return this.#value; + } + toJSON(): string { return redacted; } + toString(): string { return redacted; } + [inspect.custom](): string { return redacted; } +} + +export function redactCredentials(text: string): string { + for (const value of [...credentialValues].sort((a, b) => b.length - a.length)) text = text.replaceAll(value, redacted); + return text; +} + +export function publicJson(value: unknown, space?: number): string { + return JSON.stringify(value, (_key, item: unknown) => typeof item === 'string' ? redactCredentials(item) : item, space); +} + +const log = (...messages: unknown[]): void => { + try { process.stderr.write(publicJson(messages.map(value => value instanceof Error ? { name: value.name, message: value.message } : value)) + '\n'); } + catch { process.stderr.write('Feishu SDK diagnostic could not be serialized\n'); } +}; +export const credentialSafeLogger = { error: log, warn: log, info: log, debug: log, trace: log }; diff --git a/src/dashboard.ts b/src/dashboard.ts index 53cf964..a78b758 100644 --- a/src/dashboard.ts +++ b/src/dashboard.ts @@ -4,6 +4,7 @@ import { existsSync } from 'node:fs'; import { join } from 'node:path'; import { hostname, networkInterfaces } from 'node:os'; import { z } from 'zod'; +import { publicJson } from './credentials.js'; import { Store } from './store.js'; import { boundedDashboardText, dashboardResources, ProjectDashboardReader } from './dashboard-reader.js'; import { errorMessage, presentTaskSource } from './domain.js'; @@ -29,7 +30,7 @@ export async function startDashboard(options: { execution?: LiveExecution; dataD readStore(); const json = (response: ServerResponse, status: number, value: unknown) => { response.writeHead(status, { 'content-type': 'application/json; charset=utf-8' }); - response.end(JSON.stringify(value)); + response.end(publicJson(value)); }; const readModel = () => { const now = Date.now(); @@ -147,14 +148,14 @@ export async function startDashboard(options: { execution?: LiveExecution; dataD for (const change of changes) { cursor = change.cursor; if (change.event.taskId !== taskId || change.event.threadId !== threadId) continue; - if (!response.write('id: ' + cursor + '\nevent: execution\ndata: ' + JSON.stringify(change.event) + '\n\n')) { blocked = true; break; } + if (!response.write('id: ' + cursor + '\nevent: execution\ndata: ' + publicJson(change.event) + '\n\n')) { blocked = true; break; } } if (Date.now() - lastKeepAlive > 10_000 && !blocked) { lastKeepAlive = Date.now(); blocked = !response.write(': heartbeat\n\n'); } } catch (error) { - end('event: stream-error\ndata: ' + JSON.stringify({ error: errorMessage(error) }) + '\n\n'); + end('event: stream-error\ndata: ' + publicJson({ error: errorMessage(error) }) + '\n\n'); } }; const unsubscribe = execution.subscribe(() => send()); diff --git a/src/domain.ts b/src/domain.ts index f10fa38..188ff0a 100644 --- a/src/domain.ts +++ b/src/domain.ts @@ -1,4 +1,5 @@ import { z } from 'zod'; +import { redactCredentials } from './credentials.js'; export const criteriaSchema = z.object({ states: z.array(z.enum(['open', 'closed'])).default(['open']), @@ -258,4 +259,4 @@ export type FeishuCardRecord = z.infer; export class UncertainOperationError extends Error {} export class TurnChangedError extends Error {} export class RejectedInputError extends Error {} -export function errorMessage(error: unknown): string { return error instanceof Error ? error.message : String(error); } +export function errorMessage(error: unknown): string { return redactCredentials(error instanceof Error ? error.message : String(error)); } diff --git a/src/store.ts b/src/store.ts index 82674ac..81d87b5 100644 --- a/src/store.ts +++ b/src/store.ts @@ -3,6 +3,7 @@ import { mkdirSync } from 'node:fs'; import { join } from 'node:path'; import { createHash } from 'node:crypto'; import { z } from 'zod'; +import { publicJson, redactCredentials } from './credentials.js'; import { approvalActionSchema, feishuCardRecordSchema, inputProvenanceSchema, incomingSchema, repositorySchema, sourceSchema, taskSchema, type ApprovalAction, type FeishuCardRecord, type IncomingMessage, type InputProvenance, type Source, type Task } from './domain.js'; const payloadRow = z.object({ payload: z.string() }); @@ -112,7 +113,7 @@ export class Store { } } saveSource(source: Source, reason: string): void { - const payload = JSON.stringify(source); + const payload = publicJson(source); this.prepare('INSERT INTO sources VALUES(?,?,?) ON CONFLICT(id) DO UPDATE SET payload=excluded.payload,reason=excluded.reason').run(source.id, payload, reason); this.prepare('INSERT OR IGNORE INTO source_revisions VALUES(?,?,?)').run(source.id, createHash('sha256').update(payload).digest('hex'), payload); } @@ -121,7 +122,7 @@ export class Store { } saveTask(task: Task): void { const previousThread = this.taskThreads.get(task.id); - this.prepare('INSERT INTO tasks VALUES(?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET chat_id=excluded.chat_id,thread_id=excluded.thread_id,payload=excluded.payload').run(task.id, task.sourceId, task.chatId, task.threadId, JSON.stringify(task)); + this.prepare('INSERT INTO tasks VALUES(?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET chat_id=excluded.chat_id,thread_id=excluded.thread_id,payload=excluded.payload').run(task.id, task.sourceId, task.chatId, task.threadId, publicJson(task)); if (previousThread && previousThread !== task.threadId) this.taskIdsByThread.delete(previousThread); this.taskThreads.set(task.id, task.threadId); if (task.threadId) this.taskIdsByThread.set(task.threadId, task.id); @@ -169,7 +170,7 @@ export class Store { const facts = provenance ?? (origin === 'user' ? { kind: 'user', message } : origin === 'source' ? { kind: 'github_issue', sourceId: task.sourceId, revision: message.id, url: task.source.kind === 'github' ? task.source.issue.url : '' } : origin === 'initial' ? { kind: 'initial', sourceId: task.sourceId } : { kind: 'recovery', previousTurnId: task.turnId ?? 'legacy' }); - return this.prepare("INSERT OR IGNORE INTO inbox(id,task_id,payload,status,origin) VALUES(?,?,?,'pending',?)").run(message.id, taskId, JSON.stringify({ message, provenance: facts, requestId: null }), origin).changes > 0; + return this.prepare("INSERT OR IGNORE INTO inbox(id,task_id,payload,status,origin) VALUES(?,?,?,'pending',?)").run(message.id, taskId, publicJson({ message, provenance: facts, requestId: null }), origin).changes > 0; } inputs(taskId: string, filter: InboxFilter = {}): Inbox[] { const clauses = ['task_id=?']; @@ -208,12 +209,12 @@ export class Store { associateRequest(id: string, taskId: string, requestId: string): void { const input = this.inputs(taskId).find(item => item.id === id); if (!input) throw new Error('Unknown inbox input'); - this.prepare('UPDATE inbox SET payload=? WHERE id=?').run(JSON.stringify({ message: input.message, provenance: input.provenance, requestId }), id); + this.prepare('UPDATE inbox SET payload=? WHERE id=?').run(publicJson({ message: input.message, provenance: input.provenance, requestId }), id); } - markInput(id: string, status: Inbox['status'], error: string | null = null): void { this.prepare('UPDATE inbox SET status=?,error=? WHERE id=?').run(status, error, id); } + markInput(id: string, status: Inbox['status'], error: string | null = null): void { this.prepare('UPDATE inbox SET status=?,error=? WHERE id=?').run(status, error === null ? null : redactCredentials(error), id); } enqueue(taskId: string, text: string, key: string): string { const id = createHash('sha256').update(`${taskId}:${key}`).digest('hex').slice(0, 32); - this.prepare("INSERT OR IGNORE INTO outbox VALUES(?,?,?,'pending',NULL)").run(id, taskId, text); + this.prepare("INSERT OR IGNORE INTO outbox VALUES(?,?,?,'pending',NULL)").run(id, taskId, redactCredentials(text)); return id; } hasOutput(taskId: string, key: string): boolean { @@ -229,18 +230,18 @@ export class Store { if (!statuses.length) return false; return this.prepare(`SELECT 1 FROM outbox WHERE task_id=? AND status IN (${statuses.map(() => '?').join(',')}) LIMIT 1`).get(taskId, ...statuses) !== undefined; } - markOutput(id: string, status: Outbox['status'], error: string | null = null): void { this.prepare('UPDATE outbox SET status=?,error=? WHERE id=?').run(status, error, id); } + markOutput(id: string, status: Outbox['status'], error: string | null = null): void { this.prepare('UPDATE outbox SET status=?,error=? WHERE id=?').run(status, error === null ? null : redactCredentials(error), id); } operation(id: string): Operation | null { const row = this.prepare('SELECT * FROM operations WHERE id=?').get(id); return row ? operationSchema.parse(row) : null; } beginOperation(id: string, taskId: string, kind: string): void { this.prepare("INSERT INTO operations VALUES(?,?,?,'attempting',NULL,NULL)").run(id, taskId, kind); } retryRejectedOperation(id: string): void { const result = this.prepare("UPDATE operations SET status='attempting',result=NULL,error=NULL WHERE id=? AND status='uncertain' AND error='rejected'").run(id); if (result.changes !== 1) throw new Error('Operation is not a rejected attempt'); } - finishOperation(id: string, result: string): void { this.prepare("UPDATE operations SET status='confirmed',result=?,error=NULL WHERE id=?").run(result, id); } - uncertainOperation(id: string, error: string): void { this.prepare("UPDATE operations SET status='uncertain',error=? WHERE id=?").run(error, id); } + finishOperation(id: string, result: string): void { this.prepare("UPDATE operations SET status='confirmed',result=?,error=NULL WHERE id=?").run(redactCredentials(result), id); } + uncertainOperation(id: string, error: string): void { this.prepare("UPDATE operations SET status='uncertain',error=? WHERE id=?").run(redactCredentials(error), id); } queueApproval(action: ApprovalAction): boolean { const id = this.approvalKey(action); - return this.prepare("INSERT OR IGNORE INTO approval_actions VALUES(?,?,?,'pending',NULL)").run(id, action.taskId, JSON.stringify(action)).changes > 0; + return this.prepare("INSERT OR IGNORE INTO approval_actions VALUES(?,?,?,'pending',NULL)").run(id, action.taskId, publicJson(action)).changes > 0; } approvalKey(action: ApprovalAction): string { return `approval:${action.taskId}:${action.epoch}:${action.requestId}`; } approvals(taskId: string): { id: string; action: ApprovalAction; status: string; error: string | null }[] { @@ -250,12 +251,12 @@ export class Store { }); } markApproval(id: string, status: 'processing' | 'confirmed' | 'uncertain' | 'rejected', error: string | null = null): void { - this.prepare('UPDATE approval_actions SET status=?,error=? WHERE id=?').run(status, error, id); + this.prepare('UPDATE approval_actions SET status=?,error=? WHERE id=?').run(status, error === null ? null : redactCredentials(error), id); } saveFeishuCard(record: FeishuCardRecord): void { const card = feishuCardRecordSchema.parse(record); this.prepare('INSERT INTO feishu_cards(id,request_id,payload) VALUES(?,?,?) ON CONFLICT(id) DO UPDATE SET request_id=excluded.request_id,payload=excluded.payload') - .run(card.id, card.requestId, JSON.stringify(card)); + .run(card.id, card.requestId, publicJson(card)); } feishuCard(id: string): FeishuCardRecord | null { const row = this.prepare('SELECT payload FROM feishu_cards WHERE id=?').get(id); @@ -279,6 +280,8 @@ export class Store { return next; } diagnostic(scope: string, message: string): void { + scope = redactCredentials(scope); + message = redactCredentials(message); const createdAt = Date.now(); const bounded = message.length <= diagnosticMessageLimit ? message : message.slice(0, diagnosticMessageLimit) + '\n…'; const fingerprint = createHash('sha256').update(`${scope}\u0000${bounded}`).digest('hex'); @@ -317,7 +320,7 @@ export class Store { return sequenceRow.parse(this.prepare('SELECT COALESCE(MAX(seq),0) AS seq FROM inbox WHERE task_id=?').get(taskId)).seq; } setMeta(key: string, value: string): void { - const result = this.prepare('INSERT INTO meta VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value WHERE meta.value IS NOT excluded.value').run(key, value); + const result = this.prepare('INSERT INTO meta VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value WHERE meta.value IS NOT excluded.value').run(key, redactCredentials(value)); if (result.changes > 0) this.metaRevisions.delete(key); } deleteMeta(key: string): void { @@ -336,7 +339,7 @@ export class Store { return this.prepare('SELECT payload FROM feishu_progress WHERE active=1 ORDER BY rowid').all().map(row => JSON.parse(payloadRow.parse(row).payload)); } saveProgressRecord(taskId: string, turnId: string, record: unknown, active: boolean): void { - this.prepare('INSERT INTO feishu_progress VALUES(?,?,?,?) ON CONFLICT(task_id,turn_id) DO UPDATE SET payload=excluded.payload,active=excluded.active').run(taskId, turnId, JSON.stringify(record), Number(active)); + this.prepare('INSERT INTO feishu_progress VALUES(?,?,?,?) ON CONFLICT(task_id,turn_id) DO UPDATE SET payload=excluded.payload,active=excluded.active').run(taskId, turnId, publicJson(record), Number(active)); } close(): void { this.db.close(); this.lock?.close(); @@ -367,7 +370,7 @@ export class Store { return this.prepare('SELECT payload FROM feishu_document_progress ORDER BY rowid').all().map(row => JSON.parse(payloadRow.parse(row).payload)); } saveDocumentProgressRecord(taskId: string, record: unknown): void { - this.prepare('INSERT INTO feishu_document_progress VALUES(?,?) ON CONFLICT(task_id) DO UPDATE SET payload=excluded.payload').run(taskId, JSON.stringify(record)); + this.prepare('INSERT INTO feishu_document_progress VALUES(?,?) ON CONFLICT(task_id) DO UPDATE SET payload=excluded.payload').run(taskId, publicJson(record)); this.documentPayloads.delete(taskId); } } diff --git a/test/cli.test.ts b/test/cli.test.ts index 129fc07..eae2d8f 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -18,16 +18,15 @@ test('doctor reports discovered projects and source-only collection without requ const root = await mkdtemp(join(tmpdir(), 'agent-loop-doctor-discovery-')); t.after(() => rm(root, { recursive: true, force: true })); await mkdir(join(root, 'projects', 'multi-repo', 'nested'), { recursive: true }); - await mkdir(join(root, 'projects', '.metadata')); await writeFile(join(root, 'fixture.env'), ''); + await mkdir(join(root, 'projects', '.metadata')); const configPath = join(root, 'config.json'); await writeFile(configPath, JSON.stringify({ execution, engineering: { projectRoot: './projects' }, repositories: [{ name: 'explicit/issues', criteria: {} }], - github: { tokenEnv: 'FIXTURE_DOCTOR_TOKEN' }, - feishu: { userOpenId: 'owner', appIdEnv: 'FIXTURE_DOCTOR_APP', appSecretEnv: 'FIXTURE_DOCTOR_SECRET' }, + github: { token: 'synthetic-doctor-github' }, + feishu: { userOpenId: 'owner', appId: 'synthetic-doctor-app', appSecret: 'synthetic-doctor-secret' }, })); - const env = { ...process.env, FIXTURE_DOCTOR_TOKEN: 'fixture', FIXTURE_DOCTOR_APP: 'fixture', FIXTURE_DOCTOR_SECRET: 'fixture' }; - const run = () => execute(process.execPath, [cli, entry, 'doctor', '--config', configPath, '--env-file', join(root, 'fixture.env')], { cwd: root, env }); + const run = () => execute(process.execPath, [cli, entry, 'doctor', '--config', configPath], { cwd: root }); const output = JSON.parse((await run()).stdout); assert.equal(output.ready, true); assert.equal(output.discovery.readable, true); assert.deepEqual(output.execution, { coordinator: execution.coordinator, worker: execution.worker }); @@ -78,10 +77,10 @@ for (const scenario of [ execution, dataDir: './data', repositories: [{ name: 'example/project', path: './repository', baseBranch: 'main', criteria: { assigneesAny: ['owner'], taskTypes: scenario.taskTypes, nativeTypes: { [scenario.classification]: scenario.classification }, typeLabels: { [scenario.classification]: scenario.classification } } }], engineering: { promptPath: './prompt.md', skillPath: './SKILL.md' }, - github: { tokenEnv: 'TEST_GITHUB_TOKEN', apiUrl }, - feishu: { appIdEnv: 'UNSET_FEISHU_APP_ID', appSecretEnv: 'UNSET_FEISHU_SECRET', userOpenId: 'REPLACE_WITH_YOUR_OPEN_ID' }, + github: { token: 'test-token', apiUrl }, + feishu: { userOpenId: 'REPLACE_WITH_YOUR_OPEN_ID' }, })); - const options = { cwd: root, env: { ...process.env, TEST_GITHUB_TOKEN: 'test-token' }, timeout: 15_000 }; + const options = { cwd: root, env: { ...process.env, GITHUB_TOKEN: 'conflicting-env-token' }, timeout: 15_000 }; const collect = await execute(process.execPath, [cli, entry, 'collect', '--config', 'config.json'], options); assert.deepEqual(JSON.parse(collect.stdout), { collectedTasks: scenario.expectedNumbers.length, diagnostics: [] }); assert.equal(requests.filter(url => !/issues\/\d+/.test(url)).length, 2); @@ -129,9 +128,9 @@ async function runFixture(t: TestContext, options: { } await writeFile(join(root, 'config.json'), JSON.stringify({ execution, - dataDir: './data', repositories: [], github: { tokenEnv: 'UNSET_MANUAL_ONLY_GITHUB' }, + dataDir: './data', repositories: [], github: {}, dashboard: { port: options.port }, executionPollMs: 50, - feishu: { appIdEnv: 'FIXTURE_FEISHU_APP', appSecretEnv: 'FIXTURE_FEISHU_SECRET', userOpenId: 'ou_fixture' }, + feishu: { appId: 'fixture-app', appSecret: 'fixture-secret', userOpenId: 'ou_fixture' }, })); const sdk = ` import { existsSync, writeFileSync } from 'node:fs'; @@ -141,7 +140,11 @@ async function runFixture(t: TestContext, options: { export class Client { constructor() { throw new Error('No task should call Feishu HTTP'); } } export class EventDispatcher { register(handlers) { if (!handlers['im.message.receive_v1']) throw new Error('No receiver'); return this; } } export class WSClient { - constructor(options) { this.options = options; } + constructor(options) { + if (options.appId !== 'fixture-app' || options.appSecret !== 'fixture-secret') throw new Error('JSON credentials were not used'); + if (process.env.PROVIDER_FIXTURE !== 'inherited-provider') throw new Error('Provider environment was lost'); + this.options = options; + } async start() { writeFileSync('feishu-started', 'yes'); ${options.startup === 'fail' ? "throw new Error('Fixture Feishu startup failed');" : ''} @@ -169,8 +172,7 @@ async function runFixture(t: TestContext, options: { if (specifier === '@larksuiteoapi/node-sdk') return { url: ${JSON.stringify(`data:text/javascript,${encodeURIComponent(sdk)}`)}, shortCircuit: true }; return nextResolve(specifier, context); } });`); - const env: NodeJS.ProcessEnv = { ...process.env, FIXTURE_FEISHU_APP: 'fixture-app', FIXTURE_FEISHU_SECRET: 'fixture-secret' }; - delete env.UNSET_MANUAL_ONLY_GITHUB; + const env: NodeJS.ProcessEnv = { ...process.env, FEISHU_APP_ID: 'conflicting-env-app', FEISHU_APP_SECRET: 'conflicting-env-secret', PROVIDER_FIXTURE: 'inherited-provider' }; const child = spawn(process.execPath, ['--import', resolve('node_modules/tsx/dist/loader.mjs'), '--import', hook, entry, options.command ?? 'run', '--config', 'config.json'], { cwd: root, env, timeout: 20_000, killSignal: 'SIGKILL', stdio: ['ignore', 'pipe', 'pipe'] }); let stdout = ''; let stderr = ''; child.stdout.setEncoding('utf8').on('data', chunk => { stdout += chunk; }); @@ -193,6 +195,7 @@ for (const signal of ['SIGINT', 'SIGTERM'] as const) test(`actual CLI run serves return fixture.stdout().trimEnd().endsWith('}'); }); const started = JSON.parse(fixture.stdout()); + assert.doesNotMatch(fixture.stdout() + fixture.stderr(), /fixture-app|fixture-secret/); assert.equal(started.event, 'started'); assert.equal(started.dataDir, join(await realpath(fixture.root), 'data')); assert.equal(started.tasks, 0); assert.equal(started.dashboard.url, fixture.url); assert.equal(started.dashboard.listenAddress, `0.0.0.0:${fixture.port}`); assert.ok(Array.isArray(started.dashboard.networkUrls)); diff --git a/test/credential-redaction.test.ts b/test/credential-redaction.test.ts new file mode 100644 index 0000000..47cb0e9 --- /dev/null +++ b/test/credential-redaction.test.ts @@ -0,0 +1,89 @@ +import assert from 'node:assert/strict'; +import { inspect } from 'node:util'; +import { mkdtemp, readFile, rm } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { test } from 'node:test'; +import { configSchema } from '../src/config.js'; +import { publicJson } from '../src/credentials.js'; +import { errorMessage, taskSchema } from '../src/domain.js'; +import { Store } from '../src/store.js'; +import { ProjectLedger } from '../src/project.js'; +import { startDashboard } from '../src/dashboard.js'; +import { ExecutionFixture } from './execution-fixture.js'; +import { testExecution } from './test-config.js'; + +const secrets = ['synthetic-redaction-app-id', 'synthetic-redaction-app-secret-"\\\n-end', 'synthetic-redaction-github-token']; +const payload = secrets.join(' | '); +const config = () => configSchema.parse({ execution: testExecution, github: { token: secrets[2] }, + feishu: { appId: secrets[0], appSecret: secrets[1], userOpenId: 'owner' } }); +function assertRedacted(text: string) { + for (const value of secrets) { + assert.equal(text.includes(value), false); + assert.equal(text.includes(JSON.stringify(value).slice(1, -1)), false); + assert.equal(text.includes(encodeURIComponent(value)), false); + } + assert.match(text, /REDACTED/); +} + +test('parsed credentials stay usable by clients and serialize safely in config exports and errors', () => { + const value = config(); + assert.equal(value.feishu.appId.require('feishu.appId'), secrets[0]); + assert.equal(value.feishu.appSecret.require('feishu.appSecret'), secrets[1]?.trim()); + assertRedacted(JSON.stringify(value)); + assertRedacted(JSON.stringify({ ...value.feishu })); + assertRedacted(inspect(value, { depth: 10 })); + assertRedacted(String(value.feishu.appSecret)); + assertRedacted(publicJson({ nested: payload, encoded: encodeURIComponent(secrets[0]!) })); + assertRedacted(errorMessage(new Error(payload))); +}); + +test('diagnostics, task snapshots, reports, dashboard detail and SSE do not disclose configured credentials', async t => { + config(); + const root = await mkdtemp(join(tmpdir(), 'redaction-store-')); + const store = new Store(root), source = new ExecutionFixture(); + let storeClosed = false; + const now = Date.now(); + const task = taskSchema.parse({ id: 'worker', sourceId: 'redaction-source', source: { kind: 'manual', message: { + id: 'redaction-message', chatId: 'chat', senderId: 'owner', text: payload, attachments: [], createdAt: now, chatType: 'group', + } }, purpose: 'execution', contextDirectory: root, resources: [], status: 'running', reason: payload, prompt: payload, + promptHash: 'hash', skillPath: null, chatId: 'chat', threadId: 'thread', turnId: 'turn', executionStatus: 'running', + lastHandledTurnId: null, requests: [], recovery: { attempts: 0, nextAttemptAt: null, lastError: null, interruptedTurnId: null, lastUserInputSeq: null }, createdAt: now, updatedAt: now }); + store.saveTask(task); + const ledger = new ProjectLedger(store); + ledger.create({ id: 'project', name: 'Project', directory: root, repositories: [], instructions: '' }, task.id); + store.receive(task.id, { id: 'report', chatId: 'chat', senderId: 'runtime:project', text: payload, attachments: [], createdAt: now, chatType: 'group' }, + 'source', { kind: 'agent', projectId: 'project', taskId: task.id, eventId: 'report:worker:result', category: 'result', routing: 'final' }); + store.setMeta('project-report-test', JSON.stringify({ text: payload })); + store.diagnostic('redaction', payload); + const outputId = store.enqueue(task.id, payload, 'redaction-output'); + store.markOutput(outputId, 'uncertain', payload); + assertRedacted(JSON.stringify(store.diagnostics())); + assertRedacted(JSON.stringify(store.task(task.id))); + assertRedacted(JSON.stringify(store.outputs(task.id))); + assertRedacted(store.meta('project-report-test') ?? ''); + const dashboard = await startDashboard({ execution: source.execution, dataDir: root, port: 0 }); + t.after(async () => { await dashboard.close(); if (!storeClosed) store.close(); await rm(root, { recursive: true, force: true }); }); + for (const path of ['/api/snapshot', '/api/projects', `/api/tasks/${task.id}`]) { + const response = await fetch(dashboard.url + path); + assert.equal(response.status, 200); assertRedacted(await response.text()); + } + assert.equal((await fetch(dashboard.url + '/api/config')).status, 404); + const before = source.execution.cursor; + source.publish(task.id, { threadId: 'thread', turnId: 'turn', itemId: 'error', kind: 'error', label: payload, text: payload, createdAt: now }); + const abort = new AbortController(); + const response = await fetch(dashboard.url + `/api/events?taskId=${task.id}&after=${before}`, { signal: abort.signal }); + const reader = response.body!.getReader(); + let stream = ''; + try { + while (!stream.includes('event: execution')) { + const item = await reader.read(); assert.equal(item.done, false); stream += new TextDecoder().decode(item.value); + } + assertRedacted(stream); + } finally { abort.abort(); await reader.cancel().catch(() => undefined); } + store.close(); storeClosed = true; + for (const name of ['tasks.sqlite', 'tasks.sqlite-wal']) { + const bytes = await readFile(join(root, name)).catch(() => Buffer.alloc(0)); + for (const secret of secrets) assert.equal(bytes.includes(Buffer.from(secret.trim())), false); + } +}); diff --git a/test/execution-config.test.ts b/test/execution-config.test.ts index 89a7e60..dd3869d 100644 --- a/test/execution-config.test.ts +++ b/test/execution-config.test.ts @@ -23,18 +23,20 @@ test('execution watcher applies valid changes and keeps the last valid config af const root = await mkdtemp(join(tmpdir(), 'agent-loop-execution-config-')); t.after(() => rm(root, { recursive: true, force: true })); const path = join(root, 'config.json'); - const configFile = (profile: typeof execution = execution) => JSON.stringify({ execution: profile, feishu: { userOpenId: 'owner' } }); + const configFile = (profile: typeof execution = execution, appSecret = 'synthetic-reload-original') => JSON.stringify({ execution: profile, feishu: { appId: 'synthetic-reload-app', appSecret, userOpenId: 'owner' } }); await writeFile(path, configFile()); const target = await loadConfig(path); const diagnostics: unknown[] = []; const watcher = new ExecutionConfigWatcher(path, target, error => diagnostics.push(error)); watcher.start(); t.after(() => watcher.close()); - await writeFile(path, configFile({ ...execution, coordinator: { ...execution.coordinator, model: 'coordinator-b' } })); + await writeFile(path, configFile({ ...execution, coordinator: { ...execution.coordinator, model: 'coordinator-b' } }, 'synthetic-reload-edited')); await waitFor(() => target.execution.coordinator.model === 'coordinator-b'); - await writeFile(path, '{ invalid json'); + assert.equal(target.feishu.appSecret.require('feishu.appSecret'), 'synthetic-reload-original'); + await writeFile(path, '{ invalid json synthetic-reload-new-secret'); await waitFor(() => diagnostics.length === 1); assert.equal(target.execution.coordinator.model, 'coordinator-b'); + assert.doesNotMatch(String(diagnostics[0]), /synthetic-reload-new-secret/); const invalid = configSchema.safeParse({ execution: { ...execution, worker: { ...execution.worker, backend: 'dsh' } }, feishu: { userOpenId: 'owner' } }); assert.equal(invalid.success, false); diff --git a/test/feishu-config-helpers.test.ts b/test/feishu-config-helpers.test.ts new file mode 100644 index 0000000..648b941 --- /dev/null +++ b/test/feishu-config-helpers.test.ts @@ -0,0 +1,83 @@ +import assert from 'node:assert/strict'; +import { spawn } from 'node:child_process'; +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join, resolve } from 'node:path'; +import { test } from 'node:test'; +import { testExecution } from './test-config.js'; + +const appId = 'synthetic-helper-app-id', appSecret = 'synthetic-helper-app-secret', token = 'synthetic-helper-github-token'; +const original = { dataDir: './relative-data', execution: testExecution, github: { token }, + feishu: { appId, appSecret, userOpenId: 'REPLACE_WITH_YOUR_OPEN_ID' } }; + +for (const mode of ['pair', 'verify'] as const) test(`${mode} helper uses JSON credentials and protects its output`, async t => { + const root = await mkdtemp(join(tmpdir(), `feishu-${mode}-`)); + t.after(() => rm(root, { recursive: true, force: true })); + const raw = mode === 'pair' ? original : { ...original, feishu: { ...original.feishu, userOpenId: 'paired-owner' } }; + await writeFile(join(root, 'config.json'), JSON.stringify(raw)); + await writeFile(join(root, '.env'), 'FEISHU_APP_ID=wrong-dotenv-id\nFEISHU_APP_SECRET=wrong-dotenv-secret\n'); + const sdk = ` + import { existsSync, readFileSync } from 'node:fs'; + export const Domain = { Feishu: 'fixture' }; + export const LoggerLevel = { error: 0 }; + export const defaultHttpInstance = { request() { throw new Error('Network is forbidden'); } }; + function check(options) { + if (options.appId !== ${JSON.stringify(appId)} || options.appSecret !== ${JSON.stringify(appSecret)}) throw new Error('JSON credentials not received'); + options.logger.error({ appId: options.appId, appSecret: options.appSecret }); + } + export class Client { + constructor(options) { + check(options); + ${mode === 'verify' ? `throw new Error('verify fixture reached ' + options.appSecret);` : ''} + this.im = { message: { create: async () => ({ code: 0 }) } }; + } + } + export class EventDispatcher { register(handlers) { this.handlers = handlers; return this; } } + export class WSClient { + constructor(options) { check(options); } + async start({ eventDispatcher }) { + this.timer = setInterval(async () => { + if (!existsSync('pair-command')) return; + clearInterval(this.timer); + await eventDispatcher.handlers['im.message.receive_v1']({ + sender: { sender_type: 'user', sender_id: { open_id: 'paired-owner' } }, + message: { message_type: 'text', content: JSON.stringify({ text: readFileSync('pair-command', 'utf8') }) } + }); + }, 10); + } + close() { clearInterval(this.timer); } + } + `; + const hook = join(root, 'sdk-hook.mjs'); + await writeFile(hook, `import { registerHooks } from 'node:module'; + registerHooks({ resolve(specifier, context, nextResolve) { + if (specifier === '@larksuiteoapi/node-sdk') return { url: ${JSON.stringify(`data:text/javascript,${encodeURIComponent(sdk)}`)}, shortCircuit: true }; + return nextResolve(specifier, context); + } });`); + const child = spawn(process.execPath, ['--import', resolve('node_modules/tsx/dist/loader.mjs'), '--import', hook, '--', resolve(`scripts/${mode}-feishu.ts`), '--config', 'config.json'], { + cwd: root, timeout: 10_000, env: { ...process.env, FEISHU_APP_ID: 'wrong-env-id', FEISHU_APP_SECRET: 'wrong-env-secret' }, stdio: ['ignore', 'pipe', 'pipe'], + }); + let stdout = '', stderr = '', sent = false; + child.stdout.setEncoding('utf8').on('data', (chunk: string) => { + stdout += chunk; + if (mode === 'pair' && !sent && stdout.includes('\n')) { + const first = JSON.parse(stdout.split('\n')[0]!); + sent = true; + void writeFile(join(root, 'pair-command'), first.pairingCommand); + } + }); + child.stderr.setEncoding('utf8').on('data', (chunk: string) => { stderr += chunk; }); + const exit = await new Promise((done, reject) => { child.once('error', reject); child.once('close', code => done(code)); }); + assert.equal(exit, mode === 'pair' ? 0 : 1, stderr); + for (const secret of [appId, appSecret, token]) assert.equal((stdout + stderr).includes(secret), false); + assert.match(stderr, /REDACTED/); + if (mode === 'pair') { + const records = stdout.trim().split('\n').map(line => JSON.parse(line)); + assert.equal(records[0].appId, undefined); assert.equal(records[1].paired, true); + const actual = JSON.parse(await readFile(join(root, 'config.json'), 'utf8')); + assert.deepEqual(actual, { ...original, feishu: { ...original.feishu, userOpenId: 'paired-owner' } }); + } else { + assert.match(stderr, /verify fixture reached/); + assert.deepEqual(JSON.parse(await readFile(join(root, 'config.json'), 'utf8')), raw); + } +}); diff --git a/test/single-config-cli.test.ts b/test/single-config-cli.test.ts new file mode 100644 index 0000000..6a84bb3 --- /dev/null +++ b/test/single-config-cli.test.ts @@ -0,0 +1,94 @@ +import assert from 'node:assert/strict'; +import { execFile } from 'node:child_process'; +import { mkdtemp, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join, resolve } from 'node:path'; +import { test } from 'node:test'; +import { promisify } from 'node:util'; +import { testExecution } from './test-config.js'; + +const execute = promisify(execFile); +const entry = resolve('src/cli.ts'); +const loader = resolve('node_modules/tsx/dist/loader.mjs'); +const appId = 'synthetic-single-file-app'; +const appSecret = 'synthetic-single-file-secret'; +const token = 'synthetic-single-file-token'; +const config = { execution: testExecution, github: { token }, feishu: { appId, appSecret, userOpenId: 'owner' } }; + +async function invoke(root: string, command: string, args: string[] = []) { + try { + const result = await execute(process.execPath, ['--import', loader, '--', entry, command, '--config', 'config.json', ...args], { + cwd: root, timeout: 10_000, env: { ...process.env, FEISHU_APP_ID: 'wrong-env-app', FEISHU_APP_SECRET: 'wrong-env-secret', GITHUB_TOKEN: 'wrong-env-token' }, + }); + return { code: 0, ...result }; + } catch (error) { + assert.ok(error instanceof Error && 'code' in error && 'stdout' in error && 'stderr' in error); + assert.equal(typeof error.stdout, 'string'); assert.equal(typeof error.stderr, 'string'); + return { code: error.code, stdout: String(error.stdout), stderr: String(error.stderr) }; + } +} + +function noCredentials(output: string) { + for (const secret of [appId, appSecret, token]) assert.equal(output.includes(secret), false); +} + +test('one private JSON file drives doctor and ignores dotenv and conflicting credential environment', async t => { + const root = await mkdtemp(join(tmpdir(), 'single-config-')); + t.after(() => rm(root, { recursive: true, force: true })); + await writeFile(join(root, 'config.json'), JSON.stringify(config)); + const first = await invoke(root, 'doctor'); + assert.equal(first.code, 0, first.stderr); assert.equal(JSON.parse(first.stdout).ready, true); + noCredentials(first.stdout + first.stderr); + await writeFile(join(root, '.env'), 'NODE_OPTIONS=--invalid-dotenv-option\nFEISHU_APP_ID=dotenv-app\nFEISHU_APP_SECRET=dotenv-secret\nGITHUB_TOKEN=dotenv-token\n'); + const second = await invoke(root, 'doctor'); + assert.equal(second.code, 0, second.stderr); assert.deepEqual(JSON.parse(second.stdout), JSON.parse(first.stdout)); + await writeFile(join(root, 'config.json'), JSON.stringify({ ...config, feishu: { userOpenId: 'owner' } })); + const missing = await invoke(root, 'doctor'); + assert.equal(missing.code, 1); assert.equal(JSON.parse(missing.stdout).ready, false); + assert.deepEqual(JSON.parse(missing.stdout).credentials, [ + { name: 'feishu.appId', present: false }, { name: 'feishu.appSecret', present: false }, + ]); + const run = await invoke(root, 'run'); + assert.equal(run.code, 1); assert.match(run.stderr, /feishu.appId/); noCredentials(run.stderr); +}); + +test('GitHub token remains conditional and missing JSON token never falls back to the environment', async t => { + const root = await mkdtemp(join(tmpdir(), 'conditional-token-')); + t.after(() => rm(root, { recursive: true, force: true })); + for (const empty of [undefined, '', ' ', 'REPLACE_WITH_GITHUB_TOKEN']) { + const raw = { ...config, github: { token: empty } }; + await writeFile(join(root, 'config.json'), JSON.stringify(raw)); + assert.equal((await invoke(root, 'collect')).code, 0); + await writeFile(join(root, 'config.json'), JSON.stringify({ ...raw, repositories: [{ name: 'fixture/issues', criteria: {} }] })); + for (const command of ['run', 'collect']) { + const result = await invoke(root, command); + assert.equal(result.code, 1); assert.match(result.stderr, /github.token/); noCredentials(result.stdout + result.stderr); + } + const doctor = await invoke(root, 'doctor'); + assert.equal(doctor.code, 1); assert.equal(JSON.parse(doctor.stdout).credentials[0].present, false); + } +}); + +test('legacy options, credential selectors, and invalid JSON fail without disclosing credentials', async t => { + const root = await mkdtemp(join(tmpdir(), 'invalid-config-')); + t.after(() => rm(root, { recursive: true, force: true })); + await writeFile(join(root, 'config.json'), JSON.stringify(config)); + const legacyOption = await invoke(root, 'doctor', ['--env-file', 'removed.env']); + assert.equal(legacyOption.code, 1, legacyOption.stderr); assert.match(legacyOption.stderr, /env-file/); + await assert.rejects(execute('npm', ['start', '--', 'doctor', '--config', join(root, 'config.json'), '--env-file', 'removed.env']), error => { + assert.ok(error instanceof Error && 'stderr' in error); + assert.match(String(error.stderr), /Unknown option '--env-file'/); return true; + }); + const invalid = [ + JSON.stringify(config).slice(0, -1) + `, "invalid": "${appSecret}" bad }`, + JSON.stringify({ ...config, feishu: { ...config.feishu, appIdEnv: appId, appSecretEnv: appSecret } }), + JSON.stringify({ ...config, github: { ...config.github, tokenEnv: token } }), + JSON.stringify({ ...config, execution: { ...testExecution, worker: { ...testExecution.worker, backend: appSecret } } }), + JSON.stringify({ ...config, [appSecret]: true }), + ]; + for (const raw of invalid) { + await writeFile(join(root, 'config.json'), raw); + const result = await invoke(root, 'doctor'); + assert.equal(result.code, 1); assert.match(result.stderr, /Invalid configuration/); noCredentials(result.stdout + result.stderr); + } +}); diff --git a/test/workspace.test.ts b/test/workspace.test.ts index c25da29..5875c58 100644 --- a/test/workspace.test.ts +++ b/test/workspace.test.ts @@ -117,12 +117,12 @@ test('configuration resolves paths relative to its file and requires actual prom assert.equal(config.dataDir, join(root, 'data')); assert.equal(config.engineering!.promptPath, join(root, 'prompt.md')); assert.equal(config.engineering!.skillPath, join(root, 'SKILL.md')); - assert.equal(config.github.tokenEnv, 'GITHUB_TOKEN'); + assert.equal(config.github.token.configured, false); assert.equal(config.pollIntervalMs, 30_000); assert.equal(config.executionPollMs, 2_000); assert.equal(config.execution.coordinator.model, 'test-coordinator'); assert.throws(() => configSchema.parse({ ...raw, codex: { model: 'legacy-model' } })); - assert.throws(() => configSchema.parse({ ...raw, github: { token: 'never-store-secrets' } })); + assert.equal(configSchema.parse({ ...raw, github: { token: 'synthetic-direct-token' } }).github.token.require('github.token'), 'synthetic-direct-token'); assert.throws(() => configSchema.parse({ ...raw, github: { tokenEnv: 'github-token-value' } })); assert.throws(() => configSchema.parse({ ...raw, bug: raw.engineering }), /Unrecognized key/); await rm(join(root, 'SKILL.md')); From db3699fd2c8c8630b87a9cb86b73591dde6ac8fa Mon Sep 17 00:00:00 2001 From: jinmingtao Date: Fri, 20 Nov 2026 15:55:00 +0800 Subject: [PATCH 2/2] fix(config): redact progress credential sinks safely --- src/adapters/feishu-document.ts | 11 +++--- src/adapters/feishu.ts | 6 ++-- src/config.ts | 2 +- src/credentials.ts | 49 ++++++++++++++++++++++++--- src/domain.ts | 2 +- src/feishu-document-progress.ts | 5 +-- src/feishu-progress.ts | 10 +++--- src/store.ts | 18 +++++----- test/credential-redaction.test.ts | 48 +++++++++++++++++++++++++- test/feishu-document-adapter.test.ts | 16 +++++++++ test/feishu-document-progress.test.ts | 18 ++++++++++ test/feishu-progress.test.ts | 33 ++++++++++++++++-- 12 files changed, 187 insertions(+), 31 deletions(-) diff --git a/src/adapters/feishu-document.ts b/src/adapters/feishu-document.ts index 62913c2..2daa9b8 100644 --- a/src/adapters/feishu-document.ts +++ b/src/adapters/feishu-document.ts @@ -2,6 +2,7 @@ import { setTimeout as delay } from 'node:timers/promises'; import type { Client } from '@larksuiteoapi/node-sdk'; import { z } from 'zod'; import { documentTextElements } from '../feishu-document-text.js'; +import { redactCredentials } from '../credentials.js'; import { DocumentRequestRejected, TaskGroupUnavailable, type FeishuDocumentProgressPort, type ProgressWriteGuard } from '../ports.js'; export type FeishuDocumentApi = { @@ -74,13 +75,14 @@ export class FeishuDocumentAdapter implements FeishuDocumentProgressPort { } async createBlock(input: Parameters[0], guard?: ProgressWriteGuard): Promise { const index = z.number().int().min(-1).parse(input.index ?? -1); - const elements = documentTextElements(input.text); + const text = redactCredentials(input.text, { embedded: true }); + const elements = documentTextElements(text); const data = await this.call(() => this.api().children.create({ path: { document_id: input.documentId, block_id: input.parentBlockId ?? input.documentId }, params: { document_revision_id: -1, client_token: input.clientToken }, data: { children: [{ block_type: 2, text: { elements } }], index } }), guard); const result = z.object({ children: z.array(textBlock).length(1), client_token: z.literal(input.clientToken) }).parse(data); const block = result.children[0]; - if (!block || blockText(block) !== input.text) throw new Error('Created document block does not match intent'); + if (!block || blockText(block) !== text) throw new Error('Created document block does not match intent'); return block.block_id; } async findBlock(input: Parameters[0]): Promise { @@ -135,11 +137,12 @@ export class FeishuDocumentAdapter implements FeishuDocumentProgressPort { return blockText(z.object({ block: z.unknown() }).parse(data).block, input.blockId); } async replaceContent(input: Parameters[0], guard?: ProgressWriteGuard): Promise { - const elements = documentTextElements(input.text); + const text = redactCredentials(input.text, { embedded: true }); + const elements = documentTextElements(text); const data = await this.call(() => this.api().block.patch({ path: { document_id: input.documentId, block_id: input.blockId }, params: { document_revision_id: -1, client_token: input.clientToken }, data: { update_text_elements: { elements } } }), guard); const result = z.object({ block: textBlock, client_token: z.literal(input.clientToken) }).parse(data); - if (blockText(result.block, input.blockId) !== input.text) throw new Error('Document write response does not match intent'); + if (blockText(result.block, input.blockId) !== text) throw new Error('Document write response does not match intent'); } async grantGroupRead(input: Parameters[0], guard?: ProgressWriteGuard): Promise { const data = await this.call(() => this.api().members.create({ path: { token: input.documentId }, params: { type: 'docx', need_notification: false }, diff --git a/src/adapters/feishu.ts b/src/adapters/feishu.ts index 9b6b139..716f77c 100644 --- a/src/adapters/feishu.ts +++ b/src/adapters/feishu.ts @@ -6,7 +6,7 @@ import { pipeline } from 'node:stream/promises'; import { setTimeout as delay } from 'node:timers/promises'; import { Client, WSClient, EventDispatcher, Domain, LoggerLevel, defaultHttpInstance, type HttpInstance, type HttpRequestOptions } from '@larksuiteoapi/node-sdk'; import { z } from 'zod'; -import { credentialSafeLogger } from '../credentials.js'; +import { credentialSafeLogger, publicJson, redactCredentials } from '../credentials.js'; import { UncertainOperationError, errorMessage, projectDecisionActionSchema, type ApprovalAction, type Attachment, type IncomingMessage, type PendingRequest, type ProjectDecisionAction, type ReplyTarget } from '../domain.js'; import { FeishuApiError, GroupCreationRejected, TaskGroupUnavailable, type FeishuCardDelivery, type FeishuDecisionCardInput, type FeishuPort, type FeishuProgressPort, type FeishuReconnectEvent, type ProgressWriteGuard } from '../ports.js'; import { FeishuDocumentAdapter, type FeishuDocumentApi } from './feishu-document.js'; @@ -284,7 +284,7 @@ export class FeishuAdapter implements FeishuPort, FeishuProgressPort { ...(input.dashboardUrl ? [{ tag: 'button', text: { tag: 'plain_text', content: '查看完整日志' }, type: 'default', multi_url: { url: input.dashboardUrl, pc_url: '', android_url: '', ios_url: '' } }] : []), ] } }; - const data = JSON.stringify(card); + const data = publicJson(card); if (Buffer.byteLength(data) >= 30_000) throw new Error('Progress card exceeds the 30KB payload budget'); await this.paceProgressRequest(); guard?.(); @@ -332,7 +332,7 @@ export class FeishuAdapter implements FeishuPort, FeishuProgressPort { await this.paceProgressRequest(); guard?.(); checkWrite(await this.cardkit.cardElement.content({ path: { card_id: input.cardId, element_id: 'progress' }, - data: { content: input.content, sequence: input.sequence, uuid: input.uuid } })); + data: { content: redactCredentials(input.content, { embedded: true }), sequence: input.sequence, uuid: input.uuid } })); } async setProgressStreaming(input: Parameters[0], guard?: ProgressWriteGuard): Promise { await this.paceProgressRequest(); diff --git a/src/config.ts b/src/config.ts index 8518e78..e5d4410 100644 --- a/src/config.ts +++ b/src/config.ts @@ -120,7 +120,7 @@ export async function loadConfig(path: string): Promise { } return config; } catch (error) { - throw new Error(redactCredentials(error instanceof Error ? error.message : String(error))); + throw new Error(redactCredentials(error instanceof Error ? error.message : String(error), { embedded: true })); } } diff --git a/src/credentials.ts b/src/credentials.ts index 8a4fde4..eb3e44a 100644 --- a/src/credentials.ts +++ b/src/credentials.ts @@ -2,13 +2,31 @@ import { inspect } from 'node:util'; const credentialValues = new Set(); const redacted = '[REDACTED]'; +const word = (value: string | undefined): boolean => value !== undefined && /[\p{L}\p{N}_]/u.test(value); + +function encodedVariants(value: string): string[] { + const unicode = Buffer.from(value, 'utf8').toString('utf8'); + const component = encodeURIComponent(unicode); + const strictComponent = component.replace(/[!'()*~]/g, character => `%${character.codePointAt(0)!.toString(16).toUpperCase()}`); + const form = new URLSearchParams({ value }).toString().slice('value='.length); + const variants = [ + value, + JSON.stringify(value).slice(1, -1), + encodeURI(unicode), + component, + strictComponent, + form, + form.replaceAll('+', '%20'), + ]; + return variants.flatMap(variant => [variant, variant.replace(/%[0-9A-F]{2}/g, code => code.toLowerCase())]); +} export class ConfigCredential { readonly #value: string; constructor(value: string) { this.#value = value; if (this.configured) { - for (const variant of [value, JSON.stringify(value).slice(1, -1), new URLSearchParams({ value }).toString().slice(6).replaceAll('+', '%20')]) credentialValues.add(variant); + for (const variant of encodedVariants(value)) if (variant) credentialValues.add(variant); } } get rawValue(): string { return this.#value; } @@ -22,13 +40,36 @@ export class ConfigCredential { [inspect.custom](): string { return redacted; } } -export function redactCredentials(text: string): string { - for (const value of [...credentialValues].sort((a, b) => b.length - a.length)) text = text.replaceAll(value, redacted); +export function redactCredentials(text: string, options: { embedded?: boolean } = {}): string { + for (const value of [...credentialValues].sort((a, b) => b.length - a.length)) { + if (text === value) { text = redacted; continue; } + if (!options.embedded || !text.includes(value)) continue; + const first = [...value][0], last = [...value].at(-1); + let cursor = 0; + let output = ''; + for (;;) { + const index = text.indexOf(value, cursor); + if (index < 0) { output += text.slice(cursor); break; } + const before = [...text.slice(0, index)].at(-1); + const after = [...text.slice(index + value.length)][0]; + if ((!word(first) || !word(before)) && (!word(last) || !word(after))) { + output += text.slice(cursor, index) + redacted; + cursor = index + value.length; + } else { + output += text.slice(cursor, index + value.length); + cursor = index + value.length; + } + } + text = output; + } return text; } export function publicJson(value: unknown, space?: number): string { - return JSON.stringify(value, (_key, item: unknown) => typeof item === 'string' ? redactCredentials(item) : item, space); + return JSON.stringify(value, undefined, space).replace(/"(?:\\.|[^"\\])*"/g, token => { + const text: unknown = JSON.parse(token); + return typeof text === 'string' ? JSON.stringify(redactCredentials(text, { embedded: true })) : token; + }); } const log = (...messages: unknown[]): void => { diff --git a/src/domain.ts b/src/domain.ts index 188ff0a..f10b7bc 100644 --- a/src/domain.ts +++ b/src/domain.ts @@ -259,4 +259,4 @@ export type FeishuCardRecord = z.infer; export class UncertainOperationError extends Error {} export class TurnChangedError extends Error {} export class RejectedInputError extends Error {} -export function errorMessage(error: unknown): string { return redactCredentials(error instanceof Error ? error.message : String(error)); } +export function errorMessage(error: unknown): string { return redactCredentials(error instanceof Error ? error.message : String(error), { embedded: true }); } diff --git a/src/feishu-document-progress.ts b/src/feishu-document-progress.ts index 9b3ab71..227ac01 100644 --- a/src/feishu-document-progress.ts +++ b/src/feishu-document-progress.ts @@ -10,6 +10,7 @@ import { documentChunks } from './feishu-document-text.js'; import { DocumentRequestRejected, TaskGroupBindingChanged, TaskGroupUnavailable, type FeishuDocumentProgressPort } from './ports.js'; import { assertGroupBinding, retireTaskGroup } from './task-groups.js'; import type { Store } from './store.js'; +import { redactCredentials } from './credentials.js'; const id = z.string().min(1); const writeSchema = z.strictObject({ clientToken: id, text: z.string(), sent: z.boolean() }); @@ -45,7 +46,7 @@ const discoverySchema = z.strictObject({ excludedTurns: z.array(z.string()) }); const discoveryKey = 'feishu-document-progress:discovery:v2'; const turnKey = (taskId: string, turnId: string) => JSON.stringify([taskId, turnId]); const emptyView = () => progressViewSchema.parse({ status: 'running', cursor: 0, lastExecutionEventAt: null, assistant: [], tool: null }); -const content = (record: DocumentPublication) => record.text; +const content = (record: DocumentPublication) => redactCredentials(record.text, { embedded: true }); export class FeishuDocumentProgressPublisher { private readonly inFlight = new Map>(); private readonly publications = new Map(); @@ -213,7 +214,7 @@ export class FeishuDocumentProgressPublisher { record.threadId = task.threadId; record.turnId = task.turnId; record.sourceFrontier = sourceFrontier(observation.entries); const conversation = projectDocumentConversation(task, store.inputs(task.id), observation.entries); record.view = { status: conversation.status, cursor: conversation.cursor, lastExecutionEventAt: conversation.lastExecutionEventAt, assistant: [], tool: null, notice: null }; - record.text = documentConversationText(conversation, this.maxLines); + record.text = redactCredentials(documentConversationText(conversation, this.maxLines), { embedded: true }); record.projectionRevision = hash(record.text); this.save(record); this.observed.set(task.id, signature); this.historyRetries.succeeded(task.id); diff --git a/src/feishu-progress.ts b/src/feishu-progress.ts index 41cf55c..f12a901 100644 --- a/src/feishu-progress.ts +++ b/src/feishu-progress.ts @@ -6,6 +6,7 @@ import { type Task } from './domain.js'; import { TaskGroupBindingChanged, TaskGroupUnavailable, type FeishuProgressPort } from './ports.js'; import { assertGroupBinding, retireTaskGroup } from './task-groups.js'; import type { Store } from './store.js'; +import { redactCredentials } from './credentials.js'; import { coversSource, sourceFrontier, sourceFrontierSchema, HistoryRetries, finished, progressContent, progressViewSchema, projectProgress, taskProgressStatus } from './feishu-progress-view.js'; const entityFields = { cardId: z.string().min(1), createdAt: z.number() }; @@ -33,6 +34,7 @@ type ProgressPublication = z.infer; const discoverySchema = z.object({ snapshots: z.record(z.string(), z.string()), excludedTurns: z.array(z.string()) }); const discoveryKey = 'feishu-progress:discovery:v2'; const hash = (text: string) => createHash('sha256').update(text).digest('hex'); +const safeContent = (view: ProgressPublication['view']) => redactCredentials(progressContent(view), { embedded: true }); const key = (record: Pick) => JSON.stringify([record.taskId, record.turnId]); const renewalMs = 8 * 60_000; const entityLifetimeMs = 14 * 24 * 60 * 60_000; @@ -197,7 +199,7 @@ export class FeishuProgressPublisher { const view = progressViewSchema.parse({ status: 'running', cursor: 0, lastExecutionEventAt: null, assistant: [], tool: null }); for (const event of entries) projectProgress(view, event); if (turnId === task.turnId && (!finished(view.status) || taskProgressStatus(task) !== 'running')) view.status = taskProgressStatus(task); - record.view = view; record.sourceFrontier = sourceFrontier(entries); record.projectionHash = hash(progressContent(view)); record.terminal = finished(view.status); hydrated++; + record.view = view; record.sourceFrontier = sourceFrontier(entries); record.projectionHash = hash(safeContent(view)); record.terminal = finished(view.status); hydrated++; this.hydrated.add(key(record)); this.save(record); } @@ -221,7 +223,7 @@ export class FeishuProgressPublisher { case 'uncreated': case 'entity': case 'sending': return true; case 'closed': case 'disabled': return false; case 'published': return delivery.pending !== null || delivery.streamingAt === null || this.now() - delivery.streamingAt >= renewalMs - || delivery.contentHash !== hash(progressContent(record.view)) || finished(record.view.status); + || delivery.contentHash !== hash(safeContent(record.view)) || finished(record.view.status); default: { const exhaustive: never = delivery; return exhaustive; } } } @@ -254,7 +256,7 @@ export class FeishuProgressPublisher { if (delivery.kind === 'uncreated') { // An unacknowledged entity is unsent. Retrying creation can only leave an orphan. const createdAt = this.now(); - const cardId = await this.transport.createProgressCard({ content: progressContent(record.view), dashboardUrl: this.options.dashboardUrl }, guard); + const cardId = await this.transport.createProgressCard({ content: safeContent(record.view), dashboardUrl: this.options.dashboardUrl }, guard); record.delivery = { kind: 'entity', cardId, createdAt }; } else if (delivery.kind === 'entity' || delivery.kind === 'sending') { const firstAttemptAt = delivery.kind === 'sending' ? delivery.firstAttemptAt : this.now(); @@ -274,7 +276,7 @@ export class FeishuProgressPublisher { } const sequence = ++record.sequence; const uuid = hash(`${record.uuid}:${sequence}`).slice(0, 32); - const content = progressContent(record.view); + const content = safeContent(record.view); const operation: z.infer = delivery.streamingAt === null || this.now() - delivery.streamingAt >= renewalMs ? { kind: 'settings', sequence, uuid, enabled: true } : delivery.contentHash !== hash(content) ? { kind: 'content', sequence, uuid, contentHash: hash(content) } diff --git a/src/store.ts b/src/store.ts index 81d87b5..2997dc5 100644 --- a/src/store.ts +++ b/src/store.ts @@ -211,10 +211,10 @@ export class Store { if (!input) throw new Error('Unknown inbox input'); this.prepare('UPDATE inbox SET payload=? WHERE id=?').run(publicJson({ message: input.message, provenance: input.provenance, requestId }), id); } - markInput(id: string, status: Inbox['status'], error: string | null = null): void { this.prepare('UPDATE inbox SET status=?,error=? WHERE id=?').run(status, error === null ? null : redactCredentials(error), id); } + markInput(id: string, status: Inbox['status'], error: string | null = null): void { this.prepare('UPDATE inbox SET status=?,error=? WHERE id=?').run(status, error === null ? null : redactCredentials(error, { embedded: true }), id); } enqueue(taskId: string, text: string, key: string): string { const id = createHash('sha256').update(`${taskId}:${key}`).digest('hex').slice(0, 32); - this.prepare("INSERT OR IGNORE INTO outbox VALUES(?,?,?,'pending',NULL)").run(id, taskId, redactCredentials(text)); + this.prepare("INSERT OR IGNORE INTO outbox VALUES(?,?,?,'pending',NULL)").run(id, taskId, redactCredentials(text, { embedded: true })); return id; } hasOutput(taskId: string, key: string): boolean { @@ -230,15 +230,15 @@ export class Store { if (!statuses.length) return false; return this.prepare(`SELECT 1 FROM outbox WHERE task_id=? AND status IN (${statuses.map(() => '?').join(',')}) LIMIT 1`).get(taskId, ...statuses) !== undefined; } - markOutput(id: string, status: Outbox['status'], error: string | null = null): void { this.prepare('UPDATE outbox SET status=?,error=? WHERE id=?').run(status, error === null ? null : redactCredentials(error), id); } + markOutput(id: string, status: Outbox['status'], error: string | null = null): void { this.prepare('UPDATE outbox SET status=?,error=? WHERE id=?').run(status, error === null ? null : redactCredentials(error, { embedded: true }), id); } operation(id: string): Operation | null { const row = this.prepare('SELECT * FROM operations WHERE id=?').get(id); return row ? operationSchema.parse(row) : null; } beginOperation(id: string, taskId: string, kind: string): void { this.prepare("INSERT INTO operations VALUES(?,?,?,'attempting',NULL,NULL)").run(id, taskId, kind); } retryRejectedOperation(id: string): void { const result = this.prepare("UPDATE operations SET status='attempting',result=NULL,error=NULL WHERE id=? AND status='uncertain' AND error='rejected'").run(id); if (result.changes !== 1) throw new Error('Operation is not a rejected attempt'); } - finishOperation(id: string, result: string): void { this.prepare("UPDATE operations SET status='confirmed',result=?,error=NULL WHERE id=?").run(redactCredentials(result), id); } - uncertainOperation(id: string, error: string): void { this.prepare("UPDATE operations SET status='uncertain',error=? WHERE id=?").run(redactCredentials(error), id); } + finishOperation(id: string, result: string): void { this.prepare("UPDATE operations SET status='confirmed',result=?,error=NULL WHERE id=?").run(redactCredentials(result, { embedded: true }), id); } + uncertainOperation(id: string, error: string): void { this.prepare("UPDATE operations SET status='uncertain',error=? WHERE id=?").run(redactCredentials(error, { embedded: true }), id); } queueApproval(action: ApprovalAction): boolean { const id = this.approvalKey(action); return this.prepare("INSERT OR IGNORE INTO approval_actions VALUES(?,?,?,'pending',NULL)").run(id, action.taskId, publicJson(action)).changes > 0; @@ -251,7 +251,7 @@ export class Store { }); } markApproval(id: string, status: 'processing' | 'confirmed' | 'uncertain' | 'rejected', error: string | null = null): void { - this.prepare('UPDATE approval_actions SET status=?,error=? WHERE id=?').run(status, error === null ? null : redactCredentials(error), id); + this.prepare('UPDATE approval_actions SET status=?,error=? WHERE id=?').run(status, error === null ? null : redactCredentials(error, { embedded: true }), id); } saveFeishuCard(record: FeishuCardRecord): void { const card = feishuCardRecordSchema.parse(record); @@ -280,8 +280,8 @@ export class Store { return next; } diagnostic(scope: string, message: string): void { - scope = redactCredentials(scope); - message = redactCredentials(message); + scope = redactCredentials(scope, { embedded: true }); + message = redactCredentials(message, { embedded: true }); const createdAt = Date.now(); const bounded = message.length <= diagnosticMessageLimit ? message : message.slice(0, diagnosticMessageLimit) + '\n…'; const fingerprint = createHash('sha256').update(`${scope}\u0000${bounded}`).digest('hex'); @@ -320,7 +320,7 @@ export class Store { return sequenceRow.parse(this.prepare('SELECT COALESCE(MAX(seq),0) AS seq FROM inbox WHERE task_id=?').get(taskId)).seq; } setMeta(key: string, value: string): void { - const result = this.prepare('INSERT INTO meta VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value WHERE meta.value IS NOT excluded.value').run(key, redactCredentials(value)); + const result = this.prepare('INSERT INTO meta VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value WHERE meta.value IS NOT excluded.value').run(key, redactCredentials(value, { embedded: true })); if (result.changes > 0) this.metaRevisions.delete(key); } deleteMeta(key: string): void { diff --git a/test/credential-redaction.test.ts b/test/credential-redaction.test.ts index 47cb0e9..88c8492 100644 --- a/test/credential-redaction.test.ts +++ b/test/credential-redaction.test.ts @@ -5,7 +5,7 @@ import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { test } from 'node:test'; import { configSchema } from '../src/config.js'; -import { publicJson } from '../src/credentials.js'; +import { ConfigCredential, credentialSafeLogger, publicJson, redactCredentials } from '../src/credentials.js'; import { errorMessage, taskSchema } from '../src/domain.js'; import { Store } from '../src/store.js'; import { ProjectLedger } from '../src/project.js'; @@ -38,6 +38,52 @@ test('parsed credentials stay usable by clients and serialize safely in config e assertRedacted(errorMessage(new Error(payload))); }); +test('URI and form encodings and object keys are redacted in storage and SDK logs', async t => { + const secret = 'synthetic?a~b +/文!()'; + new ConfigCredential(secret); + const variants = [secret, encodeURI(secret), encodeURIComponent(secret), new URLSearchParams({ secret }).toString().slice(7)]; + variants.push(...variants.map(value => value.replace(/%[0-9A-F]{2}/g, code => code.toLowerCase()))); + const root = await mkdtemp(join(tmpdir(), 'encoded-redaction-')); + const store = new Store(root); + t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); + const logging = t.mock.method(process.stderr, 'write', () => true); + for (const value of variants) { + assert.equal(redactCredentials(value), '[REDACTED]'); + const json = publicJson({ [value]: { text: `credential=${value}` } }); + assert.deepEqual(JSON.parse(json), { '[REDACTED]': { text: 'credential=[REDACTED]' } }); + store.diagnostic('encoding', value); + store.setMeta('encoded-report', JSON.stringify({ [value]: value })); + assert.deepEqual(JSON.parse(store.meta('encoded-report')!), { '[REDACTED]': '[REDACTED]' }); + credentialSafeLogger.error({ [value]: value }); + } + assert.ok(store.diagnostics().every(row => row.message === '[REDACTED]')); + for (const call of logging.mock.calls) { + const text = String(call.arguments[0]); + for (const value of variants) assert.equal(text.includes(value), false); + assert.match(text, /REDACTED/); + } +}); + +test('credential substrings preserve ordinary persisted words and JSON structure', async t => { + new ConfigCredential('alpha'); + assert.equal(redactCredentials('prefix alpha suffix'), 'prefix alpha suffix'); + assert.equal(redactCredentials('alphabet'), 'alphabet'); + assert.deepEqual(JSON.parse(publicJson({ alphabet: 'alphabet alphanumeric alpha_suffix', exact: 'alpha', sentence: 'token=alpha' })), { + alphabet: 'alphabet alphanumeric alpha_suffix', exact: '[REDACTED]', sentence: 'token=[REDACTED]', + }); + const root = await mkdtemp(join(tmpdir(), 'collision-redaction-')); + const store = new Store(root); + t.after(async () => { store.close(); await rm(root, { recursive: true, force: true }); }); + store.setMeta('normal-state', JSON.stringify({ alphabet: 'alphabet' })); + store.diagnostic('alphabet', 'alphabet'); + assert.equal(store.meta('normal-state'), '{"alphabet":"alphabet"}'); + assert.equal(store.diagnostics()[0]?.message, 'alphabet'); + assert.equal(store.diagnostics()[0]?.scope, 'alphabet'); + const shared = ['alphabet']; + const date = new Date('2026-11-20T00:00:00Z'); + assert.equal(publicJson({ shared, again: shared, date }), JSON.stringify({ shared, again: shared, date })); +}); + test('diagnostics, task snapshots, reports, dashboard detail and SSE do not disclose configured credentials', async t => { config(); const root = await mkdtemp(join(tmpdir(), 'redaction-store-')); diff --git a/test/feishu-document-adapter.test.ts b/test/feishu-document-adapter.test.ts index 65c3e31..7cfd9e7 100644 --- a/test/feishu-document-adapter.test.ts +++ b/test/feishu-document-adapter.test.ts @@ -3,6 +3,7 @@ import { test } from 'node:test'; import { FeishuDocumentAdapter, type FeishuDocumentApi } from '../src/adapters/feishu-document.js'; import { documentChunks, documentTextElements } from '../src/feishu-document-text.js'; import { DocumentRequestRejected, TaskGroupBindingChanged, type FeishuDocumentProgressPort, type ProgressWriteGuard } from '../src/ports.js'; +import { ConfigCredential } from '../src/credentials.js'; const success = (data: unknown) => ({ code: 0, data }); const block = (text: string, blockId = 'block') => ({ block_id: blockId, block_type: 2, text: { elements: [{ text_run: { content: text } }] } }); @@ -33,6 +34,21 @@ const guardedWrites: { name: string; run(port: FeishuDocumentProgressPort, guard { name: 'grant', run: (port, guard) => port.grantGroupRead({ documentId: 'doc', chatId: 'group' }, guard) }, { name: 'tab', run: (port, guard) => port.createTab({ chatId: 'group', documentUrl: 'https://tenant.feishu.cn/docx/doc' }, guard) }, ]; + +test('document adapter sends redacted creation and replacement text and verifies the sent intent', async () => { + const secret = 'synthetic-document-adapter-secret?value~end'; + new ConfigCredential(secret); + const f = fixture(); + f.replies.set('block', success({ children: [block('credential=[REDACTED]')], client_token: 'create' })); + await f.adapter.createBlock({ documentId: 'doc', text: `credential=${secret}`, clientToken: 'create' }); + f.replies.set('write', success({ block: block('updated=[REDACTED]'), client_token: 'write' })); + await f.adapter.replaceContent({ documentId: 'doc', blockId: 'block', text: `updated=${encodeURIComponent(secret)}`, clientToken: 'write' }); + const payloads = JSON.stringify(f.calls); + assert.equal(payloads.includes(secret), false); + assert.equal(payloads.includes(encodeURIComponent(secret)), false); + assert.match(payloads, /credential=\[REDACTED\]/); + assert.match(payloads, /updated=\[REDACTED\]/); +}); for (const operation of guardedWrites) test(`retiring a binding before SDK dispatch cancels queued document ${operation.name}`, async () => { const f = fixture(); f.replies.set('read', success({ block: block('Original') })); diff --git a/test/feishu-document-progress.test.ts b/test/feishu-document-progress.test.ts index 8d38c2b..e1fc7e5 100644 --- a/test/feishu-document-progress.test.ts +++ b/test/feishu-document-progress.test.ts @@ -112,6 +112,24 @@ test('document config requires an explicit HTTPS origin only when enabled', () = } }); +test('document publisher redacts execution credentials before chunking, persistence and retries', async t => { + const secret = 'synthetic-document-progress-secret?value~end'; + configSchema.parse({ execution: testExecution, feishu: { userOpenId: 'owner', appSecret: secret } }); + const f = await fixture(t); + f.store.saveTask(task()); + f.event('agent_message', `First credential=${secret}`); + await f.pump(6); + f.event('agent_message', `Updated credential=${encodeURIComponent(secret)}`); + f.transport.fail = { kind: 'write', when: 'after', error: new Error('lost acknowledgement') }; + await f.pump(8); + const payloads = JSON.stringify(f.transport.calls); + assert.equal(payloads.includes(secret), false); + assert.equal(payloads.includes(encodeURIComponent(secret)), false); + assert.doesNotMatch(payloads, /synthetic-document-progress-secret/); + assert.match(f.transport.text(), /Updated credential=\[REDACTED\]/); + assert.equal(JSON.stringify(f.record()).includes(secret), false); +}); + test('one task reuses its document and tab across turns, completion and restart; visible text has no internal IDs', async t => { const f = await fixture(t); f.store.saveTask(task()); f.event('agent_delta', 'First progress'); await f.pump(5); diff --git a/test/feishu-progress.test.ts b/test/feishu-progress.test.ts index ef5f70d..beb2176 100644 --- a/test/feishu-progress.test.ts +++ b/test/feishu-progress.test.ts @@ -12,6 +12,7 @@ import { configSchema } from '../src/config.js'; import { taskSchema, type Task } from '../src/domain.js'; import type { ExecutionEvent } from '../src/execution-events.js'; import { FeishuProgressPublisher, progressPublicationSchema } from '../src/feishu-progress.js'; +import { FeishuAdapter } from '../src/adapters/feishu.js'; import { AgentLoop } from '../src/loop.js'; import { TaskGroupUnavailable, type FeishuProgressPort } from '../src/ports.js'; import { Store } from '../src/store.js'; @@ -88,13 +89,13 @@ function task(id = 'task', turnId = 'turn'): Task { contextDirectory: '/tmp', resources: [], status: 'running', reason: null, prompt: '', promptHash: '', skillPath: null, chatId: `chat-${id}`, threadId: `thread-${id}`, turnId, executionStatus: 'running', lastHandledTurnId: null, requests: [], createdAt: 1, updatedAt: 1 }); } -async function fixture(t: TestContext, initialize = true) { +async function fixture(t: TestContext, initialize = true, adapter?: FeishuProgressPort) { const directory = await realpath(await mkdtemp(join(tmpdir(), 'feishu-progress-'))); let store = new Store(directory); const transport = new Transport(); const source = new ExecutionFixture(); let now = Date.UTC(2026, 10, 7); - let publisher = new FeishuProgressPublisher({ store, execution: source.execution, transport, now: () => now }); + let publisher = new FeishuProgressPublisher({ store, execution: source.execution, transport: adapter ?? transport, now: () => now }); t.after(async () => { await publisher.close(); store.close(); await rm(directory, { recursive: true, force: true }); }); if (initialize) await publisher.poll(); const event = (kind: ExecutionEvent['kind'], text: string, changes: Partial = {}, taskId = 'task') => { @@ -114,6 +115,34 @@ test('progress is opt-in and validates its interval and explicit dashboard URL', assert.throws(() => configSchema.parse({ execution: testExecution, feishu: { userOpenId: 'owner', progress: { dashboardUrl: 'file:///etc/passwd' } } })); }); +test('actual publisher redacts cached execution text in CardKit creation and streaming updates', async t => { + const secret = 'synthetic-publisher-app-secret?value~end'; + configSchema.parse({ execution: testExecution, feishu: { userOpenId: 'owner', appSecret: secret } }); + const requests: unknown[] = []; + const unused = async (): Promise => { throw new Error('Unexpected Feishu request'); }; + const adapter = new FeishuAdapter({ appId: 'synthetic-publisher-app', appSecret: secret, userOpenId: 'owner' }, { + api: { chat: { list: unused, get: unused, create: unused }, messageResource: { get: unused }, + message: { list: unused, create: async input => { requests.push(input); return { code: 0, data: { message_id: 'message' } }; } } }, + cardkit: { card: { + create: async input => { requests.push(input); return { code: 0, data: { card_id: 'card' } }; }, + settings: async input => { requests.push(input); return { code: 0, data: {} }; }, idConvert: unused, + }, cardElement: { content: async input => { requests.push(input); return { code: 0, data: {} }; } } }, + }); + t.after(() => adapter.close()); + const f = await fixture(t, true, adapter); + f.store.saveTask(task()); + f.event('agent_message', `First credential=${secret}`); + await f.pump(4); + f.event('agent_message', `Changed credential=${encodeURIComponent(secret)}`); + await f.pump(3); + const serialized = JSON.stringify(requests); + assert.doesNotMatch(serialized, /synthetic-publisher-app-secret/); + assert.equal(serialized.includes(encodeURIComponent(secret)), false); + assert.match(serialized, /First credential=\[REDACTED\]/); + assert.match(serialized, /Changed credential=\[REDACTED\]/); + assert.deepEqual(f.store.diagnostics(), []); +}); + test('saved cards stop publishing after group removal and recreated groups get a fresh card and UUID', async t => { const f = await fixture(t); f.store.saveTask(task()); await f.pump(3); const original = f.publication(), calls = f.transport.calls.length;