4.5 KiB
Flat File Context Framework Design (Agent-Optimized)
Purpose
Design a compact, machine-first flat-file framework for ALwrity AI agents.
This framework is optimized for:
- deterministic structure,
- minimal token footprint,
- fast parsing,
- high-signal retrieval,
- robust fallback behavior.
Core Principles
-
Agent-first, not human-first
- Keys are short and stable.
- Avoid verbose prose in payloads.
- Include only fields needed for reasoning and tool actions.
-
Compact + predictable schema
- Fixed top-level keys in strict order.
- Canonical value types (no shape drift).
- Avoid polymorphic fields when possible.
-
Dual-layer context
d(full normalized data for deep reasoning).s(summary/high-signal fast path for most agent reads).
-
Fallback-safe design
- Every context doc includes source + freshness metadata.
- If missing/stale, consumers fall back to DB then SIF semantic.
-
Multi-tenant isolation
- Per-user file under
workspace/workspace_<safe_user_id>/agent_context/.
- Per-user file under
Canonical Context Envelope (compact)
{
"v": "1.0",
"t": "onboarding.step2.website_analysis",
"u": "<user_id>",
"ts": "<iso8601>",
"src": "onboarding_step2",
"d": {},
"s": {},
"m": {
"db": 0,
"sb": 0,
"q": []
}
}
Field map
v: schema versiont: context typeu: user idts: updated timestampsrc: source writerd: canonical normalized datas: high-signal summary for quick agent usem: meta (db=data bytes,sb=summary bytes,q=query hints)
Agent Readability Best Practices
- Prefer enums/controlled vocab over free text.
- Use compact keys and arrays for repetitive entities.
- Truncate long textual blobs unless explicitly required.
- Keep “quick facts” flattened.
- Separate operational metadata from semantic content.
- Include retrieval hints (
q) for consistent query drafting.
Write Pipeline Pattern
- Normalize incoming source payload.
- Derive compact summary (
s) from normalized data. - Compute lightweight metadata (
m). - Atomic write JSON file.
- Emit writer version + timestamp.
Read Pipeline Pattern
- Attempt flat-file load.
- Validate minimum envelope fields (
v,t,u,ts,d). - Prefer
sfor quick tasks; usedfor deeper reasoning. - If invalid/missing/stale: fallback DB -> SIF semantic.
Scope Expansion Pattern
Apply same envelope for:
- Step 2: website analysis
- Step 3: research preferences + competitor snapshots
- Step 4: persona profile + platform personas
Only t, d, and s payload contracts should vary.
Governance
- Schema changes require version bump (
v). - Backward compatibility policy: readers support N and N-1.
- Drift checks should compare canonical hash/checksum vs DB latest row.
Document Context + End-User Journey Metadata
Each context file should carry explicit machine-oriented document metadata so agents understand what this file is before reading full payloads.
Suggested document_context fields:
audience:ai_agentspurpose:fast_context_retrievalcontext_type: step-scoped type identifierjourney: stage/action/agent expectationretrieval_contract: preferred source + fallback ordercontext_window_guidance: byte budget and summary-first policy
This block is intentionally compact and deterministic to reduce wasted token usage for agent planning.
Context Window and Length Policy
- Keep combined
data + summaryunder a defined byte budget where practical. - Enforce summary-first reads in agent consumers.
- Truncate long textual fields in summaries; keep full text only in
datawhen needed. - Flag oversize docs in metadata so readers can skip low-priority sections.
- Prefer short, stable keys in machine envelopes and avoid natural-language verbosity.
Implemented baseline controls
- Atomic file writes to avoid partial documents.
- Best-effort restricted file permissions (
0600). - Recursive sensitive-key redaction for payload snapshots.
- Payload size budget enforcement with deterministic trimming metadata.
- Internal document linking via
related_documentsand manifest index.
Security and isolation details: docs/flat_file_context/FLAT_FILE_CONTEXT_SECURITY_AND_ISOLATION.md
Step docs: docs/flat_file_context/STEP2_FLAT_FILE_CONTEXT_DESIGN.md, docs/flat_file_context/STEP3_FLAT_FILE_CONTEXT_DESIGN.md, docs/flat_file_context/STEP4_FLAT_FILE_CONTEXT_DESIGN.md, docs/flat_file_context/STEP5_FLAT_FILE_CONTEXT_DESIGN.md