Skip to content

fix(impulse): retry a classification hook once on a transient server error - #340

Open
funana777 wants to merge 1 commit into
mainfrom
fix/retry-classification-hook
Open

funana777 wants to merge 1 commit into
mainfrom
fix/retry-classification-hook

Conversation

@funana777

@funana777 funana777 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Before each tool call, context_agent asks the server who can reach the item, whether the call overwrites something and how far it can be undone. When the server is throttled or briefly overloaded, such a lookup fails and the call falls back to the worst case: External, destroys, irreversible. The user is then asked to confirm a write into their own private folder. In a scheduled task, the run stops at that confirmation.

Seen on cloud.nextcloud.com: a scheduled run writing a note into the user's private Memories folder with upload_file was shown as "External".

Change

  • _run_hook, which every classifier uses, now tries a hook once more after HOOK_RETRY_DELAY_SECONDS (10s, the same wait the app uses after a 429 when polling tasks) when it failed with a connection error, a timeout, HTTP 429 or HTTP 5xx.
  • Any other failure (403, 404, a lookup that cannot answer, a bug in a hook) goes to the worst-case fallback at once, without the pause.
  • If the second attempt fails, the classifier falls back to the worst case exactly as before. A call now skips the confirmation only when the server actually answered that it may.
  • audience.py: the DAV mount lookup raised ValueError on a non-207 response. It now raises NextcloudException with the HTTP status, so a throttled PROPFIND counts as transient like a throttled OCS call. This is the lookup upload_file goes through.

Hooks of other tools that wrap HTTP errors in plain exceptions (Deck, Calendar, Contacts) are not retried by this change.

Tests

  • A hook the server throttles once is retried and its answer used (radius and destructive).
  • A hook that meets HTTP 503 twice still yields EXTERNAL, after two attempts.
  • A hook that cannot answer (ValueError) is asked once, not retried.
  • _is_transient: connection error, timeout, 429 and 5xx count; 403, 404, status 0 and a plain ValueError mentioning 429 do not.
  • The pause is set to 0 with monkeypatch inside these tests only.

+140/-5 in 3 files, 4 new tests; 427 tests pass.

Also tested on a sandbox Nextcloud 35.0.1 with Apache answering the Context Agent's PROPFIND with 429: after one 429, upload_file was retried 10s later, classified as the user's own folder and ran without a confirmation; with 429 on the retry too, it fell back to External and asked, as before.

Base: main.

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Code, tests and this description were drafted with Claude Code (claude-opus-5-5) under my direction.

…error

A hook that asks the server who can reach an item fails when the server
is throttled or briefly overloaded. The classifier then falls back to the
worst case (EXTERNAL, destroys, irreversible), so the user is asked to
confirm a write into a private folder, and an unattended scheduled run
stops at that confirmation.

_run_hook, which every classifier goes through, now tries a hook once
more after HOOK_RETRY_DELAY_SECONDS (10s, the wait after a 429 elsewhere
in the app) when it failed with a connection error, a timeout, HTTP 429
or HTTP 5xx. Any other failure, and a second one, is raised as before,
so the worst-case fallback is unchanged.

The DAV mount lookup in audience.py now raises NextcloudException with
the HTTP status instead of ValueError, so a throttled PROPFIND counts as
transient like a throttled OCS call.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Christian Fu Mueller <christian-fu.mueller@nextcloud.com>
@funana777
funana777 force-pushed the fix/retry-classification-hook branch from 29e4293 to f2d7e88 Compare November 24, 2026 14:48
@funana777 funana777 mentioned this pull request Nov 24, 2026
1 task done
@funana777
funana777 marked this pull request as ready for review November 24, 2026 17:12
if not _is_transient(e):
raise
print(f"Hook {getattr(hook, '__name__', hook)!r} failed ({e!r}), trying once more in {HOOK_RETRY_DELAY_SECONDS}s")
await asyncio.sleep(HOOK_RETRY_DELAY_SECONDS)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On a 429 this always waits a fixed HOOK_RETRY_DELAY_SECONDS. It doesn't add jitter or read the server's Retry-After header, which is available through NextcloudException.response.

Because graph.py gathers several classifiers for each pending tool call, a burst of tool calls can have many hooks (around 20 with 5 calls × 4 classifiers) get a 429 together. They all sleep exactly 10s and then hit the throttled server again at the same moment, often several requests each. That makes a second 429 likely, so they all fall back to EXTERNAL anyway. If the server's Retry-After is longer than 10s, the retry fails for certain.

Suggestion: when there's a Retry-After header, use it (capped at something sensible), and add some random jitter to the delay, e.g. delay * random.uniform(0.5, 1.5).

@marcelklehr

Copy link
Copy Markdown
Member

I think the retry belongs at the HTTP layer, not in the classifier. niquests already has a proper retry mechanism: Session/AsyncSession take a retries= argument that accepts a urllib3 Retry, e.g.

Retry(
    total=2,
    status_forcelist={429, 502, 503, 504},
    allowed_methods={"GET", "HEAD", "OPTIONS", "PROPFIND"},
    backoff_factor=2,
    backoff_jitter=1.0,
    respect_retry_after_header=True,
    retry_after_max=60,
    raise_on_status=False,  # so nc_py_api's check_error still raises NextcloudException on the last response
)

Compared to retrying in _run_hook, this would:

  • retry only the request that failed, not the whole hook (file_path_radius currently re-sends all of its PROPFINDs and share listings),
  • respect Retry-After and add jitter instead of a fixed 10s,
  • never retry methods we haven't listed as safe (POST, MOVE, ...),
  • also protect the actual tool calls, not only the classification before them.

The blocker is nc_py_api: _create_adapter builds the session from a fixed set of arguments and has no way to pass retries. We could work around it by overriding _create_adapter or mounting an adapter on the private session, but both depend on nc_py_api internals.

So I'd suggest fixing this in nc_py_api first: add a retries option to the session config and pass it through in the _create_adapter implementations. Then we can configure the retry policy once here and drop the custom retry from impulse.py.

@funana777

funana777 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

@marcelklehr
Putting it into the HTTP layer is cool, I missed niquests entirely! So the retry belongs in the HTTP client, not the hook. I'll add a retries option to nc_py_api first, then set it up here and close this PR.

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