Author SHA1 Message Date
Omar SobhandClaude Opus 5 5232175c88 fix(missions): forward the subscription token into mission containers
ci / gates (push) Failing after 9s
ci / rust (push) Skipped
ci / frontend (push) Skipped
ci / e2e (push) Skipped
ci / publish (push) Skipped
Switching agents to claude_cli left missions hanging: the per-mission
container had claude_cli configured but no credential, so `claude -p`
waited forever. A phase sat at `running` for ten minutes with nothing in
the logs — no error, because there is nothing to error on.

The original subscription design assumed a persisted `claude /login`
under a bind-mounted $HOME. That holds for the shared runtime and NOT for
a mission container, which gets its own data dir and therefore no login.
So subscription mode now forwards CLAUDE_CODE_OAUTH_TOKEN.

The two Anthropic credentials remain mutually exclusive, and there is now
a test asserting it in both directions: Claude Code ranks ANTHROPIC_API_KEY
above the OAuth token, so shipping both bills the API while the deployment
believes it is on the subscription — visible only on the invoice.

Deployment: CLAWMATES_RUNTIME_AUTH=subscription and CLAUDE_CODE_OAUTH_TOKEN
added to compose + .env on gw-04.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-03 17:34:13 -07:00
Omar SobhandClaude Opus 5 ac47dcbe94 feat(runtime): run agents on the subscription via the real claude binary
ci / gates (push) Failing after 5s
ci / rust (push) Skipped
ci / frontend (push) Skipped
ci / e2e (push) Skipped
ci / publish (push) Skipped
provider_alias_for now resolves Claude models to `claude_cli.default`,
which spawns the actual `claude` binary, instead of `anthropic.default`,
which posts to the raw API with Claude Code identity headers. Agent work
is ~99% of tokens, so this moves essentially all of it onto the Max
subscription and onto the supported client.

The judge deliberately stays on the API key. If both rode one credential,
a single subscription limit would blind the verifier at exactly the
moment there is most to verify; this way a throttle degrades missions but
verification keeps working.

Runtime config (applied on gw-04, reloaded via loopback — remote admin
reload is disabled by design):
  - [providers.models.claude_cli.default] with mcp_config pointing at the
    §15 door, so a subscription agent can ACT and not merely reason
  - disallowed_tools denies Claude Code's own Bash/Write/Edit/WebFetch so
    the gated door is the ONLY actuator and nothing bypasses the audit log
  - env CLAUDE_CODE_OAUTH_TOKEN = "$CLAUDE_CODE_OAUTH_TOKEN" — the $NAME
    form reads the daemon env, keeping the token out of config.toml
  - anthropic.judge swapped to the API key (0 oat01 left in config)

Verified before changing anything: the real binary returned SUBSCRIPTION-OK
through the token, then ENV-OK once the daemon carried it in env.

Note for future readers: /api/config/prop reflects what is CONFIGURED, not
what the binary supports — `openai` 404s there too. An earlier note that
the image "has no claude_cli in its schema" was true of the old :sync
image and is not true of the rebuilt one.

400 tests, clippy clean.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-03 17:14:42 -07:00
Omar SobhandClaude Opus 5 ad89ef94cd feat(library): attribute a run to a mission, and prove what it contributed
ci / gates (push) Failing after 7s
ci / rust (push) Skipped
ci / frontend (push) Skipped
ci / e2e (push) Skipped
ci / publish (push) Skipped
`corpus_items.mission_id` has existed since the table landed and nothing
could populate it. `POST /api/library/runs` now accepts `missionId`, which
is the seam the wizard needs: a mission-driven run is the same run, tagged.

`corpus::contributed()` answers the question a continuous mission has to
be able to answer — did THIS run add anything new. Because `record` never
reassigns mission_id on conflict, the mission that first found a source
keeps the credit, so a rerun cannot inflate its own count by re-recording
what an earlier run already held. The test asserts exactly that: two
missions see the same paper, the finder reports 1 and the rerun reports 0.

This is the check the 0030-0044 generation of continuous research did not
have. It could run weekly forever and every run looked like success.

The test also earned its FK: the first version attributed to a bare UUID
and the database refused it. Attribution to a mission that does not exist
is not attribution, so the test now seeds real mission rows.

400 tests, clippy clean.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-03 11:43:41 -07:00
Omar SobhandClaude Opus 5 9de2cf34e4 feat(auto-merge): merge additive branches, refuse everything else
ci / gates (push) Failing after 10s
ci / rust (push) Skipped
ci / frontend (push) Skipped
ci / e2e (push) Skipped
ci / publish (push) Skipped
Closes the branch pile-up: a catalogue branch that only adds notes now
merges into main by itself, so the work is actually in the vault rather
than waiting in a branch nobody opened.

Additive-only is measured from the diff, not assumed from the mission
type. Three conditions, all required: the type declares additive_only,
the run verified, and `git diff --name-status base...branch` contains
only A entries. A research harvest that somehow rewrote a hand-written
note is refused by the same check that lets its new notes through —
which is the case the test pins down, asserting README.md on main is
byte-identical afterwards.

Renames and deletes count as non-additive. A rename is a delete plus an
add and the delete half can destroy hand-written work.

Unknown merge_policy values fail closed to Never. A typo must not grant
auto-merge.

The diff is taken against FETCH_HEAD, freshly fetched, using `...` so an
unrelated commit landing on main meanwhile is not misread as ours. A
conflicted merge aborts and leaves the branch for a human rather than
wedging the checkout for the next run.

merge_reason is always populated and surfaced in the API: a branch that
quietly did not merge is indistinguishable from one never delivered.

399 tests, clippy clean.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-03 11:33:41 -07:00
Omar SobhandClaude Opus 5 3124fd3c8f feat(library): weekly harvest on a systemd timer
ci / gates (push) Failing after 7s
ci / rust (push) Skipped
ci / frontend (push) Skipped
ci / e2e (push) Skipped
ci / publish (push) Skipped
Monday 07:00, Persistent=true so a week missed to downtime fires on next
boot rather than leaving a silently empty library. 30-minute timeout so a
wedged run cannot hold the slot until the following week.

The script is deliberately thin — it calls the API and reports — so it
never needs changing when the harvest does. Auth is a long-lived operator
session in /etc/clawmates/library.token (root-only, 600); rotate by
replacing the file.

Exit status follows `healthy`, not paper count. A mature library shelves
nothing most weeks and that is success; a run that errored is a failure
even if it shelved something.

The first manual fire caught a real bug in this script, in the opposite
direction to this week's usual: the harvest genuinely shelved 15 papers
and pushed them, and the reporter crashed on an escaped quote inside an
f-string, so systemd marked the unit FAILED. A false failure destroys
trust in the signal exactly as a false success does. The reporter now
avoids backslashes entirely (it is embedded in a single-quoted shell
string) and was proved against the real response shape before being
trusted.

Verified end to end on gw-04:
  run 1: 25 candidates, 10 already held, 15 shelved, pushed
  run 2: 25 candidates, 25 already held,  0 shelved, no branch, healthy

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-03 10:59:13 -07:00
Omar Sobh cf076bd8ea Merge: paper library — corpus, arXiv harvest, vault catalogue, API
ci / gates (push) Failing after 7s
ci / rust (push) Skipped
ci / frontend (push) Skipped
ci / e2e (push) Skipped
ci / publish (push) Skipped
2026-08-03 10:53:20 -07:00
12 changed files with 653 additions and 31 deletions
+222
View File
@@ -0,0 +1,222 @@
//! Merging a delivered branch into the base, when that is provably safe.
//!
//! Every mission type delivers to a branch and never to `main`. For most that
//! is where it should stop — a human reads the code and merges. But some
//! missions only ever *add* files in a folder they own: a paper catalogue, a
//! benchmark record. Those branches carry no judgement call, and leaving them
//! to pile up unmerged means the work is done but not actually in the vault.
//!
//! # Additive-only is a property, not a preference
//!
//! The gate is not "is this mission type trusted". It is measured from the
//! diff: if the branch modifies or deletes anything that already existed, it
//! does not qualify, whatever its template says. A research harvest that
//! somehow rewrote a hand-written note would be refused by the same check
//! that lets its new notes through.
//!
//! Three conditions, all required:
//!
//! 1. the mission type declares [`MergePolicy::AdditiveOnly`]
//! 2. verification passed — a run that did not prove its work does not merge
//! 3. the diff against the base contains only additions
//!
//! Anything else lands as a branch for a human, which is the existing
//! behaviour and the safe default.
use std::path::Path;
/// What a mission type is allowed to do with its own branch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MergePolicy {
/// Always leave the branch for a human. Correct for anything that touches
/// code: `refactor`, `research_and_code`, security patches.
Never,
/// Merge automatically when the diff is provably additive and the run
/// verified. Correct for catalogues and recorded measurements.
AdditiveOnly,
}
impl MergePolicy {
/// Parse a template's `merge_policy`. Unknown values fall back to `Never`
/// and say so: a typo must not silently grant auto-merge.
pub fn parse(raw: Option<&str>) -> MergePolicy {
match raw.map(str::trim) {
Some("additive_only") => MergePolicy::AdditiveOnly,
Some("never") | None => MergePolicy::Never,
Some(other) => {
eprintln!(
"auto_merge: unknown merge_policy {other:?} — refusing to auto-merge"
);
MergePolicy::Never
}
}
}
}
/// Why a branch was or was not merged. The reason is always recorded: a
/// branch that silently did not merge is indistinguishable from one that was
/// never delivered.
#[derive(Debug, Clone)]
pub struct MergeOutcome {
pub merged: bool,
pub reason: String,
}
impl MergeOutcome {
fn refused(reason: impl Into<String>) -> MergeOutcome {
MergeOutcome {
merged: false,
reason: reason.into(),
}
}
}
/// Classify a `git diff --name-status` body.
///
/// Returns the offending entries, empty when every change is an addition.
/// Split out so the rule is testable without a repository.
pub fn non_additive_changes(name_status: &str) -> Vec<String> {
name_status
.lines()
.filter(|l| !l.trim().is_empty())
.filter(|l| {
// Status is the first field: A/M/D/R###/C###.
!matches!(l.chars().next(), Some('A'))
})
.map(|l| l.trim().to_string())
.collect()
}
async fn git(repo: &Path, args: &[&str]) -> Result<String, String> {
let out = tokio::process::Command::new("git")
.arg("-C")
.arg(repo)
.args(["-c", &format!("safe.directory={}", repo.display())])
.args(args)
.env("GIT_AUTHOR_NAME", crate::mission_delivery::commit_identity().0)
.env("GIT_AUTHOR_EMAIL", crate::mission_delivery::commit_identity().1)
.env(
"GIT_COMMITTER_NAME",
crate::mission_delivery::commit_identity().0,
)
.env(
"GIT_COMMITTER_EMAIL",
crate::mission_delivery::commit_identity().1,
)
.output()
.await
.map_err(|e| format!("spawn git: {e}"))?;
if !out.status.success() {
return Err(format!(
"git {} → {}: {}",
args.first().copied().unwrap_or("?"),
out.status,
crate::mission_workspace::redact_token(&String::from_utf8_lossy(&out.stderr))
.chars()
.take(300)
.collect::<String>()
));
}
Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}
/// Merge `branch` into `base` and push, if all three conditions hold.
///
/// Never returns `Err` for a refusal — a refusal is a normal outcome with a
/// reason. `Err` is reserved for the merge itself going wrong after we decided
/// to attempt it.
pub async fn try_merge(
repo: &Path,
push_url: &str,
branch: &str,
base: &str,
policy: MergePolicy,
verified: bool,
) -> Result<MergeOutcome, String> {
if policy != MergePolicy::AdditiveOnly {
return Ok(MergeOutcome::refused(
"merge_policy is not additive_only; left for a human",
));
}
if !verified {
return Ok(MergeOutcome::refused(
"run did not verify; refusing to merge unproven work",
));
}
// Compare against the base as the REMOTE has it, not a local ref that may
// be stale. `...` gives changes on the branch since it diverged, so an
// unrelated commit landing on main meanwhile is not misread as ours.
git(repo, &["fetch", push_url, base]).await?;
let diff = git(
repo,
&["diff", "--name-status", &format!("FETCH_HEAD...{branch}")],
)
.await?;
let offending = non_additive_changes(&diff);
if !offending.is_empty() {
return Ok(MergeOutcome::refused(format!(
"diff is not additive ({} non-add change(s), first: {}); left for a human",
offending.len(),
offending.first().map(String::as_str).unwrap_or("?")
)));
}
if diff.trim().is_empty() {
return Ok(MergeOutcome::refused("branch adds nothing"));
}
// Merge onto the freshly fetched base rather than a local branch.
git(repo, &["checkout", "-B", base, "FETCH_HEAD"]).await?;
if let Err(e) = git(
repo,
&["merge", "--no-ff", "-m", &format!("auto-merge {branch}"), branch],
)
.await
{
// Leave the repo clean so the next run is not fighting a wedged merge.
let _ = git(repo, &["merge", "--abort"]).await;
return Ok(MergeOutcome::refused(format!(
"merge conflicted ({e}); left for a human"
)));
}
git(repo, &["push", push_url, &format!("HEAD:refs/heads/{base}")]).await?;
Ok(MergeOutcome {
merged: true,
reason: format!("additive-only and verified; merged into {base}"),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_pure_additions_qualify() {
assert!(non_additive_changes("A\t60 Papers/a.md\nA\t60 Papers/b.md\n").is_empty());
// A modification disqualifies the whole branch.
let m = non_additive_changes("A\t60 Papers/a.md\nM\tREADME.md\n");
assert_eq!(m.len(), 1);
assert!(m[0].contains("README.md"));
// So do deletes and renames — a rename is a delete plus an add, and
// the delete half can destroy hand-written work.
assert_eq!(non_additive_changes("D\tnotes/old.md\n").len(), 1);
assert_eq!(non_additive_changes("R100\ta.md\tb.md\n").len(), 1);
}
#[test]
fn an_unknown_policy_never_grants_auto_merge() {
assert_eq!(MergePolicy::parse(None), MergePolicy::Never);
assert_eq!(MergePolicy::parse(Some("never")), MergePolicy::Never);
assert_eq!(
MergePolicy::parse(Some("additive_only")),
MergePolicy::AdditiveOnly
);
// A typo must fail closed, not open.
assert_eq!(MergePolicy::parse(Some("aditive_only")), MergePolicy::Never);
assert_eq!(MergePolicy::parse(Some("always")), MergePolicy::Never);
}
}
+29
View File
@@ -291,6 +291,35 @@ pub async fn unseen(
.collect()) .collect())
} }
/// How many NEW sources a mission contributed.
///
/// The verification predicate for a continuous research mission. `record`
/// never reassigns `mission_id` on conflict, so the first mission to find a
/// source keeps the credit and a rerun cannot inflate its own count by
/// re-recording what an earlier run already had.
///
/// A mission whose answer is zero produced nothing, whatever its transcript
/// says — which is the check the 0030-0044 generation of this feature lacked.
pub async fn contributed(
pool: &sqlx::PgPool,
workspace_id: Uuid,
corpus_id: &str,
mission_id: Uuid,
) -> Result<i64, String> {
let row: (i64,) = sqlx::query_as(
"SELECT count(*) FROM corpus_items
WHERE workspace_id = $1 AND corpus_id = $2 AND mission_id = $3
AND kind = 'source'",
)
.bind(workspace_id)
.bind(corpus_id)
.bind(mission_id)
.fetch_one(pool)
.await
.map_err(|e| format!("contributed({mission_id}): {e}"))?;
Ok(row.0)
}
/// Index every note in a checkout. Idempotent by construction. /// Index every note in a checkout. Idempotent by construction.
pub async fn index_vault( pub async fn index_vault(
pool: &sqlx::PgPool, pool: &sqlx::PgPool,
+1
View File
@@ -16,6 +16,7 @@ mod mcp_door;
mod mcp_skills; mod mcp_skills;
pub mod mission_orchestrator; pub mod mission_orchestrator;
pub mod mission_refiner; pub mod mission_refiner;
pub mod auto_merge;
pub mod corpus; pub mod corpus;
pub mod harvest; pub mod harvest;
pub mod library; pub mod library;
+38 -6
View File
@@ -35,6 +35,11 @@ pub struct LibraryRun {
/// papers but could not push still has the PDFs and the checkmarks; the /// papers but could not push still has the PDFs and the checkmarks; the
/// notes are simply not on the forge yet. /// notes are simply not on the forge yet.
pub pushed: bool, pub pushed: bool,
/// Whether the branch was auto-merged into `main`.
pub merged: bool,
/// Always populated — a branch that quietly did not merge is
/// indistinguishable from one that was never delivered.
pub merge_reason: String,
pub error: Option<String>, pub error: Option<String>,
} }
@@ -160,6 +165,8 @@ pub async fn run_to_vault(
harvest: total, harvest: total,
branch, branch,
pushed: false, pushed: false,
merged: false,
merge_reason: "nothing new to push".into(),
error: None, error: None,
}); });
} }
@@ -181,16 +188,41 @@ pub async fn run_to_vault(
let auth = mission_workspace::with_ambient_auth(clone_url); let auth = mission_workspace::with_ambient_auth(clone_url);
let refspec = format!("HEAD:refs/heads/{branch}"); let refspec = format!("HEAD:refs/heads/{branch}");
match git(&vault, &["push", &auth, &refspec]).await { match git(&vault, &["push", &auth, &refspec]).await {
Ok(_) => Ok(LibraryRun { Ok(_) => {
harvest: total, // A catalogue branch only ever adds notes under `60 Papers/`, so
branch, // it qualifies for auto-merge — but the check is measured from the
pushed: true, // diff, not assumed from the mission type. Verified here means the
error: None, // run shelved something and errored on nothing.
}), let verified = total.healthy() && !total.shelved.is_empty();
let merge = crate::auto_merge::try_merge(
&vault,
&auth,
&branch,
"main",
crate::auto_merge::MergePolicy::AdditiveOnly,
verified,
)
.await
.unwrap_or_else(|e| crate::auto_merge::MergeOutcome {
merged: false,
reason: format!("merge attempt failed: {e}"),
});
eprintln!("library: branch {branch} — {}", merge.reason);
Ok(LibraryRun {
harvest: total,
branch,
pushed: true,
merged: merge.merged,
merge_reason: merge.reason,
error: None,
})
}
Err(e) => Ok(LibraryRun { Err(e) => Ok(LibraryRun {
harvest: total, harvest: total,
branch, branch,
pushed: false, pushed: false,
merged: false,
merge_reason: "not pushed, so not merged".into(),
error: Some(e), error: Some(e),
}), }),
} }
+37 -2
View File
@@ -83,10 +83,18 @@ impl RuntimeAuth {
/// ///
/// The other three are unrelated providers with no subscription equivalent, so /// The other three are unrelated providers with no subscription equivalent, so
/// they forward in both modes. /// they forward in both modes.
///
/// In subscription mode `CLAUDE_CODE_OAUTH_TOKEN` forwards instead. The
/// original design assumed a persisted `claude /login` under a bind-mounted
/// `$HOME`, but a *mission* container gets its own data dir and therefore no
/// login — so the token has to travel. Missing it is not a loud failure:
/// `claude -p` simply hangs with no credential, which is what a phase stuck
/// at `running` for ten minutes looked like when this was first switched on.
pub fn forwarded_provider_keys(auth: RuntimeAuth) -> Vec<&'static str> { pub fn forwarded_provider_keys(auth: RuntimeAuth) -> Vec<&'static str> {
let mut keys = vec!["GEMINI_API_KEY", "GROQ_API_KEY", "OPENAI_API_KEY"]; let mut keys = vec!["GEMINI_API_KEY", "GROQ_API_KEY", "OPENAI_API_KEY"];
if auth == RuntimeAuth::ApiKey { match auth {
keys.push("ANTHROPIC_API_KEY"); RuntimeAuth::ApiKey => keys.push("ANTHROPIC_API_KEY"),
RuntimeAuth::Subscription => keys.push("CLAUDE_CODE_OAUTH_TOKEN"),
} }
keys keys
} }
@@ -805,6 +813,29 @@ mod tests {
for k in ["GEMINI_API_KEY", "GROQ_API_KEY", "OPENAI_API_KEY"] { for k in ["GEMINI_API_KEY", "GROQ_API_KEY", "OPENAI_API_KEY"] {
assert!(keys.contains(&k), "{k} should still be forwarded"); assert!(keys.contains(&k), "{k} should still be forwarded");
} }
// And the subscription credential MUST travel. A mission container
// has its own data dir, so unlike the shared runtime it has no
// persisted `claude /login` to fall back on. Without this the CLI
// has no credential and simply hangs — a phase stuck at `running`
// with nothing in the logs, which is exactly how this was found.
assert!(
keys.contains(&"CLAUDE_CODE_OAUTH_TOKEN"),
"subscription mode must forward the token; without it `claude -p` \
hangs with no credential. Forwarded: {keys:?}"
);
}
/// The two credentials must never travel together: Claude Code would pick
/// the API key and bill it while the deployment believes it is on the
/// subscription.
#[test]
fn the_two_anthropic_credentials_are_mutually_exclusive() {
for mode in [RuntimeAuth::ApiKey, RuntimeAuth::Subscription] {
let keys = forwarded_provider_keys(mode);
let both = keys.contains(&"ANTHROPIC_API_KEY")
&& keys.contains(&"CLAUDE_CODE_OAUTH_TOKEN");
assert!(!both, "{mode:?} forwards both credentials: {keys:?}");
}
} }
/// Default behaviour is unchanged, so a deployment that never opts in keeps /// Default behaviour is unchanged, so a deployment that never opts in keeps
@@ -820,6 +851,10 @@ mod tests {
] { ] {
assert!(keys.contains(&k), "{k} should be forwarded in api_key mode"); assert!(keys.contains(&k), "{k} should be forwarded in api_key mode");
} }
assert!(
!keys.contains(&"CLAUDE_CODE_OAUTH_TOKEN"),
"api_key mode must not also ship the subscription token"
);
} }
/// An unset or misspelled value must fall back to the *existing* behaviour. /// An unset or misspelled value must fall back to the *existing* behaviour.
+10 -1
View File
@@ -26,6 +26,11 @@ pub struct RunRequest {
/// library can otherwise pull hundreds of PDFs in one go. /// library can otherwise pull hundreds of PDFs in one go.
#[serde(default)] #[serde(default)]
pub per_topic: Option<usize>, pub per_topic: Option<usize>,
/// Attribute this run to a mission, so the mission can later be asked
/// what it contributed. `corpus_items.mission_id` has existed since the
/// table landed; without this field nothing could ever populate it.
#[serde(default, rename = "missionId")]
pub mission_id: Option<uuid::Uuid>,
} }
#[derive(Serialize)] #[derive(Serialize)]
@@ -37,6 +42,8 @@ pub struct RunResponse {
pub notes: Vec<String>, pub notes: Vec<String>,
pub branch: String, pub branch: String,
pub pushed: bool, pub pushed: bool,
pub merged: bool,
pub merge_reason: String,
pub error: Option<String>, pub error: Option<String>,
/// A run that errored on nothing. Reported explicitly so a caller does not /// A run that errored on nothing. Reported explicitly so a caller does not
/// have to infer health from an empty `shelved` list — a quiet week and a /// have to infer health from an empty `shelved` list — a quiet week and a
@@ -78,7 +85,7 @@ pub async fn run(
&work_root, &work_root,
&topics, &topics,
per_topic, per_topic,
None, req.mission_id,
) )
.await .await
.map_err(|e| { .map_err(|e| {
@@ -102,6 +109,8 @@ pub async fn run(
healthy: out.harvest.healthy(), healthy: out.harvest.healthy(),
branch: out.branch, branch: out.branch,
pushed: out.pushed, pushed: out.pushed,
merged: out.merged,
merge_reason: out.merge_reason,
error: out.error, error: out.error,
})) }))
} }
+32 -22
View File
@@ -20,12 +20,20 @@ pub fn claw_alias(claw_id: Uuid) -> String {
/// Map a claw's chosen model to a configured provider alias. /// Map a claw's chosen model to a configured provider alias.
/// ///
/// v0.8.3 fold: `claude_cli.*` and `kimi_cli.*` families were deleted /// Claude models resolve to `claude_cli.default`, which spawns the real
/// upstream; every alias now lives under a real provider family /// `claude` binary against the Max subscription rather than posting to the
/// (`anthropic`, `groq`, `gemini`, ...). Our compose currently /// raw API with Claude Code identity headers. The API-key path still exists
/// configures `anthropic.default`, `anthropic.door`, `groq.default`, /// and the judge uses it deliberately (see below), but agent work — which is
/// and `gemini.default`, so unknown models resolve to /// ~99% of the tokens — belongs on the subscription and on the supported
/// `anthropic.default` — the workspace's high-quality baseline. /// client.
///
/// The judge stays on `anthropic.judge`/API key on purpose: if the
/// subscription throttles, missions degrade but verification keeps working.
/// Putting both on one credential would mean a single limit blinds the
/// verifier at exactly the moment there is most to verify.
///
/// Non-Claude families are unchanged: `groq.default`, `gemini.default`, and
/// the GLM/Kimi substitution below.
pub fn provider_alias_for(model: &str) -> &'static str { pub fn provider_alias_for(model: &str) -> &'static str {
let m = model.trim().to_ascii_lowercase(); let m = model.trim().to_ascii_lowercase();
// Prefix families first (covers claude-sonnet-5, claude-opus-4-8, // Prefix families first (covers claude-sonnet-5, claude-opus-4-8,
@@ -33,7 +41,7 @@ pub fn provider_alias_for(model: &str) -> &'static str {
// decides what "its own family" means, so the two can't drift apart. // decides what "its own family" means, so the two can't drift apart.
if is_exact_provider_match(&m) { if is_exact_provider_match(&m) {
if m.starts_with("claude") { if m.starts_with("claude") {
return "anthropic.default"; return "claude_cli.default";
} }
if m.starts_with("gemini") { if m.starts_with("gemini") {
return "gemini.default"; return "gemini.default";
@@ -54,18 +62,18 @@ pub fn provider_alias_for(model: &str) -> &'static str {
| "kimi" | "kimi-k2" | "kimi-for-coding" => { | "kimi" | "kimi-k2" | "kimi-for-coding" => {
eprintln!( eprintln!(
"runtime_provision: model {m:?} has no provider family configured — \ "runtime_provision: model {m:?} has no provider family configured — \
substituting anthropic.default, which spends ANTHROPIC_API_KEY" substituting claude_cli.default, which spends the Claude subscription"
); );
"anthropic.default" "claude_cli.default"
} }
_ => { _ => {
if !m.is_empty() { if !m.is_empty() {
eprintln!( eprintln!(
"runtime_provision: unrecognised model {m:?} — defaulting to \ "runtime_provision: unrecognised model {m:?} — defaulting to \
anthropic.default" claude_cli.default"
); );
} }
"anthropic.default" "claude_cli.default"
} }
} }
} }
@@ -342,14 +350,15 @@ mod tests {
/// The GLM/Kimi substitution is intentional but must be reported as a /// The GLM/Kimi substitution is intentional but must be reported as a
/// substitution, because its consequence is that a user who picked a /// substitution, because its consequence is that a user who picked a
/// non-Anthropic model is spending the Anthropic key. /// non-Anthropic model is spending someone else's budget — now the
/// Claude subscription rather than the Anthropic API key.
#[test] #[test]
fn substituted_families_are_not_reported_as_exact_matches() { fn substituted_families_are_not_reported_as_exact_matches() {
for m in ["kimi", "glm-4.7", "glm5", "kimi-k2", "something-unknown"] { for m in ["kimi", "glm-4.7", "glm5", "kimi-k2", "something-unknown"] {
assert_eq!(super::provider_alias_for(m), "anthropic.default"); assert_eq!(super::provider_alias_for(m), "claude_cli.default");
assert!( assert!(
!super::is_exact_provider_match(m), !super::is_exact_provider_match(m),
"{m} resolves to anthropic.default by substitution, not by family" "{m} resolves to claude_cli.default by substitution, not by family"
); );
} }
for m in [ for m in [
@@ -395,19 +404,20 @@ mod tests {
fn provider_alias_mapping() { fn provider_alias_mapping() {
assert_eq!(provider_alias_for("gemini"), "gemini.default"); assert_eq!(provider_alias_for("gemini"), "gemini.default");
assert_eq!(provider_alias_for("gemini-2.0-flash"), "gemini.default"); assert_eq!(provider_alias_for("gemini-2.0-flash"), "gemini.default");
// v0.8.3: glm/kimi families fall back to anthropic until their // glm/kimi families fall back to Claude until their own provider
// own provider tables are configured in the runtime template. // tables are configured in the runtime template.
assert_eq!(provider_alias_for("GLM-4.7"), "anthropic.default"); assert_eq!(provider_alias_for("GLM-4.7"), "claude_cli.default");
assert_eq!(provider_alias_for("kimi"), "anthropic.default"); assert_eq!(provider_alias_for("kimi"), "claude_cli.default");
assert_eq!(provider_alias_for("groq"), "groq.default"); assert_eq!(provider_alias_for("groq"), "groq.default");
assert_eq!( assert_eq!(
provider_alias_for("llama-3.3-70b-versatile"), provider_alias_for("llama-3.3-70b-versatile"),
"groq.default" "groq.default"
); );
assert_eq!(provider_alias_for("claude"), "anthropic.default"); // Claude models spawn the real CLI against the subscription.
assert_eq!(provider_alias_for("claude-sonnet-5"), "anthropic.default"); assert_eq!(provider_alias_for("claude"), "claude_cli.default");
assert_eq!(provider_alias_for("claude-opus-4-8"), "anthropic.default"); assert_eq!(provider_alias_for("claude-sonnet-5"), "claude_cli.default");
assert_eq!(provider_alias_for("anything-else"), "anthropic.default"); assert_eq!(provider_alias_for("claude-opus-4-8"), "claude_cli.default");
assert_eq!(provider_alias_for("anything-else"), "claude_cli.default");
} }
#[test] #[test]
+147
View File
@@ -0,0 +1,147 @@
//! Auto-merge against real git repositories.
//!
//! The rule is measured from the diff, so it has to be tested against real
//! diffs — a unit test on the classifier alone would not catch a wrong
//! revision range.
use cm_api::auto_merge::{self, MergePolicy};
fn git(repo: &std::path::Path, args: &[&str]) {
let out = std::process::Command::new("git")
.arg("-C")
.arg(repo)
.args(args)
.env("GIT_AUTHOR_NAME", "T")
.env("GIT_AUTHOR_EMAIL", "[email protected]")
.env("GIT_COMMITTER_NAME", "T")
.env("GIT_COMMITTER_EMAIL", "[email protected]")
.output()
.unwrap();
assert!(
out.status.success(),
"git {args:?}: {}",
String::from_utf8_lossy(&out.stderr)
);
}
/// Returns (work checkout, bare remote path).
fn seed() -> (tempfile::TempDir, std::path::PathBuf, std::path::PathBuf) {
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("remote.git");
let work = tmp.path().join("work");
std::process::Command::new("git")
.args(["init", "--quiet", "--bare"])
.arg(&remote)
.output()
.unwrap();
std::fs::create_dir_all(&work).unwrap();
git(&work, &["init", "--quiet"]);
git(&work, &["checkout", "-q", "-B", "main"]);
std::fs::write(work.join("README.md"), "# vault\n").unwrap();
git(&work, &["add", "."]);
git(&work, &["commit", "--quiet", "-m", "base"]);
git(&work, &["remote", "add", "origin", remote.to_str().unwrap()]);
git(&work, &["push", "--quiet", "origin", "main"]);
(tmp, work, remote)
}
#[tokio::test]
async fn a_purely_additive_branch_is_merged() {
let (_tmp, work, remote) = seed();
git(&work, &["checkout", "-q", "-B", "lib/add"]);
std::fs::create_dir_all(work.join("60 Papers")).unwrap();
std::fs::write(work.join("60 Papers/a.md"), "# paper\n").unwrap();
git(&work, &["add", "."]);
git(&work, &["commit", "--quiet", "-m", "add paper"]);
git(&work, &["push", "--quiet", "origin", "lib/add"]);
let out = auto_merge::try_merge(
&work, remote.to_str().unwrap(), "lib/add", "main",
MergePolicy::AdditiveOnly, true,
)
.await
.unwrap();
assert!(out.merged, "should have merged: {}", out.reason);
// The note must really be on main at the remote, not just locally.
let ls = std::process::Command::new("git")
.arg("-C").arg(&remote)
.args(["ls-tree", "--name-only", "-r", "main"])
.output()
.unwrap();
let listed = String::from_utf8_lossy(&ls.stdout);
assert!(listed.contains("60 Papers/a.md"), "remote main: {listed}");
}
/// The load-bearing refusal: a branch that rewrites an existing file must be
/// left for a human even though its mission type is allowed to auto-merge.
#[tokio::test]
async fn a_branch_that_modifies_an_existing_file_is_refused() {
let (_tmp, work, remote) = seed();
git(&work, &["checkout", "-q", "-B", "lib/bad"]);
std::fs::create_dir_all(work.join("60 Papers")).unwrap();
std::fs::write(work.join("60 Papers/a.md"), "# paper\n").unwrap();
// …and clobbers a hand-written file.
std::fs::write(work.join("README.md"), "# REWRITTEN BY A MACHINE\n").unwrap();
git(&work, &["add", "."]);
git(&work, &["commit", "--quiet", "-m", "add + clobber"]);
git(&work, &["push", "--quiet", "origin", "lib/bad"]);
let out = auto_merge::try_merge(
&work, remote.to_str().unwrap(), "lib/bad", "main",
MergePolicy::AdditiveOnly, true,
)
.await
.unwrap();
assert!(!out.merged, "must refuse a non-additive branch");
assert!(out.reason.contains("not additive"), "reason: {}", out.reason);
let show = std::process::Command::new("git")
.arg("-C").arg(&remote)
.args(["show", "main:README.md"])
.output()
.unwrap();
assert_eq!(
String::from_utf8_lossy(&show.stdout),
"# vault\n",
"the hand-written file must be untouched on main"
);
}
#[tokio::test]
async fn unverified_work_is_never_merged() {
let (_tmp, work, remote) = seed();
git(&work, &["checkout", "-q", "-B", "lib/unverified"]);
std::fs::create_dir_all(work.join("60 Papers")).unwrap();
std::fs::write(work.join("60 Papers/a.md"), "# paper\n").unwrap();
git(&work, &["add", "."]);
git(&work, &["commit", "--quiet", "-m", "add"]);
git(&work, &["push", "--quiet", "origin", "lib/unverified"]);
let out = auto_merge::try_merge(
&work, remote.to_str().unwrap(), "lib/unverified", "main",
MergePolicy::AdditiveOnly, false,
)
.await
.unwrap();
assert!(!out.merged);
assert!(out.reason.contains("did not verify"), "reason: {}", out.reason);
}
#[tokio::test]
async fn a_never_policy_branch_is_left_alone() {
let (_tmp, work, remote) = seed();
git(&work, &["checkout", "-q", "-B", "code/change"]);
std::fs::write(work.join("new.rs"), "fn main() {}\n").unwrap();
git(&work, &["add", "."]);
git(&work, &["commit", "--quiet", "-m", "code"]);
git(&work, &["push", "--quiet", "origin", "code/change"]);
let out = auto_merge::try_merge(
&work, remote.to_str().unwrap(), "code/change", "main",
MergePolicy::Never, true,
)
.await
.unwrap();
assert!(!out.merged, "code must never auto-merge");
}
+58
View File
@@ -19,6 +19,24 @@ async fn workspace(pool: &sqlx::PgPool) -> Uuid {
ws ws
} }
/// A real mission row. `corpus_items.mission_id` has a foreign key, which is
/// deliberate: attribution to a mission that does not exist is not
/// attribution. The first version of the test below used a bare UUID and was
/// correctly rejected.
async fn mission(pool: &sqlx::PgPool, ws: Uuid) -> Uuid {
let id = Uuid::now_v7();
sqlx::query(
"INSERT INTO missions (id, workspace_id, title, template_kind, schedule, status, config)
VALUES ($1,$2,'library','research_only','{}'::jsonb,'running','{}'::jsonb)",
)
.bind(id)
.bind(ws)
.execute(pool)
.await
.unwrap();
id
}
fn seed_vault(root: &std::path::Path) { fn seed_vault(root: &std::path::Path) {
std::fs::create_dir_all(root.join("50 APESS 2026/Lectures")).unwrap(); std::fs::create_dir_all(root.join("50 APESS 2026/Lectures")).unwrap();
std::fs::create_dir_all(root.join("Repos")).unwrap(); std::fs::create_dir_all(root.join("Repos")).unwrap();
@@ -226,3 +244,43 @@ async fn live_arxiv_search_and_fetch() {
assert!(pdf.starts_with(b"%PDF")); assert!(pdf.starts_with(b"%PDF"));
assert!(pdf.len() > 10_000, "suspiciously small pdf: {}", pdf.len()); assert!(pdf.len() > 10_000, "suspiciously small pdf: {}", pdf.len());
} }
/// A rerun must not be able to claim credit for work an earlier run did.
///
/// This is the verification predicate for a continuous mission: "did THIS run
/// contribute anything new". If a rerun could re-record an existing source
/// under its own mission id, every run would report success forever — the
/// failure that killed the 0030-0044 generation of this feature.
#[tokio::test]
async fn a_rerun_cannot_claim_an_earlier_missions_work() {
let pool = cm_testkit::test_pool().await;
let ws = workspace(&pool).await;
let first_mission = mission(&pool, ws).await;
let second_mission = mission(&pool, ws).await;
corpus::record(
&pool, ws, "lib", "source", "arxiv:2401.55555",
Some("Paper"), None, None, "h1", Some(first_mission),
)
.await
.unwrap();
// The second mission sees the same paper and re-records it.
corpus::record(
&pool, ws, "lib", "source", "arxiv:2401.55555",
Some("Paper"), None, None, "h2", Some(second_mission),
)
.await
.unwrap();
assert_eq!(
corpus::contributed(&pool, ws, "lib", first_mission).await.unwrap(),
1,
"the finder keeps the credit"
);
assert_eq!(
corpus::contributed(&pool, ws, "lib", second_mission).await.unwrap(),
0,
"a rerun that found nothing new must report zero, not one"
);
}
+14
View File
@@ -0,0 +1,14 @@
[Unit]
Description=Clawmates paper library harvest (arXiv -> shelf + vault catalogue)
Wants=docker.service
After=docker.service network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/clawmates-library.sh
StandardOutput=journal
StandardError=journal
Nice=10
# A harvest downloads PDFs and pushes a branch; give it room but do not
# let a wedged run hold the slot until the next week.
TimeoutStartSec=30min
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env bash
# Weekly paper-library harvest.
#
# Deliberately thin: it calls the API and reports what came back. All the
# logic lives in the server, so this file never needs to change when the
# harvest does.
#
# The token lives in /etc/clawmates/library.token (root-only). It is a
# long-lived operator session; rotate by replacing the file.
set -uo pipefail
TOKEN_FILE=/etc/clawmates/library.token
[ -r "$TOKEN_FILE" ] || { echo "library: no token at $TOKEN_FILE"; exit 1; }
TOKEN=$(cat "$TOKEN_FILE")
RESP=$(docker run --rm --network clawmates_core curlimages/curl:latest \
-s -m 1800 -X POST \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"per_topic":5}' \
http://clawmates_server_1:8080/api/library/runs)
echo "library: $RESP" | head -c 2000
# Report health explicitly. A run that shelved nothing is normal for a
# mature library; a run that ERRORED is not, and the two look identical
# if you only count papers.
echo "$RESP" | python3 -c '
import json, sys
try:
d = json.load(sys.stdin)
except Exception as e:
print("library: unreadable response (%s)" % e)
sys.exit(1)
shelved = len(d.get("shelved", []))
healthy = d.get("healthy", False)
# Backslashes are avoided inside this program on purpose: it is embedded in a
# single-quoted shell string, and an escaped quote here does not survive the
# shell. The first version used one inside an f-string, crashed on every run,
# and systemd reported a FAILED unit for a harvest that had actually shelved
# 15 papers and pushed them. A false failure destroys trust in the signal as
# surely as a false success.
print("library: %d candidates, %d already held, %d shelved, healthy=%s, pushed=%s, branch=%s" % (
d.get("candidates", 0), d.get("already_had", 0), shelved,
healthy, d.get("pushed"), d.get("branch")))
for f in d.get("failed", []):
print("library: FAILED %s" % f)
sys.exit(0 if healthy else 1)
'
+16
View File
@@ -0,0 +1,16 @@
[Unit]
Description=Clawmates paper library — weekly harvest
[Timer]
# Monday 07:00 local. Weekly rather than daily because arXiv moves at
# roughly that pace for a narrow topic set, and a run that almost always
# finds nothing trains you to ignore it.
OnCalendar=Mon *-*-* 07:00:00
# Fire on next boot if the machine was down at the scheduled time — a
# missed week is a silently empty library.
Persistent=true
AccuracySec=1min
Unit=clawmates-library.service
[Install]
WantedBy=timers.target