Skip to content

fix: memoize ISP auth to eliminate duplicate auth cycles#43

Merged
aaearon merged 2 commits intomainfrom
fix/single-auth-per-invocation
Apr 21, 2026
Merged

fix: memoize ISP auth to eliminate duplicate auth cycles#43
aaearon merged 2 commits intomainfrom
fix/single-auth-per-invocation

Conversation

@aaearon
Copy link
Copy Markdown
Owner

@aaearon aaearon commented Apr 21, 2026

Summary

  • grant request submit was performing 2–3 full ISP auth cycles before the workspace picker appeared (~2.5s overhead), one per bootstrapSCAService / bootstrapWorkflowsService call
  • Added bootstrapISPAuth() with sync.Once memoization so profile load + Authenticate runs exactly once per process, shared by both bootstrap helpers
  • Changed refreshAuth from truefalse so cached, unexpired keyring tokens short-circuit the network round-trip entirely (verified by reading idsec_auth.go:144refreshAuth=true forced a live refresh even on valid tokens)
  • bootstrapImpl is a var func for test overriding without touching sync.Once

Result (verified via --verbose)

Before After
StartAuthentication calls 2–3×
Keyring ops ~18× ~6×
Time to workspace picker ~2.5s <300ms (warm token)

Test plan

  • make build
  • make test — new TestBootstrapISPAuth_MemoizesAcrossServiceBootstraps asserts bootstrapImpl called exactly once across 3 bootstrapISPAuth() invocations
  • make lint
  • Manual: ./grant request submit --verbose — single StartAuthentication in log, workspace picker appears immediately

…uest submit

bootstrapISPAuth() now uses sync.Once so profile load + Authenticate runs
exactly once per process, shared by bootstrapSCAService and
bootstrapWorkflowsService. Also passes refreshAuth=false so cached, unexpired
keyring tokens skip the network round-trip entirely.

Reduces StartAuthentication calls from 3 to 1 for `grant request submit` and
cuts keyring ops from ~18 to ~6 per invocation.
Copilot AI review requested due to automatic review settings April 21, 2026 06:29
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR reduces startup latency for commands like grant request submit by memoizing ISP profile load + authentication so multiple service bootstraps within a single CLI invocation reuse the same auth cycle and can reuse cached keyring tokens.

Changes:

  • Introduces a bootstrapISPAuth() helper in cmd/root.go that memoizes ISP auth/profile via sync.Once, shared across service bootstraps.
  • Updates bootstrapWorkflowsService() to reuse the memoized ISP auth instead of re-authenticating.
  • Adds a unit test asserting memoization behavior, and documents the fix in the changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
cmd/root.go Adds process-wide memoization for ISP auth/profile and wires it into SCA service bootstrap.
cmd/root_test.go Adds a unit test verifying memoization across repeated bootstrapISPAuth() calls.
cmd/request.go Switches workflows bootstrap to reuse the memoized ISP auth.
CHANGELOG.md Documents the authentication-cycle reduction and performance impact.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/root_test.go Outdated
t.Cleanup(func() { bootstrapImpl = origImpl })

calls := 0
stubAuth := sdkauth.NewIdsecISPAuth(true) // never Authenticate'd — used only for identity
Comment thread cmd/root.go Outdated
Comment on lines +166 to +168
// resetBootstrapCache clears the memoized auth state. Intended for tests.
func resetBootstrapCache() {
bootstrapOnce = sync.Once{}
- Use *sync.Once (pointer) instead of sync.Once value to avoid copylocks
  concern when resetting in tests
- Use NewIdsecISPAuth(false) in test stub (non-caching; Authenticate never
  called so no keyring ops in CI)
- Rename test to TestBootstrapISPAuth_MemoizesRepeatCalls to accurately
  describe what is tested
@aaearon aaearon merged commit 7cbdd38 into main Apr 21, 2026
1 check passed
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