opensymphony 1.8.0

A Rust implementation of the OpenAI Symphony orchestration design
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use chrono::Utc;
use opensymphony::opensymphony_domain::{
    ControlPlaneAgentServerStatus as AgentServerStatus,
    ControlPlaneDaemonSnapshot as DaemonSnapshot, ControlPlaneDaemonState as DaemonState,
    ControlPlaneDaemonStatus as DaemonStatus, ControlPlaneIssueRuntimeState as IssueRuntimeState,
    ControlPlaneIssueSnapshot as IssueSnapshot, ControlPlaneMetricsSnapshot as MetricsSnapshot,
    ControlPlaneWorkerOutcome as WorkerOutcome, InMemoryEventJournal, SnapshotEnvelope,
};
use opensymphony::opensymphony_gateway::action_handler::ActionHandler;
use opensymphony::opensymphony_gateway_schema::{
    action::{ActionDispatch, ActionKind, ActionStatus, ActionTarget},
    envelope::EntityKind,
    event_journal::EventKind,
    version::SchemaVersion,
};

fn fixture_snapshot(
    identifier: &str,
    runtime_state: IssueRuntimeState,
    last_outcome: WorkerOutcome,
) -> DaemonSnapshot {
    let now = Utc::now();
    DaemonSnapshot {
        generated_at: now,
        daemon: DaemonStatus {
            state: DaemonState::Ready,
            last_poll_at: now,
            workspace_root: "/tmp/opensymphony".to_owned(),
            status_line: "ready".to_owned(),
        },
        agent_server: AgentServerStatus {
            reachable: true,
            base_url: "http://127.0.0.1:3000".to_owned(),
            conversation_count: 1,
            status_line: "healthy".to_owned(),
        },
        memory_server: Default::default(),
        metrics: MetricsSnapshot {
            running_issues: 0,
            retry_queue_depth: 0,
            input_tokens: 0,
            output_tokens: 0,
            cache_read_tokens: 0,
            total_tokens: 0,
            total_cost_micros: 0,
        },
        issues: vec![IssueSnapshot {
            identifier: identifier.to_owned(),
            title: "Test Issue".to_owned(),
            tracker_state: "In Progress".to_owned(),
            runtime_state,
            last_outcome,
            last_event_at: now,
            conversation_id_suffix: "c0e255".to_owned(),
            workspace_path_suffix: "COE-255".to_owned(),
            retry_count: 0,
            blocked: false,
            server_base_url: None,
            transport_target: None,
            http_auth_mode: None,
            websocket_auth_mode: None,
            websocket_query_param_name: None,
            recent_events: Vec::new(),
            modified_files: Vec::new(),
            input_tokens: 0,
            output_tokens: 0,
            cache_read_tokens: 0,
        }],
        recent_events: Vec::new(),
    }
}

fn fixture_envelope(
    identifier: &str,
    runtime_state: IssueRuntimeState,
    last_outcome: WorkerOutcome,
) -> SnapshotEnvelope {
    SnapshotEnvelope {
        sequence: 1,
        published_at: Utc::now(),
        snapshot: fixture_snapshot(identifier, runtime_state, last_outcome),
    }
}

fn action_dispatch(
    action_kind: ActionKind,
    entity_id: &str,
    correlation_id: &str,
) -> ActionDispatch {
    ActionDispatch {
        schema_version: SchemaVersion::v1(),
        correlation_id: correlation_id.to_owned(),
        action_kind,
        target_entity: ActionTarget {
            entity_kind: EntityKind::Issue,
            entity_id: entity_id.to_owned(),
        },
        payload: None,
        idempotency_key: None,
    }
}

#[tokio::test]
async fn action_handler_returns_accepted_receipt_with_correlation_id() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let dispatch = action_dispatch(ActionKind::Cancel, "COE-255", "corr_001");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Accepted);
    assert_eq!(receipt.correlation_id, "corr_001");
    assert!(!receipt.action_id.is_empty());
    assert!(receipt.reason.is_none());
}

#[tokio::test]
async fn action_handler_returns_rejected_receipt_for_unknown_issue() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let dispatch = action_dispatch(ActionKind::Cancel, "COE-999", "corr_002");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Rejected);
    assert_eq!(receipt.correlation_id, "corr_002");
    assert!(
        receipt
            .reason
            .as_ref()
            .expect("should not be None")
            .contains("not found")
    );
}

#[tokio::test]
async fn action_handler_rejects_retry_on_active_run() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let dispatch = action_dispatch(ActionKind::Retry, "COE-255", "corr_003");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Rejected);
    assert!(
        receipt
            .reason
            .as_ref()
            .expect("should not be None")
            .contains("already active")
    );
}

#[tokio::test]
async fn action_handler_accepts_cancel_on_running_issue() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let dispatch = action_dispatch(ActionKind::Cancel, "COE-255", "corr_004");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Accepted);
    assert!(receipt.reason.is_none());
}

#[tokio::test]
async fn action_handler_rejects_rehydrate_on_running_issue() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let dispatch = action_dispatch(ActionKind::Rehydrate, "COE-255", "corr_005");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Rejected);
    assert!(
        receipt
            .reason
            .as_ref()
            .expect("should not be None")
            .contains("Rehydrate is only available")
    );
}

#[tokio::test]
async fn action_handler_accepts_rehydrate_on_completed_with_failed_outcome() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Completed,
        WorkerOutcome::Failed,
    );
    let dispatch = action_dispatch(ActionKind::Rehydrate, "COE-255", "corr_006");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Accepted);
    assert!(receipt.reason.is_none());
}

#[tokio::test]
async fn action_handler_rejects_rehydrate_on_completed_with_running_outcome() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Completed,
        WorkerOutcome::Running,
    );
    let dispatch = action_dispatch(ActionKind::Rehydrate, "COE-255", "corr_007");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Rejected);
    assert!(
        receipt
            .reason
            .as_ref()
            .expect("should not be None")
            .contains("Rehydrate is only available")
    );
}

#[tokio::test]
async fn action_handler_publishes_accepted_event_to_journal() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let dispatch = action_dispatch(ActionKind::Cancel, "COE-255", "corr_008");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Accepted);
    let cursor = opensymphony::opensymphony_gateway_schema::cursor::StreamCursor::new(0, "events");
    let page = journal.query_after(&cursor, 10).await.expect("query");
    assert_eq!(page.events.len(), 1);
    assert_eq!(page.events[0].correlation_id, Some("corr_008".to_owned()));
    assert_eq!(
        page.events[0].kind,
        EventKind::GatewayActionDispatched {
            action: "cancel".into()
        }
    );
}

#[tokio::test]
async fn action_handler_publishes_rejected_event_to_journal() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let dispatch = action_dispatch(ActionKind::Retry, "COE-255", "corr_009");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Rejected);
    let cursor = opensymphony::opensymphony_gateway_schema::cursor::StreamCursor::new(0, "events");
    let page = journal.query_after(&cursor, 10).await.expect("query");
    assert_eq!(page.events.len(), 1);
    assert_eq!(page.events[0].correlation_id, Some("corr_009".to_owned()));
    assert!(matches!(
        page.events[0].kind,
        EventKind::GatewayActionFailed { .. }
    ));
}

#[tokio::test]
async fn action_handler_rejects_duplicate_idempotency_key() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let mut dispatch = action_dispatch(ActionKind::Cancel, "COE-255", "corr_010");
    dispatch.idempotency_key = Some("key_001".to_owned());
    let receipt1 = handler.dispatch(dispatch.clone(), &envelope).await;
    assert_eq!(receipt1.status, ActionStatus::Accepted);
    let receipt2 = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt2.status, ActionStatus::Rejected);
    assert!(
        receipt2
            .reason
            .as_ref()
            .expect("should not be None")
            .contains("duplicate idempotency key")
    );
}

#[tokio::test]
async fn action_handler_returns_expected_followups_for_retry() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Completed,
        WorkerOutcome::Failed,
    );
    let dispatch = action_dispatch(ActionKind::Retry, "COE-255", "corr_011");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Accepted);
    use opensymphony::opensymphony_gateway_schema::action::ExpectedFollowup;
    assert!(
        receipt
            .expected_followup
            .iter()
            .any(|f| matches!(f, ExpectedFollowup::ActionCompletion))
    );
    assert!(
        receipt
            .expected_followup
            .iter()
            .any(|f| matches!(f, ExpectedFollowup::RunLifecycle))
    );
}

#[tokio::test]
async fn action_handler_comment_is_accepted_on_any_state() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    for (state, outcome) in [
        (IssueRuntimeState::Running, WorkerOutcome::Running),
        (IssueRuntimeState::Idle, WorkerOutcome::Unknown),
        (IssueRuntimeState::Completed, WorkerOutcome::Completed),
        (IssueRuntimeState::Failed, WorkerOutcome::Failed),
    ] {
        let envelope = fixture_envelope("COE-255", state, outcome);
        let dispatch = action_dispatch(ActionKind::Comment, "COE-255", "corr_comment");
        let receipt = handler.dispatch(dispatch, &envelope).await;
        assert_eq!(
            receipt.status,
            ActionStatus::Accepted,
            "comment should be accepted in {:?}",
            state
        );
    }
}

#[tokio::test]
async fn action_handler_pause_rejected_on_non_running_issue() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope("COE-255", IssueRuntimeState::Idle, WorkerOutcome::Unknown);
    let dispatch = action_dispatch(ActionKind::Pause, "COE-255", "corr_pause");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Rejected);
    assert!(
        receipt
            .reason
            .as_ref()
            .expect("should not be None")
            .contains("pause only valid on a running issue")
    );
}

#[tokio::test]
async fn action_handler_resume_rejected_on_non_paused_issue() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let dispatch = action_dispatch(ActionKind::Resume, "COE-255", "corr_resume");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Rejected);
    assert!(
        receipt
            .reason
            .as_ref()
            .expect("should not be None")
            .contains("resume only valid on a paused issue")
    );
}

#[tokio::test]
async fn action_handler_resume_accepted_on_paused_issue() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope("COE-255", IssueRuntimeState::Paused, WorkerOutcome::Running);
    let dispatch = action_dispatch(ActionKind::Resume, "COE-255", "corr_resume_ok");
    let receipt = handler.dispatch(dispatch, &envelope).await;
    assert_eq!(receipt.status, ActionStatus::Accepted);
    assert!(receipt.reason.is_none());
}

/// Concurrent idempotency test: two tasks with the same idempotency key
/// should result in exactly one accepted receipt and one rejected receipt.
#[tokio::test]
async fn action_handler_concurrent_idempotency_only_one_accepted() {
    let journal = InMemoryEventJournal::new(1000, 64);
    let handler = ActionHandler::new(journal.clone());
    let envelope = fixture_envelope(
        "COE-255",
        IssueRuntimeState::Running,
        WorkerOutcome::Running,
    );
    let mut dispatch = action_dispatch(ActionKind::Cancel, "COE-255", "corr_concurrent");
    dispatch.idempotency_key = Some("concurrent_key_001".to_owned());

    let h1 = handler.clone();
    let h2 = handler.clone();
    let env1 = envelope.clone();
    let env2 = envelope.clone();
    let d1 = dispatch.clone();
    let d2 = dispatch.clone();

    let (r1, r2) = tokio::join!(h1.dispatch(d1, &env1), h2.dispatch(d2, &env2),);

    let statuses = vec![r1.status, r2.status];
    assert!(
        statuses.contains(&ActionStatus::Accepted),
        "exactly one accepted receipt expected; got {:?}",
        statuses
    );
    assert!(
        statuses.contains(&ActionStatus::Rejected),
        "exactly one rejected receipt expected; got {:?}",
        statuses
    );

    // Verify exactly one audit event was appended to the journal (no
    // duplicate-race event from the rejected dispatch).
    let cursor = opensymphony::opensymphony_gateway_schema::cursor::StreamCursor::new(0, "events");
    let page = journal.query_after(&cursor, 10).await.expect("query");
    assert_eq!(
        page.events.len(),
        1,
        "journal should contain exactly one event after concurrent dispatch"
    );
}