Files
cutdex/test/agent_tool_recovery_test.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

118 lines
3.5 KiB
Dart

import 'dart:io';
import 'package:cutdex/integrations/agent/editor_agent_tools.dart';
import 'package:cutdex_agent/cutdex_agent.dart';
import 'package:cutdex_agent/io.dart';
import 'package:cutdex_sdk/editor/commands.dart';
import 'package:cutdex_sdk/tools.dart';
import 'package:flutter_test/flutter_test.dart';
class _Host implements EditorCommandHost {
int reads = 0;
int writes = 0;
@override
Set<EditorOperation> get supportedOperations =>
EditorOperation.values.toSet();
@override
Future<CommandResult> execute(
EditorCommand command,
CommandContext context,
) async {
if (command is QueryTimeline) {
reads++;
} else {
writes++;
}
return CommandResult(
status: CommandStatus.succeeded,
message: 'fresh state',
version: 'new-process:0',
);
}
}
class _Model implements ModelAdapter {
int requests = 0;
@override
Stream<ModelEvent> stream(
ModelRequest request,
CancellationToken cancellation,
) async* {
requests++;
expect(request.messages.last.role, MessageRole.tool);
yield ModelResponse(text: 'verified');
}
}
void main() {
for (final (name, status) in [
('cutdex_workspaces', CallStatus.started),
('timeline_query', CallStatus.started),
('clip_setAudio', CallStatus.started),
('clip_setAudio', CallStatus.planned),
]) {
test('restart recovery of $name $status preserves replay safety', () async {
final root = await Directory.systemTemp.createTemp('agent-recovery-');
addTearDown(() => root.delete(recursive: true));
final call = ToolCall(
id: 'call',
name: name,
arguments: {
if (name != 'cutdex_workspaces') 'workspaceId': 'timeline',
if (name == 'clip_setAudio') ...{
'clipId': 'clip',
'db': -10,
'muted': false,
'expectedVersion': status == CallStatus.planned
? 'new-process:0'
: 'old-process:1',
},
},
);
await FileSessionStore(root).save(
SessionSnapshot(
id: 'session',
runId: 'interrupted',
status: RunStatus.running,
history: [
AgentMessage(role: MessageRole.user, text: 'do the task'),
AgentMessage(role: MessageRole.assistant, calls: [call]),
],
pending: [
CallRecord(
call: call,
operationId: 'stable-operation',
status: status,
),
],
),
expectedRevision: null,
);
// All dispatcher memory is fresh, as after a process restart.
final host = _Host();
final model = _Model();
final manager = SessionManager(
model: model,
store: FileSessionStore(root),
tools: editorAgentTools({'timeline': EditorToolDispatcher(host: host)}),
);
final run = await manager.resume('session');
final result = await run.done;
expect(host.writes, status == CallStatus.planned ? 1 : 0);
if (name == 'clip_setAudio' && status == CallStatus.started) {
expect(result.status, RunStatus.waiting);
expect(result.pending.single.operationId, 'stable-operation');
expect(model.requests, 0);
} else {
expect(result.status, RunStatus.completed);
expect(model.requests, 1);
expect(host.reads, name == 'timeline_query' ? 1 : 0);
expect(
result.history.where((m) => m.role == MessageRole.tool),
hasLength(1),
);
}
});
}
}