@team-agent/installer 0.5.126 → 0.5.127
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +1 -1
- package/Cargo.toml +1 -1
- package/crates/team-agent/src/cli/diagnose.rs +0 -69
- package/crates/team-agent/src/cli/emit.rs +10 -9
- package/crates/team-agent/src/cli/grok_slot.rs +88 -3
- package/crates/team-agent/src/cli/intent/runtime.rs +6 -4
- package/crates/team-agent/src/cli/intent/runtime_tests.rs +45 -0
- package/crates/team-agent/src/cli/mod.rs +13 -1
- package/crates/team-agent/src/cli/models.rs +27 -181
- package/crates/team-agent/src/cli/tests/leader_watch.rs +1 -1
- package/crates/team-agent/src/cli/tests/mod.rs +2 -2
- package/crates/team-agent/src/cli/tests/run_delegation.rs +3 -3
- package/crates/team-agent/src/cli/tests/status_send.rs +2 -2
- package/crates/team-agent/src/compiler/tests.rs +36 -9
- package/crates/team-agent/src/compiler.rs +3 -2
- package/crates/team-agent/src/contract_runtime/backend.rs +36 -0
- package/crates/team-agent/src/contract_runtime/commands.rs +335 -4
- package/crates/team-agent/src/contract_runtime/config.rs +6 -5
- package/crates/team-agent/src/contract_runtime/framework.rs +2 -5
- package/crates/team-agent/src/contract_runtime/registry.rs +66 -67
- package/crates/team-agent/src/coordinator/steps/abnormal.rs +45 -5
- package/crates/team-agent/src/coordinator/tests/api_error_recovery.rs +27 -10
- package/crates/team-agent/src/coordinator/tests/energy.rs +4 -4
- package/crates/team-agent/src/coordinator/tests/health_sync.rs +2 -2
- package/crates/team-agent/src/coordinator/tests/main_preserved.rs +2 -2
- package/crates/team-agent/src/coordinator/tests/spine.rs +1 -1
- package/crates/team-agent/src/coordinator/tests/takeover.rs +3 -3
- package/crates/team-agent/src/lifecycle/launch/fork_agent.rs +17 -185
- package/crates/team-agent/src/lifecycle/launch/mcp_config.rs +4 -487
- package/crates/team-agent/src/lifecycle/launch/quick_start.rs +2 -2
- package/crates/team-agent/src/lifecycle/launch/spawn.rs +8 -21
- package/crates/team-agent/src/lifecycle/launch.rs +3 -11
- package/crates/team-agent/src/lifecycle/profile_launch.rs +8 -64
- package/crates/team-agent/src/lifecycle/restart/common.rs +6 -151
- package/crates/team-agent/src/lifecycle/restart/remove.rs +129 -0
- package/crates/team-agent/src/lifecycle/role_config.rs +40 -13
- package/crates/team-agent/src/lifecycle/tests/acceptance_b_batch_red.rs +1 -1
- package/crates/team-agent/src/lifecycle/tests/agent_ops.rs +8 -12
- package/crates/team-agent/src/lifecycle/tests/behavioral_diff_264_red.rs +37 -240
- package/crates/team-agent/src/lifecycle/tests/cli_argv_routing_red.rs +95 -59
- package/crates/team-agent/src/lifecycle/tests/communication_mode_runtime_contract_red.rs +20 -35
- package/crates/team-agent/src/lifecycle/tests/copilot_provider_red.rs +2 -2
- package/crates/team-agent/src/lifecycle/tests/core.rs +22 -48
- package/crates/team-agent/src/lifecycle/tests/f032_startup_prompt_best_effort_red.rs +22 -8
- package/crates/team-agent/src/lifecycle/tests/grok_require_explicit_model_red.rs +6 -124
- package/crates/team-agent/src/lifecycle/tests/host_cotenant_death_p0_contract.rs +30 -67
- package/crates/team-agent/src/lifecycle/tests/issue238_codex_effort_contract.rs +3 -31
- package/crates/team-agent/src/lifecycle/tests/lane_ops.rs +194 -169
- package/crates/team-agent/src/lifecycle/tests/launch_spawn.rs +136 -256
- package/crates/team-agent/src/lifecycle/tests/lifecycle_rollback_red.rs +15 -78
- package/crates/team-agent/src/lifecycle/tests/main_preserved.rs +7 -7
- package/crates/team-agent/src/lifecycle/tests/phase_b_contracts.rs +75 -69
- package/crates/team-agent/src/lifecycle/tests/phase_golden.rs +2 -10
- package/crates/team-agent/src/lifecycle/tests/quick_start_worker_readiness_red.rs +7 -7
- package/crates/team-agent/src/lifecycle/tests/restart.rs +12 -153
- package/crates/team-agent/src/lifecycle/tests/restart_build_before_destroy_0540_contract.rs +27 -5
- package/crates/team-agent/src/lifecycle/tests/restart_liveness_red.rs +59 -10
- package/crates/team-agent/src/lifecycle/tests/restart_rebind_hotfix_252_red.rs +47 -6
- package/crates/team-agent/src/lifecycle/tests/restart_session_capture_red.rs +3 -1374
- package/crates/team-agent/src/lifecycle/tests/resume_recover_red.rs +27 -10
- package/crates/team-agent/src/lifecycle/tests/status_credential_redaction_contract.rs +5 -5
- package/crates/team-agent/src/lifecycle/tests/subprep_codex_trust_roundtrip_red.rs +67 -313
- package/crates/team-agent/src/lifecycle/tests/team_in_team_identity_scope_red.rs +2 -2
- package/crates/team-agent/src/lifecycle/tests/team_in_team_sibling_quick_start_red.rs +1 -1
- package/crates/team-agent/src/lifecycle/tests/team_in_team_state_scope_red.rs +2 -2
- package/crates/team-agent/src/lifecycle/tests/team_key_retirement_txn_red.rs +4 -4
- package/crates/team-agent/src/lifecycle/tests/upgrade_compat_0211_red.rs +70 -15
- package/crates/team-agent/src/lifecycle/tests/verify_rs031_window_consistency_red.rs +35 -35
- package/crates/team-agent/src/lifecycle/tests/worker_spawn_env_red.rs +36 -82
- package/crates/team-agent/src/lifecycle/tests.rs +0 -4
- package/crates/team-agent/src/lifecycle/types.rs +2 -5
- package/crates/team-agent/src/mcp_server/readiness_tests.rs +15 -17
- package/crates/team-agent/src/messaging/delivery.rs +9 -61
- package/crates/team-agent/src/messaging/tests/dup_inject.rs +67 -68
- package/crates/team-agent/src/messaging/tests/main_preserved.rs +2 -2
- package/crates/team-agent/src/messaging/tests/runtime.rs +5 -5
- package/crates/team-agent/src/messaging/tests/spine.rs +5 -1
- package/crates/team-agent/src/messaging/tests/wave2.rs +13 -20
- package/crates/team-agent/src/provider/adapter.rs +58 -356
- package/crates/team-agent/src/provider/adapters/mod.rs +5 -7
- package/crates/team-agent/src/provider/session/capture.rs +5 -105
- package/crates/team-agent/src/provider/session/context_fork/outcome.rs +8 -20
- package/crates/team-agent/src/provider/session/context_fork.rs +23 -142
- package/crates/team-agent/src/provider/session/mod.rs +2 -3
- package/crates/team-agent/src/provider/session_scan/common.rs +21 -57
- package/crates/team-agent/src/provider/session_scan.rs +6 -16
- package/crates/team-agent/src/provider/tests/adapter.rs +37 -118
- package/crates/team-agent/src/provider/tests/command_helpers.rs +1 -18
- package/crates/team-agent/src/provider/tests/idle.rs +0 -43
- package/crates/team-agent/src/provider/types.rs +0 -4
- package/crates/team-agent/src/provider/wire.rs +34 -8
- package/crates/team-agent/src/tmux_backend.rs +4 -3
- package/package.json +4 -4
- package/skills/team-agent/SKILL.md +3 -2
- package/skills/team-agent/command-coverage.json +14 -0
- package/skills/team-agent/docs/reference/team-agent-operator.md +3 -1
- package/skills/team-agent/references/team-agent-operator.md +3 -1
- package/crates/team-agent/src/lifecycle/launch/grok_per_seat.rs +0 -260
- package/crates/team-agent/src/lifecycle/tests/codex_weak_window_attribution_red.rs +0 -683
- package/crates/team-agent/src/lifecycle/tests/grok_effort_argv_red.rs +0 -247
- package/crates/team-agent/src/lifecycle/tests/grok_mcp_overlay_red.rs +0 -507
- package/crates/team-agent/src/lifecycle/tests/grok_restart_resume_red.rs +0 -300
- package/crates/team-agent/src/provider/adapters/codex.rs +0 -101
- package/crates/team-agent/src/provider/adapters/grok.rs +0 -67
- package/crates/team-agent/src/provider/session/context_fork/codex.rs +0 -514
- package/crates/team-agent/src/provider/session_scan/codex.rs +0 -326
- package/crates/team-agent/src/provider/session_scan/grok.rs +0 -288
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
//! Public lifecycle dispatch for contract seats. A private native pane is never
|
|
2
2
|
//! operated through the legacy workspace socket or provider-enum fallback.
|
|
3
3
|
use std::path::Path;
|
|
4
|
+
use std::time::Duration;
|
|
4
5
|
|
|
5
6
|
use serde_json::{json, Value};
|
|
6
|
-
use team_agent_contract::contract::
|
|
7
|
+
use team_agent_contract::contract::delivery::InputSurface;
|
|
8
|
+
use team_agent_contract::contract::probe::{PaneReadyEvidence, Probe, ProbeOutcome};
|
|
9
|
+
use team_agent_contract::contract::types::{Digest, OperationId, SeatId};
|
|
10
|
+
use team_agent_contract::host::{clock::RealClock, process::{sample_process, ProcessState}};
|
|
11
|
+
use team_agent_contract::orchestration::physical::{PhysicalRuntime, Registration};
|
|
7
12
|
use team_agent_contract::orchestration::store::{Outcome, SeatStatus};
|
|
8
13
|
|
|
9
14
|
use crate::cli::CliError;
|
|
@@ -12,7 +17,7 @@ use crate::model::ids::AgentId;
|
|
|
12
17
|
use crate::state::repository::{StateRepository, StateWriteIntent};
|
|
13
18
|
use crate::state::selector::{resolve_active_team_readonly, SelectedTeam, SelectorMode};
|
|
14
19
|
|
|
15
|
-
use super::{backend::{self, Backend, BackendError}, config, framework};
|
|
20
|
+
use super::{backend::{self, Backend, BackendError}, config, framework, registry};
|
|
16
21
|
|
|
17
22
|
fn error(error: impl std::fmt::Display) -> CliError {
|
|
18
23
|
CliError::Runtime(error.to_string())
|
|
@@ -127,8 +132,12 @@ pub fn start_agent(
|
|
|
127
132
|
backend::preflight(&config).map_err(error)?;
|
|
128
133
|
}
|
|
129
134
|
let before = if unstarted { None } else {
|
|
130
|
-
Backend::open_readonly(&selected.run_workspace, &selected.team_key).map_err(error)
|
|
131
|
-
|
|
135
|
+
let backend = Backend::open_readonly(&selected.run_workspace, &selected.team_key).map_err(error)?;
|
|
136
|
+
// A retired generation is history, not a session this start resumes.
|
|
137
|
+
match backend.store.creation_receipt(id, None).map_err(error)? {
|
|
138
|
+
Some(record) if backend.store.retired(&record.target.identity).map_err(error)? => None,
|
|
139
|
+
other => other,
|
|
140
|
+
}
|
|
132
141
|
};
|
|
133
142
|
crate::lifecycle::role_config::commit_contract_role(&source, &previous, &next).map_err(error)?;
|
|
134
143
|
let receipt = backend::start(&selected.run_workspace, &selected.team_key, role, &config.members).map_err(error)?.1;
|
|
@@ -254,6 +263,267 @@ pub fn stop_agent(
|
|
|
254
263
|
})
|
|
255
264
|
}
|
|
256
265
|
|
|
266
|
+
/// What remove/reset may rely on. Idle needs positive evidence; anything the
|
|
267
|
+
/// framework cannot prove idle is never treated as idle.
|
|
268
|
+
#[derive(Debug, PartialEq, Eq)]
|
|
269
|
+
enum Activity {
|
|
270
|
+
Stopped,
|
|
271
|
+
Exited,
|
|
272
|
+
Idle,
|
|
273
|
+
Working(String),
|
|
274
|
+
Unverified(String),
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Two captures this far apart straddle a one-second elapsed counter, and a
|
|
278
|
+
// streamed reply that hides a provider's busy marker still changes the frame.
|
|
279
|
+
const SETTLE: Duration = Duration::from_millis(1500);
|
|
280
|
+
const REMOVE_WORKING: &str = "agent is currently working; stop the agent before removing it";
|
|
281
|
+
const RESET_WORKING: &str =
|
|
282
|
+
"agent is currently working; wait for completion or stop the agent before resetting session";
|
|
283
|
+
|
|
284
|
+
/// Read-only: a held seat lease, then the seat's own pane read twice, then
|
|
285
|
+
/// the lease and queue again so a delivery between the captures still counts.
|
|
286
|
+
fn activity(workspace: &Path, team: &str, id: &SeatId) -> Result<Activity, CliError> {
|
|
287
|
+
let backend = Backend::open_readonly(workspace, team).map_err(error)?;
|
|
288
|
+
let busy = || Activity::Working("a lifecycle operation or message delivery holds the seat".into());
|
|
289
|
+
// An interrupted startup holds the lease before its seat row is published.
|
|
290
|
+
if backend.store.leased(id).map_err(error)? {
|
|
291
|
+
return Ok(busy());
|
|
292
|
+
}
|
|
293
|
+
let Some(seat) = backend.store.seat(id).map_err(error)? else {
|
|
294
|
+
return Ok(Activity::Stopped);
|
|
295
|
+
};
|
|
296
|
+
if seat.status == SeatStatus::Stopped {
|
|
297
|
+
return Ok(Activity::Stopped);
|
|
298
|
+
}
|
|
299
|
+
let Some(target) = seat.physical.as_ref() else {
|
|
300
|
+
return Ok(Activity::Unverified("no native target is recorded".into()));
|
|
301
|
+
};
|
|
302
|
+
match sample_process(&target.process) {
|
|
303
|
+
ProcessState::Alive => {}
|
|
304
|
+
ProcessState::Exited | ProcessState::Replaced => return Ok(Activity::Exited),
|
|
305
|
+
ProcessState::Unknown(failure) => {
|
|
306
|
+
return Ok(Activity::Unverified(format!("native process state is unknown: {failure}")))
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
// Starting stays until the first delivery; only the surface decides then.
|
|
310
|
+
if seat.status == SeatStatus::Unknown {
|
|
311
|
+
return Ok(Activity::Unverified("native seat is quarantined after an uncertain native effect".into()));
|
|
312
|
+
}
|
|
313
|
+
let selected = registry::provider(seat.provider.as_str()).map_err(error)?;
|
|
314
|
+
let adapter = backend::adapter(&backend.binding, &seat.identity, &seat.provider).map_err(error)?;
|
|
315
|
+
let hooks = adapter.hooks();
|
|
316
|
+
let clock = RealClock::new();
|
|
317
|
+
let nonce = backend.store.propose_identity(id).map_err(error)?.instance;
|
|
318
|
+
let physical = PhysicalRuntime::new(
|
|
319
|
+
Registration {
|
|
320
|
+
descriptor: selected.descriptor(),
|
|
321
|
+
hooks: &hooks,
|
|
322
|
+
controls: selected.controls(),
|
|
323
|
+
startup: selected.startup(),
|
|
324
|
+
session_panel: selected.session_panel(),
|
|
325
|
+
},
|
|
326
|
+
backend::settings(&backend.binding, selected),
|
|
327
|
+
OperationId::new(format!("observe-{}", nonce.as_str())).map_err(error)?,
|
|
328
|
+
&clock,
|
|
329
|
+
).map_err(error)?;
|
|
330
|
+
let capture = || physical.surface(&seat)
|
|
331
|
+
.map_err(|failure| Activity::Unverified(format!("native surface capture failed: {failure}")));
|
|
332
|
+
let first = match capture() {
|
|
333
|
+
Ok(sample) => sample,
|
|
334
|
+
Err(unverified) => return Ok(unverified),
|
|
335
|
+
};
|
|
336
|
+
std::thread::sleep(SETTLE);
|
|
337
|
+
let observed = match capture() {
|
|
338
|
+
Ok(second) => classify(&first, &second),
|
|
339
|
+
Err(unverified) => unverified,
|
|
340
|
+
};
|
|
341
|
+
if observed == Activity::Idle {
|
|
342
|
+
if backend.store.leased(id).map_err(error)? {
|
|
343
|
+
return Ok(busy());
|
|
344
|
+
}
|
|
345
|
+
if backend.store.has_queued_for(&seat.identity).map_err(error)? {
|
|
346
|
+
return Ok(Activity::Working("a queued message is about to be delivered".into()));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
Ok(observed)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
fn classify(
|
|
353
|
+
first: &(Probe<PaneReadyEvidence>, Digest),
|
|
354
|
+
second: &(Probe<PaneReadyEvidence>, Digest),
|
|
355
|
+
) -> Activity {
|
|
356
|
+
let read = |sample: &(Probe<PaneReadyEvidence>, Digest)| match &sample.0.outcome {
|
|
357
|
+
ProbeOutcome::Observed(pane) => Some(pane.surface),
|
|
358
|
+
_ => None,
|
|
359
|
+
};
|
|
360
|
+
let (before, after) = (read(first), read(second));
|
|
361
|
+
if let Some(busy) = [before, after].into_iter().flatten().find(|surface| matches!(surface,
|
|
362
|
+
InputSurface::Busy | InputSurface::Queued | InputSurface::ComposerContainsPaste | InputSurface::NativeAccepted))
|
|
363
|
+
{
|
|
364
|
+
return Activity::Working(format!("native surface reads {busy:?}"));
|
|
365
|
+
}
|
|
366
|
+
match (before, after) {
|
|
367
|
+
(Some(InputSurface::ComposerReady), Some(InputSurface::ComposerReady)) if first.1 == second.1 => Activity::Idle,
|
|
368
|
+
(Some(InputSurface::ComposerReady), Some(InputSurface::ComposerReady)) => {
|
|
369
|
+
Activity::Working("native output changed between two captures".into())
|
|
370
|
+
}
|
|
371
|
+
_ => Activity::Unverified(format!("native surface reads {before:?} then {after:?}")),
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
fn refused(agent: &str, reason: &str, error: &str, evidence: Option<String>, action: &str) -> Value {
|
|
376
|
+
json!({"ok":false,"agent_id":agent,"status":"refused","runtime":"contract","reason":reason,
|
|
377
|
+
"error":error,"evidence":evidence,"action":action})
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
fn stop_seat(selected: &SelectedTeam, id: &SeatId) -> Result<OperationId, CliError> {
|
|
381
|
+
let receipt = backend::stop(&selected.run_workspace, &selected.team_key, id).map_err(error)?;
|
|
382
|
+
check_record(&receipt)?;
|
|
383
|
+
Ok(receipt.id)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/// A working seat is never removed, even with --force. A stopped seat needs
|
|
387
|
+
/// only --confirm; an idle live seat also needs --force and is stopped first.
|
|
388
|
+
pub fn remove_agent(
|
|
389
|
+
workspace: &Path,
|
|
390
|
+
team: Option<&str>,
|
|
391
|
+
agent: &str,
|
|
392
|
+
from_spec: bool,
|
|
393
|
+
confirm: bool,
|
|
394
|
+
force: bool,
|
|
395
|
+
) -> Result<Option<Value>, CliError> {
|
|
396
|
+
with_agent(workspace, team, agent, "remove-agent", |selected, id| {
|
|
397
|
+
if !confirm {
|
|
398
|
+
return Ok(refused(agent, "confirm_required", "remove-agent requires --confirm", None, "rerun with --confirm"));
|
|
399
|
+
}
|
|
400
|
+
let row = selected.state.get("agents").and_then(|agents| agents.get(agent))
|
|
401
|
+
.ok_or(BackendError::Metadata).map_err(error)?;
|
|
402
|
+
let agent_id = AgentId::new(agent);
|
|
403
|
+
// Every shared-record input resolves before the native seat is touched.
|
|
404
|
+
crate::lifecycle::restart::remove::preflight_contract_removal(
|
|
405
|
+
&selected.run_workspace, &agent_id, Some(&selected.team_key),
|
|
406
|
+
).map_err(error)?;
|
|
407
|
+
let mut stopped = None;
|
|
408
|
+
if !framework::is_paused_unstarted(row) {
|
|
409
|
+
match activity(&selected.run_workspace, &selected.team_key, id)? {
|
|
410
|
+
Activity::Stopped => {}
|
|
411
|
+
Activity::Working(evidence) => return Ok(refused(agent, "agent_working", REMOVE_WORKING,
|
|
412
|
+
Some(evidence), "wait for the agent to finish, or stop it, then rerun remove-agent")),
|
|
413
|
+
Activity::Unverified(evidence) => return Ok(refused(agent, "agent_activity_unverified",
|
|
414
|
+
"agent activity could not be verified idle; stop the agent before removing it",
|
|
415
|
+
Some(evidence), "stop the agent, then rerun remove-agent")),
|
|
416
|
+
Activity::Idle if !force => return Ok(refused(agent, "force_required",
|
|
417
|
+
"agent is running; remove-agent requires --force", None,
|
|
418
|
+
"rerun with --force to stop and remove the running agent")),
|
|
419
|
+
Activity::Idle | Activity::Exited => stopped = Some(stop_seat(selected, id)?),
|
|
420
|
+
}
|
|
421
|
+
// The native identity ends before the shared records: a failure
|
|
422
|
+
// below leaves a row a rerun removes, never a resumable old seat.
|
|
423
|
+
backend::retire(&selected.run_workspace, &selected.team_key, id).map_err(error)?;
|
|
424
|
+
}
|
|
425
|
+
let report = crate::lifecycle::restart::remove::remove_contract_records_locked(
|
|
426
|
+
&selected.run_workspace, &agent_id, from_spec, force, stopped.is_some(),
|
|
427
|
+
Some(&selected.team_key),
|
|
428
|
+
).map_err(error)?;
|
|
429
|
+
Ok(json!({"ok":true,"agent_id":agent,"status":"removed","runtime":"contract",
|
|
430
|
+
"stopped":stopped.is_some(),"operation":stopped,"report":format!("{report:?}")}))
|
|
431
|
+
})
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/// Ok: the seat is stopped, exited or idle; Err: the reset refusal receipt.
|
|
435
|
+
fn reset_guard(observed: Activity, agent: &str) -> Result<Activity, Value> {
|
|
436
|
+
match observed {
|
|
437
|
+
Activity::Working(evidence) => Err(refused(agent, "agent_working", RESET_WORKING,
|
|
438
|
+
Some(evidence), "wait for the agent to finish, or stop it, then rerun reset-agent")),
|
|
439
|
+
Activity::Unverified(evidence) => Err(refused(agent, "agent_activity_unverified",
|
|
440
|
+
"agent activity could not be verified idle; wait for completion or stop the agent before resetting session",
|
|
441
|
+
Some(evidence), "stop the agent, then rerun reset-agent")),
|
|
442
|
+
clear => Ok(clear),
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/// Discards the native session only when the seat is provably not working,
|
|
447
|
+
/// then starts the same seat ID on a fresh generation and session.
|
|
448
|
+
pub fn reset_agent(
|
|
449
|
+
workspace: &Path,
|
|
450
|
+
team: Option<&str>,
|
|
451
|
+
agent: &str,
|
|
452
|
+
discard_session: bool,
|
|
453
|
+
) -> Result<Option<Value>, CliError> {
|
|
454
|
+
with_agent(workspace, team, agent, "reset-agent", |selected, id| {
|
|
455
|
+
if !discard_session {
|
|
456
|
+
return Ok(json!({"ok":false,"agent_id":agent,"status":"refused","runtime":"contract",
|
|
457
|
+
"reason":"discard_session_required"}));
|
|
458
|
+
}
|
|
459
|
+
let row = selected.state.get("agents").and_then(|agents| agents.get(agent))
|
|
460
|
+
.ok_or(BackendError::Metadata).map_err(error)?;
|
|
461
|
+
let config = framework::team_config(&selected.run_workspace, &selected.team_dir, &selected.team_key)
|
|
462
|
+
.map_err(error)?.ok_or_else(|| error("contract role no longer configured"))?;
|
|
463
|
+
let role = config.roles.iter().find(|role| &role.id == id)
|
|
464
|
+
.ok_or_else(|| error("contract role no longer configured"))?;
|
|
465
|
+
if row.get("provider").and_then(Value::as_str) != Some(role.provider.as_str()) {
|
|
466
|
+
return Err(error("provider change requires a new seat identity"));
|
|
467
|
+
}
|
|
468
|
+
if role.paused {
|
|
469
|
+
return Ok(json!({"ok":false,"agent_id":agent,"status":"paused","reason":"agent_paused","runtime":"contract"}));
|
|
470
|
+
}
|
|
471
|
+
let live = !framework::is_paused_unstarted(row);
|
|
472
|
+
// The guard runs when the command is invoked, before the slow native
|
|
473
|
+
// admission below: a message sent just before reset is still queued or
|
|
474
|
+
// in flight then. It runs again right before the stop, so work that
|
|
475
|
+
// began during admission is refused too.
|
|
476
|
+
if live {
|
|
477
|
+
if let Err(refusal) = reset_guard(activity(&selected.run_workspace, &selected.team_key, id)?, agent) {
|
|
478
|
+
return Ok(refusal);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
// Pure admission of the replacement precedes any stop or retirement.
|
|
482
|
+
let mut admission = config.clone();
|
|
483
|
+
admission.roles.retain(|candidate| &candidate.id == id);
|
|
484
|
+
backend::preflight(&admission).map_err(error)?;
|
|
485
|
+
let mut stopped = None;
|
|
486
|
+
let mut discarded = None;
|
|
487
|
+
if live {
|
|
488
|
+
// The fresh start below must not fail its candidate fence after the
|
|
489
|
+
// old seat is already gone.
|
|
490
|
+
backend::require_candidate(&Backend::open_readonly(&selected.run_workspace, &selected.team_key)
|
|
491
|
+
.map_err(error)?.binding).map_err(error)?;
|
|
492
|
+
match reset_guard(activity(&selected.run_workspace, &selected.team_key, id)?, agent) {
|
|
493
|
+
Err(refusal) => return Ok(refusal),
|
|
494
|
+
Ok(Activity::Stopped) => {}
|
|
495
|
+
Ok(_) => stopped = Some(stop_seat(selected, id)?),
|
|
496
|
+
}
|
|
497
|
+
discarded = backend::retire(&selected.run_workspace, &selected.team_key, id).map_err(error)?
|
|
498
|
+
.and_then(|seat| seat.session).map(|session| session.native_session);
|
|
499
|
+
}
|
|
500
|
+
// The row stops naming the retired incarnation. If the fresh start
|
|
501
|
+
// below fails, this unbound stopped seed is skipped by team restart
|
|
502
|
+
// and admitted fresh by a later start.
|
|
503
|
+
StateRepository::new(&selected.run_workspace).commit(
|
|
504
|
+
StateWriteIntent::StartAgent { team_key: &selected.team_key, agent_id: agent },
|
|
505
|
+
|state| {
|
|
506
|
+
if let Some(row) = state.get_mut("agents").and_then(|agents| agents.get_mut(agent)).and_then(Value::as_object_mut) {
|
|
507
|
+
for key in ["contract_scope", "contract_instance", "generation", "session_id"] {
|
|
508
|
+
row.insert(key.into(), Value::Null);
|
|
509
|
+
}
|
|
510
|
+
row.insert("status".into(), json!("stopped"));
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
).map_err(error)?;
|
|
514
|
+
let projection = framework::start_configured(&selected.run_workspace, &selected.team_dir, &selected.team_key, id)?;
|
|
515
|
+
let coordinator = crate::coordinator::start_coordinator_with_team(
|
|
516
|
+
&crate::coordinator::WorkspacePath::new(selected.run_workspace.clone()), Some(&selected.team_key),
|
|
517
|
+
).map_err(error)?;
|
|
518
|
+
Ok(json!({"ok":coordinator.ok,"agent_id":agent,"status":"reset","runtime":"contract",
|
|
519
|
+
"start_mode":"fresh","stopped":stopped.is_some(),"operation":stopped,
|
|
520
|
+
"discarded_session_id":discarded,"session_id":projection.get("session_id"),
|
|
521
|
+
"new_session_id":projection.get("session_id"),"generation":projection.get("generation"),
|
|
522
|
+
"coordinator":crate::lifecycle::coordinator_start_summary_value(
|
|
523
|
+
&crate::lifecycle::CoordinatorStartSummary::from_start_report(&coordinator))}))
|
|
524
|
+
})
|
|
525
|
+
}
|
|
526
|
+
|
|
257
527
|
#[cfg(test)]
|
|
258
528
|
mod tests {
|
|
259
529
|
// Assembly guard, not a substitute for the real shared-session restart/
|
|
@@ -337,4 +607,65 @@ mod tests {
|
|
|
337
607
|
.unwrap_err().to_string();
|
|
338
608
|
assert!(error.contains("metadata is invalid"));
|
|
339
609
|
}
|
|
610
|
+
|
|
611
|
+
fn surface(surface: Option<InputSurface>, text: &[u8]) -> (Probe<PaneReadyEvidence>, Digest) {
|
|
612
|
+
let scope = team_agent_contract::contract::probe::EvidenceScope {
|
|
613
|
+
identity: receipt(Outcome::Committed).target.identity, endpoint: "fixture".into(),
|
|
614
|
+
pane: "%0".into(), binding: "fixture".into(), session: None, sequence: 1,
|
|
615
|
+
observed_at: Duration::ZERO, valid_for: Duration::from_secs(2), source: "fixture".into(),
|
|
616
|
+
};
|
|
617
|
+
let outcome = match surface {
|
|
618
|
+
Some(surface) => ProbeOutcome::Observed(PaneReadyEvidence { profile_id: "fixture".into(), surface }),
|
|
619
|
+
None => ProbeOutcome::Pending { deadline: Duration::ZERO },
|
|
620
|
+
};
|
|
621
|
+
(Probe { scope, outcome }, team_agent_contract::host::digest(text))
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
#[test]
|
|
625
|
+
fn working_guard_calls_a_seat_idle_only_on_two_still_composer_ready_captures() {
|
|
626
|
+
let ready = surface(Some(InputSurface::ComposerReady), b"idle");
|
|
627
|
+
assert_eq!(classify(&ready, &ready.clone()), Activity::Idle);
|
|
628
|
+
// A streamed reply can hide the busy marker, but it still moves the frame.
|
|
629
|
+
assert!(matches!(classify(&ready, &surface(Some(InputSurface::ComposerReady), b"idle, then more")),
|
|
630
|
+
Activity::Working(_)));
|
|
631
|
+
for busy in [InputSurface::Busy, InputSurface::Queued, InputSurface::ComposerContainsPaste,
|
|
632
|
+
InputSurface::NativeAccepted]
|
|
633
|
+
{
|
|
634
|
+
assert!(matches!(classify(&ready, &surface(Some(busy), b"idle")), Activity::Working(_)), "{busy:?}");
|
|
635
|
+
assert!(matches!(classify(&surface(Some(busy), b"x"), &surface(None, b"x")), Activity::Working(_)),
|
|
636
|
+
"{busy:?}");
|
|
637
|
+
}
|
|
638
|
+
for other in [None, Some(InputSurface::Starting), Some(InputSurface::ShellOrUnknown),
|
|
639
|
+
Some(InputSurface::StartupRiskWarning), Some(InputSurface::Fault)]
|
|
640
|
+
{
|
|
641
|
+
assert!(matches!(classify(&ready, &surface(other, b"idle")), Activity::Unverified(_)), "{other:?}");
|
|
642
|
+
assert!(matches!(classify(&surface(other, b"idle"), &ready), Activity::Unverified(_)), "{other:?}");
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// 2.8a: a reset issued right after a send must observe the queued or
|
|
647
|
+
// in-flight message, not a turn that the slow admission let finish.
|
|
648
|
+
#[test]
|
|
649
|
+
fn reset_guards_the_seat_before_its_slow_admission_and_again_before_the_stop() {
|
|
650
|
+
let source = include_str!("commands.rs");
|
|
651
|
+
let body = source.split_once("pub fn reset_agent(").unwrap().1
|
|
652
|
+
.split_once("#[cfg(test)]").unwrap().0;
|
|
653
|
+
let first = body.find("reset_guard(activity(").unwrap();
|
|
654
|
+
let admission = body.find("backend::preflight(&admission)").unwrap();
|
|
655
|
+
let second = body.rfind("reset_guard(activity(").unwrap();
|
|
656
|
+
let stop = body.find("stop_seat(selected, id)").unwrap();
|
|
657
|
+
assert!(first < admission && admission < second && second < stop);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
#[test]
|
|
661
|
+
fn remove_and_reset_route_contract_seats_before_the_legacy_refusal() {
|
|
662
|
+
let cli = include_str!("../cli/mod.rs");
|
|
663
|
+
for (entry, native, gate) in [
|
|
664
|
+
("pub fn remove_agent(", "contract_runtime::commands::remove_agent(", "\"remove-agent\","),
|
|
665
|
+
("pub fn reset_agent(", "contract_runtime::commands::reset_agent(", "\"reset-agent\","),
|
|
666
|
+
] {
|
|
667
|
+
let body = cli.split_once(entry).unwrap().1;
|
|
668
|
+
assert!(body.find(native).unwrap() < body.find(gate).unwrap(), "{entry}");
|
|
669
|
+
}
|
|
670
|
+
}
|
|
340
671
|
}
|
|
@@ -99,7 +99,8 @@ fn mode(meta: &Value, path: &Path) -> Result<Option<CommunicationMode>, ConfigEr
|
|
|
99
99
|
.transpose()
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
/// `contract_runtime` is an exact boolean
|
|
102
|
+
/// `contract_runtime` is an exact boolean. Every contract name selects its leaf
|
|
103
|
+
/// without it; `true` only insists that the provider be a contract leaf.
|
|
103
104
|
fn opted_in(meta: &Value, path: &Path) -> Result<bool, ConfigError> {
|
|
104
105
|
match meta.get("contract_runtime") {
|
|
105
106
|
None => Ok(false),
|
|
@@ -118,7 +119,7 @@ fn malformed_contract_header(body: &str) -> bool {
|
|
|
118
119
|
.map(|(key, value)| (key.trim(), value.split('#').next().unwrap_or("").trim().trim_matches(['\'', '"'])))
|
|
119
120
|
.collect();
|
|
120
121
|
let opted_in = fields.iter().any(|(key, value)| *key == "contract_runtime" && *value == "true");
|
|
121
|
-
opted_in || fields.iter().any(|(key, value)| *key == "provider" && super::registry::recognizes(value
|
|
122
|
+
opted_in || fields.iter().any(|(key, value)| *key == "provider" && super::registry::recognizes(value))
|
|
122
123
|
})
|
|
123
124
|
}
|
|
124
125
|
|
|
@@ -192,7 +193,7 @@ fn read_team_inner(
|
|
|
192
193
|
None | Some(Value::Null) => true,
|
|
193
194
|
Some(provider) => provider
|
|
194
195
|
.as_str()
|
|
195
|
-
.is_some_and(
|
|
196
|
+
.is_some_and(super::registry::recognizes),
|
|
196
197
|
}
|
|
197
198
|
{
|
|
198
199
|
selected = true;
|
|
@@ -264,8 +265,8 @@ fn read_team_inner(
|
|
|
264
265
|
}
|
|
265
266
|
let provider = text(&meta, "provider", &source)?.unwrap_or_else(|| "pi".into());
|
|
266
267
|
let opted = opted_in(&meta, &source)?;
|
|
267
|
-
let registration = super::registry::declared(&provider
|
|
268
|
-
if registration.is_none() && (opted || super::registry::recognizes(&provider
|
|
268
|
+
let registration = super::registry::declared(&provider);
|
|
269
|
+
if registration.is_none() && (opted || super::registry::recognizes(&provider)) {
|
|
269
270
|
return Err(invalid(&source, "provider"));
|
|
270
271
|
}
|
|
271
272
|
let runtime = if registration.is_some() {
|
|
@@ -55,7 +55,7 @@ pub fn require_legacy_lifecycle(
|
|
|
55
55
|
return Ok(());
|
|
56
56
|
};
|
|
57
57
|
let row = agent.and_then(|id| selected.state.get("agents").and_then(|agents| agents.get(id)));
|
|
58
|
-
// A pre-upgrade legacy Cursor, Claude or Pi seat has no engine left; only teardown is admitted.
|
|
58
|
+
// A pre-upgrade legacy Cursor, Claude, Codex, Grok or Pi seat has no engine left; only teardown is admitted.
|
|
59
59
|
let teardown = matches!(operation, "stop-agent" | "remove-agent");
|
|
60
60
|
if let Some(retired) = row
|
|
61
61
|
.and_then(|row| row.get("provider"))
|
|
@@ -114,11 +114,8 @@ pub fn declares_contract(path: Option<&Path>, provider: Option<&str>) -> bool {
|
|
|
114
114
|
.and_then(crate::model::yaml::Value::as_str)
|
|
115
115
|
.map(str::to_owned)
|
|
116
116
|
});
|
|
117
|
-
let opted_in = meta.as_ref().is_some_and(|meta| {
|
|
118
|
-
matches!(meta.get("contract_runtime"), Some(crate::model::yaml::Value::Bool(true)))
|
|
119
|
-
});
|
|
120
117
|
// An omitted provider is the default `pi`, which declares the Pi leaf.
|
|
121
|
-
super::registry::recognizes(provider.or(declared.as_deref()).unwrap_or("pi")
|
|
118
|
+
super::registry::recognizes(provider.or(declared.as_deref()).unwrap_or("pi"))
|
|
122
119
|
}
|
|
123
120
|
pub fn team_config(
|
|
124
121
|
workspace: &Path,
|