instinct-cli 0.3.6

Local memory for AI coding agents: an MCP server and CLI that gives Claude Code, Codex, Cursor and any MCP client the one lesson that fits. Lessons that keep failing go quiet.
# Explicit exclusions in the current brief. Match a named convention or a prescribed output.
# Keep the journal intact; the same method can be eligible again on the next request.
TAC # pub(crate) struct Brief { exclusions: Vec<Vec<String>> }
AAA # fn words(text: &str) -> Vec<&str>
TAC # text.split(|c: char| !c.is_alphanumeric() && !matches!(c, '/' | '-' | '\'')).filter(|word| !word.is_empty()).collect()
TTT # end
AAA # fn article(word: &str) -> bool
TAC # matches!(word, "a" | "an" | "the" | "my" | "our" | "your" | "her" | "his" | "their")
TTT # end
AAA # fn directive_prefix(prefix: &[&str]) -> bool
AAC # let prefix = prefix.strip_suffix(&["please"]).unwrap_or(prefix)
TAC # prefix.last().is_none_or(|last| matches!(*last, "and" | "but" | "actually" | "instead" | "then" | "also" | "just" | "still" | "want" | "wants" | "prefer" | "prefers" | "requested" | "require" | "requires")) || prefix.ends_with(&["can", "you"]) || prefix.ends_with(&["could", "you"]) || prefix.ends_with(&["would", "you"])
TTT # end
AAA # fn negation_prefix(prefix: &[&str]) -> bool
TAC # [ &["do", "not"][..], &["don't"], &["never"], &["no", "need", "to"] ].iter().any(|ending| prefix.strip_suffix(*ending).is_some_and(directive_prefix))
TTT # end
AAA # fn object(words: &[&str]) -> Vec<String>
AAC # let words = words.strip_prefix(&["as"]).unwrap_or(words)
AAC # let end = words.iter().enumerate().position(|(i,word)| matches!(*word, "but" | "today" | "this" | "for" | "please" | "instead" | "rather" | "from" | "on" | "to") || (*word == "and" && words.get(i+1).is_some_and(|next| output_verb(next) || grouping(next) || matches!(*next, "please" | "also" | "then" | "check" | "verify" | "confirm" | "no" | "do")))).unwrap_or(words.len())
AAC # let mut noun: Vec<String> = words[..end].iter().copied().skip_while(|word| article(word)).map(str::to_owned).collect()
CAA # if noun.len() > 1 && matches!(noun.last().map(String::as_str), Some("shorthand" | "format" | "template"))
AAC # noun.pop()
TTT # end
TAC # noun
TTT # end
AAA # fn grouping(word: &str) -> bool
TAC # matches!(word, "group" | "sort" | "organize" | "organise" | "order" | "arrange" | "categorize" | "categorise" | "classify" | "split")
TTT # end
AAA # fn current_action(words: &[&str]) -> bool
TAC # words.iter().enumerate().any(|(i,w)| (output_verb(w) || grouping(w)) && (directive_prefix(&words[..i]) || words[..i].strip_suffix(&["do"]).is_some_and(directive_prefix)))
TTT # end
AAA # fn changed_object<'a>(tokens: &'a [&'a str], i: usize) -> Option<&'a [&'a str]>
AAC # let prefix = &tokens[..i]
AAC # let rest = &tokens[i + 1..]
TAC # match tokens[i] {
TAC # "no" if directive_prefix(prefix) => Some(rest.strip_prefix(&["need", "for"]).unwrap_or(rest)),
TAC # "skip" | "omit" | "avoid" | "exclude" | "drop" | "remove" if directive_prefix(prefix) => Some(rest),
TAC # "without" if directive_prefix(prefix) || current_action(prefix) => Some(rest),
TAC # "leave" | "keep" if directive_prefix(prefix) => {
TAC # if let Some(after) = rest.strip_prefix(&["out"]).or_else(|| rest.strip_prefix(&["off"])) { return Some(after); }
TAC # rest.iter().position(|w| matches!(*w, "out" | "off")).map(|end| &rest[..end])
TAC # }
TAC # "rather" | "instead" if directive_prefix(prefix) || current_action(prefix) => {
TAC # let join = if tokens[i] == "rather" { "than" } else { "of" };
TAC # rest.strip_prefix(&[join]).map(|after| if after.first().is_some_and(|w| output_verb(w)) { &after[1..] } else { after })
TAC # }
TAC # verb if output_verb(verb) && negation_prefix(prefix) => Some(rest),
TAC # _ => None,
TAC # }
TTT # end
# A reported quotation cannot issue a current directive. Preserve a quoted noun when it is the explicit object of a command.
AAA # fn directive_text(text: &str) -> std::borrow::Cow<'_, str>
AAC # let spans = super::brief_action::quoted_spans(text)
CAA # if spans.is_empty()
CAG # return std::borrow::Cow::Borrowed(text)
TTT # end
AAC # let mut out = String::new()
AAC # let mut copied = 0
AGA # for span in spans
AAC # let prefix = &text[copied..span.start]
AAC # out.push_str(prefix)
AAC # let clause = prefix.rsplit(['.', ',', ';', '!', '?', '\n']).next().unwrap_or("").to_lowercase()
AAC # let preceding = words(&clause)
AAC # let operand = preceding.iter().rev().copied().find(|w| !article(w)).is_some_and(|w| matches!(w, "no" | "omit" | "skip" | "exclude" | "avoid" | "without" | "leave" | "keep" | "off" | "out" | "remove" | "drop" | "than" | "of") || output_verb(w))
AAC # let mut quoted = text[span.clone()].chars()
AAC # quoted.next()
AAC # quoted.next_back()
AAC # let literal = quoted.as_str()
AAC # let lowered = literal.to_lowercase()
AAC # let is_label = !literal.contains(['.', ';', '!', '?', '\n']) && !words(&lowered).iter().any(|w| matches!(*w, "no" | "not" | "without" | "instead" | "rather" | "omit" | "skip" | "leave" | "keep" | "avoid" | "exclude" | "remove" | "drop"))
CAA # if operand && is_label
AAC # out.push_str(literal)
CAC # else
AAC # out.push('.')
TTT # end
AAC # copied = span.end
TTT # end
AAC # out.push_str(&text[copied..])
TAC # std::borrow::Cow::Owned(out)
TTT # end
TAC # impl Brief {
AAA # pub(crate) fn parse(text: &str) -> Self
AAC # let lower = directive_text(text).to_lowercase().replace('’', "'")
AAC # let mut exclusions: Vec<Vec<String>> = Vec::new()
AGA # for clause in lower.split(['.', ',', ';', '!', '?', '\n'])
AAC # let tokens = words(clause)
TGT # A subsequent affirmative instruction can reverse an earlier exclusion in this request.
CAA # if !exclusions.is_empty() && current_action(&tokens)
AAC # exclusions.retain(|noun| !prescribes(clause, noun))
TTT # end
AGA # for i in 0..tokens.len()
CAA # if let Some(rejected) = changed_object(&tokens, i)
AAC # let noun = object(rejected)
CAA # if !noun.is_empty() && !exclusions.contains(&noun)
AAC # exclusions.push(noun)
TTT # end
TTT # end
TTT # end
TTT # end
TAC # Self { exclusions }
TTT # end
AAA # pub(crate) fn excludes(&self, trigger: &str, method: &str) -> bool
CAA # if self.exclusions.is_empty()
CAG # return false
TTT # end
AAC # let trigger = trigger.to_lowercase()
AAC # let cues = words(&trigger)
AAC # let method = method.to_lowercase()
TAC # self.exclusions.iter().any(|noun| {
TAC # if matches!(noun[0].as_str(), "usual" | "default" | "standard" | "regular") && noun.len() > 1 {
TAC # return cues.windows(noun.len()).any(|part| part == noun.as_slice());
TAC # }
TAC # method.split(['.', ',', ';', '!', '?', '\n']).any(|clause| prescribes(clause, noun))
TAC # })
TTT # end
TAC # }
AAA # fn output_verb(word: &str) -> bool
TAC # matches!(word, "make" | "create" | "prepare" | "present" | "publish" | "format" | "display" | "list" | "give" | "add" | "include" | "insert" | "put" | "show" | "attach" | "deliver" | "emit" | "export" | "generate" | "output" | "print" | "provide" | "render" | "return" | "send" | "use" | "write")
TTT # end
AAA # fn prescribes(clause: &str, noun: &[String]) -> bool
AAC # let tokens = words(clause)
TGT # An example, conditional or negative instruction does not prescribe that output.
CAA # if tokens.iter().any(|word| matches!(*word, "if" | "unless" | "whether" | "without" | "skip" | "avoid" | "omit" | "never" | "not" | "don't" | "no"))
CAG # return false
TTT # end
TGT # An excluded grouping criterion only conflicts with a method that organizes by that same criterion.
CAA # if noun.first().is_some_and(|word| word == "by")
CAG # return noun.len() > 1 && tokens.iter().any(|w| grouping(w)) && tokens.windows(noun.len()).any(|part| part == noun)
TTT # end
TGT # A slash-separated name can refer to the literal field labels in a template.
CAA # if noun.len() == 1 && noun[0].contains('/') && clause.contains(':')
AAC # let labels: Vec<&str> = clause.split(|c: char| !c.is_alphanumeric() && c != ':').filter_map(|s| s.strip_suffix(':')).filter(|s| !s.is_empty()).collect()
CAA # if labels.len() >= 2 && labels.join("/") == noun[0]
CAG # return true
TTT # end
TTT # end
TGT # Match the named object in an output role, not an incidental reference to the same word.
AGA # for (i, phrase) in tokens.windows(noun.len()).enumerate()
CAA # if phrase != noun
GAG # continue
TTT # end
AAC # let before = tokens[..i].iter().copied().rev().find(|word| !article(word))
TGT # In "export the PDF to PNG", PDF is the input and PNG is the output.
AAC # let following = tokens.get(i + noun.len())
CAA # if matches!(following, Some(&"to" | &"into" | &"as")) && matches!(before, Some("export" | "render" | "use"))
GAG # continue
TTT # end
CAA # if before.is_some_and(|word| matches!(word, "as" | "into") || output_verb(word))
CAG # return true
TTT # end
TTT # end
TAC # false
TTT # end
TAC # #[cfg(test)]
TAC # mod tests {
TAC # use super::Brief;
TAC # #[test]
AAA # fn explicit_output_overrides_are_specific_to_the_method()
AAC # let render = "Render the invoice as a PDF attachment."
AGA # for q in ["Do not render a PDF.", "Don't attach the PDF.", "Write the report without a PDF.", "Please export as HTML instead of PDF."]
AAC # let brief = Brief::parse(q)
AAC # assert!(brief.excludes("invoice delivery", render), "{q}")
AAC # assert!(!brief.excludes("invoice delivery", "Read the PDF and verify the invoice totals."), "{q}")
TTT # end
AGA # for q in ["There is no PDF. Please send it again.", "The help text says \"no PDF\".", "No screenshots.", "No PDF annotations.", "The invoice arrived without a PDF. Please send it again."]
AAC # assert!(!Brief::parse(q).excludes("invoice delivery", render), "{q}")
TTT # end
AAC # let brief = Brief::parse("No PDF.")
AAC # assert!(!brief.excludes("invoice", "Do not send a PDF."))
AAC # assert!(!brief.excludes("invoice", "Export the PDF to PNG."))
TTT # end
TAC # #[test]
AAA # fn a_cancelled_convention_is_not_a_global_opt_out()
AAC # let cue = "When Tess asks for her usual client update"
AAC # assert!(Brief::parse("Skip my usual client update today.").excludes(cue, "Use a one-line template."))
AAC # assert!(!Brief::parse("Skip my usual client update today.").excludes("Verify invoice totals", "Check the signed order."))
AAC # assert!(!Brief::parse("Don't skip my usual client update today.").excludes(cue, "Use a one-line template."))
TTT # end
TAC # #[test]
AAA # fn phrasal_exclusions_and_current_reversals()
AAC # let add = "Include a seating chart in the handout."
AGA # for q in ["Leave the seating chart off today.", "Please leave out the seating chart.", "Keep the ‘seating chart’ off the page.", "Do not include the seating chart."]
AAC # assert!(Brief::parse(q).excludes("prepare handout", add), "{q}")
TTT # end
AGA # for q in ["Do not leave the seating chart off.", "The old guide says ‘leave the seating chart off.’", "Leave the seating chart on.", "No seating chart. Actually include the seating chart."]
AAC # assert!(!Brief::parse(q).excludes("prepare handout", add), "{q}")
TTT # end
AAC # let detail = Brief::parse("Keep the materials and labor breakdown off the estimate.")
AAC # assert!(detail.excludes("write estimate", "Show a materials and labor breakdown."))
AAC # assert!(!detail.excludes("write estimate", "Show a materials and shipping breakdown."))
AAC # let alternate = Brief::parse("Organize the milestones as notes rather than as a grid.")
AAC # assert!(alternate.excludes("plan", "Arrange milestones as a grid."))
AAC # assert!(!alternate.excludes("plan", "Arrange milestones as notes."))
AAC # let sorting = Brief::parse("Sort by region rather than by surname.")
AAC # assert!(sorting.excludes("prepare register", "Group entries by surname."))
AAC # assert!(!sorting.excludes("prepare register", "Group entries by region."))
AAC # assert!(!sorting.excludes("prepare register", "Check entries by surname against the original."))
AAC # assert!(!Brief::parse("We sorted by region rather than by surname last time.").excludes("prepare register", "Group entries by surname."))
TTT # end
TAC # }