Skip to content

Commit a6bdc80

Browse files
SunnyShu0925claudesteipete
authored
fix(session-memory): honor configured slug model (#97007)
* [AI] fix(session-memory): forward hook config model to LLM slug generator When llmSlug is enabled with a model override in the session-memory hook config, that model was ignored — slug generation always resolved the agent default model via resolveDefaultModelForAgent. Add an optional model parameter to generateSlugViaLLM and pass hookConfig.model from the session-memory handler. When a hook model is provided, only the model is forwarded (provider is omitted) so runEmbeddedAgent's built-in resolveInitialEmbeddedRunModel chain handles alias, provider-qualified ref, and bare-name resolution through a single source of truth — avoiding the routing drift that blocked prior PRs. Fixes #89551 Co-Authored-By: Claude Opus 4.8 <[email protected]> * fix(session-memory): centralize slug model resolution Honor the session-memory slug model override while keeping default, alias, and provider-qualified model selection in the embedded runner. Co-authored-by: SunnyShu <[email protected]> --------- Co-authored-by: Claude Opus 4.8 <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 8f8aad9 commit a6bdc80

6 files changed

Lines changed: 35 additions & 42 deletions

File tree

‎docs/automation/hooks.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ openclaw hooks enable <hook-name>
238238

239239
### session-memory details
240240

241-
Extracts the last user/assistant messages (default 15, configurable with `hooks.internal.entries.session-memory.messages`) and saves them to `<workspace>/memory/YYYY-MM-DD-HHMM.md` using the host local date. Memory capture runs in the background so `/new` and `/reset` acknowledgements are not delayed by transcript reads or optional slug generation. Set `hooks.internal.entries.session-memory.llmSlug: true` to generate descriptive filename slugs with the configured model (falls back to timestamp slugs when unavailable). Requires `workspace.dir` to be configured.
241+
Extracts the last user/assistant messages (default 15, configurable with `hooks.internal.entries.session-memory.messages`) and saves them to `<workspace>/memory/YYYY-MM-DD-HHMM.md` using the host local date. Memory capture runs in the background so `/new` and `/reset` acknowledgements are not delayed by transcript reads or optional slug generation. Set `hooks.internal.entries.session-memory.llmSlug: true` to generate descriptive filename slugs, and optionally set `hooks.internal.entries.session-memory.model` to a configured alias such as `sonnet`, a bare model ID on the agent's default provider, or a `provider/model` ref. Slug generation uses the agent's default model when `model` is omitted and falls back to timestamp slugs when unavailable. Requires `workspace.dir` to be configured.
242242

243243
<a id="bootstrap-extra-files"></a>
244244

‎src/hooks/bundled/session-memory/HOOK.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ When `llmSlug` is enabled, the hook uses your configured LLM provider to generat
6161

6262
The hook supports optional configuration:
6363

64-
| Option | Type | Default | Description |
65-
| ---------- | ------- | ------- | ------------------------------------------------------------------------------------------- |
66-
| `messages` | number | 15 | Number of user/assistant messages to include in the memory file |
67-
| `llmSlug` | boolean | false | Use your configured model to generate descriptive filename slugs instead of timestamp slugs |
64+
| Option | Type | Default | Description |
65+
| ---------- | ------- | ------------- | ------------------------------------------------------------------------------------------- |
66+
| `messages` | number | 15 | Number of user/assistant messages to include in the memory file |
67+
| `llmSlug` | boolean | false | Use your configured model to generate descriptive filename slugs instead of timestamp slugs |
68+
| `model` | string | agent default | Configured alias, bare model ID on the default provider, or `provider/model` override |
6869

6970
Example configuration:
7071

@@ -76,7 +77,8 @@ Example configuration:
7677
"session-memory": {
7778
"enabled": true,
7879
"messages": 25,
79-
"llmSlug": true
80+
"llmSlug": true,
81+
"model": "sonnet"
8082
}
8183
}
8284
}
@@ -90,6 +92,7 @@ The hook automatically:
9092
- Uses timestamp slugs by default so `/new` and `/reset` stay fast on message channels
9193
- Runs memory capture in the background so reset acknowledgements can return immediately
9294
- Uses your configured LLM for slug generation only when `llmSlug` is `true`
95+
- Resolves configured aliases such as `sonnet`; bare model IDs use the agent's default provider, while `provider/model` selects another provider
9396
- Falls back to timestamp slugs if LLM slug generation is unavailable
9497

9598
## Disabling

‎src/hooks/bundled/session-memory/handler.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ describe("session-memory hook", () => {
354354
"session-memory": {
355355
enabled: true,
356356
llmSlug: true,
357+
model: "sonnet",
357358
},
358359
},
359360
},
@@ -365,6 +366,7 @@ describe("session-memory hook", () => {
365366
);
366367

367368
expect(generateSlug).toHaveBeenCalledTimes(1);
369+
expect(generateSlug).toHaveBeenCalledWith(expect.objectContaining({ model: "sonnet" }));
368370
});
369371

370372
it("does not block reset command handling on opt-in model slug generation", async () => {

‎src/hooks/bundled/session-memory/handler.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ async function saveSessionMemoryNow(event: Parameters<HookHandler>[0]): Promise<
234234
if (sessionContent && cfg && allowLlmSlug) {
235235
log.debug("Calling generateSlugViaLLM...");
236236
// Use LLM to generate a descriptive slug
237-
slug = await generateSlugViaLLM({ sessionContent, cfg });
237+
const slugModel = typeof hookConfig?.model === "string" ? hookConfig.model : undefined;
238+
slug = await generateSlugViaLLM({ sessionContent, cfg, model: slugModel });
238239
log.debug("Generated slug", { slug });
239240
}
240241
}

‎src/hooks/llm-slug-generator.test.ts‎

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ vi.mock("../agents/agent-scope.js", () => ({
88
resolveDefaultAgentId: vi.fn(() => "main"),
99
resolveAgentWorkspaceDir: vi.fn(() => "/tmp/openclaw-agent"),
1010
resolveAgentDir: vi.fn(() => "/tmp/openclaw-agent/.openclaw-agent"),
11-
resolveAgentEffectiveModelPrimary: vi.fn((cfg: OpenClawConfig) => {
12-
const model = cfg.agents?.defaults?.model;
13-
if (typeof model === "string") {
14-
return model;
15-
}
16-
return model?.primary;
17-
}),
1811
}));
1912

2013
vi.mock("../agents/embedded-agent.js", () => ({
@@ -81,7 +74,7 @@ describe("generateSlugViaLLM", () => {
8174
expect(requireFirstRunOptions().timeoutMs).toBe(500_000);
8275
});
8376

84-
it("infers provider metadata for bare configured agent models", async () => {
77+
it("delegates default model resolution to the embedded runner", async () => {
8578
await generateSlugViaLLM({
8679
sessionContent: "hello",
8780
cfg: {
@@ -90,33 +83,31 @@ describe("generateSlugViaLLM", () => {
9083
model: { primary: "gpt-5.5" },
9184
},
9285
},
93-
models: {
94-
providers: {
95-
openai: {
96-
baseUrl: "https://chatgpt.com/backend-api/codex",
97-
models: [
98-
{
99-
id: "gpt-5.5",
100-
name: "GPT 5.5",
101-
reasoning: true,
102-
input: ["text"],
103-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
104-
contextWindow: 200_000,
105-
maxTokens: 128_000,
106-
},
107-
],
108-
},
109-
},
110-
},
11186
} as OpenClawConfig,
11287
});
11388

11489
expect(runEmbeddedAgentMock).toHaveBeenCalledOnce();
11590
const options = requireFirstRunOptions();
116-
expect(options.provider).toBe("openai");
117-
expect(options.model).toBe("gpt-5.5");
91+
expect(options.provider).toBeUndefined();
92+
expect(options.model).toBeUndefined();
11893
});
11994

95+
it.each(["gpt-5.5", "anthropic/claude-sonnet-4-6"])(
96+
"passes hook-level model %s to the embedded runner without a provider",
97+
async (model) => {
98+
await generateSlugViaLLM({
99+
sessionContent: "hello",
100+
cfg: {} as OpenClawConfig,
101+
model,
102+
});
103+
104+
expect(runEmbeddedAgentMock).toHaveBeenCalledOnce();
105+
const options = requireFirstRunOptions();
106+
expect(options.provider).toBeUndefined();
107+
expect(options.model).toBe(model);
108+
},
109+
);
110+
120111
it("rejects error payloads before slugifying them into memory filenames", async () => {
121112
runEmbeddedAgentMock.mockResolvedValueOnce({
122113
payloads: [

‎src/hooks/llm-slug-generator.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
resolveAgentDir,
1414
} from "../agents/agent-scope.js";
1515
import { runEmbeddedAgent } from "../agents/embedded-agent.js";
16-
import { resolveDefaultModelForAgent } from "../agents/model-selection.js";
1716
import { resolveAgentTimeoutMs } from "../agents/timeout.js";
1817
import type { OpenClawConfig } from "../config/types.openclaw.js";
1918
import { createSubsystemLogger } from "../logging/subsystem.js";
@@ -74,6 +73,8 @@ function isErrorSlugPayload(payload: { text?: string; isError?: boolean } | unde
7473
export async function generateSlugViaLLM(params: {
7574
sessionContent: string;
7675
cfg: OpenClawConfig;
76+
/** Optional hook-level override; the embedded runner owns model resolution. */
77+
model?: string;
7778
}): Promise<string | null> {
7879
let tempSessionFile: string | null = null;
7980

@@ -93,10 +94,6 @@ ${truncateUtf16Safe(params.sessionContent, 2000)}
9394
9495
Reply with ONLY the slug, nothing else. Examples: "vendor-pitch", "api-design", "bug-fix"`;
9596

96-
const { provider, model } = resolveDefaultModelForAgent({
97-
cfg: params.cfg,
98-
agentId,
99-
});
10097
const timeoutMs = resolveSlugGeneratorTimeoutMs(params.cfg);
10198

10299
const result = await runEmbeddedAgent({
@@ -108,8 +105,7 @@ Reply with ONLY the slug, nothing else. Examples: "vendor-pitch", "api-design",
108105
agentDir,
109106
config: params.cfg,
110107
prompt,
111-
provider,
112-
model,
108+
model: params.model,
113109
timeoutMs,
114110
runId: `slug-gen-${Date.now()}`,
115111
cleanupBundleMcpOnRunEnd: true,

0 commit comments

Comments
 (0)