Skip to content

Respect explicit -o when stdout is piped; rename events --effective-exposures#36

Merged
joalves merged 3 commits into
mainfrom
cli/output-piping-and-valid-exposures
May 20, 2026
Merged

Respect explicit -o when stdout is piped; rename events --effective-exposures#36
joalves merged 3 commits into
mainfrom
cli/output-piping-and-valid-exposures

Conversation

@joalves
Copy link
Copy Markdown
Collaborator

@joalves joalves commented May 20, 2026

Summary

Two unrelated changes bundled (per request):

  • fix(cli): Lists fell back to ids-only when stdout was piped, even with an explicit -o json. The piping guard read the per-subcommand options.output, but -o, --output is a global option, so that local value is always undefined — making the "explicit?" check always false. Detect the explicit flag once in getGlobalOptions via commander's getOptionValueSourceWithGlobals, expose it as outputExplicit, and route the rule through a shared shouldOutputIdsOnly helper so every list command behaves the same way. Also excludes .worktrees/** from vitest discovery so sibling worktree checkouts don't appear as duplicate (stale) test files.
  • feat(events): Split events list --effective-exposures into a mutually-exclusive --valid-exposures / --invalid-exposures pair so users can explicitly request the exposures that were filtered OUT, not just the ones that were kept. The tri-state is plumbed through to the API's effective_exposures filter.

Test plan

  • abs --profile demo-2 metrics list --items 200 -o json | cat returns JSON (was: ids only) — covered by new regression test in list-command.test.ts
  • Piped without -o still emits ids only
  • -o ids still emits ids on a TTY
  • events list --valid-exposures sends effective_exposures: true
  • events list --invalid-exposures sends effective_exposures: false
  • events list --valid-exposures --invalid-exposures errors with mutual-exclusion message
  • npm run build succeeds
  • npm test — all 2377 passing (worktree noise excluded by vitest.config.ts change)

Summary by CodeRabbit

  • Documentation

    • Updated Events documentation to reflect new exposure filter flags.
  • New Features

    • Added mutually exclusive --valid-exposures and --invalid-exposures for events list filtering.
  • Tests

    • Expanded tests for the new exposure flags and list output behaviour when stdout is piped.
  • Chores

    • Centralised IDs-only output decision and bumped package version; updated test discovery to ignore git worktrees.

Review Change Stack

joalves added 2 commits May 20, 2026 18:21
…valid-exposures

Replace the single boolean --effective-exposures flag on `events list`
with a mutually exclusive --valid-exposures / --invalid-exposures pair,
so users can explicitly request the exposures that were filtered OUT
as ineffective — not just the ones that were kept.

The tri-state (true / false / undefined) is plumbed through ListEvents
to the `effective_exposures` filter so the API can distinguish "only
valid", "only invalid", and "unfiltered".
List commands fell back to ids-only output whenever stdout was piped,
even when the user explicitly passed `-o json`. The piping guard read
`options.output` on the subcommand, but `-o, --output` is a global
option, so the local value is always undefined — making the "explicit?"
check always false.

Detect the explicit flag once in `getGlobalOptions` via commander's
option-value source, expose it as `outputExplicit`, and route the
piping rule through a shared `shouldOutputIdsOnly` helper so the
behavior stays consistent across every list command.

Also exclude `.worktrees/**` from vitest discovery so sibling worktree
checkouts don't appear as duplicate (stale) test files.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ff97d264-c46b-473a-89e5-065dcc777d9b

📥 Commits

Reviewing files that changed from the base of the PR and between dce308a and 3adcaa1.

📒 Files selected for processing (1)
  • package.json
✅ Files skipped from review due to trivial changes (1)
  • package.json

Walkthrough

This PR introduces two parallel improvements: a refactoring of the events command's exposure filtering strategy, and an abstraction of piped-output behaviour common to list commands. The events command replaces its single --effective-exposures flag with mutually exclusive --valid-exposures and --invalid-exposures flags, flowing through the CLI handler, core API, and tests. Separately, a new shouldOutputIdsOnly helper centralises stdout-piping logic previously duplicated across list commands, tracking explicit user output choices via a new outputExplicit flag. Both changes are validated by comprehensive test coverage and integrated into existing commands. A minor Vitest configuration update improves test discovery in multi-worktree repositories.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 With flags now split and helpers born anew,
Events filter true, and false rings clear,
Piped output knows just what to do,
No duplication here, no fear!
The worktrees dance, configuration blessed.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes both main changes: piping-aware output handling and the events flag rename, making it specific and relevant to the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cli/output-piping-and-valid-exposures

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/core/events/events.ts (1)

38-48: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Keep effectiveExposures as a backward-compatible alias in the core params.

Renaming the exported ListEventsParams field is a breaking API change for @absmartly/cli/core/events consumers. Existing callers using effectiveExposures will lose this filter silently in JS.

Suggested patch
 export interface ListEventsParams {
   from?: number | undefined;
   to?: number | undefined;
   applications?: number[] | undefined;
   unitTypes?: number[] | undefined;
   eventTypes?: string[] | undefined;
   eventNames?: string[] | undefined;
   unitUids?: string[] | undefined;
   environmentTypes?: string[] | undefined;
   validExposures?: boolean | undefined;
+  /** `@deprecated` Use validExposures */
+  effectiveExposures?: boolean | undefined;
   take?: number | undefined;
   skip?: number | undefined;
 }
@@
-  if (params.validExposures !== undefined) filters.effective_exposures = params.validExposures;
+  const effectiveExposures = params.validExposures ?? params.effectiveExposures;
+  if (effectiveExposures !== undefined) filters.effective_exposures = effectiveExposures;

Also applies to: 67-67

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/core/events/events.ts` around lines 38 - 48, ListEventsParams removed the
previously exported effectiveExposures field causing a breaking change; restore
backwards compatibility by adding effectiveExposures?: boolean | undefined to
the ListEventsParams interface (alongside the new validExposures?: boolean |
undefined) and update any code that reads the filter (places that accept
ListEventsParams) to treat effectiveExposures as an alias for validExposures
(e.g., coalesce params.effectiveExposures ?? params.validExposures) so callers
using either name behave identically.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/commands/events/index.ts`:
- Around line 60-65: The mutually-exclusive flag check runs after initialising
the API client; move the validation so it runs immediately after computing
globalOptions and before calling getAPIClientFromOptions. Specifically, in the
handler that currently calls getGlobalOptions(listCommand) and then
getAPIClientFromOptions(globalOptions), perform the if (options.validExposures
&& options.invalidExposures) throw new Error(...) check right after
getGlobalOptions(listCommand) (or even before that if options are available),
and only call getAPIClientFromOptions(globalOptions) once the flags validation
passes to avoid unnecessary client initialisation.

---

Outside diff comments:
In `@src/core/events/events.ts`:
- Around line 38-48: ListEventsParams removed the previously exported
effectiveExposures field causing a breaking change; restore backwards
compatibility by adding effectiveExposures?: boolean | undefined to the
ListEventsParams interface (alongside the new validExposures?: boolean |
undefined) and update any code that reads the filter (places that accept
ListEventsParams) to treat effectiveExposures as an alias for validExposures
(e.g., coalesce params.effectiveExposures ?? params.validExposures) so callers
using either name behave identically.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 71b7048d-7d15-4f0d-a09c-cc057cf40c43

📥 Commits

Reviewing files that changed from the base of the PR and between 7cc838c and dce308a.

📒 Files selected for processing (11)
  • README.md
  • src/commands/events/events.test.ts
  • src/commands/events/index.ts
  • src/commands/experiments/list.ts
  • src/core/events/events.test.ts
  • src/core/events/events.ts
  • src/lib/utils/api-helper.test.ts
  • src/lib/utils/api-helper.ts
  • src/lib/utils/list-command.test.ts
  • src/lib/utils/list-command.ts
  • vitest.config.ts

Comment on lines 60 to +65
const globalOptions = getGlobalOptions(listCommand);
const client = await getAPIClientFromOptions(globalOptions);

if (options.validExposures && options.invalidExposures) {
throw new Error('--valid-exposures and --invalid-exposures are mutually exclusive');
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Validate mutually exclusive flags before client initialisation.

With invalid combined flags, the handler still initialises the API client first. That can surface unrelated auth/config errors instead of the intended usage error.

Suggested patch
   .action(
     withErrorHandling(async (options) => {
       const globalOptions = getGlobalOptions(listCommand);
-      const client = await getAPIClientFromOptions(globalOptions);

       if (options.validExposures && options.invalidExposures) {
         throw new Error('--valid-exposures and --invalid-exposures are mutually exclusive');
       }
+      const client = await getAPIClientFromOptions(globalOptions);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const globalOptions = getGlobalOptions(listCommand);
const client = await getAPIClientFromOptions(globalOptions);
if (options.validExposures && options.invalidExposures) {
throw new Error('--valid-exposures and --invalid-exposures are mutually exclusive');
}
const globalOptions = getGlobalOptions(listCommand);
if (options.validExposures && options.invalidExposures) {
throw new Error('--valid-exposures and --invalid-exposures are mutually exclusive');
}
const client = await getAPIClientFromOptions(globalOptions);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/commands/events/index.ts` around lines 60 - 65, The mutually-exclusive
flag check runs after initialising the API client; move the validation so it
runs immediately after computing globalOptions and before calling
getAPIClientFromOptions. Specifically, in the handler that currently calls
getGlobalOptions(listCommand) and then getAPIClientFromOptions(globalOptions),
perform the if (options.validExposures && options.invalidExposures) throw new
Error(...) check right after getGlobalOptions(listCommand) (or even before that
if options are available), and only call getAPIClientFromOptions(globalOptions)
once the flags validation passes to avoid unnecessary client initialisation.

@joalves joalves added this pull request to the merge queue May 20, 2026
Merged via the queue into main with commit 5f73cec May 20, 2026
5 checks passed
@joalves joalves deleted the cli/output-piping-and-valid-exposures branch May 20, 2026 20:54
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.

1 participant