dpc-tau-cli 0.1.0

A minimal Unix-first coding agent.
Documentation
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use tau_config::settings as path_tau_config_settings;
use tau_config::settings::ProfileSelection;

use super::*;

/// One available row bypasses a picker even when newer rows are locked.
#[test]
fn one_unlocked_resume_row_is_selected_deterministically() {
    assert_eq!(
        sole_unlocked_resume_index([true, true, false, true]).expect("selection succeeds"),
        Some(2)
    );
}

/// All-locked rows produce an actionable error instead of looking like a
/// cancelled picker or an absent persisted session.
#[test]
fn all_locked_resume_rows_report_attach_action() {
    let error = sole_unlocked_resume_index([true, true]).expect_err("all rows are locked");
    assert!(error.to_string().contains("tau attach SESSION"));
}

/// Several available rows preserve interactive selection.
#[test]
fn several_unlocked_resume_rows_require_picker() {
    assert_eq!(
        sole_unlocked_resume_index([false, true, false]).expect("selection succeeds"),
        None
    );
}

/// The display cap must not hide every selectable target when the newest rows
/// are all owned by running harnesses.
#[test]
fn resume_picker_preserves_unlocked_row_beyond_display_cap() {
    let mut locked = vec![true; RESUME_PICKER_LIMIT + 2];
    locked[RESUME_PICKER_LIMIT + 1] = false;

    let visible = visible_resume_indices(locked);

    assert_eq!(visible.len(), RESUME_PICKER_LIMIT);
    assert_eq!(visible[RESUME_PICKER_LIMIT - 1], RESUME_PICKER_LIMIT + 1);
}

/// Resume stderr setup must remain path-free until the harness reports
/// lock-held readiness, so a deleted selection is not recreated by logging.
#[test]
fn resumed_daemon_output_defers_session_log_creation() {
    let temp = tempfile::tempdir().expect("tempdir");
    let session_dir = temp.path().join("session-1");
    let output = daemon_output_for_session_in(
        temp.path(),
        "session-1",
        HarnessStorageMode::Durable,
        SessionLaunchStatus::Resumed,
    )
    .expect("resolve resumed output");

    assert!(output.deferred_harness_log.is_some());
    assert!(!session_dir.exists());
}

/// The resumed relay appends diagnostics to the lock-held file created by the
/// child rather than replacing or truncating it.
#[cfg(unix)]
#[test]
fn resumed_stderr_relay_appends_to_child_created_log() {
    let temp = tempfile::tempdir().expect("tempdir");
    let harness_log = temp.path().join("session/logs/tau-harness.log");
    std::fs::create_dir_all(harness_log.parent().expect("log parent")).expect("create log parent");
    std::fs::write(&harness_log, "child-ready\n").expect("create child log");
    let mut child = Command::new("sh")
        .arg("-c")
        .arg("printf 'resumed diagnostic\\n' >&2")
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn diagnostic child");
    let stderr = child.stderr.take().expect("child stderr");

    relay_stderr_after_lock_held_log(stderr, harness_log.clone());
    child.wait().expect("wait for child");
    let deadline = Instant::now() + Duration::from_secs(1);
    while Instant::now() < deadline {
        let contents = std::fs::read_to_string(&harness_log).expect("read log");
        if contents.contains("resumed diagnostic") {
            assert!(contents.starts_with("child-ready\n"));
            return;
        }
        std::thread::sleep(Duration::from_millis(10));
    }
    panic!("relay did not append resumed diagnostics");
}

/// A child exit and cleanup before the relay opens the lock-held file must not
/// recreate either the log or its deleted session directory.
#[cfg(unix)]
#[test]
fn resumed_stderr_relay_does_not_recreate_deleted_session() {
    let temp = tempfile::tempdir().expect("tempdir");
    let session_dir = temp.path().join("session");
    let harness_log = session_dir.join("logs/tau-harness.log");
    let mut child = Command::new("sh")
        .arg("-c")
        .arg("printf 'failed resume\\n' >&2")
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn diagnostic child");
    let stderr = child.stderr.take().expect("child stderr");

    relay_stderr_after_lock_held_log(stderr, harness_log);
    child.wait().expect("wait for child");
    std::thread::sleep(Duration::from_millis(100));

    assert!(!session_dir.exists());
}

#[test]
fn daemon_command_sets_and_clears_harness_config_override_env() {
    let override_ = tau_config::settings::HarnessConfigCliOverride {
        key: "session_retention_days".to_owned(),
        raw_value: "3".to_owned(),
    };
    let with_override = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: std::slice::from_ref(&override_),
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::Durable,
    });
    assert!(with_override.get_envs().any(|(key, value)| {
        key == tau_harness::HARNESS_CONFIG_CLI_OVERRIDES_ENV && value.is_some()
    }));

    let without_override = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::Durable,
    });
    assert!(without_override.get_envs().any(|(key, value)| {
        key == tau_harness::HARNESS_CONFIG_CLI_OVERRIDES_ENV && value.is_none()
    }));
    for variable in [
        tau_config::settings::TAU_PROVIDER_ALIASES_ENV,
        tau_config::settings::TAU_MODEL_ALIASES_ENV,
    ] {
        assert!(
            with_override
                .get_envs()
                .any(|(key, value)| key == variable && value.is_none()),
            "spawned daemon must not re-read inherited {variable}"
        );
    }
}

/// Ensures the launcher does not manufacture a parent-wide logging allowlist;
/// the harness and each extension must own their absent-environment fallback.
#[test]
fn daemon_command_leaves_tau_log_inherited() {
    let command = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::Durable,
    });

    assert!(
        command
            .get_envs()
            .all(|(name, _)| name != std::ffi::OsStr::new("TAU_LOG"))
    );
}

/// Ensures an ordered CLI profile selection reaches the daemon unchanged and an
/// absent selection clears inherited profile state rather than changing child
/// configuration.
#[test]
fn daemon_command_sets_and_clears_profile_env() {
    let spec = |profile| DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::Durable,
    };
    let profile = ProfileSelection::parse("focused,review").expect("profile selection");
    let selected = build_daemon_command(spec(Some(&profile)));
    assert!(selected.get_envs().any(|(key, value)| {
        key == tau_config::settings::TAU_PROFILE_ENV && value == Some("focused,review".as_ref())
    }));

    let absent = build_daemon_command(spec(None));
    assert!(
        absent.get_envs().any(|(key, value)| {
            key == tau_config::settings::TAU_PROFILE_ENV && value.is_none()
        })
    );
}

/// Ensures the private parent-to-child extension transport cannot be inherited
/// when the current invocation has no ordered CLI extension operations.
#[test]
fn daemon_command_clears_empty_extension_transport() {
    let command = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::Durable,
    });
    assert!(command.get_envs().any(|(key, value)| {
        key == tau_harness::EXTENSION_CLI_OVERRIDES_ENV && value.is_none()
    }));
    assert!(
        !command
            .get_envs()
            .any(|(key, _)| key == tau_config::settings::TAU_ENABLE_EXTENSIONS_ENV),
        "the supported public environment must remain inherited"
    );
}

/// Ensures preview daemons receive the already parsed public environment
/// through its supported variable while argv operations remain in the private
/// ordered CLI transport.
#[test]
fn daemon_command_forwards_public_extension_environment_separately() {
    let names = vec!["test-dummy".to_owned(), "std-rhai".to_owned()];
    let cli = [path_tau_config_settings::ExtensionCliOverride::Disable(
        "test-dummy".to_owned(),
    )];
    let command = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "print-tools-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: Some("engineer"),
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &cli,
            extension_environment: Some(&names),
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::Durable,
    });

    assert!(command.get_envs().any(|(key, value)| {
        key == tau_config::settings::TAU_ENABLE_EXTENSIONS_ENV
            && value == Some(std::ffi::OsStr::new("test-dummy,std-rhai"))
    }));
    assert!(command.get_envs().any(|(key, value)| {
        key == tau_harness::EXTENSION_CLI_OVERRIDES_ENV
            && value
                .and_then(std::ffi::OsStr::to_str)
                .is_some_and(|value| {
                    serde_json::from_str::<Vec<tau_config::settings::ExtensionCliOverride>>(value)
                        .is_ok_and(|decoded| decoded == cli)
                })
    }));
}

#[test]
fn daemon_command_clears_socket_activation_env() {
    let command = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::Durable,
    });

    for expected_key in [
        "LISTEN_FDS",
        "LISTEN_PID",
        "LISTEN_FDS_FIRST_FD",
        "LISTEN_FDNAMES",
    ] {
        assert!(
            command
                .get_envs()
                .any(|(key, value)| key == expected_key && value.is_none()),
            "expected {expected_key} to be removed from harness child environment"
        );
    }
}

/// Every storage mode sets only its matching child-process environment marker
/// and explicitly clears markers inherited from the parent.
#[test]
fn daemon_command_maps_storage_mode_to_exclusive_environment_marker() {
    let command = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::SessionEphemeral,
    });

    assert!(command.get_envs().any(|(key, value)| {
        key == tau_harness::EPHEMERAL_ENV && value.and_then(|v| v.to_str()) == Some("1")
    }));
    assert!(
        command
            .get_envs()
            .any(|(key, value)| key == tau_harness::MEMORY_ONLY_ENV && value.is_none())
    );

    let command = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::Durable,
    });

    assert!(
        command
            .get_envs()
            .any(|(key, value)| { key == tau_harness::EPHEMERAL_ENV && value.is_none() })
    );
    assert!(
        command
            .get_envs()
            .any(|(key, value)| key == tau_harness::MEMORY_ONLY_ENV && value.is_none())
    );

    let command = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::MemoryOnly,
    });
    assert!(command.get_envs().any(|(key, value)| {
        key == tau_harness::MEMORY_ONLY_ENV && value.and_then(|v| v.to_str()) == Some("1")
    }));
    assert!(
        command
            .get_envs()
            .any(|(key, value)| key == tau_harness::EPHEMERAL_ENV && value.is_none())
    );
}

/// The preview-only agent-store marker must be set for diagnostics and cleared
/// for both ordinary durable and global session-ephemeral launches.
#[test]
fn daemon_command_scopes_memory_only_agent_store_to_previews() {
    for storage_mode in [
        HarnessStorageMode::Durable,
        HarnessStorageMode::SessionEphemeral,
    ] {
        let mut command = build_daemon_command(DaemonCommandSpec {
            tau_binary: Path::new("tau"),
            session_id: "session-1",
            session_status: SessionLaunchStatus::New,
            stdout: Stdio::null(),
            stderr: Stdio::null(),
            stdin: Stdio::null(),
            startup_role: None,
            cli_overrides: DaemonCliOverrides {
                profile: None,
                role: &[],
                extension: &[],
                extension_environment: None,
                harness_config: &[],
                memory_only_agent_store: false,
            },
            storage_mode,
        });
        command.env(tau_harness::MEMORY_ONLY_AGENT_STORE_ENV, "ambient");
        configure_agent_store_mode(&mut command, false);
        assert!(command.get_envs().any(|(key, value)| {
            key == tau_harness::MEMORY_ONLY_AGENT_STORE_ENV && value.is_none()
        }));
    }

    let mut preview = Command::new("tau");
    configure_agent_store_mode(&mut preview, true);
    assert!(preview.get_envs().any(|(key, value)| {
        key == tau_harness::MEMORY_ONLY_AGENT_STORE_ENV
            && value.and_then(std::ffi::OsStr::to_str) == Some("1")
    }));
}

#[test]
fn daemon_command_uses_initial_ui_stdio() {
    let command = build_daemon_command(DaemonCommandSpec {
        tau_binary: Path::new("tau"),
        session_id: "session-1",
        session_status: SessionLaunchStatus::New,
        stdout: Stdio::null(),
        stderr: Stdio::null(),
        stdin: Stdio::null(),
        startup_role: None,
        cli_overrides: DaemonCliOverrides {
            profile: None,
            role: &[],
            extension: &[],
            extension_environment: None,
            harness_config: &[],
            memory_only_agent_store: false,
        },
        storage_mode: HarnessStorageMode::Durable,
    });

    let args = command
        .get_args()
        .map(|arg| arg.to_string_lossy().into_owned())
        .collect::<Vec<_>>();
    assert_eq!(args, ["component", "harness", "--initial-ui-stdio"]);
}

/// Only conversational chat launches mark their owned stdio client as eligible
/// for the welcome; preview and one-shot callers retain the cleared default.
#[test]
fn introduction_notice_marker_is_explicit_and_reversible() {
    let mut command = Command::new("tau");
    configure_introduction_notice(&mut command, true);
    assert!(command.get_envs().any(|(key, value)| {
        key == tau_harness::INITIAL_UI_INTRODUCTION_NOTICE_ENV
            && value.and_then(std::ffi::OsStr::to_str) == Some("1")
    }));

    configure_introduction_notice(&mut command, false);
    assert!(command.get_envs().any(|(key, value)| {
        key == tau_harness::INITIAL_UI_INTRODUCTION_NOTICE_ENV && value.is_none()
    }));
}

/// The production output constructors distinguish conversational chat from
/// prompt-stdin and render-preview launches before command construction.
#[test]
fn chat_output_opts_into_introduction_while_headless_output_does_not() {
    let temp = tempfile::tempdir().expect("tempdir");
    let chat = daemon_output_for_chat_session_in(
        temp.path(),
        "chat",
        HarnessStorageMode::MemoryOnly,
        SessionLaunchStatus::New,
    )
    .expect("chat output");
    let headless = daemon_output_for_session_in(
        temp.path(),
        "preview",
        HarnessStorageMode::MemoryOnly,
        SessionLaunchStatus::New,
    )
    .expect("headless output");
    assert!(chat.introduction_notice_eligible);
    assert!(!headless.introduction_notice_eligible);
}

/// Session minting must normalize arbitrary filesystem basenames into the
/// strict protocol grammar without exceeding its byte limit.
#[test]
fn mint_session_id_produces_store_safe_ids_from_hostile_basenames() {
    // Session ids are used as tau-core store directory names. Cwd basenames can
    // contain characters that are valid on Unix but forbidden by the store
    // grammar, and can be too long once the random suffix is appended.
    let id = mint_session_id(Path::new("project\\name"));
    assert!(id.starts_with("project_name-"));
    assert!(!id.contains('\\'));

    let long = format!("my project.{}", "é".repeat(100));
    let id = mint_session_id(Path::new(&long));
    assert!(id.len() <= tau_proto::SESSION_SCOPED_ID_MAX_LEN);
    assert!(id.ends_with(|ch: char| ch.is_ascii_alphanumeric()));
}