Repository navigation
Description
Summary
Agents currently run blind to critical operational metadata — they don't know their timeout budget, whether they're in a scheduled or interactive execution, what model they're running on, or who triggered them. This makes it impossible for agents to self-calibrate behavior (e.g., planning work within a timeout, skipping questions in autonomous mode, adjusting reasoning depth for the model).
The platform prompt service should inject a dynamic ## Execution Context block into every agent invocation, populated from the invocation pathway.
Proposed Context Block
## Execution Context
- **Mode**: chat | task
- **Triggered by**: user | schedule | agent | mcp
- **Schedule**: "daily-report" (cron: 0 9 * * *, next: 2026-03-26T09:00:00Z)
- **Model**: claude-sonnet-4-6
- **Timeout**: 900s — plan your work to complete well within this budget
- **Attempt**: 1
- **Agent**: oracle-1
- **Collaborators**: researcher-1, writer-1
- **Timestamp**: 2026-03-25T09:00:00Z
- **Platform**: http://localhost
Fields
Execution mode (chat vs task)
Agent doesn't know if it's in an interactive session or autonomous task execution. This fundamentally changes behavior — in chat mode, asking clarifying questions is fine; in task mode, the agent should execute and return results without blocking on input.
Behavioral guidance to include:
chat: "Interactive session. You may ask clarifying questions."task: "Autonomous execution. Do not ask questions — execute to completion and return results."
Triggered by
Who/what initiated this execution. Affects tone, urgency, and error handling strategy.
user— human-initiated via UI or APIschedule— cron-triggered autonomous executionagent— delegated from another agent (include source agent name)mcp— triggered via MCP tool call (include MCP key name)
Schedule context (conditional — only for scheduled executions)
For recurring tasks, the agent should know:
- Schedule name (e.g., "daily-report")
- Cron expression (e.g.,
0 9 * * *) - Next scheduled run (so agent can say "I'll check again tomorrow")
- Last execution result summary (succeeded/failed) if available
Model in use
The actual model being used for this execution. Agents can self-calibrate:
- Opus: tackle complex multi-step reasoning
- Sonnet: balanced approach
- Haiku: keep it simple and fast
Timeout awareness
The timeout is already enforced as a CLI kill, but the agent doesn't know its time budget. Injecting it into the prompt lets the agent:
- Prioritize critical work first
- Skip optional steps if budget is tight
- Avoid starting long operations near the deadline
Attempt number
If a schedule or task is retried, the agent should know it's attempt #2 (or N). This lets it adjust strategy — e.g., try a different approach on retry, or report "failed again" with accumulated context.
Agent's own name
Currently only available via AGENT_NAME env var. Including it in the prompt makes self-identification natural in responses and logs.
Collaborators
Names of agents this agent has permission to communicate with. Just names, no details. Lets the agent know who's available without needing to call list_agents first.
Timestamp
Current UTC timestamp at execution start. Agents often need to reason about time (e.g., "it's 2am, this is the overnight batch").
Platform URL
Base URL of the Trinity instance. Lets agents generate links back to the UI (e.g., "see results at http://localhost/agents/oracle-1").
Remaining context window (stretch)
If feasible, include the model's context window size. Lets agents self-manage conversation length in long chat sessions.
Parent execution chain (stretch)
For delegation chains: orchestrator-1 → researcher-1 → you. Helps agents understand their role in a larger workflow.
Execution history (stretch — scheduled tasks only)
Summary of last N executions: "Last run: succeeded at 2026-03-25T09:00:00Z (45s)". Lets scheduled agents reason about trends and previous failures.
Implementation
Where
src/backend/services/platform_prompt_service.py— addbuild_execution_context()functionsrc/backend/routers/chat.py— pass execution metadata when building system promptsrc/backend/services/task_execution_service.py— pass execution metadata for taskssrc/scheduler/service.py— pass schedule metadata for scheduled executions
How
- Add
build_execution_context(**kwargs) -> strto platform prompt service - Each invocation pathway (chat, task, schedule) populates the relevant kwargs
- Context block is appended to the system prompt alongside existing platform instructions
- Fields that don't apply to the current mode are omitted (e.g., no schedule block for chat)
Agent-side changes
- No changes needed to
agent-server.py— context arrives via the existingsystem_promptfield - The
ParallelTaskRequestandChatRequestmodels are unchanged
Acceptance Criteria
- Chat executions include: mode=chat, triggered_by, model, agent name, collaborators, timestamp, platform URL
- Task executions include: mode=task, triggered_by, model, timeout, agent name, collaborators, timestamp, platform URL
- Scheduled executions include: all task fields + schedule name, cron, next run, attempt number
- Agent-delegated executions include source agent name in triggered_by
- MCP-triggered executions include MCP key name in triggered_by
- Fields that don't apply are omitted (no schedule block in chat mode, no timeout in chat mode)
- Behavioral guidance is included for each mode (interactive vs autonomous)
- Collaborators list populated from agent_permissions
- Stretch: execution history for scheduled tasks
- Stretch: parent execution chain for delegated tasks
Activity
feat(prompt): inject execution context into agent system prompt (#171)
feat(prompt): inject execution context into agent system prompt (#171)
feat(prompt): inject execution context into agent system prompt (#171)
Merge pull request #325 from Abilityai/feature/171-execution-context