File size: 7,037 Bytes
68d7816 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | import { Disposable } from '#/_base/di/lifecycle';
import type { IAgentScopeHandle } from '#/_base/di/scope';
import { onUnexpectedError } from '#/_base/errors/unexpectedError';
import { ScopeActivation, registerScopedService } from '#/_base/di/scope';
import { LifecycleScope } from '#/app/scopes';
import { ISessionEventBus } from '#/app/event/eventBus';
import { IAgentLoopService } from '#/agent/loop/loop';
import { IAgentTaskService } from '#/agent/task/task';
import { SubagentSuspended } from '#/features/swarm/session/sessionSwarmService';
import { IAgentLifecycleService } from '#/session/agentLifecycle/agentLifecycle';
import { ILogService } from '#/_base/log/log';
import { IEventDispatcher } from '#/state/eventDispatcher';
import { SubagentCancelled, SubagentCompleted, SubagentFailed, SubagentStarted } from './mirrorAgentRun';
import {
ISessionSubagentScopeCacheService,
resolveSubagentScopeCacheSize,
resolveSubagentScopeEvictTimeoutMs,
} from './subagentScopeCache';
const MAX_EVICT_ATTEMPTS = 3;
type EvictOutcome = 'removed' | 'missing' | 'closing' | 'deferred' | 'timeout' | 'failed';
export class SessionSubagentScopeCacheService
extends Disposable
implements ISessionSubagentScopeCacheService
{
declare readonly _serviceBrand: undefined;
private readonly capacity: number;
private readonly removeTimeoutMs: number;
private readonly retired = new Map<string, number>();
private readonly closing = new Set<string>();
private evictions: Promise<void> = Promise.resolve();
constructor(
@IAgentLifecycleService private readonly agentLifecycle: IAgentLifecycleService,
@ISessionEventBus bus: ISessionEventBus,
@ILogService private readonly log: ILogService,
) {
super();
this.capacity = resolveSubagentScopeCacheSize();
this.removeTimeoutMs = resolveSubagentScopeEvictTimeoutMs();
if (this.capacity === 0) return;
this._register(
bus.subscribe(SubagentCompleted, (event) => {
this.retire(event.subagentId);
}),
);
this._register(
bus.subscribe(SubagentFailed, (event) => {
this.retire(event.subagentId);
}),
);
this._register(
bus.subscribe(SubagentCancelled, (event) => {
this.retire(event.subagentId);
}),
);
this._register(
bus.subscribe(SubagentStarted, (event) => {
this.revive(event.subagentId);
}),
);
this._register(
bus.subscribe(SubagentSuspended, (event) => {
this.revive(event.subagentId);
}),
);
this._register(
this.agentLifecycle.onDidCreate((agent) => {
this.revive(agent.agentId);
}),
);
this._register(
this.agentLifecycle.onWillClose((agent) => {
this.closing.add(agent.agentId);
}),
);
this._register(
this.agentLifecycle.onDidClose((agent) => {
this.closing.delete(agent.agentId);
this.revive(agent.agentId);
}),
);
}
private retire(agentId: string): void {
this.retired.delete(agentId);
this.retired.set(agentId, 0);
this.evictions = this.evictions.then(() => this.evictOverflow()).catch(onUnexpectedError);
}
private revive(agentId: string): void {
this.retired.delete(agentId);
}
private async evictOverflow(): Promise<void> {
const skipped = new Set<string>();
while (this.retired.size > this.capacity) {
const candidate = this.oldestCandidate(skipped);
if (candidate === undefined) return;
const [agentId, attempts] = candidate;
this.retired.delete(agentId);
const outcome = await this.evict(agentId);
if (outcome === 'removed' || outcome === 'missing') continue;
skipped.add(agentId);
if (outcome === 'deferred') {
this.log.debug('subagent scope eviction deferred; agent still busy', { agentId });
if (!this.retired.has(agentId)) this.retired.set(agentId, attempts);
continue;
}
if (outcome === 'closing' || outcome === 'failed') {
if (!this.retired.has(agentId)) this.retired.set(agentId, attempts);
continue;
}
const nextAttempt = attempts + 1;
if (nextAttempt >= MAX_EVICT_ATTEMPTS) {
this.log.warn('subagent scope eviction abandoned; agent still busy', {
agentId,
attempts: nextAttempt,
});
}
if (!this.retired.has(agentId)) this.retired.set(agentId, nextAttempt);
}
}
private oldestCandidate(skipped: ReadonlySet<string>): [string, number] | undefined {
for (const entry of this.retired) {
if (entry[1] < MAX_EVICT_ATTEMPTS && !skipped.has(entry[0])) return entry;
}
return undefined;
}
private async evict(agentId: string): Promise<EvictOutcome> {
const context = this.agentLifecycle.get(agentId);
if (context === undefined) return this.closing.has(agentId) ? 'closing' : 'missing';
const handle = this.agentLifecycle.handleOf(agentId);
if (handle === undefined) return this.closing.has(agentId) ? 'closing' : 'missing';
if (this.busy(handle)) return 'deferred';
try {
await handle.accessor.get(IEventDispatcher).flush();
} catch (error) {
this.log.warn('subagent scope eviction skipped; wire flush failed', { agentId, error });
return 'failed';
}
if (this.agentLifecycle.handleOf(agentId) !== handle) {
return this.closing.has(agentId) ? 'closing' : 'missing';
}
if (this.busy(handle)) return 'deferred';
const startedAt = Date.now();
const removal = this.agentLifecycle.remove(context).then(
() => 'removed' as const,
(error: unknown) => ({ error }),
);
let timer: ReturnType<typeof setTimeout> | undefined;
const outcome = await Promise.race([
removal,
new Promise<'timeout'>((resolve) => {
timer = setTimeout(() => resolve('timeout'), this.removeTimeoutMs);
}),
]);
if (timer !== undefined) clearTimeout(timer);
const durationMs = Date.now() - startedAt;
if (outcome === 'timeout') {
this.log.warn('subagent scope eviction timed out; moving on to the next eviction', {
agentId,
durationMs,
});
return 'timeout';
}
if (outcome === 'removed') {
this.log.debug('subagent scope evicted', { agentId, durationMs });
return 'removed';
}
const gone = this.agentLifecycle.get(agentId) === undefined && !this.closing.has(agentId);
this.log.warn('subagent scope eviction failed', {
agentId,
durationMs,
error: outcome.error,
});
return gone ? 'removed' : 'failed';
}
private busy(handle: IAgentScopeHandle): boolean {
const snapshot = handle.accessor.get(IAgentLoopService).snapshot();
if (snapshot.state === 'running' || snapshot.hasPendingRequests) return true;
return handle.accessor.get(IAgentTaskService).list(true).length > 0;
}
}
registerScopedService(
LifecycleScope.Session,
ISessionSubagentScopeCacheService,
SessionSubagentScopeCacheService,
ScopeActivation.OnScopeCreated,
'subagent',
);
|