heddle-cli 0.15.0

An AI-native version control system
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
// SPDX-License-Identifier: Apache-2.0
//! Hosted `CollaborationService` client wrappers.
//!
//! These are the caller-authenticated, PoP-signed RPCs the CLI uses to publish
//! and fetch discussions against a hosted weft. They are the write/read seam
//! for hosted collaboration: local discussions live in the append-only
//! [`repo::CollaborationStore`] op-log, and the CLI-side sync orchestrator
//! ([`heddle_cli`'s `discussion_sync`]) bridges that model to the server's
//! per-state `DiscussionsBlob` shape through these calls.
//!
//! Wire identity note: the canonical `CollaborationService` proto types the
//! discussion anchor state as a 32-byte `StateId`, but the hosted server still
//! resolves the inbound `state_id` field through a 16-byte `ChangeId` decode
//! (`OpenDiscussion`/`ListByState`). We therefore send the **ChangeId** bytes in
//! that field (matching weft's own canonical integration tests), while the
//! server echoes the genuine 32-byte `StateId` back in
//! `Discussion.opened_against_state`.

use api::heddle::api::v1alpha1::{
    AppendTurnRequest, ContextAnnotationKind, Discussion as ProtoDiscussion, DiscussionSeverity,
    DiscussionStatusFilter, ListDiscussionsByStateRequest, OpenDiscussionRequest, PathSymbolRef,
    ResolveDiscussionRequest, StateId as ProtoStateId, list_discussions_response,
    resolve_discussion_request,
};
use objects::object::{AnnotationKind, ChangeId, StateId};
use wire::ProtocolError;

use super::{HostedClient, helpers::hosted_to_protocol_error};

/// One turn of a hosted discussion, decoded from the wire.
#[derive(Debug, Clone)]
pub struct HostedDiscussionTurn {
    pub author_name: String,
    pub author_email: String,
    pub body: String,
    pub posted_at_secs: i64,
}

/// A hosted discussion decoded from the `CollaborationService` wire types into
/// the shape the CLI-side sync bridge consumes.
#[derive(Debug, Clone)]
pub struct HostedDiscussion {
    /// Server-assigned discussion id (opaque string).
    pub id: String,
    pub file: String,
    pub symbol: String,
    /// Genuine 32-byte anchor `StateId` echoed by the server, when present.
    pub opened_against_state: Option<StateId>,
    pub visibility: String,
    pub thread_ref: Option<String>,
    pub turns: Vec<HostedDiscussionTurn>,
}

fn decode_discussion(proto: ProtoDiscussion) -> HostedDiscussion {
    let anchor = proto.anchor.unwrap_or_default();
    HostedDiscussion {
        id: proto.id,
        file: anchor.file,
        symbol: anchor.symbol,
        opened_against_state: proto
            .opened_against_state
            .and_then(|state| StateId::try_from_slice(&state.value).ok()),
        visibility: proto.visibility,
        thread_ref: (!proto.thread_ref.is_empty()).then_some(proto.thread_ref),
        turns: proto
            .turns
            .into_iter()
            .map(|turn| HostedDiscussionTurn {
                author_name: turn.author_name,
                author_email: turn.author_email,
                body: turn.body,
                posted_at_secs: turn.posted_at.map(|ts| ts.seconds).unwrap_or(0),
            })
            .collect(),
    }
}

/// The hosted server decodes the discussion anchor `state_id` field as a
/// 16-byte `ChangeId` (see the module note); wrap the change id in the proto
/// `StateId` message accordingly.
fn change_id_state_field(change_id: ChangeId) -> Option<ProtoStateId> {
    Some(ProtoStateId {
        value: change_id.as_bytes().to_vec(),
    })
}

impl HostedClient {
    /// The authenticated hosted username (the bearer token's `principal:<subject>`
    /// subject). weft stamps discussion turns with `Principal::new(username, "")`,
    /// so this is the author name our own pushed turns carry server-side — the
    /// identity the discussion-sync bridge uses to recognize turns we published.
    /// `None` for an anonymous/unsigned client.
    pub fn authenticated_username(&self) -> Option<String> {
        self.context
            .signing_identity()
            .and_then(|principal| principal.strip_prefix("principal:"))
            .map(|subject| subject.trim().to_string())
            .filter(|subject| !subject.is_empty())
    }

    /// Open a hosted discussion anchored at `change_id`'s state, seeded with
    /// `body` as the first turn. Caller-authenticated + PoP-signed.
    #[allow(clippy::too_many_arguments)]
    pub async fn open_discussion(
        &mut self,
        repo_path: &str,
        change_id: ChangeId,
        file: &str,
        symbol: &str,
        body: &str,
        visibility: &str,
        thread_ref: Option<&str>,
        client_operation_id: String,
    ) -> Result<HostedDiscussion, ProtocolError> {
        let request = open_discussion_request(
            repo_path,
            change_id,
            file,
            symbol,
            body,
            visibility,
            thread_ref,
            client_operation_id,
        );
        let response = self
            .routes()
            .open_discussion(&request)
            .await
            .map_err(hosted_to_protocol_error)?;
        Ok(decode_discussion(response))
    }

    /// Resolve a hosted discussion by creating and linking a context annotation.
    #[allow(clippy::too_many_arguments)]
    pub async fn resolve_discussion_into_annotation(
        &mut self,
        repo_path: &str,
        discussion_id: &str,
        kind: AnnotationKind,
        content: &str,
        tags: Vec<String>,
        client_operation_id: String,
    ) -> Result<HostedDiscussion, ProtocolError> {
        let request = resolve_into_annotation_request(
            repo_path,
            discussion_id,
            kind,
            content,
            tags,
            client_operation_id,
        );
        let response = self
            .routes()
            .resolve_discussion(&request)
            .await
            .map_err(hosted_to_protocol_error)?;
        Ok(decode_discussion(response))
    }

    /// Append `body` as a new turn on an existing hosted discussion.
    /// Caller-authenticated + PoP-signed.
    pub async fn append_turn(
        &mut self,
        repo_path: &str,
        discussion_id: &str,
        body: &str,
        client_operation_id: String,
    ) -> Result<HostedDiscussion, ProtocolError> {
        let request = AppendTurnRequest {
            repo_path: super::helpers::repository_ref(repo_path),
            discussion_id: discussion_id.to_string(),
            body: body.to_string(),
            client_operation_id,
            references: Vec::new(),
        };
        let response = self
            .routes()
            .append_turn(&request)
            .await
            .map_err(hosted_to_protocol_error)?;
        Ok(decode_discussion(response))
    }

    /// List hosted discussions anchored at `change_id`'s state. `status` is one
    /// of `open` | `resolved` | `all` | `orphaned`.
    pub async fn list_discussions_by_state(
        &mut self,
        repo_path: &str,
        change_id: ChangeId,
        status: &str,
    ) -> Result<Vec<HostedDiscussion>, ProtocolError> {
        let status = discussion_status_filter(status)? as i32;
        let mut discussions = Vec::new();
        let mut page_token = String::new();
        loop {
            let request = ListDiscussionsByStateRequest {
                repo_path: super::helpers::repository_ref(repo_path),
                state_id: change_id_state_field(change_id),
                status,
                page_size: api::MAX_PAGE_SIZE,
                page_token: page_token.clone(),
            };
            let mut stream = self
                .routes()
                .list_discussions_by_state(&request)
                .await
                .map_err(hosted_to_protocol_error)?;
            let mut next_page_token = None;
            while let Some(response) = stream.next().await.map_err(hosted_to_protocol_error)? {
                match response.frame {
                    Some(list_discussions_response::Frame::Item(discussion)) => {
                        discussions.push(decode_discussion(*discussion));
                    }
                    Some(list_discussions_response::Frame::PageEnd(page_end)) => {
                        next_page_token = Some(page_end.next_page_token);
                    }
                    None => {
                        return Err(ProtocolError::InvalidState(
                            "ListByState emitted an empty frame".to_string(),
                        ));
                    }
                }
            }
            let next_page_token = next_page_token.ok_or_else(|| {
                ProtocolError::InvalidState(
                    "ListByState ended without a terminal page frame".to_string(),
                )
            })?;
            if next_page_token.is_empty() {
                return Ok(discussions);
            }
            if next_page_token == page_token {
                return Err(ProtocolError::InvalidState(
                    "ListByState returned a repeated page token".to_string(),
                ));
            }
            page_token = next_page_token;
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn open_discussion_request(
    repo_path: &str,
    change_id: ChangeId,
    file: &str,
    symbol: &str,
    body: &str,
    visibility: &str,
    thread_ref: Option<&str>,
    client_operation_id: String,
) -> OpenDiscussionRequest {
    OpenDiscussionRequest {
        repo_path: super::helpers::repository_ref(repo_path),
        state_id: change_id_state_field(change_id),
        anchor: Some(PathSymbolRef {
            file: file.to_string(),
            symbol: symbol.to_string(),
        }),
        body: body.to_string(),
        visibility: visibility.to_string(),
        thread_ref: thread_ref.unwrap_or_default().to_string(),
        client_operation_id,
        thread_id: String::new(),
        severity: DiscussionSeverity::Unspecified as i32,
    }
}

fn resolve_into_annotation_request(
    repo_path: &str,
    discussion_id: &str,
    kind: AnnotationKind,
    content: &str,
    tags: Vec<String>,
    client_operation_id: String,
) -> ResolveDiscussionRequest {
    ResolveDiscussionRequest {
        repo_path: super::helpers::repository_ref(repo_path),
        discussion_id: discussion_id.to_string(),
        resolution: Some(resolve_discussion_request::Resolution::IntoAnnotation(
            resolve_discussion_request::ResolveIntoAnnotation {
                kind: annotation_kind_to_proto(kind) as i32,
                content: content.to_string(),
                tags,
            },
        )),
        client_operation_id,
    }
}

fn annotation_kind_to_proto(kind: AnnotationKind) -> ContextAnnotationKind {
    match kind {
        AnnotationKind::Constraint => ContextAnnotationKind::Constraint,
        AnnotationKind::Invariant => ContextAnnotationKind::Invariant,
        AnnotationKind::Rationale => ContextAnnotationKind::Rationale,
    }
}

fn discussion_status_filter(status: &str) -> Result<DiscussionStatusFilter, ProtocolError> {
    match status {
        "all" => Ok(DiscussionStatusFilter::Unspecified),
        "open" => Ok(DiscussionStatusFilter::Open),
        "resolved" => Ok(DiscussionStatusFilter::Resolved),
        "orphaned" => Ok(DiscussionStatusFilter::Orphaned),
        other => Err(ProtocolError::InvalidState(format!(
            "invalid discussion status filter: {other}"
        ))),
    }
}

#[cfg(test)]
mod tests {
    use api::heddle::api::v1alpha1::{
        DiscussionTurn as ProtoTurn, PathSymbolRef, StateId as ProtoStateId,
        resolve_discussion_request::Resolution,
    };
    use objects::object::{AnnotationKind, ChangeId};

    use super::*;

    #[test]
    fn discussion_status_filter_accepts_known_and_rejects_unknown() {
        assert_eq!(
            discussion_status_filter("all").unwrap(),
            DiscussionStatusFilter::Unspecified
        );
        assert_eq!(
            discussion_status_filter("open").unwrap(),
            DiscussionStatusFilter::Open
        );
        assert_eq!(
            discussion_status_filter("resolved").unwrap(),
            DiscussionStatusFilter::Resolved
        );
        assert_eq!(
            discussion_status_filter("orphaned").unwrap(),
            DiscussionStatusFilter::Orphaned
        );
        assert!(discussion_status_filter("closed").is_err());
    }

    #[test]
    fn change_id_state_field_wraps_16_byte_change_id() {
        let change = ChangeId::from_bytes([0x11; 16]);
        let field = change_id_state_field(change).expect("proto state wrapper");
        assert_eq!(field.value, change.as_bytes().to_vec());
    }

    #[test]
    fn open_discussion_request_sets_thread_ref() {
        let change = ChangeId::from_bytes([0x11; 16]);
        let request = open_discussion_request(
            "acme/widgets",
            change,
            "src/lib.rs",
            "run",
            "keep this stable",
            "internal",
            Some("refs/heads/feature/run"),
            "open-op".to_string(),
        );
        assert_eq!(request.thread_ref, "refs/heads/feature/run");
        assert_eq!(request.anchor.unwrap().file, "src/lib.rs");
        assert_eq!(request.body, "keep this stable");
        assert_eq!(request.severity, DiscussionSeverity::Unspecified as i32);
    }

    #[test]
    fn resolve_into_annotation_request_uses_the_typed_resolution_variant() {
        let request = resolve_into_annotation_request(
            "acme/widgets",
            "discussion-1",
            AnnotationKind::Invariant,
            "the cache key must include visibility",
            vec!["cache".to_string(), "security".to_string()],
            "resolve-op".to_string(),
        );
        let Some(Resolution::IntoAnnotation(annotation)) = request.resolution else {
            panic!("expected ResolveIntoAnnotation request");
        };
        assert_eq!(annotation.kind, ContextAnnotationKind::Invariant as i32);
        assert_eq!(annotation.content, "the cache key must include visibility");
        assert_eq!(annotation.tags, ["cache", "security"]);
    }

    #[test]
    fn decode_discussion_maps_anchor_turns_and_optional_state() {
        let state = StateId::from_bytes([0x22; 32]);
        let proto = ProtoDiscussion {
            id: "disc-1".into(),
            anchor: Some(PathSymbolRef {
                file: "src/lib.rs".into(),
                symbol: "main".into(),
            }),
            opened_against_state: Some(ProtoStateId {
                value: state.as_bytes().to_vec(),
            }),
            visibility: "team".into(),
            turns: vec![ProtoTurn {
                author_name: "alice".into(),
                author_email: "a@x".into(),
                body: "lgtm".into(),
                references: Vec::new(),
                posted_at: Some(prost_types::Timestamp {
                    seconds: 42,
                    nanos: 0,
                }),
            }],
            ..Default::default()
        };
        let decoded = decode_discussion(proto);
        assert_eq!(decoded.id, "disc-1");
        assert_eq!(decoded.file, "src/lib.rs");
        assert_eq!(decoded.symbol, "main");
        assert_eq!(decoded.opened_against_state, Some(state));
        assert_eq!(decoded.visibility, "team");
        assert_eq!(decoded.thread_ref, None);
        assert_eq!(decoded.turns.len(), 1);
        assert_eq!(decoded.turns[0].author_name, "alice");
        assert_eq!(decoded.turns[0].body, "lgtm");
        assert_eq!(decoded.turns[0].posted_at_secs, 42);

        // Missing optional fields collapse cleanly.
        let empty = decode_discussion(ProtoDiscussion {
            id: "empty".into(),
            ..Default::default()
        });
        assert!(empty.file.is_empty());
        assert!(empty.opened_against_state.is_none());
        assert!(empty.turns.is_empty());
    }
}