Skip to content

Commit 0b51b2c

Browse files
fix(proto): preserve exact endpoint and profile times
Signed-off-by: Derek Carr <[email protected]>
1 parent 6574df5 commit 0b51b2c

28 files changed

Lines changed: 878 additions & 464 deletions

File tree

‎architecture/gateway.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ than extending the frozen message.
368368

369369
| Storage classification | Protobuf messages | Durable use |
370370
|---|---|---|
371-
| Encoded storage roots | `StoredProviderCredentialRefreshState`, `StoredProviderProfile`, `PolicyRevisionPayload`, `DraftChunkPayload` | Complete protobuf payload stored in an object row or a scoped policy row. |
371+
| Encoded storage roots | `StoredProviderCredentialRefreshStateV2`, `StoredProviderProfile`, `PolicyRevisionPayload`, `DraftChunkPayload` | Complete protobuf payload stored in an object row or a scoped policy row. The frozen V1 refresh state remains available only for transactional upgrade decoding. |
372372
| Nested storage-only type | `StoredRefreshMaterialDeletion` | Repeated child records inside provider refresh state. |
373373
| SQL materializations | `StoredPolicyRevision`, `StoredDraftChunk` | Server-only typed results assembled from indexed columns and decoded payloads; not public RPC messages. |
374374
| Public messages used directly as encoded storage roots | `Sandbox`, `SandboxWorkloadTemplate`, `Provider`, `Workspace`, `WorkspaceMember`, `SshSession`, `ServiceEndpoint` | The generated public type is also the persisted payload. `SshSession` is not in the current public RPC message closure. |

‎crates/openshell-cli/src/run.rs‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,7 @@ fn sandbox_to_json(sandbox: &Sandbox) -> serde_json::Value {
24932493
"ports": endpoint.ports,
24942494
"path": endpoint.path,
24952495
"last_result": endpoint_result_name(endpoint.last_result()),
2496-
"last_reported_at": endpoint.last_reported_at,
2496+
"last_reported_at": endpoint.last_reported_time.as_ref().map(ToString::to_string).unwrap_or_default(),
24972497
})
24982498
})
24992499
.collect::<Vec<_>>()
@@ -2586,8 +2586,11 @@ fn endpoint_status_display_lines(endpoint: &EndpointStatus) -> Vec<String> {
25862586
EndpointResult::UpstreamRejected => "Server rejected the request (HTTP 400 or higher).",
25872587
};
25882588
// The gateway supplies acceptance time, which can follow the actual
2589-
// exchange. An empty timestamp means there is no accepted observation.
2590-
let reported_at = non_empty_or(&endpoint.last_reported_at, "no report yet");
2589+
// exchange. An absent timestamp means there is no accepted observation.
2590+
let reported_at = endpoint
2591+
.last_reported_time
2592+
.as_ref()
2593+
.map_or_else(|| "no report yet".to_string(), ToString::to_string);
25912594
vec![
25922595
format!(
25932596
"{} (ports: {ports}; path: {})",
@@ -7377,7 +7380,7 @@ mod tests {
73777380
ports: vec![443, 8443],
73787381
path: "/mcp".to_string(),
73797382
last_result: EndpointResult::TransportFailed as i32,
7380-
last_reported_at: "2026-09-05T10:01:00Z".to_string(),
7383+
last_reported_time: Some("2026-09-05T10:01:00Z".parse().unwrap()),
73817384
}],
73827385
conditions: vec![SandboxCondition {
73837386
r#type: "Ready".to_string(),
@@ -7438,7 +7441,7 @@ mod tests {
74387441
ports: vec![443, 8443],
74397442
path: "/mcp".to_string(),
74407443
last_result: EndpointResult::TransportFailed as i32,
7441-
last_reported_at: "2026-09-05T11:01:00Z".to_string(),
7444+
last_reported_time: Some("2026-09-05T11:01:00Z".parse().unwrap()),
74427445
};
74437446

74447447
assert_eq!(
@@ -7459,7 +7462,7 @@ mod tests {
74597462
ports: vec![443],
74607463
path: "/**".to_string(),
74617464
last_result: EndpointResult::NoObservedExchange as i32,
7462-
last_reported_at: String::new(),
7465+
last_reported_time: None,
74637466
};
74647467

74657468
assert_eq!(
@@ -7499,7 +7502,7 @@ mod tests {
74997502
ports: vec![443],
75007503
path: "/mcp".to_string(),
75017504
last_result: EndpointResult::HttpResponseReceived as i32,
7502-
last_reported_at: "2026-09-05T11:01:00Z".to_string(),
7505+
last_reported_time: Some("2026-09-05T11:01:00Z".parse().unwrap()),
75037506
..Default::default()
75047507
};
75057508
assert_eq!(

‎crates/openshell-core/src/endpoint_status.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn initial_endpoint_status(
8484
ports,
8585
path: path.to_string(),
8686
last_result: crate::proto::EndpointResult::NoObservedExchange.into(),
87-
last_reported_at: String::new(),
87+
last_reported_time: None,
8888
}
8989
}
9090

@@ -672,7 +672,7 @@ mod tests {
672672
descriptor.last_result,
673673
crate::proto::EndpointResult::NoObservedExchange as i32,
674674
);
675-
assert!(descriptor.last_reported_at.is_empty());
675+
assert!(descriptor.last_reported_time.is_none());
676676

677677
let reconstructed = endpoint(
678678
&descriptor.host,

‎crates/openshell-providers/src/profiles.rs‎

Lines changed: 183 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub struct CredentialProfile {
122122
}
123123

124124
/// Origin-aware protobuf duration presence retained across profile conversion.
125-
#[derive(Debug, Clone, Copy, Default)]
125+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
126126
#[doc(hidden)]
127127
pub enum ProfileDurationWkt {
128128
/// The profile came from YAML and uses the compatibility seconds field.
@@ -138,6 +138,46 @@ impl ProfileDurationWkt {
138138
fn from_proto(value: Option<prost_types::Duration>) -> Self {
139139
value.map_or(Self::Absent, Self::Present)
140140
}
141+
142+
pub fn to_proto(self, legacy_seconds: i64) -> Option<prost_types::Duration> {
143+
match self {
144+
Self::FromYaml => (legacy_seconds != 0).then_some(prost_types::Duration {
145+
seconds: legacy_seconds,
146+
nanos: 0,
147+
}),
148+
Self::Absent => None,
149+
Self::Present(value) => Some(value),
150+
}
151+
}
152+
}
153+
154+
impl Serialize for ProfileDurationWkt {
155+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
156+
where
157+
S: Serializer,
158+
{
159+
match self {
160+
Self::Present(value) => serializer.serialize_str(&value.to_string()),
161+
Self::FromYaml | Self::Absent => serializer.serialize_none(),
162+
}
163+
}
164+
}
165+
166+
impl<'de> Deserialize<'de> for ProfileDurationWkt {
167+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
168+
where
169+
D: Deserializer<'de>,
170+
{
171+
let value = String::deserialize(deserializer)?;
172+
value
173+
.parse::<prost_types::Duration>()
174+
.map(Self::Present)
175+
.map_err(de::Error::custom)
176+
}
177+
}
178+
179+
fn profile_duration_is_legacy_or_absent(value: &ProfileDurationWkt) -> bool {
180+
!matches!(value, ProfileDurationWkt::Present(_))
141181
}
142182

143183
#[derive(Debug, Clone, Deserialize, Serialize)]
@@ -161,7 +201,11 @@ pub struct TokenGrantProfile {
161201
#[serde(default, skip_serializing_if = "is_zero_i64")]
162202
pub cache_ttl_seconds: i64,
163203
/// Exact protobuf value retained for lossless gRPC import/export.
164-
#[serde(skip)]
204+
#[serde(
205+
default,
206+
rename = "cache_ttl",
207+
skip_serializing_if = "profile_duration_is_legacy_or_absent"
208+
)]
165209
#[doc(hidden)]
166210
pub cache_ttl_wkt: ProfileDurationWkt,
167211
#[serde(default, skip_serializing_if = "Vec::is_empty")]
@@ -225,13 +269,21 @@ pub struct CredentialRefreshProfile {
225269
#[serde(default, skip_serializing_if = "is_zero_i64")]
226270
pub refresh_before_seconds: i64,
227271
/// Exact protobuf value retained for lossless gRPC import/export.
228-
#[serde(skip)]
272+
#[serde(
273+
default,
274+
rename = "refresh_before",
275+
skip_serializing_if = "profile_duration_is_legacy_or_absent"
276+
)]
229277
#[doc(hidden)]
230278
pub refresh_before_wkt: ProfileDurationWkt,
231279
#[serde(default, skip_serializing_if = "is_zero_i64")]
232280
pub max_lifetime_seconds: i64,
233281
/// Exact protobuf value retained for lossless gRPC import/export.
234-
#[serde(skip)]
282+
#[serde(
283+
default,
284+
rename = "max_lifetime",
285+
skip_serializing_if = "profile_duration_is_legacy_or_absent"
286+
)]
235287
#[doc(hidden)]
236288
pub max_lifetime_wkt: ProfileDurationWkt,
237289
#[serde(default, skip_serializing_if = "Vec::is_empty")]
@@ -1323,9 +1375,9 @@ fn credential_refresh_from_proto(refresh: &ProviderCredentialRefresh) -> Credent
13231375
.unwrap_or(ProviderCredentialRefreshStrategy::Unspecified),
13241376
token_url: refresh.token_url.clone(),
13251377
scopes: refresh.scopes.clone(),
1326-
refresh_before_seconds: duration_seconds_for_runtime(refresh.refresh_before.as_ref()),
1378+
refresh_before_seconds: 0,
13271379
refresh_before_wkt: ProfileDurationWkt::from_proto(refresh.refresh_before),
1328-
max_lifetime_seconds: duration_seconds_for_runtime(refresh.max_lifetime.as_ref()),
1380+
max_lifetime_seconds: 0,
13291381
max_lifetime_wkt: ProfileDurationWkt::from_proto(refresh.max_lifetime),
13301382
material: refresh
13311383
.material
@@ -1348,26 +1400,11 @@ fn credential_refresh_from_proto(refresh: &ProviderCredentialRefresh) -> Credent
13481400
}
13491401
}
13501402

1351-
fn duration_seconds_for_runtime(value: Option<&prost_types::Duration>) -> i64 {
1352-
let Some(value) = value else {
1353-
return 0;
1354-
};
1355-
openshell_core::time::duration_to_std(value).map_or(0, |duration| {
1356-
i64::try_from(duration.as_secs()).unwrap_or_default()
1357-
})
1358-
}
1359-
13601403
fn profile_duration_to_proto(
13611404
exact: ProfileDurationWkt,
13621405
seconds: i64,
13631406
) -> Option<prost_types::Duration> {
1364-
match exact {
1365-
ProfileDurationWkt::FromYaml => {
1366-
(seconds != 0).then_some(prost_types::Duration { seconds, nanos: 0 })
1367-
}
1368-
ProfileDurationWkt::Absent => None,
1369-
ProfileDurationWkt::Present(value) => Some(value),
1370-
}
1407+
exact.to_proto(seconds)
13711408
}
13721409

13731410
fn validate_profile_duration(value: &prost_types::Duration) -> Result<(), String> {
@@ -1423,7 +1460,7 @@ fn token_grant_from_proto(
14231460
jwt_svid_audience: token_grant.jwt_svid_audience.clone(),
14241461
client_assertion_type: token_grant.client_assertion_type.clone(),
14251462
scopes: token_grant.scopes.clone(),
1426-
cache_ttl_seconds: duration_seconds_for_runtime(token_grant.cache_ttl.as_ref()),
1463+
cache_ttl_seconds: 0,
14271464
cache_ttl_wkt: ProfileDurationWkt::from_proto(token_grant.cache_ttl),
14281465
audience_overrides: token_grant
14291466
.audience_overrides
@@ -2170,6 +2207,16 @@ pub fn validate_profile_set(
21702207
"refresh strategy is required",
21712208
));
21722209
}
2210+
if matches!(refresh.refresh_before_wkt, ProfileDurationWkt::Present(_))
2211+
&& refresh.refresh_before_seconds != 0
2212+
{
2213+
diagnostics.push(ProfileValidationDiagnostic::error(
2214+
source,
2215+
profile_id,
2216+
"credentials.refresh.refresh_before",
2217+
"refresh_before and refresh_before_seconds cannot both be set",
2218+
));
2219+
}
21732220
if let ProfileDurationWkt::Present(value) = refresh.refresh_before_wkt
21742221
&& let Err(error) = validate_profile_duration(&value)
21752222
{
@@ -2208,6 +2255,30 @@ pub fn validate_profile_set(
22082255
"max_lifetime_seconds must be greater than or equal to 0",
22092256
));
22102257
}
2258+
if matches!(refresh.max_lifetime_wkt, ProfileDurationWkt::Present(_))
2259+
&& refresh.max_lifetime_seconds != 0
2260+
{
2261+
diagnostics.push(ProfileValidationDiagnostic::error(
2262+
source,
2263+
profile_id,
2264+
"credentials.refresh.max_lifetime",
2265+
"max_lifetime and max_lifetime_seconds cannot both be set",
2266+
));
2267+
}
2268+
if matches!(
2269+
refresh.max_lifetime_wkt,
2270+
ProfileDurationWkt::Present(prost_types::Duration {
2271+
seconds: 0,
2272+
nanos: 0
2273+
})
2274+
) {
2275+
diagnostics.push(ProfileValidationDiagnostic::error(
2276+
source,
2277+
profile_id,
2278+
"credentials.refresh.max_lifetime",
2279+
"max_lifetime must be greater than zero when present",
2280+
));
2281+
}
22112282
let mut material_names = HashSet::new();
22122283
for material in &refresh.material {
22132284
let name = material.name.trim();
@@ -2379,6 +2450,16 @@ pub fn validate_profile_set(
23792450
"cache_ttl_seconds must be greater than or equal to 0",
23802451
));
23812452
}
2453+
if matches!(token_grant.cache_ttl_wkt, ProfileDurationWkt::Present(_))
2454+
&& token_grant.cache_ttl_seconds != 0
2455+
{
2456+
diagnostics.push(ProfileValidationDiagnostic::error(
2457+
source,
2458+
profile_id,
2459+
"credentials.token_grant.cache_ttl",
2460+
"cache_ttl and cache_ttl_seconds cannot both be set",
2461+
));
2462+
}
23822463
if let Err(message) = validate_token_grant_endpoint(&token_grant.token_endpoint) {
23832464
diagnostics.push(ProfileValidationDiagnostic::error(
23842465
source,
@@ -3470,6 +3551,80 @@ mod tests {
34703551
);
34713552
}
34723553

3554+
#[test]
3555+
fn protobuf_profile_durations_round_trip_through_yaml_exactly() {
3556+
let profile = parse_profile_yaml(
3557+
r"
3558+
id: exact-durations
3559+
display_name: Exact Durations
3560+
credentials:
3561+
- name: access_token
3562+
refresh:
3563+
strategy: static
3564+
token_grant:
3565+
token_endpoint: https://auth.example.com/token
3566+
",
3567+
)
3568+
.unwrap();
3569+
let mut proto = profile.to_proto();
3570+
let credential = &mut proto.credentials[0];
3571+
let refresh = credential.refresh.as_mut().unwrap();
3572+
refresh.refresh_before = Some(prost_types::Duration {
3573+
seconds: 0,
3574+
nanos: 0,
3575+
});
3576+
refresh.max_lifetime = Some(prost_types::Duration {
3577+
seconds: 1,
3578+
nanos: 500_000_000,
3579+
});
3580+
credential.token_grant.as_mut().unwrap().cache_ttl = Some(prost_types::Duration {
3581+
seconds: 0,
3582+
nanos: 500_000_000,
3583+
});
3584+
3585+
let exact = ProviderTypeProfile::from_proto(&proto);
3586+
let yaml = profile_to_yaml(&exact).unwrap();
3587+
assert!(yaml.contains("refresh_before: \"0s\""), "{yaml}");
3588+
assert!(yaml.contains("max_lifetime: \"1.500s\""), "{yaml}");
3589+
assert!(yaml.contains("cache_ttl: \"0.500s\""), "{yaml}");
3590+
assert!(!yaml.contains("refresh_before_seconds"));
3591+
assert!(!yaml.contains("max_lifetime_seconds"));
3592+
assert!(!yaml.contains("cache_ttl_seconds"));
3593+
3594+
let reparsed = parse_profile_yaml(&yaml).unwrap().to_proto();
3595+
assert_eq!(
3596+
reparsed.credentials[0].refresh,
3597+
proto.credentials[0].refresh
3598+
);
3599+
assert_eq!(
3600+
reparsed.credentials[0].token_grant,
3601+
proto.credentials[0].token_grant
3602+
);
3603+
}
3604+
3605+
#[test]
3606+
fn canonical_zero_max_lifetime_is_rejected() {
3607+
let profile = parse_profile_yaml(
3608+
r#"
3609+
id: zero-max-lifetime
3610+
display_name: Zero Max Lifetime
3611+
credentials:
3612+
- name: access_token
3613+
refresh:
3614+
strategy: oauth2_client_credentials
3615+
token_url: https://auth.example.com/token
3616+
max_lifetime: "0s"
3617+
"#,
3618+
)
3619+
.expect("profile should parse before semantic validation");
3620+
3621+
let diagnostics = validate_profile_set(&[("zero.yaml".to_string(), profile)]);
3622+
assert!(diagnostics.iter().any(|diagnostic| {
3623+
diagnostic.field == "credentials.refresh.max_lifetime"
3624+
&& diagnostic.message.contains("greater than zero")
3625+
}));
3626+
}
3627+
34733628
#[test]
34743629
fn builtin_agent_conversation_defaults_preserve_own_and_foreign_body_text() {
34753630
use openshell_core::proto::{StaticCredentialBinding, StaticCredentialEndpointBinding};
@@ -4679,15 +4834,15 @@ credentials:
46794834
);
46804835
assert_eq!(refresh.material.len(), 2);
46814836

4682-
let from_proto = ProviderTypeProfile::from_proto(&profile.to_proto());
4683-
assert_eq!(
4684-
from_proto.credentials[0].refresh,
4685-
profile.credentials[0].refresh
4686-
);
4837+
let proto = profile.to_proto();
4838+
let from_proto = ProviderTypeProfile::from_proto(&proto);
4839+
assert_eq!(from_proto.to_proto(), proto);
46874840

46884841
let exported = profile_to_yaml(&from_proto).expect("yaml");
46894842
assert!(exported.contains("oauth2_client_credentials"));
46904843
assert!(exported.contains("client_secret"));
4844+
let reparsed = parse_profile_yaml(&exported).expect("exported profile should parse");
4845+
assert_eq!(reparsed.to_proto(), proto);
46914846
}
46924847

46934848
#[test]

0 commit comments

Comments
 (0)