Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,35 @@ describe("PowerContext lifecycle", () => {
expect(harness.warnings).toEqual([]);
});

it("captures a turn under the projects its prompt was built with", async () => {
const harness = createLifecycleHarness();
const beforePromptBuild = harness.hooks.get("before_prompt_build")!;
const agentEnd = harness.hooks.get("agent_end")!;
const session = { agentId: "main", sessionId: "session-1", sessionKey: "agent:main:telegram:direct:user-1" };
const turn = async (runId: string, content: string, endProjects?: string[]) => {
const messages = [{ role: "user", content }];
await beforePromptBuild(
{ messages, prompt: content },
{ ...session, runId, activeProjectKeys: ["/workspace/project-a"] },
);
// OpenClaw's `agent_end` context carries the run but not its active projects.
await agentEnd(
{ success: true, runId, messages },
{ ...session, runId, ...(endProjects ? { activeProjectKeys: endProjects } : {}) },
);
};

await turn("run-1", "recall and capture this");
harness.config.autoRecall = false;
await turn("run-2", "capture this without recall");
// Projects the host supplies to `agent_end` itself take precedence.
await turn("run-3", "capture this under the reported project", ["/workspace/project-b"]);

expect(harness.prepareRequests.map((request) => request.scope_id)).toEqual(["scp_project_a"]);
expect(harness.capturedScopes).toEqual(["scp_project_a", "scp_project_a", "scp_project_b"]);
expect(harness.warnings).toEqual([]);
});

it("keeps capture and flush on one resolved Scope during compaction", async () => {
const harness = createLifecycleHarness();
const beforeCompaction = harness.hooks.get("before_compaction");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type LifecycleDependencies = {
};

const MAX_SESSION_SCOPES = 32;
const MAX_TRACKED_RUNS = 64;
const CLOSING_BUDGET_MS = 1000;

type FlushProgress = {
Expand Down Expand Up @@ -127,6 +128,27 @@ export function registerPowerContextLifecycle(api: OpenClawPluginApi, deps: Life
});
};
const sessionScopes = new Map<string, Set<string>>();
// OpenClaw supplies a run's active projects to `before_prompt_build` but not to `agent_end`; capture resolves
// with the projects of the run it captures, so it reaches the same project binding as that run's recall.
const runProjectKeys = new Map<string, readonly string[]>();
const rememberRunProjects = (runId: string | undefined, projectKeys: readonly string[] | undefined) => {
if (!runId || !projectKeys) {
return;
}
runProjectKeys.delete(runId);
runProjectKeys.set(runId, [...projectKeys]);
if (runProjectKeys.size > MAX_TRACKED_RUNS) {
runProjectKeys.delete(runProjectKeys.keys().next().value!);
}
};
const takeRunProjects = (runId: string | undefined): readonly string[] | undefined => {
if (!runId) {
return undefined;
}
const projectKeys = runProjectKeys.get(runId);
runProjectKeys.delete(runId);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve project bindings across completion replays

Replaying a successful agent_end without project keys—a case already covered by the lifecycle's replay contract—now resolves to the agent/default Scope because the first delivery deletes the saved binding. Since Source deduplication is Scope-local, this duplicates the project prompt into another Scope; preserve replay-safe binding resolution or suppress already captured runs while keeping memory bounded.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5a91c50. agent_end no longer drops the run's projects: it moves them to a separate map of completed runs, so a replayed agent_end, including a retry after a failed capture, resolves the same project Scope. Both maps keep their 64 most recent runs, so completed runs never evict runs still in progress.

captures a replayed turn under the same project binding and retries a failed capture under the same project binding when the turn is replayed cover the two cases. Both fail at a86fc780, where the replay goes to scp_default.

return projectKeys;
};
const pendingPositions = new Map<string, number>();
const confirmedCursors = new Map<string, number>();
const clearConfirmedPending = (scopeId: string): boolean => {
Expand Down Expand Up @@ -389,6 +411,7 @@ export function registerPowerContextLifecycle(api: OpenClawPluginApi, deps: Life
};

api.on("before_prompt_build", async (event, ctx) => {
rememberRunProjects(ctx.runId, ctx.activeProjectKeys);
const config = deps.getConfig();
const agentId = readAgentId(ctx.agentId);
if (
Expand Down Expand Up @@ -442,6 +465,7 @@ export function registerPowerContextLifecycle(api: OpenClawPluginApi, deps: Life
});

api.on("agent_end", async (event, ctx) => {
const runProjects = takeRunProjects(ctx.runId ?? event.runId);
const agentId = readAgentId(ctx.agentId);
if (!event.success || !agentId) {
return;
Expand All @@ -451,7 +475,7 @@ export function registerPowerContextLifecycle(api: OpenClawPluginApi, deps: Life
agentId,
sessionId: ctx.sessionId,
sessionKey: ctx.sessionKey,
activeProjectKeys: ctx.activeProjectKeys,
activeProjectKeys: ctx.activeProjectKeys ?? runProjects,
channel: ctx.channel ?? ctx.messageProvider,
messages: event.messages,
eventIdentity: lifecycleEventIdentity(event.messages, { runId: event.runId }),
Expand Down
Loading