Skip to content

fix(cloud): push project libraries when using --push in cloud commands - #725

Open
pcbeingused333 wants to merge 4 commits into
QuantConnect:masterfrom
pcbeingused333:fix/cloud-push-libraries
Open

pcbeingused333 wants to merge 4 commits into
QuantConnect:masterfrom
pcbeingused333:fix/cloud-push-libraries

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #719

lean cloud backtest/optimize/live deploy <project> --push resolve the project through CloudProjectManager.get_cloud_project, which pushed only the project and not the libraries it references. So the cloud compiled and ran the project against the libraries' older cloud code. lean cloud push doesn't have this problem, because PushManager.push_project pushes the project's libraries too.

Change

--push now pushes like lean cloud push. Both push branches of get_cloud_project (a local project directory, and a cloud name or id resolved to a local directory) call PushManager.push_project, the same method lean cloud push uses. It pushes the project's libraries, nested ones included, before the project. As in lean cloud push, a library that can't be pushed gets a warning and the command goes on. Only a failed push of the project itself raises PushFailedError and stops the command before it compiles. push_project now returns whether the project was pushed. push_projects is unchanged.

Libraries that already match their cloud copy are not updated. Every push sends projects/update, which the cloud refuses from a user who can't write to the project, for example a library shared read-only or locked by another collaborator. Before updating a library, PushManager now compares its name, libraries, other metadata and files with the cloud copy, and leaves the library alone when nothing would change. An unchanged library the user can only read therefore no longer warns or fails. The file comparison:

  • leaves out the files of the library's own libraries, which files/read lists with the library's files (isLibrary)
  • treats a local file as unchanged when it equals the cloud copy, or what lean cloud pull writes for the cloud copy (unix line endings and a final newline). Pull's conversion moved into get_pulled_file_content in pull_manager.py, so pull and this check use the same code.

A library with any change, or with an encryption key, is always pushed. The check also applies to lean cloud push.

Known limitation, same as lean cloud push: if a library has local changes the user can't push, the command warns and the cloud runs with the library's current cloud code.

Tests

tests/components/cloud/test_cloud_project_manager.py, through the real push path with a mocked API:

  • a changed library is pushed before the project (by project name and by cloud id)
  • a library that matches its cloud copy is skipped, including when the cloud copy is the form a pull converted (no final newline, CRLF)
  • a library whose own libraries are listed with its files is skipped
  • a library whose files match is still pushed when something else changed: a local file added, a local file deleted, a library reference removed, a changed description, changed parameters
  • a changed library that can't be pushed logs the Cannot push warning, and the project is still pushed
  • a project that can't be pushed after its libraries raises PushFailedError

The existing get_cloud_project push tests now assert the push_project call. Each test above fails when the check it covers is removed. Full suite: 2954 passed, 75 skipped, 0 failed. static_analysis.py passes.

End-to-end

Run from this branch against the real API. A Python project LeanCli725E2E references the Python library Library/LeanCli725E2ELib, whose VERSION the algorithm reports as a runtime statistic. Both were pushed once with lean cloud push, then:

Run Library lean cloud backtest LeanCli725E2E --push
1 unchanged (v1) 'Library/LeanCli725E2ELib' is already up to date in the cloud, not pushing it, project pushed, backtest completed with Library Version: v1
1b cloud file rewritten in web IDE form (CRLF, no final newline), then lean cloud pull lean cloud push --project LeanCli725E2E skipped the library as up to date, and the cloud file kept its web IDE form
2 changed to v2 [1/2] library updated, [2/2] project updated, backtest completed with Library Version: v2
3 changed to v3, push fails Cannot push 'Library\LeanCli725E2ELib': ... warning, project still pushed, exit code 0, backtest completed with Library Version: v2, the library's cloud code

In run 3 the library push was made to fail locally, by marking the library encrypted without a key, which the CLI refuses before any request. The account used has a single user, so a cloud refusal of a read-only library wasn't reproduced. Both go through the same except in PushManager. The throwaway projects were deleted afterwards.

Supersedes #749.

This change was prepared with the help of an AI assistant (Claude), and the submitter is responsible for it.

🤖 Generated with Claude Code

@quantconnect-kronos

Copy link
Copy Markdown
Contributor

Thanks @pcbeingused333. Your fix commit is cherry-picked, with your authorship, into #749. That PR adapts the test to the current resolver, which now follows a project directory renamed by the push (#734), and adds tests that run the real push path for a changed library and for a failed library upload.

pcbeingused333 and others added 3 commits November 27, 2026 18:44
…cloud copy

A library the user can only read rejects every projects.update, so --push
on cloud backtest/optimize/live deploy failed for projects referencing one
even when the library had no local changes. Libraries whose files and
metadata already match the cloud copy are now left alone; a changed
library that fails to upload still stops the command.
…d push

The cloud commands now push through PushManager.push_project, the same path
as `lean cloud push`: a referenced library that cannot be pushed only gets a
warning, and only a failed push of the project itself stops the command.
push_project returns whether the project was pushed.

The check that skips libraries already matching their cloud copy now leaves
out the files of the library's own libraries, which files/read lists with
the library's files, and accepts a local file that is what `lean cloud pull`
wrote for the cloud copy (unix line endings and a final newline). Pull's
conversion moves into get_pulled_file_content so both use the same code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Martin-Molinero
Martin-Molinero force-pushed the fix/cloud-push-libraries branch from 8913a6f to 7b53b71 Compare November 27, 2026 21:54
@Martin-Molinero

Copy link
Copy Markdown
Member

Thanks for this fix. I rebased the branch on master (which now includes #687) and added a commit (7b53b71) with the decision on the open question from #749, about libraries the user can only read:

  • --push behaves like lean cloud push. Both push branches of get_cloud_project now call the existing PushManager.push_project, which pushes the project's libraries the same way. A referenced library that can't be pushed gets the usual warning and the command goes on. Only a failed push of the project itself raises PushFailedError. push_project now returns whether the project was pushed. push_projects is unchanged.
  • Skipping unchanged libraries, from your second commit, with two fixes to the comparison:
    • files/read lists the files of the library's own libraries (isLibrary) with its files, so a library that references other libraries never matched. Those entries are now left out.
    • lean cloud pull writes cloud files with unix line endings and a final newline, so a pulled library that wasn't changed never matched its cloud copy. A local file now also matches when it is what a pull of the cloud copy writes. Pull's conversion moved into get_pulled_file_content in pull_manager.py, so both use the same code.
  • The rebase dropped the two merge commits. The only change they carried, the push_and_rename stub in the live deploy test, is in the new commit. It now patches push_project.

New tests in test_cloud_project_manager.py, through the real push path:

  • a library whose cloud copy is the pulled form of the local files is skipped
  • a library whose own libraries are listed with its files is skipped
  • a changed library that can't be pushed only warns, and the project is still pushed
  • a project that can't be pushed after its libraries still raises

Each of the skip and warning tests fails when its fix is reverted. Full suite: 2949 passed, 75 skipped, 0 failed. static_analysis.py passes.

@Martin-Molinero Martin-Molinero added the kronos:review Request review label Nov 27, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 27, 2026

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Requesting changes: the code looks right, but the change has no end-to-end run yet, and the tests don't check the case where the library check gets it wrong.

What I checked. Both --push branches of get_cloud_project now go through push_project, the same as lean cloud push --project. A library that can't be pushed only gets a warning, and only a failed push of the project itself stops the command, as decided on #749. CI is green. I checked the skip against real API responses, using the released CLI and my own script. I don't run code from pull requests by outside contributors. Two libraries match their cloud copy on name, libraries, metadata and files, so both would be skipped: one the CLI pushed, and one created in the cloud and then pulled (CRLF, no final newline). files/read lists a nested library's Library.py under the same name with isLibrary set, so filtering those files out is needed.

1. No end-to-end run. The description has only mocked tests, but both the skip and the warning depend on the real API. Please run these and put the outcomes in the description:

  • lean cloud backtest <project> --push with an unchanged library: the library isn't updated, and the backtest runs.
  • The same with a changed library: the library is pushed before the project, and the backtest runs the new library code.
  • The same with a changed library whose push fails: a warning, and the command goes on.

2. Tests skip the case that does harm. If the check wrongly treats a changed library as unchanged, the cloud runs the old library code with no warning. Of the things that must force a push, the tests only cover changed file content. Please add tests where the files match but something else differs, such as a local file added or deleted, a changed library reference, or a changed description. Also, test_get_cloud_project_push_warns_and_continues_when_changed_library_cannot_be_pushed doesn't assert that the warning is logged.

I set risk:high: the change alters what lean cloud push --project and every --push command write to the cloud for each referenced library, and there's no end-to-end run yet. The run above and the tests in item 2 are what would lower it.

Comment thread lean/components/cloud/push_manager.py Resolved
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:high High risk and removed kronos:review:working review: in progress labels Nov 27, 2026
…nged

Covers each check that must force a library push even though its files
match the cloud copy: a local file added or deleted, a library reference
removed, and a changed description or parameters. The warning test now
asserts the warning for the library that could not be pushed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Martin-Molinero

Copy link
Copy Markdown
Member

Thanks for the review. Both points are addressed in 5c106f1 and the description.

1. End-to-end run. I ran it from this branch against the real API. The full table is in the description's "End-to-end" section:

  • Unchanged library: lean cloud backtest <project> --push logged 'Library/LeanCli725E2ELib' is already up to date in the cloud, not pushing it, and the backtest ran with the library's v1.
  • Also unchanged, in the pulled form: I rewrote the cloud file the way the web IDE stores it (CRLF, no final newline) and ran lean cloud pull. The next push skipped the library, and the cloud file kept its web IDE form.
  • Changed library: the library was pushed [1/2] before the project [2/2], and the backtest ran the new v2.
  • Changed library whose push fails: the Cannot push warning, the project still pushed, exit code 0, and the backtest ran with the library's cloud code (v2, not the local v3).

One limit: the account I used has a single user, so I couldn't get a real read-only share. I made the library push fail locally instead, by marking the library encrypted without a key, which the CLI refuses before sending any request. The failure goes through the same except in PushManager as a refused projects/update.

2. Tests for a wrong skip. New test_get_cloud_project_push_pushes_library_whose_files_match_when_something_else_changed. The library's files match the cloud copy, and the library must still be pushed when, in turn:

  • a local file was added
  • a local file was deleted
  • a library reference was removed
  • the description changed
  • the parameters changed

I removed each part of the check in _is_cloud_project_up_to_date in turn (metadata, library references, the set of file names), and the cases for that part fail. The warning test now asserts the exact Cannot push 'Library/Python Library': You do not have write permission on this project. warning, and fails when the warning isn't logged.

@Martin-Molinero Martin-Molinero added the kronos:review Request review label Nov 27, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 27, 2026

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving: both findings from my last review are resolved, and CI is green.

  • End-to-end run: the description now covers an unchanged library (skipped, and the backtest runs), a library in the form a pull writes (skipped), a changed library (pushed before the project, and the backtest runs the new code), and a library push that fails (a warning, the project is still pushed, and the command goes on). A real read-only refusal wasn't reproduced. That's fine: the skip returns before any write request, and a refused update goes through the same except in _push_projects as the forced failure.
  • Tests for a wrong skip: test_get_cloud_project_push_pushes_library_whose_files_match_when_something_else_changed covers a file added, a file deleted, a library reference removed, a changed description and changed parameters. Each case needs its own part of _is_cloud_project_up_to_date. The warning test now asserts the exact warning.

I changed the label from risk:high to risk:low. The change is small and easy to revert: about 70 lines in PushManager, CloudProjectManager and pull_manager. --push now pushes the way lean cloud push --project already does. The evidence is tests that run the real push path for every skip and push case, green CI, and an end-to-end run against the API.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:low Low risk kronos:review Request review kronos:review:queued review: queued and removed risk:high High risk kronos:review:working review: in progress kronos:review Request review labels Nov 27, 2026

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Requesting changes before merging:

  • Keep implicit library sync from changing cloud encryption, and make the comparison read-only. The comparison should fetch only the library’s own files and recognize byte-identical CRLF files without a final newline.
  • Keep a library failure from breaking an otherwise valid project push. I verified that the base resolver uploads the project with the existing library ID when the library is symlinked outside the workspace or has an invalid nested reference. The new path can abort before the project update. I also reproduced a refused library update after a local rename: the directory is moved but the reference still points to the old path, so the project fails too.
  • Verify removal of the last nested reference. The new test observes libraries=[] at the mock, but that field is missing on the wire. The cloud keeps the removed reference.

@Martin-Molinero Decision needed: Should --push stop before compiling when a library upload has already modified some cloud files?

The agreed warning-only behavior works for a refused write that leaves the library intact. The existing file-by-file fallback can also succeed on one file and fail on the next; the new path logs the warning and can continue into a backtest or live deploy with mixed library code.

  • A. Stop on a known partial upload (recommended): continue after refusals that didn't change the library, but don't run against a known partial upload. Distinguish those outcomes and test both.
  • B. Continue after either outcome: keep exact lean cloud push parity, but document that a run can use mixed library code and test that behavior explicitly.

Reply with the option you choose, or another way to go.

risk:high: this can change a library’s encryption or leave a cloud run using a partially uploaded dependency. The end-to-end results in the description cover the normal sync path and a failure before any write; they don't cover these cases. Please include regression tests and report the end-to-end outcome for a write refusal and for the chosen partial-upload behavior.

encryption_key_id = get_project_key_hash(encryption_key)
update_args["encryption_key"] = encryption_key_id

if skip_if_unchanged and self._is_cloud_project_up_to_date(cloud_project, update_args, expected_correct_project_name):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please preserve the cloud library’s encryption state on an implicit push, even when the local copy differs. If it can’t be uploaded in that state, warn and leave the library untouched. Add a regression test for an encrypted cloud library with a differently configured local copy, asserting that no library write is sent.


try:
# Library files come with their own names, and belong to the project's libraries, not to the project
cloud_files = {file.name: file.content for file in self._api_client.files.get_all(cloud_project.projectId)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please use a non-mutating listing scoped to this library’s own files. files/read includes transitive files by default; dropping them after the response still downloads 55 library file sets for a chain of 10 unchanged libraries, instead of 10. Send includeLibraries=false for this comparison, and verify that the read leaves cloud files untouched.


# A file is unchanged when it is the one pushed, or the one a pull of the cloud copy wrote
from lean.components.cloud.pull_manager import get_pulled_file_content
return all(content in (cloud_files[name], get_pulled_file_content(cloud_files[name]))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

An unchanged file with identical # first\r\n# second bytes on both sides still triggers an update. Path.read_text() converts the local CRLF to LF without adding a final newline, so it matches neither raw cloud content nor the pull form (which adds a newline). Compare the cloud content using universal newlines before also checking the pull form, and test this exact-copy case.

"""
libraries = self._project_manager.get_project_libraries(project)
self.push_projects([project], libraries, encryption_action, encryption_key, force)
return project not in self._push_projects([project], libraries, encryption_action, encryption_key, force)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Treat errors in dependency discovery and path formatting as library warnings when the main project can still resolve its references. A library with a valid cloud ID but a malformed nested reference raises during get_project_libraries() before the handler; a library symlinked outside the workspace raises at relative_to() before the handler too. The base resolver can push the main project in both cases. Keep per-library discovery errors isolated and use an absolute path fallback for logging.

self._logger.info(f"[{index}/{len(all_projects_to_push)}] Pushing '{relative_path}'")
# A library whose cloud copy already matches the local one needs no update, so it is not
# rejected when the user can only read it.
self.pushed_project_paths[path] = self._push_project(path, organization_id, encryption_action_value,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The new path is recorded only after _push_project returns. With local Library/Café pointing to an existing read-only cloud library, the local rename to Library/Caf happens first. The rejected library write leaves the project config pointing at the missing Library/Café; the main push then fails reading its cloud ID. Record the move even when the cloud update fails, or roll it back, and test refused updates after a rename.

# a changed description
lambda path, cloud: _set_library_config(path, "description", "A changed description"),
# changed parameters
lambda path, cloud: _set_library_config(path, "parameters", {"period": "10"}),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This removes the only nested reference. ProjectClient.update puts libraries=[] into form data, but requests omits empty lists and the API only reconciles libraries when that field is present. So the mock sees a push while the old reference remains. Please make removal of the last reference persist and cover it at the request boundary, not just by asserting the library project ID was updated.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added risk:high High risk and removed risk:low Low risk kronos:review:working review: in progress labels Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:high High risk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cloud commands with --push do not upload referenced libraries

2 participants