commit 7795ded2e70250f61c491fbdf6d922c8aee5ae72
parent 856702a5ddcb17be133825a026b28712cee77199
Author: anon <anon@example.com>
Date: Tue, 3 Nov 2026 12:22:03 +0000
Restore ordinary stack sources after overflow refusal
Diffstat:
6 files changed, 206 insertions(+), 0 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -3782,6 +3782,14 @@ and server restarts. Turn every reproduced bug into a focused regression test.
cases, native completion/retry, page replay and exact cold ownership pass;
this leaves the broader audit open. See
[pending equipment](docs/inventory.md#another-move-while-equipment-is-pending).
+- [x] [P0] Restore a carried stack source after unsigned-short overflow refusal.
+ A controlled native 1 + 65535 join sent action 53 and left the removed source
+ hidden with an empty cursor. Explicit GUI correction now preserves both
+ usable icons, all 65536 charges and exact page/cold ownership without writing
+ storage or acknowledging a successful join. Both directions and acceptance
+ exactly at the ceiling have handler regressions. The 18-second WASM check
+ retries the refused source; the historical stack cap remains unresolved. See
+ [overflow refusal](docs/inventory-stacks.md#overflow-refusal-feedback).
- [x] [P0] Restore a carried stack source after a join refused during pending
equipment. A controlled late-completion checkpoint reproduced native action 53
leaving the removed stack hidden with an empty cursor. Restore that validated
diff --git a/docs/inventory-stacks.md b/docs/inventory-stacks.md
@@ -69,6 +69,31 @@ nor price stat 74 = 50000 establishes a merge maximum. The historical server
cap remains unresolved. Overflow assembly evidence:
`ev_e650f59cb6fa2ba4d648045ef820df9906bafaacc81b90e200adaede6597eec4`.
+### Overflow refusal feedback
+
+An ordinary native join of controlled one-charge and 65535-charge stacks sends
+action 53 and hides its removed source while awaiting the response. Silently
+refusing the sum leaves that source dummy with an empty cursor, despite both
+instances remaining owned. The overflow guard now restores the validated removed
+ordinary source using the existing two ContainerAddItem GUI moves, without a
+successful join acknowledgement or storage writes. Both quantities and final
+placements stay unchanged. This does not change the representation ceiling or
+establish the historical server's stack limit.
+
+`TestStackOverflowRefusalRestoresRemovedSource` checks both directions, exact
+source feedback, zero checkpoint writes and cold ownership. The existing ceiling
+regression still accepts 40000 plus 25535 and refuses 40000 plus 25536.
+`TestBrowserStackJoinOverflowRefusal` sends two real joins from the same source,
+verifies both usable icons and all 65536 charges, then checks page replay and
+cold identities. These fixture quantities are controlled; no acquisition or
+earned-progression claim is made. Baseline failures and native captures are
+retained under `local/stack-overflow/`.
+
+Related Go checks pass 96 roots and 377 results; focused race checks pass three
+roots and 11 results. Vet and Node syntax checks pass. The native run completes
+in 18.30 seconds with three clean, complete traces. Independent trace checks and
+source hashes are in `local/stack-overflow/verification.json`.
+
## Persistence and rejection
Each instance has a ushort count in the world database. The supported Essence
diff --git a/server/browser_stack_overflow_test.go b/server/browser_stack_overflow_test.go
@@ -0,0 +1,60 @@
+package main
+
+import (
+ "context"
+ "crypto/sha256"
+ "fmt"
+ "net/http/httptest"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "runtime"
+ "slices"
+ "testing"
+ "time"
+)
+
+func TestBrowserStackJoinOverflowRefusal(t *testing.T) {
+ if os.Getenv("AO_GAME_TEST") != "1" {
+ t.Skip("set AO_GAME_TEST=1 for stack overflow refusal")
+ }
+
+ _, source, _, _ := runtime.Caller(0)
+ root := filepath.Dir(filepath.Dir(source))
+ output := filepath.Join(root, "local/stack-overflow", time.Now().Format("20060102-150405"))
+ world := newTestServer(t)
+ player := world.newCharacter()
+ player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("stack-overflow")))
+ if err := world.saveUser_locked(player); err != nil {
+ t.Fatal(err)
+ }
+ for range 2 {
+ if granted, err := world.grantItem_locked(player, 152030); err != nil || !granted {
+ t.Fatal(granted, err)
+ }
+ }
+ player.inventory.Items[0].Count, player.inventory.Items[1].Count = 1, 65535
+ if err := world.saveInventory_locked(player, player.inventory); err != nil {
+ t.Fatal(err)
+ }
+ expected := slices.Clone(player.inventory.Items)
+ host := httptest.NewServer(world.httpHandler())
+ t.Cleanup(host.Close)
+ closeTestBrowserStreams(t, world)
+ ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
+ defer cancel()
+
+ command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_stack_overflow.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)
+ if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) ||
+ world.nextItem != 2 || cold.nextItem != 2 {
+ t.Fatal("overflow refusal changed quantities, identities or placements")
+ }
+}
diff --git a/server/stack_overflow_test.go b/server/stack_overflow_test.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "bytes"
+ "encoding/binary"
+ "fmt"
+ "slices"
+ "testing"
+)
+
+func TestStackOverflowRefusalRestoresRemovedSource(t *testing.T) {
+ for _, removed := range []uint32{64, 65} {
+ t.Run(fmt.Sprint(removed), func(t *testing.T) {
+ world, conn := newStackWorld(t)
+ player := world.characters[1]
+ player.inventory.Items[0].Count, player.inventory.Items[1].Count = 1, 65535
+ if err := world.saveInventory_locked(player, player.inventory); err != nil {
+ t.Fatal(err)
+ }
+ before := slices.Clone(player.inventory.Items)
+ persisted, err := readTestItemStore(world)
+ if err != nil {
+ t.Fatal(err)
+ }
+ auditCheckpointWrites(t, world)
+ conn.packets = nil
+ kept := uint32(64)
+ if removed == 64 {
+ kept = 65
+ }
+
+ err = world.handleInventory(world.clients[1], stackActionPacket(1, 53, kept, removed)[20:])
+ after, readErr := readTestItemStore(world)
+ cold := newTestInventoryServer(t)
+
+ if err != nil || readErr != nil || !bytes.Equal(persisted, after) ||
+ !slices.Equal(player.inventory.Items, before) || !slices.Equal(cold.characters[1].inventory.Items, before) ||
+ world.nextItem != 2 || cold.nextItem != 2 || len(conn.packets) != 2 {
+ t.Fatal("overflow refusal lost charges, ownership or native feedback", err, readErr)
+ }
+ if writes := checkpointWrites(t, world); len(writes) != 0 {
+ t.Fatal("overflow refusal wrote durable state", writes)
+ }
+ for index, move := range [][2]uint32{{removed, kept}, {kept, removed}} {
+ packet := conn.packets[index]
+ if binary.BigEndian.Uint32(packet[16:20]) != 0x47537a24 ||
+ binary.BigEndian.Uint32(packet[33:37]) != move[0]-16 || binary.BigEndian.Uint32(packet[45:49]) != move[1]-16 {
+ t.Fatal("overflow refusal did not restore the removed source")
+ }
+ }
+ })
+ }
+}
diff --git a/server/stacks.go b/server/stacks.go
@@ -74,6 +74,14 @@ func (server *server) changeStack_locked(client *worldClient, index int, body []
// Native ushort addition wraps; prevent loss at its representation ceiling.
// This is not evidence of the original server's maximum permitted stack.
if count > 65535 {
+ // The ordinary grid hides the removed source while awaiting a reply.
+ if page == 104 {
+ for _, packet := range equipmentCorrectionPackets(client.id, items[other].Slot) {
+ if err := client.send(packet); err != nil {
+ return err
+ }
+ }
+ }
return nil
}
items[index].Count = uint16(count)
diff --git a/tools/client/test_stack_overflow.mjs b/tools/client/test_stack_overflow.mjs
@@ -0,0 +1,52 @@
+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;
+try {
+ const context = await browser.newContext({viewport: {width: 1024, height: 768}});
+ await context.addCookies([{name: 'ao_client', value: 'stack-overflow', url}]);
+ const page = await context.newPage();
+ game = await GameTest.attach(page);
+ await page.goto(url);
+ await game.ready();
+ await game.key('i');
+ const cell = slot => game.state.grids.flatMap(grid => grid.cells).find(cell =>
+ cell.identity[0] === 104 && cell.identity[1] === slot && !cell.dummy);
+ const click = async slot => {
+ const source = cell(slot);
+ assert(source, 'the exact source must be usable');
+ await game.click((source.bounds[0] + source.bounds[2]) / 2, (source.bounds[1] + source.bounds[3]) / 2);
+ };
+
+ // Repeated real joins must be refused without hiding either retained stack.
+ for (const attempt of [0, 1]) {
+ const sequence = game.sequence;
+ await click(48);
+ await click(49);
+ await game.wait('actual overflow join request', () => game.events.some(event => event.sequence > sequence &&
+ event.direction === 'framesent' && event.opcode === '5e477770' && Buffer.from(event.hex, 'hex').readUInt32BE(29) === 53));
+ await game.save(output + '/refused-' + attempt);
+ await game.wait('both overflow sources remain usable', () => cell(48) && cell(49));
+ assert.deepEqual(game.state.dragged, [0, 0]);
+ assert.deepEqual(game.local().inventory.map(item => [item.slot, item.count]).sort((a, b) => a[0] - b[0]),
+ [[48, 1], [49, 65535]]);
+ }
+ 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, 48, 1], [2, 49, 65535]]);
+ assert.deepEqual(game.errors, []);
+ console.log('PASS: repeated overflow refusal preserves usable icons and all 65536 charges through page recovery');
+} finally {
+ try {
+ await fs.mkdir(output, {recursive: true});
+ if (game) await game.save(output + '/final');
+ } finally { await browser.close(); }
+}