Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(memory): match a plan refusal by its delimited code, check it first

M3gA-Mindf914a3ad0bfe
diff --git a/app/src/services/api/memoryApi.test.ts b/app/src/services/api/memoryApi.test.ts
index ea295baaac..447a243e59 100644
--- a/app/src/services/api/memoryApi.test.ts
+++ b/app/src/services/api/memoryApi.test.ts
@@ -194,12 +194,23 @@ describe('memoryErrorCode', () => {
);
expect(memoryErrorCode(new Error('UNAVAILABLE: timed out'))).toBe('UNAVAILABLE');
expect(memoryErrorCode(new Error('PLAN_REQUIRED: not in the plan'))).toBe('PLAN_REQUIRED');
- // A plan refusal that lost its structured code still names the backend's.
+ // A plan refusal that lost its structured code still names the backend's,
+ // and an older core's UNAUTHORIZED does not shadow it.
expect(
memoryErrorCode(
new Error('unauthorized: [MEMORY_PLAN_REQUIRED] memory API memory/scopes (HTTP 403)')
)
).toBe('PLAN_REQUIRED');
+ expect(
+ memoryErrorCode({
+ message: 'unauthorized: [MEMORY_PLAN_REQUIRED] memory API memory/scopes (HTTP 403)',
+ data: { code: 'UNAUTHORIZED' },
+ })
+ ).toBe('PLAN_REQUIRED');
+ // A mention of the code is not a plan refusal.
+ expect(memoryErrorCode(new Error('ENGINE: unexpected [MEMORY_PLAN_REQUIRED] token'))).toBe(
+ 'ENGINE'
+ );
});
it('returns null for an unrelated error', () => {
diff --git a/app/src/services/api/memoryApi.ts b/app/src/services/api/memoryApi.ts
index 95f273557a..65bfd2fe9e 100644
--- a/app/src/services/api/memoryApi.ts
+++ b/app/src/services/api/memoryApi.ts
@@ -547,6 +547,13 @@ export interface JobsList {
history: JobRun[];
}
+/**
+ * A plan refusal's code in the head TinyMemory gives a hosted failure,
+ * `[MEMORY_PLAN_REQUIRED] memory API …` (#8081). Delimited, so a message that
+ * merely mentions the code does not match.
+ */
+const PLAN_REFUSAL = /\[[A-Z0-9_]*_PLAN_REQUIRED\] memory API /;
+
/** The structured error codes a memory RPC can fail with. */
export type MemoryErrorCode =
| 'MEMORY_OFF'
@@ -612,6 +619,10 @@ async function call<T>(method: string, params: object = {}): Promise<T> {
*/
export function memoryErrorCode(err: unknown): MemoryErrorCode | null {
if (!err || typeof err !== 'object') return null;
+ const message = (err as { message?: unknown }).message;
+ // Before the structured code: an older core files a plan refusal under
+ // `UNAUTHORIZED`, with the backend's code still on the message.
+ if (typeof message === 'string' && PLAN_REFUSAL.test(message)) return 'PLAN_REQUIRED';
const data = (err as { data?: unknown }).data;
if (data && typeof data === 'object') {
const d = data as Record<string, unknown>;
@@ -622,10 +633,7 @@ export function memoryErrorCode(err: unknown): MemoryErrorCode | null {
}
}
}
- const message = (err as { message?: unknown }).message;
if (typeof message === 'string') {
- // A plan refusal that lost its structured code still names the backend's.
- if (message.includes('_PLAN_REQUIRED]')) return 'PLAN_REQUIRED';
const match =
/^\s*(MEMORY_OFF|UNSUPPORTED|INVALID_REQUEST|UNAUTHORIZED|INSUFFICIENT_CREDITS|PLAN_REQUIRED|UNAVAILABLE|ENGINE)\b/.exec(
message
@@ -692,7 +700,7 @@ export function memoryStatusError(
t: (key: string, fallback?: string) => string
): string {
const key = REFUSAL_KEYS.PLAN_REQUIRED;
- return key && raw.includes('_PLAN_REQUIRED]') ? t(key) : raw;
+ return key && PLAN_REFUSAL.test(raw) ? t(key) : raw;
}
/**
diff --git a/app/src/services/coreRpcClient.ts b/app/src/services/coreRpcClient.ts
index 215aefc671..185736c565 100644
--- a/app/src/services/coreRpcClient.ts
+++ b/app/src/services/coreRpcClient.ts
@@ -180,7 +180,7 @@ export function classifyRpcError(
// key was rejected (HTTP 403 …)". A refusal is never an expired session;
// matching it below signed a valid user out on opening a Memory tab.
// Mirrors Rust `is_session_expired_message`.
- if (/_PLAN_REQUIRED\]|\(HTTP 403/.test(message)) return 'unknown';
+ if (/\[[A-Z0-9_]*_PLAN_REQUIRED\] memory API |\(HTTP 403/.test(message)) return 'unknown';
// Confirmed OpenHuman session expiry — explicit markers from the backend/core.
if (/Session expired|SESSION_EXPIRED/i.test(message)) return 'auth_expired';
// Core-side "no backend session token" → the auth profile is gone but the
diff --git a/crates/openhuman-core/src/core/observability.rs b/crates/openhuman-core/src/core/observability.rs
index 5afdcaba2c..7916812c79 100644
--- a/crates/openhuman-core/src/core/observability.rs
+++ b/crates/openhuman-core/src/core/observability.rs
@@ -901,12 +901,24 @@ fn is_embedding_model_rejected(lower: &str) -> bool {
}
/// Whether `msg` is the hosted backend refusing a feature the account's plan
-/// does not include: a `[…_PLAN_REQUIRED]` code (`MEMORY_PLAN_REQUIRED`) as
-/// tinymemory prefixes it. User state, never an expired session (#8081).
+/// does not include (#8081): a `[…_PLAN_REQUIRED]` code in the head
+/// TinyMemory gives a hosted failure, `[CODE] memory API {route} …`. Matched
+/// as that delimited code, so a description that merely mentions the code
+/// is not a plan refusal. User state, never an expired session.
pub fn is_plan_required_message(msg: &str) -> bool {
- msg.contains("_PLAN_REQUIRED]")
+ // The code runs from the nearest `[` before the head and is all
+ // `A-Z0-9_`, as TinyMemory cleans it.
+ msg.match_indices(PLAN_REQUIRED_HEAD).any(|(at, _)| {
+ msg[..at].rsplit_once('[').is_some_and(|(_, code)| {
+ code.chars()
+ .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_')
+ })
+ })
}
+/// What follows a plan code in TinyMemory's hosted failure head.
+const PLAN_REQUIRED_HEAD: &str = "_PLAN_REQUIRED] memory API ";
+
/// Detect **app-session-expired** boundary errors that bubble up from any
/// backend-touching call site (agent, web channel, cron, integrations).
///
diff --git a/crates/openhuman-core/src/core/observability_error_classification_session_tests.rs b/crates/openhuman-core/src/core/observability_error_classification_session_tests.rs
index 2992d649ae..51d31d231f 100644
--- a/crates/openhuman-core/src/core/observability_error_classification_session_tests.rs
+++ b/crates/openhuman-core/src/core/observability_error_classification_session_tests.rs
@@ -760,4 +760,11 @@ fn memory_plan_refusal_is_backend_user_state_not_session_expiry() {
assert!(!is_plan_required_message(
"[USER_INSUFFICIENT_CREDITS] top up"
));
+ // Only the delimited code in a hosted failure head counts, not a mention.
+ assert!(!is_plan_required_message(
+ "backend returned 500: unexpected [MEMORY_PLAN_REQUIRED] token"
+ ));
+ assert!(!is_plan_required_message(
+ "engine error: saw [not a code_PLAN_REQUIRED] memory API x"
+ ));
}
diff --git a/crates/openhuman-core/src/memory/error.rs b/crates/openhuman-core/src/memory/error.rs
index 51093ae3ed..d87c54daf4 100644
--- a/crates/openhuman-core/src/memory/error.rs
+++ b/crates/openhuman-core/src/memory/error.rs
@@ -127,8 +127,9 @@ impl From<tinymemory_api::Error> for MemoryError {
let credits = tinymemory_integrations::cortex::is_insufficient_credits(&error);
// A plan refusal (403 `[MEMORY_PLAN_REQUIRED]`) arrives as
// `Unauthorized` from older TinyMemory and as `Engine` from newer, with
- // the code on its message either way (#8081).
- let plan = crate::core::observability::is_plan_required_message(&error.to_string());
+ // the backend's code leading its message either way (#8081).
+ let plan = tinymemory_integrations::cortex::error_code(&error)
+ .is_some_and(|code| code.ends_with("_PLAN_REQUIRED"));
match error {
E::Unauthorized(message) | E::Engine(message) if plan => Self::PlanRequired(message),
E::Unsupported(message) => Self::Unsupported(message),
diff --git a/crates/openhuman-core/src/memory/error_tests.rs b/crates/openhuman-core/src/memory/error_tests.rs
index 9aa67bc31c..a4bfc18392 100644
--- a/crates/openhuman-core/src/memory/error_tests.rs
+++ b/crates/openhuman-core/src/memory/error_tests.rs
@@ -104,6 +104,12 @@ fn a_plan_refusal_is_plan_required_not_unauthorized() {
structured.message
);
}
+ // A mention of the code that is not the leading code stays an engine failure.
+ let mention = MemoryError::from(E::Engine(
+ "[HTTP_500] memory API recall on h — unexpected [MEMORY_PLAN_REQUIRED] token".into(),
+ ));
+ assert_eq!(mention.code(), ENGINE);
+ assert!(!mention.is_account_wide());
let expired = MemoryError::from(E::Unauthorized(
"[UNAUTHORIZED] memory API memory/scopes on h: the session expired (HTTP 401)".into(),
));