@veda-hq/niyam 0.6.22 → 0.6.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- import { BASE_ENV, agentHere, anyLogin, either, loadSettings, lockScript, sameLogin, pathsFor, sessionNames, sessionRecord, sessionFileIs, } from './agent.js';
1
+ import { BASE_ENV, agentHere, exportLines, anyLogin, either, loadSettings, lockScript, sameLogin, pathsFor, sessionNames, sessionRecord, sessionFileIs, } from './agent.js';
2
2
  import { AGENT_VAR } from './names.js';
3
- import { cloudLogin, cloudToken, ranFromSetup, repoReleases } from './cloud.js';
3
+ import { SIGNING_KEY_VAR, cloudLogin, cloudSigning, cloudToken, ranFromSetup, repoReleases } from './cloud.js';
4
4
  import { CLOUD_SETUP_PATH } from './onboard-pull.js';
5
5
  import { PRE_PUSH_HOOK, prePushHook } from './onboard.js';
6
6
  import { packageVersion } from './version.js';
@@ -117,7 +117,9 @@ function recordFor(sys) {
117
117
  * identity is the account claude.ai's GitHub connection uses, which GitHub
118
118
  * names through the session's proxy. When that is the agent, the lock the
119
119
  * settings' `env` gave the session is lifted, so gh and git reach GitHub
120
- * through the proxy, and the session is marked as the agent's for the guard
120
+ * through the proxy, the agent becomes the author, committer and signer of
121
+ * its commits when the environment holds its cloud signing key
122
+ * (`cloudCommits`), and the session is marked as the agent's for the guard
121
123
  * and the pre-push hook, which it writes, since a cloud clone has none of
122
124
  * the owner's. Otherwise GitHub stays locked out and the session is told to
123
125
  * stop: it would act as someone else.
@@ -171,7 +173,32 @@ async function cloudStart(sys) {
171
173
  : hook.state === 'outside'
172
174
  ? ` Its pre-push hook ${hook.path} is outside the repository and was not checked, so only the pull request's checks may hold the work rules.`
173
175
  : '';
174
- return { line: `niyam: this cloud session works as ${asked.login} on GitHub.${unchecked}`, agent: asked.login };
176
+ const signed = await cloudCommits(sys, root, envFile, asked);
177
+ return { line: `niyam: this cloud session works as ${asked.login} on GitHub.${unchecked}${signed}`, agent: asked.login };
178
+ }
179
+ /**
180
+ * Makes the agent the author, committer and signer of a cloud session's
181
+ * commits, as a local session's file does, over the cloud image's own
182
+ * identity, Claude's: `cloudSigning`'s variables, appended to CLAUDE_ENV_FILE
183
+ * after the lock is lifted, which reaches every command Claude Code runs and
184
+ * leaves the clone's git config alone. The key's variable is unset there, so
185
+ * commands do not carry it in their environment. A warning only, said after
186
+ * the line: when any of it fails, the session works on as the agent and its
187
+ * commits keep the image's identity, which PR identity passes.
188
+ */
189
+ async function cloudCommits(sys, root, envFile, asked) {
190
+ const keeps = (why) => ` Its commits keep the cloud image's identity, Claude's: ${why}.`;
191
+ const signing = await cloudSigning(sys, root, asked.login, asked.id);
192
+ if ('problem' in signing)
193
+ return keeps(signing.problem);
194
+ try {
195
+ sys.append(envFile, `unset ${SIGNING_KEY_VAR}\n${exportLines(signing.vars).join('\n')}\n`);
196
+ }
197
+ catch (error) {
198
+ // fail-open: nothing of the signing reached the env file, so the session keeps the image's identity, and says why.
199
+ return keeps(`writing ${asked.login}'s signing to CLAUDE_ENV_FILE failed (${message(error)})`);
200
+ }
201
+ return ` Its commits are ${asked.login}'s, signed with ${SIGNING_KEY_VAR}.`;
175
202
  }
176
203
  function start(sys) {
177
204
  let local;
package/dist/work.js CHANGED
@@ -527,11 +527,16 @@ export async function prePush(sys, io, refLines, url) {
527
527
  await moveBoard(sys, io, token, slug, slice);
528
528
  return report(io, 'pre-push', problems);
529
529
  }
530
+ /** How a Claude cloud session's GitHub proxy refuses a path outside `repos/{owner}/{repo}/…`, such as an org's Projects. */
531
+ const CLOUD_PROXY_REFUSAL = /sessions are bound to their configured repositories/i;
530
532
  /**
531
533
  * Moves the board on for slice `slice`, which a push is starting to build,
532
- * with the board feature on, through GitHub's REST API, which a Claude
533
- * cloud session's proxy lets through; a board it cannot read, or a move
534
- * GitHub refuses, is said and does not stop the push.
534
+ * with the board feature on, through GitHub's REST API; a board it cannot
535
+ * read, or a move GitHub refuses, is said and does not stop the push. A
536
+ * Claude cloud session's proxy lets through only `repos/{owner}/{repo}/…`,
537
+ * never an org's or a user's Projects, so there the push only says that the
538
+ * shared workflow's Slice started job moves the slice when its pull request
539
+ * opens.
535
540
  */
536
541
  async function moveBoard(sys, io, token, slug, slice) {
537
542
  try {
@@ -539,7 +544,10 @@ async function moveBoard(sys, io, token, slug, slice) {
539
544
  io.err(line);
540
545
  }
541
546
  catch (error) {
542
- io.err(`! board: could not read where #${slice} and its work item are on the board, so nothing moved (${error.message}); the push goes on`);
547
+ const message = error.message;
548
+ io.err(CLOUD_PROXY_REFUSAL.test(message)
549
+ ? `- board: this cloud session cannot reach the board; niyam's workflow moves #${slice} when its pull request opens`
550
+ : `! board: could not read where #${slice} and its work item are on the board, so nothing moved (${message}); the push goes on`);
543
551
  }
544
552
  }
545
553
  /** Throws for a write to an issue that GitHub refused, saying how to fix a workflow that grants no issues: write. */
@@ -651,6 +659,72 @@ export async function prMerged(sys, io) {
651
659
  }
652
660
  return 0;
653
661
  }
662
+ /**
663
+ * Moves the board on for the slice a pull request names as it opens or
664
+ * reopens, run by the shared workflow's Slice started job: the move a push
665
+ * makes from the desktop, which a Claude cloud session's push cannot, since
666
+ * its proxy never reaches an org's Projects. It moves nothing unless the
667
+ * pull request names one slice, the board and work-rules features are on,
668
+ * and the slice passes the work rules, which it reads with the workflow's
669
+ * GH_TOKEN. A workflow's GITHUB_TOKEN cannot write a Project, so the board
670
+ * moves with NIYAM_BOARD_TOKEN, minted from the board GitHub App. Without
671
+ * that token, or when GitHub refuses, it says so and passes: the board never
672
+ * fails a pull request.
673
+ */
674
+ export async function prStarted(sys, io) {
675
+ const { GITHUB_EVENT_PATH: eventPath, GITHUB_REPOSITORY: slug, GH_TOKEN: token, NIYAM_BOARD_TOKEN: board } = sys.env;
676
+ if (!eventPath || !slug || !token) {
677
+ throw new Error('pr-started runs in GitHub Actions: GITHUB_EVENT_PATH, GITHUB_REPOSITORY and GH_TOKEN must be set');
678
+ }
679
+ const event = JSON.parse(sys.read(eventPath));
680
+ const pull = event.pull_request;
681
+ if (!pull) {
682
+ io.out('pr-started: not a pull request; nothing to move');
683
+ return 0;
684
+ }
685
+ const notAdopted = notAdoptedLine(sys, sys.cwd, 'pr-started');
686
+ if (notAdopted) {
687
+ io.out(notAdopted);
688
+ return 0;
689
+ }
690
+ for (const feature of ['work-rules', 'board']) {
691
+ if (!isOn(sys, sys.cwd, feature)) {
692
+ io.out(offLine('pr-started', feature));
693
+ return 0;
694
+ }
695
+ }
696
+ if (await isPersonal(sys, slug, token)) {
697
+ io.out(offLine('pr-started', 'work-rules', PERSONAL_REPO));
698
+ return 0;
699
+ }
700
+ const slices = slicesNamed(pull.body ?? '');
701
+ if (slices.length !== 1) {
702
+ io.out(`pr-started: #${pull.number} names ${slices.length === 0 ? 'no slice' : `${slices.length} slices`}; nothing moves on the board`);
703
+ return 0;
704
+ }
705
+ const [slice] = slices;
706
+ if (!board) {
707
+ io.out(`pr-started: no board app token here (the NIYAM_BOARD_APP_ID and NIYAM_BOARD_APP_KEY secrets are not passed, or GitHub would not make the token), so #${slice} moves on the board only from a desktop push`);
708
+ return 0;
709
+ }
710
+ try {
711
+ const problems = await sliceProblems(sys, token, slug, slice, loadSettings(sys, sys.cwd), { pull: pull.number, states: statesNamed(pull.body ?? '') });
712
+ if (problems.length > 0) {
713
+ io.out(`pr-started: #${slice} may not be built yet (Work rules says why), so nothing moves on the board`);
714
+ return 0;
715
+ }
716
+ const moved = await boardStarted(sys, board, slug, slice);
717
+ for (const line of moved)
718
+ io.out(line);
719
+ if (moved.length === 0)
720
+ io.out(`pr-started: #${slice} and its work item are where they belong on the board already`);
721
+ }
722
+ catch (error) {
723
+ // fail-open: the board never fails a pull request; Work rules judges the slice, and the line says what GitHub refused.
724
+ io.out(`! board: nothing moved for #${slice} (${error.message}); the pull request's checks go on`);
725
+ }
726
+ return 0;
727
+ }
654
728
  /** The newest of `comments`, which is not empty: GitHub lists comments oldest first. */
655
729
  function newest(comments) {
656
730
  return comments[comments.length - 1];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veda-hq/niyam",
3
- "version": "0.6.22",
3
+ "version": "0.6.24",
4
4
  "description": "Work rules for repos that track work in GitHub Issues: setup, agent identity, push and PR checks.",
5
5
  "license": "MIT",
6
6
  "repository": {