From db709206405e2fdbae088ef2ed352d0b6e830718 Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 6 Sep 2026 14:06:26 -0400 Subject: [PATCH] fix: retry gather/attack with a progress-based give-up (isolated retest) Testing this in isolation from the anti-throttle Chromium flags and the DB-lock change (reverted separately, ruled out on its own): the single fire-and-forget gather/attack action has no recovery if the one evaluated frame doesn't line up with the authoritative server position -- confirmed by live-instrumenting the unpatched code locally and watching it hang. This reinstates the fix from the earlier round, with both bugs found via live diagnostic testing already fixed going in: - Retries at a conservative cadence (~0.3s, well under the intent rate limit) instead of firing once and giving up forever. - Gives up on a target only once an actual progress signal (target HP for attack, held-item count for gather/loot) has stopped moving for a wall-clock budget -- not "hasn't succeeded since the click" (which wrongly aborts mid-fight during a normal cooldown lull) and not a frame/attempt count (which balloons into tens of real seconds on a tab with sparse rendered frames). - The dedicated boss-attack button now also resets the progress clock; it used to bypass action() entirely and read an uninitialized clock, immediately abandoning the boss for the nearest mob on click. Deliberately NOT re-adding the Chromium anti-throttle flags this round -- keeping this an isolated variable after the last round's session crash coincided with adding them. Also bumped one unrelated, independently-diagnosed timeout: the post-death new-character join in portal.spec.ts was polling on Playwright's default 5s timeout for a fresh join's first snapshot, with no connection to the retry logic above. Verified: tsc/build clean, three full local e2e runs (7/7 each). Co-Authored-By: Claude Sonnet 5 --- client/src/main.ts | 59 +++++++++++++++++++++++++++++++++++++++----- tests/portal.spec.ts | 3 ++- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/client/src/main.ts b/client/src/main.ts index 9f8e85b..d560eb7 100644 --- a/client/src/main.ts +++ b/client/src/main.ts @@ -18,6 +18,28 @@ let noticeTimer: ReturnType; let inputElapsed = 0; let walkTo: { x: number; z: number } | undefined; let pendingAction: { op: string; target: string } | undefined; +let actionElapsed = 0; +// A gather target can stay legitimately, persistently unreachable for a long time (a +// depleted resource waits out a real respawn timer measured in minutes, not a stray +// frame) -- retrying it forever would starve everything else. But a *working* attack is +// mostly cooldown rejections between the hits that land, so "stuck" can't just mean +// "hasn't succeeded since the click": it has to mean no progress at all for a while. +// Track a per-target progress signal (target HP for attack, held-item count for +// gather/loot) and only give up once that's stopped moving for a wall-clock budget -- +// not a frame/attempt count, since a throttled or backgrounded tab can go seconds +// between rendered frames, and counting attempts instead of real elapsed time let a +// "few tries" balloon into tens of real seconds. +let lastProgress: number | undefined; +let lastProgressAt = 0; +function progressSignature(): number | undefined { + if (!pendingAction || !snapshot) return undefined; + if (pendingAction.op === 'attack') { + if (pendingAction.target === 'boss') return snapshot.world.bossHp; + return snapshot.entities.find(e => e.id === pendingAction!.target)?.hp; + } + return Object.values(snapshot.self.inventory).reduce((a: number, b: number) => a + b, 0); +} +const avoidUntil = new Map(); let requestedClose = false; const keys = new Set(); let view: WorldView; @@ -42,16 +64,18 @@ function selection(entity?: Entity) { } function action(op: string) { if (!snapshot) return; + const now = Date.now(); const kinds = op === 'attack' ? ['boss', 'sentinel'] : ['tree', 'ore', 'herb', 'corpse']; let target = view.selected; - if (!target || !kinds.includes(target.kind)) { + if (!target || !kinds.includes(target.kind) || (avoidUntil.get(target.id) ?? 0) > now) { const p = snapshot.self.entity.position; - target = snapshot.entities.filter(e => kinds.includes(e.kind)).sort((a, b) => Math.hypot(a.position.x - p.x, a.position.z - p.z) - Math.hypot(b.position.x - p.x, b.position.z - p.z))[0]; + target = snapshot.entities.filter(e => kinds.includes(e.kind) && (avoidUntil.get(e.id) ?? 0) <= now).sort((a, b) => Math.hypot(a.position.x - p.x, a.position.z - p.z) - Math.hypot(b.position.x - p.x, b.position.z - p.z))[0]; } if (!target) { notice('No target nearby.'); return; } selection(target); pendingAction = { op: target.kind === 'corpse' ? 'loot' : op, target: target.id }; walkTo = target.position; + lastProgress = undefined; lastProgressAt = Date.now(); } function connect() { requestedClose = false; sequence = 0; @@ -157,8 +181,31 @@ async function boot() { const p = snapshot.self.entity.position, dx = walkTo.x - p.x, dz = walkTo.z - p.z, distance = Math.hypot(dx, dz); const target = snapshot.entities.find(e => e.id === pendingAction?.target); const range = pendingAction ? pendingAction.op === 'attack' ? config.attackRange + (target?.kind === 'boss' ? 3 : -0.5) : config.gatherRange - 0.5 : 0.8; - if (distance <= range) { if (pendingAction) send(pendingAction.op, pendingAction.target); walkTo = undefined; pendingAction = undefined; } - else { x = dx / distance; z = dz / distance; } + if (distance <= range) { + if (!pendingAction) { walkTo = undefined; } + // A single fire-and-forget attempt can land a hair outside the authoritative + // server position (which may lag a stale/throttled client render) and get + // rejected with no second chance, so retry at a conservative cadence (well + // under the intent rate limit) instead of giving up forever -- the server's + // own action cooldown makes repeat attempts harmless. But a target can also be + // legitimately, persistently rejected (a depleted resource waits out a real + // respawn timer), so give up on THIS target after a wall-clock budget and let + // the next action() pick a fresh one rather than retrying a lost cause forever. + else { + actionElapsed += dt; + if (actionElapsed >= 0.3) { + actionElapsed = 0; + const sig = progressSignature(); + if (sig !== undefined && sig !== lastProgress) { lastProgress = sig; lastProgressAt = Date.now(); } + if (Date.now() - lastProgressAt > 5000) { + avoidUntil.set(pendingAction.target, Date.now() + 8000); + const retryOp = pendingAction.op === 'loot' ? 'gather' : pendingAction.op; + walkTo = undefined; pendingAction = undefined; + action(retryOp); + } else { send(pendingAction.op, pendingAction.target); } + } + } + } else { x = dx / distance; z = dz / distance; actionElapsed = 0.3; } } view.moving = !!(x || z); if (inputElapsed >= 0.05) { send('move', '', x, z); inputElapsed = 0; } @@ -198,7 +245,7 @@ async function boot() { for (const id of ['inventory', 'settings']) { $(`${id}-toggle`).onclick = () => { visible(id, $(id).hidden); visible('movement', $('inventory').hidden && $('settings').hidden && !!snapshot); if (id === 'inventory' && snapshot) inventory(snapshot.self); }; } document.querySelectorAll('[data-close]').forEach(button => button.onclick = () => visible(button.dataset.close!, false)); $('attack').onclick = () => action('attack'); $('gather').onclick = () => action('gather'); $('heal').onclick = () => send('heal'); $('keystone').onclick = () => send('return'); - $('target-boss').onclick = () => { selection(snapshot?.entities.find(e => e.id === 'boss')); walkTo = { x: 0, z: 0 }; pendingAction = { op: 'attack', target: 'boss' }; }; + $('target-boss').onclick = () => { selection(snapshot?.entities.find(e => e.id === 'boss')); walkTo = { x: 0, z: 0 }; pendingAction = { op: 'attack', target: 'boss' }; lastProgress = undefined; lastProgressAt = Date.now(); }; $('dev-boss').onclick = () => void api('/api/dev/boss', {}).catch(e => notice(e.message)); $('reconnect').onclick = () => location.reload(); $('new-character').onclick = () => { requestedClose = true; ws?.close(); location.reload(); }; @@ -227,6 +274,6 @@ async function boot() { if (hit.entity) { selection(hit.entity); action(['boss', 'sentinel'].includes(hit.entity.kind) ? 'attack' : 'gather'); } else if (hit.point) { selection(); walkTo = hit.point; pendingAction = undefined; } }); - Object.defineProperty(window, '__aeternalis', { value: { diagnostics: () => view.diagnostics(), get snapshot() { return snapshot; } }, configurable: true }); + Object.defineProperty(window, '__aeternalis', { value: { diagnostics: () => view.diagnostics(), get snapshot() { return snapshot; }, get walkTo() { return walkTo; }, get pendingAction() { return pendingAction; }, get keys() { return [...keys]; } }, configurable: true }); } boot().catch(error => { console.error(error); $('arrival-error').textContent = `Unable to enter the world: ${error.message}`; $('connection-status').textContent = 'Unavailable'; }); diff --git a/tests/portal.spec.ts b/tests/portal.spec.ts index 68e5189..d437d50 100644 --- a/tests/portal.spec.ts +++ b/tests/portal.spec.ts @@ -52,7 +52,8 @@ test('admin actions are audited and death permits a distinct replacement', async await page.getByLabel('Character name', { exact: true }).fill('Portal second life'); await page.getByRole('button', { name: 'Enter world' }).click(); await expect(page.locator('#connection-status')).toHaveText('Connected'); - await expect.poll(() => page.evaluate(() => (window as any).__aeternalis?.snapshot?.self?.entity?.id)).toBeTruthy(); + // Default 5s poll was too tight for a fresh join's first snapshot on slow CI hardware. + await expect.poll(() => page.evaluate(() => (window as any).__aeternalis?.snapshot?.self?.entity?.id), { timeout: 15000 }).toBeTruthy(); const current = await page.evaluate(() => (window as any).__aeternalis.snapshot.self.entity.id); expect(current).not.toBe(old.entity.id); const account = await (await page.request.get('/api/account')).json();