(ca2a124) Add --actor-from-git beside every --actor, and bump to 0.32.8
This commit is contained in:
+123
-46
@@ -449,52 +449,78 @@ pub const ACTOR_ENV: &str = "WORKLANE_ACTOR";
|
||||
/// The author written when nothing names one.
|
||||
pub const UNKNOWN_ACTOR: &str = "unknown";
|
||||
|
||||
/// The actor for the whole run: the variable, then the git user.
|
||||
/// The actor for the whole run, from the variable alone.
|
||||
///
|
||||
/// Split out from the caching wrapper for the reason `colors_wanted` is: a threaded test
|
||||
/// binary cannot mutate the process environment to reach one branch at a time.
|
||||
///
|
||||
/// An empty `WORKLANE_ACTOR` counts as unset, the convention `colors_wanted` follows for
|
||||
/// `NO_COLOR`, so a wrapper script can clear an inherited one. `git_user` is a closure
|
||||
/// rather than a value because the fallback costs a subprocess and the variable usually
|
||||
/// answers first.
|
||||
fn session_actor(env: Option<&str>, git_user: impl FnOnce() -> Option<String>) -> Option<String> {
|
||||
match env.map(str::trim).filter(|value| !value.is_empty()) {
|
||||
Some(value) => Some(value.to_string()),
|
||||
None => git_user(),
|
||||
}
|
||||
/// `NO_COLOR`, so a wrapper script can clear an inherited one. This is the half the
|
||||
/// [`resolve_actor`] cache holds; the git user lives in a cache of its own, because with
|
||||
/// `--actor-from-git` it has to be fetched even though the variable answered.
|
||||
fn session_actor(env: Option<&str>) -> Option<String> {
|
||||
env.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(str::to_string)
|
||||
}
|
||||
|
||||
/// The actor for one entry: the flag wins, otherwise the session's, otherwise unknown.
|
||||
/// The actor for one entry, from what named one.
|
||||
///
|
||||
/// Free text, validated against nothing, the way the `chown` user is: there is no
|
||||
/// registry of actors to check a name against.
|
||||
fn entry_actor(flag: Option<&str>, session: Option<&str>) -> String {
|
||||
[flag, session]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(str::trim)
|
||||
.find(|value| !value.is_empty())
|
||||
.unwrap_or(UNKNOWN_ACTOR)
|
||||
.to_string()
|
||||
///
|
||||
/// The chain depends on the flag. An explicit `--actor` wins outright either way. With
|
||||
/// `from_git`, the git user is lifted above the session rather than added beneath it:
|
||||
/// the flag says attribute this to the human, and a machine with no git user configured
|
||||
/// has nobody to name, which reads [`UNKNOWN_ACTOR`] rather than quietly attributing
|
||||
/// back to the agent. Without it, today's chain: the variable, then the git user.
|
||||
///
|
||||
/// `git_user` is a closure in both branches so the subprocess it costs is not run when
|
||||
/// something ahead of it already answered.
|
||||
fn entry_actor(
|
||||
flag: Option<&str>,
|
||||
from_git: bool,
|
||||
session: Option<&str>,
|
||||
git_user: impl FnOnce() -> Option<String>,
|
||||
) -> String {
|
||||
if let Some(name) = flag.map(str::trim).filter(|value| !value.is_empty()) {
|
||||
return name.to_string();
|
||||
}
|
||||
|
||||
if from_git {
|
||||
return git_user().unwrap_or_else(|| UNKNOWN_ACTOR.to_string());
|
||||
}
|
||||
|
||||
if let Some(name) = session.map(str::trim).filter(|value| !value.is_empty()) {
|
||||
return name.to_string();
|
||||
}
|
||||
|
||||
git_user().unwrap_or_else(|| UNKNOWN_ACTOR.to_string())
|
||||
}
|
||||
|
||||
/// Who to attribute an entry to, from the flag the call carried.
|
||||
/// Who to attribute an entry to, from the flags the call carried.
|
||||
///
|
||||
/// Precedence: flag, then `WORKLANE_ACTOR`, then the git user, then `unknown`. Every
|
||||
/// entry the CLI writes goes through here, so a board several agents write to says which
|
||||
/// of them wrote what instead of attributing all of it to one human.
|
||||
pub fn resolve_actor(flag: Option<&str>) -> String {
|
||||
// The session half is decided once, the way should_use_colors is: neither input can
|
||||
// change mid-run, and the git user costs a subprocess.
|
||||
static SESSION: OnceLock<Option<String>> = OnceLock::new();
|
||||
/// Precedence: flag, then — unless `from_git` lifted it — `WORKLANE_ACTOR`, then the git
|
||||
/// user, then `unknown`. Every entry the CLI writes goes through here, so a board
|
||||
/// several agents write to says which of them wrote what instead of attributing all of
|
||||
/// it to one human.
|
||||
pub fn resolve_actor(flag: Option<&str>, from_git: bool) -> String {
|
||||
// Each half is decided once, the way should_use_colors is: neither input can change
|
||||
// mid-run, and the git user costs a subprocess. They are two caches rather than one,
|
||||
// because baking the git user into the session's answer would leave `--actor-from-git`
|
||||
// reading whatever the run's *first* call produced — with the variable set, that call
|
||||
// never fetched git at all.
|
||||
static VARIABLE: OnceLock<Option<String>> = OnceLock::new();
|
||||
static GIT_USER: OnceLock<Option<String>> = OnceLock::new();
|
||||
|
||||
let session = SESSION.get_or_init(|| {
|
||||
let session = VARIABLE.get_or_init(|| {
|
||||
let env = std::env::var(ACTOR_ENV).ok();
|
||||
session_actor(env.as_deref(), get_git_user)
|
||||
session_actor(env.as_deref())
|
||||
});
|
||||
|
||||
entry_actor(flag, session.as_deref())
|
||||
entry_actor(flag, from_git, session.as_deref(), || {
|
||||
GIT_USER.get_or_init(get_git_user).clone()
|
||||
})
|
||||
}
|
||||
/// One file a search matched, and where it was found.
|
||||
pub struct Found {
|
||||
@@ -3678,48 +3704,99 @@ mod tests {
|
||||
/// closure returns a name that must not appear.
|
||||
#[test]
|
||||
fn the_variable_names_the_actor() {
|
||||
let session = session_actor(Some("claude"), || Some("Ansho Rei".to_string()));
|
||||
assert_eq!(entry_actor(None, session.as_deref()), "claude");
|
||||
let session = session_actor(Some("claude"));
|
||||
let git_user = || Some("Ansho Rei".to_string());
|
||||
assert_eq!(entry_actor(None, false, session.as_deref(), git_user), "claude");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_flag_overrides_the_variable() {
|
||||
let session = session_actor(Some("claude"), || None);
|
||||
assert_eq!(entry_actor(Some("opencode"), session.as_deref()), "opencode");
|
||||
let session = session_actor(Some("claude"));
|
||||
assert_eq!(
|
||||
entry_actor(Some("opencode"), false, session.as_deref(), || None),
|
||||
"opencode"
|
||||
);
|
||||
}
|
||||
|
||||
/// With neither supplied the author is still the git user.
|
||||
#[test]
|
||||
fn the_git_user_is_the_fallback() {
|
||||
let session = session_actor(None, || Some("Ansho Rei".to_string()));
|
||||
assert_eq!(entry_actor(None, session.as_deref()), "Ansho Rei");
|
||||
let git_user = || Some("Ansho Rei".to_string());
|
||||
assert_eq!(
|
||||
entry_actor(None, false, session_actor(None).as_deref(), git_user),
|
||||
"Ansho Rei"
|
||||
);
|
||||
}
|
||||
|
||||
/// An empty variable counts as unset, the way `NO_COLOR` does, so a wrapper script
|
||||
/// can clear an inherited one and get the git user back.
|
||||
/// can clear an inherited one and get the git user back. Whitespace is the same
|
||||
/// case: a shell that quotes a missing value writes one.
|
||||
#[test]
|
||||
fn an_empty_variable_reads_as_unset() {
|
||||
let session = session_actor(Some(""), || Some("Ansho Rei".to_string()));
|
||||
assert_eq!(entry_actor(None, session.as_deref()), "Ansho Rei");
|
||||
// Whitespace is the same case: a shell that quotes a missing value writes one
|
||||
let session = session_actor(Some(" "), || Some("Ansho Rei".to_string()));
|
||||
assert_eq!(entry_actor(None, session.as_deref()), "Ansho Rei");
|
||||
let git_user = || Some("Ansho Rei".to_string());
|
||||
for raw in ["", " "] {
|
||||
let session = session_actor(Some(raw));
|
||||
assert_eq!(entry_actor(None, false, session.as_deref(), git_user), "Ansho Rei");
|
||||
}
|
||||
}
|
||||
|
||||
/// And with nothing at all, including a git that has no user configured.
|
||||
#[test]
|
||||
fn nothing_at_all_is_unknown() {
|
||||
let session = session_actor(None, || None);
|
||||
assert_eq!(entry_actor(None, session.as_deref()), UNKNOWN_ACTOR);
|
||||
let session = session_actor(None);
|
||||
assert_eq!(entry_actor(None, false, session.as_deref(), || None), UNKNOWN_ACTOR);
|
||||
// An empty flag falls through rather than writing a blank author
|
||||
assert_eq!(entry_actor(Some(""), None), UNKNOWN_ACTOR);
|
||||
assert_eq!(entry_actor(Some(""), false, None, || None), UNKNOWN_ACTOR);
|
||||
}
|
||||
|
||||
/// Free text, validated against nothing, the way the `chown` user is.
|
||||
#[test]
|
||||
fn an_actor_is_free_text() {
|
||||
let session = session_actor(Some("claude-code, session 3 (opus)"), || None);
|
||||
assert_eq!(entry_actor(None, session.as_deref()), "claude-code, session 3 (opus)");
|
||||
let session = session_actor(Some("claude-code, session 3 (opus)"));
|
||||
assert_eq!(
|
||||
entry_actor(None, false, session.as_deref(), || None),
|
||||
"claude-code, session 3 (opus)"
|
||||
);
|
||||
}
|
||||
|
||||
/// The flag is the caller acting for the person whose checkout this is: the git user
|
||||
/// is lifted above the variable rather than added beneath it.
|
||||
#[test]
|
||||
fn from_git_lifts_the_git_user_over_the_variable() {
|
||||
let session = session_actor(Some("claude"));
|
||||
let git_user = || Some("Ansho Rei".to_string());
|
||||
assert_eq!(entry_actor(None, true, session.as_deref(), git_user), "Ansho Rei");
|
||||
}
|
||||
|
||||
/// An explicit `--actor` still wins outright beside the lift: it names somebody
|
||||
/// specific, which is the stronger statement of the two.
|
||||
#[test]
|
||||
fn the_flag_wins_over_from_git_too() {
|
||||
let session = session_actor(Some("claude"));
|
||||
let git_user = || Some("Ansho Rei".to_string());
|
||||
assert_eq!(
|
||||
entry_actor(Some("opencode"), true, session.as_deref(), git_user),
|
||||
"opencode"
|
||||
);
|
||||
}
|
||||
|
||||
/// The lift is not a fallback: asked to name the human and finding none configured,
|
||||
/// the entry reads unknown instead of quietly attributing back to the agent.
|
||||
#[test]
|
||||
fn from_git_with_no_git_user_is_unknown_not_the_variable() {
|
||||
let session = session_actor(Some("claude"));
|
||||
assert_eq!(entry_actor(None, true, session.as_deref(), || None), UNKNOWN_ACTOR);
|
||||
}
|
||||
|
||||
/// An empty variable still counts as unset with the lift on, which here means the
|
||||
/// git user answers as usual.
|
||||
#[test]
|
||||
fn from_git_leaves_the_empty_variable_unset() {
|
||||
let git_user = || Some("Ansho Rei".to_string());
|
||||
assert_eq!(
|
||||
entry_actor(None, true, session_actor(Some("")).as_deref(), git_user),
|
||||
"Ansho Rei"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user