mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-11-18 15:07:57 +00:00
fix(acp): restore managed auto-memory extraction (#12018)
Some checks are pending
Qwen Code CI / Classify PR (push) Waiting to run
Qwen Code CI / Test (ubuntu-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Lint & Static (ubuntu-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / web-shell E2E Smoke (ubuntu-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Test (macos-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Test (windows-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Integration Tests (no-AK, No Sandbox) (push) Blocked by required conditions
Qwen Code CI / Integration Tests (CLI, No Sandbox) (push) Blocked by required conditions
Qwen Code CI / Desktop Shell (ubuntu-22.04) (push) Blocked by required conditions
Qwen Code CI / Desktop Shell (windows-2022) (push) Blocked by required conditions
E2E Tests / Build for E2E (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:docker - shard 1/1 (push) Blocked by required conditions
E2E Tests / E2E Test (Linux) - sandbox:none - shard 1/1 (push) Blocked by required conditions
E2E Tests / E2E Test - macOS - shard 1/2 (push) Blocked by required conditions
E2E Tests / E2E Test - macOS - shard 2/2 (push) Blocked by required conditions
E2E Tests / E2E Interactive - OpenTUI renderer (bun) (push) Blocked by required conditions
E2E Tests / channel-plugin E2E (nightly) (push) Blocked by required conditions
E2E Tests / cron-interactive E2E (nightly) (push) Blocked by required conditions
E2E Tests / web-shell Browser Regression (push) Waiting to run
npm cache producer / Save npm cache (push) Waiting to run
pnpm Lock Freshness / Lockfile freshness (push) Waiting to run
pnpm Worktree Smoke / Install (macos-latest) (push) Waiting to run
pnpm Worktree Smoke / Install (ubuntu-latest) (push) Waiting to run
pnpm Worktree Smoke / Install (windows-latest) (push) Waiting to run
SDK Java / macos-latest / Java 21 (push) Waiting to run
SDK Java / ubuntu-latest / Java 21 (push) Waiting to run
SDK Java / windows-latest / Java 21 (push) Waiting to run
SDK Java / Real daemon E2E / Java 11 (push) Waiting to run
SDK Java / ubuntu-latest / Java 11 (push) Waiting to run
SDK Java / ubuntu-latest / Java 17 (push) Waiting to run
Some checks are pending
Qwen Code CI / Classify PR (push) Waiting to run
Qwen Code CI / Test (ubuntu-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Lint & Static (ubuntu-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / web-shell E2E Smoke (ubuntu-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Test (macos-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Test (windows-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Integration Tests (no-AK, No Sandbox) (push) Blocked by required conditions
Qwen Code CI / Integration Tests (CLI, No Sandbox) (push) Blocked by required conditions
Qwen Code CI / Desktop Shell (ubuntu-22.04) (push) Blocked by required conditions
Qwen Code CI / Desktop Shell (windows-2022) (push) Blocked by required conditions
E2E Tests / Build for E2E (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:docker - shard 1/1 (push) Blocked by required conditions
E2E Tests / E2E Test (Linux) - sandbox:none - shard 1/1 (push) Blocked by required conditions
E2E Tests / E2E Test - macOS - shard 1/2 (push) Blocked by required conditions
E2E Tests / E2E Test - macOS - shard 2/2 (push) Blocked by required conditions
E2E Tests / E2E Interactive - OpenTUI renderer (bun) (push) Blocked by required conditions
E2E Tests / channel-plugin E2E (nightly) (push) Blocked by required conditions
E2E Tests / cron-interactive E2E (nightly) (push) Blocked by required conditions
E2E Tests / web-shell Browser Regression (push) Waiting to run
npm cache producer / Save npm cache (push) Waiting to run
pnpm Lock Freshness / Lockfile freshness (push) Waiting to run
pnpm Worktree Smoke / Install (macos-latest) (push) Waiting to run
pnpm Worktree Smoke / Install (ubuntu-latest) (push) Waiting to run
pnpm Worktree Smoke / Install (windows-latest) (push) Waiting to run
SDK Java / macos-latest / Java 21 (push) Waiting to run
SDK Java / ubuntu-latest / Java 21 (push) Waiting to run
SDK Java / windows-latest / Java 21 (push) Waiting to run
SDK Java / Real daemon E2E / Java 11 (push) Waiting to run
SDK Java / ubuntu-latest / Java 11 (push) Waiting to run
SDK Java / ubuntu-latest / Java 17 (push) Waiting to run
* fix(acp): restore managed auto-memory extraction
* fix(acp): refresh cache-safe slot at turn boundary; pin inFlight cleanup
R1-1: #recordPromptCompletionEffects only captures cache-safe params on a
fresh user turn with managed auto-memory enabled, but the process-global slot
has a second consumer with no freshness check: #maybeEmitFollowupSuggestion ->
generatePromptSuggestion, which prefers a session-matching slot over the live
getHistoryTail(40, true) it is handed and seeds the forked chat from
cacheSafeParams.history verbatim. A continue/retry turn that still ends
end_turn therefore got a follow-up suggestion generated from a transcript
missing that turn. Capture at the turn boundary in prompt() instead, mirroring
the interactive path in LlmClient.sendMessageStream. A failed turn never
reaches the success path, so it still does not publish a partial transcript.
R1-2: the new manager.test.ts case pinned that a rejection handler is attached
but not that it still deletes the inFlight entry -- hollowing the handler to
() => {} left the suite green at 49/49. Assert the map is empty after a failed
extraction; the mutant now fails 1 | 48.
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Patrol-Run: qwen-pr-closeout/jmu44usj48n
* test(acp): stub captureCacheSafeParams in Session LlmClient doubles
The turn-boundary refresh added in this branch calls
`config.getLlmClient().captureCacheSafeParams()` on every `end_turn`
completion. Session.test.ts's mock was updated, but the two focused
Session suites build their own inline LlmClient doubles, so the new call
threw `TypeError: ...captureCacheSafeParams is not a function` and broke
11 tests (8 in Session.worktree, 3 in Session.review-lease).
Add the missing `vi.fn()` stub in the same position as Session.test.ts
uses, matching the neighbouring managed-auto-memory stubs. Test-only
change; the real LlmClient already exposes the method.
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Patrol-Run: qwen-pr-closeout/jmu46zybs8q
---------
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
27979d2528
commit
b8def02aad
7 changed files with 90 additions and 21 deletions
|
|
@ -95,6 +95,7 @@ describe('Session review-worktree lease sweep', () => {
|
|||
beginManagedAutoMemoryRecall: vi.fn(),
|
||||
consumeManagedAutoMemoryRecall: vi.fn().mockResolvedValue(null),
|
||||
finishManagedAutoMemoryRecall: vi.fn(),
|
||||
captureCacheSafeParams: vi.fn(),
|
||||
recordCompletedToolCall: vi.fn(),
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -555,6 +555,7 @@ describe('Session', () => {
|
|||
beginManagedAutoMemoryRecall: ReturnType<typeof vi.fn>;
|
||||
consumeManagedAutoMemoryRecall: ReturnType<typeof vi.fn>;
|
||||
finishManagedAutoMemoryRecall: ReturnType<typeof vi.fn>;
|
||||
captureCacheSafeParams: ReturnType<typeof vi.fn>;
|
||||
recordCompletedToolCall: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
let mockMemoryManager: {
|
||||
|
|
@ -809,6 +810,7 @@ describe('Session', () => {
|
|||
beginManagedAutoMemoryRecall: vi.fn(),
|
||||
consumeManagedAutoMemoryRecall: vi.fn().mockResolvedValue(null),
|
||||
finishManagedAutoMemoryRecall: vi.fn(),
|
||||
captureCacheSafeParams: vi.fn(),
|
||||
recordCompletedToolCall: vi.fn(),
|
||||
};
|
||||
mockMemoryManager = {
|
||||
|
|
@ -2944,6 +2946,15 @@ describe('Session', () => {
|
|||
expect.any(AbortSignal),
|
||||
);
|
||||
expect(textParts(firstSentMessage())).toEqual([memoryPrompt, 'hello']);
|
||||
// Captured twice: once inside `#recordPromptCompletionEffects` (before
|
||||
// `scheduleExtract`, pinned below) and once at the turn boundary that
|
||||
// feeds the follow-up suggestion.
|
||||
expect(mockLlmClient.captureCacheSafeParams).toHaveBeenCalledTimes(2);
|
||||
expect(
|
||||
mockLlmClient.captureCacheSafeParams.mock.invocationCallOrder[0],
|
||||
).toBeLessThan(
|
||||
mockMemoryManager.scheduleExtract.mock.invocationCallOrder[0]!,
|
||||
);
|
||||
expect(mockMemoryManager.scheduleExtract).toHaveBeenCalledWith({
|
||||
projectRoot: '/repo',
|
||||
sessionId: 'test-session-id',
|
||||
|
|
@ -3040,6 +3051,11 @@ describe('Session', () => {
|
|||
expect(mockLlmClient.beginManagedAutoMemoryRecall).not.toHaveBeenCalled();
|
||||
expect(mockMemoryManager.scheduleExtract).not.toHaveBeenCalled();
|
||||
expect(mockMemoryManager.scheduleDream).not.toHaveBeenCalled();
|
||||
// A retry skips managed auto-memory, but the turn still ends `end_turn`,
|
||||
// so the follow-up suggestion fires and reads the process-global
|
||||
// cache-safe slot. The turn boundary has to refresh it anyway, or the
|
||||
// suggestion is generated from a transcript missing this turn.
|
||||
expect(mockLlmClient.captureCacheSafeParams).toHaveBeenCalledOnce();
|
||||
|
||||
mockChat.sendMessageStream = vi
|
||||
.fn()
|
||||
|
|
@ -3054,6 +3070,9 @@ describe('Session', () => {
|
|||
expect(mockLlmClient.beginManagedAutoMemoryRecall).toHaveBeenCalledOnce();
|
||||
expect(mockMemoryManager.scheduleExtract).not.toHaveBeenCalled();
|
||||
expect(mockMemoryManager.scheduleDream).not.toHaveBeenCalled();
|
||||
// A failed turn never reaches the success path, so it must not publish
|
||||
// its partial transcript as cache-safe.
|
||||
expect(mockLlmClient.captureCacheSafeParams).toHaveBeenCalledOnce();
|
||||
expect(
|
||||
mockLlmClient.finishManagedAutoMemoryRecall,
|
||||
).toHaveBeenCalledOnce();
|
||||
|
|
|
|||
|
|
@ -5361,6 +5361,19 @@ export class Session implements SessionContext {
|
|||
// Drain any cron prompts that queued while the prompt was active
|
||||
void this.#drainCronQueue();
|
||||
void this.#drainNotificationQueue();
|
||||
// Refresh the process-global cache-safe slot at the turn boundary.
|
||||
// `#recordPromptCompletionEffects` only captures on a fresh user turn
|
||||
// with managed auto-memory on, but the slot's other consumer —
|
||||
// `generatePromptSuggestion`, reached through
|
||||
// `#maybeEmitFollowupSuggestion` below — has no freshness check and
|
||||
// prefers the slot over the live history tail it is handed. Without
|
||||
// this, a continue/retry turn that still ends `end_turn` would get a
|
||||
// suggestion generated from a transcript missing that turn. Mirrors the
|
||||
// interactive path, which captures on every main turn in
|
||||
// `LlmClient.sendMessageStream`.
|
||||
if (completedResult.stopReason === 'end_turn') {
|
||||
this.config.getLlmClient().captureCacheSafeParams();
|
||||
}
|
||||
this.#maybeEmitFollowupSuggestion(completedResult);
|
||||
if (channelDelivery && completedResult.stopReason === 'end_turn') {
|
||||
this.#scheduleChannelDelivery({
|
||||
|
|
@ -8760,6 +8773,7 @@ export class Session implements SessionContext {
|
|||
) {
|
||||
return;
|
||||
}
|
||||
this.config.getLlmClient().captureCacheSafeParams();
|
||||
const memoryManager = this.config.getMemoryManager();
|
||||
const history = this.#getCurrentChat().getHistoryShallow();
|
||||
void memoryManager
|
||||
|
|
|
|||
|
|
@ -106,6 +106,7 @@ describe('Session.pendingWorktreeNotice', () => {
|
|||
beginManagedAutoMemoryRecall: vi.fn(),
|
||||
consumeManagedAutoMemoryRecall: vi.fn().mockResolvedValue(null),
|
||||
finishManagedAutoMemoryRecall: vi.fn(),
|
||||
captureCacheSafeParams: vi.fn(),
|
||||
recordCompletedToolCall: vi.fn(),
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1355,6 +1355,26 @@ export class LlmClient {
|
|||
}
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
captureCacheSafeParams(): void {
|
||||
try {
|
||||
const chat = this.getChat();
|
||||
const historyForCache = this.getHistoryTailShallow(40, true);
|
||||
const cachedHistory = slimCompactionInput(
|
||||
historyForCache,
|
||||
this.config.getEffectiveInputModalities(),
|
||||
).slimmedHistory;
|
||||
saveCacheSafeParams(
|
||||
chat.getGenerationConfig(),
|
||||
cachedHistory,
|
||||
this.config.getModel(),
|
||||
this.config.getSessionId(),
|
||||
);
|
||||
} catch {
|
||||
// Best-effort — don't block the main flow
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
consumeManagedAutoMemoryRecall(
|
||||
deliveryPoint: 'initial' | 'tool_result',
|
||||
|
|
@ -4836,26 +4856,7 @@ export class LlmClient {
|
|||
// Save cache-safe params here — before any early return — so that
|
||||
// background readers calling getCacheSafeParams(sessionId) can see the
|
||||
// current turn's history regardless of which path exits below.
|
||||
try {
|
||||
const chat = this.getChat();
|
||||
const maxHistoryForCache = 40;
|
||||
const historyForCache = this.getHistoryTailShallow(
|
||||
maxHistoryForCache,
|
||||
true,
|
||||
);
|
||||
const cachedHistory = slimCompactionInput(
|
||||
historyForCache,
|
||||
this.config.getEffectiveInputModalities(),
|
||||
).slimmedHistory;
|
||||
saveCacheSafeParams(
|
||||
chat.getGenerationConfig(),
|
||||
cachedHistory,
|
||||
this.config.getModel(),
|
||||
this.config.getSessionId(),
|
||||
);
|
||||
} catch {
|
||||
// Best-effort — don't block the main flow
|
||||
}
|
||||
this.captureCacheSafeParams();
|
||||
|
||||
if (this.config.getSkipNextSpeakerCheck()) {
|
||||
if (!isGoalRuntimeTurn) {
|
||||
|
|
|
|||
|
|
@ -127,6 +127,36 @@ describe('MemoryManager', () => {
|
|||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('does not emit an unhandled rejection when the caller handles a failed extraction', async () => {
|
||||
const failure = new Error('extract failed');
|
||||
const unhandled = vi.fn();
|
||||
vi.mocked(runAutoMemoryExtract).mockRejectedValueOnce(failure);
|
||||
process.on('unhandledRejection', unhandled);
|
||||
|
||||
try {
|
||||
const mgr = new MemoryManager();
|
||||
await expect(
|
||||
mgr.scheduleExtract({
|
||||
projectRoot,
|
||||
sessionId: 'sess',
|
||||
history: [{ role: 'user', parts: [{ text: 'hi' }] }],
|
||||
}),
|
||||
).rejects.toBe(failure);
|
||||
await new Promise<void>((resolve) => setImmediate(resolve));
|
||||
|
||||
expect(unhandled).not.toHaveBeenCalled();
|
||||
// The rejection handler must still untrack the task. Hollowing it out
|
||||
// to `() => {}` keeps the assertion above green while the settled
|
||||
// promise and its task id leak for the process lifetime — `inFlight`
|
||||
// has no other delete site and no `clear()`.
|
||||
expect(
|
||||
(mgr as unknown as { inFlight: Map<string, unknown> }).inFlight.size,
|
||||
).toBe(0);
|
||||
} finally {
|
||||
process.off('unhandledRejection', unhandled);
|
||||
}
|
||||
});
|
||||
|
||||
it('runs extract and records a completed task', async () => {
|
||||
vi.mocked(runAutoMemoryExtract).mockResolvedValue({
|
||||
touchedTopics: ['user'],
|
||||
|
|
|
|||
|
|
@ -582,7 +582,10 @@ export class MemoryManager {
|
|||
|
||||
private track<T>(taskId: string, promise: Promise<T>): Promise<T> {
|
||||
this.inFlight.set(taskId, promise);
|
||||
void promise.finally(() => this.inFlight.delete(taskId));
|
||||
void promise.then(
|
||||
() => this.inFlight.delete(taskId),
|
||||
() => this.inFlight.delete(taskId),
|
||||
);
|
||||
return promise;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue