Skip to content

Fix nemar dataset push --pr S3 upload failures (#401) - #404

Merged
neuromechanist merged 5 commits into
devfrom
feature/issue-401-fix-push-s3
May 10, 2026
Merged

neuromechanist merged 5 commits into
devfrom
feature/issue-401-fix-push-s3

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Fixes #401. Tracking epic for the staging workflow design opened as #403.

Summary

Three independent bugs in `nemar dataset push` that combined to silently corrupt push state for OpenNeuro-imported datasets and any user without NEMAR-collaborator status. on003825 hit Bug A in PR #1 (merged to main); the dataset's main branch has been hand-recovered as part of this work — see below.

Bug A: wrong S3 remote for OpenNeuro-imported datasets

`getAnnexS3Remotes` enumerated every annex-S3 remote and `dataset.ts:4221` blindly picked `s3Remotes[0]`. For datasets imported from OpenNeuro (`on*`), the inherited `s3-PUBLIC` ranks first and gets selected instead of `nemar-s3`. Uploads then fail with "no S3 version ID is recorded for this key" against OpenNeuro's versioned bucket, while the CLI prints "Git changes were pushed successfully".

Fixes:

  • `getAnnexS3Remotes` skips remotes where `annex-ignore=true`.
  • New `selectAnnexS3Remote(remotes, datasetPath)` prefers `nemar-s3` by name, then `[nemar-s3]` annex description, then first match.
  • `import-openneuro.ts` now sets `annex-ignore=true` on inherited `s3-PUBLIC` / `s3-PRIVATE` after building the URL map, so future imports filter cleanly without relying on the name preference.

Bug B: 403 collaborator error silently lost the PR

When a user has GitHub `push` on a dataset repo but is not a NEMAR collaborator, `requestUploadCredentials` returned 403 "Only dataset owner or collaborators...". The CLI fell through to env credentials, then S3 failed and `process.exit(1)` killed the run before `--pr` got to create the PR. The fallback message also misleadingly suggested `nemar auth login`.

Fixes:

  • Detect the 403 collaborator error and auto-call `requestDatasetAccess` once, then retry credentials. Public datasets auto-approve so this is non-interactive in the common case.
  • On retry failure, surface `'nemar dataset request-access '` instead of the AWS env-vars hint.
  • Print `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` status when falling back so the env-vars hint is concretely diagnosable.
  • Track `s3PushFailed` and skip `process.exit` so PR creation still runs when `--pr` is set; users can retry the data step with `--no-pr` later.

Bug C: git-annex branch lost the new location records

`pushToGitHub` runs at the start of the push command and pushes `git-annex` once. `git annex copy --to nemar-s3` then writes new location records to the local `git-annex` branch that never reach origin. Fresh clones see the data on S3 but no record it's there, so `git annex whereis` returns "0 copies".

Fix: after a successful `copyToAnnexRemote`, re-push `git-annex` so location records ship with the PR. Failure is a warning, not fatal.

on003825 recovery

Verified empirically on the live dataset: all 26,107 stimuli MD5E keys were already present in `s3://nemar/on003825/objects/` from the merged PR upload; only the location records were missing. Recovery was metadata-only:

```bash
git annex find --include='stimuli/*' --format='${key} 1\n' | git annex setpresentkey --batch
git push origin git-annex
```

Fresh clone of on003825 now reports `whereis` with `[nemar-s3]` as a valid copy and `nemar dataset get` works end-to-end.

Out of scope

The deeper architectural gap, where `.context/pr_architecture.md` describes a staging->merge->final flow but the wiring uploads directly to final, is tracked in #403 and will be designed and implemented separately. The bugs in this PR exist in either model, so this fix is independent of that decision.

Test plan

  • `bun test test/git-annex-remote.test.ts` — 19 pass (5 new for filter + selector + fallback)
  • `bun test test/git-annex-precount.test.ts test/openneuro.test.ts` — 18 pass, no regressions
  • `bun run build` — bundles cleanly
  • Verified live on003825 recovery: fresh clone shows nemar-s3 in `whereis`
  • /review-pr after merge of this description

Bug A from #401: getAnnexS3Remotes returned every annex-S3 remote
in alphabetical order, so OpenNeuro-imported datasets like on003825
that inherit s3-PUBLIC alongside nemar-s3 had push uploads land in
the wrong bucket.

- getAnnexS3Remotes now skips remotes with annex-ignore=true.
- New selectAnnexS3Remote prefers the canonical nemar-s3 name, then
  the [nemar-s3] annex description, then first match.
- import-openneuro now marks inherited s3-PUBLIC/s3-PRIVATE as
  annex-ignore=true after building the URL map, so future imports
  filter cleanly even without the name preference.

Tests cover the filter, the preference, and the empty-list fallback.
Bugs B and C from #401:

Bug B: when requestUploadCredentials returned 403 'Only dataset
owner or collaborators can request upload credentials', the CLI
silently fell through to environment AWS credentials and then
aborted with a misleading 'log in with nemar auth login' message.
With --pr passed, git push had already succeeded but the PR was
never created, leaving the user with a remote branch and no PR.

- Detect the 403 collaborator error and auto-call
  requestDatasetAccess once, then retry credentials. Public
  datasets auto-approve so this is non-interactive in the
  common case.
- On retry failure, surface an actionable command
  ('nemar dataset request-access <id>') instead of the
  AWS env-vars hint.
- Print AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY status when
  falling back so the env-vars hint is concretely diagnosable.
- Track s3PushFailed and skip process.exit so PR creation still
  runs — users can retry the data step with --no-pr later.

Bug C: pushToGitHub at the start of the push command pushes the
git-annex branch, but git annex copy --to nemar-s3 then writes
new location records to the local git-annex branch that never
reach origin. Fresh clones would then see the data on S3 but
no record that it's there, leaving 'whereis' empty.

- After a successful copyToAnnexRemote, push the git-annex
  branch again so location records ship with the PR.
@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@PR404

Version: 0.8.9-PR404.e7a15ad
Commit: e7a15ad


🤖 Updated on each push.

Replace runCommand call (lib-internal helper not exported) with the
spawn pattern already used elsewhere in this file. Resolves CI tsc
'Cannot find name runCommand' on src/commands/dataset.ts:4323.
Code review (4 agents) on PR #404 surfaced:

dataset.ts push command:
- s3PushFailed was set but never read — restore process.exit(1) when
  data upload fails (caller scripts/CI relied on the exit code).
- clearAnnexCredentials moved into a finally so STS creds are wiped
  from .git/annex/creds/ on failure too; an interrupted upload would
  otherwise leave expired tokens that 403 subsequent operations.
- Distinguish transient backend errors from real permission denials
  when the auto request-access retry fails: only 4xx routes the user
  to 'nemar dataset request-access', 5xx tells them the credentials
  service is unavailable.
- git-annex push failure escalated from chalk.dim to chalk.yellow
  and now also sets s3PushFailed so the partial-state exit code is
  honest about clones not being able to locate the new files.
- Drop the 'Bug C:' comment prefix and a restating comment that
  added no information.

git-annex.ts:
- Extract markInheritedOpenNeuroRemotesIgnored helper so the same
  marking that import-openneuro does can be exercised in tests.
- Surface JSON parse errors in selectAnnexS3Remote via console.warn
  instead of silently falling through; malformed git-annex info is
  the kind of state worth telling the user about.

import-openneuro.ts:
- Move the annex-ignore marking outside the !options.skipData guard
  so --skip-data imports still get clean filtering on later pushes.
- Surface failed git config calls instead of silently leaving the
  remote half-ignored.

Tests:
- selectAnnexS3Remote description tiebreaker (alt-name remote
  described as [nemar-s3] beats alphabetically-first s3-PUBLIC)
- markInheritedOpenNeuroRemotesIgnored marks both inherited remotes
  and reports which were marked
- markInheritedOpenNeuroRemotesIgnored skips remotes that aren't
  configured (no-op on fresh repo)
- Regression guard: 'git annex copy' to a directory remote really
  does advance the local git-annex branch — the invariant the Bug C
  re-push depends on
@neuromechanist
neuromechanist merged commit 87557da into dev May 10, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-401-fix-push-s3 branch May 11, 2026 05:15

This branch was previously deployed

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