Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/desktop/src/state/planUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,10 @@ export async function refreshPlanUsage(id: string): Promise<void> {
// other native thread reads them as its own.
// A thread on a paired machine reads that machine's login, not this one's:
// its numbers stay on the thread, so they never pass for the Mac's own plan.
// Antigravity's numbers are the quota group of the thread's model, not
// the harness's, so another Antigravity thread never reads them either.
const harness = harnessOf(get(), id);
if (usage && harness && harness !== "rustcode" && !isRemoteThread(id)) {
if (usage && harness && harness !== "rustcode" && harness !== "antigravity" && !isRemoteThread(id)) {
// The account's own row, so it never overwrites another login's numbers.
const key = harnessPlanKey(harness, accountOn(get(), id));
commitPlans({
Expand Down
8 changes: 7 additions & 1 deletion docs/desktop.md
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,13 @@ composer's keys stay outside the table.
path's SHA-256 for a named account — and `<config dir>/.credentials.json`,
the copy with the freshest token winning) against
`api.anthropic.com/api/oauth/usage` (5-hour, weekly, weekly per model),
read-only and never refreshed (`provider_auth/claude_usage.rs`). The first
read-only and never refreshed (`provider_auth/claude_usage.rs`). Antigravity
(macOS and Linux) comes from `agy --print /usage --output-format json`, run
with the executable and environment Settings › Harnesses › Antigravity
configures (`provider_auth/antigravity_usage.rs`); a failed read shows what
`agy` said, and a read that hangs (a CLI waiting for a sign-in) points to
Sign in. An Antigravity thread's meter shows the quota group of its model
(Gemini, or Claude and GPT), so those numbers stay on the thread. The first
read happens when Usage opens or a Claude account is used; macOS may ask to
allow Keychain access. A thread on a paired machine reads that machine's
login instead: the plans call only reaches this computer's engine, so the
Expand Down
6 changes: 6 additions & 0 deletions docs/harnesses.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ latter, and groups those apart. Detection is cached for a minute; `set` and
| `perch` | `acp` | `perch acp` | `perch status` | `perch status` (`Not signed in.` is signed out) |
| `antigravity` | `acp` | `agy acp` | `agy --version` | `unknown` |

A signed-in Antigravity session always offers a model choice
(`KnownHarness::lists_models`), so a listing that comes back empty is a Check
error naming what the session offered and `agy`'s last stderr lines, and the
models listed before stay cached; another harness that lists none is "Ready ·
no model list".

Commands are looked up on the engine's `PATH` and then the login shell's, so
an app started from the Dock finds what the terminal finds. On Windows a bare
name tries the `PATHEXT` extensions in order, as the shell does: `codex` is
Expand Down
7 changes: 5 additions & 2 deletions rustcode/engine/src/acp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,8 +1358,11 @@ async fn serve_connection(
// A named account's own login, so the meter never
// shows another home's numbers.
let home = usage::account_home(&session_id, &harness);
responder
.respond(usage::harness_limits(&harness, home.as_deref()).await)?;
let model = usage::external_model(&session_id);
responder.respond(
usage::harness_limits(&harness, home.as_deref(), model.as_deref())
.await,
)?;
Ok(())
});
};
Expand Down
81 changes: 80 additions & 1 deletion rustcode/engine/src/acp/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,17 @@ pub(crate) fn account_home(session_id: &str, harness: &str) -> Option<std::path:
pub(crate) async fn harness_limits(
harness: &str,
home: Option<&std::path::Path>,
model: Option<&str>,
) -> serde_json::Value {
match harness {
#[cfg(unix)]
"antigravity" => {
let read = crate::provider_auth::antigravity_usage::read_cached(false).await;
let limits = read
.and_then(Result::ok)
.and_then(|rows| antigravity_limits(&rows, model));
limits_json(limits.as_ref(), "antigravity")
}
"codex" => {
let limits = crate::provider_auth::codex_usage::rate_limits(home).await;
limits_json(limits.as_ref(), "codex")
Expand All @@ -305,6 +314,48 @@ pub(crate) async fn harness_limits(
}
}

/// The model a thread on another harness runs, as its record names it.
pub(crate) fn external_model(session_id: &str) -> Option<String> {
crate::harness::store::load(session_id)
.and_then(|record| record.model)
.filter(|model| !model.is_empty())
}

/// Antigravity's quota for the group `model` draws on: Gemini models share
/// one group and the other providers' models another (`agy --print /usage`).
/// A thread with no model runs Gemini, Antigravity's default. The group's
/// 5-hour window comes first and its weekly one second, as the meter reads
/// them; null when the report has neither for that group.
#[cfg_attr(not(unix), allow(dead_code))]
fn antigravity_limits(
rows: &[serde_json::Value],
model: Option<&str>,
) -> Option<crate::provider_auth::ProviderRateLimits> {
let gemini = model.is_none_or(|model| model.to_ascii_lowercase().starts_with("gemini"));
let in_group = |row: &&serde_json::Value| {
let label = row["label"].as_str().unwrap_or_default();
let group = label.split(" · ").next().unwrap_or_default();
group.to_ascii_lowercase().contains("gemini") == gemini
};
let window = |minutes: u64| {
rows.iter()
.filter(in_group)
.find(|row| row["windowMinutes"].as_u64() == Some(minutes))
.and_then(|row| {
Some(crate::provider_auth::RateLimitWindow {
used_percent: row["usedPercent"].as_f64()?,
window_minutes: Some(minutes),
resets_at: row["resetsAt"].as_i64(),
})
})
};
let limits = crate::provider_auth::ProviderRateLimits {
primary: window(300),
secondary: window(10_080),
};
(limits.primary.is_some() || limits.secondary.is_some()).then_some(limits)
}

/// Whether a thread's own limits call may read Claude Code's sign-in. On macOS
/// it is in the Keychain, which can prompt, so the app reads it only through
/// `_rustcode/usage/plans` after the user chose to. Elsewhere it is
Expand Down Expand Up @@ -1168,7 +1219,35 @@ mod harness_tests {
);
assert_eq!(external_harness("usage-no-such-thread"), None);
// A harness with no plan to read answers null.
assert!(harness_limits("gemini", None).await.is_null());
assert!(harness_limits("gemini", None, None).await.is_null());
}

#[test]
fn antigravity_meter_reads_the_quota_group_of_the_threads_model() {
let rows = vec![
serde_json::json!({"label": "Gemini Models · Weekly", "usedPercent": 21.0,
"windowMinutes": 10_080, "resetsAt": 100}),
serde_json::json!({"label": "Claude and GPT models · Weekly", "usedPercent": 60.0,
"windowMinutes": 10_080, "resetsAt": 200}),
serde_json::json!({"label": "Claude and GPT models · 5-hour", "usedPercent": 5.0,
"windowMinutes": 300, "resetsAt": 50}),
];
let gemini = super::antigravity_limits(&rows, Some("gemini-3.8-flash")).unwrap();
assert!(gemini.primary.is_none());
assert_eq!(gemini.secondary.as_ref().unwrap().used_percent, 21.0);
assert_eq!(gemini.secondary.unwrap().resets_at, Some(100));
// No model is Antigravity's default, a Gemini model.
assert_eq!(
super::antigravity_limits(&rows, None),
Some(super::antigravity_limits(&rows, Some("gemini-3.8-flash")).unwrap())
);
let claude = super::antigravity_limits(&rows, Some("claude-opus-4-6-thinking")).unwrap();
assert_eq!(claude.primary.unwrap().used_percent, 5.0);
assert_eq!(claude.secondary.unwrap().used_percent, 60.0);
assert_eq!(
super::antigravity_limits(&rows[..1], Some("gpt-oss-120b-medium")),
None
);
}

#[test]
Expand Down
30 changes: 30 additions & 0 deletions rustcode/engine/src/harness/acp_child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,36 @@ impl AcpAdapter {
matches!(answer, Ok(answer) if answer.get("outcome").and_then(Value::as_str) == Some("injected"))
}

/// Why a listing found no models: what the session offered instead, and
/// the agent's last stderr lines. `agy` answers a session it cannot fully
/// set up (a login the app cannot read) with no model choice at all.
pub(crate) async fn no_models_reason(&self, name: &str) -> String {
let offered = {
let state = lock(&self.state);
let ids: Vec<String> = state
.options
.iter()
.map(|option| option.id.0.to_string())
.collect();
let options = if ids.is_empty() {
"no config options".to_owned()
} else {
format!("config options without a model ({})", ids.join(", "))
};
let models = match &state.legacy_models {
None => "no models list",
Some(models) => match models.get("availableModels").and_then(Value::as_array) {
Some(list) if !list.is_empty() => "a models list RustCode could not read",
_ => "an empty models list",
},
};
format!("{options} and {models}")
};
let error = format!("{name} started but offered no models: its new session had {offered}.");
crate::dbg_log!("{error}");
with_stderr(error, self.process.as_ref()).await
}

pub(crate) fn config_options(&self) -> Vec<SessionConfigOption> {
mapped_options(&lock(&self.state))
}
Expand Down
2 changes: 2 additions & 0 deletions rustcode/engine/src/harness/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ mod tests {
auth_probe: None,
requires: Some("rustcode-no-such-prerequisite-on-path"),
native_questions: false,
lists_models: false,
};

#[test]
Expand Down Expand Up @@ -993,6 +994,7 @@ mod tests {
auth_probe: Some(AuthProbe::Json(&["claude", "auth", "status", "--json"])),
requires: None,
native_questions: false,
lists_models: false,
};
let harness = ResolvedHarness {
id: "fixture".into(),
Expand Down
11 changes: 11 additions & 0 deletions rustcode/engine/src/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ pub(crate) struct KnownHarness {
/// multiple-choice question (Claude Code's `AskUserQuestion`); without
/// one the thread gets RustCode's `ask_user_question` (#3393).
pub native_questions: bool,
/// A signed-in session always offers a model choice, so a listing that
/// comes back empty is a failure to report, not a harness without models.
pub lists_models: bool,
}

pub(crate) const KNOWN: &[KnownHarness] = &[
Expand All @@ -249,6 +252,7 @@ pub(crate) const KNOWN: &[KnownHarness] = &[
auth_probe: None,
requires: None,
native_questions: false,
lists_models: false,
},
KnownHarness {
id: "codex",
Expand All @@ -263,6 +267,7 @@ pub(crate) const KNOWN: &[KnownHarness] = &[
auth_probe: Some(AuthProbe::Text(&["codex", "login", "status"])),
requires: None,
native_questions: true,
lists_models: false,
},
KnownHarness {
id: "claude-code",
Expand All @@ -277,6 +282,7 @@ pub(crate) const KNOWN: &[KnownHarness] = &[
auth_probe: Some(AuthProbe::Json(&["claude", "auth", "status", "--json"])),
requires: None,
native_questions: true,
lists_models: false,
},
KnownHarness {
id: "gemini",
Expand All @@ -291,6 +297,7 @@ pub(crate) const KNOWN: &[KnownHarness] = &[
auth_probe: None,
requires: None,
native_questions: false,
lists_models: false,
},
KnownHarness {
id: "opencode",
Expand All @@ -305,6 +312,7 @@ pub(crate) const KNOWN: &[KnownHarness] = &[
auth_probe: None,
requires: None,
native_questions: false,
lists_models: false,
},
// Pi speaks its own RPC protocol; `pi-acp` adapts it to ACP, as claude-agent-acp does for
// Claude Code. Pi keeps its own models, logins, skills and sessions (~/.pi).
Expand All @@ -321,6 +329,7 @@ pub(crate) const KNOWN: &[KnownHarness] = &[
auth_probe: None,
requires: Some("pi"),
native_questions: false,
lists_models: false,
},
// Perch (`perchai-cli`) speaks ACP with its `acp` subcommand. It has no
// `--version`; `perch status` prints `Perch CLI 2.4.132 · Not signed in.`,
Expand All @@ -336,6 +345,7 @@ pub(crate) const KNOWN: &[KnownHarness] = &[
auth_probe: Some(AuthProbe::Text(&["perch", "status"])),
requires: None,
native_questions: false,
lists_models: false,
},
// Google's Antigravity CLI (`agy`) speaks ACP with `agy acp` since 1.5.0.
// Its `session/prompt` answer carries the turn's token usage
Expand All @@ -356,6 +366,7 @@ pub(crate) const KNOWN: &[KnownHarness] = &[
auth_probe: None,
requires: None,
native_questions: false,
lists_models: true,
},
];

Expand Down
15 changes: 14 additions & 1 deletion rustcode/engine/src/harness/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,21 @@ pub(crate) async fn list_models(harness: &ResolvedHarness) -> Result<Vec<Harness
let models = adapter
.probed_models(&known, &preferred_models(harness))
.await;
// A harness that always offers models when signed in and offered none
// failed to set up the session; say why rather than list nothing.
let reason = match &adapter {
Adapter::Acp(acp)
if models.is_empty() && harness.known.is_some_and(|known| known.lists_models) =>
{
Some(acp.no_models_reason(&harness.name).await)
}
_ => None,
};
adapter.kill();
Ok(models)
match reason {
Some(reason) => Err(reason),
None => Ok(models),
}
}

/// The models a setting names for `harness`: the engine's default, and the
Expand Down
Loading
Loading