Skip to content

Commit 5cd2c5b

Browse files
ragesaqChisel
authored andcommitted
fix: keep ClickClack discussion rooms durable
Separate durable ClickClack room identity from replaceable exact-session attachments. Session archive, reset, deletion, and recreation retain the existing room and history while rotating local routing authority and preserving fail-closed exact-incarnation grants. Pin ambiguous room recovery to the originating account and credential, and retain prior attachments when persistence fails. Closes #119300 Co-authored-by: Chisel <[email protected]>
1 parent e745c29 commit 5cd2c5b

17 files changed

Lines changed: 1044 additions & 339 deletions

‎docs/channels/clickclack.md‎

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -231,25 +231,26 @@ no account selector, so multiple enabled discussion accounts are rejected
231231
rather than choosing one by configuration order.
232232

233233
Opening a discussion creates a public ClickClack channel marked as externally
234-
managed. The plugin keeps the session label, category, and archive state in
235-
sync. Restoring a session restores its channel; clearing the session category
236-
moves the channel back to the configured default section. Deleting an
237-
OpenClaw session archives the ClickClack channel instead of deleting it, so its
238-
history remains available. The plugin reconciles bindings when discussion RPCs
239-
are used and approximately once per minute while any bindings exist.
234+
managed. The plugin keeps the session label and category in sync, but channel
235+
lifecycle remains independent. Clearing the session category
236+
moves the channel back to the configured default section. Archiving, resetting,
237+
or deleting an OpenClaw session never archives or replaces the ClickClack
238+
channel. ClickClack owns channel archive and restore independently. The plugin
239+
reconciles bindings when discussion RPCs are used and approximately once per
240+
minute while any bindings exist.
240241

241242
Inbound messages in a managed channel use a deterministic side session under
242243
the same agent id as the attached main session. The side agent is told which
243244
main session to observe and can use `sessions_history` and `session_status`
244245
(`changesSince` is useful for incremental checks). It uses `sessions_send` only
245246
when people in the discussion ask it to relay or steer the main session.
246-
The binding, managed ownership reference, and side-session peer identity include
247-
the concrete OpenClaw session id along with the pinned ClickClack server and
248-
channel. Resetting a reusable session key or retargeting an account revokes the
249-
old channel locally, archives it when the old credential remains usable, and
250-
cannot reuse its side transcript. Messages arriving through an
251-
archived, reset, disabled, or retargeted binding are dropped instead of falling
252-
back to the account's normal channel routing. Released bindings leave a durable
247+
The binding separates durable room identity from its replaceable session
248+
attachment. The side-session peer identity and scoped grant include the exact
249+
concrete OpenClaw session id, so resetting a reusable session key rotates the
250+
attachment and cannot reuse the old side transcript. The ClickClack channel id,
251+
URL, history, and ownership reference remain unchanged. Messages arriving
252+
through an inactive, disabled, or retargeted attachment are dropped instead of
253+
falling back to the account's normal channel routing. Released bindings leave a durable
253254
revoked-channel marker so delayed realtime events remain fail-closed. Remote
254255
ownership is keyed by ClickClack server and channel id, so renaming the local
255256
account cannot turn a managed channel into an ordinary one.
@@ -270,22 +271,22 @@ before persisting a binding. If a create response is lost, the next open adopts
270271
the channel by its server-enforced `external_ref` instead of creating another.
271272
Until that outcome is reconciled, the pending reservation quarantines
272273
otherwise-unbound events in the destination workspace. The coarse reconciler
273-
adopts the channel when the same session is still live or archives it after a
274-
reset; it clears the reservation when no remote channel was created.
274+
adopts the channel when the logical session is active, including after its
275+
concrete session id changes; it clears the reservation when no remote channel
276+
was created.
275277
That reference contains a durable per-OpenClaw-installation namespace plus a
276-
hash of the session key, concrete session id, ClickClack destination, and durable
278+
hash of the session key, ClickClack destination, and durable
277279
binding generation. Separate gateways cannot adopt each other's channels,
278-
reset sessions cannot inherit old channel history, and an account or workspace
280+
while concrete session resets keep the same channel. An account or workspace
279281
round trip cannot re-adopt a previous channel. Bindings are also pinned to the
280282
configured ClickClack server URL and are invalidated if the account is
281283
retargeted. Changing or removing `controlUrlBase` updates or clears the managed
282284
channel link on the next reconciliation pass. Changing
283-
`discussions.workspace` archives and releases the old binding before a channel
284-
can be opened in the new workspace when the old workspace credential remains
285-
configured. If the token was replaced with a workspace-scoped credential that
286-
cannot access the old workspace, OpenClaw records the old channel as revoked and
287-
releases the binding without trying the replacement token; archive that leftover
288-
channel from ClickClack.
285+
`discussions.workspace` releases the old attachment before a channel can be
286+
opened in the new workspace. It never archives the old room. If the token was
287+
replaced with a workspace-scoped credential that cannot access the old
288+
workspace, OpenClaw records the old channel as revoked and releases the binding
289+
without trying the replacement token.
289290

290291
The attached main session also receives a pull-only `discussion` tool. It reads
291292
the latest messages and recent thread replies as one escaped, attributed record

‎extensions/clickclack/README.md‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ support and return the managed-channel fields used by this integration.
9999
```
100100

101101
Opening a session discussion creates a public, externally managed channel and
102-
stores its binding in the ClickClack plugin's SQLite state. Session archive,
103-
restore, label, category, and deletion changes are reflected in the channel;
104-
deletion archives the channel and never deletes its messages. `workspace`
102+
stores its binding in the ClickClack plugin's SQLite state. Session label and
103+
category changes remain reflected in the channel, but session archive, restore,
104+
reset, and deletion never archive or replace it. ClickClack owns channel archive
105+
and restore independently. `workspace`
105106
defaults to the account workspace, and `section` defaults to `Sessions`.
106107
`controlUrlBase` adds a link back to `/chat?session=<session-key>` in the
107108
OpenClaw Control UI.
@@ -123,12 +124,11 @@ at its safer default `tree`. A second host-side policy blocks session discovery
123124
and alternate targets; the side-agent prompt is not the authorization boundary.
124125
The agent still needs those three tools in its effective tool allowlist.
125126

126-
The binding, managed ownership reference, and side-session identity include the
127-
concrete OpenClaw session id as well as the pinned server and channel. Resetting
128-
a reusable session key, replacing a binding, or retargeting it therefore revokes
129-
the old channel locally, archives it when the old credential remains usable, and
130-
starts a fresh channel and side transcript.
131-
Messages arriving through an archived, reset, disabled, or retargeted managed
127+
The binding separates durable room identity from a replaceable exact-session
128+
attachment. Resetting a reusable session key keeps the ClickClack channel,
129+
history, URL, and ownership reference while rotating the side-session identity
130+
and scoped grant. The previous side session cannot access the new main session.
131+
Messages arriving through an inactive, disabled, or retargeted managed
132132
binding are dropped instead of falling back to the account's normal channel
133133
routing. Released bindings leave a durable revoked-channel marker so delayed
134134
realtime events remain fail-closed. Remote ownership is keyed by ClickClack
@@ -144,15 +144,12 @@ lifecycle reconciliation pass.
144144

145145
If a channel-create response is lost, the pending ownership reservation
146146
temporarily quarantines otherwise-unbound events in that workspace. The same
147-
coarse reconciler then adopts the created channel or clears/archives the
148-
ambiguous attempt; a reset cannot make the old channel fall through to ordinary
149-
routing.
150-
151-
When a workspace move keeps the original workspace credential configured, the
152-
plugin archives the old channel before release. If the token is replaced with a
153-
workspace-scoped credential that cannot access the old workspace, OpenClaw
154-
releases the binding into the revoked-channel marker without trying the new token
155-
against the old channel; archive that leftover channel from ClickClack.
147+
coarse reconciler then adopts the created channel or clears the ambiguous
148+
attempt; a reset cannot duplicate or archive the durable room.
149+
150+
Workspace and account moves release local attachment authority without
151+
archiving the old channel. A workspace-scoped replacement token is never tried
152+
against the old channel.
156153

157154
The main session gets a read-only `discussion` tool that pulls the latest
158155
channel messages, including recent thread replies. The pull uses bounded

‎extensions/clickclack/src/discussions/binding-generation.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { PluginRuntime } from "openclaw/plugin-sdk/core";
33
import type { PluginStateSyncKeyedStore } from "openclaw/plugin-sdk/plugin-state-runtime";
44

55
type DiscussionBindingGeneration = {
6+
accountId?: string;
7+
credentialFingerprint?: string;
68
destinationIdentity: string;
79
generation: string;
810
pending?: {
@@ -49,16 +51,34 @@ function getGenerationStore(
4951
export function reserveDiscussionBindingGeneration(params: {
5052
runtime: PluginRuntime;
5153
sessionKey: string;
54+
accountId: string;
55+
credentialFingerprint: string;
5256
destinationIdentity: string;
5357
createGeneration?: () => string;
5458
}): string {
5559
const store = getGenerationStore(params.runtime);
5660
const existing = store.lookup(params.sessionKey);
57-
if (existing?.destinationIdentity === params.destinationIdentity) {
61+
const existingAccountId = existing?.accountId ?? existing?.pending?.accountId;
62+
const existingCredentialFingerprint =
63+
existing?.credentialFingerprint ?? existing?.pending?.credentialFingerprint;
64+
if (
65+
existing?.destinationIdentity === params.destinationIdentity &&
66+
existingAccountId === params.accountId &&
67+
existingCredentialFingerprint === params.credentialFingerprint
68+
) {
69+
if (!existing.accountId || !existing.credentialFingerprint) {
70+
store.register(params.sessionKey, {
71+
...existing,
72+
accountId: params.accountId,
73+
credentialFingerprint: params.credentialFingerprint,
74+
});
75+
}
5876
return existing.generation;
5977
}
6078
const generation = (params.createGeneration ?? randomUUID)();
6179
store.register(params.sessionKey, {
80+
accountId: params.accountId,
81+
credentialFingerprint: params.credentialFingerprint,
6282
destinationIdentity: params.destinationIdentity,
6383
generation,
6484
});
@@ -94,6 +114,12 @@ export function recordPendingDiscussionOpen(params: {
94114
if (!existing || existing.generation !== params.generation) {
95115
throw new Error("ClickClack discussion generation changed before channel creation");
96116
}
117+
if (
118+
existing.accountId !== params.pending.accountId ||
119+
existing.credentialFingerprint !== params.pending.credentialFingerprint
120+
) {
121+
throw new Error("ClickClack discussion ownership changed before channel creation");
122+
}
97123
store.register(params.sessionKey, { ...existing, pending: params.pending });
98124
}
99125

@@ -107,6 +133,25 @@ export function listPendingDiscussionOpens(runtime: PluginRuntime): PendingDiscu
107133
);
108134
}
109135

136+
/** Stops destination-wide quarantine after the exact remote channel is known. */
137+
export function clearPendingDiscussionOpen(params: {
138+
runtime: PluginRuntime;
139+
sessionKey: string;
140+
expectedGeneration: string;
141+
}): void {
142+
const store = getGenerationStore(params.runtime);
143+
const existing = store.lookup(params.sessionKey);
144+
if (!existing || existing.generation !== params.expectedGeneration || !existing.pending) {
145+
return;
146+
}
147+
store.register(params.sessionKey, {
148+
accountId: existing.accountId ?? existing.pending.accountId,
149+
credentialFingerprint: existing.credentialFingerprint ?? existing.pending.credentialFingerprint,
150+
destinationIdentity: existing.destinationIdentity,
151+
generation: existing.generation,
152+
});
153+
}
154+
110155
export function hasPendingDiscussionOpenForDestination(params: {
111156
runtime: PluginRuntime;
112157
serverBaseUrl: string;

‎extensions/clickclack/src/discussions/binding-store.ts‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ export type ClickClackDiscussionBinding = {
2424
displayTitle?: string;
2525
};
2626

27-
export function bindingMatchesSessionIncarnation(
28-
runtime: PluginRuntime,
29-
sessionKey: string,
30-
binding: ClickClackDiscussionBinding,
31-
): boolean {
32-
const entry = runtime.agent.session.getSessionEntry({
33-
sessionKey,
34-
readConsistency: "latest",
35-
});
36-
return Boolean(entry && binding.sessionId && entry.sessionId === binding.sessionId);
37-
}
38-
3927
export function bindingMatchesActiveSessionIncarnation(
4028
runtime: PluginRuntime,
4129
sessionKey: string,
@@ -53,6 +41,32 @@ export function bindingMatchesActiveSessionIncarnation(
5341
);
5442
}
5543

44+
/**
45+
* Refresh the replaceable session attachment without changing the durable room identity.
46+
* The store registers persisted state before reindexing, so a failed write leaves the
47+
* previous attachment authoritative in both persistence and memory.
48+
*/
49+
export function attachBindingToCurrentActiveSession(params: {
50+
runtime: PluginRuntime;
51+
store: ClickClackDiscussionBindingStore;
52+
sessionKey: string;
53+
binding: ClickClackDiscussionBinding;
54+
}): ClickClackDiscussionBinding | undefined {
55+
const entry = params.runtime.agent.session.getSessionEntry({
56+
sessionKey: params.sessionKey,
57+
readConsistency: "latest",
58+
});
59+
if (!entry?.sessionId || entry.archivedAt !== undefined) {
60+
return undefined;
61+
}
62+
if (entry.sessionId === params.binding.sessionId) {
63+
return params.binding;
64+
}
65+
const attached = { ...params.binding, sessionId: entry.sessionId };
66+
params.store.set(params.sessionKey, attached);
67+
return attached;
68+
}
69+
5670
const DISCUSSION_BINDINGS_NAMESPACE = "discussion-bindings";
5771
const MAX_DISCUSSION_BINDINGS = 10_000;
5872
const storesByRuntime = new WeakMap<PluginRuntime, ClickClackDiscussionBindingStore>();

0 commit comments

Comments
 (0)