Skip to content

Conversation

CedricHwong
Copy link
Contributor

@CedricHwong CedricHwong commented Jul 23, 2025

Description

Replace rstrip("api") with an exact "/api" suffix removal to avoid stripping valid domain endings like .ai, .app, etc.
rstrip("api") treats its argument as a character set and removes all trailing a/p/i, causing hosts such as www.example.ai to become www.example..
The fix uses str.removesuffix("/api") (Py≥3.9) with a safe fallback for older Python versions.
The patch calls removesuffix("/api") on Python 3.9+ and falls back to simple slicing ([:-4]) on earlier python versions.
New tests demonstrate the incorrect behavior and verify the corrected result.

Closes: #1203


🎯 PRs Should Target Issues

This PR is tied to the bug report above. If a different issue ID is used, update the Closes: line accordingly.


Changes

  • fix(host parsing): replace rstrip("api") with an exact "/api" suffix removal.
  • **tests:**local test to cover:
    • .ai domain not being truncated
    • Exact "/api" suffix removal
    • Non-suffix strings (/apip, /appi, etc.) left untouched
    • Query/fragment edge cases
    • Only one occurrence of "/api" stripped when repeated

guangwei_huang and others added 2 commits July 23, 2025 19:21
Changed the test_set_by_netrc function to use 'https://example.ai' instead of 'https://example.cn' for host verification. This aligns the test with the expected domain for SwanLab environment configuration.
@SAKURA-CAT
Copy link
Member

THX!

@SAKURA-CAT SAKURA-CAT self-requested a review July 23, 2025 11:52
@SAKURA-CAT SAKURA-CAT added the 🐛 bug Something isn't working label Jul 23, 2025
guangwei_huang and others added 4 commits July 23, 2025 20:10
Moved host suffix removal logic to a dedicated function `remove_host_suffix` in env.py and updated its usage in package.py. This improves code reuse and consistency when handling host strings.
@SAKURA-CAT SAKURA-CAT requested a review from Copilot July 24, 2025 03:16
Copy link

@Copilot 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 fixes a bug in host string parsing where rstrip("api") was incorrectly treating its argument as a character set, causing domains ending with .ai, .app, etc. to be truncated. The fix introduces a new remove_host_suffix function that properly removes exact suffix matches.

  • Replaces problematic rstrip("api") calls with a new remove_host_suffix function
  • Adds comprehensive test coverage for the new suffix removal functionality
  • Updates existing test cases to use .ai domains to demonstrate the fix

Reviewed Changes

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

File Description
swanlab/env.py Implements new remove_host_suffix function and updates host parsing logic
swanlab/package.py Replaces rstrip calls with remove_host_suffix function calls
test/unit/test_env.py Adds comprehensive tests for suffix removal and updates existing tests to use .ai domains
test/unit/test_package.py Updates test cases but uses incorrect hardcoded slicing approach
Comments suppressed due to low confidence (2)

test/unit/test_env.py:101

  • The test name "test_handles_host_with_blank_suffix" is misleading. The test actually checks a host with trailing whitespace and a suffix that doesn't match. Consider renaming to "test_handles_host_with_whitespace_and_non_matching_suffix" to better reflect what is being tested.
def test_handles_host_with_blank_suffix():

test/unit/test_env.py:104

  • This test case doesn't clearly validate the intended behavior. With host="example.com " and suffix="api", the expected result "example.com" suggests whitespace trimming, but the suffix "api" doesn't match the host ending. Consider adding a clearer test case that validates whitespace trimming behavior separately.
    assert remove_host_suffix(host, suffix) == "example.com"

Replaces manual host suffix removal with the remove_host_suffix utility in TestSaveKey. Also updates the API_HOST environment variable assignment in TestGetKey for consistency.
@SAKURA-CAT SAKURA-CAT merged commit 66a2689 into SwanHubX:main Jul 24, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] The code uses rstrip("api") to drop a trailing "/api" from a base URL/host.
2 participants