Skip to content

Expose is_authenticated() helper #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 28, 2025
Merged

Conversation

cosmicbuffalo
Copy link
Contributor

Hi @zbirenbaum, great plugin you've got here! I'm wondering if you'd be interested in merging something like this to solve a problem I'm running into.

I'm currently working on a neovim distro for my company that has a variety of users, and some don't want to auth into copilot for various reasons. These users would prefer that the plugin be disabled by default, because without authenticating, the plugin produces some noisy errors. I don't want to disable the plugin by default though, since the majority of users do and expect it to work automatically with our config without any manual tweaks on their part.

So my idea was to set up some more graceful default behavior for when a user is not authenticated, since ideally the plugin should be enabled and available to auth for anyone that wants to use it, but not noisily complain for anyone that opts not to use it. To do this, I threw in this is_authenticated() function that checks for tokens or the creds file, and added an optional argument to get_creds() to avoid pinging notifications when this is hit. I then added this to the guard at the top of the suggestion schedule function so that suggestions don't get scheduled when is_authenticated() is false.

I'm not sure if this is the best approach to solve the problem I described, but this seems to be working so far

@AntoineGS
Copy link
Collaborator

Hey, zbirenbaum rarely comes around these days but I do like the idea. :)
Instead of looking for the file/env variable, would requesting the login info from the LSP possible?
The check_status endpoint would give that information.

It's been commented a few times that the plugin is too verbose when it is not authenticated or internet is unavailable, ideally this would move the plugin towards a more silent direction.
That said, we would need to move whatever is silenced to :checkhealth.

Note that I am not requesting that you go ahead and move all notifications to :checkhealth (unless you are interested in undertaking a bigger task), just the 'not logged in' notifications are fine so that I may eventually move the rest in a similar fashion :)

@cosmicbuffalo
Copy link
Contributor Author

@AntoineGS that sounds like a good idea to me, I can look into making that change. That's a good point on moving the silenced pings to :checkhealth too, I'll see what I can do 👍

And thanks for the quick feedback!

local error = vim.health.error or vim.health.report_error
local info = vim.health.info or vim.health.report_info

function M.check()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's a screenshot of what this current healthcheck setup looks like:
Screenshot 2025-07-25 at 12 19 47 PM

end
end

function M.is_authenticated()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AntoineGS so I moved this over to use the lsp check_status endpoint. since it's async I threw some caching in here that tries to get the initial response from the lsp back ASAP, but I'm not super familiar with lsp communication setups like this so let me know if there's something better I can do here!

@AntoineGS
Copy link
Collaborator

I like it!
I do see a bunch of failing tests though (I have to run them on my fork since I do not have access to set them up on this repo: https://github.com/AntoineGS/copilot.lua/actions/runs/16530632499

@cosmicbuffalo
Copy link
Contributor Author

I do see a bunch of failing tests though

Hmm, okay I'll have to dig into those and see what's going on. I'm probably missing some test setup around the expectations for the async lsp response

Nicholas deLannoy added 2 commits July 25, 2025 21:51
it looks like the suggestions tests are failing because the new
is_authenticated check on schedule is blocking initial suggestions, I'll
have to tweak my approach a bit to preserve existing speedy behavior for
authenticated users
@@ -105,6 +105,14 @@ function M.new_child_neovim(test_name)
]])
end

function child.wait_for_lsp_authentication()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AntoineGS I was able to get the rest of the tests passing with this new helper that waits until is_authenticated() is true

@cosmicbuffalo cosmicbuffalo marked this pull request as ready for review July 25, 2025 22:06
@AntoineGS
Copy link
Collaborator

Tests look good, I will run it for a bit and merge if it looks good!

@@ -484,7 +485,7 @@ local function advance(count, ctx)
end

local function schedule(ctx)
if not is_enabled() or not c.initialized then
if not is_enabled() or not c.initialized or not auth.is_authenticated() then
Copy link
Collaborator

Choose a reason for hiding this comment

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

Reviewing the new flow and taking more time to understand why the tests were failing and how they were resolved, I can see that it is possible for Copilot to be ready and logged in but still not yield a suggestion, until the authentication is confirmed.
This will delay the first suggestion on a daily use -- slightly, but it may not be necessary.
If the first call to is_authenticated were to be synchronous (you could detect this by an empty cache timestamp) I believe it would solve this.
This would mean that the new changes to child_helper would not be needed, and removing it would be a way to test that the change works.

@AntoineGS
Copy link
Collaborator

I have reverted the last commit you had made adding the waits in the tests, and I think I found a good way to handle this.
Whenever the check_status API needs to be called, it will abort the current schedule() call (like it did already), however it will then queue another request to scheduler() once it receives the reply from the API.
So from a user perspective it is still the same initial request that went through.

Let me know what you think!

@cosmicbuffalo
Copy link
Contributor Author

@AntoineGS I think your changes look great, I was thinking at the time that it'd be nice if there were a way to avoid needing to retry directly after the first auth attempt and your callback approach addresses that perfectly. I went through it again this morning and everything looks good on my end!

@AntoineGS AntoineGS merged commit 8c4b70d into zbirenbaum:master Jul 28, 2025
@cosmicbuffalo
Copy link
Contributor Author

hey @AntoineGS thanks for merging my PR! hopefully this PR isn't the root cause, but I'm noticing after updating my plugins that copilot is having some weird behavior.

I'm seeing the client.get() function returning empty for some reason, and it looks like it might be because of changes in this other recent PR: #521

I think there might need to be another call to ensure_client_started somewhere, since I'm not seeing the client start as part of my typical workflow anymore. If I run a command like Copilot status it'll start though, thanks to the call to use_client in the user command here:

require("copilot.client").use_client(function()

@AntoineGS
Copy link
Collaborator

Yeah someone commented on the commit so I reverted it right away, I am looking into it now.
Thanks for the info though it helps!

@AntoineGS
Copy link
Collaborator

Hopefully the version I just pushed is fine!

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