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 AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
**Scope:** Repository-wide.
**Child files:** `src/lib/AGENTS.md`, `src/lib/handlers/AGENTS.md`, `tests/AGENTS.md`, `tests/integration/AGENTS.md`.

Child files define only local deltas; they inherit this file and do not override repository-wide rules unless they say so explicitly.

## Repository overview

JavaScript GitHub Action (Node 20 runtime, `dist/index.js` entrypoint) with three event flows:
Expand Down
31 changes: 23 additions & 8 deletions src/lib/handlers/AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# HANDLER MODULE GUIDE

## OVERVIEW
## Scope and inheritance

Applies to: `src/lib/handlers/` and descendants.

Inherits repository-wide guidance from `../../../AGENTS.md` and services-layer guidance from `../AGENTS.md`.

This file defines only local deltas for the command-handler subtree.

## Overview

Command handlers implement `/zai` behavior only after parsing + authorization; each module owns prompt construction, API call wiring, and response formatting. The `scheduled` handler is distinct: it executes scheduled tasks defined in `.zai-scheduled.yml` (and the manual `/zai update-agents` command) rather than responding to a standard review command.

## WHERE TO LOOK
## Where to look

| Command | File | Notes |
|---------|------|-------|
| `/zai ask` | `src/lib/handlers/ask.js` | Uses continuity state and broad PR context |
Expand All @@ -16,33 +26,38 @@ Command handlers implement `/zai` behavior only after parsing + authorization; e
| scheduled tasks | `src/lib/handlers/scheduled.js` | Largest module; cron-driven `.zai-scheduled.yml` tasks; grounded + validated AGENTS.md upgrades |
| Handler registry | `src/lib/handlers/index.js` | Dispatcher map consumed by runtime (note: `scheduled` is exported but not in the `/zai` HANDLERS map) |

## SCHEDULED MODULE (`scheduled.js`) KEY SYMBOLS
## Scheduled module (`scheduled.js`) key symbols

- `handleScheduledEvent` (entry) → `executeScheduledTask` (per-task) → `buildExecutionContext` → `getScheduledHandler` (registry lookup).
- `handleUpdateAgentsTask` (grounded flow): gist command → `collectRepositoryContext` (`../repository-context.js`: real tree + existing AGENTS.md discovery + key files) → `buildAgentsUpgradePrompt` (embeds context, tells model it has NO live repo access) → `callZaiApiWithRetry` → `parseFileUpdatesFromResponse` (multi-format JSON) → `validateGeneratedAgentFiles` (`../agents-validation.js`: rejects non-AGENTS paths, out-of-scope writes, hallucinated content referencing non-existent files) → diff vs repo files → `createPR`.
- Registry: `SCHEDULED_HANDLERS` (const) + `registerScheduledHandler`/`getAllScheduledHandlers` for extension.
- GitHub helpers: `fetchFileContent`, `getFileSha`, `updateFileInRepo`; HTTP: `fetchFromUrl` (gist, 30s timeout).
- Config consumed from `src/lib/config/scheduled-config.js` (`loadScheduledConfig`, `getTasksToRun`, `getGistUrl`, `validateAgentsConfig`).
- Scoping config (all optional, per-task in `.zai-scheduled.yml`): `context_paths`, `target_paths`, `exclude_paths`, `max_context_chars`, `max_file_chars`, `max_files_to_fetch`, `allow_create_new`, `update_existing_only`.

## CONVENTIONS
## Conventions

- Keep command argument parsing explicit and reject invalid formats early.
- Always use threaded replies (`replyToId`) for command results.
- Reactions should reflect lifecycle: acknowledge -> work -> success/failure.
- Reactions should reflect lifecycle: acknowledge work success/failure.
- Keep prompts bounded via context truncation helpers; never pass raw unbounded patches.
- Return user-safe failures; log internal details through shared logging helpers.

## TESTING
## Testing

- Local handler unit coverage exists in `tests/handlers/`: `ask.test.js`, `explain.test.js`, `impact.test.js`, `review.test.js`, `scheduled.test.js`.
- Scheduled pipeline coverage: `tests/handlers/scheduled.test.js` (registry, PR creation, parse, grounded `handleUpdateAgentsTask` flow incl. hallucination rejection), `tests/scheduled-config.test.js` (config + `validateAgentsConfig` scoping fields), `tests/repository-context.test.js` (tree/AGENTS.md discovery/budgets/globs), `tests/agents-validation.test.js` (path/hallucination/target-path guards incl. PR #15 regression).
- End-to-end command pipeline behavior is validated in `tests/integration/command-pipeline.test.js`.
- When changing parsing or output contracts, update both unit and integration assertions.

## ANTI-PATTERNS
## Anti-patterns

- Parsing arguments with loose heuristics that silently alter user intent.
- Posting top-level comments for command replies (breaks conversational threading).
- Bypassing `auth.checkForkAuthorization` in a handler.
- Embedding duplicate parser/auth logic that already exists upstream.

## NOTES
## Notes

- Prefer adding helper functions within a handler module before introducing cross-handler coupling.
- Keep marker constants stable once tests depend on them.
32 changes: 23 additions & 9 deletions tests/AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
# TEST SUITE GUIDE

## OVERVIEW
## Scope and inheritance

Applies to: `tests/` and descendants.

Inherits repository-wide guidance from `../AGENTS.md`.

This file defines only local deltas for the test suite. End-to-end integration specifics live in the child file `tests/integration/AGENTS.md`.

## Overview

Repository test coverage mixes module-focused tests in `tests/*.test.js` and scenario-driven flows in `tests/integration/*`.

## STRUCTURE
## Structure

```text
tests/
├── *.test.js # Module-level tests for lib/runtime units
├── handlers/ # Handler-focused test cases (ask, explain, impact, review, scheduled)
├── helpers/ # Shared mocks and fixtures utilities
├── lib/ # Shared test helpers + code-scope tests
├── fixtures/ # Static test payloads (issue-comment-event.json, pr-event.json)
└── integration/ # End-to-end command/review pipeline checks
└── integration/ # End-to-end command/review pipeline checks (see child AGENTS)
```

## WHERE TO LOOK
## Where to look

| Task | Location | Notes |
|------|----------|-------|
| Parser/auth/comment unit behavior | `tests/commands.test.js`, `tests/auth.test.js`, `tests/comments.test.js` | Fast regression checks |
Expand All @@ -24,23 +35,26 @@ tests/
| Continuity and events | `tests/continuity.test.js`, `tests/events.test.js` | Hidden-marker state, event-type detection |
| Code scope and window extraction | `tests/lib/code-scope.test.js` | Token budget, enclosing block, window extraction |
| Describe handler | `tests/describe.test.js` | PR description generation |
| Scheduled pipeline | `tests/handlers/scheduled.test.js`, `tests/scheduled-config.test.js`, `tests/repository-context.test.js`, `tests/agents-validation.test.js` | Config load + `validateAgentsConfig` scoping; `parseFileUpdatesFromResponse`; grounded `handleUpdateAgentsTask` flow incl. hallucination rejection; repo-context collection (tree/budgets/globs); validation guards incl. PR #15 regression |
| Scheduled pipeline (units) | `tests/handlers/scheduled.test.js`, `tests/scheduled-config.test.js`, `tests/repository-context.test.js`, `tests/agents-validation.test.js` | Config load + `validateAgentsConfig` scoping; `parseFileUpdatesFromResponse`; grounded `handleUpdateAgentsTask` flow incl. hallucination rejection; repo-context collection (tree/budgets/globs); validation guards incl. PR #15 regression |
| Scheduled pipeline (integration) | (pending) | End-to-end schedule event → context → Z.ai mock → validated PR is still a gap; unit coverage of the grounded flow exists via the `handleUpdateAgentsTask` seam (`__callZaiForTest`). |
| Full command pipeline | `tests/integration/command-pipeline.test.js` | Parse -> auth -> handler -> output contract |
| Full command pipeline | `tests/integration/command-pipeline.test.js` | Parse auth handler output contract |
| PR auto-review behavior | `tests/integration/pr-auto-review.test.js` | Marker upsert and PR event lifecycle |

## CONVENTIONS
## Conventions

- Test framework: Vitest v3 (uses vitest globals: describe/test/expect); configured via `vitest.config.js`.
- Keep tests deterministic with explicit mock payloads and marker assertions.
- Prefer scenario names that encode trigger + expected visible outcome.
- When changing comment markers or command UX, update integration snapshots/assertions immediately.

## ANTI-PATTERNS
## Anti-patterns

- Deleting integration assertions to make behavior changes pass.
- Asserting only internal calls without validating user-visible output.
- Duplicating large fixtures inline when reusable fixtures already exist.

## NOTES
## Notes

- Test command: `npm test` → `vitest run --coverage`.
- Coverage uploaded to Codecov.
- Integration tests are the safety net for command threading and marker idempotency.