heddle-cli 0.20.1

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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// SPDX-License-Identifier: Apache-2.0
//! Opt-in end-to-end coverage for the real Heddle client against a live weft.
//!
//! This test deliberately does not start or mock weft. See
//! `docs/testing/live-weft-client-flow.md` for the required environment and
//! exact invocation.

#![cfg(feature = "client")]

use std::{
    fs,
    path::{Path, PathBuf},
    process::{Command, Output},
    time::{SystemTime, UNIX_EPOCH},
};

use anyhow::{Context, Result, anyhow, ensure};
use hosted_client::client::{HostedAuthMode, HostedClient, HostedSession};
use objects::{object::StateId, store::ObjectStore};
use repo::{Repository, remote::RemoteTarget};
use tempfile::TempDir;

const WEFT_URL_ENV: &str = "HEDDLE_E2E_WEFT_URL";
const NAMED_THREAD: &str = "client-flow-e2e";

#[derive(Clone, Debug, Eq, PartialEq)]
struct AdvertisedThread {
    state: StateId,
    id: String,
}

/// Exercises the complete native client lifecycle against a real weft.
///
/// Ignored because it requires externally managed weft, Postgres, object
/// storage, and credentials. It also remains intentionally red on the
/// first-`main`-push metadata assertion until heddle#1638 lands.
#[tokio::test]
#[ignore = "requires a live weft; see docs/testing/live-weft-client-flow.md"]
async fn real_heddle_push_pull_clone_preserves_hosted_thread_identity() -> Result<()> {
    let Some(endpoint) = std::env::var_os(WEFT_URL_ENV) else {
        eprintln!("skipping live-weft client flow: {WEFT_URL_ENV} is unset");
        return Ok(());
    };
    let endpoint = endpoint
        .into_string()
        .map_err(|_| anyhow!("{WEFT_URL_ENV} must be valid UTF-8"))?;
    let authority = authority_only_endpoint(&endpoint)?;

    let temp = TempDir::new().context("create live-weft e2e root")?;
    let source = unique_repo_path(temp.path())?;
    fs::create_dir(&source).context("create source repository directory")?;

    run_heddle(&source, &["init"])?;
    run_heddle(&source, &["remote", "add", "origin", &endpoint])?;

    fs::write(source.join("main.txt"), "main-v1\n").context("write first main state")?;
    run_heddle(&source, &["capture", "-m", "live e2e main v1"])?;
    let main_v1 = local_thread_state(&source, "main")?;

    // An authority-only remote auto-provisions a fresh child spool and rewrites
    // origin to its full path. That keeps every run isolated without hardcoding
    // a weft port, namespace, spool name, or credential.
    run_heddle(&source, &["push", "origin"])?;
    let remote_url = configured_origin(&source)?;
    let (remote_authority, remote_path) = hosted_remote_parts(&remote_url)?;
    ensure!(
        remote_authority == authority,
        "auto-provisioned remote changed authority from {authority} to {remote_authority}"
    );

    let mut client = connect_client(&remote_url, &remote_authority).await?;
    let first_main = advertised_thread(&mut client, &remote_path, "main").await?;
    ensure!(
        first_main.state == main_v1,
        "first main push advertised state {} instead of {}",
        first_main.state,
        main_v1
    );
    assert_thread_metadata(
        &mut client,
        &source,
        &remote_path,
        "main",
        main_v1,
        &first_main.id,
    )
    .await?;
    client.close().await;

    fs::write(source.join("main.txt"), "main-v2\n").context("write second main state")?;
    run_heddle(&source, &["capture", "-m", "live e2e main v2"])?;
    let main_v2 = local_thread_state(&source, "main")?;
    ensure!(
        main_v2 != main_v1,
        "second main capture did not advance main"
    );

    run_heddle(&source, &["push", "origin"])?;
    let mut client = connect_client(&remote_url, &remote_authority).await?;
    let second_main = advertised_thread(&mut client, &remote_path, "main").await?;
    ensure!(
        second_main.id == first_main.id,
        "REGRESSION: second main push changed hosted thread_id from {:?} to {:?}",
        first_main.id,
        second_main.id
    );
    ensure!(
        second_main.state == main_v2,
        "second main push advertised state {} instead of {}",
        second_main.state,
        main_v2
    );
    assert_thread_metadata(
        &mut client,
        &source,
        &remote_path,
        "main",
        main_v2,
        &first_main.id,
    )
    .await?;
    client.close().await;

    let named_checkout = temp.path().join("named-checkout");
    let named_checkout_arg = path_text(&named_checkout, "named checkout")?;
    run_heddle(
        &source,
        &["start", NAMED_THREAD, "--path", named_checkout_arg.as_str()],
    )?;
    fs::write(named_checkout.join("named.txt"), "named-v1\n")
        .context("write named-thread state")?;
    run_heddle(&named_checkout, &["capture", "-m", "live e2e named thread"])?;
    let named_state = local_thread_state(&named_checkout, NAMED_THREAD)?;
    run_heddle(&named_checkout, &["push", "origin"])?;

    let mut client = connect_client(&remote_url, &remote_authority).await?;
    let main_after_named = advertised_thread(&mut client, &remote_path, "main").await?;
    let named = advertised_thread(&mut client, &remote_path, NAMED_THREAD).await?;
    ensure!(
        main_after_named == second_main,
        "named-thread push changed main's advertised identity or state"
    );
    ensure!(
        !named.id.is_empty(),
        "named thread was advertised with an empty thread_id"
    );
    ensure!(
        named.id != first_main.id,
        "named thread reused main's hosted thread_id {:?}",
        named.id
    );
    ensure!(
        named.state == named_state,
        "named push advertised state {} instead of {}",
        named.state,
        named_state
    );
    assert_thread_metadata(
        &mut client,
        &named_checkout,
        &remote_path,
        NAMED_THREAD,
        named_state,
        &named.id,
    )
    .await?;
    client.close().await;

    let pulled = temp.path().join("pulled");
    fs::create_dir(&pulled).context("create pull destination")?;
    run_heddle(&pulled, &["init"])?;
    run_heddle(&pulled, &["remote", "add", "origin", &remote_url])?;
    pull_thread(&pulled, "main")?;
    assert_local_thread(&pulled, "main", main_v2)?;
    ensure!(
        fs::read_to_string(pulled.join("main.txt"))? == "main-v2\n",
        "pull did not materialize main-v2"
    );
    let mut client = connect_client(&remote_url, &remote_authority).await?;
    assert_thread_metadata(
        &mut client,
        &pulled,
        &remote_path,
        "main",
        main_v2,
        &first_main.id,
    )
    .await?;
    client.close().await;

    pull_thread(&pulled, NAMED_THREAD)?;
    assert_local_thread(&pulled, NAMED_THREAD, named_state)?;
    run_heddle(&pulled, &["thread", "switch", NAMED_THREAD])?;
    ensure!(
        fs::read_to_string(pulled.join("named.txt"))? == "named-v1\n",
        "pull did not materialize the named-thread state"
    );
    let mut client = connect_client(&remote_url, &remote_authority).await?;
    assert_thread_metadata(
        &mut client,
        &pulled,
        &remote_path,
        NAMED_THREAD,
        named_state,
        &named.id,
    )
    .await?;
    assert_advertised_pair(
        &mut client,
        &remote_path,
        (&first_main.id, main_v2),
        (&named.id, named_state),
    )
    .await?;
    client.close().await;

    // Clone each advertised thread into a genuinely fresh repository. Hosted
    // clone selects one checkout at a time, so the pair proves both tips and
    // their identities survive the fresh-clone path.
    let cloned_main = temp.path().join("cloned-main");
    clone_thread(temp.path(), &remote_url, &cloned_main, None)?;
    assert_local_thread(&cloned_main, "main", main_v2)?;
    ensure!(
        fs::read_to_string(cloned_main.join("main.txt"))? == "main-v2\n",
        "fresh clone did not materialize main-v2"
    );
    let mut client = connect_client(&remote_url, &remote_authority).await?;
    assert_thread_metadata(
        &mut client,
        &cloned_main,
        &remote_path,
        "main",
        main_v2,
        &first_main.id,
    )
    .await?;
    client.close().await;

    let cloned_named = temp.path().join("cloned-named");
    clone_thread(temp.path(), &remote_url, &cloned_named, Some(NAMED_THREAD))?;
    assert_local_thread(&cloned_named, NAMED_THREAD, named_state)?;
    ensure!(
        fs::read_to_string(cloned_named.join("named.txt"))? == "named-v1\n",
        "fresh named-thread clone did not materialize named-v1"
    );
    let mut client = connect_client(&remote_url, &remote_authority).await?;
    assert_thread_metadata(
        &mut client,
        &cloned_named,
        &remote_path,
        NAMED_THREAD,
        named_state,
        &named.id,
    )
    .await?;
    assert_advertised_pair(
        &mut client,
        &remote_path,
        (&first_main.id, main_v2),
        (&named.id, named_state),
    )
    .await?;

    client.close().await;
    Ok(())
}

fn authority_only_endpoint(endpoint: &str) -> Result<String> {
    let target = RemoteTarget::parse_native(endpoint)
        .map_err(|error| anyhow!("invalid {WEFT_URL_ENV} {endpoint:?}: {error}"))?;
    let RemoteTarget::Network {
        authority,
        repo_path,
    } = target
    else {
        return Err(anyhow!(
            "{WEFT_URL_ENV} must be a hosted authority, not a local path"
        ));
    };
    ensure!(
        repo_path.is_none(),
        "{WEFT_URL_ENV} must contain only the weft authority; the test auto-provisions a fresh child spool"
    );
    Ok(authority)
}

fn unique_repo_path(root: &Path) -> Result<PathBuf> {
    let nonce = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .context("system clock is before the Unix epoch")?
        .as_nanos();
    Ok(root.join(format!(
        "heddle-client-flow-e2e-{}-{nonce}",
        std::process::id()
    )))
}

fn run_heddle(cwd: &Path, args: &[&str]) -> Result<Output> {
    let output = Command::new(env!("CARGO_BIN_EXE_heddle"))
        .current_dir(cwd)
        .args(["--output", "json"])
        .args(args)
        .env("HEDDLE_PRINCIPAL_NAME", "Heddle live-weft e2e")
        .env("HEDDLE_PRINCIPAL_EMAIL", "live-weft-e2e@heddle.dev")
        .env("HEDDLE_FSMONITOR", "off")
        .env("NO_COLOR", "1")
        .env_remove("HEDDLE_AGENT_PROVIDER")
        .env_remove("HEDDLE_AGENT_MODEL")
        .output()
        .with_context(|| format!("run heddle {args:?} in {}", cwd.display()))?;
    ensure_command_succeeded(args, &output)?;
    Ok(output)
}

fn ensure_command_succeeded(args: &[&str], output: &Output) -> Result<()> {
    ensure!(
        output.status.success(),
        "heddle {args:?} failed with {}\nstdout:\n{}\nstderr:\n{}",
        output.status,
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    Ok(())
}

fn configured_origin(repo_path: &Path) -> Result<String> {
    let repo = Repository::open(repo_path).context("open source after first push")?;
    repo::remote::RemoteConfig::open(&repo)
        .context("open source remote config")?
        .get("origin")
        .context("read auto-provisioned origin")
        .map(|remote| remote.url)
}

fn hosted_remote_parts(remote_url: &str) -> Result<(String, String)> {
    let target = RemoteTarget::parse_native(remote_url)
        .map_err(|error| anyhow!("invalid auto-provisioned remote {remote_url:?}: {error}"))?;
    let RemoteTarget::Network {
        authority,
        repo_path,
    } = target
    else {
        return Err(anyhow!("auto-provisioned origin is not hosted"));
    };
    let repo_path = repo_path.context("auto-provisioned origin has no spool path")?;
    Ok((authority, repo_path))
}

async fn connect_client(remote_url: &str, authority: &str) -> Result<HostedClient> {
    let user_config = config::UserConfig::load_default().context("load hosted client config")?;
    let server_key = repo::remote::credential_key_from_remote_url(remote_url);
    let session =
        HostedSession::build(&user_config, server_key, HostedAuthMode::CredentialFallback)
            .context("build authenticated hosted session")?;
    session
        .connect(authority)
        .await
        .context("connect real hosted client to weft")
}

async fn advertised_thread(
    client: &mut HostedClient,
    remote_path: &str,
    name: &str,
) -> Result<AdvertisedThread> {
    let refs = client
        .list_refs_with_revision_addresses(remote_path)
        .await
        .with_context(|| format!("ListRefs {remote_path} while resolving {name}"))?;
    let advertised = refs
        .into_iter()
        .find(|entry| entry.is_user_thread() && entry.name == name)
        .with_context(|| format!("ListRefs did not advertise thread {name:?}"))?;
    let id = advertised
        .thread_id
        .with_context(|| format!("ListRefs omitted thread_id for {name:?}"))?;
    ensure!(
        !id.is_empty(),
        "ListRefs returned an empty thread_id for {name:?}"
    );
    Ok(AdvertisedThread {
        state: advertised.state_id,
        id,
    })
}

async fn assert_thread_metadata(
    client: &mut HostedClient,
    local_path: &Path,
    remote_path: &str,
    name: &str,
    expected_state: StateId,
    expected_id: &str,
) -> Result<()> {
    let repo = Repository::open(local_path)
        .with_context(|| format!("open local repository at {}", local_path.display()))?;
    let metadata = client
        .get_thread_metadata(&repo, remote_path, name, expected_state)
        .await
        .with_context(|| format!("resolve hosted metadata for {name:?}"))?;
    ensure!(
        metadata.id == expected_id,
        "hosted metadata for {name:?} used thread_id {:?}, expected {:?}",
        metadata.id,
        expected_id
    );
    Ok(())
}

async fn assert_advertised_pair(
    client: &mut HostedClient,
    remote_path: &str,
    main: (&str, StateId),
    named: (&str, StateId),
) -> Result<()> {
    let advertised_main = advertised_thread(client, remote_path, "main").await?;
    let advertised_named = advertised_thread(client, remote_path, NAMED_THREAD).await?;
    ensure!(
        advertised_main.id == main.0 && advertised_main.state == main.1,
        "main identity/state changed after a later client operation: {advertised_main:?}"
    );
    ensure!(
        advertised_named.id == named.0 && advertised_named.state == named.1,
        "named identity/state changed after a later client operation: {advertised_named:?}"
    );
    Ok(())
}

fn pull_thread(local_path: &Path, thread: &str) -> Result<()> {
    run_heddle(
        local_path,
        &[
            "pull",
            "origin",
            "--thread",
            thread,
            "--local-thread",
            thread,
        ],
    )?;
    Ok(())
}

fn clone_thread(
    cwd: &Path,
    remote_url: &str,
    destination: &Path,
    thread: Option<&str>,
) -> Result<()> {
    let destination = path_text(destination, "clone destination")?;
    let mut args = vec!["clone", remote_url, destination.as_str()];
    if let Some(thread) = thread {
        args.extend(["--thread", thread]);
    }
    run_heddle(cwd, &args)?;
    Ok(())
}

fn local_thread_state(repo_path: &Path, thread: &str) -> Result<StateId> {
    let repo = Repository::open(repo_path)
        .with_context(|| format!("open local repository at {}", repo_path.display()))?;
    repo.refs()
        .get_thread(&objects::object::ThreadName::new(thread))
        .with_context(|| format!("read local thread {thread:?}"))?
        .with_context(|| format!("local thread {thread:?} has no state"))
}

fn assert_local_thread(repo_path: &Path, thread: &str, expected: StateId) -> Result<()> {
    let repo = Repository::open(repo_path)
        .with_context(|| format!("open local repository at {}", repo_path.display()))?;
    let actual = repo
        .refs()
        .get_thread(&objects::object::ThreadName::new(thread))
        .with_context(|| format!("read materialized thread {thread:?}"))?
        .with_context(|| format!("materialized repository has no thread {thread:?}"))?;
    ensure!(
        actual == expected,
        "local thread {thread:?} points to {actual}, expected {expected}"
    );
    ensure!(
        repo.store().get_state(&expected)?.is_some(),
        "local thread {thread:?} points to missing state {expected}"
    );
    Ok(())
}

fn path_text(path: &Path, label: &str) -> Result<String> {
    path.to_str()
        .map(str::to_string)
        .with_context(|| format!("{label} path is not valid UTF-8: {}", path.display()))
}