Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(pi): 远端权限文件按快照隔离,附件先 stat 再读

yanbbfd0fc9bb33
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
index 1c8255d066..7bf3a7189d 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
@@ -1312,7 +1312,11 @@ describe('Pi provider-aware model routing', () => {
expect(capturedRemoteEnvs[0]!.CINDY_PI_PERMISSION_HASH)
.not.toBe(capturedRemoteEnvs[1]!.CINDY_PI_PERMISSION_HASH);
expect(capturedRemoteEnvs[0]!.CINDY_PI_PERMISSION_FILE)
- .toBe(capturedRemoteEnvs[1]!.CINDY_PI_PERMISSION_FILE);
+ .not.toBe(capturedRemoteEnvs[1]!.CINDY_PI_PERMISSION_FILE);
+ expect(capturedRemoteEnvs[0]!.CINDY_PI_PERMISSION_FILE)
+ .toContain(capturedRemoteEnvs[0]!.CINDY_PI_PERMISSION_HASH);
+ expect(capturedRemoteEnvs[1]!.CINDY_PI_PERMISSION_FILE)
+ .toContain(capturedRemoteEnvs[1]!.CINDY_PI_PERMISSION_HASH);
});
it('inlines remote text attachments and rejects local path mentions before dispatch', async () => {
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 883c4570f7..4643d95ad4 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -409,8 +409,15 @@ async function buildPiPrompt(
// 避免 turn 被接受但附件静默丢失,或远端同路径文件被误读。
let data: Buffer;
try {
+ const stat = await fs.stat(block.path);
+ if (stat.size > REMOTE_PI_ATTACHMENT_MAX_BYTES) {
+ throw new Error(
+ `pi: remote file attachment ${block.path} is ${stat.size} bytes (limit ${REMOTE_PI_ATTACHMENT_MAX_BYTES}) — upload to the remote host or send a smaller file`,
+ );
+ }
data = await fs.readFile(block.path);
} catch (err) {
+ if (err instanceof Error && err.message.startsWith('pi: remote file attachment')) throw err;
throw new Error(
`pi: failed to read file attachment ${block.path}: ${err instanceof Error ? err.message : String(err)}`,
);
@@ -1189,29 +1196,8 @@ export class PiAgent extends BaseAgent {
// 轮 20-V1 HIGH:远端路径必须 POSIX join —— path.join 在 Windows 本地会把
// runtimeDir 与文件名拼成反斜杠(远端 shell 不认, 权限文件/子代理 runtime
// 写不进删不掉, 破坏权限门与子代理路由)。runtimeDir 已是 posix(956 行)。
- const permissionFile = joinRemotePosixPath(
- runtimeDir,
- `perm-${sid ?? `anon-${process.pid}-${Date.now()}`}-${runtimeInstanceId}.json`,
- );
- // 子代理运行期快照(model + provider)。与权限档同机制:文件而非 env —— env 在 spawn
- // 时定型,会话中途 setModel 后子代理会继续用启动时的旧模型(greptile P1),而 BYOM /
- // 本地 provider 不一起传还会让同名模型落到错误 endpoint(codex P2,pi-harness §3 要求
- // BYOM 直连原生 provider)。扩展每次派子代理现读本文件。
- const subagentRuntimeFile = joinRemotePosixPath(
- runtimeDir,
- `subagent-${sid ?? `anon-${process.pid}-${Date.now()}`}-${runtimeInstanceId}.json`,
- );
- let runtimeFilesCleaned = false;
- /**
- * 回收本运行时的两个 runtime 文件(幂等)。带 nonce 之后它们不再按 sessionId 复用,
- * 不回收就会随每次 startSession 无界堆积在 runtimeDir 里。
- */
- const cleanupRuntimeFiles = (): void => {
- if (runtimeFilesCleaned) return;
- runtimeFilesCleaned = true;
- void rmPath(permissionFile);
- void rmPath(subagentRuntimeFile);
- };
+ // 远端权限/子代理文件名带启动快照 hash:startSession 在 pi/ensure 之前就会写文件,
+ // 若复用旧路径, 另一实例的更宽档位会先覆盖仍在跑的 Pi 热读文件。
// auto 保留(Cindy 侧 dispatcher 用);bridge 只特判 bypassPermissions,auto 在
// 桥内行为同 ask(非只读全部冒泡)。其余档(default/acceptEdits/plan)归 ask 最严。
const normalizePermissionMode = (mode: string | undefined): 'ask' | 'auto' | 'bypassPermissions' =>
@@ -1248,6 +1234,33 @@ export class PiAgent extends BaseAgent {
readOnlyRoots: [...mutableExtraDirs],
...reviewPathSnapshot,
};
+ const permissionSnapshotHash = createHash('sha256')
+ .update(JSON.stringify(requestedPermissionSnapshot))
+ .digest('hex')
+ .slice(0, 16);
+ const permissionFile = joinRemotePosixPath(
+ runtimeDir,
+ `perm-${sid ?? `anon-${process.pid}-${Date.now()}`}-${runtimeInstanceId}${remote ? `-${permissionSnapshotHash}` : ''}.json`,
+ );
+ // 子代理运行期快照(model + provider)。与权限档同机制:文件而非 env —— env 在 spawn
+ // 时定型,会话中途 setModel 后子代理会继续用启动时的旧模型(greptile P1),而 BYOM /
+ // 本地 provider 不一起传还会让同名模型落到错误 endpoint(codex P2,pi-harness §3 要求
+ // BYOM 直连原生 provider)。扩展每次派子代理现读本文件。
+ const subagentRuntimeFile = joinRemotePosixPath(
+ runtimeDir,
+ `subagent-${sid ?? `anon-${process.pid}-${Date.now()}`}-${runtimeInstanceId}${remote ? `-${permissionSnapshotHash}` : ''}.json`,
+ );
+ let runtimeFilesCleaned = false;
+ /**
+ * 回收本运行时的两个 runtime 文件(幂等)。带 nonce 之后它们不再按 sessionId 复用,
+ * 不回收就会随每次 startSession 无界堆积在 runtimeDir 里。
+ */
+ const cleanupRuntimeFiles = (): void => {
+ if (runtimeFilesCleaned) return;
+ runtimeFilesCleaned = true;
+ void rmPath(permissionFile);
+ void rmPath(subagentRuntimeFile);
+ };
// 权限档写入串行化 + 代际跳过。并发/连续切档(本地与远程控制端同时切,或用户快速连点)时,
// 无串行的 fs.writeFile 可能让较早的 Full-access 写在较新的 Ask 写之后落盘 —— bridge 每次
// tool_call 现读就会读到过期的 bypassPermissions,而 host 闭包/UI 已切到 Ask,后续破坏性工具
@@ -1811,10 +1824,7 @@ export class PiAgent extends BaseAgent {
// 仅远端注入(本地无 envHash 机制); 值本身无凭证(只是内容指纹)。
if (remote) {
spawnEnv[PI_MODELS_JSON_HASH_ENV] = modelsJsonHash;
- spawnEnv[PI_PERMISSION_HASH_ENV] = createHash('sha256')
- .update(JSON.stringify(requestedPermissionSnapshot))
- .digest('hex')
- .slice(0, 16);
+ spawnEnv[PI_PERMISSION_HASH_ENV] = permissionSnapshotHash;
}
mergeLoopbackNoProxy(spawnEnv);
const { transport } = await this.createTransport(