Skip to content

Commit 73bba03

Browse files
authored
refactor: canonicalize session delivery state (#113225)
* refactor: canonicalize session delivery state * test: canonicalize reply persistence fixtures * test: canonicalize talk delivery fixtures * test: canonicalize voice session routes * test: canonicalize attachment delivery fixtures * test: migrate gateway delivery fixtures * fix: skip invalid session delivery rows * test: align delivery SDK surface gates * fix: preserve legacy delivery precedence * test: canonicalize heartbeat delivery fixtures * fix: preserve delivery route prompt identity * test: canonicalize session delivery fixtures * fix: preserve recoverable legacy delivery routes * fix: canonicalize remaining session state * fix: preserve canonical session classification * style: format delivery state changes * test: refresh plugin SDK delivery baseline * test: avoid mutating session fixture input * style: simplify delivery identity check * style: simplify delivery origin spread * fix: preserve fresh delivery route metadata * test: assert canonical surface route switch * fix: canonicalize doctor file-store imports * fix: preserve transitional delivery migration state * fix: satisfy canonical delivery CI gates * ci: scope GitHub App token permissions * test: infer canonical delivery projections * test: canonicalize ACP requester delivery fixtures * test: canonicalize harness rollback fixture * style: apply pinned formatter
1 parent d29b113 commit 73bba03

185 files changed

Lines changed: 2944 additions & 1905 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/auto-response.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
3838
permission-issues: write
3939
permission-members: read
40+
permission-metadata: read
4041
permission-pull-requests: write
4142
- uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3
4243
id: app-token-fallback
@@ -46,6 +47,7 @@ jobs:
4647
private-key: ${{ secrets.GH_APP_PRIVATE_KEY_FALLBACK }}
4748
permission-issues: write
4849
permission-members: read
50+
permission-metadata: read
4951
permission-pull-requests: write
5052
- name: Run Barnacle auto-response
5153
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9

‎.github/workflows/labeler.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
permission-contents: read
5151
permission-issues: write
5252
permission-members: read
53+
permission-metadata: read
5354
permission-pull-requests: write
5455
- uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3
5556
id: app-token-fallback
@@ -60,6 +61,7 @@ jobs:
6061
permission-contents: read
6162
permission-issues: write
6263
permission-members: read
64+
permission-metadata: read
6365
permission-pull-requests: write
6466
- uses: actions/labeler@b8dd2d9be0f68b860e7dae5dae7d772984eacd6d # v6
6567
if: ${{ github.event.action != 'edited' || github.event.changes.base }}
@@ -496,6 +498,7 @@ jobs:
496498
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
497499
permission-issues: write
498500
permission-members: read
501+
permission-metadata: read
499502
permission-pull-requests: write
500503
- uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3
501504
id: app-token-fallback
@@ -505,6 +508,7 @@ jobs:
505508
private-key: ${{ secrets.GH_APP_PRIVATE_KEY_FALLBACK }}
506509
permission-issues: write
507510
permission-members: read
511+
permission-metadata: read
508512
permission-pull-requests: write
509513
- name: Backfill PR labels
510514
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9

‎.github/workflows/pr-ci-sweeper.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
4343
permission-actions: write
4444
permission-checks: read
45+
permission-issues: write
4546
permission-pull-requests: write
4647
- uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3
4748
id: app-token-fallback
@@ -51,6 +52,7 @@ jobs:
5152
private-key: ${{ secrets.GH_APP_PRIVATE_KEY_FALLBACK }}
5253
permission-actions: write
5354
permission-checks: read
55+
permission-issues: write
5456
permission-pull-requests: write
5557
- name: Sweep dropped PR CI runs
5658
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9

‎.github/workflows/stale.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ jobs:
259259
app-id: "2971289"
260260
private-key: ${{ secrets.GH_APP_PRIVATE_KEY_FALLBACK }}
261261
permission-issues: write
262+
permission-metadata: read
262263
permission-pull-requests: write
263264
- name: Backfill stale closures
264265
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
@@ -509,6 +510,7 @@ jobs:
509510
app-id: "2729701"
510511
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
511512
permission-issues: write
513+
permission-metadata: read
512514
- name: Lock closed issues after 48h of no activity
513515
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
514516
with:

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ b6b8edc50ecab8386c9acd8f374a207212b5a99c8f518538bbcf0c458dda3881 module/runtime
116116
17a6a199714ba8308e62928c0491bcf9fd214c923c3879aa99a047a30138253d module/secret-ref-runtime
117117
596a315d426121c9620b314e3a9a7f523840b46e007d94d0d5e83cdedf789d15 module/security-runtime
118118
31b785e74f1f8f56241b7756ef6a5d86199c5ce177cbb1c234a261866972f270 module/session-discussion
119-
9d7d884330397701c7de9f5b6800b970b2b349027491704184cb1bd6cda1fd00 module/session-store-runtime
119+
82f1f06f875097ee894348e74346fde79325c8fee7106b51438aaec1f539cbff module/session-store-runtime
120120
7cad408673562b0ff33f60071698bea2c39d02c64f4f235851c5c2ceb946910d module/setup
121121
fbcb853789db5a1ad5d81e8094ed7f5bcea657ac2281e7887a19c1d6f1194571 module/setup-runtime
122122
cd431f6ba8327b81438b7a63b1963120f200f5abd145fb6aa7c5c561339cb0b1 module/setup-tools

‎extensions/active-memory/session.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry";
2+
import {
3+
deliveryContextFromSession,
4+
sessionDeliveryOrigin,
5+
} from "openclaw/plugin-sdk/session-store-runtime";
26
import { normalizeOptionalString, uniqueStrings } from "openclaw/plugin-sdk/string-coerce-runtime";
37
import {
48
ACTIVE_MEMORY_DEBUG_PREFIX,
@@ -119,9 +123,9 @@ function resolveRecallRunChannelContext(params: {
119123
agentId: params.agentId,
120124
sessionKey: resolvedSessionKey,
121125
});
122-
const rawStrongEntryChannel =
123-
normalizeOptionalString(sessionEntry?.lastChannel) ??
124-
normalizeOptionalString(sessionEntry?.channel);
126+
const rawStrongEntryChannel = normalizeOptionalString(
127+
deliveryContextFromSession(sessionEntry)?.channel,
128+
);
125129
// Channel IDs containing ":" or "/" are scoped conversation IDs, not
126130
// runnable channel names. The same guard that
127131
// applies to explicit channelId (#76704) must also apply to channels
@@ -130,7 +134,7 @@ function resolveRecallRunChannelContext(params: {
130134
rawStrongEntryChannel && isRunnableChannelName(rawStrongEntryChannel)
131135
? rawStrongEntryChannel
132136
: undefined;
133-
const weakEntryChannel = normalizeOptionalString(sessionEntry?.origin?.provider);
137+
const weakEntryChannel = normalizeOptionalString(sessionDeliveryOrigin(sessionEntry)?.provider);
134138
return resolveReturnValue({
135139
resolvedChannel: strongEntryChannel ?? weakEntryChannel,
136140
resolvedChannelStrength: strongEntryChannel

‎extensions/feishu/src/doctor.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DatabaseSync } from "node:sqlite";
66
import {
77
formatSqliteSessionFileMarker,
88
listSessionEntries,
9+
normalizeSessionDeliveryState,
910
type SessionEntry,
1011
upsertSessionEntry,
1112
} from "openclaw/plugin-sdk/session-store-runtime";
@@ -430,7 +431,9 @@ describe("Feishu doctor state repair", () => {
430431
sessionId: "sess-acp-bad",
431432
sessionFile: "sess-acp-bad.jsonl",
432433
updatedAt: Date.now(),
433-
route: { channel: "feishu", target: { to: "ou_user", chatType: "direct" } },
434+
delivery: normalizeSessionDeliveryState({
435+
route: { channel: "feishu", target: { to: "ou_user", chatType: "direct" } },
436+
}),
434437
},
435438
"agent:main:discord:direct:user": {
436439
sessionId: "sess-discord",
@@ -492,7 +495,9 @@ describe("Feishu doctor state repair", () => {
492495
sessionId: "sess-codex-locked",
493496
agentHarnessId: "codex",
494497
modelSelectionLocked: true,
495-
route: { channel: "feishu", target: { to: "ou_user", chatType: "direct" } },
498+
delivery: normalizeSessionDeliveryState({
499+
route: { channel: "feishu", target: { to: "ou_user", chatType: "direct" } },
500+
}),
496501
updatedAt: 1,
497502
},
498503
});

‎extensions/matrix/src/exec-approvals.test.ts‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import os from "node:os";
44
import path from "node:path";
55
import type { ExecApprovalRequest } from "openclaw/plugin-sdk/approval-runtime";
66
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
7-
import { upsertSessionEntry } from "openclaw/plugin-sdk/session-store-runtime";
7+
import {
8+
normalizeSessionDeliveryState,
9+
upsertSessionEntry,
10+
} from "openclaw/plugin-sdk/session-store-runtime";
811
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
912
import { afterEach, describe, expect, it } from "vitest";
1013
import { normalizeMatrixApproverId } from "./approval-ids.js";
@@ -364,20 +367,19 @@ describe("matrix exec approvals", () => {
364367
entry: {
365368
sessionId: "main",
366369
updatedAt: 1,
367-
origin: {
368-
provider: "matrix",
369-
accountId: "ops",
370-
to: "room:!room:example.org",
371-
nativeChannelId: "!room:example.org",
372-
},
373-
deliveryContext: {
374-
channel: "matrix",
375-
to: "room:!room:example.org",
376-
accountId: "ops",
377-
},
378-
lastChannel: "slack",
379-
lastTo: "channel:C999",
380-
lastAccountId: "work",
370+
delivery: normalizeSessionDeliveryState({
371+
origin: {
372+
provider: "matrix",
373+
accountId: "ops",
374+
to: "room:!room:example.org",
375+
nativeChannelId: "!room:example.org",
376+
},
377+
context: {
378+
channel: "matrix",
379+
to: "room:!room:example.org",
380+
accountId: "ops",
381+
},
382+
}),
381383
},
382384
});
383385
const cfg = buildMultiAccountMatrixConfig({ sessionStorePath: storePath });

‎extensions/matrix/src/matrix/monitor/handler.test.ts‎

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import {
77
testing as sessionBindingTesting,
88
registerSessionBindingAdapter,
99
} from "openclaw/plugin-sdk/session-binding-runtime";
10-
import { getSessionEntry, upsertSessionEntry } from "openclaw/plugin-sdk/session-store-runtime";
10+
import {
11+
deliveryContextFromSession,
12+
getSessionEntry,
13+
normalizeSessionDeliveryState,
14+
sessionDeliveryOrigin,
15+
upsertSessionEntry,
16+
} from "openclaw/plugin-sdk/session-store-runtime";
1117
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1218
import { installMatrixMonitorTestRuntime } from "../../test-runtime.js";
1319
import { MATRIX_OPENCLAW_FINALIZED_PREVIEW_KEY } from "../send/types.js";
@@ -87,22 +93,22 @@ async function writeMatrixSessionMeta(
8793
sessionId: `sess-${sessionKey}`,
8894
updatedAt: Date.now(),
8995
};
90-
const existingOrigin =
91-
typeof existing.origin === "object" && existing.origin !== null
92-
? (existing.origin as Record<string, unknown>)
93-
: {};
96+
const existingOrigin = sessionDeliveryOrigin(existing) ?? {};
9497
await upsertSessionEntry({
9598
storePath,
9699
sessionKey,
97100
entry: {
98101
...existing,
99-
origin: {
100-
...existingOrigin,
101-
provider: "matrix",
102-
surface: "matrix",
103-
accountId: "ops",
104-
...origin,
105-
},
102+
delivery: normalizeSessionDeliveryState({
103+
context: deliveryContextFromSession(existing),
104+
origin: {
105+
...existingOrigin,
106+
provider: "matrix",
107+
surface: "matrix",
108+
accountId: "ops",
109+
...origin,
110+
},
111+
}),
106112
},
107113
});
108114
}
@@ -1506,11 +1512,13 @@ describe("matrix monitor handler pairing account scope", () => {
15061512
entry: {
15071513
sessionId: "sess-main",
15081514
updatedAt: Date.now(),
1509-
deliveryContext: {
1510-
channel: "matrix",
1511-
to: "room:!other:example.org",
1512-
accountId: "ops",
1513-
},
1515+
delivery: normalizeSessionDeliveryState({
1516+
context: {
1517+
channel: "matrix",
1518+
to: "room:!other:example.org",
1519+
accountId: "ops",
1520+
},
1521+
}),
15141522
},
15151523
});
15161524
const sendNotice = vi.fn(async () => "$notice");
@@ -1551,11 +1559,13 @@ describe("matrix monitor handler pairing account scope", () => {
15511559
entry: {
15521560
sessionId: "sess-bound",
15531561
updatedAt: Date.now(),
1554-
deliveryContext: {
1555-
channel: "matrix",
1556-
to: "room:!other:example.org",
1557-
accountId: "ops",
1558-
},
1562+
delivery: normalizeSessionDeliveryState({
1563+
context: {
1564+
channel: "matrix",
1565+
to: "room:!other:example.org",
1566+
accountId: "ops",
1567+
},
1568+
}),
15591569
},
15601570
});
15611571
const sendNotice = vi.fn(async () => "$notice");

‎extensions/matrix/src/matrix/session-store-metadata.ts‎

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Matrix plugin module implements session store metadata behavior.
22
import { normalizeAccountId } from "openclaw/plugin-sdk/account-id";
3+
import {
4+
deliveryContextFromSession,
5+
sessionDeliveryOrigin,
6+
type SessionEntry,
7+
} from "openclaw/plugin-sdk/session-store-runtime";
38
import { resolveMatrixDirectUserId, resolveMatrixTargetIdentity } from "./target-ids.js";
49

510
function trimMaybeString(value: unknown): string | undefined {
@@ -26,38 +31,17 @@ function resolveMatrixSessionAccountId(value: unknown): string | undefined {
2631

2732
function resolveMatrixStoredRoomId(params: {
2833
deliveryTo?: unknown;
29-
lastTo?: unknown;
3034
originNativeChannelId?: unknown;
3135
originTo?: unknown;
3236
}): string | undefined {
3337
return (
3438
resolveMatrixRoomTargetId(params.deliveryTo) ??
35-
resolveMatrixRoomTargetId(params.lastTo) ??
3639
resolveMatrixRoomTargetId(params.originNativeChannelId) ??
3740
resolveMatrixRoomTargetId(params.originTo)
3841
);
3942
}
4043

41-
type MatrixStoredSessionEntryLike = {
42-
deliveryContext?: {
43-
channel?: unknown;
44-
to?: unknown;
45-
accountId?: unknown;
46-
};
47-
origin?: {
48-
provider?: unknown;
49-
from?: unknown;
50-
to?: unknown;
51-
nativeChannelId?: unknown;
52-
nativeDirectUserId?: unknown;
53-
accountId?: unknown;
54-
chatType?: unknown;
55-
};
56-
lastChannel?: unknown;
57-
lastTo?: unknown;
58-
lastAccountId?: unknown;
59-
chatType?: unknown;
60-
};
44+
type MatrixStoredSessionEntryLike = Pick<SessionEntry, "chatType" | "delivery">;
6145

6246
export function resolveMatrixStoredSessionMeta(entry?: MatrixStoredSessionEntryLike): {
6347
channel?: string;
@@ -68,32 +52,27 @@ export function resolveMatrixStoredSessionMeta(entry?: MatrixStoredSessionEntryL
6852
if (!entry) {
6953
return null;
7054
}
71-
const channel =
72-
trimMaybeString(entry.deliveryContext?.channel) ??
73-
trimMaybeString(entry.lastChannel) ??
74-
trimMaybeString(entry.origin?.provider);
55+
const deliveryContext = deliveryContextFromSession(entry);
56+
const origin = sessionDeliveryOrigin(entry);
57+
const channel = trimMaybeString(deliveryContext?.channel) ?? trimMaybeString(origin?.provider);
7558
const accountId =
76-
resolveMatrixSessionAccountId(
77-
entry.deliveryContext?.accountId ?? entry.lastAccountId ?? entry.origin?.accountId,
78-
) ?? undefined;
59+
resolveMatrixSessionAccountId(deliveryContext?.accountId ?? origin?.accountId) ?? undefined;
7960
const roomId = resolveMatrixStoredRoomId({
80-
deliveryTo: entry.deliveryContext?.to,
81-
lastTo: entry.lastTo,
82-
originNativeChannelId: entry.origin?.nativeChannelId,
83-
originTo: entry.origin?.to,
61+
deliveryTo: deliveryContext?.to,
62+
originNativeChannelId: origin?.nativeChannelId,
63+
originTo: origin?.to,
8464
});
8565
const chatType =
86-
trimMaybeString(entry.origin?.chatType) ?? trimMaybeString(entry.chatType) ?? undefined;
66+
trimMaybeString(origin?.chatType) ?? trimMaybeString(entry.chatType) ?? undefined;
8767
const directUserId =
8868
chatType === "direct"
89-
? (trimMaybeString(entry.origin?.nativeDirectUserId) ??
69+
? (trimMaybeString(origin?.nativeDirectUserId) ??
9070
resolveMatrixDirectUserId({
91-
from: trimMaybeString(entry.origin?.from),
71+
from: trimMaybeString(origin?.from),
9272
to:
9373
(roomId ? `room:${roomId}` : undefined) ??
94-
trimMaybeString(entry.deliveryContext?.to) ??
95-
trimMaybeString(entry.lastTo) ??
96-
trimMaybeString(entry.origin?.to),
74+
trimMaybeString(deliveryContext?.to) ??
75+
trimMaybeString(origin?.to),
9776
chatType,
9877
}))
9978
: undefined;

0 commit comments

Comments
 (0)