aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 533a9647d94ac91da85810b9c25a5cac61472e5d
parent 40479ea5b1cd84c7eb90194f0f9be2e2a7e081f7
Author: anon <anon@example.com>
Date:   Wed,  4 Nov 2026 15:37:44 +0000

Clear stale ground-drop sources during pending equipment

Diffstat:
MTODO.md | 9+++++++++
Mdocs/inventory.md | 27+++++++++++++++++++++++++++
Aserver/browser_pending_stale_drop_test.go | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mserver/ground_drop_test.go | 16+++++++++++++---
Mserver/inventory.go | 4+++-
Mserver/pending_stale_equipment_test.go | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/client/test_pending_stale_drop.mjs | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 318 insertions(+), 4 deletions(-)

diff --git a/TODO.md b/TODO.md @@ -4253,6 +4253,15 @@ and server restarts. Turn every reproduced bug into a focused regression test. deadline. Native completion, page replay, recovered replacement and cold loading retain both physical weapons; the broader audit remains open. See [pending equipment](docs/inventory.md#another-move-while-equipment-is-pending). +- [x] [P0] Clear an unrelated stale carried ground-drop source during pending + equipment. Reproduction: move a second weapon without refreshing its old icon, + hold the first equip, then drop the stale icon using native mouse input. + Previously no reply cleared that source. Completion check: ordered cancellation + and clearing affect only the empty row, with unchanged pending identity and + deadline, no ownership writes, successful original completion and both weapons + usable after page replay and retained by cold loading. Focused wear/removal, + boundary-slot and protected-source checks pass; the broader audit remains open. + See [pending equipment](docs/inventory.md#another-move-while-equipment-is-pending). - [x] [P0] Clear a stale empty right-hand source after native removal. A controlled earlier transfer leaves the worn axe visible; removal sends101:6 to111 but previously received no reply. Cancellation, the existing unequip notification diff --git a/docs/inventory.md b/docs/inventory.md @@ -741,6 +741,33 @@ retained identities. Focused normal checks pass11 roots/42 results; related race checks pass29 roots/117 results in175.956s. Maintained-package vet and JavaScript syntax checks pass; details are in that folder's `verification.json`. +Ground-drop requests need the same empty-row correction. The focused checkpoint +holds the first axe's equip while a missed refresh leaves the second axe's old +native slot49 visible. Actual mouse input sends Drop3a243f41 from that slot; the +pending-equipment guard previously returned without a reply. It now allows +action146 then112 for an unrelated empty carried row and sends the existing drop +refusal feedback. The admitted move's source, destination and item stay protected. +This changes GUI correction only; ground storage remains unsupported. + +`TestStaleGroundDropDuringEquipment` checks pending wear/removal, first/last +unrelated empty rows, repeat requests, zero writes, unchanged deadlines and exact +completion/cold ownership. `TestGroundDropRefusalPreservesOwnedSourcesAndEquipment` +also checks protected pending source/destination and invalid requests. +`TestBrowserStaleDropDuringEquipment` reproduces the missed-refresh failure with +real native input, completes the first equip, then reloads and equips the retained +second axe from its committed location. Both identities survive cold loading. +These controlled items and held completion are ownership evidence, not earned +progression. Baseline `local/pending-stale-drop/20261104-073308` fails in23.77s; +the fixed run `20261104-073435` passes in21.54s with four clean, complete traces. +Focused Go checks pass7 roots/43 results in15.315s; race checks pass8 roots/44 +results in104.074s. Maintained-package vet, JavaScript syntax and whitespace +checks pass. Independent packet/state checks and source hashes are recorded in +`local/pending-stale-drop/verification.json`. + +```sh +AO_GAME_TEST=1 go test ./server -run '^TestBrowserStaleDropDuringEquipment$' -count=1 -timeout=3m -v +``` + ### Owned-item Use during pending equipment An authenticated owned-item Use arriving during an admitted equipment move now diff --git a/server/browser_pending_stale_drop_test.go b/server/browser_pending_stale_drop_test.go @@ -0,0 +1,92 @@ +package main + +import ( + "context" + "crypto/sha256" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "runtime" + "slices" + "testing" + "time" +) + +func TestBrowserStaleDropDuringEquipment(t *testing.T) { + if os.Getenv("AO_GAME_TEST") != "1" { + t.Skip("set AO_GAME_TEST=1 for a stale drop during pending equipment") + } + + _, source, _, _ := runtime.Caller(0) + root := filepath.Dir(filepath.Dir(source)) + output := filepath.Join(root, "local/pending-stale-drop", time.Now().Format("20060102-150405")) + world := newTestServer(t) + player := world.newCharacter() + player.stats = testCharacterStats() + player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("pending-stale-drop"))) + if err := world.saveUser_locked(player); err != nil { + t.Fatal(err) + } + for range 2 { + if granted, err := world.grantItem_locked(player, 209283); err != nil || !granted { + t.Fatal(granted, err) + } + } + mux := http.NewServeMux() + mux.Handle("/", world.httpHandler()) + mux.HandleFunc("GET /test-state", func(w http.ResponseWriter, r *http.Request) { + world.mutex.Lock() + defer world.mutex.Unlock() + var pending any + if client := world.clients[1]; client != nil && client.equipment != nil { + pending = map[string]any{"itemID": client.equipment.itemID, "source": client.equipment.source, + "destination": client.equipment.destination, "readyAt": client.equipment.readyAt} + } + json.NewEncoder(w).Encode(map[string]any{"inventory": player.inventory, "pending": pending}) + }) + // A committed move with no refresh leaves the second carried icon stale. + mux.HandleFunc("POST /test-stale-view", func(w http.ResponseWriter, r *http.Request) { + world.updateMutex.Lock() + defer world.updateMutex.Unlock() + world.mutex.Lock() + defer world.mutex.Unlock() + inventory := player.inventory + inventory.Items = slices.Clone(inventory.Items) + inventory.Items[1].Slot = 66 + if err := world.saveInventory_locked(player, inventory); err != nil { + t.Error(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + } + }) + mux.HandleFunc("POST /test-complete", func(w http.ResponseWriter, r *http.Request) { + world.mutex.Lock() + finish := world.clients[1].equipment.readyAt + world.mutex.Unlock() + world.advanceEquipment(finish) + }) + host := httptest.NewServer(mux) + t.Cleanup(host.Close) + closeTestBrowserStreams(t, world) + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + + command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pending_stale_drop.mjs"), host.URL, output) + command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr + err := command.Run() + t.Logf("Browser artifacts: %s", output) + + if err != nil { + t.Fatal(err) + } + cold := newTestInventoryServer(t) + expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 66, Count: 1}, + {ID: 2, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}} + if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) || + world.nextItem != 2 || cold.nextItem != 2 { + t.Fatal("stale-source correction lost an equipment instance") + } +} diff --git a/server/ground_drop_test.go b/server/ground_drop_test.go @@ -21,7 +21,7 @@ func groundDropPacket(owner, page, slot uint32) []byte { } func TestGroundDropRefusalPreservesOwnedSourcesAndEquipment(t *testing.T) { - for _, scenario := range []string{"carried", "backpack", "closed backpack", "stale backpack", "stale closed backpack", "outside backpack", "separate pending", "admitted pending", "stale pending", "foreign actor", "unknown handle", "outside", "worn page"} { + for _, scenario := range []string{"carried", "backpack", "closed backpack", "stale backpack", "stale closed backpack", "outside backpack", "separate pending", "admitted pending", "pending destination", "stale pending", "foreign actor", "unknown handle", "outside", "worn page"} { t.Run(scenario, func(t *testing.T) { world, conn := newEquipmentWorld(t) player, client := world.characters[1], world.clients[1] @@ -61,7 +61,7 @@ func TestGroundDropRefusalPreservesOwnedSourcesAndEquipment(t *testing.T) { t.Fatal(err) } } - case "separate pending", "admitted pending", "stale pending": + case "separate pending", "admitted pending", "pending destination", "stale pending": if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil || client.equipment == nil { t.Fatal("axe did not begin its admitted move", err) } @@ -71,6 +71,13 @@ func TestGroundDropRefusalPreservesOwnedSourcesAndEquipment(t *testing.T) { if scenario == "stale pending" { request = groundDropPacket(1, 104, 67)[20:] } + if scenario == "pending destination" { + world.advanceEquipment(client.equipment.readyAt) + if err := world.handleInventory(client, itemMovePacket(1, 6, 64)[20:]); err != nil || client.equipment == nil { + t.Fatal("axe did not begin its admitted removal", err) + } + request = groundDropPacket(1, 104, 64)[20:] + } case "foreign actor": request = groundDropPacket(2, 104, 66)[20:] case "unknown handle": @@ -107,7 +114,7 @@ func TestGroundDropRefusalPreservesOwnedSourcesAndEquipment(t *testing.T) { if err != nil || !bytes.Equal(before, after) || len(checkpointWrites(t, world)) != 0 || !reflect.DeepEqual(expected, cold) || !reflect.DeepEqual(handles, client.backpacks) || world.nextItem != 3 || - (scenario == "separate pending" || scenario == "admitted pending" || scenario == "stale pending") != (client.equipment != nil) || + (scenario == "separate pending" || scenario == "admitted pending" || scenario == "pending destination" || scenario == "stale pending") != (client.equipment != nil) || client.equipment != nil && *client.equipment != pending { t.Fatal("ground-drop refusal changed ownership, writes, handles or admitted equipment", err) } @@ -115,6 +122,9 @@ func TestGroundDropRefusalPreservesOwnedSourcesAndEquipment(t *testing.T) { switch scenario { case "carried", "separate pending": replies = [][]byte{targetPacket(itemMovePacket(1, 66, 64)), targetPacket(itemMovePacket(1, 64, 66))} + case "stale pending": + replies = [][]byte{targetPacket(characterActionPacket(1, [5]uint32{146, 104, 67})), + targetPacket(characterActionPacket(1, [5]uint32{112, 104, 67}))} case "backpack", "closed backpack", "stale closed backpack": replies = [][]byte{targetPacket(characterActionPacket(1, [5]uint32{146, 107, 1 << 16}))} case "stale backpack": diff --git a/server/inventory.go b/server/inventory.go @@ -248,7 +248,9 @@ func (server *server) handleInventory(client *worldClient, body []byte) error { var packets [][]byte if page == 104 { index := slices.IndexFunc(character.inventory.Items, func(item ownedItem) bool { return item.Slot == slot }) - if slot < 64 || slot >= 94 || client.equipment != nil && (index < 0 || character.inventory.Items[index].ID == client.equipment.itemID) { + if slot < 64 || slot >= 94 || client.equipment != nil && + (slot == client.equipment.source || slot == client.equipment.destination || + index >= 0 && character.inventory.Items[index].ID == client.equipment.itemID) { return nil } if index < 0 { diff --git a/server/pending_stale_equipment_test.go b/server/pending_stale_equipment_test.go @@ -73,3 +73,70 @@ func TestStaleCarriedSourceDuringEquipment(t *testing.T) { } } } + +func TestStaleGroundDropDuringEquipment(t *testing.T) { + for _, removing := range []bool{false, true} { + for _, source := range []uint32{65, 93} { + t.Run(fmt.Sprintf("removing%v/source%d", removing, source), func(t *testing.T) { + world, output := newEquipmentWorld(t) + player, client := world.characters[1], world.clients[1] + if granted, err := world.grantItem_locked(player, 209283); err != nil || !granted { + t.Fatal(granted, err) + } + inventory := player.inventory + inventory.Items = slices.Clone(inventory.Items) + inventory.Items[1].Slot = 67 + if err := world.saveInventory_locked(player, inventory); err != nil { + t.Fatal(err) + } + if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil || client.equipment == nil { + t.Fatal("fixture equip did not start", err) + } + if removing { + world.advanceEquipment(client.equipment.readyAt) + if err := world.handleInventory(client, itemMovePacket(1, 6, 64)[20:]); err != nil || client.equipment == nil { + t.Fatal("fixture removal did not start", err) + } + } + pending := *client.equipment + before, err := readTestItemStore(world) + if err != nil { + t.Fatal(err) + } + output.packets = nil + auditCheckpointWrites(t, world) + + for range 2 { + if err := world.handleInventory(client, groundDropPacket(1, 104, source)[20:]); err != nil { + t.Fatal(err) + } + } + after, err := readTestItemStore(world) + + if err != nil || !bytes.Equal(before, after) || len(checkpointWrites(t, world)) != 0 || + client.equipment == nil || *client.equipment != pending || world.nextItem != 2 || len(output.packets) != 6 { + t.Fatal("stale drop omitted correction or changed storage, allocation or pending equipment", err, len(output.packets)) + } + for repeat := range 2 { + for index, action := range []uint32{146, 112} { + if !bytes.Equal(output.packets[repeat*3+index], targetPacket(characterActionPacket(1, [5]uint32{action, 104, source}))) { + t.Fatal("stale drop did not cancel then clear only its empty carried row") + } + } + } + world.advanceEquipment(pending.readyAt) + cold := newTestInventoryServer(t) + expectedSlot := uint32(6) + if removing { + expectedSlot = 64 + } + expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: expectedSlot, Count: 1}, + {ID: 2, Type: 51018, Template: 209283, Quality: 1, Slot: 67, Count: 1}} + if client.equipment != nil || !slices.Equal(player.inventory.Items, expected) || + !slices.Equal(cold.characters[1].inventory.Items, expected) || cold.nextItem != 2 { + t.Fatal("equipment completion lost either weapon after a stale drop") + } + }) + } + } +} diff --git a/tools/client/test_pending_stale_drop.mjs b/tools/client/test_pending_stale_drop.mjs @@ -0,0 +1,107 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs/promises'; +import {createRequire} from 'node:module'; +import {GameTest} from './game_test.mjs'; + +const {chromium} = createRequire(import.meta.url)('playwright'); +const [url, output] = process.argv.slice(2); +const browser = await chromium.launch({executablePath: '/usr/bin/chromium', + args: ['--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage', + '--disable-background-timer-throttling', '--disable-renderer-backgrounding']}); +let game; +const evidence = {}; +try { + const context = await browser.newContext({viewport: {width: 1024, height: 768}}); + await context.addCookies([{name: 'ao_client', value: 'pending-stale-drop', url}]); + const page = await context.newPage(); + game = await GameTest.attach(page); + const server = async () => (await page.request.get(url + '/test-state')).json(); + const cell = slot => game.state.grids.flatMap(grid => grid.cells).find(cell => + cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy); + const equip = async slot => { + const source = cell(slot); + assert(source, 'equipment source must be usable'); + const sequence = game.sequence; + await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); + await game.click(867, 206); + await game.wait('actual equipment request', () => game.events.some(event => event.sequence > sequence && + event.direction === 'framesent' && event.opcode === '47537a24' && + Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === slot)); + return sequence; + }; + const idle = () => game.wait('native equip animation ends', state => + !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); + await page.goto(url); + await game.ready(); + await game.key('i'); + await page.keyboard.press('Control+1'); + await game.wait('both fixture weapons visible', () => cell(48) && cell(49)); + if (!game.state.camera.firstPerson) await game.key('F8'); + const point = await game.point(500, 400); + await page.mouse.move(point.x, point.y); + await page.mouse.down({button: 'right'}); + try { + await page.mouse.move(point.x, point.y + 150, {steps: 8}); + await game.snapshot(); + } finally { + const release = game.sequence; + await page.mouse.up({button: 'right'}); + await game.waitInput(release, 1026); + } + assert((await page.request.post(url + '/test-stale-view')).ok()); + await equip(48); + evidence.pending = await server(); + assert(evidence.pending.pending); + await idle(); + + // Real ground-drop input addresses the old, now-empty carried row. + const source = cell(49); + await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); + await game.wait('stale source picked', state => state.dragged[0] === 104 && state.dragged[1] === 49); + const sequence = game.sequence; + await game.click(500, 580); + await game.wait('actual stale ground-drop request', () => game.events.some(event => event.sequence > sequence && + event.direction === 'framesent' && event.opcode === '3a243f41' && + Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104 && Buffer.from(event.hex, 'hex').readUInt32BE(33) === 49)); + evidence.refused = await server(); + assert.deepEqual(evidence.refused, evidence.pending); + await game.save(output + '/requested'); + await game.wait('stale source cancelled and cleared during pending equipment', () => + !cell(49) && game.events.filter(event => event.sequence > sequence && + event.direction === 'framereceived' && event.opcode === '5e477770').length >= 2); + assert.deepEqual(game.events.filter(event => event.sequence > sequence && event.direction === 'framereceived' && + event.opcode === '5e477770').map(event => { + const packet = Buffer.from(event.hex, 'hex'); + return [packet.readUInt32BE(29), packet.readUInt32BE(33), packet.readUInt32BE(37)]; + }), [[146, 104, 49], [112, 104, 49]]); + assert.deepEqual(game.state.dragged, [0, 0]); + await game.save(output + '/corrected'); + assert((await page.request.post(url + '/test-complete')).ok()); + await game.wait('original pending weapon completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); + evidence.completed = await server(); + assert.equal(evidence.completed.pending, null); + + // Page recovery exposes the committed location; that exact weapon must equip. + await page.reload(); + await game.ready(); + await page.waitForFunction(() => touchRegion(500, 400)?.world === true); + await game.snapshot(); + if (!cell(50)) await game.key('i'); + await page.keyboard.press('Control+1'); + await game.wait('both exact retained weapons replay', () => cell(50) && game.local().inventory.some(item => item.id === 1 && item.slot === 6)); + await equip(50); + assert((await page.request.post(url + '/test-complete')).ok()); + await game.wait('recovered weapon replaces original', state => game.local(state).inventory.some(item => item.id === 2 && item.slot === 6)); + evidence.after = await server(); + assert.deepEqual(evidence.after.inventory.Items.map(item => [item.ID, item.Slot, item.Count]), [[1, 66, 1], [2, 6, 1]]); + await game.save(output + '/retained'); + assert.deepEqual(game.errors, []); + assert.equal(game.dropped, 0); + console.log('PASS: pending equip survives stale ground-drop correction and both weapons remain usable after recovery'); +} finally { + try { + await fs.mkdir(output, {recursive: true}); + await fs.writeFile(output + '/verification.json', JSON.stringify(evidence, null, 2) + '\n'); + if (game) await game.save(output + '/final'); + } finally { await browser.close(); } +}