Skip to content

perf(cli): avoid duplicate load_session in auth status (#146)#153

Open
adriannoes wants to merge 2 commits into
devfrom
perf/146-auth-status-prefetch
Open

perf(cli): avoid duplicate load_session in auth status (#146)#153
adriannoes wants to merge 2 commits into
devfrom
perf/146-auth-status-prefetch

Conversation

@adriannoes

Copy link
Copy Markdown
Collaborator

Summary

  • Add optional prefetched_session to get_authenticated_client so the stored-session branch skips a second ensure_fresh_session / keychain read when the caller already refreshed the session.
  • pipefy auth status preloads the session once, reuses it for source detection and population, and passes the refreshed session into identity fetch.
  • Add regression tests asserting a single load_session per auth status invocation on the stored-session path.

Test plan

  • uv run pytest packages/cli/tests/test_auth_status.py packages/cli/tests/test_auth.py -m "not integration"
  • uv run ruff check / ruff format on touched files

Closes #146

@adriannoes adriannoes self-assigned this May 21, 2026
Always run detect_auth_source before using prefetched_session so bearer
and service-account tiers are not bypassed; pass cached_stored_session
into detection to preserve the single load_session contract for auth status.

@gbrlcustodio gbrlcustodio left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Optimization is sound and the second commit correctly preserves bearer/service-account precedence (locked in by test_prefetched_session_does_not_override_bearer_precedence). Main feedback inline.

chore: PR is CONFLICTING against dev — needs a rebase before merge (likely the describe_missing_oauth_varsdescribe_missing_service_account_vars rename landing on a parallel branch).

load_session,
)

_SESSION_FRESHNESS_LEEWAY_S = 60

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

issue (non-blocking): duplicates _LEEWAY_S = 60 from oauth/refresh.py:13. If either drifts, the fresh-path predicate here and the stale-check inside ensure_fresh_session will silently disagree.

suggestion: have ensure_fresh_session accept an optional session: StoredSession | None = None and skip the inner load_session when supplied. Then _populate_stored_session just calls ensure_fresh_session(session=loaded_session, ...) — freshness predicate + leeway stay in one place, _session_is_fresh goes away, and the stale path collapses to 1 load too (currently still 2).

if cached_stored_session is not None
else load_session(
issuer=auth.oidc_client.issuer_url,
client_id=auth.oidc_client.client_id,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nitpick: correct (the is not None binds to load_session(...), not the whole ternary) but a re-read. Splitting helps:

if cached_stored_session is None:
    cached_stored_session = (
        load_session(issuer=auth.oidc_client.issuer_url, client_id=auth.oidc_client.client_id)
        is not None
    )
if cached_stored_session:
    sources.append("stored-session")

source = detect_auth_source(
pipefy_settings,
auth,
cached_stored_session=True if prefetched_session is not None else None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nitpick (if-minor): cached_stored_session=prefetched_session is not None or None says the same thing without the ternary.

AuthContext,
AuthSource,
OidcClient,
_session_is_fresh,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nitpick: importing _session_is_fresh across module boundaries breaks the leading-underscore "module-private" convention. Folds away if ensure_fresh_session(session=...) absorbs the optimization — see the comment on auth.py:43.

auth,
cached_stored_session=loaded_session is not None
if auth.oidc_client is not None
else None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

suggestion (if-minor): the if auth.oidc_client is not None else None arm never fires usefully — detect_all_sources skips the cached_stored_session consultation entirely when oidc_client is None (auth.py:146). Just:

cached_stored_session=loaded_session is not None,

is equivalent and one line shorter.

"pipefy_cli.oauth.refresh.load_session",
"pipefy_cli.commands.auth.load_session",
"pipefy_cli.auth.load_session",
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

note (non-blocking): silently under-counts if a 5th importer is added later — the new site bypasses the counter and load_calls == 1 still holds. A docstring calling that out would be enough; not a blocker.

"Refresh failed (HTTP 400): invalid_grant", error_code="invalid_grant"
),
),
patch("pipefy_cli.commands.auth._session_is_fresh", return_value=False),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

suggestion: patching _session_is_fresh couples the test to a private helper. Seeding a stale session exercises the real freshness path instead:

_seed_session(monkeypatch, obtained_at=int(time.time()) - 7200)

Same applies to lines 231 and 258. Folds away entirely if ensure_fresh_session(session=...) lands.

@gbrlcustodio gbrlcustodio added this to the User auth milestone May 28, 2026
Base automatically changed from dev to main June 2, 2026 14:39
@gbrlcustodio gbrlcustodio changed the base branch from main to dev June 2, 2026 15:18
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.

2 participants