Skip to content

Entity generations (data 0.10.37); @adobe/ecs 0.8.0 queries, entered and exited systems - #287

Open
krisnye wants to merge 4 commits into
mainfrom
krisnye/entity-generations
Open

krisnye wants to merge 4 commits into
mainfrom
krisnye/entity-generations

Conversation

@krisnye

@krisnye krisnye commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Adds generations to the @adobe/data ECS entity ids, using the same layout as @adobe/ecs. A stale id that points at a deleted slot, or at a slot that has since been reused, now resolves to null instead of the new occupant. Generations exist in memory only: snapshots and the persistence cache keep exactly the 0.10.x format, so no reader or writer needs a rollout.

Entity layout (in memory)

entity = slot + generation * 2^32

  • Slot (low 32 bits): the same layout as before, a 2-bit quadrant plus a 30-bit local index. entity & 3, entity >>> 2 and the isPersistent/isShared helpers are unchanged.
  • Generation (high 21 bits): the generation goes up each time the slot is freed. When it reaches the last value (2^21 - 1), the slot is retired and never issued again, so an old id can never come back. Entity.none carries that last generation, so it can never be a live id.
  • Entity.schema is now integer with a range of 0 to Number.MAX_SAFE_INTEGER, so live entity columns are stored in a Float64Array.
  • Entity.none is now Number.MAX_SAFE_INTEGER, and it is still stored as -1.
  • New: Entity.generationOf, Entity.toEntity(localIndex, quadrant, generation?), Entity.toStoredEntity/fromStoredEntity, Entity.hasEntity, Entity.toStoredSchema, Entity.toStoredValue/fromStoredValue.

Stored format: unchanged

  • ECS_SNAPSHOT_VERSION stays at 2.

  • toData writes entity ids as i32 without their generation. This covers every place a stored entity id appears:

    • id and entity columns
    • ids inside JSON and struct columns (nullable oneOf, objects, arrays)
    • componentSchemas (i32 entity schemas)
    • location tables, which don't include generations

    Columns are copied up to capacity, so the output is byte-for-byte what 0.10.36 wrote. A test checks this against a fixture serialized by 0.10.36.

  • fromData widens stored i32 entity ids to generation 0 and maps a stored -1 to Entity.none. Every free slot below the high-water mark starts at generation 1, so a stored reference to an entity that was deleted before the save becomes stale once its slot is reused.

  • Known gap: suppose an entity is deleted and a new one reuses its slot in the same session. A reference to the deleted entity that was already dangling at save time will then point at the new occupant after reload.

  • @adobe/data-persistence also keeps its format:

    • The manifest stays at version 1.
    • Entity-location records stay 8 bytes.
    • The journal entity stays i32.
    • Entity columns are encoded through their stored i32 schema, so the manifest keeps recording number with stride 4, or struct, exactly as before.
    • On load, entity ids are widened to generation 0, and free slots start at generation 1.

Undo, rollback and replay

Undo and rollback need ids to come back exactly, so the undo operations now say how to restore them:

  • Store.claim(entity) makes the next create in that entity's quadrant return exactly entity.
  • The undo of a delete is an insert with entity set, which brings back the same id.
  • The undo of an insert is a delete with reclaim: true, which puts the slot back at its earlier generation.

This keeps undo/redo and the reconciling and concurrency replays deterministic across peers.

Tests

  • stored-snapshot.test.ts:
    • loads a 0.10.36 snapshot
    • checks that a reference to a deleted entity goes stale once its slot is reused
    • checks that the output matches the 0.10.36 bytes exactly
    • checks that generations are dropped on write
    • checks that none is stored as -1
  • entity-reference-durability.test.ts: entity, nullable, struct and array references round-trip through a checkpoint and through journal replay, and the manifest records i32 storage.
  • Location table: stale ids, claim, reset, retirement at the last generation, generations surviving a snapshot round-trip, and loading a table stored without generations.
  • All packages pass build, lint, typecheck and their tests locally.

@adobe/ecs 0.8.0: queries

Query

  • Query.create({ with?, not?, where? }) and Query.and(a, b) make queries. A query exposes its with, not and where.
  • Archetype query (Query): where gives shared components whole values, so it matches whole archetypes.
  • Row query (RowQuery): where matches any component's value: == != < <= > >= comparisons, or partial structs and tuples.
  • The kind is checked at compile time, and a row query is rejected where an archetype query is required.
  • There is no OR: one system per branch is faster (WASM runs only on archetypes that have every optional component).

Sites

  • Archetype queries: System.create components.query; lookups; render systems; Index.create; readonly tables; ecs-persistence selections.
  • Row queries: commands.despawnWhere.
  • Predicates on shared components are a system's components.filter.
  • The build follows a query through module-scope consts, imports, re-exports and namespaces.

Entered and exited systems

  • System.removed is now System.exited.
  • New System.entered runs for each entity that starts matching its query, after the sync point applies, and for existing matches when the system is added. Its commands apply at the next sync point. It runs on the world's thread only.

Removed

  • Component.derive and the derived trait; within stays.

Other packages

  • @adobe/ecs-persistence 0.3.0: Selection is { query?, resources? }.
  • @adobe/ecs-architecture 0.7.0: a query file kind, and the query-inferred rule.

Not yet supported

These are listed in followups.md:

  • a where on indexes;
  • a where on kernel lookups;
  • entered systems on joined threads.

Tests and performance

  • All ecs/ packages build and pass their tests, including the browser, threads and as-written projects.
  • Node and browser benchmarks show no regressions in A/B runs against the base. New benchmarks: spawn entered 10K and show entered 10K.

@krisnye krisnye changed the title Entity generations: 53-bit entity ids detect stale references; bump 0.10.37 Entity generations in memory; 0.10.x snapshot and persistence formats unchanged; bump 0.10.37 Nov 26, 2026
@krisnye

krisnye commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks. I took the preferred option in 8b95069: generations exist in memory only, so neither format changes and no rollout is needed.

  • ECS_SNAPSHOT_VERSION stays at 2. toData writes entity ids as i32 without their generation everywhere they appear: id and entity columns, ids inside JSON and struct columns (nullable, relation and array shapes), componentSchemas, and location tables without generations. The output is byte-for-byte what 0.10.36 writes, and a test checks this against a fixture serialized by 0.10.36.
  • fromData widens stored ids to generation 0 and reads -1 as Entity.none. Free slots start at generation 1, so a stored reference to a deleted slot goes stale once the slot is reused. The only gap is the one you described: a reference that was already dangling at save time.
  • @adobe/data-persistence keeps its format too. The manifest stays at v1, entity-location records stay 8 bytes and the journal entity stays i32. Entity columns are encoded through their stored i32 schema and widened on load.
  • A slot is retired when its generation reaches the maximum instead of wrapping, so an old id can never come back.
  • New tests: stored-snapshot.test.ts loads a 0.10.36 snapshot and checks that writes match it exactly, and entity-reference-durability.test.ts covers references through checkpoints and journal replay.

krisnye and others added 3 commits November 28, 2026 10:44
…ed slots

Co-Authored-By: OpenCode <noreply@opencode.ai>
… memory and retire at the last generation

Co-Authored-By: OpenCode <noreply@opencode.ai>
…m.exited; derived components removed

- Query.create and Query.and make archetype queries (with, not, shared values) and row queries (any value, comparisons, partial structs); every matching site takes one: systems, lookups, render systems, indexes, tables, despawnWhere, entered and exited systems, ecs-persistence selections
- Shared-value predicates are a system's filter
- System.removed is System.exited; System.entered runs for entities that start matching
- Component.derive and the derived trait are removed; within stays
- The build follows queries through module-scope consts and imports
- ecs-persistence 0.3.0, ecs-architecture 0.7.0 (query file kind)

Co-Authored-By: OpenCode <noreply@opencode.ai>
@krisnye
krisnye force-pushed the krisnye/entity-generations branch from 8b95069 to b882e5e Compare November 28, 2026 18:52
@krisnye krisnye changed the title Entity generations in memory; 0.10.x snapshot and persistence formats unchanged; bump 0.10.37 Entity generations (data 0.10.37); @adobe/ecs 0.8.0 queries, entered and exited systems Nov 28, 2026
…y-valued partitions are stored in the i32 form

This branch has not been deployed

No deployments
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