Skip to content

Commit 3106ad3

Browse files
committed
fix(regression): preserve ACP turns without admin provenance scope
1 parent a1ab0d9 commit 3106ad3

2 files changed

Lines changed: 104 additions & 21 deletions

File tree

‎src/acp/translator.prompt-prefix.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,56 @@ describe("acp prompt cwd prefix", () => {
152152
{ expectFinal: true },
153153
);
154154
});
155+
156+
it("retries without provenance when the gateway rejects admin-only provenance fields", async () => {
157+
const requestSpy = vi
158+
.fn()
159+
.mockRejectedValueOnce(
160+
Object.assign(new Error("system provenance fields require admin scope"), {
161+
name: "GatewayClientRequestError",
162+
gatewayCode: "INVALID_REQUEST",
163+
}),
164+
)
165+
.mockRejectedValueOnce(new Error("stop-after-send"));
166+
const sessionStore = createInMemorySessionStore();
167+
sessionStore.createSession({
168+
sessionId: TEST_SESSION_ID,
169+
sessionKey: TEST_SESSION_KEY,
170+
cwd: path.join(os.homedir(), "openclaw-test"),
171+
});
172+
const agent = new AcpGatewayAgent(
173+
createAcpConnection(),
174+
createAcpGateway(requestSpy as unknown as GatewayClient["request"]),
175+
{
176+
sessionStore,
177+
provenanceMode: "meta+receipt",
178+
},
179+
);
180+
181+
await expect(agent.prompt(TEST_PROMPT)).rejects.toThrow("stop-after-send");
182+
expect(requestSpy).toHaveBeenCalledTimes(2);
183+
expect(requestSpy).toHaveBeenNthCalledWith(
184+
1,
185+
"chat.send",
186+
expect.objectContaining({
187+
systemInputProvenance: {
188+
kind: "external_user",
189+
originSessionId: TEST_SESSION_ID,
190+
sourceChannel: "acp",
191+
sourceTool: "openclaw_acp",
192+
},
193+
systemProvenanceReceipt: expect.stringContaining("[Source Receipt]"),
194+
}),
195+
{ expectFinal: true },
196+
);
197+
expect(requestSpy).toHaveBeenNthCalledWith(
198+
2,
199+
"chat.send",
200+
expect.not.objectContaining({
201+
systemInputProvenance: expect.anything(),
202+
systemProvenanceReceipt: expect.anything(),
203+
}),
204+
{ expectFinal: true },
205+
);
206+
});
155207
});

‎src/acp/translator.ts‎

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ type SessionUsageSnapshot = {
118118
used: number;
119119
};
120120

121+
function isAdminScopeProvenanceRejection(err: unknown): boolean {
122+
if (!(err instanceof Error)) {
123+
return false;
124+
}
125+
const gatewayCode =
126+
typeof (err as { gatewayCode?: unknown }).gatewayCode === "string"
127+
? (err as { gatewayCode?: string }).gatewayCode
128+
: undefined;
129+
return (
130+
err.name === "GatewayClientRequestError" &&
131+
gatewayCode === "INVALID_REQUEST" &&
132+
err.message.includes("system provenance fields require admin scope")
133+
);
134+
}
135+
121136
type SessionSnapshot = SessionPresentation & {
122137
metadata?: SessionMetadata;
123138
usage?: SessionUsageSnapshot;
@@ -642,6 +657,15 @@ export class AcpGatewayAgent implements Agent {
642657
const abortController = new AbortController();
643658
const runId = randomUUID();
644659
this.sessionStore.setActiveRun(params.sessionId, runId, abortController);
660+
const requestParams = {
661+
sessionKey: session.sessionKey,
662+
message,
663+
attachments: attachments.length > 0 ? attachments : undefined,
664+
idempotencyKey: runId,
665+
thinking: readString(params._meta, ["thinking", "thinkingLevel"]),
666+
deliver: readBool(params._meta, ["deliver"]),
667+
timeoutMs: readNumber(params._meta, ["timeoutMs"]),
668+
};
645669

646670
return new Promise<PromptResponse>((resolve, reject) => {
647671
this.pendingPrompts.set(params.sessionId, {
@@ -652,27 +676,34 @@ export class AcpGatewayAgent implements Agent {
652676
reject,
653677
});
654678

655-
this.gateway
656-
.request(
657-
"chat.send",
658-
{
659-
sessionKey: session.sessionKey,
660-
message,
661-
attachments: attachments.length > 0 ? attachments : undefined,
662-
idempotencyKey: runId,
663-
thinking: readString(params._meta, ["thinking", "thinkingLevel"]),
664-
deliver: readBool(params._meta, ["deliver"]),
665-
timeoutMs: readNumber(params._meta, ["timeoutMs"]),
666-
systemInputProvenance,
667-
systemProvenanceReceipt,
668-
},
669-
{ expectFinal: true },
670-
)
671-
.catch((err) => {
672-
this.pendingPrompts.delete(params.sessionId);
673-
this.sessionStore.clearActiveRun(params.sessionId);
674-
reject(err instanceof Error ? err : new Error(String(err)));
675-
});
679+
const sendWithProvenanceFallback = async () => {
680+
try {
681+
await this.gateway.request(
682+
"chat.send",
683+
{
684+
...requestParams,
685+
systemInputProvenance,
686+
systemProvenanceReceipt,
687+
},
688+
{ expectFinal: true },
689+
);
690+
} catch (err) {
691+
if (
692+
(systemInputProvenance || systemProvenanceReceipt) &&
693+
isAdminScopeProvenanceRejection(err)
694+
) {
695+
await this.gateway.request("chat.send", requestParams, { expectFinal: true });
696+
return;
697+
}
698+
throw err;
699+
}
700+
};
701+
702+
void sendWithProvenanceFallback().catch((err) => {
703+
this.pendingPrompts.delete(params.sessionId);
704+
this.sessionStore.clearActiveRun(params.sessionId);
705+
reject(err instanceof Error ? err : new Error(String(err)));
706+
});
676707
});
677708
}
678709

0 commit comments

Comments
 (0)