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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion crates/dim-agent-capabilities/tests/routing_flow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Flow: a Session's parts and their runtimes reach its own lines and faces by
//! id, and answer `SessionClosing`, `NoLine` or a reach refusal otherwise
//! id, and answer `SessionClosing`, `NoLine`, `NoService` or a reach refusal
//! otherwise
//! ([#2127](https://github.com/arcships/dim/issues/2127)).

use dim_agent_capabilities::defaults;
Expand Down Expand Up @@ -76,6 +77,10 @@ impl Rig {
("sessionScope", "session"),
("toolRegistry", "session"),
("branchLifecycle", "branch"),
// A face the catalog shares but this rig's profile does not
// install.
("notInstalled", "branch"),
("notInstalledHere", "session"),
] {
builder.catalog_mut().register_shared_instance(name, group);
}
Expand Down Expand Up @@ -596,6 +601,26 @@ async fn routing_resolves_only_data_faces_of_the_scope_it_reaches() {
},
"a Session face is not a line face"
);
assert_eq!(
scope
.line_call(LineCall::new("main", "notInstalled", "read", vec![]))
.await
.unwrap_err(),
RouteError::NoService {
service: "notInstalled".into()
},
"the holder answers a face the line does not install"
);
assert_eq!(
scope
.call(SessionCall::new("notInstalledHere", "read", vec![]))
.await
.unwrap_err(),
RouteError::NoService {
service: "notInstalledHere".into()
},
"the holder answers a face the Session does not install"
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3. Problem: the new cases stop at the SessionScope surface, so the sessions.call mapping this PR adds is untested. Failure scenario: a later change maps a missing provider back to Routed at crates/dim-app-server/src/agents/sessions.rs:941, or the code is dropped from the serialized error, and these tests still pass. Evidence: the new assertions call scope.line_call/scope.call only (routing_flow.rs:604-623); sessions.call is exercised in crates/dim-app-server/tests/routed_call_flow.rs:121-164 only with installed faces, and no test covers an unknown service name or NoLine precedence. Fix: add one case to routed_call_flow.rs that registers a shared name with no provider on the builder (as routing_flow.rs:80-86 does) and asserts no_service with service and retryable: false; optionally assert no_line wins when the line is absent. A follow-up is fine.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b1f5f45. The new case at crates/dim-app-server/tests/routed_call_flow.rs:170-202 registers a shared branch name with no provider, drives it through sessions.call, and asserts no_service with service and retryable: false, plus no_line precedence for an absent line.

let error = scope
.line_call(LineCall::new("main", "timeline", "absent", vec![]))
.await
Expand Down
20 changes: 12 additions & 8 deletions crates/dim-agent-runtime/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! takes the same two forms for a process caller.
//!
//! Each call looks the target up again, so a caller never keeps a live target
//! (D4). Rust calls the same two operations: it builds `args` from the target
//! (D4). Rust calls the same two `SessionScope` operations: it builds `args` from the target
//! operation's request type and reads the reply as its reply type.
//!
//! What a call resolves is the target scope's data face in the catalog. A name
Expand All @@ -40,7 +40,9 @@
//! closing answers [`RouteError::SessionClosing`], and a line target answers it
//! too, because closing the Session closes its lines. A line that is closing or
//! closed, or that belongs to another Session, answers [`RouteError::NoLine`];
//! that is final for the line: routing never retries it.
//! that is final for the line: routing never retries it. A selected scope
//! that has no provider of the service answers [`RouteError::NoService`], for
//! example when its profile does not install that service.

use crate::*;
use rutis::Ctx;
Expand Down Expand Up @@ -81,6 +83,11 @@ pub enum RouteError {
/// lines. Final for that line: no hold and no retry.
#[error("no line {line} in Session {session}")]
NoLine { session: String, line: String },
/// The Session or line was selected, but no provider of `service` is
/// installed there, for example when its profile leaves the service out.
/// The holder answers it, not an owner; final, no retry.
#[error("{service} is not provided there")]
NoService { service: String },
/// The name is not a data face a route resolves there: the assembly marks
/// it internal, it is a mechanism registration point, or it is a scope
/// lifecycle service. A permanent answer: the request is wrong.
Expand Down Expand Up @@ -120,6 +127,7 @@ impl RouteError {
Self::SessionReloading(_) => "session_reloading",
Self::SessionUnavailable { .. } => "session_unavailable",
Self::NoLine { .. } => "no_line",
Self::NoService { .. } => "no_service",
Self::NotDataFace { .. } => "not_data_face",
Self::MethodNotRoutable { .. } => "method_not_routable",
Self::WrongScope { .. } => "wrong_scope",
Expand Down Expand Up @@ -437,12 +445,8 @@ pub(crate) async fn call_face(
) -> Routed<Option<serde_json::Value>> {
let dispatch = ctx
.get_at::<dyn HostDispatch>(&host_key_in(service, instance))
.ok_or_else(|| RouteError::Owner {
name: service.into(),
code: None,
message: format!("{service} is not provided now"),
data: None,
reloading: false,
.ok_or_else(|| RouteError::NoService {
service: service.into(),
})?;
let args = BridgeValue::List(
args.iter()
Expand Down
4 changes: 2 additions & 2 deletions crates/dim-app-server/src/agents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1823,8 +1823,8 @@ impl AgentIndex {
/// the Session is loaded for the call and held while it runs, its own scope
/// resolves a line, and the target's reply and error come back unchanged.
/// A target that cannot be selected answers `NoSession`,
/// `SessionClosing`, `SessionReloading`, `SessionUnavailable` or `NoLine`
/// before the target's operation runs.
/// `SessionClosing`, `SessionReloading`, `SessionUnavailable`, `NoLine` or
/// `NoService` before the target's operation runs.
pub(crate) async fn call(
&self,
request: crate::agents::sessions::CallRequest,
Expand Down
6 changes: 5 additions & 1 deletion crates/dim-app-server/src/agents/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub enum SessionsError {
/// A routed call could not select its target. These are the same codes a
/// Session's own scope answers: `no_session`, `session_closing`,
/// `session_reloading` (retryable), `session_unavailable`, `no_line`,
/// `not_data_face`, `method_not_routable` and `wrong_scope`.
/// `no_service`, `not_data_face`, `method_not_routable` and `wrong_scope`.
#[error("{message}")]
NoSession { message: String },
#[error("{message}")]
Expand All @@ -312,6 +312,9 @@ pub enum SessionsError {
/// The line is not the Session's, or it is gone; `line` names it.
#[error("{message}")]
NoLine { message: String, line: String },
/// The target was selected but does not provide `service`.
#[error("{message}")]
NoService { message: String, service: String },
#[error("{message}")]
NotDataFace { message: String, service: String },
#[error("{message}")]
Expand Down Expand Up @@ -935,6 +938,7 @@ fn routed(error: dim_agent_runtime::RouteError) -> SessionsError {
SessionsError::SessionUnavailable { message, reason }
}
RouteError::NoLine { line, .. } => SessionsError::NoLine { message, line },
RouteError::NoService { service } => SessionsError::NoService { message, service },
RouteError::Owner {
name,
code,
Expand Down
38 changes: 38 additions & 0 deletions crates/dim-app-server/tests/routed_call_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl Rig {
let mut builder = AgentBuilder::new(AgentConfig::default());
defaults::mock_hosts(&mut builder);
defaults::install(&mut builder);
// A face the catalog shares but this profile does not install.
builder
.catalog_mut()
.register_shared_instance("notInstalled", "branch");
let observed = Arc::new(OnceLock::new());
let probe = observed.clone();
builder.plugin(move |_| Ok(Box::new(Probe(probe.clone())) as Box<dyn DynPlugin>));
Expand Down Expand Up @@ -163,6 +167,40 @@ async fn a_routed_call_loads_and_holds_an_admitted_session() {
rig.server.join().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn an_uninstalled_service_answers_no_service_after_the_line() {
let rig = Rig::start().await;
let launched = rig
.call("launch", json!({"cwd": rig.home.path().to_string_lossy()}))
.await;
let id = launched["value"]["sessionId"]
.as_str()
.unwrap_or_else(|| panic!("a Session launches through Agents: {launched}"))
.to_owned();
let missing = rig
.call(
"call",
json!({"sessionId": id, "lineId": "main", "service": "notInstalled", "method": "read"}),
)
.await;
assert_eq!(missing["ok"], false, "{missing}");
assert_eq!(missing["error"]["code"], "no_service", "{missing}");
assert_eq!(missing["error"]["service"], "notInstalled", "{missing}");
assert_eq!(missing["error"]["retryable"], false, "{missing}");
let no_line = rig
.call(
"call",
json!({"sessionId": id, "lineId": "absent", "service": "notInstalled", "method": "read"}),
)
.await;
assert_eq!(
no_line["error"]["code"], "no_line",
"the line is selected before the service: {no_line}"
);
rig.server.shutdown();
rig.server.join().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_call_during_an_agents_restart_is_retryable() {
let rig = Rig::start().await;
Expand Down
5 changes: 3 additions & 2 deletions docs/design/contracts/runtime-owner-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ envelope leaves `value` out so a process caller reads `undefined`; a direct call
(`undefined`) either way. Agents loads the
Session for the call and holds it while it runs (ADR 027 D5). A target that cannot be
selected answers `no_session`, `session_closing`, `session_reloading` (retry later),
`session_unavailable` (report the reason) or `no_line` (final for that line), before the
target's operation runs. Tree history, directory and batch input continue to use the
`session_unavailable` (report the reason), `no_line` (final for that line) or `no_service`
(the selected target does not provide the service; final), before the target's operation
runs. Tree history, directory and batch input continue to use the
tree's own methods.

| Session caller | Target operation |
Expand Down
7 changes: 4 additions & 3 deletions docs/typescript-plugin-authoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ the id instead, and the owner of the index looks it up at the call: `sessionScop
for a line of its own Session, `sessions.call` for a Session or a line of it, from the
process. Both carry `{service, method, args}` — the target's own operation — and answer
`no_session`, `session_closing`, `session_reloading`, `session_unavailable` or `no_line` when
the target cannot be selected; the target owner's own failure arrives unchanged
the target cannot be selected, and `no_service` when the selected target does not provide the
service (its profile leaves it out); the target owner's own failure arrives unchanged
([#2127](https://github.com/arcships/dim/issues/2127), ADR 027).

```ts
Expand All @@ -61,11 +62,11 @@ again.

The two entries name a failure with different spellings. `sessions.call` answers the
`sessions` envelope with snake_case codes — `no_session`, `session_closing`,
`session_reloading`, `session_unavailable`, `no_line`, `not_data_face`,
`session_reloading`, `session_unavailable`, `no_line`, `no_service`, `not_data_face`,
`method_not_routable`, `wrong_scope`, and `routed` for the owner's own failure.
`sessionScope.lineCall`/`call` throw a `RouteError` whose variants carry the same kinds in
camelCase (`noSession`, `sessionClosing`, `sessionReloading`, `sessionUnavailable`,
`noLine`, `notDataFace`, `methodNotRoutable`, `wrongScope`, `owner`), and `owner` keeps the
`noLine`, `noService`, `notDataFace`, `methodNotRoutable`, `wrongScope`, `owner`), and `owner` keeps the
owner's `name`, `message`, `data` and `reloading`. Only `session_reloading`/`sessionReloading`
and an owner failure that says it reloads may be retried. A routed method that replies nothing
answers `null` through `sessionScope`; `sessions.call` leaves `value` out, so a process caller
Expand Down
8 changes: 5 additions & 3 deletions npm/branch-services.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,7 @@ export type RouteError =
| /** Agents is reloading and cannot take calls yet. A process caller retries later. */ { sessionReloading: string }
| /** The Session exists but cannot be loaded now: its restore failed, or the Agent that owns it is unavailable. The caller reports the reason and does not retry. */ { sessionUnavailable: { session: string; reason: string } }
| /** The Session is live but the line is closing, closed, or not one of its lines. Final for that line: no hold and no retry. */ { noLine: { session: string; line: string } }
| /** The Session or line was selected, but no provider of `service` is installed there, for example when its profile leaves the service out. The holder answers it, not an owner; final, no retry. */ { noService: { service: string } }
| /** The name is not a data face a route resolves there: the assembly marks it internal, it is a mechanism registration point, or it is a scope lifecycle service. A permanent answer: the request is wrong. */ { notDataFace: { service: string } }
| /** The service is a data face a route resolves, but this operation of it is not: its call needs a cancellation or a cleanup the routed call cannot carry, so only a direct call keeps its lifetime behaviour. */ { methodNotRoutable: { service: string; method: string } }
| /** The name is a data face, but not of the scope the route reaches. */ { wrongScope: { service: string; group: string } }
Expand Down Expand Up @@ -1478,11 +1479,12 @@ export type PermissionState = {
/** A Session service failure; generated exports throw this serde value. */
export type SessionsError =
| { code: "rejected"; message: string }
| /** A routed call could not select its target. These are the same codes a Session's own scope answers: `no_session`, `session_closing`, `session_reloading` (retryable), `session_unavailable`, `no_line`, `not_data_face`, `method_not_routable` and `wrong_scope`. */ { code: "no_session"; message: string }
| /** A routed call could not select its target. These are the same codes a Session's own scope answers: `no_session`, `session_closing`, `session_reloading` (retryable), `session_unavailable`, `no_line`, `no_service`, `not_data_face`, `method_not_routable` and `wrong_scope`. */ { code: "no_session"; message: string }
| { code: "session_closing"; message: string }
| { code: "session_reloading"; message: string }
| /** The Session exists but cannot load; `reason` says why. */ { code: "session_unavailable"; message: string; reason: string }
| /** The line is not the Session's, or it is gone; `line` names it. */ { code: "no_line"; message: string; line: string }
| /** The target was selected but does not provide `service`. */ { code: "no_service"; message: string; service: string }
| { code: "not_data_face"; message: string; service: string }
| { code: "method_not_routable"; message: string; service: string; method: string }
| { code: "wrong_scope"; message: string; service: string; group: string }
Expand Down Expand Up @@ -1821,13 +1823,13 @@ export type ReservedMethodsErrorThrown = RustError<"ReservedMethodsError", "rese
export type ResourceErrorThrown = RustError<"ResourceError", string, ResourceError>

/** What a method returning `Err(RouteError)` throws. */
export type RouteErrorThrown = RustError<"RouteError", "noSession" | "sessionClosing" | "sessionReloading" | "sessionUnavailable" | "noLine" | "notDataFace" | "methodNotRoutable" | "wrongScope" | "owner", RouteError>
export type RouteErrorThrown = RustError<"RouteError", "noSession" | "sessionClosing" | "sessionReloading" | "sessionUnavailable" | "noLine" | "noService" | "notDataFace" | "methodNotRoutable" | "wrongScope" | "owner", RouteError>

/** What a method returning `Err(SessionInstanceError)` throws. */
export type SessionInstanceErrorThrown = RustError<"SessionInstanceError", "Create" | "NotReady" | "Cleanup", undefined>

/** What a method returning `Err(SessionsError)` throws. */
export type SessionsErrorThrown = RustError<"SessionsError", "rejected" | "no_session" | "session_closing" | "session_reloading" | "session_unavailable" | "no_line" | "not_data_face" | "method_not_routable" | "wrong_scope" | "routed" | "agent_unknown" | "session_closed" | "branch_missing" | "branch_ended" | "reloading" | "future_sequence" | "ambiguous" | "publication_unknown" | "failed", SessionsError>
export type SessionsErrorThrown = RustError<"SessionsError", "rejected" | "no_session" | "session_closing" | "session_reloading" | "session_unavailable" | "no_line" | "no_service" | "not_data_face" | "method_not_routable" | "wrong_scope" | "routed" | "agent_unknown" | "session_closed" | "branch_missing" | "branch_ended" | "reloading" | "future_sequence" | "ambiguous" | "publication_unknown" | "failed", SessionsError>

/** What a method returning `Err(TaskError)` throws. */
export type TaskErrorThrown = RustError<"TaskError", "read" | "unknown" | "closed" | "running", TaskError>
Expand Down
5 changes: 3 additions & 2 deletions npm/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ export interface Sessions {
* (#2127). Agents loads and holds the Session for the call. A target that
* cannot be selected answers `no_session`, `session_closing`,
* `session_reloading` (retry later), `session_unavailable` (report the
* reason) or `no_line`; the target owner's own failure is `routed` with its
* name and message. */
* reason), `no_line` or `no_service` (the target does not provide the
* service); the target owner's own failure is `routed` with its name and
* message. */
call<C extends import('./branch-services.js').SessionsCall>(
request: C,
): Promise<SessionsResult<import('./branch-services.js').ReplyOf<C>>>;
Expand Down
2 changes: 1 addition & 1 deletion runtime/plugin-sdk.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const preset: 'read-only' | 'workspace-write' | 'full-access' = defaults.preset;
const allowed: string[] = livePolicy.allowedTools;
const disallowed: string[] = defaults.disallowedTools;
declare const permissionError: SessionsErrorThrown;
const permissionCode: 'rejected' | 'routed' | 'no_session' | 'session_closing' | 'session_reloading' | 'session_unavailable' | 'no_line' | 'not_data_face' | 'method_not_routable' | 'wrong_scope' | 'agent_unknown' | 'session_closed' | 'branch_missing' | 'branch_ended' | 'reloading' | 'future_sequence' | 'ambiguous' | 'publication_unknown' | 'failed' = permissionError.code;
const permissionCode: 'rejected' | 'routed' | 'no_session' | 'session_closing' | 'session_reloading' | 'session_unavailable' | 'no_line' | 'no_service' | 'not_data_face' | 'method_not_routable' | 'wrong_scope' | 'agent_unknown' | 'session_closed' | 'branch_missing' | 'branch_ended' | 'reloading' | 'future_sequence' | 'ambiguous' | 'publication_unknown' | 'failed' = permissionError.code;
void permissionVersion; void preset; void allowed; void disallowed; void permissionCode;
// @ts-expect-error permission reads require a branch identity.
void permissions.permission({ sessionId: 'session' });
Expand Down