diff --git a/changelog/2026-11-22-tables-route-rests.md b/changelog/2026-11-22-tables-route-rests.md new file mode 100644 index 0000000000..ae155289d8 --- /dev/null +++ b/changelog/2026-11-22-tables-route-rests.md @@ -0,0 +1 @@ +Octatrack SDK: a table module's package now carries the detours its `runtime.json` marks `"rests"`. The tables route (`build.py --octabam-tables`, `runtime_units.compile_linked`, Mute Modes' route) dropped the list without a word, so the package's detours stayed patched while the module rested and kept their frame cost. It now marks them as the octabam route does and refuses a `"rests"` address that is not one of the module's own detours. `module:doctor`'s "cost at rest" line now reads the rest marks from the committed USB package of the module's version, not only from `runtime.json`, and fails when an address `runtime.json` names is not marked there. Every published package rebuilds byte for byte (`octatrack-module-packages.py --check`). Checked with `sdk/tests/test_runtime_units.py` and `scripts/module-cpu-price.test.mjs`: both new tests fail without the change. diff --git a/scripts/module-cpu-price.mjs b/scripts/module-cpu-price.mjs index 0145ad5260..0f86bffb66 100644 --- a/scripts/module-cpu-price.mjs +++ b/scripts/module-cpu-price.mjs @@ -269,12 +269,28 @@ export function releaseDate({ root, document }) { return typeof entry?.date === 'string' ? entry.date.slice(0, 10) : null } -/** Whether the module marks any of its detours "rests": its runtime*.json or its machine port. */ -export function declaresRests({ root, id }) { +/** The detours the module marks "rests" (0x addresses): its runtime*.json and its machine port. */ +export function declaredRests({ root, id }) { const folder = resolve(root, 'sdk/octatrack/modules', id) - for (const name of existsSync(folder) ? readdirSync(folder).filter(name => /^runtime.*\.json$/.test(name)) : []) - if ((readJson(join(folder, name))?.rests ?? []).length) return true - return Boolean((readJson(resolve(root, PORTS, 'machine-ports.json'))?.ports?.[id]?.rests ?? []).length) + const files = existsSync(folder) ? readdirSync(folder).filter(name => /^runtime.*\.json$/.test(name)) : [] + return [...files.flatMap(name => readJson(join(folder, name))?.rests ?? []), + ...readJson(resolve(root, PORTS, 'machine-ports.json'))?.ports?.[id]?.rests ?? []] +} +export const declaresRests = args => declaredRests(args).length > 0 + +/** The sites the module's committed USB package of `version` marks as resting (bit 7 of an ABI 7+ site's length byte, loader.h + * RUNTIME_SITE_RESTS), or null while none of that version is committed. What the base reads, not what runtime.json asks for. */ +export function packageRests({ root, id, version }) { + const row = readJson(resolve(root, 'src/engine/assets/octatrack-module-packages.json'))?.packages?.find(item => item.id === id) + const path = row?.version === version ? resolve(root, 'public', row.path) : null + if (!path || !existsSync(path)) return null + const data = readFileSync(path), abi = data.readUInt16BE(4), rests = new Set() + let at = ({ 7: 72, 8: 76, 9: 80, 10: 80 })[abi] + 4 * data.readUInt32BE(20) + data.readUInt32BE(8) + 4 * data.readUInt32BE(16) + for (let i = 0; abi >= 7 && i < data.readUInt32BE(24); i++) { + if (data[at + 4] & 0x80) rests.add(data.readUInt32BE(at)) + at += 40 + (data[at + 4] & 0x7f) + 2 * data[at + 7] + } + return rests } /** One module's cost at rest: { state: 'ok' | 'info' | 'warn' | 'fail', detail, fix? }. A machine or an effect is answered for by @@ -284,6 +300,10 @@ export function atRestStatus({ root, document, glue = null }) { const id = document.id, figure = atRestFigure({ root, document }) const machine = machineDescriptors({ root, id, glue }).length > 0 || document.compatibility?.location === 'Track machine' const judged = machine || document.compatibility?.effectId != null, measured = figure ? ' (' + figure.value + ' points measured at rest)' : '' + const built = packageRests({ root, id, version: document.version }) + const unmarked = built ? declaredRests({ root, id }).filter(address => !built.has(Number.parseInt(address, 16))) : [] + if (unmarked.length) return { state: 'fail', detail: 'its USB package ' + document.version + ' does not mark ' + unmarked.join(', ') + ' rests, which runtime.json names', + fix: 'rebuild it (npm run octatrack:module-packages); a builder route that drops "rests" is a Fix: pull request of its own' } if (answersInUse({ root, id })) return { state: 'ok', detail: 'it answers ev_in_use, so it rests while it acts on no track' + measured } if (judged && declaresRests({ root, id })) return { state: 'ok', detail: 'the base rests ' + (machine ? 'a machine no track uses' : 'an effect no slot uses') + ', its detours marked rests with it' + measured } if (!figure) return { state: 'info', detail: 'no cost at rest measured yet (test procedure step 9, npm run device:price)' } diff --git a/scripts/module-cpu-price.test.mjs b/scripts/module-cpu-price.test.mjs index c1258b1bfb..c80ce80592 100644 --- a/scripts/module-cpu-price.test.mjs +++ b/scripts/module-cpu-price.test.mjs @@ -195,6 +195,24 @@ describe('a module costs nothing while nobody uses it (owner, 20 November 2026)' expect(loud.fix).toContain('the base rests an effect no slot uses') }) + it('reads the rest marks from the committed USB package, not only from runtime.json', () => { + const site = (address, bytes, rests) => { const s = Buffer.alloc(40 + bytes); s.writeUInt32BE(address); s[4] = bytes | (rests ? 0x80 : 0); s[6] = bytes; return s } + const header = Buffer.alloc(76); header.write('MWRM'); header.writeUInt16BE(8, 4); header.writeUInt32BE(2, 24) + const root = fixture({ + 'sdk/octatrack/modules/mutes/runtime.json': { schema: 1, subscribe: [{ event: 'ev_in_use', fn: 'in_use', order: 50 }], rests: ['0x40004dc6'] }, + 'src/engine/assets/octatrack-module-packages.json': { packages: [{ id: 'mutes', version: '0.2.3', path: 'octatrack-modules/mutes/0.2.3.mwrm' }] }, + ...changelog('mutes', '0.2.3', AT_REST_FROM), + }) + mkdirSync(join(root, 'public/octatrack-modules/mutes'), { recursive: true }) + const built = rests => writeFileSync(join(root, 'public/octatrack-modules/mutes/0.2.3.mwrm'), Buffer.concat([header, site(0x40004dc6, 6, rests), site(0x40068efe, 4, false)])) + built(false) /* a builder route that dropped the marks: the base would keep the detour patched */ + expect(atRestStatus({ root, document: hook('mutes', '0.2.3', share(2.6)), glue: {} })).toMatchObject({ state: 'fail', detail: expect.stringContaining('0x40004dc6') }) + built(true) + expect(atRestStatus({ root, document: hook('mutes', '0.2.3', share(2.6)), glue: {} }).state).toBe('ok') + /* No package of this version committed yet (a draft): the packages check names that; this one reads runtime.json. */ + expect(atRestStatus({ root, document: hook('mutes', '0.2.4', share(2.6)), glue: {} }).state).toBe('ok') + }) + it('holds on main: no published release fails, whatever it costs at rest', () => { const catalog = JSON.parse(readFileSync(join(repo, 'sdk/catalog.json'), 'utf8')), glue = portGlue(repo) for (const item of catalog.modules) { diff --git a/sdk/machines/octatrack/module-adapters/candidate-recipes.json b/sdk/machines/octatrack/module-adapters/candidate-recipes.json index 6c62c8b563..98e20029a8 100644 --- a/sdk/machines/octatrack/module-adapters/candidate-recipes.json +++ b/sdk/machines/octatrack/module-adapters/candidate-recipes.json @@ -37,7 +37,7 @@ "sdk/machines/octatrack/module-adapters/candidate_packages.py": "59b116d89146a9cd39442a7bba30c662cfc0158e97c36e07285b7b55aa8a25e9", "sdk/machines/octatrack/module-adapters/runtime_candidate.py": "849b7351d8bfadd00187acb81088f8c561964616ff8eb663450c8f43503bacdd", "sdk/machines/octatrack/module-adapters/cave_runtime.py": "d168ad9bc0782e15c4901bba1599e3a7b56f3cb50adf2776fabcda2326479f9f", - "sdk/machines/octatrack/module-adapters/runtime_units.py": "2e915a4ebee285c4f08cda5f40c123ab9823580121a369de3f35618dea4c44f0", + "sdk/machines/octatrack/module-adapters/runtime_units.py": "059d115d9f3219d75bcecb3784399c1318f74c809f51a25b03df8e53f7aa5964", "sdk/machines/octatrack/module-adapters/runtime_tables.py": "113fc8cd2b33815794576667ddf9afbea3d4dff13ad4cb1227d5f891b1fb0dad", "sdk/machines/octatrack/module-adapters/stock_recipe.py": "662b79d9dc612845cf85e5051fa2d711cad4f98244194357fdaaabca092d5ca5", "sdk/machines/octatrack/module-adapters/standalone_runtime.py": "3a7559a330ae4f031c7c47b7cb67f679cd7fb4e4f2aa69f2e3971940be4c46ae", @@ -142,7 +142,7 @@ "sdk/machines/octatrack/module-adapters/candidate_packages.py": "59b116d89146a9cd39442a7bba30c662cfc0158e97c36e07285b7b55aa8a25e9", "sdk/machines/octatrack/module-adapters/runtime_candidate.py": "849b7351d8bfadd00187acb81088f8c561964616ff8eb663450c8f43503bacdd", "sdk/machines/octatrack/module-adapters/cave_runtime.py": "d168ad9bc0782e15c4901bba1599e3a7b56f3cb50adf2776fabcda2326479f9f", - "sdk/machines/octatrack/module-adapters/runtime_units.py": "2e915a4ebee285c4f08cda5f40c123ab9823580121a369de3f35618dea4c44f0", + "sdk/machines/octatrack/module-adapters/runtime_units.py": "059d115d9f3219d75bcecb3784399c1318f74c809f51a25b03df8e53f7aa5964", "sdk/machines/octatrack/module-adapters/runtime_tables.py": "113fc8cd2b33815794576667ddf9afbea3d4dff13ad4cb1227d5f891b1fb0dad", "sdk/machines/octatrack/module-adapters/stock_recipe.py": "662b79d9dc612845cf85e5051fa2d711cad4f98244194357fdaaabca092d5ca5", "sdk/machines/octatrack/module-adapters/standalone_runtime.py": "3a7559a330ae4f031c7c47b7cb67f679cd7fb4e4f2aa69f2e3971940be4c46ae", diff --git a/sdk/machines/octatrack/module-adapters/runtime_units.py b/sdk/machines/octatrack/module-adapters/runtime_units.py index 1bd5835ad7..2d8f435c1b 100644 --- a/sdk/machines/octatrack/module-adapters/runtime_units.py +++ b/sdk/machines/octatrack/module-adapters/runtime_units.py @@ -116,6 +116,7 @@ def compile_linked(module, sdk_root, builder, work, cross, selected, guards, sto digest, checked = builder.stock_digest(detour.expect) code = builder.OPCODES[detour.kind] + struct.pack('>I', symbols[detour.symbol]) + b'\x4e\x71' * ((n - 6) // 2) sites.append((detour.site, digest, code, [2], 2 if detour.kind == 'lea' else 0, checked)) + detoured = {site[0] for site in sites} for index, table in enumerate(module.tables): for address, expected in table.refs: sites.append((address, hashlib.sha256(struct.pack('>I', expected)).digest(), @@ -131,6 +132,11 @@ def compile_linked(module, sdk_root, builder, work, cross, selected, guards, sto for address, digest, _, _, _, checked in sites if stock_at is not None else (): if hashlib.sha256(stock_at(address, checked)).digest() != digest: raise ValueError('A guarded stock site changed.') + # runtime.json "rests", marked as build.py marks them on the octabam route: only the module's own detours + rest = {int(a, 16) for a in (declared or {}).get('rests', ())} + if rest - detoured: + raise ValueError('"rests" names only its own detours: ' + ', '.join('%#x' % a for a in sorted(rest - detoured))) + sites = [site + (True,) if site[0] in rest else site for site in sites] hooks = [] if declared: builder.unevented(sites, copies) diff --git a/sdk/tests/test_runtime_units.py b/sdk/tests/test_runtime_units.py index 502b03ee36..e0cd1e8f9b 100644 --- a/sdk/tests/test_runtime_units.py +++ b/sdk/tests/test_runtime_units.py @@ -45,6 +45,8 @@ def read(self, address, n): return self.data[address - 0x40001000:address - 0x40001000 + n] if address == 0x40002002 and n == 4: return struct.pack('>I', 0x40001000) + if address == 0x40002100 and n == 6: + return b'\x4e\x71' * 3 raise ValueError('Outside authored synthetic fixture') def prepare(self, name='work', selected=frozenset({'TEST'}), selected_builder=builder): @@ -97,6 +99,25 @@ def test_a_raising_package_asks_for_the_entry_by_index_and_may_take_the_tick(sel [self.guard], self.read, events=False) self.assertEqual((raw['image'], raw['hooks']), (normal['image'], [])) + def test_runtime_json_rests_mark_its_own_detours_in_the_package(self): + (self.sdk / 'row.s').write_text('.include "remix.inc"\n.text\n.global entry\nentry:\n.ifdef SC_KEY\nnop\n.endif\nrts\n' + '.ifdef MODWERK_EVENTS\n.global hook\nhook:\nrts\n.global in_use\nin_use:\nmoveq #0,%d0\nrts\n.endif\n') + self.module.detours = (SimpleNamespace(site=0x40002100, expect=b'\x4e\x71' * 3, unit='row', symbol='hook', + target=None, kind='jmp', pad_to=None),) + (self.sdk / 'modules/test').mkdir(parents=True) + declaration = self.sdk / 'modules/test/runtime.json' + declaration.write_text('{"schema": 1, "subscribe": [{"event": "ev_in_use", "fn": "in_use", "order": 50}], ' + '"rests": ["0x40002100"]}') + result = self.prepare() + data = builder.package(result['image'], result['bss'], result['hooks'], result['offsets'], result['sites'], + name='test', hashed=True, title='TEST', layout=1, copies=result['copies'], abi=8) + length = lambda address: data[data.index(struct.pack('>I', address)) + 4] + self.assertEqual(length(0x40002100), 6 | builder.REST_FLAG) # the detour rests with the module ... + self.assertEqual(length(0x40002002), 4) # ... a table reference never + declaration.write_text(declaration.read_text().replace('0x40002100', '0x40002002')) + with self.assertRaisesRegex(ValueError, 'only its own detours'): + self.prepare('table-rests') + def test_source_drift_and_mismatched_callable_variant_are_refused(self): (self.sdk / 'row.s').write_text('.text\n.global entry\nentry:\nnop\nrts\n') with self.assertRaisesRegex(ValueError, 'reference changed'):