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): scope limit caching and reuse owner probes

lizzjinb65a38b3ef28
diff --git a/apps/desktop/src/main/maker-host/__tests__/pi-package-store-security.test.ts b/apps/desktop/src/main/maker-host/__tests__/pi-package-store-security.test.ts
index 815c6ab038..4b6a60e299 100644
--- a/apps/desktop/src/main/maker-host/__tests__/pi-package-store-security.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/pi-package-store-security.test.ts
@@ -1488,6 +1488,16 @@ describe('Pi package executable-code boundary', () => {
});
});
+ it.each(['entries', 'bytes'] as const)(
+ 'does not classify an aggregate fingerprint %s limit as a durable package failure',
+ async (reason) => {
+ const store = await import('../pi-package-store.js');
+
+ expect(store.__testing.isDurableSnapshotLimit('aggregate', reason)).toBe(false);
+ expect(store.__testing.isDurableSnapshotLimit('package', reason)).toBe(true);
+ },
+ );
+
it('omits resources owned by a skipped descendant instead of mapping them through a copied ancestor', async () => {
const ancestorRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-pi-package-budget-overlap-'));
roots.push(ancestorRoot);
diff --git a/apps/desktop/src/main/maker-host/pi-package-store.ts b/apps/desktop/src/main/maker-host/pi-package-store.ts
index 800c01341e..d5ddbadf40 100644
--- a/apps/desktop/src/main/maker-host/pi-package-store.ts
+++ b/apps/desktop/src/main/maker-host/pi-package-store.ts
@@ -1604,11 +1604,7 @@ async function inspectPackage(
source: displaySource,
message: error instanceof Error ? error.message : String(error),
});
- const deterministicLimit = error instanceof PiPackageInspectionLimitError
- ? error.reason !== 'duration'
- : error instanceof PiPackageSnapshotLimitError
- ? error.reason !== 'duration'
- : false;
+ const deterministicLimit = isDurableInspectionFailure(error);
const snapshotUnavailable = deterministicLimit
&& installedRoot
&& installationIdentity
@@ -2400,6 +2396,24 @@ class PiPackageSnapshotLimitError extends Error {
}
}
+function isDurableInspectionFailure(error: unknown): boolean {
+ return error instanceof PiPackageInspectionLimitError
+ ? error.reason !== 'duration'
+ : error instanceof PiPackageSnapshotLimitError
+ ? error.scope === 'package' && error.reason !== 'duration'
+ : false;
+}
+
+/** Narrow seam for the inspection-limit persistence decision table. */
+export const __testing = {
+ isDurableSnapshotLimit(
+ scope: 'package' | 'aggregate',
+ reason: PiPackageSnapshotLimitReason,
+ ): boolean {
+ return isDurableInspectionFailure(new PiPackageSnapshotLimitError(scope, reason));
+ },
+};
+
interface SnapshotBudgetCounters {
startedAt: number;
entries: number;
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
index bd0765a699..9474ec5be9 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
@@ -2370,6 +2370,51 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {
}
});
+ it('shares one process-start probe memo across config homes owned by the same process', async () => {
+ const ownerPid = process.ppid;
+ const ownerStartTimeSec = 1;
+ const ownerHomes = ['c'.repeat(32), 'd'.repeat(32)].map((runtimeId) => {
+ const ownerHome = path.join(agentHome, 'run-tmp', runtimeId);
+ mkdirSync(ownerHome, { recursive: true });
+ writeFileSync(
+ path.join(ownerHome, '.cindy-owner.json'),
+ `${JSON.stringify({
+ version: 2,
+ ownerPid,
+ ownerStartTimeSec,
+ createdAt: 1,
+ directoryName: runtimeId,
+ runtimeId,
+ sessionIdHash: 'e'.repeat(64),
+ })}\n`,
+ );
+ return ownerHome;
+ });
+ const probeMemos: unknown[] = [];
+ const ownerProbe = vi
+ .spyOn(piSubagentRuns, 'isPiHostProcessInstanceAlive')
+ .mockImplementation(function () {
+ probeMemos.push(arguments[1]);
+ return true;
+ });
+
+ const handle = await new PiAgent(buildDeps()).startSession({
+ sessionId: 'shared-owner-probe-memo',
+ workingDir: cwd,
+ model: 'm',
+ });
+ try {
+ expect(ownerProbe).toHaveBeenCalledTimes(ownerHomes.length);
+ // pi-subagent-runs separately locks that one shared memo means one
+ // PowerShell/ps start-time probe per owner pid and sweep.
+ expect(probeMemos[0]).toBeInstanceOf(Map);
+ expect(probeMemos[1]).toBe(probeMemos[0]);
+ for (const ownerHome of ownerHomes) expect(existsSync(ownerHome)).toBe(true);
+ } finally {
+ await handle.close();
+ }
+ });
+
it('preserves markerless legacy homes even when the host process scan finds no live Pi', async () => {
const legacyHome = path.join(agentHome, 'run-tmp', '0123456789abcdef');
mkdirSync(legacyHome, { recursive: true });
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 6c6751b86d..e0a0649dfb 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -113,6 +113,7 @@ import {
syncPiSubagentPermissions,
type PiSubagentRunDiagnostic,
type PiSubagentRunStatus,
+ type ProcessStartTimeMemo,
} from './pi-subagent-runs.js';
import {
annotatePermissionRequestForUnavailableReview,
@@ -591,7 +592,11 @@ function isLocalProcessAlive(pid: number): boolean {
}
}
-function localConfigHomeOwnerIsActive(owner: LocalConfigHomeOwner, configHome: string): boolean {
+function localConfigHomeOwnerIsActive(
+ owner: LocalConfigHomeOwner,
+ configHome: string,
+ startTimeMemo?: ProcessStartTimeMemo,
+): boolean {
if (owner.version === 1) {
return owner.ownerPid === process.pid
? activeLocalConfigHomes.has(localConfigHomeKey(configHome))
@@ -600,7 +605,7 @@ function localConfigHomeOwnerIsActive(owner: LocalConfigHomeOwner, configHome: s
if (!isPiHostProcessInstanceAlive({
pid: owner.ownerPid,
startTimeSec: owner.ownerStartTimeSec,
- })) return false;
+ }, startTimeMemo)) return false;
return owner.ownerPid !== process.pid
|| activeLocalConfigHomes.has(localConfigHomeKey(configHome));
}
@@ -656,6 +661,10 @@ async function sweepStaleLocalConfigHomes(
});
return;
}
+ // One owner process may have many active session homes. Reuse only the
+ // expensive start-time sample within this sweep; each check still performs
+ // a fresh signal-0 liveness probe and deletion still re-reads the marker.
+ const ownerStartTimeMemo: ProcessStartTimeMemo = new Map();
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const candidate = path.resolve(runTmp, entry.name);
@@ -664,7 +673,7 @@ async function sweepStaleLocalConfigHomes(
// Markerless homes may belong to an older Cindy instance between directory
// creation and Pi spawn. No process snapshot can prove them reclaimable.
if (!owner) continue;
- if (localConfigHomeOwnerIsActive(owner, candidate)) continue;
+ if (localConfigHomeOwnerIsActive(owner, candidate, ownerStartTimeMemo)) continue;
// Re-read immediately before deletion. A directory whose marker changed or
// whose owner became active is no longer the orphan we proved above.
@@ -673,7 +682,7 @@ async function sweepStaleLocalConfigHomes(
!currentOwner
|| !sameLocalConfigHomeOwner(currentOwner, owner)
) continue;
- if (localConfigHomeOwnerIsActive(currentOwner, candidate)) continue;
+ if (localConfigHomeOwnerIsActive(currentOwner, candidate, ownerStartTimeMemo)) continue;
const outcome = await removeLocalConfigHomeWithRetry(candidate);
if (!outcome.removed) {
diff --git a/packages/maker-core/src/agents/pi/pi-subagent-runs.ts b/packages/maker-core/src/agents/pi/pi-subagent-runs.ts
index 80b361b422..e6045a6a83 100644
--- a/packages/maker-core/src/agents/pi/pi-subagent-runs.ts
+++ b/packages/maker-core/src/agents/pi/pi-subagent-runs.ts
@@ -1229,7 +1229,7 @@ function probeProcessStartTimeSec(pid: number, now: number): number | null {
* cannot detect reuse; only a fresh probe can, and a sweep-scoped memo is the
* largest window in which reuse is not observable anyway.
*/
-type ProcessStartTimeMemo = Map<number, number | null>;
+export type ProcessStartTimeMemo = Map<number, number | null>;
function readProcessStartTimeSec(pid: number, memo?: ProcessStartTimeMemo): number | null {
const cached = memo?.get(pid);
@@ -1270,8 +1270,11 @@ function isOwnerInstanceAlive(
}
/** Conservative liveness check for a Pi host process incarnation. */
-export function isPiHostProcessInstanceAlive(identity: PiSubagentOwnerIdentity): boolean {
- return isOwnerInstanceAlive(identity);
+export function isPiHostProcessInstanceAlive(
+ identity: PiSubagentOwnerIdentity,
+ startTimeMemo?: ProcessStartTimeMemo,
+): boolean {
+ return isOwnerInstanceAlive(identity, startTimeMemo);
}
function isProcessAlive(pid: number | undefined): boolean | null {