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。

test(maker-core): 控制文件经目录改名原子发布,同批由构造保证

Kirozeng263b0e83f9c8
diff --git a/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
index 0edf07b14c..09e78ff36c 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
@@ -1,6 +1,6 @@
import { spawn } from 'node:child_process';
import { randomUUID } from 'node:crypto';
-import { chmod, mkdtemp, mkdir, readFile, readdir, rm, symlink, writeFile } from 'node:fs/promises';
+import { chmod, mkdtemp, mkdir, readFile, readdir, rename, rm, symlink, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
@@ -103,6 +103,35 @@ async function readCommandsIfPresent(
return trimmed.split('\n').map((line) => JSON.parse(line) as { type?: string; message?: string });
}
+/**
+ * Publish a set of controls so that no runner scan can observe a subset of
+ * them: the controls are written into a staging directory and the whole
+ * directory is swapped over the runner-created (still empty) `controls` dir.
+ * While the swap is in flight a scan reads ENOENT, which both scan sites
+ * treat as "no controls" — so the first scan that sees anything sees every
+ * control, and a shared batch is guaranteed by construction instead of by
+ * the timing of individual writes.
+ */
+async function publishControlsAtomically(
+ runDir: string,
+ controls: Array<Record<string, unknown>>,
+): Promise<void> {
+ const controlsDir = path.join(runDir, 'controls');
+ const stagingDir = path.join(runDir, `controls-staging-${randomUUID()}`);
+ const retiredDir = path.join(runDir, `controls-retired-${randomUUID()}`);
+ await mkdir(stagingDir, { recursive: true });
+ for (const control of controls) {
+ const requestId = randomUUID();
+ await writeFile(
+ path.join(stagingDir, `${requestId}.json`),
+ `${JSON.stringify({ version: 1, requestId, ...control })}\n`,
+ { mode: 0o600 },
+ );
+ }
+ await rename(controlsDir, retiredDir);
+ await rename(stagingDir, controlsDir);
+}
+
async function tempRoot(): Promise<string> {
const root = await mkdtemp(path.join(os.tmpdir(), 'cindy-pi-runner-'));
roots.push(root);
@@ -1175,34 +1204,24 @@ describe('Cindy durable PI Subagent runner', () => {
const [run] = await listPiSubagentRuns(fixture.root);
return run?.tasks[0]?.pendingApproval ? run : null;
});
- const controlsDir = path.join(fixture.runDir, 'controls');
- await mkdir(controlsDir, { recursive: true });
- const write = async (control: Record<string, unknown>): Promise<void> => {
- const requestId = randomUUID();
- await writeFile(
- path.join(controlsDir, `${requestId}.json`),
- `${JSON.stringify({ version: 1, requestId, ...control })}\n`,
- { mode: 0o600 },
- );
- };
- // Approval first by every ordering key the runner sorts on. The stop
- // still lands on disk first, mirroring the sibling case below: a control
- // poll that lands between the two writes must never see the approval
- // alone — that forwards it, the child completes the run, and the wait
- // for `stopped` times out (seen once on a slow Windows runner). Writing
- // the stop first turns that window into a stop-only scan, which still
- // stops the run and satisfies the assertions; the both-in-one-scan
- // batch this case is about stays the dominant interleaving, since the
- // write gap is a fraction of the runner's CONTROL_POLL_MS.
- await write({ seq: 2, requestedAt: 2, action: 'stop' });
- await write({
- seq: 1,
- requestedAt: 1,
- action: 'approval',
- childId: pending.tasks[0]?.childId,
- approvalId: 'approval-1',
- confirmed: true,
- });
+ // Approval first by every ordering key the runner sorts on. Both controls
+ // are published in one atomic directory swap, so no control scan can ever
+ // observe the approval without the stop: the shared batch this case is
+ // about is guaranteed by construction, not by the timing of two separate
+ // writes. (Two direct writes raced a poll that consumed the approval
+ // alone, which forwarded it, let the child finish, and completed the run
+ // before the stop was ever seen — once, on a slow Windows runner.)
+ await publishControlsAtomically(fixture.runDir, [
+ {
+ seq: 1,
+ requestedAt: 1,
+ action: 'approval',
+ childId: pending.tasks[0]?.childId,
+ approvalId: 'approval-1',
+ confirmed: true,
+ },
+ { seq: 2, requestedAt: 2, action: 'stop' },
+ ]);
const stopped = await waitFor(async () => {
const [run] = await listPiSubagentRuns(fixture.root);
@@ -1236,29 +1255,21 @@ describe('Cindy durable PI Subagent runner', () => {
const [run] = await listPiSubagentRuns(fixture.root);
return run?.tasks[0]?.pendingApproval ? run : null;
});
- const controlsDir = path.join(fixture.runDir, 'controls');
- await mkdir(controlsDir, { recursive: true });
- const stopId = randomUUID();
- await writeFile(
- path.join(controlsDir, `${stopId}.json`),
- `${JSON.stringify({ version: 1, requestId: stopId, seq: 2, requestedAt: 2, action: 'stop' })}\n`,
- { mode: 0o600 },
- );
- const approvalId = randomUUID();
- await writeFile(
- path.join(controlsDir, `${approvalId}.json`),
- `${JSON.stringify({
- version: 1,
- requestId: approvalId,
+ // Same atomic publication as the previous case: one directory swap makes
+ // the stop and the approval visible to the same scan by construction, so
+ // the partition's "approval first by ordering key" premise holds without
+ // relying on write timing.
+ await publishControlsAtomically(fixture.runDir, [
+ { seq: 2, requestedAt: 2, action: 'stop' },
+ {
seq: 1,
requestedAt: 1,
action: 'approval',
childId: pending.tasks[0]?.childId,
approvalId: 'approval-1',
confirmed: true,
- })}\n`,
- { mode: 0o600 },
- );
+ },
+ ]);
const stopped = await waitFor(async () => {
const [run] = await listPiSubagentRuns(fixture.root);