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
use std::path::PathBuf;
use pretty_assertions::assert_eq;
use rho_protocol as wire;
use rho_sdk::{
model::{ContentBlock, ModelResponse},
provider::ScriptedTurn,
};
use super::{
super::super::{engine::Engine, pull_request_wake::TEST_PULL_REQUEST, test_sessions::Load},
Fixture, Told,
};
use crate::{
app::{daemon_host::jobs::JobRecord, pull_requests::LinkOrigin, starter_report::ReportKind},
session::SessionMark,
};
/// Who sends one of a reporting session's prompts.
#[derive(Clone, Copy)]
enum Prompter {
/// The person, through a client.
Person,
/// Its starter's agent, through session control.
Lead,
}
/// What stands before a reporting session's turns.
#[derive(Clone, Copy)]
enum Before {
Nothing,
/// A pull request linked to it is open.
OpenPullRequest,
/// A subagent run it started is still in progress.
StartedInProgress,
/// It was marked done ahead of every save.
MarkedDone,
}
/// What a reporting session's mark reads as once its turns ended.
#[derive(Debug, PartialEq)]
enum Reads {
/// Marked done anew, at or after its last save: finished.
Done,
/// Not marked: active.
Active,
}
// Covers: a session that reports to its starter, as an orchestrator that
// links no pull request of its own, is marked done when it reports `done`,
// at or after its last save, also while a job it started is still in
// progress, but not while a pull request linked to it is left open; a
// report of another kind leaves it active, and so does one that follows a
// `done` report, which clears the done mark, though a lead sent the
// prompt. A lead's prompt whose turn reports nothing keeps a finished
// session finished, marked anew. A daemon that settles only on merged pull
// requests leaves the first and the last active; one that holds a report
// back for a started job leaves the second active, as an orchestrator
// whose worker's turn still runs would stay for good; one that ignores the
// links hides the third; one that leaves the mark to `updated_at` keeps
// the fifth done when both turns end in one second.
// Owner: daemon session marks (a reporting session's).
#[tokio::test]
async fn a_reporting_session_is_done_when_it_reports_done() {
use ReportKind::{Done, NeedsDecision};
const LINKED_AT: u64 = 1_700_000_000_000;
let lead = wire::SessionGrant {
depth: 1,
access: wire::GrantAccess::Write,
workspaces: wire::GrantWorkspaces::Any,
};
let cases = [
(
"done",
Before::Nothing,
vec![(Prompter::Person, Some(Done))],
),
(
"done, a job it started in progress",
Before::StartedInProgress,
vec![(Prompter::Person, Some(Done))],
),
(
"done, a pull request open",
Before::OpenPullRequest,
vec![(Prompter::Person, Some(Done))],
),
(
"needs a decision",
Before::Nothing,
vec![(Prompter::Person, Some(NeedsDecision))],
),
(
"done, then needs a decision",
Before::Nothing,
vec![
(Prompter::Person, Some(Done)),
(Prompter::Lead, Some(NeedsDecision)),
],
),
(
"finished, then a lead's silent turn",
Before::MarkedDone,
vec![(Prompter::Lead, None)],
),
];
let mut seen = Vec::new();
for (name, before, turns) in cases {
let answers = turns.iter().map(|_| {
ScriptedTurn::completed(ModelResponse::Assistant(vec![ContentBlock::Text(
"the answer".into(),
)]))
});
let fixture = Fixture::answering(Told::AtTheNextPrompt, Load::Reporting, answers).await;
// Ahead of every save, so a mark made again differs from it.
let ahead = fixture.listed().updated_at + 3600;
match before {
Before::Nothing => {}
Before::OpenPullRequest => {
fixture.forge.set_open(TEST_PULL_REQUEST, &[], &[]);
fixture
.inbox()
.link(TEST_PULL_REQUEST, LinkOrigin::Agent, LINKED_AT)
.await
.unwrap();
}
Before::StartedInProgress => {
let run = JobRecord::new_subagent(
fixture.session.job.clone(),
PathBuf::from("/work"),
/*session*/ None,
"run".into(),
/*worktree*/ None,
);
fixture.sessions.jobs.register(run).unwrap();
}
Before::MarkedDone => {
let done = Some(SessionMark::Done { at: ahead });
fixture
.sessions
.change_saved_mark(&fixture.session.id, move |_, _| done)
.await
.unwrap();
}
}
for (from, report) in &turns {
let grant = match from {
Prompter::Person => None,
Prompter::Lead => Some(lead.clone()),
};
// As its `report_to_starter` call marks it in the turn: the
// turn starts with it, and its end takes it. The tool itself
// runs in the daemon end-to-end test.
if let Some(kind) = report {
let engine = fixture.session.engine.lock().await;
let Some(Engine::Rho(runtime)) = &*engine else {
panic!("a Rho session");
};
runtime.starter_report().expect("it reports").mark(*kind);
}
fixture.prompt_with(grant).await;
}
let listed = fixture.listed();
let reads = match listed.mark {
Some(SessionMark::Done { at }) if at >= listed.updated_at && at != ahead => Reads::Done,
None => Reads::Active,
mark => panic!("{name}: marked {mark:?}, updated at {}", listed.updated_at),
};
seen.push((name, reads));
}
assert_eq!(
seen,
[
("done", Reads::Done),
("done, a job it started in progress", Reads::Done),
("done, a pull request open", Reads::Active),
("needs a decision", Reads::Active),
("done, then needs a decision", Reads::Active),
("finished, then a lead's silent turn", Reads::Done),
]
);
}