hird 0.3.1

Cross-harness agent work queue and shared assertion memory, backed by one local SQLite database
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
//! Who is acting, and on which project.
//!
//! Both answers come from the environment the harness starts `hird` in, so a
//! Claude Code session and a Codex session pointed at the same checkout land on
//! the same project scope while staying distinguishable as actors. The one
//! thing the environment does not always say is the harness's name, and there
//! the client's own name for itself stands in — see [`AgentId`].
//!
//! An actor answers *what* is acting. `HIRD_IDENTITY` adds *who*: an optional
//! principal, prefixed as `<principal>/<harness>:<session>`, for a queue more
//! than one person files into. It is recorded and reported; it steers nothing.
//! Recusal keeps barring the harness, because §15's bar is about models rather
//! than people — two people on one harness are still one model reading its own
//! work. Absent the variable the actor string is exactly what it was.

use std::path::{Path, PathBuf};
use std::sync::OnceLock;

use ulid::Ulid;

/// Environment variable naming the harness, set in each harness's MCP config.
pub const HARNESS_ENV: &str = "HIRD_HARNESS";
/// Environment variable naming the person a session acts for.
pub const IDENTITY_ENV: &str = "HIRD_IDENTITY";
/// Environment variable naming the model a session runs, recorded on its
/// events. Self-reported: the harness knows, hird can only be told.
pub const MODEL_ENV: &str = "HIRD_MODEL";
/// Environment variable overriding project detection.
pub const PROJECT_ENV: &str = "HIRD_PROJECT";
/// Environment variable overriding the database path.
pub const DB_ENV: &str = "HIRD_DB";

/// Actor string recorded for CLI actions.
pub const ACTOR_CLI: &str = "cli";
/// Actor string recorded for TUI actions.
pub const ACTOR_TUI: &str = "tui";
/// Actor string recorded for what `hird web` does on the human's behalf —
/// sweeping leases and looking at the tree, never anything a task would call
/// a decision.
pub const ACTOR_WEB: &str = "web";

/// Longest harness name recorded in an actor string. A name is a badge in a
/// TUI column, not a payload, and it arrives over the wire in the MCP case.
const HARNESS_MAX: usize = 32;

/// Longest principal recorded in an actor string, on the same reasoning as
/// [`HARNESS_MAX`]: a name in a column, not a payload.
const PRINCIPAL_MAX: usize = 32;
/// Longest model name kept; model ids are long, but not unboundedly so.
const MODEL_MAX: usize = 96;

/// Separates the principal from the harness in an actor string. Stripped by
/// [`sanitize`] from every component, so no name can forge a second one.
const PRINCIPAL_SEP: char = '/';

/// A `[<principal>/]<harness>:<session>` identity for one MCP session.
///
/// The session half is minted once, when the process starts. The harness half
/// is whatever `HIRD_HARNESS` says; when the environment does not say, it is
/// taken from the first client that names itself — MCP 2026-07-28 carries the
/// client's implementation on every request, so a harness configured by hand
/// still arrives with a name instead of being filed as `unknown`.
///
/// Taken once and then latched, not re-read per call. An actor string that
/// changed mid-session would leave this process unable to find its own leases.
///
/// The principal — *who* the session acts for — comes from `HIRD_IDENTITY` and
/// nowhere else. Deliberately not from the client: a harness may name itself,
/// because being wrong about that costs a badge, but a client that could name
/// the person would be a client that could sign another person's work. The
/// human sets it where they set the rest of the registration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AgentId {
    /// Unset until the environment or a client supplies a usable name.
    harness: OnceLock<String>,
    /// Who this session acts for. `None` until somebody says, and no client may.
    principal: Option<String>,
    /// The model this session runs, when anybody said. Recorded, never parsed
    /// out of the actor string, so no reader of that string has to change.
    model: Option<String>,
    session: String,
}

impl AgentId {
    /// Mint an identity for a named harness without consulting the
    /// environment, for a process whose environment is not the caller's.
    ///
    /// An empty name leaves the harness unset, so a client that names itself
    /// still supplies one — the fallback §1.6 added, and the one a server has
    /// to rely on because it cannot see the far end's configuration.
    pub fn fresh(harness: impl Into<String>) -> AgentId {
        AgentId::new(harness, short_session_id())
    }

    /// Read the harness and principal from the environment and mint a fresh
    /// session suffix.
    pub fn from_env() -> AgentId {
        AgentId::new(
            std::env::var(HARNESS_ENV).unwrap_or_default(),
            short_session_id(),
        )
        .acting_for(std::env::var(IDENTITY_ENV).unwrap_or_default())
        .running(std::env::var(MODEL_ENV).unwrap_or_default())
    }

    pub fn new(harness: impl Into<String>, session: impl Into<String>) -> AgentId {
        let id = AgentId {
            harness: OnceLock::new(),
            principal: None,
            model: None,
            session: sanitize(&session.into()),
        };
        id.name_harness(&harness.into());
        id
    }

    /// Name the person this session acts for. An empty or unusable name leaves
    /// the identity as it was, which is the unattributed actor of every queue
    /// with one human on it.
    pub fn acting_for(mut self, principal: impl Into<String>) -> AgentId {
        self.principal = clamp(&principal.into(), PRINCIPAL_MAX);
        self
    }

    /// Who this session acts for, if anybody said.
    pub fn principal(&self) -> Option<&str> {
        self.principal.as_deref()
    }

    /// Name the model this session runs. Empty leaves it as it was, so an
    /// unset variable never erases a name somebody else supplied.
    pub fn running(mut self, model: impl Into<String>) -> AgentId {
        let model = model.into();
        let model = model.trim();
        if !model.is_empty() {
            self.model = Some(model.chars().take(MODEL_MAX).collect());
        }
        self
    }

    /// The model this session runs, if anybody said.
    pub fn model(&self) -> Option<&str> {
        self.model.as_deref()
    }

    /// Offer a name a client gave for itself, and say whether it was taken.
    ///
    /// Ignored once the identity has a name, so `HIRD_HARNESS` — set by
    /// `hird register`, and the only half of this the human controls — is never
    /// overridden by what a client calls itself.
    pub fn name_from_client(&self, client_name: &str) -> bool {
        self.name_harness(client_name)
    }

    fn name_harness(&self, raw: &str) -> bool {
        let Some(name) = clamp(raw, HARNESS_MAX) else {
            return false;
        };
        self.harness.set(name).is_ok()
    }

    /// The harness name alone, used for colour-coding badges in the TUI.
    pub fn harness(&self) -> &str {
        self.harness.get().map_or("unknown", String::as_str)
    }

    pub fn session(&self) -> &str {
        &self.session
    }

    /// The full `[principal/]harness:session` string stored on claims and
    /// assertions.
    pub fn as_actor(&self) -> String {
        match self.principal() {
            Some(who) => format!("{who}{PRINCIPAL_SEP}{}:{}", self.harness(), self.session),
            None => format!("{}:{}", self.harness(), self.session),
        }
    }
}

impl std::fmt::Display for AgentId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.as_actor())
    }
}

/// The harness part of an actor string, for badge rendering.
///
/// Non-agent actors (`cli`, `tui`, `hird`) have no colon and are returned
/// whole. A principal prefix is dropped: the badge answers *what* is acting,
/// and every reader of it — the TUI's colours, recusal's bar, the record's
/// rows — is asking about the model rather than the person.
pub fn actor_harness(actor: &str) -> &str {
    let what = actor.split_once(':').map_or(actor, |(what, _)| what);
    what.rsplit_once(PRINCIPAL_SEP)
        .map_or(what, |(_, harness)| harness)
}

/// The person an actor string was recorded for, if it names one.
///
/// `None` for the unattributed actors of a single-human queue, and for the
/// `cli`, `tui` and `web` actors, which are the human at the keyboard already.
pub fn actor_principal(actor: &str) -> Option<&str> {
    let what = actor.split_once(':').map_or(actor, |(what, _)| what);
    what.split_once(PRINCIPAL_SEP)
        .map(|(who, _)| who)
        .filter(|who| !who.is_empty())
}

/// Normalize a principal the way an identity would, for callers writing one
/// down before any session exists — `hird register`, chiefly.
///
/// `None` when nothing usable survives, so an empty `--identity` writes no
/// variable rather than an empty one.
pub fn principal_name(raw: &str) -> Option<String> {
    clamp(raw, PRINCIPAL_MAX)
}

/// Which half of an actor a reading groups by.
///
/// The record measures "whose work survives a reading by a different model"
/// (§16). With one human on a queue those two words mean the same thing and
/// [`Axis::Harness`] answers both. With more than one they come apart, and
/// only the reader knows which was meant — so this is a choice at the point of
/// reading, never a change to what is stored.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Axis {
    /// Group by the model that acted. Every actor has one.
    #[default]
    Harness,
    /// Group by the person it acted for. Unattributed actors have none, and a
    /// reading along this axis leaves them out rather than inventing a name.
    Person,
}

impl Axis {
    /// The key `actor` falls under, or `None` when it carries no such half.
    pub fn key<'a>(&self, actor: &'a str) -> Option<&'a str> {
        match self {
            Axis::Harness => Some(actor_harness(actor)),
            Axis::Person => actor_principal(actor),
        }
    }
}

/// Four lowercase characters of ULID randomness — enough to tell a handful of
/// concurrent sessions apart without making log lines unreadable.
fn short_session_id() -> String {
    let ulid = Ulid::generate().to_string().to_lowercase();
    ulid[ulid.len() - 4..].to_string()
}

/// Strip our separators and whitespace from identity components: the colon
/// splits `harness:session`, the slash splits the principal from the harness,
/// and the comma joins harness names in the herald's `HIRD_RECUSED` list.
///
/// Stripping rather than rejecting keeps a badly set variable a cosmetic
/// problem, and stripping the slash is what stops a harness or session name
/// from claiming to be somebody.
fn sanitize(raw: &str) -> String {
    raw.trim()
        .chars()
        .filter(|c| !c.is_whitespace() && !matches!(*c, ':' | ',' | PRINCIPAL_SEP))
        .collect()
}

/// The harness name an [`AgentId`] would carry for `raw`, or `None` if nothing
/// usable survives sanitizing.
///
/// Public so that a configuration naming harnesses — the server's roster,
/// which pins one per worker and keys defaults on the same names — matches
/// them the way the identity will record them, rather than by the spelling
/// the operator happened to type.
pub fn harness_name(raw: &str) -> Option<String> {
    clamp(raw, HARNESS_MAX)
}

/// Sanitize `raw` and cut it to `max` characters, or `None` if nothing usable
/// survives. Shared by the harness and the principal, which have the same
/// shape and the same reason for a limit.
fn clamp(raw: &str, max: usize) -> Option<String> {
    let mut name = sanitize(raw);
    if name.is_empty() {
        return None;
    }
    name.truncate(name.char_indices().nth(max).map_or(name.len(), |(i, _)| i));
    Some(name)
}

/// Resolve the project root a process is working in.
///
/// `HIRD_PROJECT` wins; otherwise the nearest enclosing directory containing a
/// `.git` entry; otherwise the working directory itself. The result is
/// canonicalized so two harnesses reaching the checkout by different symlinks
/// agree on the scope.
pub fn resolve_project(cwd: &Path) -> String {
    if let Ok(explicit) = std::env::var(PROJECT_ENV) {
        let explicit = explicit.trim();
        if !explicit.is_empty() {
            return canonical(Path::new(explicit));
        }
    }
    match git_toplevel(cwd) {
        Some(root) => canonical(&root),
        None => canonical(cwd),
    }
}

/// Walk up from `start` looking for a `.git` directory or worktree file.
///
/// Done by directory walk rather than by shelling out to `git`, because MCP
/// servers are spawned per session and must start fast.
fn git_toplevel(start: &Path) -> Option<PathBuf> {
    let mut dir = Some(start);
    while let Some(current) = dir {
        if current.join(".git").exists() {
            return Some(current.to_path_buf());
        }
        dir = current.parent();
    }
    None
}

/// Canonicalize a user-supplied project path the same way detection does, so
/// `--project .` and automatic detection agree on the scope string.
pub fn canonical_project(path: &Path) -> String {
    canonical(path)
}

fn canonical(path: &Path) -> String {
    let resolved = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    resolved.to_string_lossy().into_owned()
}

/// The default database path: `${XDG_DATA_HOME:-~/.local/share}/hird/hird.db`.
pub fn default_db_path() -> PathBuf {
    data_dir().join("hird").join("hird.db")
}

/// The default config path: `${XDG_CONFIG_HOME:-~/.config}/hird/config.toml`.
pub fn default_config_path() -> PathBuf {
    config_dir().join("hird").join("config.toml")
}

fn data_dir() -> PathBuf {
    non_empty_env("XDG_DATA_HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| home().join(".local").join("share"))
}

pub(crate) fn config_dir() -> PathBuf {
    non_empty_env("XDG_CONFIG_HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| home().join(".config"))
}

/// The home directory, or the current one when the environment will not say.
pub fn home() -> PathBuf {
    non_empty_env("HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("."))
}

fn non_empty_env(key: &str) -> Option<String> {
    std::env::var(key).ok().filter(|v| !v.trim().is_empty())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn agent_ids_render_as_harness_colon_session() {
        let id = AgentId::new("claude-code", "af31");
        assert_eq!(id.as_actor(), "claude-code:af31");
        assert_eq!(id.to_string(), "claude-code:af31");
        assert_eq!(id.harness(), "claude-code");
        assert_eq!(id.session(), "af31");
    }

    #[test]
    fn a_missing_harness_name_becomes_unknown() {
        assert_eq!(AgentId::new("", "af31").harness(), "unknown");
        assert_eq!(AgentId::new("   ", "af31").harness(), "unknown");
    }

    #[test]
    fn a_nameless_identity_takes_the_name_the_client_gives_for_itself() {
        let id = AgentId::new("", "af31");
        assert!(id.name_from_client("codex-cli"));
        assert_eq!(id.as_actor(), "codex-cli:af31");
    }

    #[test]
    fn the_environment_outranks_whatever_the_client_calls_itself() {
        let id = AgentId::new("claude-code", "af31");
        assert!(!id.name_from_client("something-else"));
        assert_eq!(id.harness(), "claude-code");
    }

    #[test]
    fn the_first_client_name_is_the_one_that_sticks() {
        let id = AgentId::new("", "af31");
        assert!(id.name_from_client("codex-cli"));
        assert!(!id.name_from_client("copilot"));
        assert_eq!(id.harness(), "codex-cli");
    }

    #[test]
    fn a_client_that_names_itself_nothing_leaves_the_identity_open() {
        let id = AgentId::new("", "af31");
        assert!(!id.name_from_client("  "));
        assert_eq!(id.harness(), "unknown");
        assert!(id.name_from_client("cursor"));
        assert_eq!(id.harness(), "cursor");
    }

    #[test]
    fn a_client_cannot_make_its_name_an_essay() {
        let id = AgentId::new("", "af31");
        id.name_from_client(&"x".repeat(500));
        assert_eq!(id.harness().len(), HARNESS_MAX);
    }

    #[test]
    fn a_client_name_cannot_forge_a_second_field_either() {
        let id = AgentId::new("", "af31");
        id.name_from_client("cla ude:code");
        assert_eq!(id.as_actor(), "claudecode:af31");
    }

    #[test]
    fn colons_and_whitespace_cannot_forge_a_second_field() {
        let id = AgentId::new("cla ude:code", "af:31");
        assert_eq!(id.as_actor(), "claudecode:af31");
    }

    /// The comma joins harness names in `HIRD_RECUSED`, so a name carrying
    /// one could smuggle a second entry into the herald's list.
    #[test]
    fn commas_cannot_forge_an_entry_in_the_recused_list() {
        let id = AgentId::new("codex,claude-code", "af31");
        assert_eq!(id.harness(), "codexclaude-code");
    }

    #[test]
    fn session_ids_are_four_characters_and_differ() {
        let a = short_session_id();
        let b = short_session_id();
        assert_eq!(a.len(), 4);
        assert!(a.chars().all(|c| c.is_ascii_alphanumeric()));
        // ULID randomness makes a collision here a 1-in-a-million event, but
        // the point of the assertion is that ids are not constant.
        assert!(a != b || short_session_id() != a);
    }

    #[test]
    fn actor_harness_splits_agents_and_passes_humans_through() {
        assert_eq!(actor_harness("claude-code:af31"), "claude-code");
        assert_eq!(actor_harness("cli"), "cli");
        assert_eq!(actor_harness("tui"), "tui");
    }

    #[test]
    fn actor_harness_looks_past_the_principal() {
        // Every reader of a badge asks what model acted, so the answer must not
        // change when a queue starts recording who it acted for.
        assert_eq!(actor_harness("ana/claude-code:af31"), "claude-code");
        assert_eq!(actor_harness("ana/claude-code"), "claude-code");
    }

    #[test]
    fn actor_principal_names_who_acted_or_nobody() {
        assert_eq!(actor_principal("ana/claude-code:af31"), Some("ana"));
        assert_eq!(actor_principal("claude-code:af31"), None);
        assert_eq!(actor_principal("cli"), None);
    }

    #[test]
    fn an_identity_without_a_principal_is_the_actor_it_always_was() {
        let id = AgentId::new("claude-code", "af31");
        assert_eq!(id.as_actor(), "claude-code:af31");
        assert_eq!(id.principal(), None);
        assert_eq!(id.to_string(), id.as_actor());
    }

    #[test]
    fn an_identity_with_a_principal_carries_it_in_the_actor() {
        let id = AgentId::new("claude-code", "af31").acting_for("ana");
        assert_eq!(id.as_actor(), "ana/claude-code:af31");
        assert_eq!(id.principal(), Some("ana"));
        assert_eq!(actor_harness(&id.as_actor()), "claude-code");
        assert_eq!(actor_principal(&id.as_actor()), Some("ana"));
    }

    #[test]
    fn an_empty_principal_leaves_the_identity_unattributed() {
        let id = AgentId::new("claude-code", "af31").acting_for("   ");
        assert_eq!(id.principal(), None);
        assert_eq!(id.as_actor(), "claude-code:af31");
    }

    #[test]
    fn no_component_can_forge_a_second_separator() {
        // A harness that could write a slash could claim to be someone; a
        // principal that could write a colon could claim to be a session.
        let id = AgentId::new("ben/claude-code", "af:31").acting_for("ana/root:x");
        assert_eq!(id.as_actor(), "anarootx/benclaude-code:af31");
        assert_eq!(actor_principal(&id.as_actor()), Some("anarootx"));
        assert_eq!(actor_harness(&id.as_actor()), "benclaude-code");
    }

    #[test]
    fn a_long_principal_is_cut_to_a_column_width() {
        let id = AgentId::new("claude-code", "af31").acting_for("a".repeat(80));
        assert_eq!(id.principal().unwrap().chars().count(), PRINCIPAL_MAX);
    }

    #[test]
    fn a_client_may_name_the_harness_but_never_the_person() {
        let id = AgentId::new("", "af31");
        assert!(id.name_from_client("codex"));
        assert_eq!(id.harness(), "codex");
        // There is no path from a client's words to the principal: naming one
        // takes an owned identity, which only the process's own environment
        // gets to build.
        assert_eq!(id.principal(), None);
    }

    #[test]
    fn project_detection_finds_the_enclosing_git_root() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        std::fs::create_dir(root.join(".git")).unwrap();
        let nested = root.join("crates").join("inner");
        std::fs::create_dir_all(&nested).unwrap();

        let found = git_toplevel(&nested).unwrap();
        assert_eq!(
            std::fs::canonicalize(found).unwrap(),
            std::fs::canonicalize(root).unwrap()
        );
    }

    #[test]
    fn project_detection_falls_back_to_the_directory_itself() {
        let dir = tempfile::tempdir().unwrap();
        assert!(git_toplevel(dir.path()).is_none());
    }

    #[test]
    fn worktree_git_files_count_as_a_root() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".git"), "gitdir: /elsewhere").unwrap();
        assert!(git_toplevel(dir.path()).is_some());
    }

    #[test]
    fn default_paths_sit_under_the_hird_directory() {
        assert!(default_db_path().ends_with("hird/hird.db"));
        assert!(default_config_path().ends_with("hird/config.toml"));
    }
}