Handle legacy OpenClaw prepare gateways

This commit is contained in:
Haitao Pan 2026-06-06 06:52:11 +08:00
parent 1f617e9c63
commit 6acdb01eb4
2 changed files with 114 additions and 0 deletions

View File

@ -593,6 +593,17 @@ func (o *SessionOrchestrator) openClawArtifactPrepare(
notify,
)
if !prepareResult.OK {
if openClawPrepareUnsupported(prepareResult.Error) {
prepared := openClawLegacyPreparedArtifactScope(params, sessionKey, runID)
log.Printf(
"level=warn component=openclaw_gateway event=session_prepare_legacy_fallback provider=%q sessionId=%q runId=%q artifactScope=%q",
gatewayProvider,
sessionKey,
runID,
prepared.ArtifactScope,
)
return prepared, nil
}
return nil, gatewayRPCError(prepareResult.Error, "openclaw artifact prepare failed")
}
prepared := openClawPreparedArtifactScopeFromPayload(shared.AsMap(prepareResult.Payload))
@ -602,6 +613,48 @@ func (o *SessionOrchestrator) openClawArtifactPrepare(
return prepared, nil
}
func openClawPrepareUnsupported(errorPayload map[string]any) bool {
code := strings.ToUpper(strings.TrimSpace(shared.StringArg(errorPayload, "code", "")))
message := strings.ToLower(strings.TrimSpace(shared.StringArg(errorPayload, "message", "")))
if !strings.Contains(message, "xworkmate.session.prepare") {
return false
}
return code == "INVALID_REQUEST" ||
code == "METHOD_NOT_FOUND" ||
code == "UNKNOWN_METHOD" ||
strings.Contains(message, "unknown method") ||
strings.Contains(message, "method not found")
}
func openClawLegacyPreparedArtifactScope(params map[string]any, sessionKey string, runID string) *openClawPreparedArtifactScope {
sessionKey = strings.TrimSpace(sessionKey)
runID = strings.TrimSpace(runID)
artifactScope := "tasks/" + sessionKey + "/" + runID
workspaceRoot := openClawLegacyArtifactWorkspaceRoot(params)
return &openClawPreparedArtifactScope{
RemoteWorkingDirectory: workspaceRoot,
RemoteWorkspaceRefKind: "remotePath",
ArtifactScope: artifactScope,
ArtifactDirectory: filepath.Join(workspaceRoot, filepath.FromSlash(artifactScope)),
RelativeArtifactDirectory: artifactScope,
ScopeKind: "task",
}
}
func openClawLegacyArtifactWorkspaceRoot(params map[string]any) string {
for _, key := range []string{"remoteWorkingDirectoryHint", "remoteWorkingDirectory"} {
value := strings.TrimSpace(shared.StringArg(params, key, ""))
if value == "" {
continue
}
cleaned := filepath.Clean(value)
if strings.HasPrefix(cleaned, "/home/ubuntu/.openclaw/workspace") {
return strings.TrimRight(cleaned, string(os.PathSeparator))
}
}
return "/home/ubuntu/.openclaw/workspace"
}
func openClawSessionPrepareParams(params map[string]any, openClawSessionKey string, runID string, artifactContract openClawArtifactContract) map[string]any {
appThreadKey := openClawAppThreadKey(params)
result := map[string]any{

View File

@ -808,6 +808,54 @@ func TestExecuteSessionTaskGatewayNoDisplayableOutputFails(t *testing.T) {
}
}
func TestExecuteSessionTaskGatewayFallsBackWhenPrepareUnsupported(t *testing.T) {
gateway := newAcpFakeOpenClawGateway(t)
gateway.unsupportedSessionPrepare.Store(true)
defer gateway.Close()
t.Setenv("GATEWAY_RPC_URL", gateway.URL())
t.Setenv("BRIDGE_AUTH_TOKEN", "bridge-token")
server := NewServer()
response, rpcErr := server.executeSessionTask(task{
req: shared.RPCRequest{
Method: "session.start",
Params: map[string]any{
"sessionId": "session-openclaw-legacy-prepare",
"threadId": "thread-openclaw-legacy-prepare",
"taskPrompt": "say pong",
"workingDirectory": t.TempDir(),
"routing": map[string]any{
"routingMode": "explicit",
"explicitExecutionTarget": "gateway",
"preferredGatewayProviderId": "openclaw",
},
},
},
})
if rpcErr != nil {
t.Fatalf("expected legacy prepare fallback response, got rpc error: %#v", rpcErr)
}
if got := response["output"]; got != "gateway pong" {
t.Fatalf("expected gateway pong output after legacy prepare fallback, got %#v", response)
}
if got := gateway.Methods(); !sameMethods(got, []string{"connect", "xworkmate.session.prepare", "chat.send", "agent.wait", "xworkmate.artifacts.export", "xworkmate.artifacts.collect-and-snapshot"}) {
t.Fatalf("expected legacy prepare attempt to continue through chat/send and export, got %#v", got)
}
chatParams := gateway.LastChatSendParams()
receipt := strings.TrimSpace(shared.StringArg(chatParams, "systemProvenanceReceipt", ""))
sessionKey := shared.StringArg(chatParams, "sessionKey", "")
runID := shared.StringArg(chatParams, "idempotencyKey", "")
for _, expected := range []string{
"artifactDirectory: /home/ubuntu/.openclaw/workspace/tasks/" + sessionKey + "/" + runID,
"artifactScope: tasks/" + sessionKey + "/" + runID,
} {
if !strings.Contains(receipt, expected) {
t.Fatalf("expected fallback provenance receipt to include %q, got %q", expected, receipt)
}
}
}
func TestExecuteSessionTaskGatewayFailsClosedWhenOpenClawAcceptsDifferentSession(t *testing.T) {
gateway := newAcpFakeOpenClawGateway(t)
gateway.alternateSessionKey = "dashboard:c061bfeb-ad08-45f5-971d-d9018f745d7a"
@ -2657,6 +2705,7 @@ type acpFakeOpenClawGateway struct {
artifactWorkspaceRoot string
alternateRunID string
alternateSessionKey string
unsupportedSessionPrepare atomic.Bool
}
func newAcpFakeOpenClawGateway(t *testing.T) *acpFakeOpenClawGateway {
@ -2795,6 +2844,18 @@ func newAcpFakeOpenClawGateway(t *testing.T) *acpFakeOpenClawGateway {
fake.artifactPrepareCount.Add(1)
params := shared.AsMap(frame["params"])
fake.lastArtifactPrepareParams.Store(params)
if fake.unsupportedSessionPrepare.Load() {
_ = conn.WriteJSON(map[string]any{
"type": "res",
"id": id,
"ok": false,
"error": map[string]any{
"code": "INVALID_REQUEST",
"message": "unknown method: xworkmate.session.prepare",
},
})
continue
}
runID := strings.TrimSpace(shared.StringArg(params, "runId", "fake-run"))
sessionKey := strings.TrimSpace(shared.StringArg(params, "openclawSessionKey", "main"))
artifactScope := "tasks/" + sessionKey + "/" + runID