Skip to content

Commit 2cd5f15

Browse files
committed
fix(api): reconcile watch resume rebase
Signed-off-by: John Myers <[email protected]>
1 parent 64d8976 commit 2cd5f15

8 files changed

Lines changed: 6955 additions & 4244 deletions

File tree

‎crates/openshell-sdk/src/error.rs‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ pub enum SdkError {
111111
/// the sandbox log files.
112112
#[error("out of range: {message}")]
113113
#[diagnostic(code(openshell::sdk::out_of_range))]
114-
OutOfRange { message: String },
114+
OutOfRange {
115+
/// Error message.
116+
message: String,
117+
/// Original gateway status, including details and metadata.
118+
status: Box<tonic::Status>,
119+
},
115120
}
116121

117122
impl SdkError {
@@ -163,7 +168,7 @@ impl SdkError {
163168
match code {
164169
tonic::Code::NotFound => Self::NotFound { message, status },
165170
tonic::Code::AlreadyExists => Self::AlreadyExists { message, status },
166-
tonic::Code::OutOfRange => Self::OutOfRange { message },
171+
tonic::Code::OutOfRange => Self::OutOfRange { message, status },
167172
tonic::Code::InvalidArgument => Self::InvalidConfig {
168173
message,
169174
status: Some(status),
@@ -187,6 +192,7 @@ impl SdkError {
187192
Self::InvalidConfig { status, .. } | Self::Auth { status, .. } => status.as_deref(),
188193
Self::NotFound { status, .. }
189194
| Self::AlreadyExists { status, .. }
195+
| Self::OutOfRange { status, .. }
190196
| Self::Rpc { status, .. } => Some(status),
191197
_ => None,
192198
}
@@ -205,13 +211,6 @@ impl SdkError {
205211
self.error_details()?.retry_info()?.retry_delay
206212
}
207213

208-
/// Create an `OutOfRange` error.
209-
pub fn out_of_range(message: impl Into<String>) -> Self {
210-
Self::OutOfRange {
211-
message: message.into(),
212-
}
213-
}
214-
215214
/// Stable string code for cross-language binding consumers.
216215
///
217216
/// Returns one of: `invalid_config`, `tls`, `connect`, `auth`, `io`,

‎crates/openshell-sdk/src/types.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,16 @@ impl From<proto::SandboxLogLine> for LogLine {
159159
};
160160
Self {
161161
sandbox_id: value.sandbox_id,
162-
timestamp_ms: value.timestamp_ms,
162+
// The wire contract carries `google.protobuf.Timestamp`; these
163+
// curated types stay dependency-light and expose milliseconds, the
164+
// same reduction the CLI applies at its own presentation edge. An
165+
// absent or unrepresentable timestamp reads as 0, which is what
166+
// this field meant before the wire types gained presence.
167+
timestamp_ms: value
168+
.event_time
169+
.as_ref()
170+
.and_then(|time| openshell_core::time::timestamp_to_millis(time).ok())
171+
.unwrap_or(0),
163172
level: value.level,
164173
target: value.target,
165174
message: value.message,
@@ -172,7 +181,11 @@ impl From<proto::SandboxLogLine> for LogLine {
172181
impl From<proto::PlatformEvent> for PlatformEvent {
173182
fn from(value: proto::PlatformEvent) -> Self {
174183
Self {
175-
timestamp_ms: value.timestamp_ms,
184+
timestamp_ms: value
185+
.event_time
186+
.as_ref()
187+
.and_then(|time| openshell_core::time::timestamp_to_millis(time).ok())
188+
.unwrap_or(0),
176189
source: value.source,
177190
r#type: value.r#type,
178191
reason: value.reason,

‎crates/openshell-sdk/tests/client_mock.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use openshell_core::proto::open_shell_server::{OpenShell, OpenShellServer};
1313
use openshell_sdk::{
1414
AuthConfig, ClientConfig, ExecOptions, ListOptions, OpenShellClient, Refresh, RefreshError,
1515
RefreshedToken, SandboxPhase, SandboxSpec, SandboxTemplateCreateSpec,
16-
SandboxTemplateListOptions, ServiceExposure, ServiceStatus as SdkServiceStatus,
17-
WatchEvent, WatchOptions,
16+
SandboxTemplateListOptions, ServiceExposure, ServiceStatus as SdkServiceStatus, WatchEvent,
17+
WatchOptions,
1818
};
1919
use std::collections::HashMap;
2020
use std::sync::Arc;
@@ -151,7 +151,7 @@ fn log_event(seq: u64, msg: &str) -> proto::SandboxStreamEvent {
151151
payload: Some(proto::sandbox_stream_event::Payload::Log(
152152
proto::SandboxLogLine {
153153
sandbox_id: "id-my-box".into(),
154-
timestamp_ms: 0,
154+
event_time: None,
155155
level: "INFO".into(),
156156
target: "t".into(),
157157
message: msg.into(),

‎crates/openshell-server/src/grpc/sandbox.rs‎

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@ pub(super) async fn handle_watch_sandbox(
19911991
// water. Marking every event the phase examined, not only the
19921992
// ones that survived the filters, keeps a filtered event's live
19931993
// duplicate suppressed: the live loop does not re-apply
1994-
// `log_since_ms` and would otherwise let it through.
1994+
// `log_since_time` and would otherwise let it through.
19951995
let mut merged: Vec<CursoredEvent> = Vec::new();
19961996
if let Some(Ok(v)) = log_replay {
19971997
if let Some(last) = v.last() {
@@ -3220,7 +3220,7 @@ async fn stream_exec_over_relay(
32203220
)),
32213221
}))
32223222
.await;
3223-
let _ = proxy_task.await;
3223+
finish_interactive_exec_proxy(proxy_task).await;
32243224
return Ok(());
32253225
}
32263226
} else {
@@ -3230,12 +3230,12 @@ async fn stream_exec_over_relay(
32303230
let exit_code = match exec_result {
32313231
Ok(code) => code,
32323232
Err(status) => {
3233-
let _ = proxy_task.await;
3233+
finish_interactive_exec_proxy(proxy_task).await;
32343234
return Err(status);
32353235
}
32363236
};
32373237

3238-
let _ = proxy_task.await;
3238+
finish_interactive_exec_proxy(proxy_task).await;
32393239

32403240
let _ = tx
32413241
.send(Ok(ExecSandboxEvent {
@@ -3301,7 +3301,7 @@ async fn stream_interactive_exec_over_relay(
33013301
)),
33023302
}))
33033303
.await;
3304-
finish_interactive_exec_proxy(proxy_task).await;
3304+
let _ = proxy_task.await;
33053305
return Ok(());
33063306
}
33073307
} else {
@@ -3311,12 +3311,12 @@ async fn stream_interactive_exec_over_relay(
33113311
let exit_code = match exec_result {
33123312
Ok(code) => code,
33133313
Err(status) => {
3314-
finish_interactive_exec_proxy(proxy_task).await;
3314+
let _ = proxy_task.await;
33153315
return Err(status);
33163316
}
33173317
};
33183318

3319-
finish_interactive_exec_proxy(proxy_task).await;
3319+
let _ = proxy_task.await;
33203320

33213321
let _ = tx
33223322
.send(Ok(ExecSandboxEvent {
@@ -4305,7 +4305,7 @@ mod tests {
43054305
.tracing_log_bus
43064306
.publish_external(openshell_core::proto::SandboxLogLine {
43074307
sandbox_id: sandbox_id.to_string(),
4308-
timestamp_ms: i as i64,
4308+
event_time: openshell_core::time::timestamp_from_millis(i as i64).ok(),
43094309
level: "INFO".to_string(),
43104310
target: "test".to_string(),
43114311
message: format!("line {i}"),
@@ -4321,7 +4321,7 @@ mod tests {
43214321
SandboxStreamEvent {
43224322
payload: Some(openshell_core::proto::sandbox_stream_event::Payload::Event(
43234323
openshell_core::proto::PlatformEvent {
4324-
timestamp_ms: 0,
4324+
event_time: openshell_core::time::timestamp_from_millis(0).ok(),
43254325
source: "test".to_string(),
43264326
r#type: "Normal".to_string(),
43274327
reason: reason.to_string(),
@@ -4373,7 +4373,8 @@ mod tests {
43734373
let response = handle_watch_sandbox(
43744374
&state,
43754375
authed_request(WatchSandboxRequest {
4376-
id: id.clone(),
4376+
sandbox: sandbox.object_name().to_string(),
4377+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
43774378
follow_logs: true,
43784379
resume_after_cursor: cursor_token(&state, &id, 1),
43794380
..Default::default()
@@ -4413,7 +4414,7 @@ mod tests {
44134414
.tracing_log_bus
44144415
.publish_external(openshell_core::proto::SandboxLogLine {
44154416
sandbox_id: id.clone(),
4416-
timestamp_ms: 3,
4417+
event_time: openshell_core::time::timestamp_from_millis(3).ok(),
44174418
level: "INFO".to_string(),
44184419
target: "test".to_string(),
44194420
message: "line 3".to_string(),
@@ -4425,7 +4426,8 @@ mod tests {
44254426
let response = handle_watch_sandbox(
44264427
&state,
44274428
authed_request(WatchSandboxRequest {
4428-
id: id.clone(),
4429+
sandbox: sandbox.object_name().to_string(),
4430+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
44294431
follow_logs: true,
44304432
follow_events: true,
44314433
resume_after_cursor: cursor_token(&state, &id, 1),
@@ -4470,7 +4472,7 @@ mod tests {
44704472
.tracing_log_bus
44714473
.publish_external(openshell_core::proto::SandboxLogLine {
44724474
sandbox_id: id.clone(),
4473-
timestamp_ms: 3,
4475+
event_time: openshell_core::time::timestamp_from_millis(3).ok(),
44744476
level: "INFO".to_string(),
44754477
target: "test".to_string(),
44764478
message: "line 3".to_string(),
@@ -4482,7 +4484,8 @@ mod tests {
44824484
let response = handle_watch_sandbox(
44834485
&state,
44844486
authed_request(WatchSandboxRequest {
4485-
id: id.clone(),
4487+
sandbox: sandbox.object_name().to_string(),
4488+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
44864489
follow_logs: true,
44874490
follow_events: true,
44884491
// event_tail has no default; 0 would replay no platform events.
@@ -4516,7 +4519,8 @@ mod tests {
45164519
let response = handle_watch_sandbox(
45174520
&state,
45184521
authed_request(WatchSandboxRequest {
4519-
id: id.clone(),
4522+
sandbox: sandbox.object_name().to_string(),
4523+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
45204524
follow_logs: true,
45214525
follow_events: true,
45224526
..Default::default()
@@ -4591,7 +4595,8 @@ mod tests {
45914595
let response = handle_watch_sandbox(
45924596
&state,
45934597
authed_request(WatchSandboxRequest {
4594-
id: id.clone(),
4598+
sandbox: sandbox.object_name().to_string(),
4599+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
45954600
follow_logs: true,
45964601
follow_events: true,
45974602
..Default::default()
@@ -4690,7 +4695,8 @@ mod tests {
46904695
let response = handle_watch_sandbox(
46914696
&state,
46924697
authed_request(WatchSandboxRequest {
4693-
id: id.clone(),
4698+
sandbox: sandbox.object_name().to_string(),
4699+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
46944700
follow_logs: true,
46954701
follow_events: true,
46964702
resume_after_cursor: last_cursor,
@@ -4739,7 +4745,8 @@ mod tests {
47394745
let response = handle_watch_sandbox(
47404746
&state,
47414747
authed_request(WatchSandboxRequest {
4742-
id: id.clone(),
4748+
sandbox: sandbox.object_name().to_string(),
4749+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
47434750
follow_logs: true,
47444751
follow_events: true,
47454752
..Default::default()
@@ -4781,7 +4788,8 @@ mod tests {
47814788
let response = handle_watch_sandbox(
47824789
&state,
47834790
authed_request(WatchSandboxRequest {
4784-
id: id.clone(),
4791+
sandbox: sandbox.object_name().to_string(),
4792+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
47854793
follow_logs: true,
47864794
resume_after_cursor: cursor_token(&state, &id, 3),
47874795
..Default::default()
@@ -4817,7 +4825,8 @@ mod tests {
48174825
let response = handle_watch_sandbox(
48184826
&state,
48194827
authed_request(WatchSandboxRequest {
4820-
id: id.clone(),
4828+
sandbox: sandbox.object_name().to_string(),
4829+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
48214830
follow_logs: true,
48224831
// Seq 2 was trimmed; this is an unrecoverable gap.
48234832
resume_after_cursor: cursor_token(&state, &id, 2),
@@ -4911,7 +4920,8 @@ mod tests {
49114920
let response = handle_watch_sandbox(
49124921
&state,
49134922
authed_request(WatchSandboxRequest {
4914-
id: id.clone(),
4923+
sandbox: sandbox.object_name().to_string(),
4924+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
49154925
follow_logs: true,
49164926
resume_after_cursor: retired_cursor,
49174927
..Default::default()
@@ -4960,7 +4970,8 @@ mod tests {
49604970
let response = handle_watch_sandbox(
49614971
&state,
49624972
authed_request(WatchSandboxRequest {
4963-
id: id.clone(),
4973+
sandbox: sandbox.object_name().to_string(),
4974+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
49644975
follow_logs: true,
49654976
resume_after_cursor: retired_cursor,
49664977
..Default::default()
@@ -4996,7 +5007,8 @@ mod tests {
49965007
let response = handle_watch_sandbox(
49975008
&state,
49985009
authed_request(WatchSandboxRequest {
4999-
id: id.clone(),
5010+
sandbox: sandbox.object_name().to_string(),
5011+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
50005012
follow_logs: true,
50015013
resume_after_cursor: retired_cursor,
50025014
..Default::default()
@@ -5031,7 +5043,8 @@ mod tests {
50315043
let response = handle_watch_sandbox(
50325044
&state,
50335045
authed_request(WatchSandboxRequest {
5034-
id: id.clone(),
5046+
sandbox: sandbox.object_name().to_string(),
5047+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
50355048
follow_logs: true,
50365049
resume_after_cursor: retired_cursor,
50375050
..Default::default()
@@ -5068,7 +5081,8 @@ mod tests {
50685081
let response = handle_watch_sandbox(
50695082
&state,
50705083
authed_request(WatchSandboxRequest {
5071-
id: b_id.clone(),
5084+
sandbox: b.object_name().to_string(),
5085+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
50725086
follow_logs: true,
50735087
resume_after_cursor: a_cursor,
50745088
..Default::default()
@@ -5102,7 +5116,8 @@ mod tests {
51025116
let response = handle_watch_sandbox(
51035117
&state,
51045118
authed_request(WatchSandboxRequest {
5105-
id: id.clone(),
5119+
sandbox: sandbox.object_name().to_string(),
5120+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
51065121
follow_logs: true,
51075122
resume_after_cursor: ahead,
51085123
..Default::default()
@@ -5133,7 +5148,8 @@ mod tests {
51335148
let err = handle_watch_sandbox(
51345149
&state,
51355150
authed_request(WatchSandboxRequest {
5136-
id: id.clone(),
5151+
sandbox: sandbox.object_name().to_string(),
5152+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
51375153
follow_logs: true,
51385154
resume_after_cursor: raw.to_string(),
51395155
..Default::default()
@@ -5167,7 +5183,8 @@ mod tests {
51675183
let response = handle_watch_sandbox(
51685184
&state,
51695185
authed_request(WatchSandboxRequest {
5170-
id: id.clone(),
5186+
sandbox: sandbox.object_name().to_string(),
5187+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
51715188
follow_logs: true,
51725189
resume_after_cursor: foreign_cursor(1),
51735190
..Default::default()
@@ -5198,7 +5215,8 @@ mod tests {
51985215
let response = handle_watch_sandbox(
51995216
&state,
52005217
authed_request(WatchSandboxRequest {
5201-
id: id.clone(),
5218+
sandbox: sandbox.object_name().to_string(),
5219+
workspace_scope: Some(openshell_core::proto::workspace_selector("default")),
52025220
follow_logs: true,
52035221
..Default::default()
52045222
}),
@@ -5214,7 +5232,7 @@ mod tests {
52145232
.tracing_log_bus
52155233
.publish_external(openshell_core::proto::SandboxLogLine {
52165234
sandbox_id: id.clone(),
5217-
timestamp_ms: i64::from(i),
5235+
event_time: openshell_core::time::timestamp_from_millis(i64::from(i)).ok(),
52185236
level: "INFO".to_string(),
52195237
target: "test".to_string(),
52205238
message: format!("line {i}"),

0 commit comments

Comments
 (0)