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
use std::path::PathBuf;
use clap::{Args, ValueEnum};
use serde::{Deserialize, Serialize};
#[derive(Args, Debug, Clone)]
#[command(
after_help = "Examples:\n tokmd context --budget 128k --mode bundle --output context.txt\n tokmd context crates/tokmd xtask --strategy spread --budget 200k"
)]
pub struct CliContextArgs {
/// Paths to scan (directories, files, or globs). Defaults to "."
#[arg(value_name = "PATH")]
pub paths: Option<Vec<PathBuf>>,
/// Token budget with optional k/m/g suffix, or 'unlimited' (e.g., "128k", "1m", "1g", "unlimited").
#[arg(long, default_value = "128k")]
pub budget: String,
/// Packing strategy.
#[arg(long, value_enum, default_value_t = ContextStrategy::Greedy)]
pub strategy: ContextStrategy,
/// Metric to rank files by.
#[arg(long, value_enum, default_value_t = ValueMetric::Code)]
pub rank_by: ValueMetric,
/// Output mode.
#[arg(long = "mode", value_enum, default_value_t = ContextOutput::List)]
pub output_mode: ContextOutput,
/// Strip blank lines from bundle output.
#[arg(long)]
pub compress: bool,
/// Disable smart exclusion of lockfiles, minified files, and generated artifacts.
#[arg(long)]
pub no_smart_exclude: bool,
/// Module roots (see `tokmd module`).
#[arg(long, value_delimiter = ',')]
pub module_roots: Option<Vec<String>>,
/// Module depth (see `tokmd module`).
#[arg(long, visible_alias = "depth")]
pub module_depth: Option<usize>,
/// Enable git-based ranking (required for churn/hotspot).
#[arg(long)]
pub git: bool,
/// Disable git-based ranking.
#[arg(long = "no-git")]
pub no_git: bool,
/// Maximum commits to scan for git metrics.
#[arg(long, default_value = "1000")]
pub max_commits: usize,
/// Maximum files per commit to process.
#[arg(long, default_value = "100")]
pub max_commit_files: usize,
/// Write output to file instead of stdout.
#[arg(long, value_name = "PATH", visible_alias = "out")]
pub output: Option<PathBuf>,
/// Overwrite existing output file.
#[arg(long)]
pub force: bool,
/// Write bundle to directory with manifest (for large outputs).
#[arg(long, value_name = "DIR", conflicts_with = "output")]
pub bundle_dir: Option<PathBuf>,
/// Warn if output exceeds N bytes (default: 10MB, 0=disable).
#[arg(long, default_value = "10485760")]
pub max_output_bytes: u64,
/// Append JSONL record to log file (metadata only, not content).
#[arg(long, value_name = "PATH")]
pub log: Option<PathBuf>,
/// Maximum fraction of budget a single file may consume (0.0–1.0).
#[arg(long, default_value = "0.15")]
pub max_file_pct: f64,
/// Hard cap on tokens per file (overrides percentage-based cap).
#[arg(long)]
pub max_file_tokens: Option<usize>,
/// Error if git scores are unavailable when using churn/hotspot ranking.
#[arg(long)]
pub require_git_scores: bool,
}
#[derive(ValueEnum, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum ContextStrategy {
/// Select files by value until budget is exhausted.
#[default]
Greedy,
/// Round-robin across modules/languages for coverage, then greedy fill.
Spread,
}
#[derive(ValueEnum, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum ValueMetric {
/// Rank by lines of code.
#[default]
Code,
/// Rank by token count.
Tokens,
/// Rank by git churn (requires git feature).
Churn,
/// Rank by hotspot score (requires git feature).
Hotspot,
}
#[derive(ValueEnum, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum ContextOutput {
/// Print list of selected files with stats.
#[default]
List,
/// Concatenate file contents into a single bundle.
Bundle,
/// Output JSON receipt with selection details.
Json,
}
#[derive(Args, Debug, Clone)]
#[command(
after_help = "Examples:\n tokmd handoff crates/tokmd xtask --out-dir .handoff --budget 128k\n tokmd handoff . --review-packet-dir .tokmd/review --proof-route target/ci/proof-pack-route.json --proof-plan target/proof/proof-plan.json"
)]
pub struct HandoffArgs {
/// Paths to scan (directories, files, or globs). Defaults to ".".
#[arg(value_name = "PATH")]
pub paths: Option<Vec<PathBuf>>,
/// Output directory for handoff artifacts.
#[arg(long, default_value = ".handoff")]
pub out_dir: PathBuf,
/// Token budget with optional k/m/g suffix, or 'unlimited' (e.g., "128k", "1m", "1g", "unlimited").
#[arg(long, default_value = "128k")]
pub budget: String,
/// Packing strategy for code bundle.
#[arg(long, value_enum, default_value_t = ContextStrategy::Greedy)]
pub strategy: ContextStrategy,
/// Metric to rank files by for packing.
#[arg(long, value_enum, default_value_t = ValueMetric::Hotspot)]
pub rank_by: ValueMetric,
/// Intelligence preset level.
#[arg(long, value_enum, default_value_t = HandoffPreset::Risk)]
pub preset: HandoffPreset,
/// Module roots (see `tokmd module`).
#[arg(long, value_delimiter = ',')]
pub module_roots: Option<Vec<String>>,
/// Module depth (see `tokmd module`).
#[arg(long, visible_alias = "depth")]
pub module_depth: Option<usize>,
/// Overwrite existing output directory.
#[arg(long)]
pub force: bool,
/// Strip blank lines from code bundle.
#[arg(long)]
pub compress: bool,
/// Disable smart exclusion of lockfiles, minified files, and generated artifacts.
#[arg(long)]
pub no_smart_exclude: bool,
/// Disable git-based features.
#[arg(long = "no-git")]
pub no_git: bool,
/// Maximum commits to scan for git metrics.
#[arg(long, default_value = "1000")]
pub max_commits: usize,
/// Maximum files per commit to process.
#[arg(long, default_value = "100")]
pub max_commit_files: usize,
/// Maximum fraction of budget a single file may consume (0.0–1.0).
#[arg(long, default_value = "0.15")]
pub max_file_pct: f64,
/// Hard cap on tokens per file (overrides percentage-based cap).
#[arg(long)]
pub max_file_tokens: Option<usize>,
/// Link an existing cockpit review packet directory from the handoff bundle.
///
/// If this packet contains proof/proof-pack-route.json and --proof-route is
/// absent, handoff links that packet-local route as proof-route evidence.
#[arg(long)]
pub review_packet_dir: Option<PathBuf>,
/// Link an existing review-packet verifier receipt from the handoff bundle.
#[arg(long)]
pub review_packet_check: Option<PathBuf>,
/// Link an existing affected-proof report from the handoff bundle.
#[arg(long)]
pub affected: Option<PathBuf>,
/// Link an existing proof-plan report from the handoff bundle.
#[arg(long)]
pub proof_plan: Option<PathBuf>,
/// Link an existing proof-pack route receipt from the handoff bundle.
#[arg(long)]
pub proof_route: Option<PathBuf>,
/// Link an existing tokmd evidence packet manifest from the handoff bundle.
#[arg(long = "evidence-packet", value_name = "PATH")]
pub evidence_packet: Option<PathBuf>,
}
#[derive(ValueEnum, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum HandoffPreset {
/// Minimal: tree + map only.
Minimal,
/// Standard: + complexity, derived.
Standard,
/// Risk: + hotspots, coupling (default).
#[default]
Risk,
/// Deep: everything.
Deep,
}