-
Notifications
You must be signed in to change notification settings - Fork 2
feat(resilience): provider fallback chain for retryable failures #128
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * Copyright (c) 2025-2026 Netresearch DTT GmbH | ||
| * SPDX-License-Identifier: GPL-2.0-or-later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Netresearch\NrLlm\Domain\DTO; | ||
|
|
||
| use JsonSerializable; | ||
|
|
||
| /** | ||
| * Ordered list of LlmConfiguration identifiers to try when the primary | ||
| * configuration fails with a retryable error. | ||
| * | ||
| * Fallback is shallow: a fallback configuration's own chain is ignored | ||
| * to prevent recursion and cycles. Identifiers are normalised (trimmed | ||
| * and lowercased) to match how `tx_nrllm_configuration.identifier` | ||
| * stores them (TCA `eval=trim,alphanum_x,lower,unique`); withLink(), | ||
| * without() and contains() all apply the same normalisation so a | ||
| * manually-edited JSON payload with stray whitespace or capitals still | ||
| * resolves to the right row. Normalised duplicates are dropped on entry | ||
| * via fromArray()/fromJson() and silently skipped by withLink(). | ||
| * | ||
| * The constructor itself does NOT normalise — it trusts already-sanitised | ||
| * input (see the sanitize() docblock). | ||
| */ | ||
| final readonly class FallbackChain implements JsonSerializable | ||
| { | ||
| /** | ||
| * @param list<string> $configurationIdentifiers Ordered list of LlmConfiguration identifiers (should be pre-normalised) | ||
| */ | ||
| public function __construct( | ||
| public array $configurationIdentifiers = [], | ||
| ) {} | ||
|
|
||
| /** | ||
| * @param array{configurationIdentifiers?: mixed} $data | ||
| */ | ||
| public static function fromArray(array $data): self | ||
| { | ||
| $identifiers = $data['configurationIdentifiers'] ?? []; | ||
| if (!is_array($identifiers)) { | ||
| return new self(); | ||
| } | ||
| return new self(self::sanitize($identifiers)); | ||
| } | ||
|
|
||
| public static function fromJson(string $json): self | ||
| { | ||
| if ($json === '') { | ||
| return new self(); | ||
|
Check warning on line 54 in Classes/Domain/DTO/FallbackChain.php
|
||
| } | ||
| $data = json_decode($json, true); | ||
| if (!is_array($data)) { | ||
| return new self(); | ||
| } | ||
| /** @var array{configurationIdentifiers?: mixed} $data */ | ||
| return self::fromArray($data); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{configurationIdentifiers: list<string>} | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'configurationIdentifiers' => $this->configurationIdentifiers, | ||
| ]; | ||
| } | ||
|
|
||
| public function toJson(): string | ||
| { | ||
| return json_encode($this->toArray(), JSON_THROW_ON_ERROR); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{configurationIdentifiers: list<string>} | ||
| */ | ||
| public function jsonSerialize(): array | ||
| { | ||
| return $this->toArray(); | ||
| } | ||
|
|
||
| public function isEmpty(): bool | ||
| { | ||
| return $this->configurationIdentifiers === []; | ||
| } | ||
|
|
||
| public function count(): int | ||
| { | ||
| return count($this->configurationIdentifiers); | ||
| } | ||
|
|
||
| public function contains(string $identifier): bool | ||
| { | ||
| return in_array(self::normalise($identifier), $this->configurationIdentifiers, true); | ||
| } | ||
|
|
||
| /** | ||
| * Return a new chain with the given identifier appended, deduplicated. | ||
| * Empty / whitespace-only identifiers are silently ignored. | ||
| */ | ||
| public function withLink(string $identifier): self | ||
| { | ||
| $normalised = self::normalise($identifier); | ||
| if ($normalised === '' || $this->contains($normalised)) { | ||
| return $this; | ||
| } | ||
| return new self([...$this->configurationIdentifiers, $normalised]); | ||
| } | ||
|
CybotTM marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Return a new chain without the given identifier. | ||
| * Useful for excluding the primary configuration before walking fallbacks. | ||
| */ | ||
| public function without(string $identifier): self | ||
| { | ||
| $normalised = self::normalise($identifier); | ||
| if ($normalised === '' || !$this->contains($normalised)) { | ||
| return $this; | ||
| } | ||
| $filtered = array_values(array_filter( | ||
| $this->configurationIdentifiers, | ||
| static fn(string $link): bool => $link !== $normalised, | ||
| )); | ||
| return new self($filtered); | ||
| } | ||
|
CybotTM marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Normalise input: drop non-strings, drop empty strings, trim, lowercase, | ||
| * drop duplicates, reindex as list. Matches the sanitisation TCA already | ||
| * applies to `tx_nrllm_configuration.identifier` so round-trips through | ||
| * hand-written JSON do not accidentally miss a row. | ||
| * | ||
| * @param array<mixed> $identifiers | ||
| * | ||
| * @return list<string> | ||
| */ | ||
| private static function sanitize(array $identifiers): array | ||
| { | ||
| $seen = []; | ||
| $out = []; | ||
| foreach ($identifiers as $identifier) { | ||
| if (!is_string($identifier)) { | ||
| continue; | ||
| } | ||
| $normalised = self::normalise($identifier); | ||
| if ($normalised === '' || isset($seen[$normalised])) { | ||
| continue; | ||
| } | ||
| $seen[$normalised] = true; | ||
| $out[] = $normalised; | ||
| } | ||
| return $out; | ||
|
CybotTM marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static function normalise(string $identifier): string | ||
| { | ||
| return strtolower(trim($identifier)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
Classes/Provider/Exception/FallbackChainExhaustedException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * Copyright (c) 2025-2026 Netresearch DTT GmbH | ||
| * SPDX-License-Identifier: GPL-2.0-or-later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Netresearch\NrLlm\Provider\Exception; | ||
|
|
||
| use Throwable; | ||
|
|
||
| /** | ||
| * Thrown when every configuration in a fallback chain (primary + fallbacks) | ||
| * failed with a retryable error. Carries per-attempt errors so callers can | ||
| * reason about the full failure sequence. | ||
| */ | ||
| final class FallbackChainExhaustedException extends ProviderException | ||
| { | ||
| /** | ||
| * @param list<array{configuration: string, error: Throwable}> $attemptErrors | ||
| */ | ||
| public function __construct( | ||
| string $message, | ||
| int $code, | ||
| private readonly array $attemptErrors, | ||
| ?Throwable $previous = null, | ||
| ) { | ||
| parent::__construct($message, $code, $previous); | ||
| } | ||
|
|
||
| /** | ||
| * @param list<array{configuration: string, error: Throwable}> $attempts | ||
| */ | ||
| public static function fromAttempts(array $attempts): self | ||
| { | ||
| $configurations = array_map( | ||
| static fn(array $attempt): string => $attempt['configuration'], | ||
| $attempts, | ||
| ); | ||
| $last = $attempts === [] ? null : $attempts[array_key_last($attempts)]['error']; | ||
|
|
||
| $message = sprintf( | ||
| 'All %d configuration(s) in the fallback chain failed: %s', | ||
| count($attempts), | ||
| implode(' -> ', $configurations), | ||
| ); | ||
|
|
||
| return new self($message, 1745712001, $attempts, $last); | ||
| } | ||
|
|
||
| /** | ||
| * @return list<array{configuration: string, error: Throwable}> | ||
| */ | ||
| public function getAttemptErrors(): array | ||
| { | ||
| return $this->attemptErrors; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<string> | ||
| */ | ||
| public function getAttemptedConfigurations(): array | ||
| { | ||
| return array_map( | ||
| static fn(array $attempt): string => $attempt['configuration'], | ||
| $this->attemptErrors, | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.