Skip to content
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

Allow clients to redefine untitled files protocol during their creation #1903

Merged
merged 2 commits into from
Jul 31, 2024

Conversation

pkukielka
Copy link
Contributor

Closes https://linear.app/sourcegraph/issue/CODY-2004/bug-cody-recreates-the-file-that-was-created-by-generate-unit-tests

Changes

Some IDEs (maybe even most IDEs except VSC) does not have notion of unsaved files.
Keeping two different models in IDE and in the agent is problematic and leads to hard to track errors.
It is easier to let client be honest and notify agent about real protocol which it is using (most likely file:// instead of untitled://).

As a result that change slightly modifies behaviour of the openTextDocument.
One could now call openTextDocument with untitled://abc parameter, and as a result get a TextDocument which uri points to file://abc (or any other path, really).

Test plan

  1. Build JetBrains with this PR using CODY_DIR env var and this PR
  2. Ask cody to generate tests for some file which does not have tests yet
  3. Accept the changes
  4. Delete that newly created file
  5. Perform autocomplete in some other file
  6. Make sure test file was not recreated

@mkondratek
Copy link
Contributor

image

I found a problem - accept lens group appears too quickly (while the file content is still being generated)


// For the sake of correctness we only allow untitled files to be created.
// If we receive a URI with a scheme other than "untitled" it should already exist on the disk
if (uri.scheme != "untitled") return null
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not like this return statement and the fact we need the comment. If this method assumes the file does not exist we should simply throw an exception and handle it higher.

Or maybe we could just do this check where this method is called - in src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentService.kt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Returning null is consistent with other IJ file APIs where if file cannot be created it returns a null (e.g. ScratchRootType::createScratchFile)
As for comment I'm open for discussion.
It is not needed to understand this function, but I think it's a domain knowledge detail to understand that file:// coming from VSC is almost certainly existing file while untitiled:// is not. And as such it might deserve a comment, perhaps on the top of the function to document it.

Copy link
Contributor

Choose a reason for hiding this comment

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

if it's consistent with IJ let it be as it is 🆗

@pkukielka pkukielka force-pushed the pkukielka/fix-untitled-files-protocol branch from 7695efd to 2b590f6 Compare July 16, 2024 13:21
pkukielka added a commit to sourcegraph/cody that referenced this pull request Jul 31, 2024
…on (#4841)

Closes
https://linear.app/sourcegraph/issue/CODY-2004/bug-cody-recreates-the-file-that-was-created-by-generate-unit-tests

## Changes

Some IDEs (maybe even most IDEs except VSC) does not have notion of
unsaved files.
Keeping two different models in IDE and in the agent is problematic and
leads to hard to track errors.
It is easier to let client be honest and notify agent about real
protocol which it is using (most likely `file://` instead of
`untitled://`).

As a result that change slightly modifies behaviour of the
`openTextDocument`.
One could now call `openTextDocument` with `untitled://abc` parameter,
and as a result get a `TextDocument` which uri points to `file://abc`
(or any other path, really).

## Test plan

1. Build [JetBrains branch
](sourcegraph/jetbrains#1903 this PR using
CODY_DIR env var
2. Ask cody to generate tests for some file which does not have tests
yet
3. Accept the changes
4. Delete that newly created file
5. Perform autocomplete in some other file
6. Make sure test file was not recreated

<!-- Required. See
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles.
-->
@pkukielka pkukielka force-pushed the pkukielka/fix-untitled-files-protocol branch from 2b590f6 to 350704f Compare July 31, 2024 12:13
@pkukielka pkukielka force-pushed the pkukielka/fix-untitled-files-protocol branch from 350704f to 0b94299 Compare July 31, 2024 12:43
@pkukielka pkukielka merged commit 395e971 into main Jul 31, 2024
6 of 7 checks passed
@pkukielka pkukielka deleted the pkukielka/fix-untitled-files-protocol branch July 31, 2024 12:53
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