Skip to content

Commit 1b499e2

Browse files
authored
fix(reset): preserve bootstrap guidance for dynamic models (#119179)
Punchcard-Session: silver-valley-valley-dt
1 parent e8eb765 commit 1b499e2

3 files changed

Lines changed: 121 additions & 6 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import type { OpenClawConfig } from "../../config/types.openclaw.js";
3+
4+
const inventoryMocks = vi.hoisted(() => {
5+
const runtimeModel = {
6+
id: "dynamic-chat",
7+
name: "dynamic-chat",
8+
provider: "dynamic-provider",
9+
api: "openai-responses",
10+
baseUrl: "https://example.invalid/v1",
11+
};
12+
return {
13+
runtimeModel,
14+
resolveRuntimeModelContext: vi.fn(async () => ({
15+
modelApi: runtimeModel.api,
16+
runtimeModel,
17+
})),
18+
resolveInventory: vi.fn((params: Record<string, unknown>) => {
19+
if (!Object.hasOwn(params, "modelApi") || !Object.hasOwn(params, "runtimeModel")) {
20+
throw new Error("runtime model facts must be explicitly owner-published");
21+
}
22+
return {
23+
agentId: "main",
24+
profile: "coding",
25+
groups: [
26+
{
27+
id: "core",
28+
label: "Built-in tools",
29+
source: "core",
30+
tools: [
31+
{
32+
id: "read",
33+
label: "Read",
34+
description: "Read files",
35+
rawDescription: "Read files",
36+
source: "core",
37+
},
38+
],
39+
},
40+
],
41+
};
42+
}),
43+
};
44+
});
45+
46+
vi.mock("../../agents/tools-effective-inventory.js", () => ({
47+
resolveEffectiveToolInventory: inventoryMocks.resolveInventory,
48+
resolveEffectiveToolInventoryRuntimeModelContextAsync: inventoryMocks.resolveRuntimeModelContext,
49+
}));
50+
51+
describe("resolveBareResetBootstrapFileAccess runtime model ownership", () => {
52+
beforeEach(() => {
53+
inventoryMocks.resolveInventory.mockClear();
54+
inventoryMocks.resolveRuntimeModelContext.mockClear();
55+
});
56+
57+
it("resolves runtime model context once and passes explicit facts to sync inventory", async () => {
58+
const { resolveBareResetBootstrapFileAccess } = await import("./session-reset-prompt.js");
59+
const cfg = {} as OpenClawConfig;
60+
const params = {
61+
cfg,
62+
agentId: "main",
63+
sessionKey: "agent:main:main",
64+
workspaceDir: "/tmp/workspace-main",
65+
modelProvider: "dynamic-provider",
66+
modelId: "dynamic-chat",
67+
};
68+
69+
await expect(resolveBareResetBootstrapFileAccess(params)).resolves.toBe(true);
70+
71+
expect(inventoryMocks.resolveRuntimeModelContext).toHaveBeenCalledTimes(1);
72+
expect(inventoryMocks.resolveRuntimeModelContext).toHaveBeenCalledWith({
73+
cfg,
74+
agentId: params.agentId,
75+
workspaceDir: params.workspaceDir,
76+
modelProvider: params.modelProvider,
77+
modelId: params.modelId,
78+
});
79+
expect(inventoryMocks.resolveInventory).toHaveBeenCalledTimes(1);
80+
const inventoryParams = inventoryMocks.resolveInventory.mock.calls[0]?.[0];
81+
expect(inventoryParams).toMatchObject({
82+
...params,
83+
modelApi: inventoryMocks.runtimeModel.api,
84+
runtimeModel: inventoryMocks.runtimeModel,
85+
});
86+
expect(Object.hasOwn(inventoryParams ?? {}, "modelApi")).toBe(true);
87+
expect(Object.hasOwn(inventoryParams ?? {}, "runtimeModel")).toBe(true);
88+
});
89+
});

‎src/auto-reply/reply/session-reset-prompt.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Tests session reset prompt generation and transcript-preserving restart hints.
22
import fs from "node:fs/promises";
33
import path from "node:path";
4-
import { describe, it, expect } from "vitest";
4+
import { describe, expect, it, vi } from "vitest";
55
import type { OpenClawConfig } from "../../config/config.js";
66
import { makeTempWorkspace } from "../../test-helpers/workspace.js";
77
import { resolveBareSessionResetPromptState } from "./session-reset-prompt.js";
@@ -141,4 +141,18 @@ describe("resolveBareSessionResetPromptState", () => {
141141
expect(pending.prompt).toContain("while bootstrap is still pending for this workspace");
142142
expect(pending.prompt).not.toContain("Execute your Session Startup sequence now");
143143
});
144+
145+
it("awaits async bootstrap file access before selecting reset mode", async () => {
146+
const workspaceDir = await makeBootstrapPendingWorkspace();
147+
const hasBootstrapFileAccess = vi.fn(async () => false);
148+
149+
const pending = await resolveBareSessionResetPromptState({
150+
workspaceDir,
151+
hasBootstrapFileAccess,
152+
});
153+
154+
expect(hasBootstrapFileAccess).toHaveBeenCalledTimes(1);
155+
expect(pending.bootstrapMode).toBe("limited");
156+
expect(pending.shouldPrependStartupContext).toBe(false);
157+
});
144158
});

‎src/auto-reply/reply/session-reset-prompt.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import {
55
buildLimitedBootstrapPromptLines,
66
} from "../../agents/bootstrap-prompt.js";
77
import { appendCronStyleCurrentTimeLine } from "../../agents/current-time.js";
8-
import { resolveEffectiveToolInventory } from "../../agents/tools-effective-inventory.js";
8+
import {
9+
resolveEffectiveToolInventory,
10+
resolveEffectiveToolInventoryRuntimeModelContextAsync,
11+
} from "../../agents/tools-effective-inventory.js";
912
import { isWorkspaceBootstrapPending } from "../../agents/workspace.js";
1013
import type { OpenClawConfig } from "../../config/types.openclaw.js";
1114

@@ -36,24 +39,33 @@ const BARE_SESSION_RESET_PROMPT_BOOTSTRAP_LIMITED = [
3639
"Do not mention internal steps, files, tools, or reasoning.",
3740
].join(" ");
3841

39-
export function resolveBareResetBootstrapFileAccess(params: {
42+
export async function resolveBareResetBootstrapFileAccess(params: {
4043
cfg?: OpenClawConfig;
4144
agentId?: string;
4245
sessionKey?: string;
4346
workspaceDir?: string;
4447
modelProvider?: string;
4548
modelId?: string;
46-
}): boolean {
49+
}): Promise<boolean> {
4750
if (!params.cfg) {
4851
return false;
4952
}
53+
const runtimeModelContext = await resolveEffectiveToolInventoryRuntimeModelContextAsync({
54+
cfg: params.cfg,
55+
agentId: params.agentId,
56+
workspaceDir: params.workspaceDir,
57+
modelProvider: params.modelProvider,
58+
modelId: params.modelId,
59+
});
5060
const inventory = resolveEffectiveToolInventory({
5161
cfg: params.cfg,
5262
agentId: params.agentId,
5363
sessionKey: params.sessionKey,
5464
workspaceDir: params.workspaceDir,
5565
modelProvider: params.modelProvider,
5666
modelId: params.modelId,
67+
modelApi: runtimeModelContext.modelApi,
68+
runtimeModel: runtimeModelContext.runtimeModel,
5769
});
5870
return inventory.groups.some((group) => group.tools.some((tool) => tool.id === "read"));
5971
}
@@ -64,7 +76,7 @@ export async function resolveBareSessionResetPromptState(params: {
6476
nowMs?: number;
6577
isPrimaryRun?: boolean;
6678
isCanonicalWorkspace?: boolean;
67-
hasBootstrapFileAccess?: boolean | (() => boolean);
79+
hasBootstrapFileAccess?: boolean | (() => boolean | Promise<boolean>);
6880
}): Promise<{
6981
bootstrapMode: BootstrapMode;
7082
prompt: string;
@@ -75,7 +87,7 @@ export async function resolveBareSessionResetPromptState(params: {
7587
: false;
7688
const hasBootstrapFileAccess = bootstrapPending
7789
? typeof params.hasBootstrapFileAccess === "function"
78-
? params.hasBootstrapFileAccess()
90+
? await params.hasBootstrapFileAccess()
7991
: (params.hasBootstrapFileAccess ?? true)
8092
: true;
8193
const bootstrapMode = resolveBootstrapMode({

0 commit comments

Comments
 (0)