Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: npm run check

windows-workspaces:
name: Workspace regression tests (Windows)
name: Workspace and QQ persistence regression tests (Windows)
runs-on: windows-latest
steps:
- name: Checkout
Expand All @@ -52,3 +52,6 @@ jobs:
test/conversation-workspace-races.test.mjs
test/conversation-workspace-switch-order.test.mjs
test/conversation-workspace-bind-recovery.test.mjs
test/channels/qq/state-store.test.mjs
test/channels/qq/reply-continuity.test.mjs
test/channels/qq/normal-update.test.mjs
60 changes: 60 additions & 0 deletions docs/qq-consumer-continuity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# QQ consumer continuity across controlled updates

An exclusive QQ consumer keeps accepted work bound to its original message when
the Provider restarts. The existing account state stores compact reply proofs,
the next native `msg_seq`, and any attempted reply budget. Message text remains
with the consumer. Source proofs contain a hash of that text and the original
account, route, timestamp and reply eligibility.

Proofs are written before inbound acceptance. Reply attempts are reserved before
the native POST. An uncertain send keeps its spent attempt across restart. A
definite rate-limit refusal releases the attempt and reuses the same sequence
for the existing bounded retry. The normal native reply window and reply budget
continue to apply.

Proof files are looked up by their known account/message key. The bounded memory
cache does not evict unfinished work from disk. Files are flushed before atomic
rename. On systems where Node supports directory handles, the containing
directories are synchronized too and a synchronization failure rejects the
operation. Windows retains file flushing and atomic replacement because Node
cannot fsync a directory there. This contract covers completed writes and
controlled process replacement, not a power-loss guarantee on every filesystem.

## Completing a source

The consumer can return `{ accepted: true, complete: true }` when no work remains,
or use the optional `sourceReleaseVersion: 1` operation:

```js
const result = await ctx.dshIm.releaseSource(botId, event.reply, {
expectedFingerprint: event.fingerprint,
signal,
});
```

The result is `{ released: true }`. A source with a previous native attempt keeps
its budget through the remaining reply window and returns `retainUntil` in
milliseconds since the epoch. The owner calls the same release operation again
after that time. Redelivery cannot reset a spent budget. Releasing a source does
not erase the consumer's own work or message history.

## Updating the receiver

The optional `inboundDrainVersion: 1` methods use the current verified account and
exclusive consumer lease:

1. Call `quiesceInbound(botId, { expectedFingerprint, signal })` while the lease is
still active. It stops intake, waits for callbacks and canonical acceptance,
then saves the native session/cursor. It returns
`{ stopped: true, resumeCursor: boolean }`.
2. Dispose the old lease after quiescing. Provider shutdown follows the same
ordering before unregistering the public delivery adapter.
3. Call `resumeInbound` to verify the same account, then register the new consumer
with `consumeInbound`. Verification can return
`{ connected: false, accountVerified: true }` while reception waits for its
consumer. A reconnect does not silently reopen a quiesced receiver.

The saved native session is bound to the account and credential. A rejected
inbound callback prevents a resumable cursor from being recorded. The platform
controls replay availability and retention. A saved cursor does not promise
recovery after abrupt termination or platform replay expiry.
4 changes: 4 additions & 0 deletions plugin-src/host/channels/qq/production.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export async function createProductionController(ctx, config = {}, internals = {
controller,
deliveryAdapter: createDeliveryAdapter({ channel: 'qq', workspaces, coreController, stateFor }),
ready: supervisor.ready,
async prepareClose() {
await supervisor.close();
await controller.close();
},
async close() {
await supervisor.close();
await controller.close();
Expand Down
5 changes: 3 additions & 2 deletions plugin-src/host/channels/shared/startup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ export async function installProductionChannel(ctx, config, {
const closeProduction = () => (closing ??= (async () => {
try {
unregisterSetup?.();
await unregisterDelivery?.();
await production?.prepareClose?.();
} finally {
await production?.close();
try { await unregisterDelivery?.(); }
finally { await production?.close(); }
}
})());
try {
Expand Down
15 changes: 15 additions & 0 deletions plugin-src/host/delivery-adapter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,26 @@ export function createDeliveryAdapter({ channel, workspaces, coreController, sta
}
return Object.freeze({
channel,
releaseSource: (botId, route, options) => {
if (typeof coreController.releaseSource !== 'function')
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
return coreController.releaseSource(botId, route, options);
},
consumeInbound: (botId, options) => {
if (typeof coreController.consumeInbound !== 'function')
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
return coreController.consumeInbound(botId, options);
},
quiesceInbound: (botId, options) => {
if (typeof coreController.quiesceInbound !== 'function')
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
return coreController.quiesceInbound(botId, options);
},
resumeInbound: (botId, options) => {
if (typeof coreController.resumeInbound !== 'function')
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
return coreController.resumeInbound(botId, options);
},
reactionChecked: (botId, route, reaction, options) => {
if (channel !== 'feishu' || typeof coreController.reactionChecked !== 'function')
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
Expand Down
51 changes: 51 additions & 0 deletions plugin-src/host/delivery-service.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,57 @@ export class DeliveryService {
return dispose;
}

/** Stop this receiver after canonical acceptance, before its lease is revoked.
* A saved cursor is a normal-update resume position, not a delivery guarantee.
*/
async quiesceInbound(botId, options = {}) {
const id = botIdOf(botId);
cancellation(options.signal);
if (!/^[a-f0-9]{64}$/.test(options.expectedFingerprint ?? '')) throw deliveryError('bad-request');
const registration = await this.#checkedRegistrationFor(id);
if (typeof registration.adapter.quiesceInbound !== 'function') throw deliveryError('capability-unavailable');
this.#assertRegistered(registration);
const result = await registration.adapter.quiesceInbound(id, {
...options, signal: options.signal ? AbortSignal.any([options.signal, registration.controller.signal])
: registration.controller.signal,
});
this.#assertRegistered(registration);
if (result?.stopped !== true || typeof result.resumeCursor !== 'boolean') throw deliveryError('stop-result-unknown');
return result;
}

async releaseSource(botId, route, options = {}) {
const id = botIdOf(botId);
cancellation(options.signal);
if (!/^[a-f0-9]{64}$/.test(options.expectedFingerprint ?? '')) throw deliveryError('bad-request');
const registration = await this.#checkedRegistrationFor(id);
if (typeof registration.adapter.releaseSource !== 'function') throw deliveryError('capability-unavailable');
this.#assertRegistered(registration);
const result = await registration.adapter.releaseSource(id, structuredClone(route), {
...options, signal: options.signal ? AbortSignal.any([options.signal, registration.controller.signal]) : registration.controller.signal,
});
this.#assertRegistered(registration);
cancellation(options.signal);
return result;
}

/** Reopen an explicitly registered receiver before inspecting its connection. */
async resumeInbound(botId, options = {}) {
const id = botIdOf(botId);
cancellation(options.signal);
if (!/^[a-f0-9]{64}$/.test(options.expectedFingerprint ?? '')) throw deliveryError('bad-request');
const registration = await this.#checkedRegistrationFor(id);
if (typeof registration.adapter.resumeInbound !== 'function') throw deliveryError('capability-unavailable');
this.#assertRegistered(registration);
const result = await registration.adapter.resumeInbound(id, {
...options, signal: options.signal ? AbortSignal.any([options.signal, registration.controller.signal])
: registration.controller.signal,
});
this.#assertRegistered(registration);
if (typeof result?.connected !== 'boolean' || result.accountVerified !== true) throw deliveryError('start-result-unknown');
return result;
}

async reactionChecked(botId, route, reaction, options = {}) {
const id = botIdOf(botId);
cancellation(options.signal);
Expand Down
5 changes: 5 additions & 0 deletions plugin-src/host/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ export function createImHostPlugin(internals = {}) {
reactionVersion: 1,
reactionChecked: (botId, route, reaction, options) => deliveryService.reactionChecked(botId, route, reaction, options),
inboundVersion: 1,
inboundDrainVersion: 1,
sourceReleaseVersion: 1,
releaseSource: (botId, route, options) => deliveryService.releaseSource(botId, route, options),
quiesceInbound: (botId, options) => deliveryService.quiesceInbound(botId, options),
resumeInbound: (botId, options) => deliveryService.resumeInbound(botId, options),
fileVersion: 1,
approvalCardVersion: 1,
questionCardVersion: 1,
Expand Down
Loading