Elevator pitch

Living Memory Engine is a TypeScript context-composition engine for persistent agents. It selects a small working context from memory, live signals, and open plans instead of resending the full conversation. As the conversation grows, the context sent to Qwen stays bounded.

The engine is demonstrated through เชียงใหม่ (Chiang Mai): a city-entity that senses its environment, remembers across sessions, and shows what shaped each answer.

Inspiration

Most chat agents have a context window, not memory. As a conversation grows, developers either truncate earlier messages and lose useful information, or resend the transcript and pay for increasing amounts of irrelevant context.

We wanted memory that behaves less like a log: keep what matters, let weak memories fade, and retrieve only what is useful now.

What it does

Each turn has three stages:

  1. Retrieve — MMR search balances relevance and diversity across episodic memories, stable self-knowledge, open intentions, and a short recent tail.
  2. Respond — only the selected working context is sent to Qwen.
  3. Consolidate — memories form, decay, reinforce, merge, prune, or crystallize into stable self-facets.

The interface makes this process inspectable. Working Context compares the conversation with what Qwen actually receives. The explainability view separates context used in the latest answer from relevant context that remained available but was not selected. A Lab pane exposes the exact prompt.

Chiang Mai also has two runtime senses:

  • Exteroception: live Chiang Mai weather and air quality from Open-Meteo, scored against the entity's learned diurnal normal rather than fixed thresholds.
  • Interoception: model, network, embedding, world-feed, and memory-health signals that help the entity respond honestly when part of its runtime is degraded.

How we built it

The core @nature-labs/living-memory-engine package uses ports and adapters. It accepts storage, chat, and embedding implementations plus clock, random, and policy dependencies. It contains no framework or IO code, so the Vite web app could supply browser adapters without changing the engine.

The default profile uses Qwen Cloud:

  • qwen3.7-plus for chat
  • text-embedding-v4 at 768 dimensions for embeddings

Requests follow this path:

Browser → /api/qwen/v1 → server-side key injection → Alibaba Cloud Model Studio

The key never reaches the browser. The proxy restricts paths and models, caps output tokens, and fixes the embedding dimension to preserve one vector space. Relevant implementation: qwenproxy.ts, vite.config.ts, and config.ts.

Each persona has its own IndexedDB snapshot, memory store, and ambient world log. Semantic self-facet deduplication reinforces reworded versions of the same trait. Model Markdown uses an escape-first allowlist with XSS and unsafe-link tests.

Evaluation — measured, not asserted

engine/eval/run.ts runs the real engine through four deterministic scenarios against a naive full-history baseline:

Scenario Result Injected Engine tokens Full history
Cross-session preference recalled 5 ~42 ~143
Updated preference old and new recalled 2 ~25 ~24
Critical fact among 31 candidates recalled 2 ~23 ~428
Expired memory after ~12 weeks forgotten 0 ~0 ~12

Cross-session recall, selective retrieval from noise, and prune-based forgetting behaved as expected. The updated-preference scenario exposed a current limitation: the engine retrieves both versions because contradiction-aware supersession is not implemented yet.

The offline harness uses a deterministic lexical fake embedder for reproducibility. The live app uses Qwen's semantic text-embedding-v4.

Alibaba Cloud deployment

The production app runs on an Alibaba Cloud Simple Application Server. A zero-dependency Node service serves the build and proxies Qwen requests under systemd, behind nginx and Cloudflare TLS.

The backend host and model path are both on Alibaba Cloud: application hosting uses Simple Application Server, while chat and embeddings use Qwen Cloud / Alibaba Cloud Model Studio. Deployment evidence is in docs/superpowers/hackathon/evidence/.

Challenges we ran into

  • Selection, not storage: decay, reinforcement, retrieval, deduplication, and pruning had to work as one lifecycle.
  • Explainability: we needed to prove what reached the model, not merely claim that context was smaller.
  • Provider safety: Qwen integration had to keep the key server-side and maintain the engine's 768-dimensional vector space.
  • Contradictions: deduplication handles reworded traits, but replacing an outdated fact requires a separate supersession mechanism.

Accomplishments we are proud of

  • A framework-agnostic engine and a live, inspectable demonstration
  • Cross-session recall, prospective memory, environmental sensing, and runtime self-state
  • Reproducible evaluation against a full-history baseline
  • 177 automated tests: 95 engine and 82 web, strict TypeScript, zero type errors
  • Live Qwen Cloud inference and Alibaba Cloud deployment
  • Open-source architecture and deployment documentation

What we learned

Useful memory is not how much an agent stores; it is the quality of the context selected for the current moment. Runtime self-awareness also matters: an entity that knows the state of its memory and senses can behave more consistently. The contradiction result reinforced a third lesson—an honest limitation is more useful than a tuned benchmark that hides it.

Significantly updated during the submission period

This existing project was substantially extended after May 26, 2026:

  • June 2–5: Vite web app, live weather and air sensing, self-state grounding, prospective-memory resolution, and attributed multi-person memory
  • July 20: Qwen Cloud integration, baseline evaluation, memory-lifecycle UI, Working Context visualization, semantic self-facet deduplication, safe Markdown rendering, Alibaba Cloud deployment, and architecture documentation

What's next

  • Run one canonical server-side entity continuously, including while users are away.
  • Add privacy-scoped retrieval for attributed multi-person memory.
  • Add contradiction-aware consolidation so new facts can supersede old ones.

Built with

TypeScript · Vite · vanilla JavaScript · IndexedDB · Qwen Cloud · Alibaba Cloud Model Studio · Open-Meteo · Vitest

Built With

  • alibaba-cloud-model-studio
  • chatgpt
  • claude-code
  • codex
  • gemini
  • indexeddb
  • open-meteo
  • qwen-cloud
  • squish
  • typescript
  • vite
  • vitest
Share this project:

Updates