Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): probe a wwebjs session that awaits operator action

Yudhi Armyndharisc0a7356b23aa
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index 649302a58..e21082d2f 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -6471,6 +6471,22 @@ describe('WhatsAppWebJsAdapter.probeLiveness (session watchdog probe)', () => {
await expect(adapter.probeLiveness()).resolves.toBe(false);
});
+ it('asks the page when the session awaits operator action, so the watchdog can tell a live page from a dead one', async () => {
+ // The watchdog probes ACTION_REQUIRED observe-only; a probe that never asked reported "dead" for a
+ // healthy page and could never report the recovery.
+ const alive = adapterWith(
+ { getState: jest.fn().mockResolvedValue(WAState.CONNECTED) },
+ EngineStatus.ACTION_REQUIRED,
+ );
+ const gone = adapterWith(
+ { getState: jest.fn().mockRejectedValue(new Error('Target closed')) },
+ EngineStatus.ACTION_REQUIRED,
+ );
+
+ await expect(alive.probeLiveness()).resolves.toBe(true);
+ await expect(gone.probeLiveness()).resolves.toBe(false);
+ });
+
it('resolves false without touching the client when the adapter is not READY', async () => {
const getState = jest.fn();
const adapter = adapterWith({ getState }, EngineStatus.AUTHENTICATING);
@@ -6616,7 +6632,7 @@ describe('WhatsAppWebJsAdapter navigation re-inject grace (#1081)', () => {
const { adapter, client } = wireAdapter();
client.pupPage.emit('framenavigated', navFrame());
- (adapter as unknown as { status: EngineStatus }).status = EngineStatus.ACTION_REQUIRED;
+ (adapter as unknown as { status: EngineStatus }).status = EngineStatus.DISCONNECTED;
await expect(adapter.probeLiveness()).resolves.toBe(false);
expect(client.getState).not.toHaveBeenCalled();
diff --git a/src/engine/adapters/wwebjs-lifecycle.ts b/src/engine/adapters/wwebjs-lifecycle.ts
index cd5d50b60..d51f59b4c 100644
--- a/src/engine/adapters/wwebjs-lifecycle.ts
+++ b/src/engine/adapters/wwebjs-lifecycle.ts
@@ -1114,7 +1114,11 @@ export class WwebjsLifecycle {
* failing probe answers alive, since the page is healing, not dead (#1081).
*/
async probeLiveness(): Promise<boolean> {
- if (this.status !== EngineStatus.READY || !this.client) return false;
+ // The watchdog also probes an ACTION_REQUIRED session, observe-only (it never reconnects one): the page
+ // is still up while the operator acts. Answering false without asking made every such session look
+ // dead on the first tick, and hid a page that really had died.
+ const probeable = this.status === EngineStatus.READY || this.status === EngineStatus.ACTION_REQUIRED;
+ if (!probeable || !this.client) return false;
let timeout: ReturnType<typeof setTimeout> | undefined;
try {