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(plugins): 保留启动重试与中断终态信号

liugaowei5eb8c2e3b684
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/harnessSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/harnessSlot.test.ts
index 30ab7b7d4f..6ca14be360 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/harnessSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/harnessSlot.test.ts
@@ -130,6 +130,44 @@ function fixture(remote: { remoteHostId?: string; agentDeviceId?: string } = {})
afterEach(() => vi.useRealTimers());
describe('Harness host bridge', () => {
+ // Startup row availability is transient; aborting still owns terminal signals.
+ // These unit inputs cover states the active/eligible fixtures did not exercise.
+ it('retries eligibility after repeated startup misses without granting early access', async () => {
+ const f = fixture();
+ f.eligible.mockResolvedValue('retry');
+ const request = { kind: 'read', name: 'runtime', sessionId: 's' };
+ for (let i = 0; i < 5; i++) {
+ expect(await f.slot.handleRequest('a', request, () => true)).toMatchObject({ errorCode: 'DENIED' });
+ }
+ f.eligible.mockResolvedValue('eligible');
+ expect(await f.slot.handleRequest('a', request, () => true)).toMatchObject({ ok: true });
+ expect(f.eligible).toHaveBeenCalledTimes(6);
+ f.dispose();
+ });
+
+ it('delivers terminal status and usage while aborting without accepting new requests or hooks', async () => {
+ const f = fixture();
+ f.ghosts[0].manifest.harness!.events!.push('status', 'usage');
+ f.ghosts[0].harnessCapabilityApproved!.push('harness:events:status', 'harness:events:usage');
+ await f.slot.handleRequest('a', { kind: 'read', name: 'runtime', sessionId: 's' }, () => true);
+ vi.spyOn(f.session, 'getStatus').mockReturnValue('aborting');
+ // Session still reserves the interrupt action until the native RPC returns.
+ expect(f.session.isTurnRunning()).toBe(true);
+ f.emit({ type: 'status', sessionTurnGeneration: 1, data: { isRunning: false } });
+ f.emit({ type: 'done', sessionTurnGeneration: 1, data: { usage: { inputTokens: 42 } } });
+ expect(f.events).toMatchObject([
+ { name: 'status.updated', data: { status: 'idle' } },
+ { name: 'usage.updated', data: { usage: { inputTokens: 42 } } },
+ ]);
+ expect(f.guards.every(current => current())).toBe(true);
+ expect(await f.slot.handleRequest('a', {
+ kind: 'read', name: 'runtime', sessionId: 's',
+ }, () => true)).toMatchObject({ errorCode: 'SESSION_UNAVAILABLE' });
+ expect(await f.screen()).toEqual({ action: 'continue' });
+ expect(f.messages).toHaveLength(0);
+ f.dispose();
+ });
+
// Unit regressions for the local-only, per-turn and per-runtime contracts.
// Existing cases only used local bindings and current event generations.
it.each([{ remoteHostId: 'ssh-host' }, { agentDeviceId: 'other-device' }])(
diff --git a/apps/desktop/src/main/cindy-brain/harnessSlot.ts b/apps/desktop/src/main/cindy-brain/harnessSlot.ts
index 9a16c39c23..b9be2e2ebf 100644
--- a/apps/desktop/src/main/cindy-brain/harnessSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/harnessSlot.ts
@@ -46,7 +46,6 @@ interface Binding {
current(): boolean;
eligibility: Promise<boolean> | null;
eligibilityState: 'unknown' | 'eligible' | 'ineligible';
- eligibilityAttempts: number;
buffered: Array<Omit<GhostPipeHarnessEvent, 'seq' | 'ts'>>;
dropped: number;
hooks: Map<string, boolean>;
@@ -97,16 +96,17 @@ export class GhostHarnessSlot {
nativeIdentity: this.nativeIdentity(session),
eligibility: null,
eligibilityState: 'unknown',
- eligibilityAttempts: 0,
buffered: [],
dropped: 0,
hooks: new Map(),
actions: new Map(),
lastUsage: {},
+ // Native terminal signals can arrive before abort acknowledgement.
+ // Requests and Hook decisions still require active status below.
current: () =>
this.bindings.get(session.id) === binding &&
isCurrent() &&
- session.getStatus() === 'active' &&
+ (session.getStatus() === 'active' || session.getStatus() === 'aborting') &&
isGhostOwnerScopeUsable(this.deps.ownerScope, owner),
};
this.bindings.set(session.id, binding);
@@ -157,17 +157,11 @@ export class GhostHarnessSlot {
if (binding.eligibilityState !== 'unknown')
return Promise.resolve(binding.eligibilityState === 'eligible');
if (!binding.eligibility) {
- binding.eligibilityAttempts += 1;
binding.eligibility = this.deps
.eligible(binding.session.id)
.then((outcome) => {
if (!binding.current()) return false;
- binding.eligibilityState =
- outcome === 'retry'
- ? binding.eligibilityAttempts >= 5
- ? 'ineligible'
- : 'unknown'
- : outcome;
+ binding.eligibilityState = outcome === 'retry' ? 'unknown' : outcome;
if (binding.eligibilityState === 'eligible') {
for (const queued of binding.buffered.splice(0)) this.publish(binding, queued);
} else if (binding.eligibilityState === 'ineligible') binding.buffered.length = 0;
@@ -239,7 +233,8 @@ export class GhostHarnessSlot {
if (!ghost || !hasGhostHarnessPermission(ghost, group, p.name))
return { ok: false, errorCode: 'DENIED' };
const binding = this.bindings.get(p.sessionId);
- if (!binding?.current()) return { ok: false, errorCode: 'SESSION_UNAVAILABLE' };
+ if (!binding?.current() || binding.session.getStatus() !== 'active')
+ return { ok: false, errorCode: 'SESSION_UNAVAILABLE' };
this.sync(binding);
const runtimeId = binding.runtimeId;
const current = () => {
@@ -247,6 +242,7 @@ export class GhostHarnessSlot {
return (
sourceCurrent() &&
binding.current() &&
+ binding.session.getStatus() === 'active' &&
binding.runtimeId === runtimeId &&
this.deps.allowed(ghostId, binding.session) &&
this.pluginCurrent(ghost, group, p.name as string)
@@ -469,7 +465,8 @@ export class GhostHarnessSlot {
}
break;
case 'status':
- data.status = binding.session.isTurnRunning() ? 'running' : 'idle';
+ data.status = (typeof raw.isRunning === 'boolean' ? raw.isRunning : binding.session.isTurnRunning())
+ ? 'running' : 'idle';
break;
case 'done':
break;
@@ -541,7 +538,7 @@ export class GhostHarnessSlot {
.sort((a, b) =>
installedGhostStoragePart(a).localeCompare(installedGhostStoragePart(b), 'en'),
);
- if (!ghosts.length || !binding.current()) return CONTINUE;
+ if (!ghosts.length || !binding.current() || binding.session.getStatus() !== 'active') return CONTINUE;
const data = this.metadata(binding);
if (request.toolName) data.toolName = request.toolName.slice(0, 128);
if (request.toolUseId) data.toolId = ghostActivityId(data.runtimeId, request.toolUseId);
@@ -575,6 +572,7 @@ export class GhostHarnessSlot {
sourceCurrent() &&
this.deps.allowed(ghostId, binding.session) &&
binding.current() &&
+ binding.session.getStatus() === 'active' &&
binding.runtimeId === data.runtimeId &&
binding.session.getTurnGeneration() === generation &&
binding.hooks.get(`${ghostId}:${request.name}`) !== false &&