Skip to content

Commit cb0a5b5

Browse files
committed
fix(durable): gate startup recovery and publish protocol schema
1 parent 3e39e28 commit cb0a5b5

11 files changed

Lines changed: 510 additions & 29 deletions

File tree

‎docs/specs/durable-runtime-foundation-pr-proof-2026-07-01.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ is intentionally a local-first, opt-in runtime substrate:
3131

3232
Focused validation observed on the PR2 branch:
3333

34+
- Latest reviewer P1/P2 follow-up validation, rerun on 2026-07-02:
35+
- Addressed reviewer P1: gateway startup still records the durable startup
36+
marker when `OPENCLAW_DURABLE_RUNTIME=1`, but agent-turn/chat-send/subagent
37+
mark-lost reconciliation now runs only when `OPENCLAW_DURABLE_WORKER=1`.
38+
- Regression proof: `src/durable/startup.test.ts` seeds an open durable agent
39+
turn, runs gateway startup with runtime enabled and no worker flag, and
40+
verifies the run/step remain `running`; the paired worker-enabled case
41+
verifies the same run/step are marked `lost`.
42+
- Addressed reviewer P2: `durable.coordination.get` now has public
43+
`packages/gateway-protocol` params/result/projection schemas, inferred
44+
exported TypeScript types, lazy validators, and validator tests. The
45+
Gateway handler now uses `validateDurableCoordinationGetParams` instead of
46+
local ad hoc param parsing and rejects invalid params before opening durable
47+
state.
48+
- `node scripts/run-vitest.mjs run packages/gateway-protocol/src/index.test.ts`
49+
- 1 file, 47 tests passed.
50+
- `node scripts/run-vitest.mjs run --config test/vitest/vitest.gateway.config.ts src/gateway/server-methods/durable.test.ts`
51+
- 2 files, 6 tests passed.
52+
- `node scripts/run-vitest.mjs run --config test/vitest/vitest.unit.config.ts src/durable/startup.test.ts`
53+
- 1 file, 2 tests passed.
54+
- `node scripts/run-vitest.mjs run --config test/vitest/vitest.unit.config.ts src/durable/recovery.test.ts src/durable/sqlite-store.test.ts src/durable/worker.test.ts src/durable/coordination-projection.test.ts`
55+
- 4 files, 24 tests passed.
56+
- `npm run tsgo:core`
57+
- passed.
58+
- `git diff --check`
59+
- passed.
3460
- Reviewer follow-up validation, rerun on 2026-07-02 after rebasing onto
3561
upstream `origin/main` commit `133ce01e4a`:
3662
- Code fix commit: `c6cf9d12c0` (`fix(durable): gate recovery and schema

‎packages/gateway-protocol/src/index.test.ts‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
validateChatEvent,
1212
validateCommandsListParams,
1313
validateConnectParams,
14+
validateDurableCoordinationGetParams,
15+
validateDurableCoordinationGetResult,
1416
validateModelsListParams,
1517
validateNodeEventResult,
1618
validateNodePairRequestParams,
@@ -787,6 +789,92 @@ describe("validateTasksListParams", () => {
787789
});
788790
});
789791

792+
describe("validateDurableCoordinationGet", () => {
793+
it("accepts coordination inspection params and rejects unknown fields", () => {
794+
expect(validateDurableCoordinationGetParams({ runtimeRunId: "rt_123" })).toBe(true);
795+
expect(validateDurableCoordinationGetParams({ runtimeRunId: "" })).toBe(false);
796+
expect(
797+
validateDurableCoordinationGetParams({ runtimeRunId: "rt_123", includeSteps: true }),
798+
).toBe(false);
799+
});
800+
801+
it("accepts the public durable coordination projection result shape", () => {
802+
expect(
803+
validateDurableCoordinationGetResult({
804+
projection: {
805+
runtimeRunId: "rt_123",
806+
operationKind: "openclaw.agent.turn",
807+
operationVersion: "1",
808+
status: "waiting_child",
809+
recoveryState: "waiting_child",
810+
currentStepId: "subagents",
811+
waitingReason: "child",
812+
updatedAt: 200,
813+
refs: {
814+
outputRefs: [],
815+
errorRefs: [],
816+
artifactRefs: [],
817+
},
818+
external: {
819+
sessionKey: "agent:main:main",
820+
taskId: "task_123",
821+
},
822+
children: {
823+
total: 1,
824+
pending: 0,
825+
running: 0,
826+
succeeded: 1,
827+
failed: 0,
828+
cancelled: 0,
829+
lost: 0,
830+
terminal: 1,
831+
open: 0,
832+
},
833+
controls: {
834+
canCancel: false,
835+
canRetry: true,
836+
canResume: false,
837+
canSignal: false,
838+
canOpenTimeline: true,
839+
},
840+
},
841+
}),
842+
).toBe(true);
843+
expect(
844+
validateDurableCoordinationGetResult({
845+
projection: {
846+
runtimeRunId: "rt_123",
847+
operationKind: "openclaw.agent.turn",
848+
operationVersion: "1",
849+
status: "paused",
850+
recoveryState: "waiting_child",
851+
updatedAt: 200,
852+
refs: { outputRefs: [], errorRefs: [], artifactRefs: [] },
853+
external: {},
854+
children: {
855+
total: 0,
856+
pending: 0,
857+
running: 0,
858+
succeeded: 0,
859+
failed: 0,
860+
cancelled: 0,
861+
lost: 0,
862+
terminal: 0,
863+
open: 0,
864+
},
865+
controls: {
866+
canCancel: false,
867+
canRetry: false,
868+
canResume: false,
869+
canSignal: false,
870+
canOpenTimeline: true,
871+
},
872+
},
873+
}),
874+
).toBe(false);
875+
});
876+
});
877+
790878
describe("validateNodePresenceAlivePayload", () => {
791879
it("accepts a closed trigger and known metadata fields", () => {
792880
expect(

‎packages/gateway-protocol/src/index.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ import {
194194
DeviceTokenRevokeParamsSchema,
195195
type DeviceTokenRotateParams,
196196
DeviceTokenRotateParamsSchema,
197+
type DurableCoordinationGetParams,
198+
DurableCoordinationGetParamsSchema,
199+
type DurableCoordinationGetResult,
200+
DurableCoordinationGetResultSchema,
201+
type DurableCoordinationProjection,
202+
DurableCoordinationProjectionSchema,
197203
type ExecApprovalsGetParams,
198204
ExecApprovalsGetParamsSchema,
199205
type ExecApprovalsNodeGetParams,
@@ -705,6 +711,12 @@ export const validateSessionsUsageParams =
705711
export const validateTasksListParams = lazyCompile<TasksListParams>(TasksListParamsSchema);
706712
export const validateTasksGetParams = lazyCompile<TasksGetParams>(TasksGetParamsSchema);
707713
export const validateTasksCancelParams = lazyCompile<TasksCancelParams>(TasksCancelParamsSchema);
714+
export const validateDurableCoordinationGetParams = lazyCompile<DurableCoordinationGetParams>(
715+
DurableCoordinationGetParamsSchema,
716+
);
717+
export const validateDurableCoordinationGetResult = lazyCompile<DurableCoordinationGetResult>(
718+
DurableCoordinationGetResultSchema,
719+
);
708720
export const validateConfigGetParams = lazyCompile<ConfigGetParams>(ConfigGetParamsSchema);
709721
export const validateConfigSetParams = lazyCompile<ConfigSetParams>(ConfigSetParamsSchema);
710722
export const validateConfigApplyParams = lazyCompile<ConfigApplyParams>(ConfigApplyParamsSchema);
@@ -1075,6 +1087,9 @@ export {
10751087
TasksGetResultSchema,
10761088
TasksCancelParamsSchema,
10771089
TasksCancelResultSchema,
1090+
DurableCoordinationGetParamsSchema,
1091+
DurableCoordinationProjectionSchema,
1092+
DurableCoordinationGetResultSchema,
10781093
ConfigGetParamsSchema,
10791094
ConfigSetParamsSchema,
10801095
ConfigApplyParamsSchema,
@@ -1392,6 +1407,9 @@ export type {
13921407
TasksGetResult,
13931408
TasksCancelParams,
13941409
TasksCancelResult,
1410+
DurableCoordinationGetParams,
1411+
DurableCoordinationProjection,
1412+
DurableCoordinationGetResult,
13951413
CronJob,
13961414
CronListParams,
13971415
CronStatusParams,

‎packages/gateway-protocol/src/schema.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export * from "./schema/error-codes.js";
1616
export * from "./schema/environments.js";
1717
export * from "./schema/exec-approvals.js";
1818
export * from "./schema/devices.js";
19+
export * from "./schema/durable.js";
1920
export * from "./schema/frames.js";
2021
export * from "./schema/logs-chat.js";
2122
export * from "./schema/nodes.js";
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// Gateway Protocol schema module defines durable runtime control-plane shapes.
2+
import { Type } from "typebox";
3+
import { NonEmptyString } from "./primitives.js";
4+
5+
const TimestampMsSchema = Type.Integer({ minimum: 0 });
6+
7+
export const DurableRuntimeRunStatusSchema = Type.Union([
8+
Type.Literal("accepted"),
9+
Type.Literal("received"),
10+
Type.Literal("queued"),
11+
Type.Literal("running"),
12+
Type.Literal("waiting"),
13+
Type.Literal("waiting_signal"),
14+
Type.Literal("waiting_timer"),
15+
Type.Literal("waiting_child"),
16+
Type.Literal("blocked"),
17+
Type.Literal("retrying"),
18+
Type.Literal("retry_scheduled"),
19+
Type.Literal("canceling"),
20+
Type.Literal("unknown_after_side_effect"),
21+
Type.Literal("succeeded"),
22+
Type.Literal("failed"),
23+
Type.Literal("cancelled"),
24+
Type.Literal("lost"),
25+
]);
26+
27+
export const DurableRecoveryStateSchema = Type.Union([
28+
Type.Literal("runnable"),
29+
Type.Literal("claimed"),
30+
Type.Literal("running"),
31+
Type.Literal("waiting_signal"),
32+
Type.Literal("waiting_timer"),
33+
Type.Literal("waiting_child"),
34+
Type.Literal("retry_scheduled"),
35+
Type.Literal("reconciling"),
36+
Type.Literal("unknown_after_side_effect"),
37+
Type.Literal("lost"),
38+
Type.Literal("terminal"),
39+
]);
40+
41+
export const DurableCoordinationWaitingReasonSchema = Type.Union([
42+
Type.Literal("signal"),
43+
Type.Literal("timer"),
44+
Type.Literal("child"),
45+
Type.Literal("retry"),
46+
Type.Literal("worker"),
47+
Type.Literal("unknown"),
48+
]);
49+
50+
export const DurableCoordinationGetParamsSchema = Type.Object(
51+
{
52+
runtimeRunId: NonEmptyString,
53+
},
54+
{ additionalProperties: false },
55+
);
56+
57+
export const DurableCoordinationExternalRefsSchema = Type.Object(
58+
{
59+
workUnitId: Type.Optional(Type.String()),
60+
reportRouteId: Type.Optional(Type.String()),
61+
taskId: Type.Optional(Type.String()),
62+
taskFlowId: Type.Optional(Type.String()),
63+
sessionKey: Type.Optional(Type.String()),
64+
childSessionKey: Type.Optional(Type.String()),
65+
runId: Type.Optional(Type.String()),
66+
agentId: Type.Optional(Type.String()),
67+
requesterAgentId: Type.Optional(Type.String()),
68+
},
69+
{ additionalProperties: false },
70+
);
71+
72+
export const DurableCoordinationChildCountsSchema = Type.Object(
73+
{
74+
total: Type.Integer({ minimum: 0 }),
75+
pending: Type.Integer({ minimum: 0 }),
76+
running: Type.Integer({ minimum: 0 }),
77+
succeeded: Type.Integer({ minimum: 0 }),
78+
failed: Type.Integer({ minimum: 0 }),
79+
cancelled: Type.Integer({ minimum: 0 }),
80+
lost: Type.Integer({ minimum: 0 }),
81+
terminal: Type.Integer({ minimum: 0 }),
82+
open: Type.Integer({ minimum: 0 }),
83+
},
84+
{ additionalProperties: false },
85+
);
86+
87+
export const DurableCoordinationRefsSchema = Type.Object(
88+
{
89+
inputRef: Type.Optional(Type.String()),
90+
checkpointRef: Type.Optional(Type.String()),
91+
outputRefs: Type.Array(Type.String()),
92+
errorRefs: Type.Array(Type.String()),
93+
artifactRefs: Type.Array(Type.String()),
94+
},
95+
{ additionalProperties: false },
96+
);
97+
98+
export const DurableCoordinationControlsSchema = Type.Object(
99+
{
100+
canCancel: Type.Boolean(),
101+
canRetry: Type.Boolean(),
102+
canResume: Type.Boolean(),
103+
canSignal: Type.Boolean(),
104+
canOpenTimeline: Type.Boolean(),
105+
},
106+
{ additionalProperties: false },
107+
);
108+
109+
export const DurableCoordinationRecoveryDiagnosticSchema = Type.Object(
110+
{
111+
state: Type.Union([Type.Literal("lost"), Type.Literal("unknown_after_side_effect")]),
112+
severity: Type.Union([Type.Literal("warning"), Type.Literal("error")]),
113+
reportable: Type.Boolean(),
114+
retryable: Type.Boolean(),
115+
reason: Type.Optional(Type.String()),
116+
message: Type.String(),
117+
nextAction: Type.String(),
118+
safeRecoveryActions: Type.Optional(Type.Array(Type.String())),
119+
input: Type.Optional(
120+
Type.Object(
121+
{
122+
inputRef: Type.Optional(Type.String()),
123+
inputAvailability: Type.Optional(Type.String()),
124+
canReplay: Type.Optional(Type.Boolean()),
125+
reason: Type.Optional(Type.String()),
126+
messageLength: Type.Optional(Type.Integer({ minimum: 0 })),
127+
messageHash: Type.Optional(Type.String()),
128+
},
129+
{ additionalProperties: false },
130+
),
131+
),
132+
detectedAt: Type.Optional(TimestampMsSchema),
133+
processInstanceId: Type.Optional(Type.String()),
134+
},
135+
{ additionalProperties: false },
136+
);
137+
138+
export const DurableCoordinationProjectionSchema = Type.Object(
139+
{
140+
runtimeRunId: NonEmptyString,
141+
operationKind: NonEmptyString,
142+
operationVersion: NonEmptyString,
143+
status: DurableRuntimeRunStatusSchema,
144+
recoveryState: DurableRecoveryStateSchema,
145+
sourceType: Type.Optional(Type.String()),
146+
sourceRef: Type.Optional(Type.String()),
147+
parentRuntimeRunId: Type.Optional(Type.String()),
148+
parentStepId: Type.Optional(Type.String()),
149+
workUnitId: Type.Optional(Type.String()),
150+
reportRouteId: Type.Optional(Type.String()),
151+
currentStepId: Type.Optional(Type.String()),
152+
waitingReason: Type.Optional(DurableCoordinationWaitingReasonSchema),
153+
heartbeatAt: Type.Optional(TimestampMsSchema),
154+
updatedAt: TimestampMsSchema,
155+
completedAt: Type.Optional(TimestampMsSchema),
156+
refs: DurableCoordinationRefsSchema,
157+
external: DurableCoordinationExternalRefsSchema,
158+
children: DurableCoordinationChildCountsSchema,
159+
controls: DurableCoordinationControlsSchema,
160+
recovery: Type.Optional(DurableCoordinationRecoveryDiagnosticSchema),
161+
},
162+
{ additionalProperties: false },
163+
);
164+
165+
export const DurableCoordinationGetResultSchema = Type.Object(
166+
{
167+
projection: DurableCoordinationProjectionSchema,
168+
},
169+
{ additionalProperties: false },
170+
);

‎packages/gateway-protocol/src/schema/protocol-schemas.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ import {
163163
DeviceTokenRevokeParamsSchema,
164164
DeviceTokenRotateParamsSchema,
165165
} from "./devices.js";
166+
import {
167+
DurableCoordinationGetParamsSchema,
168+
DurableCoordinationGetResultSchema,
169+
DurableCoordinationProjectionSchema,
170+
} from "./durable.js";
166171
import {
167172
EnvironmentSummarySchema,
168173
EnvironmentsListParamsSchema,
@@ -418,6 +423,9 @@ export const ProtocolSchemas = {
418423
TasksGetResult: TasksGetResultSchema,
419424
TasksCancelParams: TasksCancelParamsSchema,
420425
TasksCancelResult: TasksCancelResultSchema,
426+
DurableCoordinationGetParams: DurableCoordinationGetParamsSchema,
427+
DurableCoordinationProjection: DurableCoordinationProjectionSchema,
428+
DurableCoordinationGetResult: DurableCoordinationGetResultSchema,
421429
ConfigGetParams: ConfigGetParamsSchema,
422430
ConfigSetParams: ConfigSetParamsSchema,
423431
ConfigApplyParams: ConfigApplyParamsSchema,

‎packages/gateway-protocol/src/schema/types.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ export type TasksGetParams = SchemaType<"TasksGetParams">;
104104
export type TasksGetResult = SchemaType<"TasksGetResult">;
105105
export type TasksCancelParams = SchemaType<"TasksCancelParams">;
106106
export type TasksCancelResult = SchemaType<"TasksCancelResult">;
107+
export type DurableCoordinationGetParams = SchemaType<"DurableCoordinationGetParams">;
108+
export type DurableCoordinationProjection = SchemaType<"DurableCoordinationProjection">;
109+
export type DurableCoordinationGetResult = SchemaType<"DurableCoordinationGetResult">;
107110

108111
/** Config read/write/schema payloads plus update status and run controls. */
109112
export type ConfigGetParams = SchemaType<"ConfigGetParams">;

0 commit comments

Comments
 (0)