惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
S
SegmentFault 最新的问题
V
Visual Studio Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
美团技术团队
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
有赞技术团队
有赞技术团队
量子位
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
博客园 - 叶小钗
月光博客
月光博客
P
Proofpoint News Feed
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
refactor(agents): read btw context asynchronously · openc...
steipete · 2026-05-02 · via Recent Commits to openclaw:main

@@ -2,10 +2,10 @@ import { beforeEach, describe, expect, it, vi } from "vitest";

22

import type { SessionEntry } from "../config/sessions.js";

3344

const streamSimpleMock = vi.fn();

5+

const readFileMock = vi.fn();

6+

const parseSessionEntriesMock = vi.fn();

7+

const migrateSessionEntriesMock = vi.fn();

58

const buildSessionContextMock = vi.fn();

6-

const getLeafEntryMock = vi.fn();

7-

const branchMock = vi.fn();

8-

const resetLeafMock = vi.fn();

99

const ensureOpenClawModelsJsonMock = vi.fn();

1010

const discoverAuthStorageMock = vi.fn();

1111

const discoverModelsMock = vi.fn();

@@ -29,16 +29,18 @@ vi.mock("@mariozechner/pi-ai", async () => {

2929

};

3030

});

313132+

vi.mock("node:fs/promises", () => ({

33+

default: {

34+

readFile: (...args: unknown[]) => readFileMock(...args),

35+

},

36+

readFile: (...args: unknown[]) => readFileMock(...args),

37+

}));

38+3239

vi.mock("@mariozechner/pi-coding-agent", () => ({

40+

buildSessionContext: (...args: unknown[]) => buildSessionContextMock(...args),

3341

generateSummary: vi.fn(async () => "summary"),

34-

SessionManager: {

35-

open: () => ({

36-

getLeafEntry: getLeafEntryMock,

37-

branch: branchMock,

38-

resetLeaf: resetLeafMock,

39-

buildSessionContext: buildSessionContextMock,

40-

}),

41-

},

42+

migrateSessionEntries: (...args: unknown[]) => migrateSessionEntriesMock(...args),

43+

parseSessionEntries: (...args: unknown[]) => parseSessionEntriesMock(...args),

4244

}));

43454446

vi.mock("./models-config.js", () => ({

@@ -216,6 +218,19 @@ function createAssistantTranscriptMessage(

216218

};

217219

}

218220221+

function createTranscriptEntry(params: { id: string; parentId?: string | null; message: unknown }) {

222+

return {

223+

type: "message",

224+

id: params.id,

225+

parentId: params.parentId ?? null,

226+

message: params.message,

227+

};

228+

}

229+230+

function mockTranscriptEntries(entries: unknown[]) {

231+

parseSessionEntriesMock.mockReturnValue(entries);

232+

}

233+219234

function mockActiveTranscript(messages: unknown[]) {

220235

getActiveEmbeddedRunSnapshotMock.mockReturnValue({

221236

transcriptLeafId: "assistant-1",

@@ -266,10 +281,10 @@ function expectSeedOnlyUserContext(context: unknown) {

266281

describe("runBtwSideQuestion", () => {

267282

beforeEach(() => {

268283

streamSimpleMock.mockReset();

284+

readFileMock.mockReset();

285+

parseSessionEntriesMock.mockReset();

286+

migrateSessionEntriesMock.mockReset();

269287

buildSessionContextMock.mockReset();

270-

getLeafEntryMock.mockReset();

271-

branchMock.mockReset();

272-

resetLeafMock.mockReset();

273288

ensureOpenClawModelsJsonMock.mockReset();

274289

discoverAuthStorageMock.mockReset();

275290

discoverModelsMock.mockReset();

@@ -284,10 +299,25 @@ describe("runBtwSideQuestion", () => {

284299

registerProviderStreamForModelMock.mockReset();

285300

diagDebugMock.mockReset();

286301287-

buildSessionContextMock.mockReturnValue({

288-

messages: [{ role: "user", content: [{ type: "text", text: "hi" }], timestamp: 1 }],

302+

readFileMock.mockResolvedValue("mock transcript");

303+

parseSessionEntriesMock.mockReturnValue([

304+

createTranscriptEntry({

305+

id: "user-1",

306+

message: { role: "user", content: [{ type: "text", text: "hi" }], timestamp: 1 },

307+

}),

308+

createTranscriptEntry({

309+

id: "assistant-1",

310+

parentId: "user-1",

311+

message: {

312+

role: "assistant",

313+

content: [{ type: "text", text: "hello" }],

314+

timestamp: 2,

315+

},

316+

}),

317+

]);

318+

buildSessionContextMock.mockImplementation((entries: Array<{ message?: unknown }> = []) => {

319+

return { messages: entries.flatMap((entry) => (entry.message ? [entry.message] : [])) };

289320

});

290-

getLeafEntryMock.mockReturnValue(null);

291321

resolveModelWithRegistryMock.mockReturnValue({

292322

provider: "anthropic",

293323

id: "claude-sonnet-4-6",

@@ -662,47 +692,70 @@ describe("runBtwSideQuestion", () => {

662692

});

663693664694

it("branches away from an unresolved trailing user turn before building BTW context", async () => {

665-

getLeafEntryMock.mockReturnValue({

666-

type: "message",

695+

const assistantEntry = createTranscriptEntry({

696+

id: "assistant-1",

697+

message: createAssistantTranscriptMessage([{ type: "text", text: "seed answer" }]),

698+

});

699+

const trailingUserEntry = createTranscriptEntry({

700+

id: "user-2",

667701

parentId: "assistant-1",

668-

message: { role: "user" },

702+

message: createUserTranscriptMessage([{ type: "text", text: "unfinished task" }]),

669703

});

704+

mockTranscriptEntries([assistantEntry, trailingUserEntry]);

670705

mockDoneAnswer(MATH_ANSWER);

671706672707

const result = await runMathSideQuestion();

673708674-

expect(branchMock).toHaveBeenCalledWith("assistant-1");

675-

expect(resetLeafMock).not.toHaveBeenCalled();

676-

expect(buildSessionContextMock).toHaveBeenCalledTimes(1);

709+

expect(buildSessionContextMock).toHaveBeenCalledWith([assistantEntry]);

677710

expect(result).toEqual({ text: MATH_ANSWER });

678711

});

679712680713

it("branches to the active run snapshot leaf when the session is busy", async () => {

714+

const userEntry = createTranscriptEntry({

715+

id: "user-seed",

716+

message: createUserTranscriptMessage(),

717+

});

718+

const assistantEntry = createTranscriptEntry({

719+

id: "assistant-seed",

720+

parentId: "user-seed",

721+

message: createAssistantTranscriptMessage([{ type: "text", text: "seed answer" }]),

722+

});

723+

const newerEntry = createTranscriptEntry({

724+

id: "newer-user",

725+

parentId: "assistant-seed",

726+

message: createUserTranscriptMessage([{ type: "text", text: "newer unfinished task" }]),

727+

});

728+

mockTranscriptEntries([userEntry, assistantEntry, newerEntry]);

681729

getActiveEmbeddedRunSnapshotMock.mockReturnValue({

682730

transcriptLeafId: "assistant-seed",

683731

});

684732

mockDoneAnswer(MATH_ANSWER);

685733686734

const result = await runMathSideQuestion();

687735688-

expect(branchMock).toHaveBeenCalledWith("assistant-seed");

689-

expect(getLeafEntryMock).not.toHaveBeenCalled();

736+

expect(buildSessionContextMock).toHaveBeenCalledWith([userEntry, assistantEntry]);

690737

expect(result).toEqual({ text: MATH_ANSWER });

691738

});

692739693740

it("falls back when the active run snapshot leaf no longer exists", async () => {

741+

const userEntry = createTranscriptEntry({

742+

id: "user-seed",

743+

message: createUserTranscriptMessage(),

744+

});

745+

const assistantEntry = createTranscriptEntry({

746+

id: "assistant-seed",

747+

parentId: "user-seed",

748+

message: createAssistantTranscriptMessage([{ type: "text", text: "seed answer" }]),

749+

});

750+

mockTranscriptEntries([userEntry, assistantEntry]);

694751

getActiveEmbeddedRunSnapshotMock.mockReturnValue({

695752

transcriptLeafId: "assistant-gone",

696753

});

697-

branchMock.mockImplementationOnce(() => {

698-

throw new Error("Entry 3235c7c4 not found");

699-

});

700754

mockDoneAnswer(MATH_ANSWER);

701755702756

const result = await runMathSideQuestion();

703757704-

expect(branchMock).toHaveBeenCalledWith("assistant-gone");

705-

expect(resetLeafMock).toHaveBeenCalled();

758+

expect(buildSessionContextMock).toHaveBeenCalledWith([userEntry, assistantEntry]);

706759

expect(result).toEqual({ text: MATH_ANSWER });

707760

expect(diagDebugMock).toHaveBeenCalledWith(

708761

expect.stringContaining("btw snapshot leaf unavailable: sessionId=session-1"),