Skip to content

Phase 3: idempotent workflow deploy + sync command - #414

Merged
neuromechanist merged 2 commits into
feature/issue-406-epic-rate-limit-hardeningfrom
feature/issue-407-phase3-predeploy-workflows
May 10, 2026
Merged

neuromechanist merged 2 commits into
feature/issue-406-epic-rate-limit-hardeningfrom
feature/issue-407-phase3-predeploy-workflows

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Stop rewriting identical workflow files on every `finalize` retry, and give operators a way to repair drift after we update a template.

Closes #407
Part of epic #406

Changes

  • `backend/src/services/github.ts`:
    • `ensureWorkflowsDeployed(repo, branch, pat)`: lists `.github/workflows` once; if all expected templates are present, returns without commit (1 REST call). Otherwise, commits only the missing files via Phase 1's `commitFilesAsTree`. Never throws.
    • `syncWorkflowTemplates(repo, branch, pat)`: lists the workflow directory, fetches the deployed content of each template via `getFileContent`, normalizes line endings, and commits in one tree only the files whose content drifted or that are missing entirely. On a per-file read failure, records an error and leaves the file unchanged (never clobbers).
  • `backend/src/routes/datasets.ts`: `POST /datasets/:id/finalize` swaps `deployWorkflows()` for `ensureWorkflowsDeployed()`. Steady-state cost on this idempotent endpoint drops from 4 -> 1 REST call. Response now includes `workflows.deployed` and `workflows.already_present` for debuggability.
  • `backend/src/routes/admin.ts`: new `POST /admin/datasets/:id/ci/sync` route. Audit log entry `ci_templates_synced` records the `changed`/`added`/`errors` arrays.
  • `src/lib/api.ts`: `syncCi(datasetId): Promise` client helper.
  • `src/commands/admin.ts`: new `nemar admin ci sync [] [--all]` subcommand. Sequential iteration with `--all` so Phase 2's pre-flight throttle can pace the sweep.
  • `test/workflow-sync.test.ts` (new, 7 tests): real Bun.serve fake; asserts on call counts, tree-commit body contents, and CRLF normalization. Handlers are derived from `getWorkflowTemplates()` so the test doesn't break when templates are added.

Acceptance checklist (from #407)

  • Workflow templates already land in a single tree commit (since Phase 1's `commitFilesAsTree`).
  • `ci_check` step does not call `deployWorkflows()` unless the workflow file is missing. (Already true before this PR; left unchanged. Steady state is 1 HEAD + 1 GET = 2 calls.)
  • `nemar admin ci sync` exists and only writes diffs.
  • Publish flow REST-call count for `ci_check` is `<= 2` in the steady state. (Already met before this PR.)

Intentionally not addressed (with reason)

  • Deploying workflows at `createRepository()` time. The CLI's first-push flow (DataLad / `git push -u origin main`) expects an empty remote; `auto_init: true` or an early Git Data API commit would break that path. Workflows continue to land at `finalize` time, which now costs 1 REST call in the steady state.
  • Migrating `checkWorkflowExists` to `githubFetchWithRetry`. Separate sweep across the ~45 raw `fetch()` sites in `github.ts`.

Test plan

  • `bun test test/workflow-sync.test.ts` -> 7/7 pass
  • `bun test test/{workflow-sync,rate-limit-retry,tree-batched-commit}.test.ts` -> 44/44 pass
  • `bun run build` clean
  • Manual: `nemar admin ci sync nm099999` against dev backend; edit a template, redeploy, rerun to confirm only the changed file is committed (post-merge)

ensureWorkflowsDeployed: list .github/workflows once, deploy only the
templates that are missing in a single tree commit. Steady-state cost
drops from 4 REST calls (unconditional tree commit) to 1 (the listing).

syncWorkflowTemplates: compare every template against deployed content
and commit, in one tree, only the files that drift or are missing.
Line-ending normalized to avoid CRLF false positives. Read failures
record an error and leave the file unchanged so we never clobber a
hand-edited workflow on a transient read fail.

Replace the unconditional deployWorkflows() call in POST /datasets/:id/
finalize with ensureWorkflowsDeployed; surface deployed/already_present
in the response.

New admin route POST /admin/datasets/:id/ci/sync and CLI command
nemar admin ci sync [<id>] [--all] wired through src/lib/api.ts.
@github-actions

github-actions Bot commented May 10, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR414

Version: 0.8.9-PR414.e0697c4
Commit: e0697c4


🤖 Updated on each push.

Result types now distinguish success-with-no-work, list-failed, and
commit-failed:
- ensureWorkflowsDeployed: new `listFailed` flag so callers can tell
  `alreadyPresent: []` apart from 'we could not check.'
- syncWorkflowTemplates: `committed` flag and the `changed`/`added`
  lists are now preserved on commit failure (they encode 'what we
  intended' rather than getting zeroed out).

Content normalization for sync now strips UTF-8 BOM, normalizes bare \r
and CRLF to \n, and ignores trailing whitespace. A workflow that round-
tripped through Windows tooling no longer triggers an infinite-rewrite
loop on every sync.

Listing filter accepts both `file` and `symlink` entry types so a
symlinked workflow is correctly classified as present.

Admin route returns HTTP 207 when `errors.length > 0` so automation
and --all loops can't false-green on partial failures. Audit-log action
renamed `ci_templates_synced` -> `ci_workflows_synced` for grep parity
with the existing `ci_workflows_deployed` shape.

Tests grew from 7 to 14, adding coverage for:
- listing failure (non-404)
- commit failure (both helpers)
- per-file read failure (file is skipped, others still sync)
- symlinked workflow treated as present
- UTF-8 BOM + trailing whitespace not triggering false-positive diffs
- tree-commit count assertions on every drift scenario
Failure tests use 422 (terminal status) so wall-clock stays under 40ms.
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review summary

Three parallel reviewers ran (code-reviewer, pr-test-analyzer, silent-failure-hunter).

Critical / High — addressed

  • silent-failure-hunter: `ensureWorkflowsDeployed` returning `alreadyPresent: []` on listing failure conflates "presence unknown" with "no workflows deployed." Fix: new `listFailed` flag on the result type; callers can disambiguate.
  • silent-failure-hunter: `syncWorkflowTemplates` commit-failure path zeroed `changed`/`added` so callers couldn't tell "no work needed" from "tried to commit but failed." Fix: new `committed` flag; the intended `changed`/`added` lists are preserved on commit failure.
  • silent-failure-hunter + code-reviewer: line-ending normalization missed BOM, bare `\r`, and trailing newline drift — would have caused an infinite rewrite loop on workflows round-tripped through Windows editors. Fix: normalize BOM, all CR variants, trailing whitespace.
  • silent-failure-hunter: `listDeployedWorkflowPaths` filtered only `type === "file"`, silently dropping symlinked workflows. Fix: accept both `file` and `symlink`.

Important — addressed

  • code-reviewer: route returned HTTP 200 even with non-empty `errors`. Fix: returns 207 (Multi-Status) when `errors.length > 0` so automation and `--all` loops don't false-green.
  • code-reviewer: audit log action name inconsistent. Fix: renamed `ci_templates_synced` -> `ci_workflows_synced` for grep parity with sibling `ci_workflows_deployed`.
  • pr-test-analyzer: commit-failure rollback paths in both helpers were untested. Fix: added 2 tests.
  • pr-test-analyzer: per-file read failure path was untested. Fix: added 1 test that confirms the failed-read file is skipped while other drifts still sync.
  • pr-test-analyzer: non-404 listing error path was untested. Fix: added 1 test.
  • pr-test-analyzer: tree-commit count assertions missing. Fix: added `countByMethodPath[trees] === 1` to every drift scenario.

Intentionally not addressed (with reason)

  • silent-failure-hunter: "use `logError` instead of `console.error` for audit-log failures." Grep confirmed `logError` is only used as a catch variable name in this codebase, not as a helper function — `console.error` is the actual project convention. Sibling `ci_workflows_deployed` uses the same pattern.
  • silent-failure-hunter: "tighten the finalize catch warning text." That catch is for synchronous throws (which the helper avoids by design); the existing text is fine and matches the surrounding warning style.

Verification

  • `bun test test/workflow-sync.test.ts` -> 14/14 pass (38ms)
  • `bun test test/{workflow-sync,rate-limit-retry,tree-batched-commit,publish-retry-classifier,manifest}.test.ts` -> 93/93 pass
  • `bun run build` clean

@neuromechanist
neuromechanist merged commit 80247c4 into feature/issue-406-epic-rate-limit-hardening May 10, 2026
1 check passed
@neuromechanist
neuromechanist deleted the feature/issue-407-phase3-predeploy-workflows branch May 11, 2026 05:15

This branch was previously deployed

1 inactive deployment
npm — e0697c4a Deployed May 10, 2026 by neuromechanist via publish #724
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.

1 participant