Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(hooks): resolve workspace override against session before disqualifying sticky routing

Maxsuel Einstein5fcfdb310a14
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bf0c92f3..1f2635c3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+- `[routing] mid_session = "sticky"` was silently disabled for every
+ marker-covered install. `workspace` is a required `.ai-memory.toml` key, so
+ the host hook forwards `&workspace=…` on every event under any marker's
+ tree — including events that never left the session's own workspace — and
+ `overrides_permit_sticky` (`ai-memory-hooks::router`) disqualified
+ stickiness on that presence alone, before the project-provenance logic
+ (`project_src=repo-root` vs. `marker`) ever ran. A mid-session `cd` into a
+ sibling checkout therefore always rescoped under a marker, exactly as if
+ `sticky` were unset, while the identical scenario outside any marker's tree
+ (`workspace_override` naturally `None`) worked as documented — the entire
+ difference between the two cases this bug reports as "identical except for
+ being under `$HOME`". `find_session_scope` now runs before the sticky-permit
+ gate, and a `workspace_override` is resolved once against the session's own
+ workspace (`workspace_override_is_rescope`, via the existing no-create
+ `lookup_existing_workspace` — no new lookup mechanism): the SAME workspace
+ is not a rescope and falls through to the existing project-provenance logic
+ unchanged; a genuinely DIFFERENT or unresolvable workspace still fails
+ closed and disqualifies sticky, exactly as before. (#984)
+
## [2.4.2] - 2026-09-29
### Changed
diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rs
index 02ecad19..4764e14c 100644
--- a/crates/ai-memory-hooks/src/router.rs
+++ b/crates/ai-memory-hooks/src/router.rs
@@ -19,7 +19,10 @@ use ai_memory_core::{
NewSession, ObservationKind, ProjectId, Sanitized, Sanitizer, SessionId, WorkspaceId,
WorkstreamEvent, WorkstreamEventKind,
};
-use ai_memory_store::{HookSessionAdmission, IngestObservationOutcome, StoreError, WriterHandle};
+use ai_memory_store::{
+ HookSessionAdmission, IngestObservationOutcome, StoreError, WriterHandle,
+ lookup_existing_workspace,
+};
use ai_memory_wiki::{AdmissionContext, AdmissionOp, Wiki};
use axum::Json;
use axum::Router;
@@ -2348,6 +2351,39 @@ fn sticky_out_of_tree_under_repo_root(
&& meaningful_session_anchor(session_cwd, home_dir).is_some()
}
+/// Whether `workspace_override`, resolved against the session's own
+/// workspace, is a genuine rescope (issue #976).
+///
+/// `workspace` is a *required* key in every `.ai-memory.toml` (see
+/// `docs/marker-file.md`), so the host-side hook forwards `&workspace=…` on
+/// every event under any marker's tree — including events that never left the
+/// session's own workspace. Treating that presence alone as a rescope (the
+/// pre-#976 behavior) silently disqualified sticky routing for every
+/// marker-covered install, because the marker's own workspace rides along
+/// with every event whether or not the agent actually moved.
+///
+/// `None` never disqualifies. `Some(name)` that resolves, via the existing
+/// no-create scope lookup (never a hand-rolled chain — see the "Scope
+/// resolution" rule in AGENTS.md), to the SAME workspace as the session is
+/// not a rescope either: the event just re-declared where it already is.
+/// Different, or unresolvable (a typo, a workspace that doesn't exist yet),
+/// fails closed and is treated as a genuine rescope — matching
+/// `ProjectSource::parse`'s fail-closed stance in this same file: a typo must
+/// never silently downgrade a real rescope into a sticky no-op.
+async fn workspace_override_is_rescope(
+ reader: &ai_memory_store::ReaderPool,
+ workspace_override: Option<&str>,
+ session_workspace: WorkspaceId,
+) -> bool {
+ let Some(name) = workspace_override else {
+ return false;
+ };
+ match lookup_existing_workspace(reader, name).await {
+ Ok(resolved) => resolved != session_workspace,
+ Err(_) => true,
+ }
+}
+
/// Whether this event's declared overrides leave room for session-sticky
/// attribution at all (issue #394's `sticky` knob).
///
@@ -2360,14 +2396,17 @@ fn sticky_out_of_tree_under_repo_root(
/// too old to tag its override reports `Unspecified` and keeps today's
/// behavior, so `sticky` degrades safely rather than silently capturing
/// deliberate rescopes.
+///
+/// `workspace_is_rescope` is [`workspace_override_is_rescope`]'s verdict,
+/// resolved by the caller before this function runs (it needs the session's
+/// own workspace and a DB read that this function has no access to).
fn overrides_permit_sticky(
- workspace_override: Option<&str>,
+ workspace_is_rescope: bool,
project_override: Option<&str>,
project_source: ProjectSource,
routing: MidSessionRouting,
) -> bool {
- // A workspace override only ever comes from a marker file.
- if workspace_override.is_some() {
+ if workspace_is_rescope {
return false;
}
if project_override.is_none() {
@@ -2561,27 +2600,38 @@ async fn process_authorized(
// - Under `[routing] mid_session = "sticky"` the session also overrules a
// host-derived `repo-root` override, closing the cross-repo `cd` case;
// marker-declared scopes still win. See `overrides_permit_sticky`.
- let sticky_scope = if overrides_permit_sticky(
- env.workspace_override.as_deref(),
- env.project_override.as_deref(),
- env.project_source,
- state.mid_session_routing,
- ) {
- state
- .reader
- .find_session_scope(session_id)
- .await?
- .filter(|(_, _, session_cwd)| {
- sticky_cwd_admits(
- session_cwd.as_deref(),
- env.cwd.as_deref(),
- state.home_dir.as_deref(),
- env.project_strategy,
- state.mid_session_routing,
- )
- })
- } else {
- None
+ //
+ // `find_session_scope` runs unconditionally (issue #976) — the
+ // `workspace_override.is_some()` early return used to gate it, so a
+ // marker's mandatory `workspace` key disqualified stickiness before the
+ // session's own workspace was ever consulted. Resolving the session's
+ // scope first lets `workspace_override_is_rescope` compare the two:
+ // "still the session's own workspace" is not a rescope, only a genuinely
+ // different (or unresolvable) one is.
+ let session_scope = state.reader.find_session_scope(session_id).await?;
+ let sticky_scope = match session_scope {
+ Some((session_ws, session_proj, session_cwd)) => {
+ let workspace_is_rescope = workspace_override_is_rescope(
+ &state.reader,
+ env.workspace_override.as_deref(),
+ session_ws,
+ )
+ .await;
+ let permits = overrides_permit_sticky(
+ workspace_is_rescope,
+ env.project_override.as_deref(),
+ env.project_source,
+ state.mid_session_routing,
+ ) && sticky_cwd_admits(
+ session_cwd.as_deref(),
+ env.cwd.as_deref(),
+ state.home_dir.as_deref(),
+ env.project_strategy,
+ state.mid_session_routing,
+ );
+ permits.then_some((session_ws, session_proj, session_cwd))
+ }
+ None => None,
};
let publishable_scope = sticky_scope.is_some()
|| has_publishable_scope_hint(env.cwd.as_deref(), env.project_override.as_deref());
@@ -4345,42 +4395,104 @@ mod tests {
// The override gate for `[routing] mid_session` (#394). The invariant
// under test: a marker-declared project is a deliberate rescope and wins
// in BOTH modes; only a host-derived repo-root name may yield, and only
- // under `sticky`.
+ // under `sticky`. The workspace side of the gate is exercised in terms of
+ // `workspace_is_rescope` directly here (the caller's already-resolved
+ // verdict) — `workspace_override_is_rescope`'s own DB-backed resolution
+ // (same-workspace vs. different vs. unresolvable) is covered separately
+ // below (issue #976) and in the `cross_repo_cd`-based integration tests.
#[test]
fn override_gate_distinguishes_marker_from_derived_project() {
use MidSessionRouting::{FollowCwd, Sticky};
use ProjectSource::{Marker, RepoRoot, Unspecified};
- // (workspace, project, source, routing, expected)
+ // (workspace_is_rescope, project, source, routing, expected)
let cases = [
- // No override at all: both modes may stick (pre-#394 behavior).
- (None, None, Unspecified, FollowCwd, true),
- (None, None, Unspecified, Sticky, true),
+ // No workspace rescope, no project override: both modes may
+ // stick (pre-#394 behavior).
+ (false, None, Unspecified, FollowCwd, true),
+ (false, None, Unspecified, Sticky, true),
// Marker-declared project: never yields, in either mode.
- (None, Some("acme"), Marker, FollowCwd, false),
- (None, Some("acme"), Marker, Sticky, false),
+ (false, Some("acme"), Marker, FollowCwd, false),
+ (false, Some("acme"), Marker, Sticky, false),
// Host-derived repo-root name: yields only under `sticky`. This
// is the cross-repo `cd` case the knob exists for.
- (None, Some("acme"), RepoRoot, FollowCwd, false),
- (None, Some("acme"), RepoRoot, Sticky, true),
+ (false, Some("acme"), RepoRoot, FollowCwd, false),
+ (false, Some("acme"), RepoRoot, Sticky, true),
// An untagged override from an older client stays authoritative,
// so `sticky` degrades safely instead of capturing a rescope.
- (None, Some("acme"), Unspecified, Sticky, false),
- // A marker workspace is itself a deliberate scope declaration.
- (Some("oss"), None, Unspecified, Sticky, false),
- (Some("oss"), Some("acme"), RepoRoot, Sticky, false),
+ (false, Some("acme"), Unspecified, Sticky, false),
+ // A genuine workspace rescope (a marker naming a DIFFERENT
+ // workspace than the session's own, or an unresolvable one)
+ // disqualifies sticky outright, regardless of project override.
+ (true, None, Unspecified, Sticky, false),
+ (true, Some("acme"), RepoRoot, Sticky, false),
];
- for (ws, project, source, routing, expected) in cases {
+ for (workspace_is_rescope, project, source, routing, expected) in cases {
assert_eq!(
- overrides_permit_sticky(ws, project, source, routing),
+ overrides_permit_sticky(workspace_is_rescope, project, source, routing),
expected,
- "ws={ws:?} project={project:?} source={} routing={}",
+ "workspace_is_rescope={workspace_is_rescope:?} project={project:?} source={} routing={}",
source.as_str(),
routing.as_str(),
);
}
}
+ // Issue #976: `workspace_override_is_rescope`'s own resolution, in
+ // isolation from the rest of the sticky gate. `workspace` is a required
+ // marker key, so every marker-covered install forwards it on every
+ // event — the same workspace the session is already in, most of the
+ // time. Only a genuinely different (or unresolvable) workspace name may
+ // disqualify stickiness.
+ #[tokio::test]
+ async fn workspace_override_is_rescope_only_on_a_genuine_workspace_change() {
+ let tmp = TempDir::new().unwrap();
+ let state = make_state(&tmp).await;
+ let other_ws = state
+ .writer
+ .get_or_create_workspace("other-workspace")
+ .await
+ .unwrap();
+
+ // No override at all: never a rescope.
+ assert!(
+ !workspace_override_is_rescope(&state.reader, None, state.workspace_id).await,
+ "a missing workspace override must never disqualify sticky"
+ );
+ // The marker re-declaring the session's OWN workspace: not a
+ // rescope — this is the exact #976 repro (every event under a
+ // marker's tree carries the workspace it's already in).
+ assert!(
+ !workspace_override_is_rescope(&state.reader, Some("default"), state.workspace_id)
+ .await,
+ "re-declaring the session's own workspace must not disqualify sticky"
+ );
+ // A marker naming a genuinely different, EXISTING workspace: a real
+ // rescope.
+ assert!(
+ workspace_override_is_rescope(
+ &state.reader,
+ Some("other-workspace"),
+ state.workspace_id
+ )
+ .await,
+ "a marker naming a different workspace must disqualify sticky"
+ );
+ let _ = other_ws;
+ // An unresolvable workspace name (typo, not-yet-created): fails
+ // closed, exactly like `ProjectSource::parse`'s stance on an unknown
+ // `project_src` value.
+ assert!(
+ workspace_override_is_rescope(
+ &state.reader,
+ Some("no-such-workspace"),
+ state.workspace_id
+ )
+ .await,
+ "an unresolvable workspace override must fail closed and disqualify sticky"
+ );
+ }
+
// `sticky` extends out-of-tree inheritance to every strategy, but must
// NOT relax the broad-anchor guard that keeps a stray `$HOME` session
// from becoming a catch-all (#103).
@@ -4673,10 +4785,22 @@ mod tests {
/// host hook tagging each `project` override by provenance, and report
/// where the second observation landed. The shared fixture behind the
/// cross-repo cases below (#394).
+ ///
+ /// `workspace`, when set, is forwarded on BOTH events — mirroring every
+ /// real marker-covered install, where `workspace` is a required marker
+ /// key and rides along with every event under the marker's tree whether
+ /// or not the agent actually changed workspace (#976). Before #976 this
+ /// fixture never set `workspace` at all, which is exactly why the bug —
+ /// a marker's mandatory `workspace` unconditionally disqualifying sticky
+ /// — went untested: none of the cross-repo cases below ever exercised a
+ /// marker's workspace key riding alongside a repo-root-derived project,
+ /// which is exactly the combination every real marker-covered install
+ /// sends.
async fn cross_repo_cd(
state: &HookState,
sid: &str,
source: &str,
+ workspace: Option<&str>,
) -> (ProjectId, Option<ProjectId>) {
let fire = |event: &str, cwd: &str, project: &str, project_src: Option<&str>| {
HookEnvelope::from_query_and_body(
@@ -4684,6 +4808,7 @@ mod tests {
event: event.into(),
agent: Some("claude-code".into()),
cwd: Some(cwd.to_string()),
+ workspace: workspace.map(str::to_owned),
project: Some(project.to_string()),
project_src: project_src.map(str::to_owned),
project_strategy: Some("repo-root".into()),
@@ -4733,6 +4858,15 @@ mod tests {
// hop into a sibling checkout keeps the session's project: the override
// is host-derived (`project_src=repo-root`), so it carries no operator
// intent and yields to the session.
+ //
+ // Also issue #976's regression case: BOTH events carry `workspace=default`
+ // (matching `state.workspace_id`'s name, per `make_state`), exactly as a
+ // real marker-covered install forwards its required `workspace` key on
+ // every event under the marker's tree — the same workspace the session
+ // is already in. Before the #976 fix, `workspace_override.is_some()`
+ // alone disqualified sticky here, so this exact test would have failed
+ // (see `mid_session_out_of_tree_cwd_still_resolves_per_event_under_basename`-
+ // style per-event splitting) had the fixture ever forwarded `workspace`.
#[tokio::test]
async fn sticky_routing_keeps_the_session_project_across_a_sibling_checkout() {
let tmp = TempDir::new().unwrap();
@@ -4747,7 +4881,7 @@ mod tests {
.unwrap();
assert_eq!(repo_a, None, "fixture starts clean");
- let (landed, repo_b) = cross_repo_cd(&state, sid, "repo-root").await;
+ let (landed, repo_b) = cross_repo_cd(&state, sid, "repo-root", Some("default")).await;
let repo_a = state
.reader
.find_project(state.workspace_id, "repo-a".to_string())
@@ -4778,7 +4912,7 @@ mod tests {
);
let sid = "88888888-8888-4888-8888-888888888888";
- let (landed, repo_b) = cross_repo_cd(&state, sid, "repo-root").await;
+ let (landed, repo_b) = cross_repo_cd(&state, sid, "repo-root", Some("default")).await;
let repo_b = repo_b.expect("follow-cwd mints the visited checkout's project");
assert_eq!(
landed, repo_b,
@@ -4796,7 +4930,7 @@ mod tests {
state.mid_session_routing = MidSessionRouting::Sticky;
let sid = "99999999-9999-4999-8999-999999999999";
- let (landed, repo_b) = cross_repo_cd(&state, sid, "marker").await;
+ let (landed, repo_b) = cross_repo_cd(&state, sid, "marker", Some("default")).await;
let repo_b = repo_b.expect("a marker-declared project must still be created");
assert_eq!(
landed, repo_b,
@@ -4804,6 +4938,103 @@ mod tests {
);
}
+ // Issue #976's opposite-direction case, left untested before this fix: a
+ // marker naming a genuinely DIFFERENT workspace than the session's own
+ // must still rescope, even under `sticky`. Only "same workspace, still
+ // here" may fall through to the project-provenance logic above — a real
+ // workspace change is not drift and must never be captured by
+ // stickiness.
+ #[tokio::test]
+ async fn sticky_routing_still_rescopes_when_marker_names_a_different_workspace_mid_session() {
+ let tmp = TempDir::new().unwrap();
+ let mut state = make_state(&tmp).await;
+ state.mid_session_routing = MidSessionRouting::Sticky;
+ // Pre-create the target workspace so the mid-session event's lookup
+ // resolves to a real, distinct WorkspaceId (the `Ok(resolved) !=
+ // session_workspace` branch) instead of accidentally only exercising
+ // the fail-closed "unresolvable name" branch, which would also
+ // disqualify stickiness but for the wrong reason and leave the
+ // resolved-and-different comparison unproven at this level.
+ state
+ .writer
+ .get_or_create_workspace("other-workspace")
+ .await
+ .unwrap();
+ let sid = "cdcdcdcd-cdcd-4cdc-8cdc-cdcdcdcdcdcd";
+ let fire = |event: &str, cwd: &str, project: &str, workspace: &str| {
+ HookEnvelope::from_query_and_body(
+ HookQuery {
+ event: event.into(),
+ agent: Some("claude-code".into()),
+ cwd: Some(cwd.to_string()),
+ workspace: Some(workspace.to_string()),
+ project: Some(project.to_string()),
+ project_src: Some("repo-root".into()),
+ project_strategy: Some("repo-root".into()),
+ ..Default::default()
+ },
+ serde_json::json!({
+ "session_id": sid,
+ "cwd": cwd,
+ "tool_name": "Bash",
+ }),
+ )
+ };
+
+ process(
+ &state,
+ fire("session-start", "/checkouts/repo-a", "repo-a", "default"),
+ None,
+ Vec::new(),
+ )
+ .await
+ .unwrap();
+ process(
+ &state,
+ fire(
+ "post-tool-use",
+ "/checkouts/repo-b",
+ "repo-b",
+ "other-workspace",
+ ),
+ None,
+ Vec::new(),
+ )
+ .await
+ .unwrap();
+
+ let session_id: SessionId = sid.parse().unwrap();
+ let observations = state
+ .reader
+ .observations_for_session(session_id)
+ .await
+ .unwrap();
+ assert_eq!(observations.len(), 2);
+ let other_ws = state
+ .reader
+ .find_workspace("other-workspace".to_string())
+ .await
+ .unwrap()
+ .expect(
+ "a genuinely different workspace override must still resolve/create its own workspace",
+ );
+ let repo_b_in_other_ws = state
+ .reader
+ .find_project(other_ws, "repo-b".to_string())
+ .await
+ .unwrap()
+ .expect("the rescoped event's project must exist in the NEW workspace");
+ let second = observations.last().unwrap();
+ assert_eq!(
+ second.project_id, repo_b_in_other_ws,
+ "a marker naming a genuinely different workspace must still rescope, even under sticky"
+ );
+ assert_ne!(
+ second.workspace_id, state.workspace_id,
+ "the rescoped event must not stay in the session's original workspace"
+ );
+ }
+
// A client too old to tag its override reports no provenance. `sticky`
// must treat that as authoritative rather than assume it was derived,
// so a mixed-version fleet cannot silently capture deliberate rescopes.
diff --git a/docs/security-boundaries.md b/docs/security-boundaries.md
index d032bd48..acb39ebf 100644
--- a/docs/security-boundaries.md
+++ b/docs/security-boundaries.md
@@ -36,7 +36,7 @@ boundary not yet built.
| 8b | Messaging: cancel-own-only | `ai-memory-store/src/ops.rs` `cancel_messages` (AND-gated on `from_*` sender coordinate) | `agent_messages.rs` — whole-outbox **and** a foreign **specific-id** cancel refused | STRONG |
| 8c | Messaging: pop-exactly-once | `ai-memory-store/src/ops.rs` `pop_message_in_transaction` atomic CAS `WHERE state='pending'` | `agent_messages.rs` — sequential **and** `tokio::join!` concurrent double-pop yields exactly one `Some` | STRONG |
| 8d | Messaging: inferred-scope read is diagnosed (#854) | `scope.rs` `is_inferred`; `server.rs` `inferred_scope_hint` on empty pop/list | `agent_messages_briefing.rs` `no_scope_pop_that_misses_the_mail_is_diagnosed_not_a_silent_null` | STRONG |
-| 9 | Scope resolution fail-closed | `ai-memory-store/src/scope.rs` no-create `lookup_existing_*`; create only via `create_explicit_scope` | `scope.rs` no-auto-create + `unscoped_write_with_unresolvable_coordinate_errors` | STRONG |
+| 9 | Scope resolution fail-closed | `ai-memory-store/src/scope.rs` no-create `lookup_existing_*`; create only via `create_explicit_scope`. Also `ai-memory-hooks/src/router.rs` `workspace_override_is_rescope` — the hook router's session-sticky gate resolves a marker's `workspace` override via this same no-create `lookup_existing_workspace` (never a hand-rolled lookup) and fails closed (treats it as a rescope, disqualifying sticky) on a different or unresolvable workspace; only an override that resolves to the session's OWN workspace is not a rescope (#976) | `scope.rs` no-auto-create + `unscoped_write_with_unresolvable_coordinate_errors`; `router.rs` `workspace_override_is_rescope_only_on_a_genuine_workspace_change` (same/different/unresolvable workspace cases), `sticky_routing_keeps_the_session_project_across_a_sibling_checkout` (regression: same workspace forwarded on every event + `project_src=repo-root` from a nested checkout stays sticky), `sticky_routing_still_rescopes_when_marker_names_a_different_workspace_mid_session` (opposite direction: a genuinely different workspace still rescopes even under `sticky`) | STRONG |
| 10 | Destructive-op live-process refusal + confirm flags (invariant #9) | `ai-memory-cli/src/commands/process_guard.rs` `sibling_processes` + confirm flags in `reset`/`restore`/`reindex`/`uninstall --purge-data`/`purge_project` | `admin_purge.rs` confirm→400; `removal.rs` — injected live-sibling makes each destructive command bail before touching the data dir | STRONG |
| 10b | Session purge is scope+owner-bound (no cross-session/project over-delete) | `ai-memory-store/src/ops.rs` `purge_session` — selection scoped to `(workspace_id, project_id)` and keyed on this session's own `summary_page_id` **or** `path='sessions/<sid>.md'` + `json_extract(frontmatter_json,'$.session_id')=<sid>` (frontmatter owner, not the recursive latest-chain); `in_scope==0 → NotFound` fail-closed; whole op in one transaction | `ops.rs` `purge_session_leaves_a_sibling_session_in_the_same_project_intact`, `…refuses_a_session_from_another_project_and_deletes_nothing`, `…refuses_a_session_from_another_workspace`, `…does_not_delete_an_identically_pathed_page_in_another_project`, `…removes_older_summary_versions_without_deleting_prior_manual_page` (#862) | STRONG |
| 11a | Hook backpressure (202/429) + bounded fan-out (invariant #5) | `ai-memory-hooks/src/router.rs` semaphore→429, 202 immediately, `MAX_HOOK_BATCH_ITEMS`, bounded LRU limiter | `router.rs` `handle_hook_returns_429_when_ingest_saturated`, `ingest_rate_limiter_is_bounded` | STRONG |