Mintplex-Labs/anything-llm · warning · Error

Invalid scope: ${JSON.stringify(v)}

Error message

Invalid scope: ${JSON.stringify(v)}

What it means

Thrown by the Memory model's scope validator. VALID_SCOPES is fixed to ['workspace', 'global']; any other value (including undefined-coerced strings, typos, or a stale enum) is rejected with the offending value JSON-stringified for diagnostics. The validator is the default for the scope field across create/update paths.

Source

Thrown at server/models/memory.js:35

  if (!Number.isInteger(n))
    throw new Error(`Expected integer, got ${JSON.stringify(v)}`);
  return n;
}

const Memory = {
  GLOBAL_LIMIT: 5,
  WORKSPACE_LIMIT: 20,
  MAX_INJECTED_WORKSPACE_LIMIT: 5,
  VALID_SCOPES: ["workspace", "global"],

  validations: {
    id: (v) => toInt(v),
    userId: (v = null) => (v === null || v === undefined ? null : toInt(v)),
    workspaceId: (v = null) =>
      v === null || v === undefined ? null : toInt(v),
    scope: (v = "workspace") => {
      if (!Memory.VALID_SCOPES.includes(v))
        throw new Error(`Invalid scope: ${JSON.stringify(v)}`);
      return v;
    },
    content: (v) => {
      if (typeof v !== "string" || v.trim().length === 0)
        throw new Error("Content must be a non-empty string");
      return v;
    },
  },

  /**
   * List a user's workspace-scoped memories, newest first.
   * @param {number|null} userId
   * @param {number} workspaceId
   * @returns {Promise<Memory[]>}
   */
  forUserWorkspace: async function (userId, workspaceId) {
    try {
      const memories = await prisma.memories.findMany({

View on GitHub (pinned to 526360e320)

Solutions

  1. Pass exactly 'workspace' or 'global' (lowercase).
  2. Validate scope against the allowed list on the API boundary before reaching the model.
  3. If a new scope is genuinely needed, add it to Memory.VALID_SCOPES and update the DB enum/check constraint.
  4. Default the parameter when optional: scope ?? 'workspace'.

Example fix

// before
await Memory.create({ userId, scope: req.body.scope, content });
// after
const scope = ['workspace','global'].includes(req.body.scope) ? req.body.scope : 'workspace';
await Memory.create({ userId, scope, content });
Defensive patterns

Strategy: validation

Validate before calling

const VALID_SCOPES = ['workspace', 'global'];
const scope = VALID_SCOPES.includes(input.scope) ? input.scope : 'workspace';
// pass `scope` to Memory.create

Type guard

function isValidMemoryScope(v) {
  return v === 'workspace' || v === 'global';
}

Prevention

When it happens

Trigger: Calling Memory.create / update with scope set to something other than 'workspace' or 'global' — e.g. 'Workspace' (wrong case), 'user', null coerced to 'null', or an integer. Also when client code reads scope from an unvalidated request body.

Common situations: Frontend dropdown sent a display label instead of the enum value; a refactor introduced a new scope tier without updating VALID_SCOPES; deserialization produced undefined and it was passed through.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/80b8057de4052960. Report an issue: GitHub.