ag-agent 0.18.3

Agentty is an ADE (Agentic Development Environment) for structured, controllable AI-assisted software development.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! In-process [`AgentChannel`] adapter backed by durable `ag-harness`
//! sessions.
//!
//! The harness session shares the Agentty session id and is canonical for
//! model context. A session without harness history restarts from Agentty's
//! replay transcript. Session turns return a versioned instruction key and
//! the digest of the turn input that carried the full contract as their
//! provider conversation id. A follow-up session turn sends only the refresh
//! reminder while the host passes that current id back and the harness still
//! replays that input; history eviction re-sends the full contract.

use std::collections::{BTreeMap, HashMap};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::path::Path;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};

use ag_contracts::{
    AgentChannel, AgentError, AgentFuture, ReasoningLevel, SessionRef, StartSessionRequest,
    TurnEvent, TurnRequest, TurnResult,
};
use ag_harness::model::ModelMessage;
use ag_harness::store::{HistoryTurn, LoadedSession, SessionStore, SqliteStore};
use ag_harness::{Harness, Repository, Session, SessionError, TurnControl};
use ag_protocol::ProtocolRequestProfile;
use ag_session::AgentKind;
use tokio::sync::mpsc;

use crate::agent::native::{
    ActivityBridge, HarnessContext, ModelSelection, NativeHarnessConfig, NativePrompt, failure,
    output_schema, parse_outcome, reject_attachments, repository, running_context_window, settle,
    system_prompt, token_usage, turn_options,
};
use crate::agent::{
    InstructionDeliveryMode, apply_response_style_prompt, execution_policy,
    instruction_bootstrap_key, plan_instruction_delivery,
};

/// Started turns whose settlement a shutdown or the session's next turn must
/// still observe, by session.
type ActiveTurns = Arc<Mutex<HashMap<String, TurnControl>>>;

/// [`AgentChannel`] adapter that runs harness turns in process.
pub(crate) struct NativeAgentChannel {
    active: ActiveTurns,
    config: NativeHarnessConfig,
}

impl NativeAgentChannel {
    /// Creates a channel storing session history under `config`.
    pub(crate) fn new(config: NativeHarnessConfig) -> Self {
        Self {
            active: Arc::default(),
            config,
        }
    }
}

impl AgentChannel for NativeAgentChannel {
    /// Returns immediately; the harness session opens on the first turn.
    fn start_session(
        &self,
        req: StartSessionRequest,
    ) -> AgentFuture<Result<SessionRef, AgentError>> {
        let session_id = req.session_id;

        Box::pin(async move { Ok(SessionRef { session_id }) })
    }

    /// Runs one durable harness turn.
    ///
    /// Dropping the future cancels the turn; [`Self::shutdown_session`] then
    /// waits for its cleanup.
    ///
    /// # Errors
    /// Returns [`AgentError::Backend`] for unsupported controls,
    /// configuration, provider, settlement, or protocol failures.
    fn run_turn(
        &self,
        session_id: String,
        req: TurnRequest,
        events: mpsc::UnboundedSender<TurnEvent>,
    ) -> AgentFuture<Result<TurnResult, AgentError>> {
        let active = Arc::clone(&self.active);
        let config = self.config.clone();

        Box::pin(async move {
            run_session_turn(&config, &active, session_id, req, events)
                .await
                .map_err(AgentError::Backend)
        })
    }

    /// Cancels the session's started turn and waits for its cleanup.
    ///
    /// A failed or abandoned cleanup keeps the turn registered so the
    /// session's next turn retries it.
    fn shutdown_session(&self, session_id: String) -> AgentFuture<Result<(), AgentError>> {
        let active = Arc::clone(&self.active);

        Box::pin(async move {
            let control = lock(&active).get(&session_id).cloned();
            if let Some(control) = control {
                control.cancel();
            }

            settle_previous_turn(&active, &session_id)
                .await
                .map_err(AgentError::Runtime)
        })
    }
}

/// Opens the durable session, runs one turn, and waits for it to settle.
async fn run_session_turn(
    config: &NativeHarnessConfig,
    active: &ActiveTurns,
    session_id: String,
    req: TurnRequest,
    events: mpsc::UnboundedSender<TurnEvent>,
) -> Result<TurnResult, String> {
    execution_policy::validate(AgentKind::Harness, &req.execution_policy)
        .map_err(|error| error.to_string())?;
    let environment = config.environment();
    let selection = ModelSelection::resolve(&req.model, &environment)?;
    let profile = req.request_kind.protocol_profile();
    let options = turn_options(profile, req.permission_mode, &environment)?;
    let repository = repository(&req.folder, &environment)?;
    reject_attachments(&req.prompt.attachments)?;
    let prompt_text = apply_response_style_prompt(req.prompt.clone(), profile, req.response_style)
        .map_err(failure("Failed to apply the response style"))?
        .agent_text();
    let continuation = req.continuation.into_parts();
    let replay_transcript = continuation.replay_transcript.or_else(|| {
        continuation
            .live_transcript
            .and_then(|transcript| transcript.replay_text())
    });
    let bootstrap = BootstrapMarker::parse(continuation.provider_conversation_id.as_deref());
    settle_previous_turn(active, &session_id).await?;
    let running_context_window = running_context_window(&selection, &environment).await?;
    let database = config.session_database(&session_id).await?;
    let opener = SessionOpener {
        database: &database,
        environment: &environment,
        events: &events,
        profile,
        provider_call_budget: req.provider_call_budget.as_ref(),
        reasoning_level: req.reasoning_level,
        repository: &repository,
        running_context_window,
        session_id: &session_id,
    };
    let (mut session, history) = opener.open(&selection).await?;
    // Turns since the last full contract, counting its own turn; `None` once
    // the harness no longer loads it.
    let bootstrap_age = bootstrap
        .as_ref()
        .and_then(|bootstrap| history.age_of(bootstrap.input_digest));
    let delivery = plan_instruction_delivery(
        &req.request_kind,
        bootstrap_age.is_some().then_some(session_id.as_str()),
        bootstrap.as_ref().map(|bootstrap| bootstrap.key.as_str()),
        !history.has_finished_turn && replay_transcript.is_some(),
    );
    let input = NativePrompt {
        delivery,
        agent_profile_prompt: req.agent_profile.current(),
        agent_profile_update: req.agent_profile.update(),
        prompt: &prompt_text,
        protocol_profile: profile,
        replay_transcript: replay_transcript.as_deref(),
        workspace_root: &req.folder,
    }
    .render()?;
    let input_digest = input_digest(&input);
    let turn = session.turn(input).options(options).start();
    let control = turn.control();
    lock(active).insert(session_id.clone(), control.clone());
    let outcome = turn.await;
    // A failed settlement stays registered so the next turn retries it.
    settle(&control).await?;
    lock(active).remove(&session_id);
    let outcome = outcome.map_err(|error| format!("Harness turn failed: {error}"))?;
    let assistant_message = parse_outcome(&outcome, profile)?;
    let (input_tokens, output_tokens) = token_usage(outcome.report());
    // An unversioned id makes the next session turn re-send the session
    // contract: a utility or review turn bootstrapped its own, or the
    // context budget dropped the turn that carried it from this request.
    let bootstrap_replayed =
        bootstrap_age.is_some_and(|age| outcome.report().history().replayed_turns() >= age);
    let provider_conversation_id = if profile != ProtocolRequestProfile::SessionTurn {
        Some(session_id)
    } else if delivery != InstructionDeliveryMode::DeltaOnly {
        BootstrapMarker::encode(&session_id, input_digest)
    } else if bootstrap_replayed {
        continuation.provider_conversation_id
    } else {
        Some(session_id)
    };

    Ok(TurnResult {
        assistant_message,
        context_reset: false,
        harness_report: serde_json::to_string(outcome.report()).ok(),
        input_tokens,
        output_tokens,
        provider_conversation_id,
    })
}

/// Versioned instruction key and input digest of the session turn that last
/// carried the full contract, encoded in its provider conversation id.
struct BootstrapMarker {
    input_digest: u64,
    key: String,
}

impl BootstrapMarker {
    /// Separates the instruction key from the hexadecimal input digest.
    const DIGEST_SEPARATOR: char = '#';

    /// Parses a stored provider conversation id; ids without a digest, such
    /// as unversioned or earlier keys, yield `None`.
    fn parse(provider_conversation_id: Option<&str>) -> Option<Self> {
        let (key, digest) = provider_conversation_id?.rsplit_once(Self::DIGEST_SEPARATOR)?;

        Some(Self {
            input_digest: u64::from_str_radix(digest, 16).ok()?,
            key: key.to_string(),
        })
    }

    /// Encodes the provider conversation id for a turn whose input digest is
    /// `input_digest` and which carried the full contract.
    fn encode(session_id: &str, input_digest: u64) -> Option<String> {
        instruction_bootstrap_key(Some(session_id))
            .map(|key| format!("{key}{}{input_digest:016x}", Self::DIGEST_SEPARATOR))
    }
}

/// Settles the session's previous turn when its cleanup failed or its future
/// was dropped, so unresolved commands stop blocking admission.
///
/// # Errors
/// Returns an error when that cleanup still cannot be acknowledged.
async fn settle_previous_turn(active: &ActiveTurns, session_id: &str) -> Result<(), String> {
    let control = lock(active).get(session_id).cloned();
    if let Some(control) = control {
        settle(&control).await?;
        lock(active).remove(session_id);
    }

    Ok(())
}

/// Locks the started-turn registry; its map stays valid after a panic.
fn lock(active: &ActiveTurns) -> MutexGuard<'_, HashMap<String, TurnControl>> {
    active.lock().unwrap_or_else(PoisonError::into_inner)
}

/// Inputs needed to create, resume, or switch one durable harness session.
struct SessionOpener<'a> {
    database: &'a Path,
    environment: &'a BTreeMap<String, String>,
    events: &'a mpsc::UnboundedSender<TurnEvent>,
    profile: ProtocolRequestProfile,
    provider_call_budget: Option<&'a ag_contracts::ProviderCallBudget>,
    reasoning_level: ReasoningLevel,
    repository: &'a Repository,
    running_context_window: Option<u64>,
    session_id: &'a str,
}

impl SessionOpener<'_> {
    /// Resumes the stored session on its recorded model and switches to
    /// `selection` when it differs, or creates the session when it does not
    /// exist. Returns the session with the history the harness loads for it.
    async fn open(&self, selection: &ModelSelection) -> Result<(Session, LoadedHistory), String> {
        let loaded = match self.load().await {
            Ok(loaded) => loaded,
            Err(SessionError::NotFound { .. }) => {
                let session = self
                    .harness(selection)?
                    .session(self.session_id, output_schema(self.profile)?)
                    .system_prompt(system_prompt())
                    .create()
                    .await
                    .map_err(failure("Failed to create the harness session"))?;

                return Ok((session, LoadedHistory::default()));
            }
            Err(error) => return Err(failure("Failed to load the harness session")(error)),
        };
        let history = LoadedHistory::new(loaded.latest_finished_turn.is_some(), &loaded.turns);
        let stored_selection = loaded
            .registration_identity
            .as_ref()
            .and_then(|identity| ModelSelection::from_key(identity.key()));
        let session = match stored_selection {
            Some(stored_selection) if stored_selection != *selection => {
                self.switch(&stored_selection, selection).await?
            }
            _ => self.resume(self.harness(selection)?).await?,
        };

        Ok((session, history))
    }

    /// Loads the stored session's model identity and finished-turn state.
    async fn load(&self) -> Result<LoadedSession, SessionError> {
        SqliteStore::open(self.database)
            .await?
            .load_session(self.session_id)
            .await
    }

    /// Resumes the session recorded on `stored_selection` and switches it to
    /// `selection`, needing only the target provider's credentials.
    async fn switch(
        &self,
        stored_selection: &ModelSelection,
        selection: &ModelSelection,
    ) -> Result<Session, String> {
        let registry = selection.registry(
            self.environment,
            self.provider_call_budget,
            self.running_context_window,
        )?;
        let harness = self.build_harness(|context, observer| {
            stored_selection.resume_only_harness(context, observer)
        })?;
        let mut session = self.resume(harness).await?;
        session
            .switch_model(&registry, &selection.key())
            .await
            .map_err(failure("Failed to switch the harness model"))?;

        Ok(session)
    }

    /// Resumes the session with `harness`.
    async fn resume(&self, harness: Harness) -> Result<Session, String> {
        harness
            .resume(self.session_id)
            .await
            .map_err(failure("Failed to resume the harness session"))
    }

    /// Builds a harness for `selection` storing history in the session
    /// database.
    fn harness(&self, selection: &ModelSelection) -> Result<Harness, String> {
        self.build_harness(|context, observer| selection.harness(context, observer))
    }

    /// Builds a harness with this turn's context and activity forwarding,
    /// storing history in the session database.
    fn build_harness(
        &self,
        build: impl FnOnce(&HarnessContext<'_>, ActivityBridge) -> Result<Harness, String>,
    ) -> Result<Harness, String> {
        let events = self.events.clone();
        let harness = build(
            &HarnessContext {
                environment: self.environment,
                provider_call_budget: self.provider_call_budget,
                running_context_window: self.running_context_window,
                reasoning_level: self.reasoning_level,
                repository: self.repository,
            },
            ActivityBridge::new(move |event| {
                let _ = events.send(event);
            }),
        )?;

        Ok(harness.database(self.database))
    }
}

/// Finished-turn state of the history the harness loads for a turn.
#[derive(Default)]
struct LoadedHistory {
    /// Whether the session holds any finished turn, loaded or not.
    has_finished_turn: bool,
    /// Input digests of the loaded turns, oldest first.
    input_digests: Vec<Option<u64>>,
}

impl LoadedHistory {
    /// Summarizes the turns the store loads within the session's byte
    /// budget, oldest first with their positions.
    fn new(has_finished_turn: bool, turns: &[(i64, HistoryTurn)]) -> Self {
        let input_digests = turns
            .iter()
            .map(|(_, turn)| match turn.messages.first() {
                Some(ModelMessage::User(text)) => Some(input_digest(text)),
                _ => None,
            })
            .collect();

        Self {
            has_finished_turn,
            input_digests,
        }
    }

    /// Returns how many loaded turns, counted from the newest, reach the
    /// newest turn whose input has `digest`.
    fn age_of(&self, digest: u64) -> Option<usize> {
        self.input_digests
            .iter()
            .rev()
            .position(|input_digest| *input_digest == Some(digest))
            .map(|position| position + 1)
    }
}

/// Fingerprints one turn's rendered input text.
fn input_digest(input: &str) -> u64 {
    let mut hasher = DefaultHasher::new();
    input.hash(&mut hasher);

    hasher.finish()
}

#[cfg(test)]
#[path = "native_test.rs"]
mod tests;