Skip to content

feat(completion): carapace-bin as the third-party completion source (spike + implementation) - #243

Merged
caomengxuan666 merged 2 commits into
masterfrom
feat/carapace-completions
Oct 10, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
feat/carapace-completions

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #187 (interaction checklist item "补全描述", A 路线). Follow-up spike on top of the 80% completion infra: ExternalCompletionPlugin already serves the 180 winuxcmd applet TOMLs + NIU_COMPDEFS; this adds carapace-bin as the spec source for everything else (git / curl / gh / docker / …, ~1200 community specs).

Spike findings (carapace-bin v1.8.0)

  • License / size: MIT (rsteube) — compatible with niubash's MIT. carapace.exe 87 MB unpacked, 17.5 MB packed (windows_amd64 zip).
  • CLI contract (verified by experiment): carapace <command> fish <words...> — completer name first, then the shell, then the words after the command (last word = word under cursor, empty string right after a space). Stdout is one candidate per line in fish format value\tDescription, already prefix-filtered. Stderr unused; unknown command → exit 0, no output. Meta lines to skip: ERR\t… (dynamic-spec failure channel) and a bare _ placeholder. Note the bridge scripts also export COMP_LINE/COMP_POINT, but nested parsing works through the plain args — no env needed.
  • Latency: 104–146 ms per invocation warm on Windows (137/146/137 ms git, 104/105 ms curl, 127 ms gh). Above the 100 ms interactive budget → caching is required, see below.
  • Spec quirks found: git's spec does not resolve nested context through the CLI (git commit <TAB> returns top-level candidates; git stash push <TAB> in a non-repo emits ERR\tfatal: not a git repository), and git's top-level flag query (-) returns nothing. Static specs are solid (cargo flags, gh subcommands, docker levels). Recorded as known limitation / follow-up.

Implementation

  • New crates/niubash-runtime/src/completion/carapace.rs: CarapaceCompletionPlugin registered behind the existing chain.
    • Discovery: NIU_CARAPACE (explicit path; off/0/false disables), then carapace[.exe] on PATH, then bundle locations (<exe_dir>\carapace.exe, <exe_dir>\opt\carapace\carapace.exe).
    • Latency mitigation: per-session cache keyed by (command, preceding words, word shape, cwd), 5-minute TTL. Ordinary words query with an empty last word (full list fetched once) and are prefix-filtered locally, so git ch → git che keystrokes hit the same cache entry — one spawn per (command, args) shape, not per keystroke. Flag-shaped words query with - and filter locally. Cache cleared on cd because dynamic specs (git branch lists…) are cwd-sensitive.
    • Hard failure ceiling: 300 ms spawn timeout (poll try_wait), silent spawn/parse errors, stderr discarded — a missing/broken carapace is zero user-visible error.
    • Priority: a coverage set shared with CompletionState records every command that has a local definition; the carapace source stands down for them. Winuxcmd applet TOMLs, bundle definitions and user completion files always win; carapace covers the long tail (verified: ls --a output identical with and without carapace).
  • Descriptions flow through CompletionResult::with_descriptions → reedline suggestions, i.e. straight into the [feature] 交互体验对照三大 shell 的缺失清单:abbr/瞬态提示/描述菜单/fzf 式搜索/补全描述——逐项补齐 #187 description menu.
  • CHANGELOG.md entry under Unreleased.

Layered delivery

Landed: discovery, fish-format integration, caching, timeout/degradation, priority gate, tests, changelog.
Deferred / follow-up:

  • Bundle decision (needs owner sign-off, not done here): adding carapace to scripts/release/preinstall.json — data above (87 MB / 17 MB, MIT). Recommendation: ship via wpm on x64 only, or expose a winuxcmd wpm install carapace hint instead of preinstalling, given the size.
  • Git nested-context quirk (upstream CLI behavior), async (non-blocking) completion, disk-persistent cache.
  • All live tests (live_invocation_*) skip automatically when no carapace binary is installed, so CI without the spike stays green.

Verification

  • git ch<TAB> via niu --completion-probe "git ch" with NIU_CARAPACE set → check-attr / check-ignore / … / cherry-pick (carapace candidates); without carapace → unchanged legacy behavior (path completer).
  • cargo test -p niubash-runtime --lib completion → 52/52 green (11 new carapace unit tests + live registration test).
  • Full workspace suite: all green except host_contract::closed_stdout_pipe_does_not_print_broken_pipe_error and 3 plugins::distros offline tests, all verified failing on clean origin/master (pre-existing, unrelated).
  • cargo fmt --check clean.

…commands

Wire carapace-bin (MIT, ~1200 community specs, niubash#187 description
menu) in behind the existing completion chain:

- CarapaceCompletionPlugin: NIU_CARAPACE / PATH / bundle discovery,
  fish-format parsing (value<TAB>Description), 300ms hard timeout,
  silent degradation, per-session cache keyed by (command, preceding
  words, word shape, cwd) with 5-minute TTL — a warm call costs
  104-146ms, so ordinary words fetch the full list once with an empty
  query word and are prefix-filtered locally per keystroke.
- Coverage gate: winuxcmd applet TOMLs, bundle definitions and user
  completion files always win; the carapace source only serves
  commands without a local definition.
- Tests: fish-format parsing, ERR/_ meta-line skipping, query-word
  shaping, prefix filtering with descriptions, coverage stand-down,
  discovery off-switch/explicit path, plus live end-to-end probes
  that skip when no carapace binary is installed.

Bundle decision deferred: 87MB exe (17MB packed) — adding it to
scripts/release/preinstall.json needs a separate owner call.
@caomengxuan666
caomengxuan666 force-pushed the feat/carapace-completions branch from 472b507 to f2a8f0e Compare October 10, 2026 04:45
@caomengxuan666
caomengxuan666 merged commit e8b7b5c into master Oct 10, 2026
@caomengxuan666
caomengxuan666 deleted the feat/carapace-completions branch October 10, 2026 04:45
caomengxuan666 added a commit that referenced this pull request Oct 10, 2026
…notes draft (#254)

- Version 1.3.4 -> 1.4.0 (workspace root + crates/niubash-runtime;
  Cargo.lock synced; internal niubash-runtime dep requirement aligned
  1.2.5 -> 1.4.0). The rubash engine requirement stays 1.3.4: it is
  semver-open (^1.3.4 admits the engine's 1.4.0), and the committed
  [patch] resolves it to the local ../rubash worktree, whose Cargo.toml
  still says 1.3.4 until the engine cuts its own release.
- CHANGELOG: the Unreleased section becomes [1.4.0] - 2026-10-10. Also
  repairs the committed merge damage from #243 (stray conflict marker
  and the #241 doctor-advice entry glued onto the carapace entry) and
  folds in the merged cycle: #235 #239 #246 #247 #250 #251 #252 #253
  plus a compact rubash 1.4.0 engine summary. #240's P2-batch content
  stays under [1.3.4] where the file already records it (no dup).
- docs/release-notes-1.4.0-draft.md: zh+en draft release notes
  (Added / Fixed / Performance), winuxcmd #1151/#1154-#1157 marked as
  in-flight for the coordinator, plus the local verification log:
  release build PASS (niu.exe 10.6 MB), MSIX packed unsigned with a
  placeholder identity (5.1 MB), wpm-source main carries all four
  preinstall packages, and cargo publish --dry-run chain findings
  (rubash -> niubash-runtime -> niubash, blocked on the engine's
  crates.io release).

No tag, no release workflow trigger, no in-flight PRs merged.
caomengxuan666 added a commit that referenced this pull request Oct 12, 2026
… integrate through the rc data layer

Architecture correction before 1.4.0 (owner ruling 2026-10-12): no shell
hardcodes a third-party completion provider into its source. #243 compiled
CarapaceCompletionPlugin (NIU_CARAPACE discovery, Go-binary output parsing,
300 ms timeout, 5-min TTL session cache, winuxcmd-TOML yield logic) into the
completion core; the provider belongs in the rc DATA layer, integrating
through the protocol the shell already speaks.

Removed: crates/niubash-runtime/src/completion/carapace.rs and every hook
(mod declaration, CarapaceCoverage plumbing in completer.rs, the live
registration test). NIU_CARAPACE never shipped in a tag, so zero
compatibility debt.

The protocol layer is unchanged — the three native surfaces still stand:
`complete` compspecs (rubash engine), NIU_COMPDEFS compdef functions, and
winuxcmd applet TOMLs / completion definition files.

Put back at the data layer, per the standard bash-completion practice:

- niu setup grows an opt-in question (default OFF) that appends the standard
  guarded carapace-bin rc snippet — the same block a bash user writes by
  hand, with the install requirement commented; niu ships and installs no
  provider.
- New docs page "External Completion Providers" (SUMMARY-linked): the
  protocol surfaces, priority rules, carapace-bin as one example, and an
  honest status note — complete -F/-C hook functions resolve in-script
  (compgen -F) but their interactive Tab resolution remains a rubash engine
  follow-up; function providers speak the compdef protocol today.
- Binary-level tests pin the rc route: `complete -W` wordlists and compdef
  hooks sourced from ~/.niubashrc serve Tab candidates through the real
  startup sequence, and a standard .bash completion script serves flag
  candidates through the bash-import channel. The --completion-probe now
  mirrors the REPL startup (loads compdefs, installs the completion bridge),
  which is what makes those probes faithful.
- CHANGELOG 1.4.0 records #243 as the architecture correction it was.

cargo test --no-fail-fast failure set is identical to the origin/master
baseline run on the same machine (environment-bound interactive/ConPTY
failures only); cargo fmt --check clean.
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
… integrate through the rc data layer (#278)

* refactor(completion): retire the in-core carapace provider; providers integrate through the rc data layer

Architecture correction before 1.4.0 (owner ruling 2026-10-12): no shell
hardcodes a third-party completion provider into its source. #243 compiled
CarapaceCompletionPlugin (NIU_CARAPACE discovery, Go-binary output parsing,
300 ms timeout, 5-min TTL session cache, winuxcmd-TOML yield logic) into the
completion core; the provider belongs in the rc DATA layer, integrating
through the protocol the shell already speaks.

Removed: crates/niubash-runtime/src/completion/carapace.rs and every hook
(mod declaration, CarapaceCoverage plumbing in completer.rs, the live
registration test). NIU_CARAPACE never shipped in a tag, so zero
compatibility debt.

The protocol layer is unchanged — the three native surfaces still stand:
`complete` compspecs (rubash engine), NIU_COMPDEFS compdef functions, and
winuxcmd applet TOMLs / completion definition files.

Put back at the data layer, per the standard bash-completion practice:

- niu setup grows an opt-in question (default OFF) that appends the standard
  guarded carapace-bin rc snippet — the same block a bash user writes by
  hand, with the install requirement commented; niu ships and installs no
  provider.
- New docs page "External Completion Providers" (SUMMARY-linked): the
  protocol surfaces, priority rules, carapace-bin as one example, and an
  honest status note — complete -F/-C hook functions resolve in-script
  (compgen -F) but their interactive Tab resolution remains a rubash engine
  follow-up; function providers speak the compdef protocol today.
- Binary-level tests pin the rc route: `complete -W` wordlists and compdef
  hooks sourced from ~/.niubashrc serve Tab candidates through the real
  startup sequence, and a standard .bash completion script serves flag
  candidates through the bash-import channel. The --completion-probe now
  mirrors the REPL startup (loads compdefs, installs the completion bridge),
  which is what makes those probes faithful.
- CHANGELOG 1.4.0 records #243 as the architecture correction it was.

cargo test --no-fail-fast failure set is identical to the origin/master
baseline run on the same machine (environment-bound interactive/ConPTY
failures only); cargo fmt --check clean.

* feat(history): expose Shell::flush_session_history for explicit session-end writes

Public API retention after the #281 ruling: the rc-0 one-shot exits return
from main normally, so the provider's Drop flush carries the session-end
history write on every standard path and the funnel never calls this —
the method exists for embedding hosts that terminate through
std::process::exit on paths the funnel does not own (a hard exit skips
Drop, so the write must be made explicitly). Best-effort like every
history-file write: never fails, never reaches stderr.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature] 交互体验对照三大 shell 的缺失清单:abbr/瞬态提示/描述菜单/fzf 式搜索/补全描述——逐项补齐

1 participant