rinne_core/worker/exec.rs
1//! The execution contract: what goes into a worker and what comes back
2//! (`CONTEXT.md` §8).
3
4use std::path::PathBuf;
5
6use serde::{Deserialize, Serialize};
7
8/// The role a node plays in the DAG (`CONTEXT.md` §10). Defined here because
9/// `execute` takes a role; the DAG types in Phase 3 reuse it.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11#[serde(rename_all = "lowercase")]
12pub enum Role {
13 Planner,
14 Generator,
15 Evaluator,
16 Synthesizer,
17 Fixer,
18}
19
20/// The assembled context for one node, shaped per worker family by the context
21/// assembler (`CONTEXT.md` §12).
22///
23/// For a harness worker the assembler writes a thin packet and **pins file
24/// paths** (the worker reads the repo itself). For an API worker it **inlines
25/// file contents** (the model sees only what is sent). Both forms are carried
26/// here; an adapter consumes whichever fits its family.
27#[derive(Debug, Clone, Default, Serialize, Deserialize)]
28pub struct ContextPacket {
29 /// Pinned file paths for harness workers to read themselves.
30 #[serde(default)]
31 pub pinned_paths: Vec<PathBuf>,
32 /// Inlined `(path, contents)` for API workers.
33 #[serde(default)]
34 pub inlined_files: Vec<InlinedFile>,
35 /// Prior node outputs / blackboard digest text relevant to this node.
36 #[serde(default)]
37 pub prior_context: String,
38 /// A critique artifact from a failed evaluator, fed back on loop-back
39 /// (`CONTEXT.md` §10, §11).
40 #[serde(default)]
41 pub critique: Option<String>,
42}
43
44/// A file inlined into an API worker's context.
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct InlinedFile {
47 pub path: PathBuf,
48 pub contents: String,
49}
50
51/// Per-invocation limits and steering (`CONTEXT.md` §10 budgets).
52#[derive(Debug, Clone, Default, Serialize, Deserialize)]
53pub struct Constraints {
54 /// Hard wall-clock timeout for this invocation, if any.
55 pub timeout_secs: Option<u64>,
56 /// Optional session id to continue cheap intra-worker context where the
57 /// underlying tool supports it (`CONTEXT.md` §8).
58 pub session_id: Option<String>,
59 /// Ambient steering text captured from the user mid-run (`CONTEXT.md` §11).
60 pub steer: Option<String>,
61 /// Model the harness should run for this node, if selected. Passed to the
62 /// CLI as e.g. `--model sonnet` / `-m grok-build`.
63 pub model: Option<String>,
64}
65
66/// Everything a worker needs to do one unit of work (`CONTEXT.md` §8).
67#[derive(Debug, Clone)]
68pub struct ExecuteRequest {
69 pub role: Role,
70 pub instruction: String,
71 pub context: ContextPacket,
72 /// The repository / working directory the worker operates in.
73 pub workspace: PathBuf,
74 pub constraints: Constraints,
75}
76
77/// How an execution ended.
78#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79#[serde(rename_all = "kebab-case", tag = "status", content = "detail")]
80pub enum ExecStatus {
81 /// Completed normally.
82 Success,
83 /// The worker ran but reported failure (non-zero exit, error result).
84 Failed(String),
85 /// Exceeded its timeout.
86 TimedOut,
87 /// Cancelled via a cancellation token (`/pause`, budget kill, stuck abort).
88 Cancelled,
89}
90
91impl ExecStatus {
92 pub fn is_success(&self) -> bool {
93 matches!(self, ExecStatus::Success)
94 }
95}
96
97/// Token / time accounting for one invocation (`CONTEXT.md` §8 usage).
98#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
99pub struct Usage {
100 pub prompt_tokens: u64,
101 pub completion_tokens: u64,
102 /// Wall-clock duration of the invocation.
103 pub wall_ms: u64,
104}
105
106impl Usage {
107 pub fn total_tokens(&self) -> u64 {
108 self.prompt_tokens + self.completion_tokens
109 }
110}
111
112/// The normalized result every adapter returns (`CONTEXT.md` §8).
113#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct ExecuteResult {
115 /// The worker's primary textual output.
116 pub result: String,
117 /// A unified diff of file changes, if the worker edited the workspace.
118 #[serde(default)]
119 pub file_diff: Option<String>,
120 /// The raw transcript of the worker's session (for `.rinne/transcripts/`).
121 #[serde(default)]
122 pub transcript: String,
123 pub status: ExecStatus,
124 pub usage: Usage,
125 /// A session id the worker can be resumed with, if it supports continuation.
126 #[serde(default)]
127 pub session_id: Option<String>,
128}