Skip to content

Add application-scoped state API (ctx.app_state)#542

Open
diptanu wants to merge 1 commit intomainfrom
feature/app-state
Open

Add application-scoped state API (ctx.app_state)#542
diptanu wants to merge 1 commit intomainfrom
feature/app-state

Conversation

@diptanu
Copy link
Copy Markdown
Contributor

@diptanu diptanu commented Mar 2, 2026

Summary

Some agents require updating context about users or objects they are managing. Application State is a way for applications to share state across request boundaries as they learn about the problem domain.

For example, a fitness tracker agent managing fitness logs of multiple users can store per-user context in app_state as a flexible K/V store — much more practical than using rigid database schemas for evolving agent knowledge.

API

ctx = RequestContext.get()

# Write — persists across all requests within the same application
ctx.app_state.set("user_123_preferences", preferences)

# Read — available from any function execution in the same app
prefs = ctx.app_state.get("user_123_preferences")

How it works

  • Mirrors the existing request-scoped ctx.state architecture at every layer
  • Same 3-phase blob protocol: prepare_read → transfer → commit_write
  • Same presigned URL pattern for blob store access
  • Different URI namespace: {namespace}/{application}/app_state/{key} (vs {namespace}/{application}/{request_id}/state/{key} for request state)
  • Last-writer-wins concurrency (S3 PUT is atomic per-object)

Changes

  • SDK interface: ApplicationState class + app_state property on RequestContext
  • Proto: App state operation/result messages in function_executor.proto + regenerated stubs
  • FE allocation runner: AllocationAppState class, wired into AllocationRunner and AllocationStateWrapper
  • HTTP handlers: Base, function-executor, and local handlers for prepare_read/prepare_write/commit_write
  • HTTP client: ApplicationStateHTTPClient wired into RequestContextHTTPClient

Note: Requires corresponding indexify repo changes (proto + Rust server/dataplane) for remote mode to work end-to-end.

Test plan

  • All existing request state tests pass (no regressions)
  • Integration test: set/get within single function
  • Integration test: set in one function, read in downstream function
  • Integration test: two different requests read/write each other's data (same app)
  • Integration test: last-writer-wins overwrite across requests
  • Integration test: default values for missing keys

Some agents require updating context about users or objects they are
managing. Application State is a way for applications to share state
across request boundaries as they learn about the problem domain.

For example, a fitness tracker agent managing fitness logs of multiple
users can store per-user context in app_state as a flexible K/V store,
which is much more practical than using rigid database schemas for
evolving agent knowledge.

The API mirrors the existing request-scoped ctx.state but persists
across all requests within the same application:

  ctx = RequestContext.get()
  ctx.app_state.set("key", value)   # write
  ctx.app_state.get("key")          # read from any request

Implementation uses the same 3-phase blob protocol as request state
(prepare_read -> transfer -> commit_write) with a different URI
namespace: {namespace}/{application}/app_state/{key}.

Changes:
- Add ApplicationState abstract class and app_state property on
  RequestContext
- Add AllocationAppState for FE-side operation handling
- Add app state proto messages (operations + results) to
  function_executor.proto and regenerate stubs
- Add HTTP handlers (base, FE, local) for app state operations
- Add ApplicationStateHTTPClient and wire into RequestContextHTTPClient
- Wire app state into allocation runner and state wrapper

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@diptanu diptanu force-pushed the feature/app-state branch from 9f5591a to 8493ad1 Compare March 2, 2026 06:59
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