Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/ai-framework/mcp-servers/amrit-docs-mcp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
169 changes: 169 additions & 0 deletions docs/ai-framework/mcp-servers/amrit-docs-mcp/RETRY_DESIGN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Agent Task Retry & Failure Recovery

> **Scope:** `docs/ai-framework/mcp-servers/amrit-docs-mcp/`
> **Affects:** Network-bound MCP tool helpers (`fetchGitHubReadme`, `fetchRepoStructure`)
> **Breaking changes:** None — all existing tool APIs are identical.

---

## Why this is needed

The MCP server makes live HTTP calls to GitHub (raw content + GitHub API) for
every `get_repo_readme` and `get_repo_structure` tool call. Without retry logic,
a single transient network blip returns an error to the AI agent — even though
resending the same request milliseconds later would succeed.

For a health-data platform like AMRIT where agents may run in constrained
network environments (rural deployments, CI pipelines), silent retries give
reliability without changing the agent's experience.

---

## Architecture

```

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add language identifiers to fenced code blocks.

These fences are unlabeled and currently trigger MD040 lint warnings.

💡 Proposed fix
-```
+```text
 MCP Tool Handler
@@
-```
+```text
 delay = min(baseDelayMs × 2^attempt, maxDelayMs) + (jitter ? random(0, baseDelayMs) : 0)
@@
-```
+```text
 running retry engine tests…

Also applies to: 69-69, 151-151

🧰 Tools
🪛 markdownlint-cli2 (0.22.1)

[warning] 24-24: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/ai-framework/mcp-servers/amrit-docs-mcp/RETRY_DESIGN.md` at line 24, The
markdown contains unlabeled fenced code blocks that trigger MD040; locate the
three fences that wrap the snippets "MCP Tool Handler", "delay = min(baseDelayMs
× 2^attempt, maxDelayMs) + (jitter ? random(0, baseDelayMs) : 0)" and "running
retry engine tests…" and add a language identifier (e.g., ```text) to each
opening fence so they become labeled code blocks.

MCP Tool Handler
fetchGitHubReadme / fetchRepoStructure
withRetry(taskName, task, config) ◄── new
├── attempt 1 ──► axios.get(url, { timeout: 5000 })
│ ↓ fails
├── backoff (300ms × 2^1 + jitter)
├── attempt 2 ──► axios.get(url, { timeout: 5000 })
│ ↓ fails
├── backoff (300ms × 2^2 + jitter)
├── attempt 3 ──► axios.get(url, { timeout: 5000 })
│ ↓ fails
└── throw last error ──► MCP tool returns graceful error message
```

---

## Execution States

| State | Description |
|-------|-------------|
| `Pending` | Task created, not yet started |
| `Running` | First attempt in progress |
| `Retrying` | Subsequent attempt after a recoverable failure |
| `Success` | Task completed successfully |
| `Failed` | All attempts exhausted or non-retryable error encountered |

---

## Configuration (`RetryConfig`)

| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `maxRetries` | `number` | `3` | Retry attempts after first failure |
| `baseDelayMs` | `number` | `300` | Base for exponential backoff (ms) |
| `maxDelayMs` | `number` | `10000` | Cap on any single backoff delay (ms) |
| `jitter` | `boolean` | `true` | Add randomness to avoid retry storms |
| `isRetryable` | `fn` | always true | Filter which errors trigger a retry |

**Backoff formula:**
```
delay = min(baseDelayMs × 2^attempt, maxDelayMs) + (jitter ? random(0, baseDelayMs) : 0)
```

| Attempt | Delay (no jitter) |
|---------|-------------------|
| 1st retry | 600ms |
| 2nd retry | 1,200ms |
| 3rd retry | 2,400ms |
| 4th retry | 4,800ms |

---

## Files Changed

| File | Change |
|------|--------|
| `src/retry.ts` | **New** — self-contained retry engine, `withRetry()`, `isNetworkError()` |
| `src/retry.test.ts` | **New** — 13 unit tests covering all states and edge cases |
| `src/index.ts` | **Modified** — imports retry module, wraps `fetchGitHubReadme` and `fetchRepoStructure`, version bumped `1.0.0` → `1.1.0` |
Comment on lines +87 to +88

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Test count in docs is stale versus the current suite.

The doc says 13 tests, but docs/ai-framework/mcp-servers/amrit-docs-mcp/src/retry.test.ts currently defines 11 tests.

Also applies to: 168-168

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/ai-framework/mcp-servers/amrit-docs-mcp/RETRY_DESIGN.md` around lines 87
- 88, The docs claim "13 unit tests" but the actual test suite in retry.test.ts
contains 11 tests; update the RETRY_DESIGN.md entry that lists
`src/retry.test.ts` so the test count reflects the current suite (change "13
unit tests" to "11 unit tests" or adjust to the true current number), and ensure
the related changelog note for `src/index.ts` remains accurate; verify the
number by running the test file (retry.test.ts) before committing.


---

## Edge Cases Handled

| Scenario | Behaviour |
|----------|-----------|
| **Network timeout** (`ECONNABORTED`) | Retried up to `maxRetries` times with backoff |
| **HTTP 5xx** | Retried — server-side transient failure |
| **HTTP 4xx** (404, 400) | Fails fast — `isNetworkError` returns `false` |
| **Invalid task input** | Custom `isRetryable` predicate can short-circuit retries |
| **Repeated failures** | All failure reasons logged with timestamps and attempt numbers |
| **Partial success** | Each branch URL tried independently; success on any branch returns immediately |
| **Max retries reached** | Last error re-thrown; full failure log emitted to stderr |

---

## Usage Examples

### Basic usage
```ts
import { withRetry } from "./retry.js";

const data = await withRetry(
"fetch-readme:HWC-API",
() => axios.get(url, { timeout: 5000 }).then(r => r.data),
{ maxRetries: 3, baseDelayMs: 300, maxDelayMs: 8000, jitter: true }
);
```

### With retryable predicate
```ts
import { withRetry, isNetworkError } from "./retry.js";

// Will retry on 5xx and timeouts, but NOT on 404
const data = await withRetry("my-task", task, {
maxRetries: 3,
isRetryable: isNetworkError,
});
```

### Custom predicate
```ts
await withRetry("validate-task", task, {
maxRetries: 2,
isRetryable: (err) => {
if (err instanceof Error && err.message.startsWith("Invalid")) return false;
return true;
}
});
```

---

## Running Tests

```bash
cd docs/ai-framework/mcp-servers/amrit-docs-mcp
npx ts-node --esm src/retry.test.ts
```

Expected output:
```
running retry engine tests…

✅ succeeds on first attempt
✅ succeeds on second attempt after one failure
✅ throws after exhausting all retries
✅ non-retryable 404 fails fast without retrying
✅ retryable 500 error triggers retry
✅ network timeout error is retried
✅ works with async task function
✅ partial success: fails twice then succeeds
✅ invalid task input throws without retrying
✅ isNetworkError returns true for timeout code
✅ isNetworkError returns false for 404
✅ isNetworkError returns true for 500
✅ DEFAULT_RETRY_CONFIG has expected values

13 tests: 13 passed, 0 failed.
```
Loading