Skip to content

fix(openclaw): scope recall and capture by project#598

Open
MackDing wants to merge 1 commit into
rohitg00:mainfrom
MackDing:fix/openclaw-project-scope
Open

fix(openclaw): scope recall and capture by project#598
MackDing wants to merge 1 commit into
rohitg00:mainfrom
MackDing:fix/openclaw-project-scope

Conversation

@MackDing
Copy link
Copy Markdown

@MackDing MackDing commented May 22, 2026

Summary

  • derive project/workspace scope from OpenClaw hook events
  • pass that scope into OpenClaw recall requests via smart-search
  • include project and cwd on OpenClaw observe payloads so captured sessions are grouped consistently
  • add regression tests for scoped recall/capture behavior

Problem

The OpenClaw integration recalled memory using only the prompt text and captured turns with only a session id. In shared agentmemory deployments, that makes it too easy for the OpenClaw plugin to query global memory without any project/workspace scoping.

Fix

This PR keeps the change deliberately small and backward-compatible:

  • before_agent_start now derives a project value from the OpenClaw event (project, nested workspace metadata, or cwd fallback) and passes it to /agentmemory/smart-search
  • agent_end now includes project and cwd in /agentmemory/observe
  • when no project metadata is available, capture falls back to the session id so the payload still satisfies the observe contract and avoids collapsing everything into an empty/global bucket

This is not full principal/tenant isolation, but it is a practical hardening step that restores existing project-based filtering support in the backend and reduces cross-project leakage risk for the OpenClaw integration.

Verification

  • npm test -- --run test/openclaw-plugin.test.ts test/openclaw-project-scope.test.ts test/integration-plaintext-http.test.ts

Refs #596

Summary by CodeRabbit

  • New Features

    • OpenClaw plugin now derives project context from multiple event locations and includes this information in API requests for improved session and workspace context association.
  • Tests

    • Added comprehensive test suite validating OpenClaw project scoping behavior across various scenarios.

Review Change Stack

@vercel
Copy link
Copy Markdown

vercel Bot commented May 22, 2026

@MackDing is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 22, 2026

📝 Walkthrough

Walkthrough

The PR adds project-scoped memory operations to the OpenClaw plugin integration. It introduces helpers to derive a project identifier from multiple event fields, then uses that derived project to augment smart-search recall requests and observe session-tracking requests, with appropriate fallback values.

Changes

OpenClaw Project Scoping

Layer / File(s) Summary
Project derivation and smart-search integration
integrations/openclaw/plugin.mjs
Adds normalizeString and getProject helpers to extract a project identifier from event fields (project, workspace.project, agent.project, metadata.context, and cwd variants). Updates before_agent_start to compute derived project and conditionally include it in the /agentmemory/smart-search request body.
Observe request enrichment with project and cwd
integrations/openclaw/plugin.mjs
Updates agent_end to derive project and augment the /agentmemory/observe request body with project (or empty fallback) and cwd computed from the derived project (or falling back to session identifier).
Project scoping test suite
test/openclaw-project-scope.test.ts
Adds Vitest test module with fetch mocking and plugin registration helpers. Covers three scenarios: smart-search project propagation, observe project/cwd fallback behavior when derived from sessionKey, and nested workspace.project discovery.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

  • #596: Directly related; both PRs modify integrations/openclaw/plugin.mjs to add derived project/context fields to smart-search and observe payloads for scoping memory operations.

Poem

🐰 A project now scopes every memory call,
From OpenClaw events both nested and tall,
Smart-search recalls what the context reveals,
While observe tracks sessions with project appeals!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(openclaw): scope recall and capture by project' directly and concisely summarizes the main change: scoping OpenClaw's recall and capture functionality by project.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/openclaw-project-scope.test.ts`:
- Around line 34-40: The test setup deletes process.env["AGENTMEMORY_SECRET"] in
beforeEach but never restores it, risking state leakage; modify the setup to
capture the original value (e.g., const originalAgentMemorySecret =
process.env["AGENTMEMORY_SECRET"] or similar) before deleting in beforeEach and
then restore process.env["AGENTMEMORY_SECRET"] = originalAgentMemorySecret (or
delete it if undefined) in afterEach alongside restoring globalThis.fetch =
originalFetch; update the beforeEach/afterEach blocks in
test/openclaw-project-scope.test.ts to use these variables.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 18de1097-da1a-4435-85e1-83a08def6653

📥 Commits

Reviewing files that changed from the base of the PR and between bc64107 and a81bd0a.

📒 Files selected for processing (2)
  • integrations/openclaw/plugin.mjs
  • test/openclaw-project-scope.test.ts

Comment on lines +34 to +40
beforeEach(() => {
delete process.env["AGENTMEMORY_SECRET"];
});

afterEach(() => {
globalThis.fetch = originalFetch;
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Restore AGENTMEMORY_SECRET after each test for isolation.

Line 35 deletes the env var, but it is never restored. This can leak state to other test files.

Suggested fix
 describe("OpenClaw plugin project scoping", () => {
   const originalFetch = globalThis.fetch;
+  const originalAgentMemorySecret = process.env["AGENTMEMORY_SECRET"];

   beforeEach(() => {
     delete process.env["AGENTMEMORY_SECRET"];
   });

   afterEach(() => {
     globalThis.fetch = originalFetch;
+    if (originalAgentMemorySecret === undefined) {
+      delete process.env["AGENTMEMORY_SECRET"];
+    } else {
+      process.env["AGENTMEMORY_SECRET"] = originalAgentMemorySecret;
+    }
   });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
beforeEach(() => {
delete process.env["AGENTMEMORY_SECRET"];
});
afterEach(() => {
globalThis.fetch = originalFetch;
});
describe("OpenClaw plugin project scoping", () => {
const originalFetch = globalThis.fetch;
const originalAgentMemorySecret = process.env["AGENTMEMORY_SECRET"];
beforeEach(() => {
delete process.env["AGENTMEMORY_SECRET"];
});
afterEach(() => {
globalThis.fetch = originalFetch;
if (originalAgentMemorySecret === undefined) {
delete process.env["AGENTMEMORY_SECRET"];
} else {
process.env["AGENTMEMORY_SECRET"] = originalAgentMemorySecret;
}
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/openclaw-project-scope.test.ts` around lines 34 - 40, The test setup
deletes process.env["AGENTMEMORY_SECRET"] in beforeEach but never restores it,
risking state leakage; modify the setup to capture the original value (e.g.,
const originalAgentMemorySecret = process.env["AGENTMEMORY_SECRET"] or similar)
before deleting in beforeEach and then restore process.env["AGENTMEMORY_SECRET"]
= originalAgentMemorySecret (or delete it if undefined) in afterEach alongside
restoring globalThis.fetch = originalFetch; update the beforeEach/afterEach
blocks in test/openclaw-project-scope.test.ts to use these variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant