File size: 10,819 Bytes
f0634fb | 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { SyncDescriptor } from '#/_base/di/descriptors';
import { DisposableStore } from '#/_base/di/lifecycle';
import { TestInstantiationService } from '#/_base/di/test';
import {
ContextAppendMessage,
ContextUndo,
} from '#/agent/contextMemory/contextEvents';
import { IEventBus } from '#/app/event/eventBus';
import { EventBusService } from '#/app/event/eventBusService';
import {
PlanModeCancel,
PlanModeEnter,
PlanModeExit,
planKey,
PlanRevision,
} from '#/features/plan/planOps';
import { AppendLogStore } from '#/persistence/backends/node-fs/appendLogStore';
import { InMemoryStorageService } from '#/persistence/backends/memory/inMemoryStorageService';
import { IAppendLogStore } from '#/persistence/interface/appendLogStore';
import { IFileSystemStorageService } from '#/persistence/interface/storage';
import { IAgentStateService } from '#/agent/state/agentState';
import { IEventDispatcher } from '#/state/eventDispatcher';
import { IWireService } from '#/wire/wire';
import { AGENT_WIRE_RECORD_KEY, type WireRecord } from '#/wire/record';
import {
registerTestAgentWire,
registerTestEventDispatcher,
restoreTestEventDispatcher,
testWireScope,
} from '../../wire/stubs';
const SCOPE = 'wire';
const KEY = 'plan-test';
let disposables: DisposableStore;
let dispatcher: IEventDispatcher;
let agentState: IAgentStateService;
let log: IAppendLogStore;
interface Host {
wire: IWireService;
dispatcher: IEventDispatcher;
agentState: IAgentStateService;
log: IAppendLogStore;
eventBus: IEventBus;
}
function buildHost(key: string): Host {
const ix = disposables.add(new TestInstantiationService());
ix.stub(IFileSystemStorageService, new InMemoryStorageService());
ix.set(IAppendLogStore, new SyncDescriptor(AppendLogStore));
ix.set(IEventBus, new SyncDescriptor(EventBusService));
const wire = registerTestAgentWire(ix, testWireScope(SCOPE, key), {
log: ix.get(IAppendLogStore),
eventBus: ix.get(IEventBus),
});
const dispatcher = registerTestEventDispatcher(ix);
const agentState = ix.get(IAgentStateService);
agentState.contributeState(planKey);
return { wire, dispatcher, agentState, log: ix.get(IAppendLogStore), eventBus: ix.get(IEventBus) };
}
beforeEach(() => {
disposables = new DisposableStore();
const host = buildHost(KEY);
dispatcher = host.dispatcher;
agentState = host.agentState;
log = host.log;
});
afterEach(() => disposables.dispose());
async function readRecords(key = KEY): Promise<WireRecord[]> {
await dispatcher.flush();
const out: WireRecord[] = [];
for await (const record of log.read<WireRecord>(testWireScope(SCOPE, key), AGENT_WIRE_RECORD_KEY)) {
out.push(record);
}
return out;
}
describe('plan ops (wire-backed)', () => {
it('enter/cancel/exit drive active state and persist flat records', async () => {
expect(agentState.get(planKey).active).toBe(false);
await dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p1' }));
expect(agentState.get(planKey)).toEqual({
active: true,
id: 'p1',
});
await dispatcher.dispatch(new PlanModeCancel({ agentId: 'test-agent', id: 'p1' }));
expect(agentState.get(planKey)).toEqual({ active: false });
await dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p2' }));
await dispatcher.dispatch(new PlanModeExit({ agentId: 'test-agent' }));
expect(agentState.get(planKey).active).toBe(false);
const records = await readRecords();
expect(records.map((record) => record.type)).toEqual([
'plan_mode.enter',
'plan_mode.cancel',
'plan_mode.enter',
'plan_mode.exit',
]);
expect(records.every((record) => 'payload' in record === false)).toBe(true);
expect(records[0]).toEqual(
expect.objectContaining({
type: 'plan_mode.enter',
id: 'p1',
}),
);
});
it('cancel and exit both deactivate plan mode but emit distinct record types', async () => {
await dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p1' }));
await dispatcher.dispatch(new PlanModeCancel({ agentId: 'test-agent', id: 'p1' }));
expect(agentState.get(planKey)).toEqual({ active: false });
await dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p2' }));
await dispatcher.dispatch(new PlanModeExit({ agentId: 'test-agent', id: 'p2' }));
expect(agentState.get(planKey)).toEqual({ active: false });
const records = await readRecords();
expect(records.map((record) => record.type)).toEqual([
'plan_mode.enter',
'plan_mode.cancel',
'plan_mode.enter',
'plan_mode.exit',
]);
expect(records[1]).toEqual(expect.objectContaining({ type: 'plan_mode.cancel', id: 'p1' }));
expect(records[3]).toEqual(expect.objectContaining({ type: 'plan_mode.exit', id: 'p2' }));
});
it('apply returns the same reference on a no-op (gate stays quiet)', async () => {
const initial = agentState.get(planKey);
await dispatcher.dispatch(new PlanModeCancel({ agentId: 'test-agent' }));
expect(agentState.get(planKey)).toBe(initial);
await dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p1' }));
const active = agentState.get(planKey);
await dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p1' }));
expect(agentState.get(planKey)).toBe(active);
});
it('ignores an invalid undo count without corrupting checkpoint state', async () => {
await dispatcher.dispatch(
new ContextAppendMessage({
agentId: 'test-agent',
message: {
role: 'user',
content: [{ type: 'text', text: 'keep me' }],
toolCalls: [],
origin: { kind: 'user' },
},
}),
);
const checkpointed = agentState.get(planKey);
await dispatcher.dispatch(new ContextUndo({ agentId: 'test-agent', count: 0.5 }));
expect(agentState.get(planKey)).toBe(checkpointed);
expect(agentState.get(planKey)).toEqual({ active: false });
});
it('replay rebuilds active state silently', async () => {
await dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p1' }));
const records = await readRecords();
const host = buildHost('plan-replay');
const emissions: string[] = [];
host.eventBus.subscribe((e) => {
emissions.push(e.type);
});
await restoreTestEventDispatcher(
host.dispatcher,
host.log,
testWireScope(SCOPE, 'plan-replay'),
records,
);
expect(host.agentState.get(planKey)).toEqual({
active: true,
id: 'p1',
});
expect(emissions).toEqual([]);
const cancelled = buildHost('plan-replay-cancel');
await restoreTestEventDispatcher(
cancelled.dispatcher,
cancelled.log,
testWireScope(SCOPE, 'plan-replay-cancel'),
[
{ type: 'plan_mode.enter', id: 'p1', planFilePath: '/w/plan/p1.md' },
{ type: 'plan_mode.cancel', id: 'p1' },
],
);
expect(cancelled.agentState.get(planKey).active).toBe(false);
});
it('plan.revision persists a flat reference record and advances the per-id counter', async () => {
await dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p1' }));
await dispatcher.dispatch(
new PlanRevision({
agentId: 'test-agent',
id: 'p1',
version: 1,
key: 'plan/p1/v1.md',
sha256: 'sha-a',
bytes: 12,
}),
);
expect(agentState.get(planKey)).toEqual({
active: true,
id: 'p1',
revisionCount: { p1: 1 },
});
await dispatcher.dispatch(
new PlanRevision({
agentId: 'test-agent',
id: 'p1',
version: 2,
key: 'plan/p1/v2.md',
sha256: 'sha-b',
bytes: 20,
}),
);
expect(agentState.get(planKey).revisionCount).toEqual({ p1: 2 });
const records = await readRecords();
expect(records.map((record) => record.type)).toEqual([
'plan_mode.enter',
'plan.revision',
'plan.revision',
]);
expect(records[1]).toEqual(
expect.objectContaining({
type: 'plan.revision',
id: 'p1',
version: 1,
key: 'plan/p1/v1.md',
sha256: 'sha-a',
bytes: 12,
time: expect.any(Number),
}),
);
expect(records.every((record) => 'payload' in record === false)).toBe(true);
});
it('keeps the revision counter across the lifecycle and emits the event only live', async () => {
const host = buildHost('plan-revision-events');
const emissions: unknown[] = [];
host.eventBus.subscribe((e) => {
emissions.push(e);
});
await host.dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p1' }));
await host.dispatcher.dispatch(
new PlanRevision({
agentId: 'test-agent',
id: 'p1',
version: 1,
key: 'plan/p1/v1.md',
sha256: 'sha-a',
bytes: 12,
}),
);
await host.dispatcher.dispatch(new PlanModeExit({ agentId: 'test-agent' }));
expect(host.agentState.get(planKey)).toEqual({
active: false,
revisionCount: { p1: 1 },
});
await host.dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p1' }));
expect(host.agentState.get(planKey).revisionCount).toEqual({ p1: 1 });
expect(
emissions.filter((e) => (e as { type: string }).type === 'plan.revision'),
).toEqual([
expect.objectContaining({
type: 'plan.revision',
id: 'p1',
version: 1,
key: 'plan/p1/v1.md',
sha256: 'sha-a',
bytes: 12,
}),
]);
});
it('replay restores the revision counter silently', async () => {
await dispatcher.dispatch(new PlanModeEnter({ agentId: 'test-agent', id: 'p1' }));
await dispatcher.dispatch(
new PlanRevision({
agentId: 'test-agent',
id: 'p1',
version: 1,
key: 'plan/p1/v1.md',
sha256: 'sha-a',
bytes: 12,
}),
);
await dispatcher.dispatch(
new PlanRevision({
agentId: 'test-agent',
id: 'p1',
version: 2,
key: 'plan/p1/v2.md',
sha256: 'sha-b',
bytes: 20,
}),
);
const records = await readRecords();
const host = buildHost('plan-revision-replay');
const emissions: string[] = [];
host.eventBus.subscribe((e) => {
emissions.push(e.type);
});
await restoreTestEventDispatcher(
host.dispatcher,
host.log,
testWireScope(SCOPE, 'plan-revision-replay'),
records,
);
expect(host.agentState.get(planKey)).toEqual({
active: true,
id: 'p1',
revisionCount: { p1: 2 },
});
expect(emissions).toEqual([]);
});
});
|