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): measure shared inspection wait per startup

zqchris01bee37b3445
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 6bb3166062..0216089633 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
@@ -705,6 +705,58 @@ describe('Pi package executable-code boundary', () => {
expect(runtime.spawns.filter(({ args }) => args.includes('list'))).toHaveLength(1);
});
+ it('records in-flight inspection wait separately for each startup and skips completed-cache hits', async () => {
+ const { root } = await createSkillOnlyPackage('npm:waiting-startup');
+ const manifest = path.join(await fs.realpath(root), 'package.json');
+ const store = await import('../pi-package-store.js');
+ const open = fs.open.bind(fs);
+ let entered = false;
+ let release!: () => void;
+ const gate = new Promise<void>((resolve) => { release = resolve; });
+ const openSpy = vi.spyOn(fs, 'open').mockImplementation(async (...args) => {
+ if (!entered && path.resolve(String(args[0])) === manifest) {
+ entered = true;
+ await gate;
+ }
+ return open(...args);
+ });
+ const first = store.resolveManagedPiPackageResources({ startupTraceId: '0123456789abcdef' });
+ let second: ReturnType<typeof store.resolveManagedPiPackageResources> | undefined;
+ let clock: ReturnType<typeof vi.spyOn> | undefined;
+ try {
+ await vi.waitFor(() => expect(entered).toBe(true));
+ const now = Date.now();
+ clock = vi.spyOn(Date, 'now').mockReturnValue(now);
+ second = store.resolveManagedPiPackageResources({ startupTraceId: 'fedcba9876543210' });
+ clock.mockReturnValue(now + 125);
+ release();
+ const results = await Promise.all([first, second]);
+ expect(results[1]).toEqual(results[0]);
+ expect(results[1].skills).toHaveLength(1);
+ const eventsFor = (trace: string) => loggerRuntime.info.mock.calls
+ .filter(([message, fields]) => message === 'pi startup stage' && fields.startupTraceId === trace)
+ .map(([, fields]) => fields as Record<string, unknown>);
+ const waitingEvents = eventsFor('fedcba9876543210');
+ expect(waitingEvents).toHaveLength(5);
+ expect(waitingEvents.find((event) => event.stage === 'package-inspection')).toMatchObject({
+ status: 'ok', durationMs: 125,
+ });
+ for (const event of waitingEvents.filter((event) => event.stage !== 'package-inspection')) {
+ expect(event).toMatchObject({ status: 'skipped', durationMs: 0 });
+ }
+ await store.resolveManagedPiPackageResources({ startupTraceId: '1111111111111111' });
+ for (const event of eventsFor('1111111111111111')) {
+ expect(event).toMatchObject({ status: 'skipped', durationMs: 0 });
+ }
+ expect(runtime.spawns.filter(({ args }) => args.includes('list'))).toHaveLength(1);
+ } finally {
+ release();
+ await Promise.allSettled([first, ...(second ? [second] : [])]);
+ clock?.mockRestore();
+ openSpy.mockRestore();
+ }
+ });
+
it.each([null, [null], [{ warning: 'bad-cache' }]])(
'keeps native loading and disable preferences when advisory cache is malformed: %j',
async (snapshotUnavailablePackages) => {
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 3428be8578..3ec147c35e 100644
--- a/apps/desktop/src/main/maker-host/pi-package-store.ts
+++ b/apps/desktop/src/main/maker-host/pi-package-store.ts
@@ -2234,10 +2234,6 @@ async function inspectAllPackagesUncached(
// process. Yield between packages so a long roster cannot monopolize it.
await new Promise<void>((resolve) => setImmediate(resolve));
}
- if (inspected.some((pkg) => pkg.view.warning === 'inspection-failed'
- || pkg.view.warning === 'inspection-limit' || Boolean(pkg.snapshotUnavailable))) {
- options.startupTiming?.degradedStages.add('package-inspection');
- }
recordPiPackageStartupDuration(options.startupTiming, 'package-inspection', inspectionStartedAt);
return inspected;
}
@@ -2250,7 +2246,12 @@ function invalidateInspectionCache(): void {
async function inspectAllPackages(startupTiming?: PiPackageStartupTiming): Promise<InspectedPackage[]> {
if (inspectionCache && inspectionCache.expiresAt > Date.now()) return inspectionCache.value;
- if (inspectionPromise) return inspectionPromise;
+ if (inspectionPromise) {
+ // Reusing in-flight work still blocks this startup. Attribute only its wait
+ // to inspection; completed-cache hits above remain skipped.
+ const pending = inspectionPromise;
+ return measurePiPackageStartupStage(startupTiming, 'package-inspection', () => pending);
+ }
const generation = inspectionGeneration;
const pending = inspectAllPackagesUncached({ startupTiming }).then((value) => {
if (generation === inspectionGeneration) {
@@ -2520,6 +2521,10 @@ export async function resolveManagedPiPackageResources(
|| pkg.view.warning === 'inspection-limit'
|| Boolean(pkg.snapshotUnavailable)
)).length;
+ if (startupTiming.skippedPackageCount > 0
+ && startupTiming.durationsMs['package-inspection'] !== undefined) {
+ startupTiming.degradedStages.add('package-inspection');
+ }
}
if (!snapshotRoot) {
// Native Pi owns loading. Cache only advisory inspection limits here;