agentty 0.9.1

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
//! Codex account and rate-limit usage collection.

use std::path::Path;

use serde_json::Value;

use super::lifecycle;
use super::transport::{CodexRuntimeTransport, CodexStdioTransport};
use crate::domain::agent::{AgentKind, ReasoningLevel};
use crate::domain::agent_usage::{AgentRateLimit, AgentUsageDetails};
use crate::infra::app_server::AppServerError;
use crate::infra::{agent, app_server_transport};

/// Loads `ChatGPT` account plan and current `Codex` rate-limit buckets from
/// one temporary `codex app-server` runtime.
pub(crate) async fn load_codex_account_usage(
    folder: &Path,
    model: &str,
) -> Result<AgentUsageDetails, AppServerError> {
    let request_kind = crate::infra::channel::AgentRequestKind::AccountRead;
    let command = agent::create_backend(AgentKind::Codex)
        .build_command(agent::BuildCommandRequest {
            attachments: &[],
            folder,
            prompt: "",
            request_kind: &request_kind,
            model,
            reasoning_level: ReasoningLevel::default(),
        })
        .map_err(|error| {
            AppServerError::Provider(format!(
                "Failed to build `codex app-server` account usage command: {error}"
            ))
        })?;
    let (mut child, stdin, stdout) =
        app_server_transport::spawn_runtime_command(command, "codex app-server")?;
    let mut transport = CodexStdioTransport::new(stdin, stdout);

    let usage_result = load_codex_account_usage_with_transport(&mut transport).await;
    transport.close_stdin();
    app_server_transport::shutdown_child(&mut child).await;

    usage_result
}

/// Loads Codex account usage through an initialized app-server transport.
pub(super) async fn load_codex_account_usage_with_transport<Transport: CodexRuntimeTransport>(
    transport: &mut Transport,
) -> Result<AgentUsageDetails, AppServerError> {
    lifecycle::initialize_runtime(transport).await?;
    let account_response =
        send_account_request(transport, "account/read", "codex-account-read").await?;
    let rate_limits_response = send_account_request(
        transport,
        "account/rateLimits/read",
        "codex-rate-limits-read",
    )
    .await?;

    Ok(AgentUsageDetails {
        plan: extract_plan_type(&account_response),
        rate_limits: extract_rate_limits(&rate_limits_response),
        reached_type: extract_reached_type(&rate_limits_response),
    })
}

/// Sends one account-scoped JSON-RPC request and returns its parsed response.
async fn send_account_request<Transport: CodexRuntimeTransport>(
    transport: &mut Transport,
    method: &str,
    id_prefix: &str,
) -> Result<Value, AppServerError> {
    let request_id = format!("{id_prefix}-{}", uuid::Uuid::new_v4());
    let payload = serde_json::json!({
        "method": method,
        "id": request_id,
        "params": {}
    });
    transport.write_json_line(payload).await?;
    let response_line = transport.wait_for_response_line(request_id).await?;
    let response_value = serde_json::from_str::<Value>(&response_line).map_err(|error| {
        AppServerError::Provider(format!(
            "Failed to parse Codex account usage response: {error}"
        ))
    })?;

    if let Some(error_message) = app_server_transport::extract_json_error_message(&response_value) {
        return Err(AppServerError::Provider(format!(
            "Codex account usage request `{method}` failed: {error_message}"
        )));
    }

    Ok(response_value)
}

/// Extracts the `ChatGPT` plan type from an `account/read` response.
fn extract_plan_type(response_value: &Value) -> Option<String> {
    response_value
        .get("result")
        .and_then(|result| result.get("account"))
        .and_then(|account| account.get("planType").or_else(|| account.get("plan_type")))
        .and_then(Value::as_str)
        .map(ToString::to_string)
}

/// Extracts Codex rate-limit buckets from an `account/rateLimits/read`
/// response.
fn extract_rate_limits(response_value: &Value) -> Vec<AgentRateLimit> {
    let Some(rate_limits) = response_value.get("result").and_then(|result| {
        result
            .get("rateLimits")
            .or_else(|| result.get("rate_limits"))
    }) else {
        return Vec::new();
    };

    ["primary", "secondary"]
        .into_iter()
        .filter_map(|bucket_name| {
            rate_limits
                .get(bucket_name)
                .filter(|bucket| !bucket.is_null())
                .map(|bucket| extract_rate_limit(bucket_name, bucket))
        })
        .collect()
}

/// Extracts one named Codex rate-limit bucket.
fn extract_rate_limit(bucket_name: &str, bucket: &Value) -> AgentRateLimit {
    AgentRateLimit {
        label: title_case_ascii(bucket_name),
        used_percent: bucket
            .get("usedPercent")
            .or_else(|| bucket.get("used_percent"))
            .and_then(format_percent_value),
        resets_at_unix_seconds: bucket
            .get("resetsAt")
            .or_else(|| bucket.get("resets_at"))
            .and_then(Value::as_i64),
        window_duration_mins: bucket
            .get("windowDurationMins")
            .or_else(|| bucket.get("window_duration_mins"))
            .and_then(Value::as_u64),
    }
}

/// Extracts the backend-classified reached-limit type, when present.
fn extract_reached_type(response_value: &Value) -> Option<String> {
    response_value
        .get("result")
        .and_then(|result| {
            result
                .get("rateLimits")
                .or_else(|| result.get("rate_limits"))
        })
        .and_then(|rate_limits| {
            rate_limits
                .get("rateLimitReachedType")
                .or_else(|| rate_limits.get("rate_limit_reached_type"))
        })
        .and_then(Value::as_str)
        .map(ToString::to_string)
}

/// Formats one JSON numeric percentage without adding noisy trailing zeros.
fn format_percent_value(value: &Value) -> Option<String> {
    if let Some(percent) = value.as_u64() {
        return Some(format!("{percent}%"));
    }

    let percent = value.as_f64()?;
    if percent.fract() == 0.0 {
        return Some(format!("{percent:.0}%"));
    }

    Some(format!("{percent:.1}%"))
}

/// Converts a known ASCII bucket identifier into a title label.
fn title_case_ascii(value: &str) -> String {
    let mut characters = value.chars();
    let Some(first) = characters.next() else {
        return String::new();
    };

    format!(
        "{}{}",
        first.to_ascii_uppercase(),
        characters.as_str().to_ascii_lowercase()
    )
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use mockall::Sequence;

    use super::*;
    use crate::infra::agent::app_server::codex::MockCodexRuntimeTransport;

    /// Stores the request id from one JSON-RPC write for a later response.
    fn remember_request_id(id_store: &Arc<Mutex<Option<String>>>, payload: &Value) {
        let id = payload
            .get("id")
            .and_then(Value::as_str)
            .map(ToString::to_string);
        if let Ok(mut guard) = id_store.lock() {
            *guard = id;
        }
    }

    /// Expects the `initialize` and `initialized` messages that prepare the
    /// temporary `Codex` app-server runtime.
    fn expect_initialize_handshake(
        transport: &mut MockCodexRuntimeTransport,
        sequence: &mut Sequence,
        initialize_id: &Arc<Mutex<Option<String>>>,
    ) {
        transport
            .expect_write_json_line()
            .times(1)
            .in_sequence(sequence)
            .withf({
                let initialize_id = Arc::clone(initialize_id);

                move |payload| {
                    remember_request_id(&initialize_id, payload);

                    payload.get("method").and_then(Value::as_str) == Some("initialize")
                }
            })
            .returning(|_| Box::pin(async { Ok(()) }));
        transport
            .expect_wait_for_response_line()
            .times(1)
            .in_sequence(sequence)
            .returning({
                let initialize_id = Arc::clone(initialize_id);

                move |response_id| {
                    let expected_id = initialize_id
                        .lock()
                        .expect("initialize id mutex should not be poisoned")
                        .clone()
                        .expect("initialize id should be captured");
                    assert_eq!(response_id, expected_id);

                    Box::pin(
                        async move { Ok(format!(r#"{{"id":"{response_id}","result":{{}}}}"#)) },
                    )
                }
            });
        transport
            .expect_write_json_line()
            .times(1)
            .in_sequence(sequence)
            .withf(|payload| payload.get("method").and_then(Value::as_str) == Some("initialized"))
            .returning(|_| Box::pin(async { Ok(()) }));
    }

    /// Expects the `account/read` request and returns a `ChatGPT` plan
    /// response.
    fn expect_account_read(
        transport: &mut MockCodexRuntimeTransport,
        sequence: &mut Sequence,
        account_id: &Arc<Mutex<Option<String>>>,
    ) {
        transport
            .expect_write_json_line()
            .times(1)
            .in_sequence(sequence)
            .withf({
                let account_id = Arc::clone(account_id);

                move |payload| {
                    remember_request_id(&account_id, payload);

                    payload.get("method").and_then(Value::as_str) == Some("account/read")
                }
            })
            .returning(|_| Box::pin(async { Ok(()) }));
        transport
            .expect_wait_for_response_line()
            .times(1)
            .in_sequence(sequence)
            .returning({
                let account_id = Arc::clone(account_id);

                move |response_id| {
                    let expected_id = account_id
                        .lock()
                        .expect("account id mutex should not be poisoned")
                        .clone()
                        .expect("account id should be captured");
                    assert_eq!(response_id, expected_id);

                    Box::pin(async move {
                        Ok(format!(
                            r#"{{"id":"{response_id}","result":{{"account":{{"type":"chatgpt","planType":"pro"}}}}}}"#
                        ))
                    })
                }
            });
    }

    /// Expects the `account/rateLimits/read` request and returns one primary
    /// rate-limit bucket.
    fn expect_rate_limits_read(
        transport: &mut MockCodexRuntimeTransport,
        sequence: &mut Sequence,
        rate_limits_id: &Arc<Mutex<Option<String>>>,
    ) {
        transport
            .expect_write_json_line()
            .times(1)
            .in_sequence(sequence)
            .withf({
                let rate_limits_id = Arc::clone(rate_limits_id);

                move |payload| {
                    remember_request_id(&rate_limits_id, payload);

                    payload.get("method").and_then(Value::as_str) == Some("account/rateLimits/read")
                }
            })
            .returning(|_| Box::pin(async { Ok(()) }));
        transport
            .expect_wait_for_response_line()
            .times(1)
            .in_sequence(sequence)
            .returning({
                let rate_limits_id = Arc::clone(rate_limits_id);

                move |response_id| {
                    let expected_id = rate_limits_id
                        .lock()
                        .expect("rate-limit id mutex should not be poisoned")
                        .clone()
                        .expect("rate-limit id should be captured");
                    assert_eq!(response_id, expected_id);

                    Box::pin(async move {
                        Ok(format!(
                            r#"{{"id":"{response_id}","result":{{"rateLimits":{{"primary":{{"usedPercent":25,"windowDurationMins":15,"resetsAt":1730947200}},"secondary":null,"rateLimitReachedType":null}}}}}}"#
                        ))
                    })
                }
            });
    }

    #[tokio::test]
    async fn load_codex_account_usage_with_transport_reads_plan_and_rate_limits() {
        // Arrange
        let initialize_id = Arc::new(Mutex::new(None));
        let account_id = Arc::new(Mutex::new(None));
        let rate_limits_id = Arc::new(Mutex::new(None));
        let mut sequence = Sequence::new();
        let mut transport = MockCodexRuntimeTransport::new();
        expect_initialize_handshake(&mut transport, &mut sequence, &initialize_id);
        expect_account_read(&mut transport, &mut sequence, &account_id);
        expect_rate_limits_read(&mut transport, &mut sequence, &rate_limits_id);

        // Act
        let usage = load_codex_account_usage_with_transport(&mut transport)
            .await
            .expect("usage should load");

        // Assert
        assert_eq!(usage.plan, Some("pro".to_string()));
        assert_eq!(
            usage.rate_limits,
            vec![AgentRateLimit {
                label: "Primary".to_string(),
                used_percent: Some("25%".to_string()),
                resets_at_unix_seconds: Some(1_730_947_200),
                window_duration_mins: Some(15),
            }]
        );
        assert_eq!(usage.reached_type, None);
    }

    #[test]
    fn extract_rate_limits_reads_snake_case_response_fields() {
        // Arrange
        let response_value = serde_json::json!({
            "result": {
                "rate_limits": {
                    "primary": {
                        "used_percent": 12.5,
                        "window_duration_mins": 300,
                        "resets_at": 1_730_947_200
                    },
                    "rate_limit_reached_type": "primary"
                }
            }
        });

        // Act
        let rate_limits = extract_rate_limits(&response_value);
        let reached_type = extract_reached_type(&response_value);

        // Assert
        assert_eq!(rate_limits[0].used_percent, Some("12.5%".to_string()));
        assert_eq!(rate_limits[0].window_duration_mins, Some(300));
        assert_eq!(reached_type, Some("primary".to_string()));
    }
}