Skip to content

fix(openclaw): inject the user profile into automatic context - #5676

Merged
t0saki merged 3 commits into
volcengine:mainfrom
t0saki:fix/openclaw-profile-injection
Oct 7, 2026
Merged

t0saki merged 3 commits into
volcengine:mainfrom
t0saki:fix/openclaw-profile-injection

Conversation

@t0saki

@t0saki t0saki commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Description

OpenClaw now injects the user profile into its automatic context, so facts that memory extraction files only into memories/profile.md reach the model in later sessions. On every main assemble, the plugin reads viking://~/memories/profile.md, plus the actor peer's profile.md when an actor peer resolves, and adds it to systemPromptAddition as a <user-profile> block next to the session guide and auto-recall. The PR also rewords a misleading log line and fixes the OpenClaw rows in the capability reference.

Root cause

Two things combine:

  1. Server context assembly never returns profile.md. OpenClaw auto-recall calls /api/v1/search/search with mode: "context", purpose: "coding" and coding quotas. Quotas are always sent, because recallLimit has a default. With quotas, gathering runs per bucket, and each memory bucket searches only memories/<category> (events, entities, preferences, experiences), so memories/profile.md falls under none of them. Flat mode would search the memory root, but _build drops */profile.md explicitly.
  2. That exclusion assumes every client injects the profile itself. Claude Code, Codex, OpenCode, pi, DSH, Cursor, TRAE, ZCode and Kimi do this through buildProfileBlock() at session start, and Hermes has its own reader. OpenClaw's assembleOpenVikingSession added only the archive "Session Context Guide" and the recall block. Nothing in the plugin read profile.md.

How it was introduced

Why every main turn

OpenClaw's systemPromptAddition is not persisted: assemble() rebuilds it on every call, and the Session Context Guide is re-added every turn. Injecting the profile only on the first assemble would drop it from turn 2 onward. Injecting it on every main assemble is the OpenClaw equivalent of a session-start block that stays in the transcript, and pi already rebuilds its profile in the system prompt every turn.

Alternatives considered

  • Read the profile in OpenClaw's main assemble (chosen). Small and local: no server, API or shared-lib change, so no vendored copies and no cross-plugin version bumps. The viking://~ alias resolves on the server per request, so it is safe for a multi-user gateway. Cost: one or two GET /content/read per main turn, run in parallel with recall.
  • Reuse the shared buildProfileBlock(). It caches the resolved user space in a module-level variable, which can leak across identities in a long-running multi-user gateway, and it adds 3–4 requests per turn.
  • Add a profile bucket or include_profile flag to /search context mode. This adds public API surface, and the profile would show up only when it scores above the threshold, while the other integrations always inject it.

Human Involvement

  • A human participated in the implementation or review loop
  • This PR was generated entirely by AI agents without human participation in the loop

Related Issue

Fixes #5671

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

  • services/context-lifecycle-service.ts:
    • New readProfileBlock() reads the user profile and, when an actor peer resolves, the peer profile. Each is capped at 8,000 characters with a truncation marker.
    • The read is gated like auto-recall: main assemble only, autoRecall on, session not bypassed. Short prompts that skip recall still get the profile.
    • The read is bounded by min(autoRecallTimeoutMs, 3 s). Failures other than a missing file are logged as warnings, and a recall failure leaves the profile in place.
    • Order is session guide, profile, recall. Under a tight token budget the plugin tries profile + recall, then profile only, then recall only.
    • Actor-peer resolution moved into resolveAssembleRouting() so the profile read and recall share it.
    • "assemble skipped because OV session does not exist" now reads "session history not assembled because OV session does not exist", since recall and the profile read still run after it.
  • client.ts: read() takes an optional per-request timeout.
  • Docs: the OpenClaw rows in 16-capability-reference.md and the systemPromptAddition description in 03-openclaw.md (en and zh) now describe profile injection and the context-search recall (/api/v1/search/search, mode: "context", with the session id).
  • Version: not bumped, following recent OpenClaw-only fixes.

Testing

  • I have added tests that prove my fix is effective or that my feature works

  • New and existing unit tests pass locally with my changes

  • I have tested this on the following platforms:

    • Linux
    • macOS
    • Windows
  • New tests in tests/ut/context-engine-assemble.test.ts and tests/ut/client.test.ts cover: the issue's scenario for fresh and missing sessions, order across two turns, short prompts, a missing profile, a recall failure, peer profiles, truncation, the budget fallback, the read timeout, failure logging, and no read when autoRecall is off, the session is bypassed, or during transformContext. With the source changes reverted, the tests covering the new behavior fail.

  • npx vitest run: 810 passed, 1 failed. The failure (package-install-contract.test.ts › "ships and registers the canonical Experience skill") also fails on origin/main, because the two copies of the ov-experience-memory skill already differ there.

  • npm run typecheck is clean.

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Screenshots (if applicable)

N/A

Additional Notes

  • On a degraded server, the profile read adds at most 3 s per main turn, on top of the existing 500 ms health check.
  • 16-capability-reference.md still says OpenClaw and Hermes cap injected recall at 4,000 characters and skip entries that do not fit. OpenClaw now passes that cap to the server as max_tokens. That line is not changed here.

t0saki added 3 commits October 6, 2026 12:28
Server context assembly leaves memories/profile.md out because the hook
plugins inject it at session start, but OpenClaw never did, so facts the
extractor files into the profile never reached the model. Read the user's
profile (and the actor peer's under peer_role) through the viking://~ alias
and add it as <user-profile> to systemPromptAddition on every main assemble,
since that addition is rebuilt each turn. Recall is dropped before the
profile when the token budget is tight.

Also reword the missing-session log line: it only means session history was
not assembled, and recall still runs afterwards.
The capability reference still described OpenClaw recall as /find without a
session and listed no profile injection. Update the OpenClaw rows for
context-search recall, the system-prompt placement, the profile block and the
recall timeout, in English and Chinese.
The profile read used the client default timeout (15 s), so a degraded
server could add a full timeoutMs to every main assemble even after recall
gave up on its 500 ms health precheck. Give client.read an optional
request timeout and cap the profile read at min(autoRecallTimeoutMs, 3 s).

When the profile did not fit the token budget, the fallback never tried
recall alone, so recall the old code injected was lost. Try recall by
itself as the last candidate.

Profile read failures other than a missing file (401/403, 5xx, timeouts)
were indistinguishable from "no profile.md". Warn on them.
@ZaynJarvis ZaynJarvis added the agent-plugins Agent harness and plugin integrations label Oct 7, 2026
@t0saki
t0saki marked this pull request as ready for review October 7, 2026 07:44
Copilot AI balanced review requested due to automatic review settings October 7, 2026 07:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@t0saki
t0saki merged commit 1c99008 into volcengine:main Oct 7, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-plugins Agent harness and plugin integrations

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug]: OpenClaw auto-recall never surfaces profile.md, so facts extracted into the user profile are missing in new sessions

3 participants