rightkit-context 0.2.0

Context, memory and repository-graph commands for coding agents: the membrane CLI, hook entry points and the resident a host app supervises.
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
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
//! Command line of the one `membrane` binary (C4).
//!
//! `host context|memory|blueprint ...` pass their arguments straight through to these
//! commands, and agent hooks call `membrane hook`. Nothing here keeps a process running except
//! `serve`, which the host app starts and owns.

use clap::{Args, Parser, Subcommand, ValueEnum};
use std::ffi::OsString;
use std::path::PathBuf;

#[derive(Debug, Parser)]
#[command(
    name = "membrane",
    bin_name = "membrane",
    about = "Membrane: context, memory and Blueprint queries for the host app.",
    version,
    disable_help_subcommand = true
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    /// Context for an agent.
    Context {
        #[command(subcommand)]
        command: ContextCommand,
    },
    /// Durable memory: `push`, `search`, `delete`, plus the maintenance commands.
    Memory {
        #[command(subcommand)]
        command: MemoryCommand,
    },
    /// Search or erase the indexed ChatGPT and Codex history: `search <text>`, `status`, `erase`.
    History(HistoryArgs),
    /// Adapt: observe executions, recall Taste, apply reviewed manifests, and review Taste.
    Adapt {
        #[command(subcommand)]
        command: AdaptCommand,
    },
    /// Append committed session events to a session's transcript, behind a cursor.
    Transcript {
        #[command(subcommand)]
        command: TranscriptCommand,
    },
    /// Query the Blueprint graph of the current repository.
    Blueprint(BlueprintArgs),
    /// Report what Membrane is doing.
    Status(StatusArgs),
    /// Print the path of the running `membrane` binary, with its version (`membrane.where.v1`).
    Where(WhereArgs),
    /// Answer one agent hook event: payload on stdin, reply on stdout. Always exits 0.
    Hook(HookArgs),
    /// Delivery receipts for hook context: `confirm` (from the host app's Stop hook) and `list`.
    Receipts {
        #[command(subcommand)]
        command: ReceiptsCommand,
    },
    /// Run the resident for the host app. Exits when the host app closes this process's stdin pipe.
    Serve,
    /// Enroll a repository in the registry that Blueprint and context pulls authorize against.
    Init(InitArgs),
}

#[derive(Debug, Subcommand)]
pub enum ContextCommand {
    /// Print the context Membrane would give an agent starting this task.
    Pull(PullArgs),
}

#[derive(Debug, Args)]
pub struct PullArgs {
    /// Repository to pull context for (default: the current directory).
    #[arg(long)]
    pub cwd: Option<PathBuf>,
    /// The repository, by its root path. Resolved exactly as `--cwd` is. Both flags may be given
    /// (CodeRight sends both) when they name the same path; different paths are exit 2.
    #[arg(long, alias = "repo-root")]
    pub repo: Option<PathBuf>,
    /// The task or prompt the context is for.
    #[arg(long)]
    pub prompt: Option<String>,
    /// Context budget in tokens.
    #[arg(long)]
    pub budget: Option<usize>,
    /// Print one JSON document (`membrane.context.pull.v1`) instead of the bare context text.
    #[arg(long)]
    pub json: bool,
    /// Record the returned context as a receipt for this harness, under `--request-id`.
    #[arg(long, value_enum, requires = "request_id")]
    pub wire: Option<PullWireArg>,
    /// The caller's id for this context. `receipts mark <id> --delivered` names it later.
    #[arg(long)]
    pub request_id: Option<String>,
}

/// The harnesses that may record a receipt through `context pull --wire`. Their transcripts are
/// their own; Membrane only keeps the receipt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum PullWireArg {
    Coderight,
}

#[derive(Debug, Subcommand)]
pub enum MemoryCommand {
    /// Store text as a durable memory, optionally named, scoped, tiered and keyworded.
    Push(PushArgs),
    /// Search memories by text, within tiers and a scope chain or a repository's scope.
    Search(SearchArgs),
    /// Delete one memory by id.
    Delete(DeleteArgs),
    /// A durable-memory maintenance command (`list`, `get`, `recall`, `put`, `export`, ...),
    /// run in process against the store: `membrane memory <command> [args...]`. `list --json`,
    /// `get <id> --json`, `scopes` and `info` answer as versioned documents.
    #[command(external_subcommand)]
    Durable(Vec<String>),
}

#[derive(Debug, Args)]
pub struct PushArgs {
    /// The text to remember.
    #[arg(required_unless_present = "stdin", conflicts_with = "stdin")]
    pub text: Option<String>,
    /// Read the text from standard input.
    #[arg(long)]
    pub stdin: bool,
    /// Name the memory: its id becomes `<scope>/<name>`, so pushing the same name in the same
    /// scope again replaces it. Without a name the id is generated.
    #[arg(long)]
    pub name: Option<String>,
    /// The scope to store it under: a repository path, a scope slug, or `global` (the default).
    #[arg(long)]
    pub scope: Option<String>,
    /// The tier to store it in (default `semantic`).
    #[arg(long, value_enum)]
    pub tier: Option<TierArg>,
    /// A retrieval keyword. Repeat the flag for several. Without any, keywords are derived from
    /// the text.
    #[arg(long = "keyword")]
    pub keywords: Vec<String>,
    /// Print one JSON document (`membrane.memory.push.v1`) with the new id.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Subcommand)]
pub enum AdaptCommand {
    /// Record one observation: an `AdaptObservationRequestV1` JSON document (`membrane.adapt.observe.v1`).
    Observe(AdaptObserveArgs),
    /// Recall the active Taste records for a query (`membrane.adapt.recall.v1`).
    Recall(AdaptRecallArgs),
    /// Admit a reviewed manifest, or sealed Insight issues (`membrane.adapt.apply.v1`).
    Apply(AdaptApplyArgs),
    /// Build a pending Taste manifest from native mine output (`membrane.adapt.taste_review.v1`).
    TasteReview(AdaptTasteReviewArgs),
    /// Live Adapt progress for a scope (`membrane.adapt.status.v1`).
    Status(AdaptStatusArgs),
    /// Bind semantic decisions to a pending Taste manifest (`membrane.adapt.adjudicate_taste.v1`).
    AdjudicateTaste(AdaptAdjudicateTasteArgs),
    /// Mine committed session transcripts from the store, report only (`membrane.adapt.mine_session.v1`).
    MineSession(AdaptMineSessionArgs),
}

#[derive(Debug, Args)]
pub struct AdaptStatusArgs {
    /// The scope whose Adapt progress is read.
    #[arg(long, default_value = "global")]
    pub scope: String,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.adapt.status.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct AdaptAdjudicateTasteArgs {
    /// The pending manifest (`PreferenceManifestV1` JSON) from `adapt taste-review`.
    #[arg(long)]
    pub manifest: PathBuf,
    /// The semantic decisions (`SemanticAdjudicationV1` JSON).
    #[arg(long)]
    pub decisions: PathBuf,
    /// When the decisions were validated; must equal the decisions document's `validated_at`.
    #[arg(long)]
    pub validated_at: String,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.adapt.adjudicate_taste.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct AdaptMineSessionArgs {
    /// The sessions whose committed events are mined. Repeat for several.
    #[arg(long = "session", required = true)]
    pub session_ids: Vec<String>,
    /// The scope the findings are for.
    #[arg(long, default_value = "workspace")]
    pub scope: String,
    /// How many times a pattern must recur to be reported.
    #[arg(long, default_value_t = 2)]
    pub min_recurrence: u32,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.adapt.mine_session.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct AdaptObserveArgs {
    /// Read the observation document from standard input.
    #[arg(long, conflicts_with = "input")]
    pub stdin: bool,
    /// Read the observation document from this file.
    #[arg(long, conflicts_with = "stdin")]
    pub input: Option<PathBuf>,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.adapt.observe.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct AdaptRecallArgs {
    /// The text the Taste records must apply to.
    pub query: String,
    /// Most records to return.
    #[arg(short = 'k', default_value_t = 6)]
    pub k: usize,
    /// The scope to recall from.
    #[arg(long, default_value = "global")]
    pub scope: String,
    /// A scope dimension as KEY=VALUE. Repeat the flag for several.
    #[arg(long = "dimension", value_name = "KEY=VALUE")]
    pub dimensions: Vec<String>,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.adapt.recall.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct AdaptApplyArgs {
    /// A reviewed preference manifest (`PreferenceManifestV1` JSON) to admit.
    #[arg(long, conflicts_with = "insights")]
    pub manifest: Option<PathBuf>,
    /// Sealed Insight issues (a JSON array) to request admission for. Needs `--installation-id`.
    #[arg(long, conflicts_with = "manifest")]
    pub insights: Option<PathBuf>,
    /// The installation the Insights are admitted under (with `--insights`).
    #[arg(long)]
    pub installation_id: Option<String>,
    /// Validate and plan without writing.
    #[arg(long)]
    pub dry_run: bool,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.adapt.apply.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct AdaptTasteReviewArgs {
    /// Native mine output with Taste source bindings (`{"taste_review": {...}}` JSON).
    #[arg(long)]
    pub input: PathBuf,
    /// The installation the manifest is bound to.
    #[arg(long)]
    pub installation_id: String,
    /// The canonical pool digest to bind; default is the current pool.
    #[arg(long)]
    pub canonical_pool_sha256: Option<String>,
    /// When the manifest is created, as the caller records it.
    #[arg(long)]
    pub created_at: String,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.adapt.taste_review.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Subcommand)]
pub enum TranscriptCommand {
    /// Append committed events at `--cursor`, the session's current end (`membrane.transcript.append.v1`).
    Append(TranscriptAppendArgs),
    /// The committed end cursor of a session's transcript (`membrane.transcript.tip.v1`).
    Tip(TranscriptTipArgs),
}

#[derive(Debug, Args)]
pub struct TranscriptTipArgs {
    /// The session whose transcript end is read.
    #[arg(long)]
    pub session: String,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.transcript.tip.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct TranscriptAppendArgs {
    /// The session whose transcript is appended to.
    #[arg(long)]
    pub session: String,
    /// The session's current end: the `expected_stream_seq` the batch extends.
    #[arg(long)]
    pub cursor: u64,
    /// The scope the rows belong to.
    #[arg(long, default_value = "global")]
    pub scope: String,
    /// Read the events (a JSON array of `SessionTranscriptEventV1` rows) from standard input.
    #[arg(long, conflicts_with = "text")]
    pub stdin: bool,
    /// The events as one JSON array of `SessionTranscriptEventV1` rows.
    #[arg(long, conflicts_with = "stdin")]
    pub text: Option<String>,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.transcript.append.v1`).
    #[arg(long)]
    pub json: bool,
}

/// A memory tier, by the name `memory search --tier` takes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum TierArg {
    Working,
    Episodic,
    Semantic,
}

#[derive(Debug, Args)]
pub struct SearchArgs {
    /// The text to search for.
    pub query: String,
    /// Only memories in this tier. Repeat the flag for several tiers.
    #[arg(long = "tier", value_enum)]
    pub tiers: Vec<TierArg>,
    /// Most results to return, 1 to 100.
    #[arg(long, default_value_t = 6)]
    pub limit: usize,
    /// Only the memories a repository can see: its own scope, the scopes above it, and `global`.
    /// Without `--cwd`, `--repo` or `--scope`, every scope is searched.
    #[arg(long, conflicts_with_all = ["repo", "scopes"])]
    pub cwd: Option<PathBuf>,
    /// The repository, by its root path. Resolved exactly as `--cwd` is.
    #[arg(long, alias = "repo-root", conflicts_with_all = ["cwd", "scopes"])]
    pub repo: Option<PathBuf>,
    /// A scope chain: search exactly these scopes (paths or slugs), most specific first. Repeat
    /// the flag for several. No ancestors or `global` are added.
    #[arg(long = "scope", conflicts_with_all = ["cwd", "repo"])]
    pub scopes: Vec<String>,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.memory.search.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct DeleteArgs {
    /// The memory id, as `memory search` or `memory list` printed it.
    pub id: String,
    /// The Cortex store (default: the store under the state root).
    #[arg(long)]
    pub db: Option<PathBuf>,
    /// Print one JSON document (`membrane.memory.delete.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct HistoryArgs {
    /// `search <text> [--scope S] [--limit N] --json`, `status --json`, or
    /// `erase --conversation ID | --source ID`.
    #[arg(required = true, trailing_var_arg = true, allow_hyphen_values = true)]
    pub words: Vec<String>,
}

#[derive(Debug, Args)]
pub struct BlueprintArgs {
    /// A Blueprint verb with its arguments (`status`, `search <text>`, `impact <node>`, `build`,
    /// `refresh`, ...), or free text, which is searched. `build` and `refresh` publish a new graph
    /// generation for the repository.
    #[arg(required = true, trailing_var_arg = true, allow_hyphen_values = true)]
    pub query: Vec<String>,
}

#[derive(Debug, Args)]
pub struct WhereArgs {
    /// Print one JSON object (`membrane.where.v1`) instead of the bare path.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct StatusArgs {
    /// Print one JSON object (`membrane.status.v1`).
    #[arg(long)]
    pub json: bool,
    /// Exit 4 unless the resident answered; the status is still printed.
    #[arg(long)]
    pub require_resident: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum WireArg {
    Claude,
    Codex,
}

#[derive(Debug, Args)]
pub struct HookArgs {
    /// The harness whose hook schema the reply follows.
    #[arg(long, value_enum)]
    pub wire: WireArg,
    /// The hook event, e.g. `SessionStart` or `UserPromptSubmit`.
    #[arg(long)]
    pub event: String,
}

#[derive(Debug, Subcommand)]
pub enum ReceiptsCommand {
    /// Mark emitted context `delivered` when its marker is in the host transcript. Prints
    /// nothing and always exits 0.
    Confirm(ConfirmArgs),
    /// List the context receipts.
    List(ReceiptsListArgs),
    /// Mark the receipt `<id>` delivered, for a caller that injected the context itself.
    Mark(MarkArgs),
}

#[derive(Debug, Args)]
pub struct MarkArgs {
    /// The `--request-id` the context was pulled with.
    pub id: String,
    /// Required: the only change `mark` makes. An `emitted` receipt becomes `delivered`; any
    /// other state is left as it is.
    #[arg(long, required = true)]
    pub delivered: bool,
    /// Print one JSON document (`membrane.receipts.mark.v1`).
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct ConfirmArgs {
    /// The harness whose transcript this is.
    #[arg(long, value_enum)]
    pub wire: WireArg,
    /// The session whose receipts are confirmed.
    #[arg(long)]
    pub session: String,
    /// The host's record of the session: the Claude transcript or the Codex rollout (JSONL).
    #[arg(long)]
    pub transcript: PathBuf,
}

#[derive(Debug, Args)]
pub struct ReceiptsListArgs {
    /// Only this session's receipts.
    #[arg(long)]
    pub session: Option<String>,
    /// Print one JSON array.
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct InitArgs {
    /// The repository root to enroll.
    pub root: PathBuf,
    #[arg(long)]
    pub repository: String,
    #[arg(long)]
    pub scope: String,
    /// Move an existing exact registry binding from this old root to `root`.
    #[arg(long)]
    pub rebind_from: Option<PathBuf>,
    #[arg(long)]
    pub virtual_id: Option<String>,
    #[arg(long)]
    pub tenant_id: Option<String>,
    #[arg(long)]
    pub parent: Vec<String>,
    #[arg(long)]
    pub native: Option<String>,
    #[arg(long)]
    pub config: Option<PathBuf>,
    /// Show what would change without writing the registry.
    #[arg(long)]
    pub dry_run: bool,
}

/// Parse a command line. Argument zero is the program name.
pub fn parse<I, T>(args: I) -> Result<Cli, clap::Error>
where
    I: IntoIterator<Item = T>,
    T: Into<OsString> + Clone,
{
    Cli::try_parse_from(args)
}