World API¶
Central coordination service for entity lifecycle and system execution.
Overview¶
The World is the main entry point for AgentECS. It coordinates: - Entity lifecycle: Spawn, destroy, and query entities - Component management: Get, set, and query components - System execution: Register and execute systems with proper isolation - Entity operations: Merge and split entities dynamically
Key Features: - Snapshot isolation: Systems see their own writes immediately - Atomic updates: Changes applied at tick boundaries - Parallel execution: Non-conflicting systems run concurrently
World Class¶
The central coordinator for your ECS world.
World
¶
Central world state and system execution coordinator.
Owns storage backend and execution strategy. Systems interact via ScopedAccess, not World directly. Execution strategy handles all system registration and orchestration.
Source code in src/agentecs/world/world.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
__init__(storage=None, execution=None)
¶
Source code in src/agentecs/world/world.py
spawn(*components)
¶
Create entity with components. For use outside systems.
Source code in src/agentecs/world/world.py
destroy(entity)
¶
set(entity, component)
¶
set_singleton(component)
¶
merge_entities(entity1, entity2)
¶
Merge two entities into a single new entity.
Components implementing Combinable protocol are merged via combine. In other cases, entity2's component takes precedence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity1
|
EntityId
|
First entity to merge. |
required |
entity2
|
EntityId
|
Second entity to merge. |
required |
Returns:
| Type | Description |
|---|---|
EntityId
|
EntityId of the newly created merged entity. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either entity doesn't exist. |
Example
merged = world.merge_entities(agent1, agent2)
agent1 and agent2 are destroyed¶
merged has combined components¶
Source code in src/agentecs/world/world.py
split_entity(entity)
¶
Split one entity into two new entities.
Components implementing Splittable protocol are split via split. Non-splittable components are duplicated on a component-by-component basis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
EntityId
|
Entity to split. |
required |
Returns:
| Type | Description |
|---|---|
tuple[EntityId, EntityId]
|
Tuple of (first_entity, second_entity) IDs. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If entity doesn't exist |
Example
left, right = world.split_entity(agent)
original agent is destroyed¶
left and right have split components¶
Source code in src/agentecs/world/world.py
register_system(descriptor)
¶
Register system for execution.
Delegates to the injected execution strategy.
execute_system(descriptor)
¶
Execute single system synchronously (wrapper for execute_system_async).
For backward compatibility. Prefer execute_system_async() in async contexts.
Source code in src/agentecs/world/world.py
execute_system_async(descriptor)
async
¶
Execute single system asynchronously, returning collected changes.
Handles both sync and async systems automatically based on descriptor.is_async.
Source code in src/agentecs/world/world.py
tick()
¶
Execute all registered systems once synchronously (wrapper for tick_async).
For backward compatibility and simple scripts. Prefer tick_async() in async contexts.
tick_async()
async
¶
Execute all registered systems once asynchronously.
Delegates to the injected execution strategy, which handles parallelization, conflict detection, or other orchestration logic.
Source code in src/agentecs/world/world.py
apply_result(result)
¶
Apply system result to world state (sync wrapper for backward compatibility).
Prefer apply_result_async() in async contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
SystemResult
|
System execution result containing all changes. |
required |
Returns:
| Type | Description |
|---|---|
list[EntityId]
|
List of newly created entity IDs from spawns. |
Source code in src/agentecs/world/world.py
Access Control¶
Systems access the world through scoped interfaces that enforce access patterns.
ScopedAccess¶
ScopedAccess
¶
World access scoped to system's declared patterns with magic methods.
Provides snapshot isolation: sees own writes immediately, others' writes only after tick boundary.
Gotcha: For PURE mode systems, write methods raise AccessViolation.
Source code in src/agentecs/world/access.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
__getitem__(key)
¶
Get directly the component T for the entity in key.
Example: world[entity, Position] -> Position
Source code in src/agentecs/world/access.py
__setitem__(key, value)
¶
Set directly the component T for entity in key.
Example: world[entity, Position] = new_pos.
__delitem__(key)
¶
Delete component.
Example: Del world[entity, Position].
__contains__(key)
¶
__call__(*component_types)
¶
Create a query for component types.
Example: world(Position, Velocity) -> QueryResult iterable.
Source code in src/agentecs/world/access.py
spawn(*components)
¶
Spawn new entity with components. Returns provisional ID.
Source code in src/agentecs/world/access.py
ReadOnlyAccess¶
ReadOnlyAccess
¶
Bases: Protocol
Read-only world view for PURE and READONLY systems.
Source code in src/agentecs/world/access.py
EntityHandle¶
Convenient wrapper for single-entity operations.
EntityHandle
¶
Convenient wrapper for repeated single-entity operations.
Provides dict-style access to an entity's components with syntax like: e[Position] = pos, del e[Velocity], Position in e.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access
|
ScopedAccess
|
ScopedAccess instance. |
required |
entity
|
EntityId
|
EntityId to wrap. |
required |
Source code in src/agentecs/world/access.py
System Results¶
Systems can return results describing changes to apply.
SystemResult
dataclass
¶
Accumulated changes from system execution.
Source code in src/agentecs/world/result.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
ops
property
¶
Returns all ops.
updates
property
¶
Returns all updates as {entity: {Type: component}}.
inserts
property
¶
Returns all inserts as {entity: [components]}.
removes
property
¶
Returns all removes as {entity: [component types]}.
spawns
property
¶
Returns all spawns as list of component tuples.
spawn_count
property
¶
Returns total number of queued spawn operations.
destroys
property
¶
Returns all destroys as list of entity IDs.
record_update(entity, component)
¶
Record an update operation for an entity's component.
Source code in src/agentecs/world/result.py
record_insert(entity, component)
¶
Record an insert operation for an entity's component.
Source code in src/agentecs/world/result.py
record_remove(entity, component_type)
¶
Record a remove operation for an entity's component type.
Source code in src/agentecs/world/result.py
record_spawn(*components)
¶
Record a spawn operation for a new entity with given components.
Source code in src/agentecs/world/result.py
record_destroy(entity)
¶
Record a destroy operation for an entity.
Source code in src/agentecs/world/result.py
is_empty()
¶
Check if this result contains no changes.
Returns:
| Type | Description |
|---|---|
bool
|
True if result has no updates, inserts, removes, spawns, or destroys. |
merge(other)
¶
Merge other result into this one.
ADDS all ops from other into this result. Does NOT check for conflicts or merge individual ops caller is responsible for ensuring this is safe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
SystemResult
|
SystemResult to merge into this one. |
required |
Source code in src/agentecs/world/result.py
normalize_result(raw)
¶
Convert any valid system return format to SystemResult.
Supports multiple return formats for convenience: - None: No changes - SystemResult: Direct passthrough - Dict[EntityId, Dict[type, Any]]: Entity to component dict - Dict[EntityId, Any]: Entity to single component - List[Tuple[EntityId, Any]]: List of (entity, component) pairs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
SystemReturn
|
System return value in any supported format. |
required |
Returns:
| Type | Description |
|---|---|
SystemResult
|
Normalized SystemResult. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If return value is not a recognized format. |
Source code in src/agentecs/world/result.py
Entity Allocator¶
Manages entity ID allocation with generational indices.
EntityAllocator
¶
Allocates entity IDs with generation tracking for recycling.
Maintains a free list of deallocated entity indices with incremented generations to safely reuse entity IDs. Starts allocation after reserved system entities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shard
|
int
|
Shard number for this allocator (default 0 for local). |
0
|
Source code in src/agentecs/storage/allocator.py
allocate()
¶
Allocate new entity ID, reusing recycled slots when available.
Prioritizes reusing freed entity IDs from the free list before allocating new indices. Reused IDs have incremented generation numbers.
Returns:
| Type | Description |
|---|---|
EntityId
|
Newly allocated EntityId. |
Source code in src/agentecs/storage/allocator.py
deallocate(entity)
¶
Return entity ID for reuse with incremented generation.
Adds the entity's index to the free list with an incremented generation number, making it available for reallocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
EntityId
|
Entity ID to deallocate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If entity is from a different shard. |
Source code in src/agentecs/storage/allocator.py
is_alive(entity)
¶
Check if entity ID is still valid (not recycled).
Compares entity's generation number with the current generation for that index to determine if the entity is still alive or has been recycled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
EntityId
|
Entity ID to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if entity is alive, False if recycled or from different shard. |