aorecomp

Anarchy Online classic in the browser

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


commit 42856dad06aaf6bf30175ad78481673deb77fae9
parent bfb98cdff5a3bf018f43fbb91f258cc9a7c222d4
Author: anon <anon@example.com>
Date:   Wed,  4 Nov 2026 23:04:37 +0000

Close backpack splits refused during pending equipment

Diffstat:
MTODO.md | 10++++++++++
Mdocs/backpacks.md | 27+++++++++++++++++++++++++++
Mserver/backpacks.go | 4++--
Mserver/browser_pending_bag_stack_test.go | 29+++++++++++++++++++++++++----
Mserver/pending_bag_stack_test.go | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/client/test_pending_bag_split.mjs | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 250 insertions(+), 6 deletions(-)

diff --git a/TODO.md b/TODO.md @@ -4556,6 +4556,16 @@ and server restarts. Turn every reproduced bug into a focused regression test. checks pass 44 roots/124 results, focused race checks pass 5/19, and ten native traces are clean. See [session and destination refusals](docs/backpacks.md#session-and-destination-policy-refusals) and `local/backpack-refusals/verification-summary.json`. +- [x] [P0] Correct an optimistic backpack split refused during pending equipment. + Reproduction: Ctrl-split an owned three-charge stack while a separate real + equip awaits held server completion. The unchanged contents-only refusal leaves + an unowned clone. Exact window/session closure now removes it without changing + ownership, allocation or the pending item/deadline. The 23.02-second WASM check + completes the same equip, retries split/join normally and retains both original + stacks/six charges through page/cold recovery. Wear/removal, both sources and + repeats preserve exact ownership/deadlines without writes; related Go/race and + the native join control pass. This is controlled evidence, not earned progress. + See [pending backpack splits](docs/backpacks.md#stack-split-during-pending-equipment). - [x] [P0] Correct an optimistic split after an open backpack's source becomes empty. Reproduction: retain a three-charge native row while an earlier controlled transfer moves that stack to another owned bag, then Ctrl-click diff --git a/docs/backpacks.md b/docs/backpacks.md @@ -239,6 +239,33 @@ refusal, both usable sources,3+3 ->6 charges, and3 complete clean traces. All5 focused handler results, native syntax checking and vet pass. See `local/pending-bag-stack/verification-summary.json`. +## Stack split during pending equipment + +A valid split needs different refusal handling. The controlled native fixture +reproduces a third unowned row after real Ctrl-click while a separate equip +awaits authoritative completion. The original contents-only reply still lists +both original three-charge stacks, but the selected source and clone remain. +The server now closes that known bag's window/session for an in-range split +during pending equipment. The admitted item/deadline, ownership and allocation +stay unchanged, with no checkpoint writes on repeated refusal. + +`TestPendingBackpackStackSplitPreservesOwnershipAndRetries` covers both source +stacks during wear/removal, exact ordered closure, normal completion/reopening, +one admitted split and cold ownership/charge conservation. +`TestBrowserBackpackStackSplitDuringPendingEquipment` shares the existing join +fixture and uses `tools/client/test_pending_bag_split.mjs`. Its unchanged-handler +baseline fails in 24.96 seconds; the corrected native check passes in 23.02. +Normal placement refusal clears the selected source. Completing the same equip +permits ordinary Use and a real split/join that retains both original IDs and +six charges, with exactly one new identity allocated and retired by the join. +Page/cold recovery passes. The native join control still passes in 21.58 seconds. +These are controlled items/completion checkpoints, not earned progression or +evidence of original concurrent-inventory permission. Evidence is retained under +`local/pending-bag-split/`; the broader disappearance audit stays open. +Related Go checks pass 51 roots / 349 results in 64.378 seconds. +Focused race checks pass six roots / 33 results; the independent audit verifies +pending/deadline conservation, exact packets/allocation and seven clean traces. + ## Deposit during pending equipment Carried-item deposits into an open bag also need a refusal while equipment is diff --git a/server/backpacks.go b/server/backpacks.go @@ -450,8 +450,8 @@ func (server *server) handleBackpackAction_locked(client *worldClient, body []by } items := player.inventory.Containers[handle.id] index := slices.IndexFunc(items, func(item ownedItem) bool { return item.Slot == position&65535 }) - if action == 52 && position&65535 < 21 && (index < 0 || !items[index].isStackable()) { - // Contents alone cannot remove an optimistic split of a stale source. + if action == 52 && position&65535 < 21 && (client.equipment != nil || index < 0 || !items[index].isStackable()) { + // Contents alone cannot undo these refused optimistic splits. client.backpacks[position>>16] = backpackHandle{handle.id, false} return client.send(backpackClosePacket(client.id, handle.id)) } diff --git a/server/browser_pending_bag_stack_test.go b/server/browser_pending_bag_stack_test.go @@ -17,13 +17,27 @@ import ( ) func TestBrowserBackpackStackJoinDuringPendingEquipment(t *testing.T) { + testBrowserBackpackStacksDuringPendingEquipment(t, false) +} + +func TestBrowserBackpackStackSplitDuringPendingEquipment(t *testing.T) { + testBrowserBackpackStacksDuringPendingEquipment(t, true) +} + +func testBrowserBackpackStacksDuringPendingEquipment(t *testing.T, split bool) { + t.Helper() if os.Getenv("AO_GAME_TEST") != "1" { - t.Skip("set AO_GAME_TEST=1 for stack joining during pending equipment") + t.Skip("set AO_GAME_TEST=1 for backpack stacks during pending equipment") } _, source, _, _ := runtime.Caller(0) root := filepath.Dir(filepath.Dir(source)) output := filepath.Join(root, "local/pending-bag-stack", time.Now().Format("20060102-150405")) + driver := "test_pending_bag_stack.mjs" + if split { + output = filepath.Join(root, "local/pending-bag-split", time.Now().Format("20060102-150405")) + driver = "test_pending_bag_split.mjs" + } world := newTestServer(t) player := world.newCharacter() player.stats = testCharacterStats() @@ -68,7 +82,7 @@ func TestBrowserBackpackStackJoinDuringPendingEquipment(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) defer cancel() - command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pending_bag_stack.mjs"), host.URL, output) + command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client", driver), host.URL, output) command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr err := command.Run() t.Logf("Browser artifacts: %s", output) @@ -80,7 +94,14 @@ func TestBrowserBackpackStackJoinDuringPendingEquipment(t *testing.T) { expected := []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}, {ID: 2, Type: 51017, Template: 99228, Quality: 1, Slot: 65, Count: 1}} expectedContents := []ownedItem{{ID: 4, Type: 51005, Template: 152030, Quality: 42, Slot: 1, Count: 6}} - if !slices.Equal(cold.characters[1].inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Containers[2], expectedContents) { - t.Fatal("backpack join refusal/retry changed equipment or lost charges") + last := uint32(4) + if split { + expectedContents = []ownedItem{{ID: 3, Type: 51005, Template: 152030, Quality: 42, Slot: 0, Count: 3}, + {ID: 4, Type: 51005, Template: 152030, Quality: 42, Slot: 1, Count: 3}} + last = 5 + } + if !slices.Equal(cold.characters[1].inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Containers[2], expectedContents) || + cold.characters[1].inventory.LastID != last || cold.nextItem != last || world.nextItem != last { + t.Fatal("backpack stack refusal/retry changed equipment, allocation or charges") } } diff --git a/server/pending_bag_stack_test.go b/server/pending_bag_stack_test.go @@ -4,10 +4,89 @@ import ( "bytes" "encoding/binary" "fmt" + "reflect" "slices" "testing" ) +func TestPendingBackpackStackSplitPreservesOwnershipAndRetries(t *testing.T) { + for _, removing := range []bool{false, true} { + for _, slot := range []uint32{0, 1} { + t.Run(fmt.Sprintf("removing%v/slot%d", removing, slot), func(t *testing.T) { + world, output := newEquipmentWorld(t) + player, client := world.characters[1], world.clients[1] + for _, template := range []uint32{99228, 152030, 152030} { + if granted, err := world.grantItem_locked(player, template); err != nil || !granted { + t.Fatal(granted, err) + } + } + inventory := player.inventory + contents := slices.Clone(inventory.Items[2:]) + contents[0].Slot, contents[1].Slot = 0, 1 + inventory.Items = slices.Clone(inventory.Items[:2]) + inventory.Containers = map[uint32][]ownedItem{2: contents} + if err := world.saveInventory_locked(player, inventory); err != nil { + t.Fatal(err) + } + if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 65, 1)[20:]); err != nil { + t.Fatal(err) + } + if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil { + t.Fatal(err) + } + if removing { + world.advanceEquipment(client.equipment.readyAt) + if err := world.handleInventory(client, itemMovePacket(1, 6, 64)[20:]); err != nil { + t.Fatal(err) + } + } + pending := *client.equipment + before, err := readTestItemStore(world) + if err != nil { + t.Fatal(err) + } + auditCheckpointWrites(t, world) + output.packets = nil + request := characterActionPacket(1, [5]uint32{52, 107, 65536 + slot, 0, 1})[20:] + + for range 2 { + if err := world.handleInventory(client, request); err != nil { + t.Fatal(err) + } + } + after, err := readTestItemStore(world) + + close := targetPacket(backpackClosePacket(1, 2)) + if err != nil || !bytes.Equal(before, after) || len(checkpointWrites(t, world)) != 0 || + *client.equipment != pending || client.backpacks[1] != (backpackHandle{2, false}) || world.nextItem != 4 || + !reflect.DeepEqual(output.packets, [][]byte{close, close}) { + t.Fatal("pending split changed storage, allocation, equipment/deadline or exact window closure", err) + } + + world.advanceEquipment(pending.readyAt) + if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 65, 2)[20:]); err != nil { + t.Fatal(err) + } + if err := world.handleInventory(client, request); err != nil { + t.Fatal(err) + } + cold := newTestInventoryServer(t) + + expected := []ownedItem{{ID: 3, Type: 51005, Template: 152030, Quality: 42, Slot: 0, Count: 3}, + {ID: 4, Type: 51005, Template: 152030, Quality: 42, Slot: 1, Count: 3}, + {ID: 5, Type: 51005, Template: 152030, Quality: 42, Slot: 2, Count: 2}} + expected[slot].Count = 1 + if !slices.Equal(player.inventory.Containers[2], expected) || !slices.Equal(cold.characters[1].inventory.Containers[2], expected) || + !reflect.DeepEqual(player.inventory, cold.characters[1].inventory) || client.equipment != nil || + client.backpacks[1] != (backpackHandle{2, true}) || world.nextItem != 5 || cold.nextItem != 5 || + player.inventory.LastID != 5 || binary.BigEndian.Uint32(output.packets[len(output.packets)-1][29:33]) != 52 { + t.Fatal("normal reopening/split retry lost equipment, identities or six charges") + } + }) + } + } +} + func TestPendingBackpackStackJoinPreservesOwnershipAndRetries(t *testing.T) { for _, removing := range []bool{false, true} { for _, kept := range []uint32{0, 1} { diff --git a/tools/client/test_pending_bag_split.mjs b/tools/client/test_pending_bag_split.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-bag-stack', url}]); + const page = await context.newPage(); + game = await GameTest.attach(page); + await page.goto(url); + await game.ready(); + const server = async () => (await page.request.get(url + '/test-state')).json(); + const cell = (page, slot) => game.state.grids.flatMap(grid => grid.cells).find(cell => + cell.identity[0] === page && cell.identity[1] === slot && !cell.dummy); + const click = async source => { + assert(source, 'the exact native source must be usable'); + await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2); + }; + await game.key('KeyI'); + await game.click(735, 45, 'right'); + await game.wait('two three-charge stacks open', () => cell(107, 65536) && cell(107, 65537)); + await page.keyboard.press('Control+1'); + await click(cell(104, 48)); + await game.click(867, 206); + await game.wait('real carried weapon move sent', () => game.events.some(event => + event.direction === 'framesent' && event.opcode === '47537a24' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 104)); + const deadline = Date.now() + 10000; + do { + evidence.pending = await server(); + if (evidence.pending.pending) break; + await game.snapshot(); + } while (Date.now() < deadline); + assert(evidence.pending.pending); + await game.wait('local equipment bar ends before held server completion', state => + !game.local(state).actions.includes(3) && !game.local(state).actions.includes(6)); + const split = async () => { + const sequence = game.sequence; + await page.keyboard.down('Control'); + try { await click(cell(107, 65536)); } finally { await page.keyboard.up('Control'); } + await game.wait('actual native split request', () => game.events.some(event => event.sequence > sequence && + event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 52)); + return sequence; + }; + await split(); + evidence.requested = await server(); + await game.save(output + '/requested'); + await game.wait('refused optimistic clone removed', () => !cell(107, 65538)); + if (game.state.dragged[0] !== 0) { + await game.click(789, 45); + await game.wait('refused selected source placement clears', state => state.dragged[0] === 0); + } + evidence.refused = await server(); + assert.deepEqual(evidence.refused, evidence.pending); + await game.save(output + '/refused'); + + // Complete the unchanged move, then normally reopen and split/join the source. + assert((await page.request.post(url + '/test-complete')).ok()); + await game.wait('original weapon completes', state => game.local(state).inventory.some(item => item.id === 1 && item.slot === 6)); + if (!cell(107, 65536)) await game.click(735, 45, 'right'); + await game.wait('authoritative source stacks usable', () => cell(107, 65536) && cell(107, 65537) && !cell(107, 65538)); + const retry = await split(); + await game.wait('normal split acknowledged', () => game.events.some(event => event.sequence > retry && + event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 52)); + evidence.split = await server(); + assert.deepEqual(evidence.split.inventory.Containers[2].map(item => [item.ID, item.Slot, item.Count]), [[3, 0, 1], [4, 1, 3], [5, 2, 2]]); + if (game.state.dragged[0] !== 0) { + const grid = game.state.grids.find(grid => grid.cells.some(cell => cell.identity[0] === 107)); + await click(grid.cells.find(source => source.identity[0] === 0 && source.index > 2)); + await game.wait('normal split source placed', state => state.dragged[0] === 0); + } + const join = game.sequence; + await click(cell(107, 65538)); + await click(cell(107, 65536)); + await game.wait('normal join acknowledged', () => game.events.some(event => event.sequence > join && + event.direction === 'framereceived' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53)); + evidence.after = await server(); + assert.equal(evidence.after.pending, null); + assert.equal(evidence.after.inventory.LastID, 5); + assert.deepEqual(evidence.after.inventory.Containers, evidence.pending.inventory.Containers); + assert.deepEqual(evidence.after.inventory.Items, [ + {...evidence.pending.inventory.Items[0], Slot: 6}, evidence.pending.inventory.Items[1]]); + await game.save(output + '/retried'); + await page.reload(); + await game.ready(); + assert.deepEqual(game.local().inventory.map(item => [item.id, item.slot, item.count]).sort((a, b) => a[0] - b[0]), + [[1, 6, 1], [2, 49, 1]]); + await game.key('KeyI'); + await game.click(735, 45, 'right'); + await game.wait('page recovery retains both original stacks', () => cell(107, 65536) && cell(107, 65537) && !cell(107, 65538)); + assert.deepEqual((await server()).inventory, evidence.after.inventory); + assert.deepEqual(game.errors, []); + console.log('PASS: pending split refusal preserves equipment, two original stacks and six charges; real retry/page/cold retain exact ownership with one admitted allocation'); +} 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(); } +}