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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
mod cli;
mod llm;
mod mcp;
mod triage_policy;
// Core modules re-exported from the library crate
pub use rectilinear_core::config;
pub use rectilinear_core::db;
pub use rectilinear_core::embedding;
pub use rectilinear_core::linear;
pub use rectilinear_core::search;
use anyhow::Result;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(
name = "rectilinear",
about = "Linear issue intelligence tool",
version
)]
struct Cli {
#[command(subcommand)]
command: Commands,
/// Override the active workspace
#[arg(long, global = true)]
workspace: Option<String>,
}
#[derive(Subcommand)]
enum Commands {
/// Manage configuration (interactive if no subcommand given)
Config {
#[command(subcommand)]
action: Option<ConfigAction>,
},
/// Manage workspaces
Workspace {
#[command(subcommand)]
action: WorkspaceAction,
},
/// Synchronize a team's Linear entities
Sync {
/// Team key (e.g., ENG)
#[arg(short, long)]
team: Option<String>,
/// Force full sync (ignore cursor)
#[arg(long)]
full: bool,
/// Generate embeddings after sync
#[arg(long)]
embed: bool,
/// Include archived Linear entities
#[arg(long)]
include_archived: bool,
/// Report each synchronization page and adaptive page-size reduction
#[arg(long)]
verbose: bool,
},
/// Search issues
Search {
/// Search query
query: String,
/// Filter by team
#[arg(short, long)]
team: Option<String>,
/// Filter by state
#[arg(short, long)]
state: Option<String>,
/// Search mode: fts, vector, or hybrid
#[arg(short, long, default_value = "hybrid")]
mode: String,
/// Max results
#[arg(short, long)]
limit: Option<usize>,
/// Output as JSON
#[arg(long)]
json: bool,
},
/// Find similar issues (duplicate detection)
Find {
/// Text to find similar issues for
#[arg(long)]
similar: String,
/// Filter by team
#[arg(short, long)]
team: Option<String>,
/// Similarity threshold (0.0-1.0)
#[arg(long, default_value = "0.7")]
threshold: f32,
/// Max results
#[arg(short, long)]
limit: Option<usize>,
/// Output as JSON
#[arg(long)]
json: bool,
},
/// Show issue details
Show {
/// Issue ID or identifier (e.g., ENG-123)
id: String,
/// Output as JSON
#[arg(long)]
json: bool,
/// Include comments
#[arg(long)]
comments: bool,
},
/// Create a new issue in Linear
Create {
/// Team key (e.g., ENG)
#[arg(short, long)]
team: Option<String>,
/// Issue title
#[arg(long)]
title: String,
/// Issue description
#[arg(short, long)]
description: Option<String>,
/// Priority (1=Urgent, 2=High, 3=Medium, 4=Low)
#[arg(short, long)]
priority: Option<i32>,
/// Label names
#[arg(short, long)]
labels: Vec<String>,
/// Project UUID, slug, or name
#[arg(long)]
project: Option<String>,
/// Project milestone UUID or name
#[arg(long)]
project_milestone: Option<String>,
},
/// Append to an existing issue
Append {
/// Issue ID or identifier
id: String,
/// Add a comment
#[arg(long)]
comment: Option<String>,
/// Append to description
#[arg(long)]
description: Option<String>,
},
/// Generate embeddings for synced issues
Embed {
/// Filter by team
#[arg(short, long)]
team: Option<String>,
/// Regenerate all embeddings
#[arg(long)]
force: bool,
},
/// Interactively triage unprioritized issues with AI
Triage {
/// Team key (e.g., ENG)
#[arg(short, long)]
team: Option<String>,
/// Max issues to triage
#[arg(short, long)]
limit: Option<usize>,
/// Skip similar-issue context
#[arg(long)]
no_context: bool,
/// Include completed/canceled issues (for archival prioritization)
#[arg(long)]
include_completed: bool,
/// Output queue as JSON instead of launching the TUI
#[arg(long)]
json: bool,
},
/// Mark an issue as triaged: set priority and optionally update fields
MarkTriaged {
/// Issue identifier (e.g., CUT-42)
id: String,
/// Priority (1=Urgent, 2=High, 3=Medium, 4=Low)
#[arg(short, long)]
priority: i32,
/// Improved title
#[arg(long)]
title: Option<String>,
/// Updated description
#[arg(short, long)]
description: Option<String>,
/// Triage comment
#[arg(long)]
comment: Option<String>,
/// Set state (e.g., "Done", "Cancelled", "Duplicate")
#[arg(long)]
state: Option<String>,
/// Set labels (comma-separated, replaces existing)
#[arg(short, long, value_delimiter = ',')]
labels: Option<Vec<String>>,
/// Set project name (or "none" to remove)
#[arg(long)]
project: Option<String>,
/// Output as JSON
#[arg(long)]
json: bool,
},
/// List available Linear teams
Teams,
/// Manage Linear projects
Projects {
#[command(subcommand)]
action: cli::projects_cmd::ProjectAction,
},
/// Manage Linear project milestones
Milestones {
#[command(subcommand)]
action: cli::projects_cmd::MilestoneAction,
},
/// Start MCP server (stdio transport)
Serve,
}
#[derive(Subcommand)]
enum ConfigAction {
/// Set a config value
Set {
/// Config key
key: String,
/// Config value
value: String,
},
/// Get a config value
Get {
/// Config key
key: String,
},
/// Show all config
Show,
/// Add a new workspace
AddWorkspace,
/// Remove a workspace
RemoveWorkspace {
/// Workspace name to remove
name: String,
},
}
#[derive(Subcommand)]
enum WorkspaceAction {
/// Set the active workspace
Assume {
/// Workspace name
name: String,
},
/// List configured workspaces
List,
/// Show the current active workspace
Current,
}
/// Resolve which workspace to use: CLI flag > config resolution
fn resolve_workspace(cli_flag: Option<&str>, config: &config::Config) -> Result<String> {
if let Some(name) = cli_flag {
// Validate workspace exists
if !config.workspaces.contains_key(name) {
anyhow::bail!("Workspace '{}' not found in config", name);
}
Ok(name.to_string())
} else {
config.resolve_active_workspace()
}
}
#[tokio::main]
async fn main() -> Result<()> {
if std::env::var("RECTILINEAR_DEBUG").is_ok() {
eprintln!(
"rectilinear {} ({})",
env!("CARGO_PKG_VERSION"),
env!("GIT_HASH")
);
}
let cli = Cli::parse();
// Initialize logging for non-serve commands
match &cli.command {
Commands::Serve => {
// MCP server uses stdio, so no logging to stdout
tracing_subscriber::fmt()
.with_env_filter("rectilinear=warn")
.with_writer(std::io::stderr)
.init();
}
_ => {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "rectilinear=info".into()),
)
.with_writer(std::io::stderr)
.without_time()
.init();
}
}
let config = config::Config::load()?;
match cli.command {
Commands::Config { action } => match action {
Some(ConfigAction::Set { key, value }) => cli::config_cmd::handle_set(&key, &value)?,
Some(ConfigAction::Get { key }) => cli::config_cmd::handle_get(&key)?,
Some(ConfigAction::Show) => cli::config_cmd::handle_show()?,
Some(ConfigAction::AddWorkspace) => cli::config_cmd::handle_add_workspace()?,
Some(ConfigAction::RemoveWorkspace { name }) => {
let db = db::Database::open(&config::Config::db_path()?)?;
cli::config_cmd::handle_remove_workspace(&name, &db)?
}
None => cli::config_cmd::handle_interactive()?,
},
Commands::Workspace { action } => match action {
WorkspaceAction::Assume { name } => cli::workspace_cmd::handle_assume(&name, &config)?,
WorkspaceAction::List => cli::workspace_cmd::handle_list(&config)?,
WorkspaceAction::Current => cli::workspace_cmd::handle_current(&config)?,
},
Commands::Serve => {
let db = db::Database::open(&config::Config::db_path()?)?;
mcp::serve(db, config).await?;
}
_ => {
// All other commands need the database and workspace
let db = db::Database::open(&config::Config::db_path()?)?;
let workspace = resolve_workspace(cli.workspace.as_deref(), &config)?;
match cli.command {
Commands::Sync {
team,
full,
embed,
include_archived,
verbose,
} => {
cli::sync_cmd::handle_sync(
&db,
&config,
team.as_deref(),
full,
embed,
include_archived,
verbose,
&workspace,
)
.await?;
}
Commands::Search {
query,
team,
state,
mode,
limit,
json,
} => {
let mode = mode.parse()?;
let limit = limit.unwrap_or(config.search.default_limit);
cli::search_cmd::handle_search(
&db,
&config,
cli::search_cmd::HandleSearchParams {
query: &query,
team: team.as_deref(),
state: state.as_deref(),
mode,
limit,
json,
workspace: &workspace,
},
)
.await?;
}
Commands::Find {
similar,
team,
threshold,
limit,
json,
} => {
let limit = limit.unwrap_or(config.search.default_limit);
cli::search_cmd::handle_find_similar(
&db,
&config,
&similar,
team.as_deref(),
threshold,
limit,
json,
&workspace,
)
.await?;
}
Commands::Show { id, json, comments } => {
cli::show_cmd::handle_show(&db, &config, &id, json, comments, &workspace)?;
}
Commands::Create {
team,
title,
description,
priority,
labels,
project,
project_milestone,
} => {
cli::create_cmd::handle_create(
&db,
&config,
cli::create_cmd::HandleCreateParams {
team: team.as_deref(),
title: &title,
description: description.as_deref(),
priority,
labels: &labels,
project: project.as_deref(),
project_milestone: project_milestone.as_deref(),
workspace: &workspace,
},
)
.await?;
}
Commands::Append {
id,
comment,
description,
} => {
cli::append_cmd::handle_append(
&db,
&config,
&id,
comment.as_deref(),
description.as_deref(),
&workspace,
)
.await?;
}
Commands::Embed { team, force } => {
cli::embed_cmd::handle_embed(&db, &config, team.as_deref(), force, &workspace)
.await?;
}
Commands::Triage {
team,
limit,
no_context,
include_completed,
json,
} => {
if json {
cli::triage_cmd::handle_triage_json(
&db,
&config,
team.as_deref(),
limit,
no_context,
include_completed,
&workspace,
)
.await?;
} else {
cli::triage_cmd::handle_triage(
&db,
&config,
team.as_deref(),
limit,
no_context,
include_completed,
&workspace,
)
.await?;
}
}
Commands::MarkTriaged {
id,
priority,
title,
description,
comment,
state,
labels,
project,
json,
} => {
cli::mark_triaged_cmd::handle_mark_triaged(
&db,
&config,
cli::mark_triaged_cmd::MarkTriagedParams {
id: &id,
priority,
title: title.as_deref(),
description: description.as_deref(),
comment: comment.as_deref(),
state: state.as_deref(),
labels: labels.as_deref(),
project: project.as_deref(),
json_output: json,
workspace: &workspace,
},
)
.await?;
}
Commands::Teams => {
cli::teams_cmd::handle_teams(&config, &workspace).await?;
}
Commands::Projects { action } => {
cli::projects_cmd::handle_project_action(
action,
&db,
&config,
&workspace,
)
.await?;
}
Commands::Milestones { action } => {
cli::projects_cmd::handle_milestone_action(
action,
&db,
&config,
&workspace,
)
.await?;
}
Commands::Config { .. } | Commands::Workspace { .. } | Commands::Serve => {
unreachable!()
}
}
}
}
Ok(())
}