Skip to content

Lua hot reload: tuned constants take their new values; fixes from an in-game run - #1550

Merged
Frotty merged 4 commits into
masterfrom
lua-hot-reload-constant-values
Oct 24, 2026
Merged

Frotty merged 4 commits into
masterfrom
lua-hot-reload-constant-values

Conversation

@Frotty

@Frotty Frotty commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Tuning a constant now reaches the running map, and four hot reload defects found by running every kind of edit in the game are fixed. Design and limits: docs/LUA_HOT_RELOAD.md ("Constants", "Measured in the game").

Tuned constants

A changed initial value of a constant (constant, let, a @config override of one, a static constant) runs again in the running map when it only computes its value, and so does every constant computed from it, in initialisation order. Nothing but its initializer writes a constant, so it gets the value a restarted map would give it.

  • ConstantInitializers decides which initializers may run again: they read their own locals, constants initialised before them and the game's constants, write only their own global, and call natives whose result depends on their arguments alone (not getters of game state such as GetPlayerTeam), the backend's operator intrinsics and functions which follow the same rules. The stack-trace bookkeeping does not count. A constant also runs again when a function it calls changes: the functions are part of the revision the map records.
  • Everything else keeps its value with the warning, as before: variables, constants which allocate or call a native with effects, array constants (running one again would keep entries an edit drops), and constants which read a variable, an array element (code may assign one of a constant array) or a constant initialised later.
  • The map records the revision of each constant it last ran (__wurst_hotreload_constants), so an undone edit runs the original again, a missed payload changes nothing the map has, and a constant a payload added follows later edits. A constant whose storage the baseline or an earlier payload declared as a variable, initialised or not, keeps its value, and so does every constant computed from one (Manifest.variables, History.variables).

Fixes found in the game

Each has a test which fails before its fix:

  • A closure in a package's init block crashed every -luaHotReload build with "unhandled case: InitBlockImpl" (from Lua hot reload: preserve named closure identities across sibling additions #1549). The standard library's Preloader has one, so hot run of a Lua map with the standard library did not build. The owner of such a closure is now its package (closuresInAnInitBlockAreNamedAndReloaded).
  • A closure in a constructor crashed the build the same way ("Cannot change parent of element ImVarImpl"): the owner is the constructor's construct function (closuresInAConstructorAreNamedAndReloaded).
  • With -stacktraces, a call passes its source position, so a moved line changed the bodies below it. Closures told apart by their order alone have protected bodies (Lua hot reload: preserve named closure identities across sibling additions #1549), so any edit above them was refused. Comparisons of what must not change or what already ran now leave the positions out (Function.shape); the payload still ships the new positions (movedLinesDoNotFreezeClosuresToldApartByOrder).
  • A global added to a package which starts before another made every later payload print "global initializer order changed ... restart the map". Order is now compared only among initializers which first ran together, at the start or in one payload (addedGlobalBeforeALaterPackageIsNoReorder).

Also:

  • A session can be driven without the editor: LuaHotReloadSession start|send <build dir> <CustomMapData> after a -build ... -luaHotReload.
  • A payload's status (__wurst_hotreload_status()) carries what it printed it could not apply, and the kept-value message names the global rather than its initializer function.

Evidence

Measured on the 3.0.1.24371 client, single player, in a test map built with -build ... -lua -luaHotReload and a project's default run arguments (-runcompiletimefunctions -injectobjects -stacktraces), its session driven by the new command line. Nine payloads in one game, 44 checks, all passed:

Phase Edit In the game
1 15 kinds of body: free function, override, trigger action and periodic callback registered before, both generics, tuple, varargs, constructor, ondestroy, string escapes, static every body new
2 300 bodies, and a function with 210 locals applied
3 an edit of phase 1 undone the original body is back, the other edits stay
4 a stale payload, one for another script, one which does not compile each rejected, nothing changed
5 a field's type changed, then a new function, global, class with a field default, interface, lambda, field the first refused with the reasons, the second applied
6 a closure bound to another variable added before an existing one, whose body changed the closure made at the start runs its new body with what it captured (11); new ones 1014
7 a configured constant 5 to 7, a real constant, a variable 7, the constant computed from it 10 to 14, the string computed with I2S "lvl7", the real 250; the variable kept 5
8 a new package its init ran once
9 a constant of that package, 40 to 55 55; the init did not run again

A thread waiting in TriggerSleepAction through phase 1 resumed in its old body and then called the new one by name. Every payload which applied printed nothing but "applied payload n". A payload arrived 25 to 51 s after the map asked for it, almost all of it the build.

The runs, in order: before the fixes the map did not build (the init block crash, then the constructor one); with those fixed, phases 5 to 9 were refused (moved lines); with that fixed, every payload from phase 5 printed the order warning; then 44 of 44. After the review rounds below, the suite ran again on the final head (bef18f1f4): 44 of 44.

Unit tests, Windows: LuaHotReloadTests (91) and DeterministicChecks (37, including the constants manifest across unit orders) on the final head; LuaBackendAuditTests (297), LuaTranslationTests (152), ClosureTests (68), HotReloadPipelineTests (6) and RunArgsTests (5) on 5b53be90f, before the last change, which only narrows the natives a constant may call.

Review

Codex found five more cases where a constant could run again from a value a restart would not give it, each fixed with a test which fails before the fix: a read of a constant array's element, a former variable declared without an initializer, a variable made a constant (now it keeps its value, as the constants computed from it already did), a constant computed by a function whose body changed (it did not follow), and getters of game state in the optimiser's list of pure natives. That list is unchanged here, since the optimiser uses it.

Builds without -luaHotReload are unchanged by construction: every change is behind the flag (the stack-trace globals are only recorded). No large-map byte comparison was run for this PR.

Not covered

  • The editor's part (the language server's hot run and wurst.hotreload) was not measured again; the in-game run builds from the command line. Linux is covered by PR CI; macOS is unverified.
  • Values copied at startup do not follow a tuned constant (a unit's life set in init, a timer's period): that needs the library reload hook.
  • Editing the body of one of several anonymous closures of one interface in one function (two doAfter(...) -> callbacks, say) is still refused, by Lua hot reload: preserve named closure identities across sibling additions #1549's protection against swaps. Allowing it when the group's size is unchanged and only one of its closures changed (a swap changes two) would keep that protection; left for a decision.

…in-game run

A changed initial value of a constant which only computes its value runs again in the
running map, and so do the constants computed from it; variables, allocating or impure
initializers and array constants keep their values with the warning as before.

Running every kind of edit in the game found four defects, each fixed with a test:
- a closure in a package's init block crashed every -luaHotReload build (the standard
  library's Preloader has one);
- a closure in a constructor crashed it the same way;
- with -stacktraces a moved line counted as a change of closures told apart by their
  order, so any edit above them was refused;
- a global added to a package which starts before another made every later payload
  warn that the initializer order changed.

A session can be driven without the editor (LuaHotReloadSession start/send), and a
payload's status carries what it printed it could not apply.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T08:57:25.771840Z bef18f1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ab3a4da6d8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…eeps a constant's value

Code may assign an element of a constant array, so a constant computed from one keeps its
value. A variable need not have an initializer, so former variables are tracked by their
storage (Manifest.variables, History.variables) rather than by initializer.
@Frotty

Frotty commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@Frotty

Frotty commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 09d49666f8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…ariable keeps its value

A constant computed by a pure function runs again when that function changes: the manifest
lists the functions each constant's initializer calls, and their shapes are part of the
revision the map records. A constant whose own storage was a variable in a version the map
ran keeps its value, as the constants computed from it already did.
@Frotty

Frotty commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5b53be90f6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…rguments alone

The optimiser's list of pure natives has getters of game state (GetPlayerTeam, GetWorldBounds,
GetUnitDefault*), which a payload could find changed since the start. Hot reload allows only
natives whose result depends on their arguments, and the conversions to constant handles.
@Frotty

Frotty commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: bef18f1f45

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Frotty
Frotty merged commit d18f851 into master Oct 24, 2026
2 checks passed
@Frotty
Frotty deleted the lua-hot-reload-constant-values branch November 25, 2026 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant