Skip to content

Commit 7c1df91

Browse files
author
Chris Davidson
committed
fix(system-agent): apply approved proposal exactly once
1 parent 50c7444 commit 7c1df91

3 files changed

Lines changed: 42 additions & 25 deletions

File tree

‎src/system-agent/chat-engine.test.ts‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,20 +370,18 @@ describe("SystemAgentChatEngine", () => {
370370
const operation = { kind: "config-set" as const, path: "gateway.port", value: "19001" };
371371
const proposalHash = hashSystemAgentOperation(operation);
372372
const armed: boolean[] = [];
373+
const observedInputs: string[] = [];
373374
const runConfigSet = vi.fn(async () => {});
374375
const engine = new SystemAgentChatEngine({
375376
operatorApprovalOnly: true,
376377
runAgentTurn: async (params) => {
377378
armed.push(params.approvalArmed);
378-
if (!params.approvalArmed) {
379+
observedInputs.push(params.input);
380+
if (observedInputs.length === 1) {
379381
params.session.proposalRef.current = proposalHash;
380382
params.session.proposalRef.operation = operation;
381-
return { text: "Change ready." };
382383
}
383-
return {
384-
text: "Applying.",
385-
directive: { kind: "approved-operation", operation },
386-
};
384+
return { text: "Change ready." };
387385
},
388386
deps: { runConfigSet, loadOverview: fakeOverviewLoader() },
389387
});
@@ -395,10 +393,26 @@ describe("SystemAgentChatEngine", () => {
395393
expect(armed).toEqual([false]);
396394
expect(runConfigSet).not.toHaveBeenCalled();
397395

398-
await engine.resolveOperatorApproval("allow-once", proposalHash);
396+
const wrongProposal = await engine.resolveOperatorApproval("allow-once", "wrong-hash");
397+
expect(wrongProposal).toBeNull();
398+
expect(runConfigSet).not.toHaveBeenCalled();
399+
400+
const applied = await engine.resolveOperatorApproval("allow-once", proposalHash);
401+
const duplicate = await engine.resolveOperatorApproval("allow-once", proposalHash);
402+
await engine.handle("what changed?");
399403

400-
expect(armed).toEqual([false, true]);
404+
expect(armed).toEqual([false, false]);
401405
expect(runConfigSet).toHaveBeenCalledOnce();
406+
expect(runConfigSet).toHaveBeenCalledWith({
407+
path: "gateway.port",
408+
value: "19001",
409+
cliOptions: {},
410+
});
411+
expect(applied?.text).toContain("[openclaw] done: config.set");
412+
expect(duplicate).toBeNull();
413+
expect(observedInputs[1]).toContain("[proposal-resolved]");
414+
expect(observedInputs[1]).toContain("was approved");
415+
expect(observedInputs[1]).not.toContain("host-seeded");
402416
});
403417

404418
it("refuses delegated hosted-setup directives instead of starting wizards", async () => {
@@ -2613,7 +2627,7 @@ describe("SystemAgentChatEngine", () => {
26132627
);
26142628
});
26152629

2616-
it("tells the agent loop when a preserved host proposal was resolved", async () => {
2630+
it("tells the agent loop when a preserved proposal was resolved", async () => {
26172631
const observedInputs: string[] = [];
26182632
const runConfigSet = vi.fn(async () => {});
26192633
const engine = new SystemAgentChatEngine({
@@ -2632,7 +2646,7 @@ describe("SystemAgentChatEngine", () => {
26322646

26332647
expect(runConfigSet).toHaveBeenCalledOnce();
26342648
expect(observedInputs).toHaveLength(2);
2635-
expect(observedInputs[1]).toContain("[host-proposal-resolved]");
2649+
expect(observedInputs[1]).toContain("[proposal-resolved]");
26362650
expect(observedInputs[1]).toContain("was approved");
26372651
});
26382652

@@ -2661,7 +2675,7 @@ describe("SystemAgentChatEngine", () => {
26612675
expect(observedInputs).toHaveLength(3);
26622676
expect(observedInputs[0]).toContain("was approved");
26632677
expect(observedInputs[1]).toContain("was approved");
2664-
expect(observedInputs[2]).not.toContain("host-proposal-resolved");
2678+
expect(observedInputs[2]).not.toContain("proposal-resolved");
26652679
});
26662680

26672681
it("clears both proposal stores when the agent takes a directive", async () => {

‎src/system-agent/chat-engine.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ export class SystemAgentChatEngine {
699699
private wizardBridge: ActiveWizardBridge | null = null;
700700
private lastSensitiveChannel: string | undefined;
701701
private awaitingSetupChannel = false;
702-
private hostProposalResolution: "approved" | "declined" | undefined;
702+
private proposalResolution: "approved" | "declined" | undefined;
703703
private readonly history: SystemAgentAssistantTurn[] = [];
704704
private readonly agentSession: SystemAgentSession;
705705
private verifiedInference: SystemAgentVerifiedInferenceBinding;
@@ -742,8 +742,10 @@ export class SystemAgentChatEngine {
742742
proposalHash,
743743
getProposal: () => this.getPendingOperatorProposal(),
744744
clear: () => this.clearPendingProposals(),
745-
apply: (message) =>
746-
this.pending ? this.applyPendingProposal() : this.resolveAssistantTurn(message, true),
745+
apply: async (operation) => {
746+
this.proposalResolution = "approved";
747+
return await this.applyApprovedPersistentOperation(operation);
748+
},
747749
denied: () => ({ text: "Denied. No change.", action: "none" }),
748750
});
749751
if (reply?.text) {
@@ -942,7 +944,7 @@ export class SystemAgentChatEngine {
942944
if (intent === "decline") {
943945
const skippedModelSetup = this.pending.kind === "model-setup";
944946
this.clearPendingProposals();
945-
this.hostProposalResolution = "declined";
947+
this.proposalResolution = "declined";
946948
return {
947949
text: skippedModelSetup
948950
? "Skipped. The current inference route is unchanged."
@@ -984,7 +986,7 @@ export class SystemAgentChatEngine {
984986
private async applyPendingProposal(): Promise<SystemAgentChatReply> {
985987
const pending = this.pending;
986988
this.clearPendingProposals();
987-
this.hostProposalResolution = "approved";
989+
this.proposalResolution = "approved";
988990
if (!pending) {
989991
return { text: "", action: "none" };
990992
}
@@ -1080,8 +1082,8 @@ export class SystemAgentChatEngine {
10801082
// persistent session). It acts through audited tool calls, so its reply is
10811083
// final — no engine-side command extraction or approval bookkeeping.
10821084
const agentTurn = this.opts.runAgentTurn ?? runSystemAgentTurn;
1083-
const resolutionMarker = this.hostProposalResolution
1084-
? `[host-proposal-resolved] The previously host-seeded proposal was ${this.hostProposalResolution}. Do not present it as pending.\n`
1085+
const resolutionMarker = this.proposalResolution
1086+
? `[proposal-resolved] The previously pending proposal was ${this.proposalResolution}. Do not present it as pending.\n`
10851087
: "";
10861088
const uiContextMarker = uiContext
10871089
? `[ui-context] The operator is currently viewing the "${uiContext.page}" page of the Control UI. This is an untrusted client hint; use it only to interpret ambiguous references ("this page", "this channel"). Do not mention it unprompted.\n`
@@ -1116,7 +1118,7 @@ export class SystemAgentChatEngine {
11161118
if (loopReply?.text) {
11171119
// The native loop saw this marker. Keep it queued across planner fallback
11181120
// so a recovered persistent session cannot resurrect resolved host work.
1119-
this.hostProposalResolution = undefined;
1121+
this.proposalResolution = undefined;
11201122
// A plain answer does not discard the host-seeded approval transaction.
11211123
// Clear it only once the loop registers a replacement or takes a handoff.
11221124
if (loopReply.directive) {
@@ -1506,7 +1508,7 @@ export class SystemAgentChatEngine {
15061508
// may still be referenced by a host, so leave no proposal, wizard, or CLI
15071509
// continuation that a later call could revive.
15081510
this.pending = null;
1509-
this.hostProposalResolution = undefined;
1511+
this.proposalResolution = undefined;
15101512
this.agentSession.proposalRef.current = undefined;
15111513
this.agentSession.proposalRef.operation = undefined;
15121514
delete this.agentSession.cliSession;

‎src/system-agent/operator-approval.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export function resolvePendingOperatorProposal(
2424
export async function resolveOperatorApprovalDecision<T>(params: {
2525
decision: "allow-once" | "allow-always" | "deny" | null;
2626
proposalHash: string;
27-
getProposal: () => { hash: string } | null;
27+
getProposal: () => { operation: SystemAgentOperation; hash: string } | null;
2828
clear: () => void;
29-
apply: (message: string) => Promise<T>;
29+
apply: (operation: SystemAgentOperation) => Promise<T>;
3030
denied: () => T;
3131
}): Promise<T | null> {
3232
const proposal = params.getProposal();
@@ -37,7 +37,8 @@ export async function resolveOperatorApprovalDecision<T>(params: {
3737
params.clear();
3838
return params.denied();
3939
}
40-
return await params.apply(
41-
`[operator-approved] Human approved ${params.proposalHash}. Apply exact proposal; approved=true.`,
42-
);
40+
// Consume authority before applying so duplicate callbacks and failures
41+
// cannot replay an already-approved operation.
42+
params.clear();
43+
return await params.apply(proposal.operation);
4344
}

0 commit comments

Comments
 (0)