feat(FN-2000): add memory auto-summarize settings and automation sync
- Add Memory section controls for enabling auto-summarize with threshold and cron schedule inputs - Wire ProjectEngine to sync auto-summarize automation on startup and when related settings change - Reuse a single startup settings snapshot when syncing insight extraction and auto-summarize automations - Add SettingsModal and ProjectEngine tests covering auto-summarize UI persistence and automation re-sync behavior
This commit is contained in:
197
packages/engine/src/project-engine.test.ts
Normal file
197
packages/engine/src/project-engine.test.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { ProjectEngine } from "./project-engine.js";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
syncInsightExtractionAutomation: vi.fn(),
|
||||
syncAutoSummarizeAutomation: vi.fn(),
|
||||
automationStoreInit: vi.fn(async () => undefined),
|
||||
createAiPromptExecutor: vi.fn(async () => vi.fn()),
|
||||
cronRunnerStart: vi.fn(),
|
||||
cronRunnerStop: vi.fn(),
|
||||
runtimeStart: vi.fn(async () => undefined),
|
||||
runtimeStop: vi.fn(async () => undefined),
|
||||
currentStore: null as Record<string, unknown> | null,
|
||||
}));
|
||||
|
||||
vi.mock("@fusion/core", async () => {
|
||||
class MockAutomationStore {
|
||||
constructor(_cwd: string) {}
|
||||
|
||||
init = mocks.automationStoreInit;
|
||||
}
|
||||
|
||||
return {
|
||||
AutomationStore: MockAutomationStore,
|
||||
syncInsightExtractionAutomation: mocks.syncInsightExtractionAutomation,
|
||||
syncAutoSummarizeAutomation: mocks.syncAutoSummarizeAutomation,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("./cron-runner.js", () => {
|
||||
return {
|
||||
CronRunner: vi.fn().mockImplementation(() => ({
|
||||
start: mocks.cronRunnerStart,
|
||||
stop: mocks.cronRunnerStop,
|
||||
})),
|
||||
createAiPromptExecutor: mocks.createAiPromptExecutor,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("./pr-monitor.js", () => ({
|
||||
PrMonitor: vi.fn().mockImplementation(() => ({
|
||||
onNewComments: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock("./pr-comment-handler.js", () => ({
|
||||
PrCommentHandler: vi.fn().mockImplementation(() => ({
|
||||
handleNewComments: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock("./notifier.js", () => ({
|
||||
NtfyNotifier: vi.fn().mockImplementation(() => ({
|
||||
start: vi.fn(async () => undefined),
|
||||
stop: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock("./runtimes/in-process-runtime.js", () => ({
|
||||
InProcessRuntime: vi.fn().mockImplementation(() => ({
|
||||
start: mocks.runtimeStart,
|
||||
stop: mocks.runtimeStop,
|
||||
getTaskStore: () => mocks.currentStore,
|
||||
getAgentStore: vi.fn(),
|
||||
getMessageStore: vi.fn(),
|
||||
getRoutineStore: vi.fn(),
|
||||
getRoutineRunner: vi.fn(),
|
||||
getHeartbeatMonitor: vi.fn(),
|
||||
getTriggerScheduler: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
type SettingsHandlerPayload = {
|
||||
settings: Record<string, unknown>;
|
||||
previous: Record<string, unknown>;
|
||||
};
|
||||
|
||||
function createMockStore(initialSettings: Record<string, unknown>) {
|
||||
let settings = { ...initialSettings };
|
||||
const settingsHandlers = new Set<(payload: SettingsHandlerPayload) => void | Promise<void>>();
|
||||
|
||||
const store = {
|
||||
getSettings: vi.fn(async () => ({ ...settings })),
|
||||
listTasks: vi.fn(async () => []),
|
||||
on: vi.fn((event: string, handler: (...args: unknown[]) => void | Promise<void>) => {
|
||||
if (event === "settings:updated") {
|
||||
settingsHandlers.add(handler as (payload: SettingsHandlerPayload) => void | Promise<void>);
|
||||
}
|
||||
}),
|
||||
off: vi.fn((event: string, handler: (...args: unknown[]) => void | Promise<void>) => {
|
||||
if (event === "settings:updated") {
|
||||
settingsHandlers.delete(
|
||||
handler as (payload: SettingsHandlerPayload) => void | Promise<void>,
|
||||
);
|
||||
}
|
||||
}),
|
||||
};
|
||||
|
||||
const emitSettingsUpdated = async (
|
||||
next: Record<string, unknown>,
|
||||
previous: Record<string, unknown>,
|
||||
) => {
|
||||
settings = { ...next };
|
||||
for (const handler of settingsHandlers) {
|
||||
await handler({ settings: { ...next }, previous: { ...previous } });
|
||||
}
|
||||
};
|
||||
|
||||
return { store, emitSettingsUpdated };
|
||||
}
|
||||
|
||||
const baseSettings: Record<string, unknown> = {
|
||||
autoMerge: false,
|
||||
globalPause: false,
|
||||
enginePaused: false,
|
||||
pollIntervalMs: 15_000,
|
||||
taskStuckTimeoutMs: undefined,
|
||||
memoryAutoSummarizeEnabled: false,
|
||||
memoryAutoSummarizeThresholdChars: 50_000,
|
||||
memoryAutoSummarizeSchedule: "0 3 * * *",
|
||||
insightExtractionEnabled: false,
|
||||
insightExtractionSchedule: "0 3 * * *",
|
||||
insightExtractionMinIntervalMs: 0,
|
||||
};
|
||||
|
||||
function createEngine() {
|
||||
return new ProjectEngine(
|
||||
{
|
||||
projectId: "proj_test",
|
||||
workingDirectory: "/tmp/proj_test",
|
||||
isolationMode: "in-process",
|
||||
maxConcurrent: 2,
|
||||
maxWorktrees: 2,
|
||||
},
|
||||
{} as never,
|
||||
{ skipNotifier: true },
|
||||
);
|
||||
}
|
||||
|
||||
describe("ProjectEngine auto-summarize wiring", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
const mockStore = createMockStore(baseSettings);
|
||||
mocks.currentStore = mockStore.store;
|
||||
});
|
||||
|
||||
it("syncs auto-summarize automation on startup using one settings snapshot", async () => {
|
||||
const engine = createEngine();
|
||||
|
||||
await engine.start();
|
||||
|
||||
expect(mocks.syncInsightExtractionAutomation).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.syncAutoSummarizeAutomation).toHaveBeenCalledTimes(1);
|
||||
|
||||
const insightSettings = mocks.syncInsightExtractionAutomation.mock.calls[0][1];
|
||||
const autoSummarizeSettings = mocks.syncAutoSummarizeAutomation.mock.calls[0][1];
|
||||
expect(autoSummarizeSettings).toBe(insightSettings);
|
||||
|
||||
await engine.stop();
|
||||
});
|
||||
|
||||
it("re-syncs auto-summarize automation only when related settings change", async () => {
|
||||
const mockStore = createMockStore(baseSettings);
|
||||
mocks.currentStore = mockStore.store;
|
||||
const engine = createEngine();
|
||||
|
||||
await engine.start();
|
||||
mocks.syncAutoSummarizeAutomation.mockClear();
|
||||
|
||||
const previous = { ...baseSettings };
|
||||
const nextEnabled = {
|
||||
...previous,
|
||||
memoryAutoSummarizeEnabled: true,
|
||||
};
|
||||
|
||||
await mockStore.emitSettingsUpdated(nextEnabled, previous);
|
||||
expect(mocks.syncAutoSummarizeAutomation).toHaveBeenCalledTimes(1);
|
||||
|
||||
const unrelatedChange = {
|
||||
...nextEnabled,
|
||||
pollIntervalMs: 30_000,
|
||||
};
|
||||
|
||||
await mockStore.emitSettingsUpdated(unrelatedChange, nextEnabled);
|
||||
expect(mocks.syncAutoSummarizeAutomation).toHaveBeenCalledTimes(1);
|
||||
|
||||
const disabled = {
|
||||
...unrelatedChange,
|
||||
memoryAutoSummarizeEnabled: false,
|
||||
};
|
||||
|
||||
await mockStore.emitSettingsUpdated(disabled, unrelatedChange);
|
||||
expect(mocks.syncAutoSummarizeAutomation).toHaveBeenCalledTimes(2);
|
||||
|
||||
await engine.stop();
|
||||
});
|
||||
});
|
||||
@@ -168,17 +168,28 @@ export class ProjectEngine {
|
||||
scope: "project", // Project-scoped execution — global schedules run separately
|
||||
});
|
||||
|
||||
const settings = await store.getSettings();
|
||||
|
||||
// Sync insight extraction automation on startup
|
||||
try {
|
||||
const { syncInsightExtractionAutomation } = await import("@fusion/core");
|
||||
if (typeof syncInsightExtractionAutomation === "function") {
|
||||
const settings = await store.getSettings();
|
||||
await syncInsightExtractionAutomation(this.automationStore, settings);
|
||||
}
|
||||
} catch {
|
||||
// syncInsightExtractionAutomation may not be exported yet
|
||||
}
|
||||
|
||||
// Sync auto-summarize automation on startup
|
||||
try {
|
||||
const { syncAutoSummarizeAutomation } = await import("@fusion/core");
|
||||
if (typeof syncAutoSummarizeAutomation === "function") {
|
||||
await syncAutoSummarizeAutomation(this.automationStore, settings);
|
||||
}
|
||||
} catch {
|
||||
// syncAutoSummarizeAutomation may not be exported yet
|
||||
}
|
||||
|
||||
this.cronRunner.start();
|
||||
runtimeLog.log("CronRunner initialized and started");
|
||||
} catch (err) {
|
||||
@@ -969,6 +980,40 @@ export class ProjectEngine {
|
||||
};
|
||||
store.on("settings:updated", onInsightSettingsChange);
|
||||
this.settingsHandlers.push(onInsightSettingsChange);
|
||||
|
||||
// 6. Auto-summarize settings change — sync automation
|
||||
const onAutoSummarizeSettingsChange = async ({
|
||||
settings: s,
|
||||
previous: prev,
|
||||
}: {
|
||||
settings: Settings;
|
||||
previous: Settings;
|
||||
}) => {
|
||||
const autoSummarizeKeys = [
|
||||
"memoryAutoSummarizeEnabled",
|
||||
"memoryAutoSummarizeThresholdChars",
|
||||
"memoryAutoSummarizeSchedule",
|
||||
] as const;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const changed = autoSummarizeKeys.some((key) => (s as any)[key] !== (prev as any)[key]);
|
||||
if (!changed || !this.automationStore) return;
|
||||
|
||||
try {
|
||||
const { syncAutoSummarizeAutomation } = await import("@fusion/core");
|
||||
if (typeof syncAutoSummarizeAutomation === "function") {
|
||||
await syncAutoSummarizeAutomation(this.automationStore, s);
|
||||
runtimeLog.log("Auto-summarize automation synced with settings");
|
||||
}
|
||||
} catch (err) {
|
||||
runtimeLog.warn(
|
||||
"Failed to sync auto-summarize automation:",
|
||||
err instanceof Error ? err.message : err,
|
||||
);
|
||||
}
|
||||
};
|
||||
store.on("settings:updated", onAutoSummarizeSettingsChange);
|
||||
this.settingsHandlers.push(onAutoSummarizeSettingsChange);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user