prodex 0.62.0

OpenAI profile pooling and safe auto-rotate for Codex CLI and Claude Code
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
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};

use super::*;

const RUNTIME_LOG_FUNCTIONS: &[&str] = &[
    "runtime_proxy_log",
    "runtime_proxy_log_to_path",
    "runtime_proxy_structured_log_message",
];

const RUNTIME_MARKER_PREFIXES: &[&str] = &[
    "chain",
    "compact",
    "compat",
    "continuation",
    "first",
    "local",
    "precommit",
    "previous_response",
    "profile",
    "quota",
    "responses",
    "runtime_proxy",
    "selection",
    "stale",
    "state",
    "stream",
    "upstream",
    "websocket",
];

const RUNTIME_DOCTOR_MARKER_GUARD_ALLOWLIST: &[&str] = &[
    "continuation_journal_save_inline",
    "continuation_journal_save_suppressed",
    "local_close",
    "local_connection_closed",
    "precommit_hold",
    "profile_commit",
    "profile_probe_refresh_suppressed",
    "state_save_inline",
    "state_save_suppressed",
    "stream_complete",
    "stream_start",
    "upstream_connect_ok",
    "upstream_connect_start",
    "upstream_stream_end",
    "websocket_reuse_start",
];

#[test]
fn runtime_doctor_marker_registry_covers_runtime_log_markers() {
    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let mut source_files = Vec::new();
    collect_rust_source_files(&manifest_dir.join("src"), &mut source_files);

    let known_markers = RUNTIME_DOCTOR_MARKERS
        .iter()
        .copied()
        .collect::<BTreeSet<_>>();
    let allowlist = RUNTIME_DOCTOR_MARKER_GUARD_ALLOWLIST
        .iter()
        .copied()
        .collect::<BTreeSet<_>>();
    let mut candidates = BTreeMap::<String, BTreeSet<String>>::new();

    for path in source_files {
        if path.ends_with("runtime_doctor.rs")
            || path.components().any(|c| c.as_os_str() == "runtime_doctor")
        {
            continue;
        }
        let Ok(source) = fs::read_to_string(&path) else {
            continue;
        };
        for function_name in RUNTIME_LOG_FUNCTIONS {
            collect_runtime_log_marker_candidates(
                &manifest_dir,
                &path,
                &source,
                function_name,
                &mut candidates,
            );
        }
    }

    let missing = candidates
        .into_iter()
        .filter(|(marker, _)| !known_markers.contains(marker.as_str()))
        .filter(|(marker, _)| !allowlist.contains(marker.as_str()))
        .collect::<BTreeMap<_, _>>();

    if !missing.is_empty() {
        let details = missing
            .iter()
            .map(|(marker, locations)| {
                let sample_locations = locations
                    .iter()
                    .take(5)
                    .cloned()
                    .collect::<Vec<_>>()
                    .join("\n  ");
                format!("{marker}\n  {sample_locations}")
            })
            .collect::<Vec<_>>()
            .join("\n");
        panic!(
            "runtime doctor marker registry missing runtime log marker(s). Add diagnostic markers to RUNTIME_DOCTOR_MARKERS, or classify noisy lifecycle tokens in RUNTIME_DOCTOR_MARKER_GUARD_ALLOWLIST.\n{details}"
        );
    }
}

fn collect_rust_source_files(root: &Path, files: &mut Vec<PathBuf>) {
    let Ok(entries) = fs::read_dir(root) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            collect_rust_source_files(&path, files);
        } else if path.extension().is_some_and(|extension| extension == "rs") {
            files.push(path);
        }
    }
}

fn collect_runtime_log_marker_candidates(
    manifest_dir: &Path,
    path: &Path,
    source: &str,
    function_name: &str,
    candidates: &mut BTreeMap<String, BTreeSet<String>>,
) {
    let mut offset = 0;
    while let Some(relative_position) = source[offset..].find(function_name) {
        let call_start = offset + relative_position;
        offset = call_start + function_name.len();
        if !is_identifier_boundary(source, call_start, function_name.len()) {
            continue;
        }
        let Some((call_end, call_body)) = runtime_log_call_body(source, offset) else {
            continue;
        };
        offset = call_end;
        let line_number = source[..call_start]
            .bytes()
            .filter(|byte| *byte == b'\n')
            .count()
            + 1;
        let display_path = path.strip_prefix(manifest_dir).unwrap_or(path);
        let location = format!("{}:{line_number}", display_path.display());
        let messages = if function_name == "runtime_proxy_structured_log_message" {
            runtime_structured_log_message_events(call_body)
        } else {
            runtime_log_message_format_strings(call_body)
        };
        for message in messages {
            for marker in runtime_log_marker_tokens(&message) {
                candidates
                    .entry(marker)
                    .or_default()
                    .insert(location.clone());
            }
        }
    }
}

fn is_identifier_boundary(source: &str, start: usize, len: usize) -> bool {
    let before = start
        .checked_sub(1)
        .and_then(|index| source.as_bytes().get(index))
        .copied();
    let after = source.as_bytes().get(start + len).copied();
    !before.is_some_and(is_rust_identifier_byte) && !after.is_some_and(is_rust_identifier_byte)
}

fn is_rust_identifier_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || byte == b'_'
}

fn runtime_log_call_body(source: &str, search_from: usize) -> Option<(usize, &str)> {
    let open = source[search_from..].find('(')? + search_from;
    let bytes = source.as_bytes();
    let mut depth = 0usize;
    let mut index = open;
    while index < bytes.len() {
        match bytes[index] {
            b'(' => depth = depth.saturating_add(1),
            b')' => {
                depth = depth.saturating_sub(1);
                if depth == 0 {
                    return Some((index + 1, &source[open + 1..index]));
                }
            }
            b'"' => index = skip_normal_string(bytes, index)?,
            b'\'' => index = skip_char_literal(bytes, index)?,
            b'/' if bytes.get(index + 1) == Some(&b'/') => {
                index = skip_line_comment(bytes, index);
            }
            b'/' if bytes.get(index + 1) == Some(&b'*') => {
                index = skip_block_comment(bytes, index)?;
            }
            b'r' if raw_string_hash_count(bytes, index).is_some() => {
                index = skip_raw_string(bytes, index)?;
            }
            _ => {}
        }
        index += 1;
    }
    None
}

fn runtime_log_message_format_strings(call_body: &str) -> Vec<String> {
    let mut messages = Vec::new();
    let mut offset = 0;
    while let Some(relative_position) = call_body[offset..].find("format!") {
        let format_start = offset + relative_position;
        offset = format_start + "format!".len();
        let Some(open) = call_body[offset..]
            .find('(')
            .map(|position| offset + position)
        else {
            continue;
        };
        let mut string_start = open + 1;
        while call_body
            .as_bytes()
            .get(string_start)
            .is_some_and(|byte| byte.is_ascii_whitespace() || *byte == b'&')
        {
            string_start += 1;
        }
        if let Some((end, message)) = read_string_literal(call_body, string_start) {
            messages.push(message);
            offset = end;
        }
    }
    if !messages.is_empty() {
        return messages;
    }

    let mut index = 0;
    while index < call_body.len() {
        if let Some((_, message)) = read_string_literal(call_body, index) {
            return vec![message];
        }
        index += 1;
    }
    Vec::new()
}

fn runtime_structured_log_message_events(call_body: &str) -> Vec<String> {
    let mut string_start = 0;
    while call_body
        .as_bytes()
        .get(string_start)
        .is_some_and(|byte| byte.is_ascii_whitespace() || *byte == b'&')
    {
        string_start += 1;
    }
    read_string_literal(call_body, string_start)
        .map(|(_, event)| vec![event])
        .unwrap_or_default()
}

#[test]
fn runtime_doctor_marker_guard_reads_structured_log_message_events() {
    assert!(RUNTIME_LOG_FUNCTIONS.contains(&"runtime_proxy_structured_log_message"));

    let manifest_dir = Path::new("/workspace");
    let path = manifest_dir.join("src/runtime_proxy/sample.rs");
    let source = r#"
fn sample(shared: &RuntimeRotationProxyShared) {
    let message = runtime_proxy_structured_log_message(
        "selection_pick",
        [
            runtime_proxy_log_field("route", "responses"),
            runtime_proxy_log_field("profile", "main"),
        ],
    );
    runtime_proxy_log(shared, message);
    runtime_proxy_log(
        shared,
        runtime_proxy_structured_log_message(
            "runtime_proxy_queue_overloaded",
            [
                runtime_proxy_log_field("reason", "queue_full"),
                runtime_proxy_log_field("debug", format!("{:?}", (1, 2))),
            ],
        ),
    );
}
"#;
    let mut candidates = BTreeMap::<String, BTreeSet<String>>::new();

    collect_runtime_log_marker_candidates(
        manifest_dir,
        &path,
        source,
        "runtime_proxy_structured_log_message",
        &mut candidates,
    );

    assert!(candidates.contains_key("selection_pick"));
    assert!(candidates.contains_key("runtime_proxy_queue_overloaded"));
}

fn runtime_log_marker_tokens(message: &str) -> Vec<String> {
    message
        .split_whitespace()
        .filter_map(|token| {
            let token = token.trim_matches(|c: char| {
                matches!(
                    c,
                    '`' | '\'' | '"' | '[' | ']' | '(' | ')' | '{' | '}' | ',' | '.' | ';' | ':'
                )
            });
            if token.contains(['=', '{', '}', '/', '-']) || !runtime_marker_like_token(token) {
                return None;
            }
            Some(token.to_string())
        })
        .collect()
}

fn runtime_marker_like_token(token: &str) -> bool {
    !token.is_empty()
        && token.as_bytes()[0].is_ascii_lowercase()
        && token.contains('_')
        && token
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_')
        && RUNTIME_MARKER_PREFIXES
            .iter()
            .any(|prefix| token.starts_with(&format!("{prefix}_")))
}

fn read_string_literal(source: &str, start: usize) -> Option<(usize, String)> {
    let bytes = source.as_bytes();
    if bytes.get(start) == Some(&b'"') {
        let end = skip_normal_string(bytes, start)?;
        return Some((end + 1, source[start + 1..end].to_string()));
    }
    if bytes.get(start) == Some(&b'r') {
        let hash_count = raw_string_hash_count(bytes, start)?;
        let content_start = start + 2 + hash_count;
        let end = raw_string_end(bytes, content_start, hash_count)?;
        return Some((end + 1 + hash_count, source[content_start..end].to_string()));
    }
    None
}

fn raw_string_hash_count(bytes: &[u8], start: usize) -> Option<usize> {
    if bytes.get(start) != Some(&b'r') {
        return None;
    }
    let mut index = start + 1;
    while bytes.get(index) == Some(&b'#') {
        index += 1;
    }
    (bytes.get(index) == Some(&b'"')).then_some(index - start - 1)
}

fn raw_string_end(bytes: &[u8], content_start: usize, hash_count: usize) -> Option<usize> {
    let mut index = content_start;
    while index < bytes.len() {
        if bytes[index] == b'"'
            && (0..hash_count).all(|offset| bytes.get(index + 1 + offset) == Some(&b'#'))
        {
            return Some(index);
        }
        index += 1;
    }
    None
}

fn skip_raw_string(bytes: &[u8], start: usize) -> Option<usize> {
    let hash_count = raw_string_hash_count(bytes, start)?;
    let content_start = start + 2 + hash_count;
    raw_string_end(bytes, content_start, hash_count).map(|end| end + hash_count)
}

fn skip_normal_string(bytes: &[u8], start: usize) -> Option<usize> {
    let mut index = start + 1;
    while index < bytes.len() {
        match bytes[index] {
            b'\\' => index += 2,
            b'"' => return Some(index),
            _ => index += 1,
        }
    }
    None
}

fn skip_char_literal(bytes: &[u8], start: usize) -> Option<usize> {
    let mut index = start + 1;
    while index < bytes.len() {
        match bytes[index] {
            b'\\' => index += 2,
            b'\'' => return Some(index),
            _ => index += 1,
        }
    }
    None
}

fn skip_line_comment(bytes: &[u8], start: usize) -> usize {
    let mut index = start + 2;
    while index < bytes.len() && bytes[index] != b'\n' {
        index += 1;
    }
    index
}

fn skip_block_comment(bytes: &[u8], start: usize) -> Option<usize> {
    let mut index = start + 2;
    while index + 1 < bytes.len() {
        if bytes[index] == b'*' && bytes[index + 1] == b'/' {
            return Some(index + 1);
        }
        index += 1;
    }
    None
}