feat(terminals): reprise de conversation par cellule + fix ordre d'écriture

Permet de recharger la conversation CLI précédente de chaque cellule à la
réouverture du projet, de façon universelle (indépendant du modèle/CLI).

- profil AgentRuntime: bloc déclaratif optionnel `session { assignFlag, resumeFlag }`
- LeafCell: `conversationId` (persistant, distinct du SessionId PTY) + `agentWasRunning`
- runtime: SessionPlan (None/Assign/Resume) + composition pure des args
- LaunchAgent: décide Assign vs Resume, génère l'UUID, remonte l'id assigné
  (persistance par l'appelant via setCellConversation — découplage SRP)
- close: SnapshotRunningAgents fige `agentWasRunning` avant le kill-all
  (statut clot/en cours universel, sans parsing CLI)
- SessionInspector: port optionnel best-effort + adapter ClaudeTranscriptInspector
- popup de reprise par cellule (statut + sujet/tokens si dispo), intercalée
  avant le Resume auto, jamais sur le chemin reattach

fix(terminals): sérialise les écritures PTY (file FIFO par handle) — corrige
les caractères mélangés/accents dus au réordonnancement des invoke Tauri concurrents

fix(layout): l'opération `move` préservait mal les champs du leaf (perdait `agent`)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-07 22:27:08 +02:00
parent d11eaaa8c0
commit 3ed0f6b45f
61 changed files with 5098 additions and 98 deletions

View File

@ -11,9 +11,10 @@ use application::{
AppError, CloseProjectInput, CreateAgentInput, CreateLayoutInput, DeleteAgentInput,
DeleteLayoutInput, DeleteTemplateInput, DetectAgentDriftInput, GitBranchesInput,
GitCheckoutInput, GitCommitInput, GitGraphInput, GitInitInput, GitLogInput, GitStagePathInput,
GitStatusInput, LaunchAgentInput, ListAgentsInput, ListLayoutsInput, LoadLayoutInput,
GitStatusInput, InspectConversationInput, LaunchAgentInput, ListAgentsInput, ListLayoutsInput, LoadLayoutInput,
MutateLayoutInput, OpenProjectInput, ReadAgentContextInput, RenameLayoutInput,
SetActiveLayoutInput, SyncAgentWithTemplateInput, UpdateAgentContextInput,
SetActiveLayoutInput, SnapshotRunningAgentsInput, SyncAgentWithTemplateInput,
UpdateAgentContextInput,
AssignSkillToAgentInput, CreateSkillInput, DeleteSkillInput, ListSkillsInput,
UnassignSkillFromAgentInput, UpdateSkillInput,
};
@ -28,6 +29,7 @@ use crate::dto::{
DetectProfilesRequestDto, DetectProfilesResponseDto, ErrorDto, FirstRunStateDto,
GitBranchesDto, GitCheckoutRequestDto, GitCommitDto, GitCommitListDto, GitCommitRequestDto,
GitStageRequestDto, GitStatusListDto, GraphCommitListDto, HealthRequestDto, HealthResponseDto,
ConversationDetailsDto, InspectConversationRequestDto,
LaunchAgentRequestDto, LayoutDto, LayoutOperationDto, ListLayoutsDto, OpenTerminalRequestDto,
ProfileDto, ProfileListDto, ProjectDto, ProjectListDto, ReadAgentContextResponseDto,
ReattachResultDto, RenameLayoutRequestDto, ResizeTerminalRequestDto, SaveProfileRequestDto,
@ -110,6 +112,13 @@ pub async fn close_project(
state: State<'_, AppState>,
) -> Result<(), ErrorDto> {
let id = parse_project_id(&project_id)?;
// T5: freeze `agent_was_running` on every agent leaf BEFORE any PTY release,
// reading the live registry as it stands now. Best-effort: a snapshot failure
// must not block the close.
let _ = state
.snapshot_running_agents
.execute(SnapshotRunningAgentsInput { project_id: id })
.await;
// Stop tailing this project's `.ideai/requests/` tree (§14.3).
state.stop_orchestrator_watch(&id);
state
@ -740,6 +749,35 @@ pub async fn delete_agent(
.map_err(ErrorDto::from)
}
/// `inspect_conversation` — best-effort enriched details (last topic + token
/// indicator) for a resume popup (T7).
///
/// Best-effort by contract: a missing/unsupported inspector or a missing
/// transcript yields **empty details** (absent `lastTopic`/`tokenCount`), never
/// an error. Only a genuine store failure (resolving the agent/profile) errors.
///
/// # Errors
/// Returns an [`ErrorDto`] (`INVALID` for a malformed id, `NOT_FOUND` if the
/// project, agent or profile is unknown, `STORE` on a manifest/profile failure).
#[tauri::command]
pub async fn inspect_conversation(
request: InspectConversationRequestDto,
state: State<'_, AppState>,
) -> Result<ConversationDetailsDto, ErrorDto> {
let project = resolve_project(&request.project_id, &state).await?;
let agent_id = parse_agent_id(&request.agent_id)?;
state
.inspect_conversation
.execute(InspectConversationInput {
project,
agent_id,
conversation_id: request.conversation_id,
})
.await
.map(ConversationDetailsDto::from)
.map_err(ErrorDto::from)
}
/// `launch_agent` — spawn an agent's CLI in a PTY and wire its byte stream to
/// the frontend via a [`Channel`].
///
@ -770,6 +808,10 @@ pub async fn launch_agent(
rows: request.rows,
cols: request.cols,
node_id: None,
// Resume id is a property of the hosting cell; the frontend passes the
// leaf's current conversation id here. `None` until the launch_agent
// command is made layout-aware (T4 wiring follow-up).
conversation_id: request.conversation_id.clone(),
})
.await
.map_err(ErrorDto::from)?;

View File

@ -231,6 +231,12 @@ pub struct TerminalSessionDto {
pub rows: u16,
/// Current cols.
pub cols: u16,
/// Conversation id **assigned** by this launch, when the agent's profile
/// supports session assignment and the hosting cell had none yet (T4b). The
/// front persists it on the leaf (`setCellConversation`) so the next open
/// resumes. `None` for a plain terminal, a resume, or a degraded launch.
#[serde(skip_serializing_if = "Option::is_none")]
pub assigned_conversation_id: Option<String>,
}
impl From<OpenTerminalOutput> for TerminalSessionDto {
@ -241,6 +247,7 @@ impl From<OpenTerminalOutput> for TerminalSessionDto {
cwd: s.cwd.as_str().to_owned(),
rows: s.pty_size.rows,
cols: s.pty_size.cols,
assigned_conversation_id: None,
}
}
}
@ -436,6 +443,15 @@ pub enum LayoutOperationDto {
#[serde(default)]
agent: Option<String>,
},
/// Record/clear the persistent CLI conversation id on a leaf (T4b).
#[serde(rename_all = "camelCase")]
SetCellConversation {
/// Hosting leaf.
target: String,
/// Conversation id, or `null` to clear.
#[serde(default)]
conversation_id: Option<String>,
},
}
impl LayoutOperationDto {
@ -479,6 +495,13 @@ impl LayoutOperationDto {
target: parse_node_id(&target)?,
agent: agent.as_deref().map(parse_agent_id).transpose()?,
},
Self::SetCellConversation {
target,
conversation_id,
} => LayoutOperation::SetCellConversation {
target: parse_node_id(&target)?,
conversation_id,
},
})
}
}
@ -832,7 +855,8 @@ pub fn parse_profile_id(raw: &str) -> Result<ProfileId, ErrorDto> {
// ---------------------------------------------------------------------------
use application::{
CreateAgentOutput, LaunchAgentOutput, ListAgentsOutput, ReadAgentContextOutput,
CreateAgentOutput, InspectConversationOutput, LaunchAgentOutput, ListAgentsOutput,
ReadAgentContextOutput,
};
use domain::Agent;
@ -915,16 +939,60 @@ pub struct LaunchAgentRequestDto {
pub rows: u16,
/// Initial terminal width in columns.
pub cols: u16,
/// Persistent CLI conversation id currently recorded on the hosting cell, if
/// any. `Some` ⇒ the launch resumes it; absent/`None` ⇒ a fresh cell (the
/// launch may assign a new id when the profile supports it).
#[serde(default)]
pub conversation_id: Option<String>,
}
impl From<LaunchAgentOutput> for TerminalSessionDto {
fn from(out: LaunchAgentOutput) -> Self {
let assigned_conversation_id = out.assigned_conversation_id;
let s = out.session;
Self {
session_id: s.id.to_string(),
cwd: s.cwd.as_str().to_owned(),
rows: s.pty_size.rows,
cols: s.pty_size.cols,
assigned_conversation_id,
}
}
}
/// Request DTO for `inspect_conversation` (T7).
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InspectConversationRequestDto {
/// Id of the owning project.
pub project_id: String,
/// Id of the agent whose conversation is inspected.
pub agent_id: String,
/// The conversation id recorded on the hosting cell.
pub conversation_id: String,
}
/// Response DTO for `inspect_conversation` (T7): the best-effort enriched
/// details for a resume popup. Both fields are optional and **omitted from the
/// wire when `None`** (`skip_serializing_if`), so the TypeScript side sees an
/// absent key — not `null` — for a degraded inspection.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ConversationDetailsDto {
/// A short, best-effort label for the conversation (last user message,
/// truncated). Absent when it could not be extracted.
#[serde(skip_serializing_if = "Option::is_none")]
pub last_topic: Option<String>,
/// A best-effort cumulative token count. Absent when no usage info exists.
#[serde(skip_serializing_if = "Option::is_none")]
pub token_count: Option<u64>,
}
impl From<InspectConversationOutput> for ConversationDetailsDto {
fn from(out: InspectConversationOutput) -> Self {
Self {
last_topic: out.details.last_topic,
token_count: out.details.token_count,
}
}
}

View File

@ -59,8 +59,22 @@ pub fn run() {
if let tauri::WindowEvent::CloseRequested { .. } = event {
if let Some(state) = handle.try_state::<AppState>() {
let pty = std::sync::Arc::clone(&state.pty_port);
// ORDER IS CRITICAL: freeze `agent_was_running` on every
// agent leaf of every open project FIRST, reading the live
// PTY registry as it stands now; only THEN kill the PTYs.
// If we killed first, the registry would be empty and every
// agent would be persisted as "closed".
let snapshot = std::sync::Arc::clone(&state.snapshot_running_agents);
let open_projects = state.open_project_ids();
let handles = state.terminal_sessions.handles();
tauri::async_runtime::block_on(async move {
for project_id in open_projects {
let _ = snapshot
.execute(application::SnapshotRunningAgentsInput {
project_id,
})
.await;
}
for h in handles {
let _ = pty.kill(&h).await;
}
@ -103,6 +117,7 @@ pub fn run() {
commands::update_agent_context,
commands::delete_agent,
commands::launch_agent,
commands::inspect_conversation,
commands::create_template,
commands::update_template,
commands::list_templates,

View File

@ -14,10 +14,10 @@ use application::{
CreateAgentFromTemplate, CreateLayout, CreateProject, CreateTemplate, DeleteAgent,
DeleteLayout, DeleteProfile, DeleteTemplate, DetectAgentDrift, DetectProfiles, FirstRunState,
GitBranches, GitCheckout, GitCommit, GitGraph, GitInit, GitLog, GitStage, GitStatus, GitUnstage,
HealthUseCase, LaunchAgent, ListAgents, ListLayouts, ListProfiles, ListProjects, ListSkills,
ListTemplates, LoadLayout, MoveTabToNewWindow, MutateLayout, OpenProject, OpenTerminal,
OrchestratorService, ReadAgentContext, ReferenceProfiles, RenameLayout, ResizeTerminal,
SaveProfile, SetActiveLayout,
HealthUseCase, InspectConversation, LaunchAgent, ListAgents, ListLayouts, ListProfiles, ListProjects, ListSkills,
ListTemplates, LiveAgentRegistry, LoadLayout, MoveTabToNewWindow, MutateLayout, OpenProject,
OpenTerminal, OrchestratorService, ReadAgentContext, ReferenceProfiles, RenameLayout,
ResizeTerminal, SaveProfile, SetActiveLayout, SnapshotRunningAgents,
AssignSkillToAgent, CreateSkill, DeleteSkill, UnassignSkillFromAgent, UpdateSkill,
SyncAgentWithTemplate, TerminalSessions, UpdateAgentContext, UpdateTemplate, WriteToTerminal,
};
@ -28,9 +28,10 @@ use domain::ports::{
use domain::{DomainEvent, Project, ProjectId};
use infrastructure::{
CliAgentRuntime, FsOrchestratorWatcher, FsProfileStore, FsProjectStore, FsSkillStore,
FsTemplateStore, Git2Repository, IdeaiContextStore, LocalFileSystem, LocalProcessSpawner,
OrchestratorWatchHandle, PortablePtyAdapter, SystemClock, TokioBroadcastEventBus, UuidGenerator,
ClaudeTranscriptInspector, CliAgentRuntime, FsOrchestratorWatcher, FsProfileStore,
FsProjectStore, FsSkillStore, FsTemplateStore, Git2Repository, IdeaiContextStore,
LocalFileSystem, LocalProcessSpawner, OrchestratorWatchHandle, PortablePtyAdapter, SystemClock,
TokioBroadcastEventBus, UuidGenerator,
};
use crate::pty::PtyBridge;
@ -76,6 +77,8 @@ pub struct AppState {
pub delete_layout: Arc<DeleteLayout>,
/// Set the active named layout (#4).
pub set_active_layout: Arc<SetActiveLayout>,
/// Freeze `agent_was_running` on every agent leaf before a PTY kill (T5).
pub snapshot_running_agents: Arc<SnapshotRunningAgents>,
/// Detect which candidate profiles' CLIs are installed (first-run).
pub detect_profiles: Arc<DetectProfiles>,
/// List configured profiles.
@ -112,6 +115,11 @@ pub struct AppState {
pub delete_agent: Arc<DeleteAgent>,
/// Launch an agent (spawn PTY, apply injection strategy).
pub launch_agent: Arc<LaunchAgent>,
/// Best-effort inspection of a conversation (last topic + token indicator)
/// for the resume popup (T7). Optional/extensible: backed by a `Vec` of
/// [`domain::ports::SessionInspector`]s; an empty/missing match yields empty
/// details, never an error.
pub inspect_conversation: Arc<InspectConversation>,
/// Project registry — used by agent commands to resolve a `Project` from an id.
pub project_store: Arc<dyn ProjectStore>,
// --- Windows (L10) ---
@ -282,6 +290,14 @@ impl AppState {
Arc::clone(&fs_port),
Arc::clone(&events_port),
));
// Close-time snapshot of running agents (T5). Shares the SAME live-session
// registry as the terminal/agent use cases, so its liveness check reflects
// the very PTYs the shutdown hook is about to kill — it must run *before*.
let snapshot_running_agents = Arc::new(SnapshotRunningAgents::new(
Arc::clone(&store_port),
Arc::clone(&fs_port),
Arc::clone(&terminal_sessions) as Arc<dyn LiveAgentRegistry>,
));
// --- Profiles & AI runtime (L5) ---
// One generic, profile-driven runtime adapter (Open/Closed): it holds the
@ -347,6 +363,27 @@ impl AppState {
Arc::clone(&skill_store_port),
Arc::clone(&terminal_sessions),
Arc::clone(&events_port),
Arc::clone(&ids) as Arc<dyn IdGenerator>,
));
// --- Conversation inspection (T7) ---
// Best-effort, optional, extensible: a `Vec` of SessionInspectors routed
// by profile. Adding an inspectable CLI = pushing one more adapter here.
// The Claude inspector reads `<home>/.claude/projects/<cwd>/<id>.jsonl`;
// `$HOME` is resolved from the environment (empty string if unset — the
// inspector then simply finds nothing and yields empty details).
let home_dir = std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.unwrap_or_default();
let inspectors: Vec<Arc<dyn domain::ports::SessionInspector>> =
vec![Arc::new(ClaudeTranscriptInspector::new(
Arc::clone(&fs_port),
home_dir,
))];
let inspect_conversation = Arc::new(InspectConversation::new(
Arc::clone(&contexts_port),
Arc::clone(&profile_store_port),
inspectors,
));
let project_store = Arc::clone(&store_port);
@ -461,6 +498,7 @@ impl AppState {
rename_layout,
delete_layout,
set_active_layout,
snapshot_running_agents,
detect_profiles,
list_profiles,
save_profile,
@ -478,6 +516,7 @@ impl AppState {
update_agent_context,
delete_agent,
launch_agent,
inspect_conversation,
project_store,
create_template,
update_template,
@ -535,6 +574,20 @@ impl AppState {
watchers.insert(project.id, handle);
}
/// Returns the ids of every currently-open project.
///
/// Derived from the orchestrator watcher registry, which holds exactly one
/// entry per open project (started on open/create, dropped on close). Used by
/// the shutdown hook to snapshot running agents across all open projects
/// before the global PTY kill.
#[must_use]
pub fn open_project_ids(&self) -> Vec<ProjectId> {
self.orchestrator_watchers
.lock()
.map(|w| w.keys().copied().collect())
.unwrap_or_default()
}
/// Stops and removes the orchestrator watcher for `project_id`, if any.
/// Called from `close_project` so a closed project stops consuming requests.
pub fn stop_orchestrator_watch(&self, project_id: &ProjectId) {

View File

@ -223,6 +223,8 @@ fn layout_dto_serialises_camelcase_tagged_tree() {
id: nid(1),
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
});
let dto = LayoutDto::from(LoadLayoutOutput {
layout_id: domain::LayoutId::new_random(),
@ -322,8 +324,10 @@ fn layout_dto_round_trips_a_split_tree_shape() {
id: nid(1),
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
})
.split(nid(1), Direction::Column, LeafCell { id: nid(2), session: None, agent: None }, nid(9))
.split(nid(1), Direction::Column, LeafCell { id: nid(2), session: None, agent: None, conversation_id: None, agent_was_running: false }, nid(9))
.unwrap();
let dto = LayoutDto::from(LoadLayoutOutput {
layout_id: domain::LayoutId::new_random(),

View File

@ -3,10 +3,14 @@
//! and `From<LaunchAgentOutput>` for [`TerminalSessionDto`].
use app_tauri_lib::dto::{
parse_agent_id, AgentDto, AgentListDto, CreateAgentRequestDto, LaunchAgentRequestDto,
TerminalSessionDto, UpdateAgentContextRequestDto,
parse_agent_id, AgentDto, AgentListDto, ConversationDetailsDto, CreateAgentRequestDto,
InspectConversationRequestDto, LaunchAgentRequestDto, TerminalSessionDto,
UpdateAgentContextRequestDto,
};
use application::{CreateAgentOutput, LaunchAgentOutput, ListAgentsOutput};
use application::{
CreateAgentOutput, InspectConversationOutput, LaunchAgentOutput, ListAgentsOutput,
};
use domain::ports::ConversationDetails;
use domain::ids::{AgentId, NodeId, ProfileId, SessionId};
use domain::terminal::{PtySize, SessionKind, SessionStatus, TerminalSession};
use domain::{Agent, AgentOrigin, ProjectPath};
@ -126,6 +130,72 @@ fn launch_agent_request_deserialises_camelcase() {
assert_eq!(dto.cols, 80);
assert_eq!(dto.project_id, project_id);
assert_eq!(dto.agent_id, agent_id);
// Omitting the resume id defaults to None (a fresh cell).
assert_eq!(dto.conversation_id, None);
}
#[test]
fn launch_agent_request_carries_conversation_id_for_resume() {
let raw = json!({
"projectId": Uuid::from_u128(1).to_string(),
"agentId": Uuid::from_u128(2).to_string(),
"rows": 24,
"cols": 80,
"conversationId": "resume-me"
});
let dto: LaunchAgentRequestDto = serde_json::from_value(raw).unwrap();
// The leaf's persisted id reaches the backend so the launch resumes (T4b).
assert_eq!(dto.conversation_id.as_deref(), Some("resume-me"));
}
// ---------------------------------------------------------------------------
// InspectConversation DTOs (T7)
// ---------------------------------------------------------------------------
#[test]
fn inspect_conversation_request_deserialises_camelcase() {
let raw = json!({
"projectId": Uuid::from_u128(1).to_string(),
"agentId": Uuid::from_u128(2).to_string(),
"conversationId": "conv-7"
});
let dto: InspectConversationRequestDto = serde_json::from_value(raw).unwrap();
assert_eq!(dto.project_id, Uuid::from_u128(1).to_string());
assert_eq!(dto.agent_id, Uuid::from_u128(2).to_string());
assert_eq!(dto.conversation_id, "conv-7");
}
#[test]
fn conversation_details_dto_serialises_camelcase_when_present() {
let out = InspectConversationOutput {
details: ConversationDetails {
last_topic: Some("refactor parser".to_owned()),
token_count: Some(1234),
},
};
let dto = ConversationDetailsDto::from(out);
let v = serde_json::to_value(&dto).unwrap();
assert_eq!(v["lastTopic"], "refactor parser");
assert_eq!(v["tokenCount"], 1234);
// No snake_case leak.
assert!(v.get("last_topic").is_none());
assert!(v.get("token_count").is_none());
}
#[test]
fn conversation_details_dto_omits_fields_when_none() {
let out = InspectConversationOutput {
details: ConversationDetails {
last_topic: None,
token_count: None,
},
};
let dto = ConversationDetailsDto::from(out);
let v = serde_json::to_value(&dto).unwrap();
// Both optional fields are omitted from the wire (absent, not null).
assert!(v.get("lastTopic").is_none(), "absent topic ⇒ field omitted");
assert!(v.get("tokenCount").is_none(), "absent tokens ⇒ field omitted");
assert_eq!(v, json!({}), "fully degraded ⇒ empty object");
}
// ---------------------------------------------------------------------------
@ -168,7 +238,10 @@ fn launch_agent_output_maps_to_terminal_session_dto() {
);
session.status = SessionStatus::Running;
let out = LaunchAgentOutput { session };
let out = LaunchAgentOutput {
session,
assigned_conversation_id: None,
};
let dto = TerminalSessionDto::from(out);
assert_eq!(dto.session_id, session_id.to_string());
@ -176,9 +249,51 @@ fn launch_agent_output_maps_to_terminal_session_dto() {
assert_eq!(dto.rows, 24);
assert_eq!(dto.cols, 80);
// Nothing assigned ⇒ no conversation id surfaced.
assert_eq!(dto.assigned_conversation_id, None);
// Also verify camelCase serialisation.
let v = serde_json::to_value(&dto).unwrap();
assert_eq!(v["sessionId"], session_id.to_string());
assert_eq!(v["cwd"], "/tmp/project");
assert!(v.get("session_id").is_none(), "no snake_case leak");
// Absent assignment must be omitted from the wire payload entirely.
assert!(
v.get("assignedConversationId").is_none(),
"no assignment ⇒ field omitted"
);
}
#[test]
fn launch_agent_output_propagates_assigned_conversation_id() {
let session_id = SessionId::from_uuid(Uuid::from_u128(70));
let node_id = NodeId::from_uuid(Uuid::from_u128(80));
let agent_id = AgentId::from_uuid(Uuid::from_u128(90));
let cwd = ProjectPath::new("/tmp/project".to_owned()).expect("valid path");
let size = PtySize::new(24, 80).unwrap();
let mut session = TerminalSession::starting(
session_id,
node_id,
cwd,
SessionKind::Agent { agent_id },
size,
);
session.status = SessionStatus::Running;
let out = LaunchAgentOutput {
session,
assigned_conversation_id: Some("conv-xyz".to_owned()),
};
let dto = TerminalSessionDto::from(out);
// The id minted by the launch is carried through to the DTO …
assert_eq!(dto.assigned_conversation_id.as_deref(), Some("conv-xyz"));
// … and serialised in camelCase for the front to persist on the leaf.
let v = serde_json::to_value(&dto).unwrap();
assert_eq!(v["assignedConversationId"], "conv-xyz");
assert!(
v.get("assigned_conversation_id").is_none(),
"no snake_case leak"
);
}

View File

@ -245,3 +245,66 @@ fn set_cell_agent_op_deserialises_with_absent_agent_defaults_to_none() {
_ => panic!("expected SetCellAgent with None agent"),
}
}
// ---------------------------------------------------------------------------
// setCellConversation operation deserialisation (T4b)
// ---------------------------------------------------------------------------
#[test]
fn set_cell_conversation_op_deserialises_with_id() {
let target = nid(1);
let raw = json!({
"type": "setCellConversation",
"target": target.to_string(),
"conversationId": "conv-42"
});
let dto: LayoutOperationDto = serde_json::from_value(raw).unwrap();
let op = dto.into_operation().unwrap();
match op {
application::LayoutOperation::SetCellConversation {
target: t,
conversation_id: Some(id),
} => {
assert_eq!(t, target);
assert_eq!(id, "conv-42");
}
_ => panic!("expected SetCellConversation with id"),
}
}
#[test]
fn set_cell_conversation_op_deserialises_with_null_id() {
let target = nid(2);
let raw = json!({
"type": "setCellConversation",
"target": target.to_string(),
"conversationId": null
});
let dto: LayoutOperationDto = serde_json::from_value(raw).unwrap();
let op = dto.into_operation().unwrap();
match op {
application::LayoutOperation::SetCellConversation {
target: t,
conversation_id: None,
} => assert_eq!(t, target),
_ => panic!("expected SetCellConversation with None id"),
}
}
#[test]
fn set_cell_conversation_op_deserialises_with_absent_id_defaults_to_none() {
let target = nid(3);
let raw = json!({
"type": "setCellConversation",
"target": target.to_string()
});
let dto: LayoutOperationDto = serde_json::from_value(raw).unwrap();
let op = dto.into_operation().unwrap();
match op {
application::LayoutOperation::SetCellConversation {
conversation_id: None,
..
} => {}
_ => panic!("expected SetCellConversation with None id"),
}
}

View File

@ -24,6 +24,7 @@ fn profile(id: u128, name: &str, command: &str) -> AgentProfile {
ContextInjection::convention_file("CLAUDE.md").unwrap(),
Some(format!("{command} --version")),
"{projectRoot}",
None,
)
.unwrap()
}

View File

@ -54,6 +54,7 @@ pub fn reference_profiles() -> Vec<AgentProfile> {
.expect("CLAUDE.md is a valid convention target"),
Some("claude --version".to_owned()),
"{agentRunDir}",
None,
)
.expect("claude reference profile is valid"),
AgentProfile::new(
@ -65,6 +66,7 @@ pub fn reference_profiles() -> Vec<AgentProfile> {
.expect("AGENTS.md is a valid convention target"),
Some("codex --version".to_owned()),
"{agentRunDir}",
None,
)
.expect("codex reference profile is valid"),
AgentProfile::new(
@ -76,6 +78,7 @@ pub fn reference_profiles() -> Vec<AgentProfile> {
.expect("GEMINI.md is a valid convention target"),
Some("gemini --version".to_owned()),
"{agentRunDir}",
None,
)
.expect("gemini reference profile is valid"),
AgentProfile::new(
@ -87,6 +90,7 @@ pub fn reference_profiles() -> Vec<AgentProfile> {
.expect("aider flag template is non-empty"),
Some("aider --version".to_owned()),
"{agentRunDir}",
None,
)
.expect("aider reference profile is valid"),
]

View File

@ -0,0 +1,142 @@
//! Best-effort conversation inspection use case (CONTEXT §T7, Part A).
//!
//! [`InspectConversation`] enriches a resume popup with the *last topic* and a
//! *token indicator* read from a CLI's on-disk transcript. It is **best-effort
//! by construction**: it routes the agent's [`AgentProfile`] to the first
//! injected [`SessionInspector`] that [`supports`](SessionInspector::supports)
//! it, and *any* miss — no inspector at all, an unsupported profile,
//! [`InspectError::NotFound`], or [`InspectError::Read`] — degrades to **empty
//! details** (`last_topic: None, token_count: None`) instead of an error. The
//! resume must never be blocked by inspection.
//!
//! Extensibility (Open/Closed): adding a new inspectable CLI is *pushing one
//! more adapter into the `Vec`* at the composition root — no change here.
//!
//! Like [`super::lifecycle::LaunchAgent`], it resolves the agent from the
//! project manifest and its profile from the [`ProfileStore`], and inspects
//! against the agent's **isolated run directory** (the very `cwd` the CLI was
//! launched with) so the inspector points at the right transcript folder.
use std::sync::Arc;
use domain::ports::{
AgentContextStore, ConversationDetails, InspectError, ProfileStore, SessionInspector,
};
use domain::{AgentId, Project};
use super::lifecycle::agent_run_dir;
use crate::error::AppError;
/// Input for [`InspectConversation::execute`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InspectConversationInput {
/// The project owning the agent.
pub project: Project,
/// The agent whose conversation is being inspected.
pub agent_id: AgentId,
/// The persistent CLI conversation id recorded on the hosting cell.
pub conversation_id: String,
}
/// Output of [`InspectConversation::execute`]: the (possibly empty) best-effort
/// details. Never an inspection error — a miss surfaces as empty fields.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InspectConversationOutput {
/// Enriched, best-effort details (every field optional).
pub details: ConversationDetails,
}
/// Reads best-effort [`ConversationDetails`] for an agent's conversation.
///
/// Holds a (possibly empty) `Vec<Arc<dyn SessionInspector>>`: the agent's
/// profile is routed to the first inspector that supports it. The use case still
/// needs the context store (resolve the agent) and the profile store (resolve
/// the profile), exactly like [`super::lifecycle::LaunchAgent`].
pub struct InspectConversation {
contexts: Arc<dyn AgentContextStore>,
profiles: Arc<dyn ProfileStore>,
inspectors: Vec<Arc<dyn SessionInspector>>,
}
impl InspectConversation {
/// Builds the use case from its injected ports and the inspector list (which
/// may be empty: that path simply yields empty details).
#[must_use]
pub fn new(
contexts: Arc<dyn AgentContextStore>,
profiles: Arc<dyn ProfileStore>,
inspectors: Vec<Arc<dyn SessionInspector>>,
) -> Self {
Self {
contexts,
profiles,
inspectors,
}
}
/// Resolves the agent + profile + run dir, then asks the first supporting
/// inspector for details. Returns **empty** details when no inspector
/// matches or inspection misses (`NotFound`/`Read`); only genuine store
/// failures (loading the manifest / profiles) surface as an error.
///
/// # Errors
/// - [`AppError::NotFound`] if the agent or its profile is unknown,
/// - [`AppError::Invalid`] if a persisted manifest entry / run dir is invalid,
/// - [`AppError::Store`] on a manifest / profile store failure.
pub async fn execute(
&self,
input: InspectConversationInput,
) -> Result<InspectConversationOutput, AppError> {
// Resolve the agent from the manifest (for its profile + run dir).
let manifest = self.contexts.load_manifest(&input.project).await?;
let entry = manifest
.entries
.iter()
.find(|e| e.agent_id == input.agent_id)
.ok_or_else(|| AppError::NotFound(format!("agent {}", input.agent_id)))?;
let agent = entry
.to_agent()
.map_err(|e| AppError::Invalid(e.to_string()))?;
let profile = self
.profiles
.list()
.await?
.into_iter()
.find(|p| p.id == agent.profile_id)
.ok_or_else(|| AppError::NotFound(format!("profile {} for agent", agent.profile_id)))?;
// The CLI runs with its isolated run dir as cwd; the inspector keys its
// transcript lookup off that same cwd (same value LaunchAgent uses).
let run_dir = agent_run_dir(&input.project.root, &agent.id)
.map_err(|e| AppError::Invalid(e.to_string()))?;
// Route to the first inspector that supports this profile. No match ⇒
// empty details (best-effort).
let Some(inspector) = self.inspectors.iter().find(|i| i.supports(&profile)) else {
return Ok(InspectConversationOutput {
details: empty_details(),
});
};
// Any inspection miss (NotFound / Read) degrades to empty details — it
// must never block a resume.
let details = match inspector
.details(&profile, &input.conversation_id, &run_dir)
.await
{
Ok(details) => details,
Err(InspectError::NotFound | InspectError::Read(_)) => empty_details(),
};
Ok(InspectConversationOutput { details })
}
}
/// The empty, fully-degraded [`ConversationDetails`] (no topic, no tokens).
fn empty_details() -> ConversationDetails {
ConversationDetails {
last_topic: None,
token_count: None,
}
}

View File

@ -14,12 +14,14 @@
use std::sync::Arc;
use domain::ports::{
AgentContextStore, AgentRuntime, ContextInjectionPlan, EventBus, FileSystem, PreparedContext,
ProfileStore, PtyPort, RemotePath, SkillStore, SpawnSpec, StoreError,
AgentContextStore, AgentRuntime, ContextInjectionPlan, EventBus, FileSystem, IdGenerator,
PreparedContext, ProfileStore, PtyPort, RemotePath, SessionPlan, SkillStore, SpawnSpec,
StoreError,
};
use domain::{
Agent, AgentId, AgentManifest, AgentOrigin, DomainEvent, ManifestEntry, MarkdownDoc, NodeId,
Project, ProfileId, ProjectPath, PtySize, SessionKind, SessionStatus, Skill, TerminalSession,
Agent, AgentId, AgentManifest, AgentOrigin, AgentProfile, ContextInjection, DomainEvent,
ManifestEntry, MarkdownDoc, NodeId, Project, ProfileId, ProjectPath, PtySize, SessionKind,
SessionStatus, Skill, TerminalSession,
};
use crate::error::AppError;
@ -327,6 +329,12 @@ pub struct LaunchAgentInput {
pub cols: u16,
/// The layout leaf hosting the session (a fresh node when `None`).
pub node_id: Option<NodeId>,
/// The persistent CLI conversation id currently recorded on the hosting cell,
/// if any. `Some` means a previous conversation exists and the launch should
/// **resume** it; `None` means a fresh cell (the launch may *assign* a new id
/// when the profile supports it). The caller (which owns the layout) reads this
/// from the leaf's [`domain::layout::LeafCell::conversation_id`].
pub conversation_id: Option<String>,
}
/// Output of [`LaunchAgent::execute`].
@ -334,6 +342,13 @@ pub struct LaunchAgentInput {
pub struct LaunchAgentOutput {
/// The created agent terminal session.
pub session: TerminalSession,
/// The conversation id **assigned** by this launch, when the profile supports
/// session assignment and the cell had none yet. The caller persists it on the
/// hosting leaf (via the layout flow, e.g. `set_cell_conversation`) so the next
/// open resumes instead of re-assigning. `None` when nothing new was assigned
/// (resume of an existing id, degraded mode, or a profile without a session
/// block) — the caller has nothing to persist.
pub assigned_conversation_id: Option<String>,
}
/// Launches an agent: resolve profile + context, prepare the invocation, apply
@ -353,6 +368,7 @@ pub struct LaunchAgent {
skills: Arc<dyn SkillStore>,
sessions: Arc<TerminalSessions>,
events: Arc<dyn EventBus>,
ids: Arc<dyn IdGenerator>,
}
impl LaunchAgent {
@ -368,6 +384,7 @@ impl LaunchAgent {
skills: Arc<dyn SkillStore>,
sessions: Arc<TerminalSessions>,
events: Arc<dyn EventBus>,
ids: Arc<dyn IdGenerator>,
) -> Self {
Self {
contexts,
@ -378,6 +395,7 @@ impl LaunchAgent {
skills,
sessions,
events,
ids,
}
}
@ -459,6 +477,16 @@ impl LaunchAgent {
.create_dir_all(&RemotePath::new(run_dir.as_str().to_owned()))
.await?;
// 3b. Seed the CLI's permission config in the run dir so the agent runs
// with the project's full autonomy and never blocks on per-command
// permission prompts. The agent's cwd is the run dir, so the CLI
// writes/reads its permission file there; without a seed, the CLI
// accumulates narrow per-command approvals and keeps prompting.
// Pragmatic per-CLI seed pending the universal `.ideai/permissions.json`
// + OS-sandbox model. Non-clobbering and best-effort.
self.seed_cli_permissions(&profile, &run_dir, &input.project.root)
.await?;
// 4. Prepare the invocation (pure): command + args + injection plan + cwd.
// The run dir is passed as the cwd base; the profile's `{agentRunDir}`
// placeholder resolves against it.
@ -466,9 +494,15 @@ impl LaunchAgent {
content: content.clone(),
relative_path: agent.context_path.clone(),
};
let mut spec = self
.runtime
.prepare_invocation(&profile, &prepared, &run_dir)?;
// 4a. Resolve the session intention (T4). The conversation id is a property
// of the *cell*, not the PTY: the caller (which owns the layout) passes
// the cell's current `conversation_id`. Any id this launch *assigns* is
// returned in the output so the caller persists it on the leaf.
let (session_plan, assigned_conversation_id) =
self.resolve_session_plan(&profile, input.conversation_id.clone());
let mut spec =
self.runtime
.prepare_invocation(&profile, &prepared, &run_dir, &session_plan)?;
// 5. Resolve the agent's assigned skills (their `.md` bodies), then apply
// the injection plan side effects *before* spawning.
@ -503,7 +537,102 @@ impl LaunchAgent {
session_id,
});
Ok(LaunchAgentOutput { session })
Ok(LaunchAgentOutput {
session,
assigned_conversation_id,
})
}
/// Resolves the [`SessionPlan`] for a launch from the profile's session
/// strategy and the cell's current `conversation_id` (T4).
///
/// Returns the plan *and* — when this launch mints a fresh id — that id, so the
/// caller can persist it on the hosting leaf. The id is only generated for an
/// `Assign` (profile has a `session` block with an `assign_flag`, and the cell
/// had no id yet); every other branch returns `None` (nothing to persist).
///
/// Branches:
/// - cell already has an id ⇒ [`SessionPlan::Resume`] (reopen) — no new id;
/// - no id, profile has `session.assign_flag` ⇒ mint a UUID, [`SessionPlan::Assign`];
/// - no id, profile has `session` but no `assign_flag` (degraded) ⇒
/// [`SessionPlan::None`] (nothing to resume on a first launch; the adapter
/// uses the bare resume flag only on later reopens);
/// - profile without a `session` block ⇒ [`SessionPlan::None`] (legacy).
fn resolve_session_plan(
&self,
profile: &AgentProfile,
cell_conversation_id: Option<String>,
) -> (SessionPlan, Option<String>) {
// No session strategy at all: behave exactly as before.
let Some(session) = &profile.session else {
return (SessionPlan::None, None);
};
// The cell already carries a conversation: resume it (no new id minted).
if let Some(conversation_id) = cell_conversation_id {
return (SessionPlan::Resume { conversation_id }, None);
}
// Fresh cell. Only mint+assign an id when the profile can assign one;
// otherwise (degraded mode) the first launch has nothing to resume.
if session.assign_flag.is_some() {
let conversation_id = self.ids.new_uuid().to_string();
(
SessionPlan::Assign {
conversation_id: conversation_id.clone(),
},
Some(conversation_id),
)
} else {
(SessionPlan::None, None)
}
}
/// Seeds the agent's run dir with the CLI permission config matching its
/// context-injection convention, so the agent inherits the project's autonomy
/// instead of prompting per command.
///
/// Conditioned on the CLI convention (only Claude Code — convention file
/// `CLAUDE.md` — has a known seed today); a no-op for any other CLI.
/// Best-effort and **non-clobbering**: an existing file (possibly user-edited)
/// is left untouched.
///
/// # Errors
/// [`AppError::FileSystem`] if the directory/file cannot be written.
async fn seed_cli_permissions(
&self,
profile: &AgentProfile,
run_dir: &ProjectPath,
project_root: &ProjectPath,
) -> Result<(), AppError> {
let is_claude = matches!(
&profile.context_injection,
ContextInjection::ConventionFile { target }
if target
.rsplit(['/', '\\'])
.next()
.unwrap_or(target)
.eq_ignore_ascii_case("CLAUDE.md")
);
if !is_claude {
return Ok(());
}
let settings_path =
RemotePath::new(format!("{}/.claude/settings.local.json", run_dir.as_str()));
if self.fs.exists(&settings_path).await? {
return Ok(());
}
self.fs
.create_dir_all(&RemotePath::new(format!("{}/.claude", run_dir.as_str())))
.await?;
self.fs
.write(
&settings_path,
claude_settings_seed(project_root.as_str()).as_bytes(),
)
.await?;
Ok(())
}
/// Applies the context-injection plan that must happen *before* spawn:
@ -561,10 +690,76 @@ fn join(base: &ProjectPath, rel: &str) -> String {
/// # Errors
/// Propagates [`DomainError`](domain::error::DomainError) if the joined path is
/// not a valid [`ProjectPath`] (should not happen for an absolute project root).
fn agent_run_dir(root: &ProjectPath, agent_id: &AgentId) -> Result<ProjectPath, domain::error::DomainError> {
pub(crate) fn agent_run_dir(root: &ProjectPath, agent_id: &AgentId) -> Result<ProjectPath, domain::error::DomainError> {
ProjectPath::new(join(root, &format!(".ideai/run/{agent_id}")))
}
/// Builds the Claude Code permission seed (`.claude/settings.local.json`) written
/// into an agent's run dir: full project autonomy (`bypassPermissions` + broad
/// Read/Edit/Write/Bash) with the project root granted as an additional working
/// directory (the cwd is the run dir, the agent works on the root above it), while
/// keeping destructive/out-of-project commands denied. `project_root` is embedded
/// verbatim; it is JSON-escaped to stay valid for unusual paths.
///
/// Pure (no I/O), so it is unit-testable in isolation.
#[must_use]
fn claude_settings_seed(project_root: &str) -> String {
let root = json_escape(project_root);
format!(
r#"{{
"permissions": {{
"defaultMode": "bypassPermissions",
"additionalDirectories": [
"{root}"
],
"allow": [
"Read",
"Edit",
"Write",
"Bash"
],
"deny": [
"Bash(sudo *)",
"Bash(rm -rf /)",
"Bash(rm -rf /*)",
"Bash(rm -rf ~)",
"Bash(rm -rf ~/)",
"Bash(rm -rf ~/*)",
"Bash(rm -rf $HOME*)",
"Bash(mkfs*)",
"Bash(dd if=*)",
"Bash(shutdown*)",
"Bash(reboot*)"
]
}},
"skipDangerousModePermissionPrompt": true,
"sandbox": {{
"enabled": false
}}
}}
"#
)
}
/// Minimal JSON string escaper for embedding a filesystem path in the settings
/// seed (handles the characters that actually occur in paths: backslash, quote,
/// and control chars).
fn json_escape(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
c => out.push(c),
}
}
out
}
/// Composes the convention file IdeA writes into an agent's run directory: an
/// absolute project-root header (the agent's cwd is the run dir, *not* the root,
/// so it must be told where to work), the agent's persona `.md`, then the bodies
@ -700,4 +895,38 @@ mod tests {
assert!(doc.contains("## refactor"));
assert!(doc.contains("## review"));
}
#[test]
fn claude_settings_seed_grants_autonomy_and_keeps_guardrails() {
let json = claude_settings_seed("/home/me/proj");
// Full autonomy.
assert!(json.contains("\"defaultMode\": \"bypassPermissions\""));
assert!(json.contains("\"Bash\""));
// Project root granted as an additional working directory.
assert!(json.contains("\"/home/me/proj\""));
// Destructive guardrails preserved.
assert!(json.contains("Bash(sudo *)"));
assert!(json.contains("Bash(rm -rf /)"));
assert!(json.contains("Bash(mkfs*)"));
// Valid JSON.
let parsed: serde_json::Value = serde_json::from_str(&json).expect("seed is valid JSON");
assert_eq!(parsed["permissions"]["defaultMode"], "bypassPermissions");
assert_eq!(
parsed["permissions"]["additionalDirectories"][0],
"/home/me/proj"
);
}
#[test]
fn claude_settings_seed_escapes_paths_for_valid_json() {
// A path with a backslash and a quote must not break the JSON.
let json = claude_settings_seed(r#"/weird\path"x"#);
let parsed: serde_json::Value =
serde_json::from_str(&json).expect("seed with odd path is valid JSON");
assert_eq!(
parsed["permissions"]["additionalDirectories"][0],
r#"/weird\path"x"#
);
}
}

View File

@ -7,12 +7,16 @@
//! ports. Launching an agent (PTY + injection) is L6.
mod catalogue;
mod inspect;
mod lifecycle;
mod usecases;
pub(crate) use lifecycle::unique_md_path;
pub use catalogue::{reference_profile_id, reference_profiles};
pub use inspect::{
InspectConversation, InspectConversationInput, InspectConversationOutput,
};
pub use lifecycle::{
CreateAgentFromScratch, CreateAgentInput, CreateAgentOutput, DeleteAgent, DeleteAgentInput,
LaunchAgent, LaunchAgentInput, LaunchAgentOutput, ListAgents, ListAgentsInput, ListAgentsOutput,

View File

@ -21,9 +21,13 @@
//! `TerminalSessions` registry, keyed by that same `SessionId`.
mod management;
mod snapshot;
mod store;
mod usecases;
pub use snapshot::{
SnapshotRunningAgents, SnapshotRunningAgentsInput, SnapshotRunningAgentsOutput,
};
pub use management::{
CreateLayout, CreateLayoutInput, CreateLayoutOutput, DeleteLayout, DeleteLayoutInput,
DeleteLayoutOutput, LayoutInfo, ListLayouts, ListLayoutsInput, ListLayoutsOutput, RenameLayout,

View File

@ -0,0 +1,113 @@
//! [`SnapshotRunningAgents`] — freeze, at close time, which agent cells still
//! held a live PTY (feature: "conversation resume", task T5).
//!
//! When the IDE (or a single project) closes, every live PTY is about to be
//! killed. *Before* that happens, we record on each agent-bearing leaf whether
//! its agent process was still running (`agent_was_running = true`) or had
//! already exited / never launched (`false`). On reopen, that flag is what tells
//! the resume popup "this conversation was still in progress" vs "it was closed".
//!
//! The decision is **universal**: it is derived purely from the process
//! lifecycle (the live-session registry), never from parsing CLI output. The use
//! case itself is a thin orchestrator over:
//! - the persisted layouts store ([`super::store`]),
//! - the pure domain operation [`domain::LayoutTree::set_agent_running`],
//! - a [`LiveAgentRegistry`] liveness query.
//!
//! **Ordering contract:** this snapshot reads the registry *as it is at call
//! time*. The composition root (app-tauri) is responsible for calling it
//! **before** the global PTY kill; if the kill ran first, every agent would look
//! "closed". See `app-tauri`'s `CloseRequested` hook and `close.rs` callers.
use std::sync::Arc;
use domain::ports::{FileSystem, ProjectStore};
use domain::ProjectId;
use crate::error::AppError;
use crate::terminal::LiveAgentRegistry;
use super::store::{persist_doc, resolve_doc};
/// Input for [`SnapshotRunningAgents::execute`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SnapshotRunningAgentsInput {
/// The project whose layouts must be frozen.
pub project_id: ProjectId,
}
/// Output of [`SnapshotRunningAgents::execute`].
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SnapshotRunningAgentsOutput {
/// Number of agent-bearing leaves that were found running at snapshot time.
pub running: usize,
/// Number of agent-bearing leaves that were found stopped at snapshot time.
pub stopped: usize,
}
/// Freezes the `agent_was_running` flag on every agent leaf of a project's
/// layouts, from the live-session registry, then persists the layouts.
pub struct SnapshotRunningAgents {
store: Arc<dyn ProjectStore>,
fs: Arc<dyn FileSystem>,
live: Arc<dyn LiveAgentRegistry>,
}
impl SnapshotRunningAgents {
/// Builds the use case from its injected ports.
#[must_use]
pub fn new(
store: Arc<dyn ProjectStore>,
fs: Arc<dyn FileSystem>,
live: Arc<dyn LiveAgentRegistry>,
) -> Self {
Self { store, fs, live }
}
/// Executes the snapshot for one project.
///
/// Walks every named layout of the project; for each agent-bearing leaf it
/// applies [`domain::LayoutTree::set_agent_running`] with the agent's *current*
/// liveness, then persists the whole layouts store. A project with no
/// agent leaf is a no-op (the doc is still resolved/healed, never written if
/// unchanged).
///
/// # Errors
/// - [`AppError::NotFound`] if the project is unknown,
/// - [`AppError::FileSystem`] on persistence failure,
/// - [`AppError::Store`] on registry I/O failure.
pub async fn execute(
&self,
input: SnapshotRunningAgentsInput,
) -> Result<SnapshotRunningAgentsOutput, AppError> {
let project = self.store.load_project(input.project_id).await?;
let mut doc = resolve_doc(self.fs.as_ref(), &project).await?;
let mut out = SnapshotRunningAgentsOutput::default();
let mut changed = false;
for named in &mut doc.layouts {
for (leaf_id, agent_id) in named.tree.agent_leaves() {
let running = self.live.is_agent_live(&agent_id);
if running {
out.running += 1;
} else {
out.stopped += 1;
}
// Pure op: only NodeNotFound is possible, which cannot happen
// since `leaf_id` came from this very tree.
named.tree = named
.tree
.set_agent_running(leaf_id, running)
.map_err(|e| AppError::Invalid(e.to_string()))?;
changed = true;
}
}
if changed {
persist_doc(self.fs.as_ref(), &project, &doc).await?;
}
Ok(out)
}
}

View File

@ -103,6 +103,8 @@ pub fn default_tree() -> LayoutTree {
id: NodeId::new_random(),
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
})
}

View File

@ -76,6 +76,17 @@ pub enum LayoutOperation {
/// Agent to associate, or `None` to clear.
agent: Option<AgentId>,
},
/// Record (or clear) the persistent CLI conversation id on a leaf (T4b).
///
/// Persisting the id minted at first launch is what makes session resume
/// effective: on the next open, the leaf carries the id and the launch
/// resumes the conversation instead of assigning a new one.
SetCellConversation {
/// The hosting leaf.
target: NodeId,
/// Conversation id to record, or `None` to clear.
conversation_id: Option<String>,
},
}
impl LayoutOperation {
@ -94,6 +105,8 @@ impl LayoutOperation {
id: *new_leaf,
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
},
*container,
),
@ -105,6 +118,10 @@ impl LayoutOperation {
Self::Move { from, to } => tree.move_session(*from, *to),
Self::SetSession { target, session } => tree.set_session(*target, *session),
Self::SetCellAgent { target, agent } => tree.set_cell_agent(*target, *agent),
Self::SetCellConversation {
target,
conversation_id,
} => tree.set_cell_conversation(*target, conversation_id.clone()),
};
result.map_err(map_layout_err)
}

View File

@ -28,7 +28,8 @@ pub use agent::{
reference_profile_id, reference_profiles, ConfigureProfiles, ConfigureProfilesInput,
ConfigureProfilesOutput, CreateAgentFromScratch, CreateAgentInput, CreateAgentOutput,
DeleteAgent, DeleteAgentInput, DeleteProfile, DeleteProfileInput, DetectProfiles,
DetectProfilesInput, DetectProfilesOutput, FirstRunState, FirstRunStateOutput, LaunchAgent,
DetectProfilesInput, DetectProfilesOutput, FirstRunState, FirstRunStateOutput,
InspectConversation, InspectConversationInput, InspectConversationOutput, LaunchAgent,
LaunchAgentInput, LaunchAgentOutput, ListAgents, ListAgentsInput, ListAgentsOutput,
ListProfiles, ListProfilesOutput, ProfileAvailability, ReadAgentContext, ReadAgentContextInput,
ReadAgentContextOutput, ReferenceProfiles, ReferenceProfilesOutput, SaveProfile,
@ -49,7 +50,8 @@ pub use layout::{
DeleteLayoutOutput, LayoutInfo, LayoutKind, LayoutOperation, LayoutsDoc, ListLayouts,
ListLayoutsInput, ListLayoutsOutput, LoadLayout, LoadLayoutInput, LoadLayoutOutput,
MutateLayout, MutateLayoutInput, MutateLayoutOutput, NamedLayout, RenameLayout,
RenameLayoutInput, SetActiveLayout, SetActiveLayoutInput, LAYOUTS_FILE,
RenameLayoutInput, SetActiveLayout, SetActiveLayoutInput, SnapshotRunningAgents,
SnapshotRunningAgentsInput, SnapshotRunningAgentsOutput, LAYOUTS_FILE,
};
pub use project::{
CloseProject, CloseProjectInput, CloseProjectOutput, CloseTab, CloseTabInput, CreateProject,
@ -71,6 +73,7 @@ pub use skill::{
UpdateSkillOutput,
};
pub use terminal::{
LiveAgentRegistry,
CloseTerminal, CloseTerminalInput, CloseTerminalOutput, OpenTerminal, OpenTerminalInput,
OpenTerminalOutput, ResizeTerminal, ResizeTerminalInput, TerminalSessions, WriteToTerminal,
WriteToTerminalInput,

View File

@ -144,6 +144,7 @@ impl OrchestratorService {
rows: DEFAULT_ROWS,
cols: DEFAULT_COLS,
node_id: None,
conversation_id: None,
})
.await?;
@ -294,6 +295,7 @@ mod tests {
ContextInjection::convention_file("CLAUDE.md").unwrap(),
None,
"{agentRunDir}",
None,
)
.unwrap()
}

View File

@ -28,7 +28,7 @@
mod registry;
mod usecases;
pub use registry::TerminalSessions;
pub use registry::{LiveAgentRegistry, TerminalSessions};
pub use usecases::{
CloseTerminal, CloseTerminalInput, CloseTerminalOutput, OpenTerminal, OpenTerminalInput,
OpenTerminalOutput, ResizeTerminal, ResizeTerminalInput, WriteToTerminal, WriteToTerminalInput,

View File

@ -19,12 +19,30 @@ struct Entry {
session: TerminalSession,
}
/// Read-only liveness query over the agents that currently own a live PTY.
///
/// Abstracted as a trait so use cases that only need to ask "is this agent
/// running right now?" (e.g. the close-time snapshot of running agents) depend
/// on the *capability*, not on the whole [`TerminalSessions`] registry — and can
/// be tested against a trivial fake. [`TerminalSessions`] is the production
/// implementation.
pub trait LiveAgentRegistry: Send + Sync {
/// Whether `agent_id` currently has a live session in the registry.
fn is_agent_live(&self, agent_id: &AgentId) -> bool;
}
/// In-memory registry of active terminal sessions.
#[derive(Default)]
pub struct TerminalSessions {
entries: Mutex<HashMap<SessionId, Entry>>,
}
impl LiveAgentRegistry for TerminalSessions {
fn is_agent_live(&self, agent_id: &AgentId) -> bool {
self.session_for_agent(agent_id).is_some()
}
}
impl TerminalSessions {
/// Creates an empty registry.
#[must_use]

View File

@ -0,0 +1,359 @@
//! T7 tests for the [`InspectConversation`] use case (best-effort conversation
//! inspection feeding the resume popup).
//!
//! Each port is faked in-memory:
//! - [`FakeContexts`] — an [`AgentContextStore`] holding a manifest seeded with
//! one agent,
//! - [`FakeProfiles`] — a [`ProfileStore`] returning a fixed profile list,
//! - [`FakeInspector`] — a [`SessionInspector`] whose `supports`/`details` are
//! configurable, so we can exercise: a supporting inspector returning details,
//! one returning `NotFound`, and the empty-`Vec` (no inspector) path.
//!
//! The contract under test (CONTEXT §T7 Part A): inspection is **best-effort** —
//! any miss (no inspector, unsupported profile, `NotFound`/`Read`) degrades to
//! empty details, never an error; only a genuine store failure errors.
use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use domain::agent::{Agent, AgentManifest, AgentOrigin, ManifestEntry};
use domain::ids::{AgentId, ProfileId, ProjectId};
use domain::markdown::MarkdownDoc;
use domain::ports::{
AgentContextStore, ConversationDetails, InspectError, ProfileStore, SessionInspector,
StoreError,
};
use domain::profile::{AgentProfile, ContextInjection};
use domain::project::{Project, ProjectPath};
use domain::remote::RemoteRef;
use uuid::Uuid;
use application::{InspectConversation, InspectConversationInput};
// ---------------------------------------------------------------------------
// FakeContexts (AgentContextStore) — only load_manifest is exercised here
// ---------------------------------------------------------------------------
#[derive(Clone)]
struct FakeContexts(Arc<Mutex<AgentManifest>>);
impl FakeContexts {
fn with_agent(agent: &Agent) -> Self {
Self(Arc::new(Mutex::new(AgentManifest {
version: 1,
entries: vec![ManifestEntry::from_agent(agent)],
})))
}
fn empty() -> Self {
Self(Arc::new(Mutex::new(AgentManifest {
version: 1,
entries: Vec::new(),
})))
}
}
#[async_trait]
impl AgentContextStore for FakeContexts {
async fn read_context(
&self,
_project: &Project,
_agent: &AgentId,
) -> Result<MarkdownDoc, StoreError> {
Ok(MarkdownDoc::new(""))
}
async fn write_context(
&self,
_project: &Project,
_agent: &AgentId,
_md: &MarkdownDoc,
) -> Result<(), StoreError> {
Ok(())
}
async fn load_manifest(&self, _project: &Project) -> Result<AgentManifest, StoreError> {
Ok(self.0.lock().unwrap().clone())
}
async fn save_manifest(
&self,
_project: &Project,
manifest: &AgentManifest,
) -> Result<(), StoreError> {
*self.0.lock().unwrap() = manifest.clone();
Ok(())
}
}
// ---------------------------------------------------------------------------
// FakeProfiles (ProfileStore)
// ---------------------------------------------------------------------------
#[derive(Clone)]
struct FakeProfiles(Arc<Vec<AgentProfile>>);
#[async_trait]
impl ProfileStore for FakeProfiles {
async fn list(&self) -> Result<Vec<AgentProfile>, StoreError> {
Ok((*self.0).clone())
}
async fn save(&self, _profile: &AgentProfile) -> Result<(), StoreError> {
Ok(())
}
async fn delete(&self, _id: ProfileId) -> Result<(), StoreError> {
Ok(())
}
async fn is_configured(&self) -> Result<bool, StoreError> {
Ok(true)
}
async fn mark_configured(&self) -> Result<(), StoreError> {
Ok(())
}
}
// ---------------------------------------------------------------------------
// FakeInspector (SessionInspector) — configurable support + result
// ---------------------------------------------------------------------------
/// What the fake inspector returns from `details`.
#[derive(Clone)]
enum InspectResult {
Ok(ConversationDetails),
Err(InspectError),
}
/// Shared probe recording the `(conversation_id, cwd)` an inspection was asked
/// for (so a test can assert the run dir was routed, not the project root).
type SeenProbe = Arc<Mutex<Option<(String, String)>>>;
struct FakeInspector {
supports: bool,
result: InspectResult,
seen: SeenProbe,
}
impl FakeInspector {
fn new(supports: bool, result: InspectResult) -> (Arc<Self>, SeenProbe) {
let seen = Arc::new(Mutex::new(None));
let me = Arc::new(Self {
supports,
result,
seen: Arc::clone(&seen),
});
(me, seen)
}
}
#[async_trait]
impl SessionInspector for FakeInspector {
fn supports(&self, _profile: &AgentProfile) -> bool {
self.supports
}
async fn details(
&self,
_profile: &AgentProfile,
conversation_id: &str,
cwd: &ProjectPath,
) -> Result<ConversationDetails, InspectError> {
*self.seen.lock().unwrap() = Some((conversation_id.to_owned(), cwd.as_str().to_owned()));
match &self.result {
InspectResult::Ok(d) => Ok(d.clone()),
InspectResult::Err(e) => Err(e.clone()),
}
}
}
// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------
fn project() -> Project {
Project::new(
ProjectId::from_uuid(Uuid::from_u128(1000)),
"demo",
ProjectPath::new("/home/me/proj").unwrap(),
RemoteRef::local(),
1_700_000_000_000,
)
.unwrap()
}
fn profile(id: ProfileId) -> AgentProfile {
AgentProfile::new(
id,
"Claude Code",
"claude",
Vec::new(),
ContextInjection::ConventionFile {
target: "CLAUDE.md".to_owned(),
},
Some("claude --version".to_owned()),
"{agentRunDir}",
None,
)
.unwrap()
}
fn agent(id: AgentId, profile_id: ProfileId) -> Agent {
Agent::new(id, "Bob", "agents/bob.md", profile_id, AgentOrigin::Scratch, false).unwrap()
}
fn input(agent_id: AgentId) -> InspectConversationInput {
InspectConversationInput {
project: project(),
agent_id,
conversation_id: "conv-123".to_owned(),
}
}
// ---------------------------------------------------------------------------
// (a) supporting inspector → details propagated
// ---------------------------------------------------------------------------
#[tokio::test]
async fn supporting_inspector_propagates_details() {
let pid = ProfileId::from_uuid(Uuid::from_u128(7));
let aid = AgentId::from_uuid(Uuid::from_u128(42));
let a = agent(aid, pid);
let (inspector, seen) = FakeInspector::new(
true,
InspectResult::Ok(ConversationDetails {
last_topic: Some("refactor the parser".to_owned()),
token_count: Some(4242),
}),
);
let uc = InspectConversation::new(
Arc::new(FakeContexts::with_agent(&a)),
Arc::new(FakeProfiles(Arc::new(vec![profile(pid)]))),
vec![inspector],
);
let out = uc.execute(input(aid)).await.unwrap();
assert_eq!(out.details.last_topic.as_deref(), Some("refactor the parser"));
assert_eq!(out.details.token_count, Some(4242));
// Routed the conversation id and the agent's isolated run dir (not the root).
let (conv, cwd) = seen.lock().unwrap().clone().expect("inspector was called");
assert_eq!(conv, "conv-123");
assert_eq!(cwd, format!("/home/me/proj/.ideai/run/{aid}"));
}
// ---------------------------------------------------------------------------
// (b) inspector returns NotFound → empty details, no error
// ---------------------------------------------------------------------------
#[tokio::test]
async fn not_found_degrades_to_empty_details() {
let pid = ProfileId::from_uuid(Uuid::from_u128(7));
let aid = AgentId::from_uuid(Uuid::from_u128(42));
let a = agent(aid, pid);
let (inspector, _seen) = FakeInspector::new(true, InspectResult::Err(InspectError::NotFound));
let uc = InspectConversation::new(
Arc::new(FakeContexts::with_agent(&a)),
Arc::new(FakeProfiles(Arc::new(vec![profile(pid)]))),
vec![inspector],
);
let out = uc.execute(input(aid)).await.unwrap();
assert_eq!(out.details.last_topic, None);
assert_eq!(out.details.token_count, None);
}
// ---------------------------------------------------------------------------
// (b') inspector returns Read error → empty details, no error
// ---------------------------------------------------------------------------
#[tokio::test]
async fn read_error_degrades_to_empty_details() {
let pid = ProfileId::from_uuid(Uuid::from_u128(7));
let aid = AgentId::from_uuid(Uuid::from_u128(42));
let a = agent(aid, pid);
let (inspector, _seen) =
FakeInspector::new(true, InspectResult::Err(InspectError::Read("boom".to_owned())));
let uc = InspectConversation::new(
Arc::new(FakeContexts::with_agent(&a)),
Arc::new(FakeProfiles(Arc::new(vec![profile(pid)]))),
vec![inspector],
);
let out = uc.execute(input(aid)).await.unwrap();
assert_eq!(out.details, ConversationDetails { last_topic: None, token_count: None });
}
// ---------------------------------------------------------------------------
// (c) empty Vec (no inspector) → empty details, no error
// ---------------------------------------------------------------------------
#[tokio::test]
async fn no_inspector_yields_empty_details() {
let pid = ProfileId::from_uuid(Uuid::from_u128(7));
let aid = AgentId::from_uuid(Uuid::from_u128(42));
let a = agent(aid, pid);
let uc = InspectConversation::new(
Arc::new(FakeContexts::with_agent(&a)),
Arc::new(FakeProfiles(Arc::new(vec![profile(pid)]))),
Vec::new(),
);
let out = uc.execute(input(aid)).await.unwrap();
assert_eq!(out.details.last_topic, None);
assert_eq!(out.details.token_count, None);
}
// ---------------------------------------------------------------------------
// (c') an inspector that does NOT support the profile is skipped → empty
// ---------------------------------------------------------------------------
#[tokio::test]
async fn unsupported_inspector_is_skipped() {
let pid = ProfileId::from_uuid(Uuid::from_u128(7));
let aid = AgentId::from_uuid(Uuid::from_u128(42));
let a = agent(aid, pid);
let (inspector, seen) = FakeInspector::new(
false, // does not support
InspectResult::Ok(ConversationDetails {
last_topic: Some("should never surface".to_owned()),
token_count: Some(1),
}),
);
let uc = InspectConversation::new(
Arc::new(FakeContexts::with_agent(&a)),
Arc::new(FakeProfiles(Arc::new(vec![profile(pid)]))),
vec![inspector],
);
let out = uc.execute(input(aid)).await.unwrap();
assert_eq!(out.details.last_topic, None);
assert_eq!(out.details.token_count, None);
// details() must not have been called.
assert!(seen.lock().unwrap().is_none());
}
// ---------------------------------------------------------------------------
// Genuine resolution failures still error (unknown agent)
// ---------------------------------------------------------------------------
#[tokio::test]
async fn unknown_agent_errors() {
let aid = AgentId::from_uuid(Uuid::from_u128(99));
let (inspector, _seen) = FakeInspector::new(
true,
InspectResult::Ok(ConversationDetails { last_topic: None, token_count: None }),
);
let uc = InspectConversation::new(
Arc::new(FakeContexts::empty()),
Arc::new(FakeProfiles(Arc::new(Vec::new()))),
vec![inspector],
);
let err = uc.execute(input(aid)).await.unwrap_err();
// Should be a NOT_FOUND-class error, not a panic / empty success.
assert_eq!(err.code(), "NOT_FOUND");
}

View File

@ -26,9 +26,10 @@ use domain::markdown::MarkdownDoc;
use domain::ports::{
AgentContextStore, AgentRuntime, ContextInjectionPlan, DirEntry, EventBus, EventStream,
ExitStatus, FileSystem, FsError, IdGenerator, OutputStream, PreparedContext, ProfileStore,
PtyError, PtyHandle, PtyPort, RemotePath, RuntimeError, SkillStore, SpawnSpec, StoreError,
PtyError, PtyHandle, PtyPort, RemotePath, RuntimeError, SessionPlan, SkillStore, SpawnSpec,
StoreError,
};
use domain::profile::{AgentProfile, ContextInjection};
use domain::profile::{AgentProfile, ContextInjection, SessionStrategy};
use domain::project::{Project, ProjectPath};
use domain::remote::RemoteRef;
use domain::skill::{Skill, SkillScope};
@ -228,11 +229,22 @@ impl SkillStore for FakeSkills {
struct FakeRuntime {
trace: Trace,
plan: Option<ContextInjectionPlan>,
/// The last [`SessionPlan`] handed to `prepare_invocation`, captured so tests
/// can assert the launch resolved the right Assign/Resume/None intention (T4).
last_session: Arc<Mutex<Option<SessionPlan>>>,
}
impl FakeRuntime {
fn new(trace: Trace, plan: Option<ContextInjectionPlan>) -> Self {
Self { trace, plan }
Self {
trace,
plan,
last_session: Arc::new(Mutex::new(None)),
}
}
/// Shared handle to inspect the captured session plan after a launch.
fn session_probe(&self) -> Arc<Mutex<Option<SessionPlan>>> {
Arc::clone(&self.last_session)
}
}
@ -245,7 +257,9 @@ impl AgentRuntime for FakeRuntime {
profile: &AgentProfile,
_ctx: &PreparedContext,
cwd: &ProjectPath,
session: &SessionPlan,
) -> Result<SpawnSpec, RuntimeError> {
*self.last_session.lock().unwrap() = Some(session.clone());
self.trace.lock().unwrap().push("prepare".to_owned());
Ok(SpawnSpec {
command: profile.command.clone(),
@ -282,6 +296,28 @@ impl FakeFs {
fn created_dirs(&self) -> Vec<String> {
self.created_dirs.lock().unwrap().clone()
}
/// Convention-file / context writes only (excludes the Claude permission seed),
/// so injection assertions stay focused on the agent's `.md`.
fn context_writes(&self) -> Vec<(String, Vec<u8>)> {
self.writes()
.into_iter()
.filter(|(p, _)| !p.ends_with("/.claude/settings.local.json"))
.collect()
}
/// Only the Claude permission seed writes (`.claude/settings.local.json`).
fn seed_writes(&self) -> Vec<(String, Vec<u8>)> {
self.writes()
.into_iter()
.filter(|(p, _)| p.ends_with("/.claude/settings.local.json"))
.collect()
}
/// Created run dirs excluding the seed's `.claude` subdir.
fn created_run_dirs(&self) -> Vec<String> {
self.created_dirs()
.into_iter()
.filter(|p| !p.ends_with("/.claude"))
.collect()
}
}
#[async_trait]
@ -443,6 +479,7 @@ fn profile(id: ProfileId, injection: ContextInjection) -> AgentProfile {
injection,
Some("claude --version".to_owned()),
"{agentRunDir}",
None,
)
.unwrap()
}
@ -602,15 +639,26 @@ type LaunchFixture = (
SpyBus,
Arc<TerminalSessions>,
Trace,
Arc<Mutex<Option<SessionPlan>>>,
);
/// Wires a LaunchAgent over fakes for a given injection strategy/plan.
fn launch_fixture(injection: ContextInjection, plan: Option<ContextInjectionPlan>) -> LaunchFixture {
let agent = scratch_agent(aid(1), "Backend", "agents/backend.md", pid(9));
launch_fixture_with_profile(profile(pid(9), injection), plan)
}
/// Like [`launch_fixture`] but takes a fully-built profile (so session-strategy
/// variants can be exercised). The seeded agent uses the profile's id.
fn launch_fixture_with_profile(
profile: AgentProfile,
plan: Option<ContextInjectionPlan>,
) -> LaunchFixture {
let agent = scratch_agent(aid(1), "Backend", "agents/backend.md", profile.id);
let contexts = FakeContexts::with_agent(&agent, "# ctx body");
let profiles = FakeProfiles::new(vec![profile(pid(9), injection)]);
let profiles = FakeProfiles::new(vec![profile]);
let tr = trace();
let runtime = FakeRuntime::new(Arc::clone(&tr), plan);
let session_probe = runtime.session_probe();
let fs = FakeFs::new(Arc::clone(&tr));
let pty = FakePty::new(Arc::clone(&tr), sid(777));
let sessions = Arc::new(TerminalSessions::new());
@ -624,8 +672,9 @@ fn launch_fixture(injection: ContextInjection, plan: Option<ContextInjectionPlan
Arc::new(FakeSkills::default()),
Arc::clone(&sessions),
Arc::new(bus.clone()),
Arc::new(SeqIds::new()),
);
(launch, agent, fs, pty, bus, sessions, tr)
(launch, agent, fs, pty, bus, sessions, tr, session_probe)
}
fn launch_input(agent_id: AgentId) -> LaunchAgentInput {
@ -635,13 +684,14 @@ fn launch_input(agent_id: AgentId) -> LaunchAgentInput {
rows: 24,
cols: 80,
node_id: None,
conversation_id: None,
}
}
#[tokio::test]
async fn launch_orders_prepare_then_injection_then_spawn() {
// conventionFile strategy → an fs.write must happen between prepare and spawn.
let (launch, agent, fs, pty, bus, sessions, tr) = launch_fixture(
let (launch, agent, fs, pty, bus, sessions, tr, _session) = launch_fixture(
ContextInjection::convention_file("CLAUDE.md").unwrap(),
Some(ContextInjectionPlan::File {
target: "CLAUDE.md".to_owned(),
@ -650,11 +700,17 @@ async fn launch_orders_prepare_then_injection_then_spawn() {
let out = launch.execute(launch_input(agent.id)).await.expect("launch");
// Ordering contract.
// Ordering contract. The Claude permission seed is written first (right after
// the run dir is created), then prepare → injection (convention file) → spawn.
assert_eq!(
*tr.lock().unwrap(),
vec!["prepare".to_owned(), "fs.write".to_owned(), "spawn".to_owned()],
"prepare → injection → spawn"
vec![
"fs.write".to_owned(),
"prepare".to_owned(),
"fs.write".to_owned(),
"spawn".to_owned()
],
"seed → prepare → injection → spawn"
);
// The conventionFile was written inside the agent's isolated run directory
@ -662,7 +718,7 @@ async fn launch_orders_prepare_then_injection_then_spawn() {
// is the *composed* document: an absolute project-root header followed by the
// agent persona `.md`.
let run_dir = format!("/home/me/proj/.ideai/run/{}", agent.id);
let writes = fs.writes();
let writes = fs.context_writes();
assert_eq!(writes.len(), 1);
assert_eq!(writes[0].0, format!("{run_dir}/CLAUDE.md"));
let written = String::from_utf8(writes[0].1.clone()).unwrap();
@ -675,8 +731,20 @@ async fn launch_orders_prepare_then_injection_then_spawn() {
"convention file must carry the agent persona, got: {written}"
);
// The run directory was created (via the FileSystem port) before spawn.
assert_eq!(fs.created_dirs(), vec![run_dir.clone()]);
// Bug #5: a Claude permission seed was written into the run dir so the agent
// runs with the project's autonomy instead of prompting per command.
let seeds = fs.seed_writes();
assert_eq!(seeds.len(), 1);
assert_eq!(seeds[0].0, format!("{run_dir}/.claude/settings.local.json"));
let seed = String::from_utf8(seeds[0].1.clone()).unwrap();
assert!(seed.contains("bypassPermissions"), "seed grants autonomy: {seed}");
assert!(seed.contains("/home/me/proj"), "seed grants the project root");
// The run directory (and the seed's `.claude` subdir) were created before spawn.
assert_eq!(fs.created_run_dirs(), vec![run_dir.clone()]);
assert!(fs
.created_dirs()
.contains(&format!("{run_dir}/.claude")));
// Spawn happened at the isolated run dir with the profile command.
let spawns = pty.spawns();
@ -743,6 +811,7 @@ async fn two_agents_same_root_get_distinct_run_dirs_no_collision() {
Arc::new(FakeSkills::default()),
Arc::clone(&sessions),
Arc::new(SpyBus::default()),
Arc::new(SeqIds::new()),
);
launch.execute(launch_input(agent_a.id)).await.unwrap();
@ -752,8 +821,8 @@ async fn two_agents_same_root_get_distinct_run_dirs_no_collision() {
let dir_b = format!("/home/me/proj/.ideai/run/{}", agent_b.id);
assert_ne!(dir_a, dir_b, "the two agents must map to different run dirs");
// Two distinct run dirs were created.
assert_eq!(fs.created_dirs(), vec![dir_a.clone(), dir_b.clone()]);
// Two distinct run dirs were created (ignoring each seed's `.claude` subdir).
assert_eq!(fs.created_run_dirs(), vec![dir_a.clone(), dir_b.clone()]);
// Two spawns at two distinct cwd — the core anti-collision guarantee.
let spawns = pty.spawns();
@ -763,7 +832,7 @@ async fn two_agents_same_root_get_distinct_run_dirs_no_collision() {
assert_ne!(spawns[0].cwd, spawns[1].cwd);
// Two convention files, each inside its own run dir (no shared root file).
let writes = fs.writes();
let writes = fs.context_writes();
assert_eq!(writes.len(), 2);
assert_eq!(writes[0].0, format!("{dir_a}/CLAUDE.md"));
assert_eq!(writes[1].0, format!("{dir_b}/CLAUDE.md"));
@ -815,11 +884,12 @@ async fn launch_conventionfile_injects_assigned_skills_in_order() {
Arc::new(skills),
Arc::new(TerminalSessions::new()),
Arc::new(SpyBus::default()),
Arc::new(SeqIds::new()),
);
launch.execute(launch_input(agent.id)).await.unwrap();
let writes = fs.writes();
let writes = fs.context_writes();
assert_eq!(writes.len(), 1);
let doc = String::from_utf8(writes[0].1.clone()).unwrap();
assert!(doc.contains("# persona"), "persona present: {doc}");
@ -861,16 +931,42 @@ async fn launch_skips_dangling_skill_ref_without_failing() {
Arc::new(FakeSkills::default()), // empty store ⇒ the ref is dangling
Arc::new(TerminalSessions::new()),
Arc::new(SpyBus::default()),
Arc::new(SeqIds::new()),
);
launch.execute(launch_input(agent.id)).await.expect("launch must succeed");
let doc = String::from_utf8(fs.writes()[0].1.clone()).unwrap();
let doc = String::from_utf8(fs.context_writes()[0].1.clone()).unwrap();
assert!(!doc.contains("# Skills"), "no Skills section for a dangling ref: {doc}");
}
#[tokio::test]
async fn launch_non_claude_convention_does_not_seed_permissions() {
// A CLI whose convention file is NOT CLAUDE.md (e.g. Codex → AGENTS.md) must
// get its convention file but NO Claude permission seed (Bug #5 is per-CLI).
let (launch, agent, fs, _pty, _bus, _sessions, _tr, _session) = launch_fixture(
ContextInjection::convention_file("AGENTS.md").unwrap(),
Some(ContextInjectionPlan::File {
target: "AGENTS.md".to_owned(),
}),
);
launch.execute(launch_input(agent.id)).await.unwrap();
// The convention file was written, but no permission seed.
assert_eq!(fs.context_writes().len(), 1);
assert!(
fs.seed_writes().is_empty(),
"no Claude seed for a non-Claude CLI"
);
assert!(
!fs.created_dirs().iter().any(|p| p.ends_with("/.claude")),
"no .claude dir created for a non-Claude CLI"
);
}
#[tokio::test]
async fn launch_stdin_strategy_pipes_context_after_spawn() {
let (launch, agent, fs, pty, _bus, _sessions, tr) =
let (launch, agent, fs, pty, _bus, _sessions, tr, _session) =
launch_fixture(ContextInjection::stdin(), Some(ContextInjectionPlan::Stdin));
launch.execute(launch_input(agent.id)).await.unwrap();
@ -886,7 +982,7 @@ async fn launch_stdin_strategy_pipes_context_after_spawn() {
#[tokio::test]
async fn launch_unknown_agent_is_not_found() {
let (launch, _agent, _fs, pty, _bus, _sessions, _tr) = launch_fixture(
let (launch, _agent, _fs, pty, _bus, _sessions, _tr, _session) = launch_fixture(
ContextInjection::stdin(),
Some(ContextInjectionPlan::Stdin),
);
@ -912,9 +1008,109 @@ async fn launch_unknown_profile_is_not_found() {
Arc::new(FakeSkills::default()),
Arc::new(TerminalSessions::new()),
Arc::new(SpyBus::default()),
Arc::new(SeqIds::new()),
);
let err = launch.execute(launch_input(agent.id)).await.unwrap_err();
assert_eq!(err.code(), "NOT_FOUND", "got {err:?}");
assert!(pty.spawns().is_empty(), "no spawn when profile unresolved");
}
// ---------------------------------------------------------------------------
// LaunchAgent — session resume (T4)
// ---------------------------------------------------------------------------
/// Builds a stdin profile carrying a [`SessionStrategy`] (assign + resume flags).
fn profile_with_session(
id: ProfileId,
assign_flag: Option<&str>,
resume_flag: &str,
) -> AgentProfile {
let session =
SessionStrategy::new(assign_flag.map(str::to_owned), resume_flag.to_owned()).unwrap();
AgentProfile::new(
id,
"Claude Code",
"claude",
Vec::new(),
ContextInjection::stdin(),
Some("claude --version".to_owned()),
"{agentRunDir}",
Some(session),
)
.unwrap()
}
#[tokio::test]
async fn launch_first_time_with_assign_flag_mints_and_exposes_conversation_id() {
// Fresh cell (conversation_id = None), profile WITH an assign_flag.
let (launch, agent, _fs, _pty, _bus, _sessions, _tr, session) = launch_fixture_with_profile(
profile_with_session(pid(9), Some("--session-id"), "--resume"),
Some(ContextInjectionPlan::Stdin),
);
let out = launch.execute(launch_input(agent.id)).await.expect("launch");
// SeqIds yields Uuid::from_u128(1) on its first call.
let expected = Uuid::from_u128(1).to_string();
// The use case exposes the assigned id (caller persists it on the leaf).
assert_eq!(out.assigned_conversation_id.as_deref(), Some(expected.as_str()));
// The runtime received an Assign plan carrying exactly that id.
assert_eq!(
*session.lock().unwrap(),
Some(SessionPlan::Assign { conversation_id: expected }),
);
}
#[tokio::test]
async fn launch_reopen_with_existing_conversation_id_resumes_without_new_id() {
// The cell already carries a conversation id ⇒ Resume, no new id minted.
let (launch, agent, _fs, _pty, _bus, _sessions, _tr, session) = launch_fixture_with_profile(
profile_with_session(pid(9), Some("--session-id"), "--resume"),
Some(ContextInjectionPlan::Stdin),
);
let mut input = launch_input(agent.id);
input.conversation_id = Some("conv-existing".to_owned());
let out = launch.execute(input).await.expect("launch");
// Nothing newly assigned (the id pre-existed): caller has nothing to persist.
assert_eq!(out.assigned_conversation_id, None);
// Resume plan with the existing id; SeqIds was never consulted.
assert_eq!(
*session.lock().unwrap(),
Some(SessionPlan::Resume {
conversation_id: "conv-existing".to_owned()
}),
);
}
#[tokio::test]
async fn launch_profile_without_session_block_passes_none() {
// Non-regression: a profile with no session block behaves exactly as before.
let (launch, agent, _fs, _pty, _bus, _sessions, _tr, session) =
launch_fixture(ContextInjection::stdin(), Some(ContextInjectionPlan::Stdin));
let out = launch.execute(launch_input(agent.id)).await.expect("launch");
assert_eq!(out.assigned_conversation_id, None);
assert_eq!(*session.lock().unwrap(), Some(SessionPlan::None));
}
#[tokio::test]
async fn launch_degraded_mode_without_assign_flag_first_launch_is_none() {
// Profile HAS a session block but NO assign_flag (degraded): a first launch
// (no cell id) must NOT mint an id and must pass SessionPlan::None.
let (launch, agent, _fs, _pty, _bus, _sessions, _tr, session) = launch_fixture_with_profile(
profile_with_session(pid(9), None, "--continue"),
Some(ContextInjectionPlan::Stdin),
);
let out = launch.execute(launch_input(agent.id)).await.expect("launch");
assert_eq!(out.assigned_conversation_id, None, "degraded mode mints no id");
assert_eq!(*session.lock().unwrap(), Some(SessionPlan::None));
}

View File

@ -191,6 +191,8 @@ fn single_leaf(node_id: NodeId) -> LayoutTree {
id: node_id,
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
})
}
@ -638,6 +640,77 @@ async fn mutate_set_cell_agent_missing_leaf_is_not_found() {
assert_eq!(err.code(), "NOT_FOUND", "got {err:?}");
}
// ---------------------------------------------------------------------------
// SetCellConversation (T4b — persist the assigned CLI conversation id)
// ---------------------------------------------------------------------------
#[tokio::test]
async fn mutate_set_cell_conversation_persists_id_on_leaf() {
let env = mut_env(pid(52)).await;
// Record a conversation id on the single root leaf (nid(1)).
env.mutate
.execute(MutateLayoutInput {
project_id: env.project_id,
layout_id: None,
operation: LayoutOperation::SetCellConversation {
target: nid(1),
conversation_id: Some("conv-42".to_owned()),
},
})
.await
.expect("set_cell_conversation records the id");
// The id must survive in the persisted JSON, so the next open resumes.
let tree_json = active_tree_json(&env.fs);
assert_eq!(
tree_json["root"]["node"]["conversationId"], "conv-42",
"conversation id must be persisted on the leaf"
);
// Now clear it.
let out = env
.mutate
.execute(MutateLayoutInput {
project_id: env.project_id,
layout_id: None,
operation: LayoutOperation::SetCellConversation {
target: nid(1),
conversation_id: None,
},
})
.await
.expect("set_cell_conversation clears the id");
match &out.layout.root {
LayoutNode::Leaf(l) => assert_eq!(l.conversation_id, None, "id must be cleared"),
_ => panic!("expected leaf root"),
}
assert!(
active_tree_json(&env.fs)["root"]["node"]
.get("conversationId")
.is_none(),
"cleared id must not be serialised"
);
}
#[tokio::test]
async fn mutate_set_cell_conversation_missing_leaf_is_not_found() {
let env = mut_env(pid(53)).await;
let err = env
.mutate
.execute(MutateLayoutInput {
project_id: env.project_id,
layout_id: None,
operation: LayoutOperation::SetCellConversation {
target: nid(404),
conversation_id: Some("x".to_owned()),
},
})
.await
.expect_err("unknown node rejected");
assert_eq!(err.code(), "NOT_FOUND", "got {err:?}");
}
// ---------------------------------------------------------------------------
// Named-layout management (#4)
// ---------------------------------------------------------------------------

View File

@ -22,7 +22,8 @@ use domain::markdown::MarkdownDoc;
use domain::ports::{
AgentContextStore, AgentRuntime, ContextInjectionPlan, DirEntry, EventBus, EventStream,
ExitStatus, FileSystem, FsError, IdGenerator, OutputStream, PreparedContext, ProfileStore,
PtyError, PtyHandle, PtyPort, RemotePath, RuntimeError, SkillStore, SpawnSpec, StoreError,
PtyError, PtyHandle, PtyPort, RemotePath, RuntimeError, SessionPlan, SkillStore, SpawnSpec,
StoreError,
};
use domain::ids::SkillId;
use domain::profile::{AgentProfile, ContextInjection};
@ -231,6 +232,7 @@ impl AgentRuntime for FakeRuntime {
profile: &AgentProfile,
_ctx: &PreparedContext,
cwd: &ProjectPath,
_session: &SessionPlan,
) -> Result<SpawnSpec, RuntimeError> {
Ok(SpawnSpec {
command: profile.command.clone(),
@ -372,6 +374,7 @@ fn claude_profile() -> AgentProfile {
ContextInjection::stdin(),
Some("claude --version".to_owned()),
"{agentRunDir}",
None,
)
.unwrap()
}
@ -410,6 +413,7 @@ fn fixture(contexts: FakeContexts) -> Fixture {
Arc::new(FakeSkills),
Arc::clone(&sessions),
Arc::new(bus.clone()),
Arc::new(SeqIds::new()),
));
let list = Arc::new(ListAgents::new(Arc::new(contexts.clone())));
let close = Arc::new(CloseTerminal::new(

View File

@ -13,7 +13,7 @@ use async_trait::async_trait;
use domain::ids::ProfileId;
use domain::ports::{
AgentRuntime, PreparedContext, ProfileStore, RuntimeError, SpawnSpec, StoreError,
AgentRuntime, PreparedContext, ProfileStore, RuntimeError, SessionPlan, SpawnSpec, StoreError,
};
use domain::profile::{AgentProfile, ContextInjection};
use domain::project::ProjectPath;
@ -103,6 +103,7 @@ impl AgentRuntime for StubRuntime {
_profile: &AgentProfile,
_ctx: &PreparedContext,
_cwd: &ProjectPath,
_session: &SessionPlan,
) -> Result<SpawnSpec, RuntimeError> {
unreachable!("not used in these tests")
}
@ -117,6 +118,7 @@ fn profile(id: u128, name: &str, command: &str) -> AgentProfile {
ContextInjection::stdin(),
Some(format!("{command} --version")),
"{projectRoot}",
None,
)
.unwrap()
}

View File

@ -0,0 +1,389 @@
//! T5 tests for [`SnapshotRunningAgents`].
//!
//! At close time, before the global PTY kill, the use case must freeze on each
//! agent-bearing leaf whether that agent's PTY was still live
//! (`agent_was_running`). Liveness is derived purely from the live-session
//! registry (a [`LiveAgentRegistry`]), never from CLI parsing.
//!
//! Every port is faked in-memory. We assert the persisted `layouts.json` flags
//! and the ordering guarantee (snapshot reads liveness BEFORE the kill).
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use domain::layout::Workspace;
use domain::ports::{DirEntry, FileSystem, FsError, ProjectStore, RemotePath, StoreError};
use domain::{
AgentId, Direction, LayoutId, LayoutNode, LayoutTree, LeafCell, NodeId, Project, ProjectId,
ProjectPath, RemoteRef, SplitContainer, WeightedChild,
};
use uuid::Uuid;
use application::{LiveAgentRegistry, SnapshotRunningAgents, SnapshotRunningAgentsInput};
// ---------------------------------------------------------------------------
// Fakes
// ---------------------------------------------------------------------------
#[derive(Default)]
struct FakeFsInner {
files: HashMap<String, Vec<u8>>,
dirs: HashSet<String>,
}
#[derive(Default, Clone)]
struct FakeFs(Arc<Mutex<FakeFsInner>>);
impl FakeFs {
fn read_file(&self, path: &str) -> Option<Vec<u8>> {
self.0.lock().unwrap().files.get(path).cloned()
}
fn put(&self, path: &str, data: &[u8]) {
self.0
.lock()
.unwrap()
.files
.insert(path.to_owned(), data.to_vec());
}
fn writes(&self) -> usize {
self.0.lock().unwrap().files.len()
}
}
#[async_trait]
impl FileSystem for FakeFs {
async fn read(&self, path: &RemotePath) -> Result<Vec<u8>, FsError> {
self.0
.lock()
.unwrap()
.files
.get(path.as_str())
.cloned()
.ok_or_else(|| FsError::NotFound(path.as_str().to_owned()))
}
async fn write(&self, path: &RemotePath, data: &[u8]) -> Result<(), FsError> {
self.0
.lock()
.unwrap()
.files
.insert(path.as_str().to_owned(), data.to_vec());
Ok(())
}
async fn exists(&self, path: &RemotePath) -> Result<bool, FsError> {
let inner = self.0.lock().unwrap();
Ok(inner.files.contains_key(path.as_str()) || inner.dirs.contains(path.as_str()))
}
async fn create_dir_all(&self, path: &RemotePath) -> Result<(), FsError> {
self.0.lock().unwrap().dirs.insert(path.as_str().to_owned());
Ok(())
}
async fn list(&self, _path: &RemotePath) -> Result<Vec<DirEntry>, FsError> {
Ok(Vec::new())
}
async fn symlink(&self, _src: &RemotePath, _dst: &RemotePath) -> Result<(), FsError> {
Ok(())
}
}
#[derive(Default)]
struct FakeStoreInner {
projects: Vec<Project>,
}
#[derive(Default, Clone)]
struct FakeStore(Arc<Mutex<FakeStoreInner>>);
#[async_trait]
impl ProjectStore for FakeStore {
async fn list_projects(&self) -> Result<Vec<Project>, StoreError> {
Ok(self.0.lock().unwrap().projects.clone())
}
async fn load_project(&self, id: ProjectId) -> Result<Project, StoreError> {
self.0
.lock()
.unwrap()
.projects
.iter()
.find(|p| p.id == id)
.cloned()
.ok_or(StoreError::NotFound)
}
async fn save_project(&self, project: &Project) -> Result<(), StoreError> {
self.0.lock().unwrap().projects.push(project.clone());
Ok(())
}
async fn save_workspace(&self, _w: &Workspace) -> Result<(), StoreError> {
Ok(())
}
async fn load_workspace(&self) -> Result<Workspace, StoreError> {
Ok(Workspace::default())
}
}
/// A controllable liveness registry: an agent is "live" iff it is in the set.
#[derive(Default, Clone)]
struct FakeLive(Arc<Mutex<HashSet<AgentId>>>);
impl FakeLive {
fn with(agents: &[AgentId]) -> Self {
Self(Arc::new(Mutex::new(agents.iter().copied().collect())))
}
/// Simulates a PTY kill: forget every live agent.
fn kill_all(&self) {
self.0.lock().unwrap().clear();
}
}
impl LiveAgentRegistry for FakeLive {
fn is_agent_live(&self, agent_id: &AgentId) -> bool {
self.0.lock().unwrap().contains(agent_id)
}
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const ROOT: &str = "/home/me/proj";
const LAYOUTS_PATH: &str = "/home/me/proj/.ideai/layouts.json";
fn pid(n: u128) -> ProjectId {
ProjectId::from_uuid(Uuid::from_u128(n))
}
fn nid(n: u128) -> NodeId {
NodeId::from_uuid(Uuid::from_u128(n))
}
fn aid(n: u128) -> AgentId {
AgentId::from_uuid(Uuid::from_u128(n))
}
fn lid(n: u128) -> LayoutId {
LayoutId::from_uuid(Uuid::from_u128(n))
}
fn agent_leaf(node: NodeId, agent: Option<AgentId>) -> LeafCell {
LeafCell {
id: node,
session: None,
agent,
conversation_id: None,
agent_was_running: false,
}
}
async fn register_project(store: &FakeStore, id: ProjectId) {
let project =
Project::new(id, "Demo", ProjectPath::new(ROOT).unwrap(), RemoteRef::Local, 0).unwrap();
store.save_project(&project).await.unwrap();
}
/// Seeds a valid `layouts.json` with one active layout holding `tree`.
fn seed_layouts(fs: &FakeFs, id: LayoutId, tree: &LayoutTree) {
let doc = serde_json::json!({
"version": 1,
"activeId": id.to_string(),
"layouts": [ { "id": id.to_string(), "name": "Default", "tree": tree } ],
});
fs.put(LAYOUTS_PATH, &serde_json::to_vec(&doc).unwrap());
}
fn doc_json(fs: &FakeFs) -> serde_json::Value {
serde_json::from_slice(&fs.read_file(LAYOUTS_PATH).expect("layouts.json present")).unwrap()
}
/// Walks the active layout tree and returns `agent_was_running` for the leaf
/// `node`, or `None` if absent.
fn was_running(fs: &FakeFs, node: NodeId) -> Option<bool> {
let doc = doc_json(fs);
let tree: LayoutTree =
serde_json::from_value(doc["layouts"][0]["tree"].clone()).expect("tree parseable");
fn find(node: &LayoutNode, target: NodeId) -> Option<bool> {
match node {
LayoutNode::Leaf(l) if l.id == target => Some(l.agent_was_running),
LayoutNode::Leaf(_) => None,
LayoutNode::Split(s) => s.children.iter().find_map(|c| find(&c.node, target)),
LayoutNode::Grid(g) => g.cells.iter().find_map(|c| find(&c.node, target)),
}
}
find(&tree.root, node)
}
fn make_use_case(
store: &FakeStore,
fs: &FakeFs,
live: &FakeLive,
) -> SnapshotRunningAgents {
SnapshotRunningAgents::new(
Arc::new(store.clone()) as Arc<dyn ProjectStore>,
Arc::new(fs.clone()) as Arc<dyn FileSystem>,
Arc::new(live.clone()) as Arc<dyn LiveAgentRegistry>,
)
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
/// A live agent's leaf is persisted with `agent_was_running = true`.
#[tokio::test]
async fn live_agent_is_marked_running() {
let store = FakeStore::default();
let fs = FakeFs::default();
register_project(&store, pid(1)).await;
let leaf = nid(10);
let agent = aid(100);
seed_layouts(&fs, lid(1), &LayoutTree::single(agent_leaf(leaf, Some(agent))));
let live = FakeLive::with(&[agent]);
let uc = make_use_case(&store, &fs, &live);
let out = uc
.execute(SnapshotRunningAgentsInput { project_id: pid(1) })
.await
.unwrap();
assert_eq!(out.running, 1);
assert_eq!(out.stopped, 0);
assert_eq!(was_running(&fs, leaf), Some(true));
}
/// An agent whose PTY has already exited is persisted with `false`.
#[tokio::test]
async fn stopped_agent_is_marked_not_running() {
let store = FakeStore::default();
let fs = FakeFs::default();
register_project(&store, pid(1)).await;
let leaf = nid(10);
let agent = aid(100);
seed_layouts(&fs, lid(1), &LayoutTree::single(agent_leaf(leaf, Some(agent))));
// Registry is empty: the agent has no live session.
let live = FakeLive::default();
let uc = make_use_case(&store, &fs, &live);
let out = uc
.execute(SnapshotRunningAgentsInput { project_id: pid(1) })
.await
.unwrap();
assert_eq!(out.running, 0);
assert_eq!(out.stopped, 1);
assert_eq!(was_running(&fs, leaf), Some(false));
}
/// Mixed tree (split with one live agent, one stopped agent, one plain leaf):
/// each agent leaf gets the right flag; the non-agent leaf is untouched.
#[tokio::test]
async fn mixed_tree_flags_each_agent_independently() {
let store = FakeStore::default();
let fs = FakeFs::default();
register_project(&store, pid(1)).await;
let live_leaf = nid(10);
let dead_leaf = nid(11);
let plain_leaf = nid(12);
let live_agent = aid(100);
let dead_agent = aid(101);
let tree = LayoutTree::new(LayoutNode::Split(SplitContainer {
id: nid(1),
direction: Direction::Row,
children: vec![
WeightedChild {
node: LayoutNode::Leaf(agent_leaf(live_leaf, Some(live_agent))),
weight: 1.0,
},
WeightedChild {
node: LayoutNode::Leaf(agent_leaf(dead_leaf, Some(dead_agent))),
weight: 1.0,
},
WeightedChild {
node: LayoutNode::Leaf(agent_leaf(plain_leaf, None)),
weight: 1.0,
},
],
}));
seed_layouts(&fs, lid(1), &tree);
let live = FakeLive::with(&[live_agent]);
let uc = make_use_case(&store, &fs, &live);
let out = uc
.execute(SnapshotRunningAgentsInput { project_id: pid(1) })
.await
.unwrap();
assert_eq!(out.running, 1);
assert_eq!(out.stopped, 1);
assert_eq!(was_running(&fs, live_leaf), Some(true));
assert_eq!(was_running(&fs, dead_leaf), Some(false));
assert_eq!(was_running(&fs, plain_leaf), Some(false)); // default, untouched
}
/// Ordering guarantee: the snapshot reads liveness BEFORE the kill. Running the
/// snapshot on the live registry persists `true`; running it AFTER `kill_all`
/// (simulating a kill-then-snapshot mistake) would persist `false`.
#[tokio::test]
async fn snapshot_reads_liveness_before_kill() {
let store = FakeStore::default();
let fs = FakeFs::default();
register_project(&store, pid(1)).await;
let leaf = nid(10);
let agent = aid(100);
seed_layouts(&fs, lid(1), &LayoutTree::single(agent_leaf(leaf, Some(agent))));
let live = FakeLive::with(&[agent]);
let uc = make_use_case(&store, &fs, &live);
// Correct order (composition root contract): snapshot THEN kill.
uc.execute(SnapshotRunningAgentsInput { project_id: pid(1) })
.await
.unwrap();
live.kill_all();
assert_eq!(
was_running(&fs, leaf),
Some(true),
"snapshot taken before kill must record running"
);
// Re-seed and demonstrate the opposite order yields `false` — proving the
// flag is sensitive to registry state at call time (hence order matters).
seed_layouts(&fs, lid(1), &LayoutTree::single(agent_leaf(leaf, Some(agent))));
uc.execute(SnapshotRunningAgentsInput { project_id: pid(1) })
.await
.unwrap();
assert_eq!(
was_running(&fs, leaf),
Some(false),
"snapshot taken after kill records not-running"
);
}
/// A layout with no agent leaf is a no-op: nothing is written.
#[tokio::test]
async fn no_agent_leaf_is_noop() {
let store = FakeStore::default();
let fs = FakeFs::default();
register_project(&store, pid(1)).await;
let leaf = nid(10);
seed_layouts(&fs, lid(1), &LayoutTree::single(agent_leaf(leaf, None)));
let writes_before = fs.writes();
let live = FakeLive::default();
let uc = make_use_case(&store, &fs, &live);
let out = uc
.execute(SnapshotRunningAgentsInput { project_id: pid(1) })
.await
.unwrap();
assert_eq!(out.running, 0);
assert_eq!(out.stopped, 0);
// No agent leaf → no persistence triggered (file count unchanged).
assert_eq!(fs.writes(), writes_before);
assert_eq!(was_running(&fs, leaf), Some(false));
}

View File

@ -61,6 +61,8 @@ fn tab(n: u128) -> Tab {
id: NodeId::from_uuid(Uuid::from_u128(900 + n)),
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
})),
}
}

View File

@ -21,6 +21,15 @@ pub enum Direction {
Column,
}
/// Returns `true` when a boolean is `false`. Used as a `skip_serializing_if`
/// predicate so that default (`false`) flags are omitted from the serialized
/// form, preserving backward/forward compatibility with leaves that predate the
/// field.
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_false(b: &bool) -> bool {
!*b
}
/// A leaf cell hosting zero or one terminal session.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
@ -33,6 +42,15 @@ pub struct LeafCell {
/// The agent to launch automatically in this cell, if any.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub agent: Option<AgentId>,
/// Opaque CLI conversation id, **persistent** across the cell's lifetime
/// (distinct from the ephemeral PTY [`SessionId`]). Enables resuming a CLI
/// conversation after the terminal/PTY has been closed and reopened.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub conversation_id: Option<String>,
/// Whether the cell's agent process was running at the moment the cell was
/// last closed. Used to decide whether to auto-resume the agent on reopen.
#[serde(default, skip_serializing_if = "is_false")]
pub agent_was_running: bool,
}
/// A weighted child within a [`SplitContainer`]. The `weight` is a *relative*
@ -349,6 +367,8 @@ impl LayoutTree {
id: leaf.id,
session: None,
agent: leaf.agent,
conversation_id: leaf.conversation_id.clone(),
agent_was_running: leaf.agent_was_running,
});
}
if leaf.id == to {
@ -356,6 +376,8 @@ impl LayoutTree {
id: leaf.id,
session: Some(session),
agent: leaf.agent,
conversation_id: leaf.conversation_id.clone(),
agent_was_running: leaf.agent_was_running,
});
}
}
@ -392,6 +414,8 @@ impl LayoutTree {
id: leaf.id,
session,
agent: leaf.agent,
conversation_id: leaf.conversation_id.clone(),
agent_was_running: leaf.agent_was_running,
});
}
}
@ -427,6 +451,8 @@ impl LayoutTree {
id: leaf.id,
session: leaf.session,
agent,
conversation_id: leaf.conversation_id.clone(),
agent_was_running: leaf.agent_was_running,
});
}
}
@ -440,6 +466,113 @@ impl LayoutTree {
Ok(tree)
}
/// Sets (or, with `None`, clears) the persistent CLI `conversation_id` on
/// the leaf `target`.
///
/// Unlike [`Self::set_session`] (the ephemeral PTY binding), the conversation
/// id survives PTY close/reopen and is what lets the agent CLI *resume* its
/// previous conversation.
///
/// Pure: returns a new validated tree.
///
/// # Errors
/// - [`LayoutError::NodeNotFound`] if `target` is not a leaf in the tree.
pub fn set_cell_conversation(
&self,
target: NodeId,
conversation_id: Option<String>,
) -> Result<Self, LayoutError> {
let mut found = false;
let root = map_node(&self.root, &mut |node| {
if let LayoutNode::Leaf(leaf) = node {
if leaf.id == target {
found = true;
return LayoutNode::Leaf(LeafCell {
id: leaf.id,
session: leaf.session,
agent: leaf.agent,
conversation_id: conversation_id.clone(),
agent_was_running: leaf.agent_was_running,
});
}
}
node.clone()
});
if !found {
return Err(LayoutError::NodeNotFound(target));
}
let tree = Self { root };
tree.validate()?;
Ok(tree)
}
/// Records whether the cell's agent process was `running` at close time, on
/// the leaf `target`.
///
/// Pure: returns a new validated tree.
///
/// # Errors
/// - [`LayoutError::NodeNotFound`] if `target` is not a leaf in the tree.
pub fn set_agent_running(
&self,
target: NodeId,
running: bool,
) -> Result<Self, LayoutError> {
let mut found = false;
let root = map_node(&self.root, &mut |node| {
if let LayoutNode::Leaf(leaf) = node {
if leaf.id == target {
found = true;
return LayoutNode::Leaf(LeafCell {
id: leaf.id,
session: leaf.session,
agent: leaf.agent,
conversation_id: leaf.conversation_id.clone(),
agent_was_running: running,
});
}
}
node.clone()
});
if !found {
return Err(LayoutError::NodeNotFound(target));
}
let tree = Self { root };
tree.validate()?;
Ok(tree)
}
/// Collects every leaf that carries an agent, as `(leaf id, agent id)` pairs.
///
/// Used by the close-time snapshot of running agents: the application walks
/// these leaves and records, on each, whether the agent's PTY was still live
/// (`agent_was_running`) before the global PTY kill. Pure read-only traversal.
#[must_use]
pub fn agent_leaves(&self) -> Vec<(NodeId, AgentId)> {
fn walk(node: &LayoutNode, out: &mut Vec<(NodeId, AgentId)>) {
match node {
LayoutNode::Leaf(leaf) => {
if let Some(agent) = leaf.agent {
out.push((leaf.id, agent));
}
}
LayoutNode::Split(split) => {
for child in &split.children {
walk(&child.node, out);
}
}
LayoutNode::Grid(grid) => {
for cell in &grid.cells {
walk(&cell.node, out);
}
}
}
}
let mut out = Vec::new();
walk(&self.root, &mut out);
out
}
/// Returns `Ok(Some(session))` / `Ok(None)` for the session held by the leaf
/// `id`, or [`LayoutError::NodeNotFound`] if no such leaf exists.
fn session_in_leaf(&self, id: NodeId) -> Result<Option<SessionId>, LayoutError> {

View File

@ -67,7 +67,7 @@ pub use skill::{Skill, SkillRef, SkillScope};
pub use template::{AgentTemplate, TemplateVersion};
pub use profile::{AgentProfile, ContextInjection};
pub use profile::{AgentProfile, ContextInjection, SessionStrategy};
pub use markdown::MarkdownDoc;

View File

@ -65,6 +65,23 @@ pub enum ContextInjectionPlan {
},
}
/// Intention de session pour un lancement d'agent donné.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SessionPlan {
/// Pas de reprise : profil sans bloc `session`, ou cellule neuve sans id.
None,
/// Premier lancement : IdeA a généré `conversation_id`, à assigner via assign_flag.
Assign {
/// Identifiant de conversation généré par IdeA pour ce lancement.
conversation_id: String,
},
/// Réouverture : reprendre la conversation existante via resume_flag.
Resume {
/// Identifiant de la conversation existante à reprendre.
conversation_id: String,
},
}
/// A fully-resolved process invocation: command, args, cwd, environment, and the
/// plan for delivering the agent context.
#[derive(Debug, Clone, PartialEq, Eq)]
@ -91,6 +108,22 @@ pub struct PreparedContext {
pub relative_path: String,
}
/// Enriched, **best-effort** details about a conversation, specific to a CLI's
/// on-disk transcript format (CONTEXT §T6).
///
/// Every field is optional: the core never *requires* this data, and a missing
/// piece (or a missing inspector) must never block a resume. The values exist
/// purely to enrich a resume popup (last topic, token indicator).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConversationDetails {
/// A short, best-effort label for the conversation (e.g. the last user
/// message, truncated). `None` when it cannot be extracted.
pub last_topic: Option<String>,
/// A best-effort cumulative token count for the conversation. `None` when no
/// usage information is available.
pub token_count: Option<u64>,
}
/// An opaque handle to a live PTY, owned by the adapter.
///
/// The domain only needs an identity to address the PTY in subsequent calls;
@ -235,6 +268,21 @@ pub enum RemoteError {
Auth(String),
}
/// Errors from a [`SessionInspector`].
///
/// Inspection is **best-effort and optional** (CONTEXT §T6/T7): these errors
/// must never block a conversation resume. A caller routing several inspectors
/// simply treats any error as "no enriched details available".
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum InspectError {
/// No transcript was found for the requested conversation.
#[error("conversation transcript not found")]
NotFound,
/// The transcript could not be read.
#[error("conversation transcript read failed: {0}")]
Read(String),
}
/// Errors from the git [`GitPort`].
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum GitError {
@ -309,6 +357,7 @@ pub trait AgentRuntime: Send + Sync {
profile: &AgentProfile,
ctx: &PreparedContext,
cwd: &ProjectPath,
session: &SessionPlan,
) -> Result<SpawnSpec, RuntimeError>;
}
@ -707,6 +756,39 @@ pub trait GitPort: Send + Sync {
async fn push(&self, root: &ProjectPath) -> Result<(), GitError>;
}
/// **Optional** capability to read enriched details from a CLI's conversation
/// transcript (CONTEXT §T6).
///
/// This port is optional *by construction*: it backs a best-effort resume popup
/// and is never wired as a hard dependency of the launch/resume flow. A caller
/// (the future `InspectConversation` use case, §T7) holds a
/// `Vec<Arc<dyn SessionInspector>>` and routes a profile to the first inspector
/// whose [`supports`](Self::supports) returns `true`; if none matches, or the
/// call errors, the resume proceeds with no enriched details.
///
/// All Claude/Codex/Gemini transcript shapes stay **inside the adapter**: no
/// CLI-specific type ever crosses this boundary — only [`ConversationDetails`].
#[async_trait]
pub trait SessionInspector: Send + Sync {
/// Returns whether this inspector knows how to read transcripts for the
/// given profile (e.g. by recognising its context-injection convention).
fn supports(&self, profile: &AgentProfile) -> bool;
/// Reads best-effort [`ConversationDetails`] for `conversation_id` whose
/// agent runs in `cwd`.
///
/// # Errors
/// [`InspectError::NotFound`] when no transcript exists for the conversation;
/// [`InspectError::Read`] on an I/O / decoding failure. Malformed *lines*
/// inside an otherwise-readable transcript are skipped, not surfaced.
async fn details(
&self,
profile: &AgentProfile,
conversation_id: &str,
cwd: &ProjectPath,
) -> Result<ConversationDetails, InspectError>;
}
/// Publish/subscribe domain events. Synchronous, in-process.
pub trait EventBus: Send + Sync {
/// Publishes an event.

View File

@ -76,11 +76,54 @@ impl ContextInjection {
}
}
/// Declares how IdeA assigns and resumes a CLI conversation, without parsing
/// the model's output (universal, declarative — see CONTEXT.md §9).
///
/// Optional on a profile: a profile without a `session` block behaves as today
/// (no resume).
///
/// Invariants:
/// - `resume_flag` is non-empty,
/// - if `assign_flag` is `Some`, it is non-empty.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SessionStrategy {
/// Flag passed on the FIRST launch with a UUID generated by IdeA
/// (e.g. `"--session-id"`). `None` => degraded mode (resume without id).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub assign_flag: Option<String>,
/// Resume flag (e.g. `"--resume"` or `"--continue"`).
pub resume_flag: String,
}
impl SessionStrategy {
/// Builds a validated session strategy.
///
/// # Errors
/// Returns [`DomainError::EmptyField`] if `resume_flag` is empty, or if
/// `assign_flag` is `Some` but empty.
pub fn new(
assign_flag: Option<String>,
resume_flag: impl Into<String>,
) -> Result<Self, DomainError> {
let resume_flag = resume_flag.into();
crate::validation::non_empty(&resume_flag, "session.resumeFlag")?;
if let Some(flag) = &assign_flag {
crate::validation::non_empty(flag, "session.assignFlag")?;
}
Ok(Self {
assign_flag,
resume_flag,
})
}
}
/// Declarative runtime configuration for one AI CLI.
///
/// Invariants:
/// - `name` and `command` non-empty,
/// - `context_injection` is itself valid (guaranteed by its constructors).
/// - `context_injection` is itself valid (guaranteed by its constructors),
/// - `session` is itself valid (guaranteed by its constructor).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentProfile {
@ -101,6 +144,10 @@ pub struct AgentProfile {
/// **never** the project root, so that N agents of the same profile never
/// collide on a single conventional file (`CLAUDE.md`, …) at the root.
pub cwd_template: String,
/// Optional conversation assign/resume strategy. Absent (legacy / no resume)
/// keeps today's behaviour.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session: Option<SessionStrategy>,
}
impl AgentProfile {
@ -117,6 +164,7 @@ impl AgentProfile {
context_injection: ContextInjection,
detect: Option<String>,
cwd_template: impl Into<String>,
session: Option<SessionStrategy>,
) -> Result<Self, DomainError> {
let name = name.into();
let command = command.into();
@ -131,6 +179,7 @@ impl AgentProfile {
context_injection,
detect,
cwd_template,
session,
})
}
}

View File

@ -5,8 +5,8 @@ mod helpers;
use domain::{
Agent, AgentManifest, AgentOrigin, AgentProfile, AgentTemplate, ContextInjection, DomainError,
ManifestEntry, MarkdownDoc, ProfileId, Project, ProjectPath, PtySize, RemoteRef, Skill, SkillId,
SkillRef, SkillScope, SshAuth, TemplateId, TemplateVersion,
ManifestEntry, MarkdownDoc, ProfileId, Project, ProjectPath, PtySize, RemoteRef, SessionStrategy,
Skill, SkillId, SkillRef, SkillScope, SshAuth, TemplateId, TemplateVersion,
};
use helpers::{AtomicSeqIdGenerator, FixedClock};
use uuid::Uuid;
@ -180,6 +180,7 @@ fn profile_valid() {
ci_stdin(),
Some("claude --version".into()),
"{projectRoot}",
None,
);
assert!(p.is_ok());
}
@ -194,6 +195,7 @@ fn profile_rejects_empty_command() {
ci_stdin(),
None,
"{projectRoot}",
None,
)
.unwrap_err();
assert!(matches!(err, DomainError::EmptyField { field } if field == "profile.command"));
@ -201,11 +203,36 @@ fn profile_rejects_empty_command() {
#[test]
fn profile_rejects_empty_name() {
let err = AgentProfile::new(profile_id(), "", "claude", vec![], ci_stdin(), None, "{r}")
let err = AgentProfile::new(profile_id(), "", "claude", vec![], ci_stdin(), None, "{r}", None)
.unwrap_err();
assert!(matches!(err, DomainError::EmptyField { field } if field == "profile.name"));
}
#[test]
fn session_strategy_valid_with_assign_flag() {
let s = SessionStrategy::new(Some("--session-id".into()), "--resume");
assert!(s.is_ok());
}
#[test]
fn session_strategy_valid_without_assign_flag() {
let s = SessionStrategy::new(None, "--continue").unwrap();
assert_eq!(s.assign_flag, None);
assert_eq!(s.resume_flag, "--continue");
}
#[test]
fn session_strategy_rejects_empty_resume_flag() {
let err = SessionStrategy::new(Some("--session-id".into()), "").unwrap_err();
assert!(matches!(err, DomainError::EmptyField { field } if field == "session.resumeFlag"));
}
#[test]
fn session_strategy_rejects_empty_assign_flag() {
let err = SessionStrategy::new(Some(String::new()), "--resume").unwrap_err();
assert!(matches!(err, DomainError::EmptyField { field } if field == "session.assignFlag"));
}
// ---------------------------------------------------------------------------
// RemoteRef invariants
// ---------------------------------------------------------------------------

View File

@ -19,9 +19,36 @@ fn leaf(id: u128, sess: Option<u128>) -> LeafCell {
id: node(id),
session: sess.map(session),
agent: None,
conversation_id: None,
agent_was_running: false,
}
}
/// A leaf pre-populated with the two resume fields, used by the
/// non-regression tests that assert these fields survive other operations.
fn leaf_with_resume(id: u128, sess: Option<u128>) -> LeafCell {
LeafCell {
id: node(id),
session: sess.map(session),
agent: None,
conversation_id: Some("conv-1".to_string()),
agent_was_running: true,
}
}
/// Walks the public tree to fetch the full [`LeafCell`] for `id`.
fn leaf_for(tree: &LayoutTree, id: domain::NodeId) -> Option<LeafCell> {
fn walk(n: &LayoutNode, id: domain::NodeId) -> Option<LeafCell> {
match n {
LayoutNode::Leaf(l) if l.id == id => Some(l.clone()),
LayoutNode::Leaf(_) => None,
LayoutNode::Split(s) => s.children.iter().find_map(|c| walk(&c.node, id)),
LayoutNode::Grid(g) => g.cells.iter().find_map(|c| walk(&c.node, id)),
}
}
walk(&tree.root, id)
}
fn single(id: u128, sess: Option<u128>) -> LayoutTree {
LayoutTree::single(leaf(id, sess))
}
@ -493,3 +520,372 @@ fn set_cell_agent_preserves_session() {
_ => panic!("expected leaf"),
}
}
// ---------------------------------------------------------------------------
// set_cell_conversation (resume: persistent CLI conversation id)
// ---------------------------------------------------------------------------
#[test]
fn set_cell_conversation_attaches_to_leaf() {
let tree = single(1, None);
let out = tree
.set_cell_conversation(node(1), Some("conv-xyz".to_string()))
.unwrap();
let l = leaf_for(&out, node(1)).expect("leaf");
assert_eq!(l.conversation_id, Some("conv-xyz".to_string()));
}
#[test]
fn set_cell_conversation_clears_with_none() {
let tree = LayoutTree::single(leaf_with_resume(1, None));
let out = tree.set_cell_conversation(node(1), None).unwrap();
let l = leaf_for(&out, node(1)).expect("leaf");
assert_eq!(l.conversation_id, None);
}
#[test]
fn set_cell_conversation_targets_correct_leaf() {
// Two leaves; only leaf 2 should receive the conversation id.
let tree = two_leaves(None, None);
let out = tree
.set_cell_conversation(node(2), Some("conv-2".to_string()))
.unwrap();
assert_eq!(
leaf_for(&out, node(1)).unwrap().conversation_id,
None,
"untouched leaf must stay clear"
);
assert_eq!(
leaf_for(&out, node(2)).unwrap().conversation_id,
Some("conv-2".to_string())
);
}
#[test]
fn set_cell_conversation_is_immutable_source_unchanged() {
let tree = single(1, None);
let before = tree.clone();
let _ = tree
.set_cell_conversation(node(1), Some("conv-1".to_string()))
.unwrap();
assert_eq!(tree, before, "source tree must not be mutated");
}
#[test]
fn set_cell_conversation_missing_leaf_is_node_not_found() {
let tree = single(1, None);
let err = tree
.set_cell_conversation(node(404), Some("conv".to_string()))
.unwrap_err();
assert_eq!(err, LayoutError::NodeNotFound(node(404)));
}
#[test]
fn set_cell_conversation_preserves_session_and_agent_running() {
// conversation id must coexist with session and agent_was_running.
let tree = LayoutTree::single(leaf_with_resume(1, Some(100)));
let out = tree
.set_cell_conversation(node(1), Some("conv-new".to_string()))
.unwrap();
let l = leaf_for(&out, node(1)).expect("leaf");
assert_eq!(l.session, Some(session(100)));
assert_eq!(l.agent_was_running, true);
assert_eq!(l.conversation_id, Some("conv-new".to_string()));
}
// ---------------------------------------------------------------------------
// set_agent_running (resume: agent process state at close)
// ---------------------------------------------------------------------------
#[test]
fn set_agent_running_sets_true_then_false() {
let tree = single(1, None);
let out = tree.set_agent_running(node(1), true).unwrap();
assert_eq!(leaf_for(&out, node(1)).unwrap().agent_was_running, true);
let out2 = out.set_agent_running(node(1), false).unwrap();
assert_eq!(leaf_for(&out2, node(1)).unwrap().agent_was_running, false);
}
#[test]
fn set_agent_running_targets_correct_leaf() {
let tree = two_leaves(None, None);
let out = tree.set_agent_running(node(2), true).unwrap();
assert_eq!(
leaf_for(&out, node(1)).unwrap().agent_was_running,
false,
"untouched leaf must stay false"
);
assert_eq!(leaf_for(&out, node(2)).unwrap().agent_was_running, true);
}
// ---------------------------------------------------------------------------
// agent_leaves (close-time snapshot traversal)
// ---------------------------------------------------------------------------
#[test]
fn agent_leaves_empty_when_no_agent() {
let tree = two_leaves(Some(1), None);
assert!(tree.agent_leaves().is_empty());
}
#[test]
fn agent_leaves_collects_only_agent_bearing_leaves() {
// Split: leaf 1 with agent 100, leaf 2 plain, leaf 3 with agent 101.
let tree = LayoutTree::new(LayoutNode::Split(SplitContainer {
id: node(99),
direction: Direction::Row,
children: vec![
WeightedChild {
node: LayoutNode::Leaf(LeafCell {
id: node(1),
session: None,
agent: Some(agent_id(100)),
conversation_id: None,
agent_was_running: false,
}),
weight: 1.0,
},
WeightedChild {
node: LayoutNode::Leaf(leaf(2, None)),
weight: 1.0,
},
WeightedChild {
node: LayoutNode::Leaf(LeafCell {
id: node(3),
session: None,
agent: Some(agent_id(101)),
conversation_id: None,
agent_was_running: false,
}),
weight: 1.0,
},
],
}));
let mut found = tree.agent_leaves();
found.sort_by_key(|(n, _)| *n);
assert_eq!(
found,
vec![(node(1), agent_id(100)), (node(3), agent_id(101))]
);
}
#[test]
fn set_agent_running_is_immutable_source_unchanged() {
let tree = single(1, None);
let before = tree.clone();
let _ = tree.set_agent_running(node(1), true).unwrap();
assert_eq!(tree, before, "source tree must not be mutated");
}
#[test]
fn set_agent_running_missing_leaf_is_node_not_found() {
let tree = single(1, None);
let err = tree.set_agent_running(node(404), true).unwrap_err();
assert_eq!(err, LayoutError::NodeNotFound(node(404)));
}
#[test]
fn set_agent_running_preserves_session_and_conversation() {
let tree = LayoutTree::single(leaf_with_resume(1, Some(100)));
let out = tree.set_agent_running(node(1), false).unwrap();
let l = leaf_for(&out, node(1)).expect("leaf");
assert_eq!(l.session, Some(session(100)));
assert_eq!(l.conversation_id, Some("conv-1".to_string()));
assert_eq!(l.agent_was_running, false);
}
// ---------------------------------------------------------------------------
// NON-REGRESSION (piège n°1): the resume fields (conversation_id,
// agent_was_running) MUST survive every leaf-rebuilding operation.
// ---------------------------------------------------------------------------
#[test]
fn set_session_preserves_resume_fields() {
// leaf starts with conversation_id = Some("conv-1") and agent_was_running = true.
let tree = LayoutTree::single(leaf_with_resume(1, None));
let out = tree.set_session(node(1), Some(session(100))).unwrap();
let l = leaf_for(&out, node(1)).expect("leaf");
assert_eq!(l.session, Some(session(100)));
assert_eq!(
l.conversation_id,
Some("conv-1".to_string()),
"set_session must NOT erase conversation_id"
);
assert_eq!(
l.agent_was_running, true,
"set_session must NOT erase agent_was_running"
);
}
#[test]
fn set_cell_agent_preserves_resume_fields() {
let tree = LayoutTree::single(leaf_with_resume(1, None));
let out = tree.set_cell_agent(node(1), Some(agent_id(42))).unwrap();
let l = leaf_for(&out, node(1)).expect("leaf");
assert_eq!(l.agent, Some(agent_id(42)));
assert_eq!(
l.conversation_id,
Some("conv-1".to_string()),
"set_cell_agent must NOT erase conversation_id"
);
assert_eq!(
l.agent_was_running, true,
"set_cell_agent must NOT erase agent_was_running"
);
}
#[test]
fn move_session_preserves_resume_fields_on_both_leaves() {
// from-leaf carries a session + resume fields; to-leaf is empty but ALSO
// carries resume fields. After the move, BOTH leaves must keep their own
// conversation_id / agent_was_running (only the session moves).
let tree = LayoutTree::new(LayoutNode::Split(SplitContainer {
id: node(9),
direction: Direction::Row,
children: vec![
WeightedChild {
node: LayoutNode::Leaf(LeafCell {
id: node(1),
session: Some(session(100)),
agent: None,
conversation_id: Some("conv-from".to_string()),
agent_was_running: true,
}),
weight: 1.0,
},
WeightedChild {
node: LayoutNode::Leaf(LeafCell {
id: node(2),
session: None,
agent: None,
conversation_id: Some("conv-to".to_string()),
agent_was_running: true,
}),
weight: 1.0,
},
],
}));
let out = tree.move_session(node(1), node(2)).unwrap();
let from = leaf_for(&out, node(1)).expect("from leaf");
assert_eq!(from.session, None, "session left the from-leaf");
assert_eq!(
from.conversation_id,
Some("conv-from".to_string()),
"move_session must NOT erase from-leaf conversation_id"
);
assert_eq!(
from.agent_was_running, true,
"move_session must NOT erase from-leaf agent_was_running"
);
let to = leaf_for(&out, node(2)).expect("to leaf");
assert_eq!(to.session, Some(session(100)), "session arrived at to-leaf");
assert_eq!(
to.conversation_id,
Some("conv-to".to_string()),
"move_session must NOT erase to-leaf conversation_id"
);
assert_eq!(
to.agent_was_running, true,
"move_session must NOT erase to-leaf agent_was_running"
);
}
// ---------------------------------------------------------------------------
// serde: compat ascendante & combinaisons des nouveaux champs
// ---------------------------------------------------------------------------
#[test]
fn leaf_serde_all_four_combinations_roundtrip() {
let combos = [
(None, false),
(Some("c".to_string()), false),
(None, true),
(Some("c".to_string()), true),
];
for (conv, running) in combos {
let cell = LeafCell {
id: node(1),
session: None,
agent: None,
conversation_id: conv.clone(),
agent_was_running: running,
};
let json = serde_json::to_string(&cell).unwrap();
let back: LeafCell = serde_json::from_str(&json).unwrap();
assert_eq!(back, cell, "roundtrip failed for {conv:?}/{running}");
}
}
#[test]
fn leaf_serde_omits_defaults() {
let cell = LeafCell {
id: node(1),
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
};
let json = serde_json::to_string(&cell).unwrap();
assert!(
!json.contains("conversationId"),
"default conversation_id must be omitted; json was {json}"
);
assert!(
!json.contains("agentWasRunning"),
"default agent_was_running must be omitted; json was {json}"
);
}
#[test]
fn leaf_serde_field_names_are_camel_case_when_present() {
let cell = LeafCell {
id: node(1),
session: None,
agent: None,
conversation_id: Some("c".to_string()),
agent_was_running: true,
};
let json = serde_json::to_string(&cell).unwrap();
assert!(json.contains("conversationId"), "json was {json}");
assert!(json.contains("agentWasRunning"), "json was {json}");
}
#[test]
fn legacy_leaf_json_deserialises_to_defaults() {
// A leaf persisted before these fields existed (only id present).
let json = r#"{"id":"00000000-0000-0000-0000-000000000001"}"#;
let cell: LeafCell = serde_json::from_str(json).unwrap();
assert_eq!(cell.conversation_id, None);
assert_eq!(cell.agent_was_running, false);
assert_eq!(cell.session, None);
assert_eq!(cell.agent, None);
}
#[test]
fn leaf_can_carry_conversation_without_session_and_inversely() {
// conversation_id present, no session.
let a = LeafCell {
id: node(1),
session: None,
agent: None,
conversation_id: Some("c".to_string()),
agent_was_running: false,
};
let a_back: LeafCell = serde_json::from_str(&serde_json::to_string(&a).unwrap()).unwrap();
assert_eq!(a_back, a);
// session present, no conversation_id.
let b = LeafCell {
id: node(2),
session: Some(session(7)),
agent: None,
conversation_id: None,
agent_was_running: false,
};
let b_back: LeafCell = serde_json::from_str(&serde_json::to_string(&b).unwrap()).unwrap();
assert_eq!(b_back, b);
}

View File

@ -6,7 +6,8 @@ mod helpers;
use domain::{
Agent, AgentManifest, AgentOrigin, AgentProfile, AgentTemplate, ContextInjection, Direction,
LayoutNode, LayoutTree, LeafCell, ManifestEntry, MarkdownDoc, Project, ProjectPath, RemoteRef,
Skill, SkillId, SkillRef, SkillScope, SplitContainer, SshAuth, TemplateVersion, WeightedChild,
SessionStrategy, Skill, SkillId, SkillRef, SkillScope, SplitContainer, SshAuth, TemplateVersion,
WeightedChild,
};
use helpers::{node, session};
use uuid::Uuid;
@ -108,6 +109,7 @@ fn profile_roundtrip_all_injection_variants() {
ci,
Some("claude --version".into()),
"{projectRoot}",
None,
)
.unwrap();
assert_eq!(roundtrip(&p), p);
@ -133,12 +135,78 @@ fn profile_cwd_template_is_camel_case() {
ContextInjection::stdin(),
None,
"{projectRoot}",
None,
)
.unwrap();
let json = serde_json::to_string(&p).unwrap();
assert!(json.contains("\"cwdTemplate\""), "json was {json}");
}
#[test]
fn profile_with_session_roundtrips_and_uses_camel_case() {
let session = SessionStrategy::new(Some("--session-id".into()), "--resume").unwrap();
let p = AgentProfile::new(
profid(1),
"Claude Code",
"claude",
vec![],
ContextInjection::stdin(),
None,
"{agentRunDir}",
Some(session),
)
.unwrap();
let json = serde_json::to_string(&p).unwrap();
assert!(
json.contains("\"session\":{\"assignFlag\":\"--session-id\",\"resumeFlag\":\"--resume\"}"),
"json was {json}"
);
assert_eq!(roundtrip(&p), p);
}
#[test]
fn profile_without_session_omits_the_field() {
let p = AgentProfile::new(
profid(1),
"n",
"c",
vec![],
ContextInjection::stdin(),
None,
"{projectRoot}",
None,
)
.unwrap();
let json = serde_json::to_string(&p).unwrap();
assert!(!json.contains("session"), "json was {json}");
assert_eq!(roundtrip(&p), p);
}
#[test]
fn legacy_profile_without_session_deserialises_to_none() {
// A `profiles.json` produced before the `session` field existed.
let json = r#"{
"id": "00000000-0000-0000-0000-000000000001",
"name": "Claude Code",
"command": "claude",
"args": [],
"contextInjection": { "strategy": "stdin" },
"detect": null,
"cwdTemplate": "{agentRunDir}"
}"#;
let p: AgentProfile = serde_json::from_str(json).expect("legacy profile must deserialise");
assert_eq!(p.session, None);
}
#[test]
fn session_assign_flag_omitted_when_none() {
let session = SessionStrategy::new(None, "--continue").unwrap();
let json = serde_json::to_string(&session).unwrap();
assert!(!json.contains("assignFlag"), "json was {json}");
assert!(json.contains("\"resumeFlag\":\"--continue\""), "json was {json}");
assert_eq!(roundtrip(&session), session);
}
// ---------------------------------------------------------------------------
// AgentTemplate
// ---------------------------------------------------------------------------
@ -299,6 +367,8 @@ fn layout_roundtrip() {
id: node(1),
session: Some(session(100)),
agent: None,
conversation_id: None,
agent_was_running: false,
}),
weight: 1.5,
},
@ -307,6 +377,8 @@ fn layout_roundtrip() {
id: node(2),
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
}),
weight: 2.5,
},
@ -330,6 +402,8 @@ fn leaf_with_agent_roundtrip_and_omits_null() {
id: node(1),
session: None,
agent: Some(AgentId::from_uuid(agent_uuid)),
conversation_id: None,
agent_was_running: false,
}));
let rt = roundtrip(&tree);
match rt.root {
@ -344,6 +418,8 @@ fn leaf_with_agent_roundtrip_and_omits_null() {
id: node(2),
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
}));
let json2 = serde_json::to_string(&tree_no_agent).unwrap();
assert!(!json2.contains("\"agent\""), "agent field should be omitted when None; json was {json2}");

View File

@ -19,6 +19,8 @@ fn leaf_tree() -> LayoutTree {
id: NodeId::from_uuid(Uuid::from_u128(900)),
session: None,
agent: None,
conversation_id: None,
agent_was_running: false,
}))
}
fn tab(n: u128) -> Tab {

View File

@ -0,0 +1,364 @@
//! [`ClaudeTranscriptInspector`] — a best-effort [`SessionInspector`] adapter
//! (CONTEXT §T6) for the **Claude Code** CLI.
//!
//! Claude Code records each conversation as a JSONL transcript under the user's
//! home directory:
//!
//! ```text
//! <home>/.claude/projects/<encoded-cwd>/<conversation-id>.jsonl
//! ```
//!
//! where `<encoded-cwd>` is the agent's working directory with its path
//! separators flattened to `-` (see [`encode_cwd`]). Each line of the `.jsonl`
//! is one JSON message object.
//!
//! This adapter is the **only** place that knows the Claude transcript shape:
//! it reads the file through the injected [`FileSystem`] port, extracts a
//! best-effort `last_topic` (the last `user` message text) and `token_count`
//! (the cumulative `usage` input+output tokens of `assistant` messages), and
//! returns the CLI-agnostic [`ConversationDetails`]. No Claude-specific type
//! ever leaves this module. Anything it cannot parse is skipped, never fatal.
use std::sync::Arc;
use async_trait::async_trait;
use serde::Deserialize;
use domain::ports::{
ConversationDetails, FileSystem, FsError, InspectError, RemotePath, SessionInspector,
};
use domain::profile::{AgentProfile, ContextInjection};
use domain::project::ProjectPath;
/// Max byte length of the extracted `last_topic` before it is truncated.
///
/// Kept small: the topic only labels a resume popup, it is not the message.
const TOPIC_MAX_LEN: usize = 120;
/// Reads Claude Code conversation transcripts.
///
/// Composes a [`FileSystem`] port (so it stays Tauri- and OS-agnostic and is
/// trivially testable) plus the base directory that stands in for the user's
/// home (`<home>/.claude/projects/`). The composition root passes the real
/// `$HOME`; tests pass a temp directory via [`with_base_dir`](Self::with_base_dir).
#[derive(Clone)]
pub struct ClaudeTranscriptInspector {
fs: Arc<dyn FileSystem>,
/// Directory that plays the role of the user's home directory; the adapter
/// looks under `<home>/.claude/projects/`.
home_dir: String,
}
impl ClaudeTranscriptInspector {
/// Builds the inspector from an injected [`FileSystem`] and the user's home
/// directory (resolved by the composition root, e.g. from `$HOME`).
#[must_use]
pub fn new(fs: Arc<dyn FileSystem>, home_dir: impl Into<String>) -> Self {
Self {
fs,
home_dir: home_dir.into(),
}
}
/// Test/seam constructor: identical to [`new`](Self::new) but named to make
/// the injected base directory explicit at call sites (tests point it at a
/// temp `~/.claude/projects/` fixture).
#[must_use]
pub fn with_base_dir(fs: Arc<dyn FileSystem>, home_dir: impl Into<String>) -> Self {
Self::new(fs, home_dir)
}
/// `<home>/.claude/projects/<encoded-cwd>/<conversation-id>.jsonl`.
fn transcript_path(&self, conversation_id: &str, cwd: &ProjectPath) -> RemotePath {
let base = self.home_dir.trim_end_matches(['/', '\\']);
let encoded = encode_cwd(cwd.as_str());
RemotePath::new(format!(
"{base}/.claude/projects/{encoded}/{conversation_id}.jsonl"
))
}
}
/// Encodes a cwd the way Claude Code names its per-project transcript folder.
///
/// **Assumption (documented, isolated, and unit-tested):** Claude flattens the
/// absolute cwd into a single directory segment by replacing each path
/// separator (`/` or `\`) — and the leading separator — with `-`. So
/// `/home/anthony/Documents/Projects/IdeA` becomes
/// `-home-anthony-Documents-Projects-IdeA`. We keep this convention in one
/// small, tested function so that if Claude's exact encoding differs in some
/// edge case, only this seam needs to change. We do not attempt to encode `.`
/// or other characters, as the common case (clean absolute project paths) is
/// covered and over-encoding would risk pointing at the wrong folder.
fn encode_cwd(cwd: &str) -> String {
cwd.chars()
.map(|c| if c == '/' || c == '\\' { '-' } else { c })
.collect()
}
/// One transcript line, as far as we care about it. Everything is optional so
/// that an unexpected/partial line never fails deserialization of the fields we
/// *can* read; truly unparseable lines are skipped by the caller.
#[derive(Debug, Deserialize)]
struct TranscriptLine {
#[serde(default)]
role: Option<String>,
/// Some Claude transcript variants nest the chat turn under `message`.
#[serde(default)]
message: Option<InnerMessage>,
#[serde(default)]
content: Option<Content>,
#[serde(default)]
usage: Option<Usage>,
}
/// The nested `{ role, content, usage }` object some transcript shapes use.
#[derive(Debug, Deserialize)]
struct InnerMessage {
#[serde(default)]
role: Option<String>,
#[serde(default)]
content: Option<Content>,
#[serde(default)]
usage: Option<Usage>,
}
/// Message content is either a plain string or an array of typed blocks.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Content {
/// `"content": "hello"`.
Text(String),
/// `"content": [{ "type": "text", "text": "hello" }, ...]`.
Blocks(Vec<ContentBlock>),
}
impl Content {
/// Flattens the content to plain text (concatenating text blocks).
fn to_text(&self) -> String {
match self {
Self::Text(s) => s.clone(),
Self::Blocks(blocks) => blocks
.iter()
.filter_map(|b| b.text.as_deref())
.collect::<Vec<_>>()
.join(" "),
}
}
}
/// A single content block; we only care about text blocks.
#[derive(Debug, Deserialize)]
struct ContentBlock {
#[serde(default)]
text: Option<String>,
}
/// Token usage as Claude reports it on assistant turns.
#[derive(Debug, Deserialize)]
struct Usage {
#[serde(default)]
input_tokens: Option<u64>,
#[serde(default)]
output_tokens: Option<u64>,
}
impl TranscriptLine {
/// The effective role, looking at the top level then the nested message.
fn effective_role(&self) -> Option<&str> {
self.role
.as_deref()
.or_else(|| self.message.as_ref().and_then(|m| m.role.as_deref()))
}
/// The effective content, top level then nested.
fn effective_content(&self) -> Option<&Content> {
self.content
.as_ref()
.or_else(|| self.message.as_ref().and_then(|m| m.content.as_ref()))
}
/// The effective usage, top level then nested.
fn effective_usage(&self) -> Option<&Usage> {
self.usage
.as_ref()
.or_else(|| self.message.as_ref().and_then(|m| m.usage.as_ref()))
}
}
/// Parses an already-read transcript body (the JSONL bytes) into best-effort
/// [`ConversationDetails`]. Pulled out of the async `details` so it is a pure,
/// directly unit-testable function. Malformed lines are silently skipped.
fn parse_transcript(body: &[u8]) -> ConversationDetails {
let text = String::from_utf8_lossy(body);
let mut last_topic: Option<String> = None;
let mut token_total: u64 = 0;
let mut saw_usage = false;
for line in text.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
// Malformed line → skip, never fatal.
let Ok(parsed) = serde_json::from_str::<TranscriptLine>(line) else {
continue;
};
let role = parsed.effective_role();
// last_topic = the last user message's text (best-effort).
if role == Some("user") {
if let Some(content) = parsed.effective_content() {
let t = content.to_text();
let t = t.trim();
if !t.is_empty() {
last_topic = Some(truncate_topic(t));
}
}
}
// token_count = cumulative usage across assistant turns (best-effort).
if let Some(usage) = parsed.effective_usage() {
let input = usage.input_tokens.unwrap_or(0);
let output = usage.output_tokens.unwrap_or(0);
if input != 0 || output != 0 {
saw_usage = true;
token_total = token_total
.saturating_add(input)
.saturating_add(output);
}
}
}
ConversationDetails {
last_topic,
token_count: saw_usage.then_some(token_total),
}
}
/// Truncates a topic to [`TOPIC_MAX_LEN`] bytes on a char boundary, appending an
/// ellipsis when cut.
fn truncate_topic(s: &str) -> String {
if s.len() <= TOPIC_MAX_LEN {
return s.to_owned();
}
let mut end = TOPIC_MAX_LEN;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
format!("{}", &s[..end])
}
#[async_trait]
impl SessionInspector for ClaudeTranscriptInspector {
fn supports(&self, profile: &AgentProfile) -> bool {
// Recognise Claude by its conventional context file `CLAUDE.md`,
// mirroring the detection in `application::agent::lifecycle`.
matches!(
&profile.context_injection,
ContextInjection::ConventionFile { target }
if target
.rsplit(['/', '\\'])
.next()
.unwrap_or(target)
.eq_ignore_ascii_case("CLAUDE.md")
)
}
async fn details(
&self,
_profile: &AgentProfile,
conversation_id: &str,
cwd: &ProjectPath,
) -> Result<ConversationDetails, InspectError> {
let path = self.transcript_path(conversation_id, cwd);
match self.fs.read(&path).await {
Ok(bytes) => Ok(parse_transcript(&bytes)),
Err(FsError::NotFound(_)) => Err(InspectError::NotFound),
Err(e) => Err(InspectError::Read(e.to_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encode_cwd_flattens_separators_to_dash() {
assert_eq!(
encode_cwd("/home/anthony/Documents/Projects/IdeA"),
"-home-anthony-Documents-Projects-IdeA"
);
assert_eq!(encode_cwd("/a/b"), "-a-b");
// Windows-style separators flatten too.
assert_eq!(encode_cwd("C:\\Users\\me"), "C:-Users-me");
}
#[test]
fn parse_extracts_last_user_topic_and_token_sum() {
let body = concat!(
r#"{"role":"user","content":"first question"}"#,
"\n",
r#"{"role":"assistant","content":"hi","usage":{"input_tokens":10,"output_tokens":5}}"#,
"\n",
r#"{"role":"user","content":"second question"}"#,
"\n",
r#"{"role":"assistant","content":"there","usage":{"input_tokens":20,"output_tokens":7}}"#,
"\n",
);
let d = parse_transcript(body.as_bytes());
assert_eq!(d.last_topic.as_deref(), Some("second question"));
assert_eq!(d.token_count, Some(42));
}
#[test]
fn parse_skips_malformed_lines_without_panicking() {
let body = concat!(
r#"{"role":"user","content":"valid one"}"#,
"\n",
"this is not json at all",
"\n",
r#"{"role":"assistant","usage":{"input_tokens":3,"output_tokens":4}}"#,
"\n",
"{ broken json",
"\n",
);
let d = parse_transcript(body.as_bytes());
assert_eq!(d.last_topic.as_deref(), Some("valid one"));
assert_eq!(d.token_count, Some(7));
}
#[test]
fn parse_handles_nested_message_and_block_content() {
let body = concat!(
r#"{"type":"user","message":{"role":"user","content":[{"type":"text","text":"nested topic"}]}}"#,
"\n",
r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"ok"}],"usage":{"input_tokens":100,"output_tokens":50}}}"#,
"\n",
);
let d = parse_transcript(body.as_bytes());
assert_eq!(d.last_topic.as_deref(), Some("nested topic"));
assert_eq!(d.token_count, Some(150));
}
#[test]
fn parse_returns_none_when_no_topic_or_usage() {
let body = concat!(
r#"{"role":"assistant","content":"no usage here"}"#,
"\n",
);
let d = parse_transcript(body.as_bytes());
assert_eq!(d.last_topic, None);
assert_eq!(d.token_count, None);
}
#[test]
fn truncate_topic_cuts_long_text() {
let long = "x".repeat(TOPIC_MAX_LEN + 50);
let t = truncate_topic(&long);
assert!(t.ends_with('…'));
assert!(t.len() <= TOPIC_MAX_LEN + 4);
}
}

View File

@ -0,0 +1,12 @@
//! Best-effort conversation-transcript inspectors (CONTEXT §T6).
//!
//! These adapters implement the **optional** [`domain::ports::SessionInspector`]
//! port: they read a CLI's on-disk transcript to enrich a resume popup (last
//! topic, token count). Each CLI's transcript format stays fully encapsulated in
//! its adapter; only the CLI-agnostic [`domain::ports::ConversationDetails`]
//! crosses the port boundary. Nothing here is wired as a hard dependency — a
//! missing or failing inspector must never block a resume.
mod claude;
pub use claude::ClaudeTranscriptInspector;

View File

@ -17,6 +17,7 @@ pub mod eventbus;
pub mod fs;
pub mod git;
pub mod id;
pub mod inspector;
pub mod orchestrator;
pub mod process;
pub mod pty;
@ -29,6 +30,7 @@ pub use eventbus::TokioBroadcastEventBus;
pub use fs::LocalFileSystem;
pub use git::Git2Repository;
pub use id::UuidGenerator;
pub use inspector::ClaudeTranscriptInspector;
pub use orchestrator::{
process_request_file, FsOrchestratorWatcher, OrchestratorResponse, OrchestratorWatchHandle,
REQUESTS_SUBDIR,

View File

@ -22,9 +22,10 @@ use std::sync::Arc;
use async_trait::async_trait;
use domain::ports::{
AgentRuntime, ContextInjectionPlan, PreparedContext, ProcessSpawner, RuntimeError, SpawnSpec,
AgentRuntime, ContextInjectionPlan, PreparedContext, ProcessSpawner, RuntimeError, SessionPlan,
SpawnSpec,
};
use domain::profile::{AgentProfile, ContextInjection};
use domain::profile::{AgentProfile, ContextInjection, SessionStrategy};
use domain::project::ProjectPath;
/// The single generic AI-runtime adapter. Holds a [`ProcessSpawner`] (used only
@ -140,6 +141,40 @@ impl CliAgentRuntime {
ContextInjection::Env { var } => ContextInjectionPlan::Env { var: var.clone() },
}
}
/// Composes the session resume/assign arguments for a launch. **Pure** — the
/// testable heart of T3, mirroring [`injection_plan`](Self::injection_plan).
///
/// The profile's optional [`SessionStrategy`] crossed with the per-launch
/// [`SessionPlan`] yields the args to append (exhaustive truth table):
///
/// | `profile.session` | `SessionPlan` | Args added |
/// |----------------------------------------|----------------|------------|
/// | `None` | any | `[]` |
/// | `Some{assign_flag: Some(f), ..}` | `Assign{id}` | `[f, id]` |
/// | `Some{resume_flag: r, ..}` | `Resume{id}` | `[r, id]` |
/// | `Some{assign_flag: None, resume_flag: r}` | `Resume{id}` | `[r]` (degraded) |
/// | `Some{..}` | `None` | `[]` |
/// | `Some{assign_flag: None, ..}` | `Assign{id}` | `[]` (no flag) |
fn session_args(session: Option<&SessionStrategy>, plan: &SessionPlan) -> Vec<String> {
let Some(strategy) = session else {
return Vec::new();
};
match plan {
SessionPlan::None => Vec::new(),
SessionPlan::Assign { conversation_id } => match &strategy.assign_flag {
Some(flag) => vec![flag.clone(), conversation_id.clone()],
None => Vec::new(),
},
SessionPlan::Resume { conversation_id } => {
let mut args = vec![strategy.resume_flag.clone()];
if strategy.assign_flag.is_some() {
args.push(conversation_id.clone());
}
args
}
}
}
}
#[async_trait]
@ -161,6 +196,7 @@ impl AgentRuntime for CliAgentRuntime {
profile: &AgentProfile,
ctx: &PreparedContext,
cwd: &ProjectPath,
session: &SessionPlan,
) -> Result<SpawnSpec, RuntimeError> {
let resolved_cwd = Self::resolve_cwd(profile, cwd)?;
let plan = Self::injection_plan(&profile.context_injection, ctx);
@ -174,6 +210,10 @@ impl AgentRuntime for CliAgentRuntime {
args.extend(extra.iter().cloned());
}
// Session resume/assign args come *after* the static + context-injection
// args, so re-opening a conversation never disturbs context delivery.
args.extend(Self::session_args(profile.session.as_ref(), session));
Ok(SpawnSpec {
command: profile.command.clone(),
args,

View File

@ -15,9 +15,9 @@ use async_trait::async_trait;
use domain::ports::{
AgentRuntime, ContextInjectionPlan, ExitStatus, Output, PreparedContext, ProcessError,
ProcessSpawner, RuntimeError, SpawnSpec,
ProcessSpawner, RuntimeError, SessionPlan, SpawnSpec,
};
use domain::profile::{AgentProfile, ContextInjection};
use domain::profile::{AgentProfile, ContextInjection, SessionStrategy};
use domain::project::ProjectPath;
use domain::ids::ProfileId;
use domain::MarkdownDoc;
@ -36,6 +36,7 @@ fn profile(injection: ContextInjection, cwd_template: &str) -> AgentProfile {
injection,
Some("mycli probe --json".to_owned()),
cwd_template,
None,
)
.unwrap()
}
@ -97,7 +98,7 @@ fn prepare_convention_file_keeps_args_and_plans_file() {
);
let root = ProjectPath::new("/home/me/proj").unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root).unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root, &SessionPlan::None).unwrap();
assert_eq!(spec.command, "mycli");
assert_eq!(spec.args, vec!["--static", "arg"], "args unchanged");
@ -123,7 +124,7 @@ fn prepare_flag_with_path_substitutes_and_splits() {
);
let root = ProjectPath::new("/p").unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root).unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root, &SessionPlan::None).unwrap();
// static args first, then the substituted+split flag args.
assert_eq!(
@ -148,7 +149,7 @@ fn prepare_flag_without_path_is_switch_then_path() {
let p = profile(ContextInjection::flag("-f").unwrap(), "{projectRoot}");
let root = ProjectPath::new("/p").unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root).unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root, &SessionPlan::None).unwrap();
assert_eq!(spec.args, vec!["--static", "arg", "-f", ".ideai/agent.md"]);
assert_eq!(
@ -169,7 +170,7 @@ fn prepare_stdin_keeps_args_and_plans_stdin() {
let p = profile(ContextInjection::stdin(), "{projectRoot}");
let root = ProjectPath::new("/p").unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root).unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root, &SessionPlan::None).unwrap();
assert_eq!(spec.args, vec!["--static", "arg"], "args unchanged for stdin");
assert_eq!(spec.context_plan, Some(ContextInjectionPlan::Stdin));
@ -188,7 +189,7 @@ fn prepare_env_keeps_args_and_plans_env() {
);
let root = ProjectPath::new("/p").unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root).unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root, &SessionPlan::None).unwrap();
assert_eq!(spec.args, vec!["--static", "arg"], "args unchanged for env");
assert_eq!(
@ -212,7 +213,7 @@ fn prepare_substitutes_project_root_in_cwd_template() {
);
let root = ProjectPath::new("/home/me/proj").unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root).unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &root, &SessionPlan::None).unwrap();
assert_eq!(spec.cwd.as_str(), "/home/me/proj/subdir");
}
@ -222,7 +223,7 @@ fn prepare_empty_cwd_template_defaults_to_base() {
let p = profile(ContextInjection::stdin(), "");
let base = ProjectPath::new("/home/me/proj").unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &base).unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &base, &SessionPlan::None).unwrap();
assert_eq!(spec.cwd.as_str(), "/home/me/proj");
}
@ -234,7 +235,7 @@ fn prepare_substitutes_agent_run_dir_in_cwd_template() {
let p = profile(ContextInjection::convention_file("CLAUDE.md").unwrap(), "{agentRunDir}");
let run_dir = ProjectPath::new("/home/me/proj/.ideai/run/agent-1").unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &run_dir).unwrap();
let spec = rt.prepare_invocation(&p, &ctx(), &run_dir, &SessionPlan::None).unwrap();
assert_eq!(spec.cwd.as_str(), "/home/me/proj/.ideai/run/agent-1");
}
@ -262,6 +263,7 @@ fn detection_spec_falls_back_to_command_version() {
ContextInjection::stdin(),
None,
"{projectRoot}",
None,
)
.unwrap();
@ -327,3 +329,190 @@ fn detect_runs_the_detection_spec_command() {
assert_eq!(spec.command, "mycli");
assert_eq!(spec.args, vec!["probe", "--json"]);
}
// ---------------------------------------------------------------------------
// prepare_invocation — session args (T3 truth table)
// ---------------------------------------------------------------------------
/// Like [`profile`] but carries a [`SessionStrategy`]. `Stdin` injection keeps
/// the context out of the args so session args are the *only* trailing tokens.
fn profile_with_session(session: Option<SessionStrategy>) -> AgentProfile {
AgentProfile::new(
ProfileId::from_uuid(uuid::Uuid::from_u128(7)),
"Test",
"mycli",
vec!["--static".to_owned(), "arg".to_owned()],
ContextInjection::stdin(),
Some("mycli probe --json".to_owned()),
"{agentRunDir}",
session,
)
.unwrap()
}
fn root() -> ProjectPath {
ProjectPath::new("/p").unwrap()
}
// Row 1: profile.session == None ⇒ no args added, whatever the plan.
#[test]
fn session_none_profile_adds_nothing_for_any_plan() {
let rt = pure_runtime();
let p = profile_with_session(None);
for plan in [
SessionPlan::None,
SessionPlan::Assign { conversation_id: "id-1".to_owned() },
SessionPlan::Resume { conversation_id: "id-1".to_owned() },
] {
let spec = rt.prepare_invocation(&p, &ctx(), &root(), &plan).unwrap();
assert_eq!(spec.args, vec!["--static", "arg"], "plan = {plan:?}");
}
}
// Row 2: Some{assign_flag: Some(f)} + Assign{id} ⇒ [f, id].
#[test]
fn session_assign_with_flag_emits_flag_and_id() {
let rt = pure_runtime();
let p = profile_with_session(Some(
SessionStrategy::new(Some("--session-id".to_owned()), "--resume").unwrap(),
));
let spec = rt
.prepare_invocation(
&p,
&ctx(),
&root(),
&SessionPlan::Assign { conversation_id: "abc".to_owned() },
)
.unwrap();
assert_eq!(spec.args, vec!["--static", "arg", "--session-id", "abc"]);
}
// Row 3: Some{resume_flag: r, assign_flag: Some} + Resume{id} ⇒ [r, id].
#[test]
fn session_resume_with_flag_emits_resume_and_id() {
let rt = pure_runtime();
let p = profile_with_session(Some(
SessionStrategy::new(Some("--session-id".to_owned()), "--resume").unwrap(),
));
let spec = rt
.prepare_invocation(
&p,
&ctx(),
&root(),
&SessionPlan::Resume { conversation_id: "abc".to_owned() },
)
.unwrap();
assert_eq!(spec.args, vec!["--static", "arg", "--resume", "abc"]);
}
// Row 4: Some{assign_flag: None, resume_flag: r} + Resume{id} ⇒ [r] (degraded).
#[test]
fn session_resume_without_assign_flag_emits_resume_only() {
let rt = pure_runtime();
let p = profile_with_session(Some(SessionStrategy::new(None, "--continue").unwrap()));
let spec = rt
.prepare_invocation(
&p,
&ctx(),
&root(),
&SessionPlan::Resume { conversation_id: "abc".to_owned() },
)
.unwrap();
assert_eq!(spec.args, vec!["--static", "arg", "--continue"]);
}
// Row 5: Some{..} + SessionPlan::None ⇒ nothing added.
#[test]
fn session_plan_none_with_strategy_adds_nothing() {
let rt = pure_runtime();
let p = profile_with_session(Some(
SessionStrategy::new(Some("--session-id".to_owned()), "--resume").unwrap(),
));
let spec = rt
.prepare_invocation(&p, &ctx(), &root(), &SessionPlan::None)
.unwrap();
assert_eq!(spec.args, vec!["--static", "arg"]);
}
// Row 6: Some{assign_flag: None} + Assign{id} ⇒ nothing (no assign possible).
#[test]
fn session_assign_without_flag_adds_nothing() {
let rt = pure_runtime();
let p = profile_with_session(Some(SessionStrategy::new(None, "--continue").unwrap()));
let spec = rt
.prepare_invocation(
&p,
&ctx(),
&root(),
&SessionPlan::Assign { conversation_id: "abc".to_owned() },
)
.unwrap();
assert_eq!(spec.args, vec!["--static", "arg"]);
}
// Non-regression: a profile WITHOUT session + SessionPlan::None yields the exact
// same args as before T3 (the existing strategy tests already assert these; here
// we pin it against an Assign/Resume plan too — still nothing added).
#[test]
fn no_session_profile_is_unaffected_by_any_plan() {
let rt = pure_runtime();
let p = profile(ContextInjection::stdin(), "{agentRunDir}");
for plan in [
SessionPlan::None,
SessionPlan::Assign { conversation_id: "x".to_owned() },
SessionPlan::Resume { conversation_id: "x".to_owned() },
] {
let spec = rt.prepare_invocation(&p, &ctx(), &root(), &plan).unwrap();
assert_eq!(spec.args, vec!["--static", "arg"], "plan = {plan:?}");
}
}
// Ordering: session args land *after* the context-injection (Flag) args.
#[test]
fn session_args_come_after_context_injection_args() {
let rt = pure_runtime();
let p = AgentProfile::new(
ProfileId::from_uuid(uuid::Uuid::from_u128(8)),
"Test",
"mycli",
vec!["--static".to_owned()],
ContextInjection::flag("--context-file {path}").unwrap(),
Some("mycli probe".to_owned()),
"{agentRunDir}",
Some(SessionStrategy::new(Some("--session-id".to_owned()), "--resume").unwrap()),
)
.unwrap();
let spec = rt
.prepare_invocation(
&p,
&ctx(),
&root(),
&SessionPlan::Assign { conversation_id: "abc".to_owned() },
)
.unwrap();
// static arg, then context-injection args, then session args — in that order.
assert_eq!(
spec.args,
vec![
"--static",
"--context-file",
".ideai/agent.md",
"--session-id",
"abc"
]
);
}

View File

@ -0,0 +1,178 @@
//! T6 integration tests for [`ClaudeTranscriptInspector`] against a real temp
//! directory and a real [`LocalFileSystem`], exercising the full transcript
//! discovery + parsing path:
//!
//! - a realistic `~/.claude/projects/<encoded-cwd>/<id>.jsonl` fixture →
//! `last_topic` + `token_count` extracted;
//! - a missing transcript → [`InspectError::NotFound`];
//! - malformed lines in the middle → skipped, valid lines still extracted;
//! - [`SessionInspector::supports`] true for a `CLAUDE.md` profile, false for an
//! `AGENTS.md` one or a non-`conventionFile` injection.
use std::path::PathBuf;
use std::sync::Arc;
use domain::ids::ProfileId;
use domain::ports::{FileSystem, InspectError, SessionInspector};
use domain::profile::{AgentProfile, ContextInjection};
use domain::project::ProjectPath;
use infrastructure::{ClaudeTranscriptInspector, LocalFileSystem};
use uuid::Uuid;
/// A unique scratch directory under the OS temp dir, cleaned up on drop. Plays
/// the role of the user's `$HOME` so the inspector reads
/// `<home>/.claude/projects/...` entirely inside the fixture.
struct TempHome(PathBuf);
impl TempHome {
fn new() -> Self {
let p = std::env::temp_dir().join(format!("idea-t6-claude-{}", Uuid::new_v4()));
std::fs::create_dir_all(&p).unwrap();
Self(p)
}
fn path(&self) -> String {
self.0.to_string_lossy().into_owned()
}
/// Writes a transcript at `<home>/.claude/projects/<encoded-cwd>/<id>.jsonl`,
/// mirroring the adapter's own cwd-encoding convention.
fn write_transcript(&self, cwd: &str, conversation_id: &str, body: &str) {
let encoded: String = cwd
.chars()
.map(|c| if c == '/' || c == '\\' { '-' } else { c })
.collect();
let dir = self.0.join(".claude").join("projects").join(&encoded);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join(format!("{conversation_id}.jsonl")), body).unwrap();
}
}
impl Drop for TempHome {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn inspector(home: &TempHome) -> ClaudeTranscriptInspector {
let fs: Arc<dyn FileSystem> = Arc::new(LocalFileSystem::new());
ClaudeTranscriptInspector::with_base_dir(fs, home.path())
}
fn claude_profile() -> AgentProfile {
AgentProfile::new(
ProfileId::from_uuid(Uuid::from_u128(1)),
"Claude Code",
"claude",
Vec::new(),
ContextInjection::convention_file("CLAUDE.md").unwrap(),
Some("claude --version".to_owned()),
"{agentRunDir}",
None,
)
.unwrap()
}
#[tokio::test]
async fn extracts_last_topic_and_token_count_from_fixture() {
let home = TempHome::new();
let cwd = "/home/dev/Projects/Demo";
let id = "11111111-2222-3333-4444-555555555555";
let body = concat!(
r#"{"role":"user","content":"set up the project"}"#,
"\n",
r#"{"role":"assistant","content":"on it","usage":{"input_tokens":12,"output_tokens":8}}"#,
"\n",
r#"{"role":"user","content":"now add tests"}"#,
"\n",
r#"{"role":"assistant","content":"done","usage":{"input_tokens":30,"output_tokens":20}}"#,
"\n",
);
home.write_transcript(cwd, id, body);
let insp = inspector(&home);
let details = insp
.details(&claude_profile(), id, &ProjectPath::new(cwd).unwrap())
.await
.expect("transcript should be found and parsed");
assert_eq!(details.last_topic.as_deref(), Some("now add tests"));
assert_eq!(details.token_count, Some(70));
}
#[tokio::test]
async fn missing_transcript_is_not_found() {
let home = TempHome::new();
let insp = inspector(&home);
let err = insp
.details(
&claude_profile(),
"does-not-exist",
&ProjectPath::new("/home/dev/nope").unwrap(),
)
.await
.expect_err("absent transcript must error");
assert_eq!(err, InspectError::NotFound);
}
#[tokio::test]
async fn malformed_lines_are_skipped_not_fatal() {
let home = TempHome::new();
let cwd = "/home/dev/Projects/Resilient";
let id = "aaaa";
let body = concat!(
r#"{"role":"user","content":"valid first"}"#,
"\n",
"{ this is not valid json",
"\n",
"plain garbage line",
"\n",
r#"{"role":"assistant","usage":{"input_tokens":5,"output_tokens":6}}"#,
"\n",
r#"{"role":"user","content":"valid last"}"#,
"\n",
);
home.write_transcript(cwd, id, body);
let insp = inspector(&home);
let details = insp
.details(&claude_profile(), id, &ProjectPath::new(cwd).unwrap())
.await
.expect("readable transcript with bad lines should still parse");
assert_eq!(details.last_topic.as_deref(), Some("valid last"));
assert_eq!(details.token_count, Some(11));
}
#[test]
fn supports_only_claude_md_profiles() {
let fs: Arc<dyn FileSystem> = Arc::new(LocalFileSystem::new());
let insp = ClaudeTranscriptInspector::new(fs, "/tmp/whatever");
// CLAUDE.md → supported.
assert!(insp.supports(&claude_profile()));
// AGENTS.md (Codex) → not supported.
let codex = AgentProfile::new(
ProfileId::from_uuid(Uuid::from_u128(2)),
"Codex",
"codex",
Vec::new(),
ContextInjection::convention_file("AGENTS.md").unwrap(),
None,
"{agentRunDir}",
None,
)
.unwrap();
assert!(!insp.supports(&codex));
// Non-conventionFile injection (stdin) → not supported.
let stdin_profile = AgentProfile::new(
ProfileId::from_uuid(Uuid::from_u128(3)),
"Piped",
"aider",
Vec::new(),
ContextInjection::stdin(),
None,
"{agentRunDir}",
None,
)
.unwrap();
assert!(!insp.supports(&stdin_profile));
}

View File

@ -21,7 +21,8 @@ use domain::markdown::MarkdownDoc;
use domain::ports::{
AgentContextStore, AgentRuntime, ContextInjectionPlan, DirEntry, EventBus, EventStream,
ExitStatus, FileSystem, FsError, IdGenerator, OutputStream, PreparedContext, ProfileStore,
PtyError, PtyHandle, PtyPort, RemotePath, RuntimeError, SkillStore, SpawnSpec, StoreError,
PtyError, PtyHandle, PtyPort, RemotePath, RuntimeError, SessionPlan, SkillStore, SpawnSpec,
StoreError,
};
use domain::ids::SkillId;
use domain::profile::{AgentProfile, ContextInjection};
@ -183,6 +184,7 @@ impl AgentRuntime for FakeRuntime {
profile: &AgentProfile,
_ctx: &PreparedContext,
cwd: &ProjectPath,
_session: &SessionPlan,
) -> Result<SpawnSpec, RuntimeError> {
Ok(SpawnSpec {
command: profile.command.clone(),
@ -283,6 +285,7 @@ fn build_service(contexts: FakeContexts) -> Arc<OrchestratorService> {
ContextInjection::stdin(),
None,
"{agentRunDir}",
None,
)
.unwrap()])));
let sessions = Arc::new(TerminalSessions::new());
@ -301,6 +304,7 @@ fn build_service(contexts: FakeContexts) -> Arc<OrchestratorService> {
Arc::new(FakeSkills),
Arc::clone(&sessions),
bus.clone(),
Arc::new(SeqIds(Mutex::new(1))),
));
let list = Arc::new(ListAgents::new(Arc::new(contexts.clone())));
let close = Arc::new(CloseTerminal::new(Arc::new(FakePty), Arc::clone(&sessions)));

View File

@ -46,6 +46,7 @@ fn sample(id: u128, name: &str, command: &str) -> AgentProfile {
ContextInjection::convention_file("CLAUDE.md").unwrap(),
Some(format!("{command} --version")),
"{projectRoot}",
None,
)
.unwrap()
}