@warren-dean/code-kit 0.22.0 → 0.23.0

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.
@@ -17,6 +17,7 @@ import {
17
17
  chmodSync,
18
18
  existsSync,
19
19
  mkdirSync,
20
+ readdirSync,
20
21
  readFileSync,
21
22
  renameSync,
22
23
  statSync,
@@ -104,6 +105,40 @@ const mainRoot = (cwd) => {
104
105
  const stateDir = (root) => join(mainRoot(root), '.claude', 'state');
105
106
  const hubDown = (root) => readJson(join(stateDir(root), 'hub-down.json'), {});
106
107
 
108
+ /** The Claude Code session calling: code-kit passes it on, and Claude Code tells every command an agent runs. */
109
+ const sessionOf = () =>
110
+ String(process.env.CODE_KIT_SESSION ?? process.env.CLAUDE_CODE_SESSION_ID ?? '').replace(
111
+ /[^\w-]/g,
112
+ '',
113
+ ) || null;
114
+ const streamFileOf = (root, session) => join(stateDir(root), `hub-stream-${session}.json`);
115
+
116
+ /**
117
+ * A session's live link: up while its daemon runs and its event stream is connected. Each session holds
118
+ * its own, so one session's link never makes another look live.
119
+ */
120
+ function linkOf(root, session) {
121
+ const daemon = readJson(join(stateDir(root), `hub-daemon-${session}.json`));
122
+ const stream = readJson(streamFileOf(root, session), {});
123
+ return {
124
+ up: Boolean(daemon?.pid && alive(daemon.pid) && stream.connected),
125
+ why: stream.why ?? null,
126
+ };
127
+ }
128
+
129
+ /** The sessions on this checkout whose live link is up. */
130
+ function linkedSessions(root) {
131
+ let names = [];
132
+ try {
133
+ names = readdirSync(stateDir(root));
134
+ } catch {
135
+ // no state yet
136
+ }
137
+ return names
138
+ .map((n) => n.match(/^hub-daemon-([\w-]+)\.json$/)?.[1])
139
+ .filter((s) => s && linkOf(root, s).up);
140
+ }
141
+
107
142
  // --- talking to the hub ------------------------------------------------------------------------
108
143
 
109
144
  async function hub(method, path, body, { timeoutMs = 15_000 } = {}) {
@@ -153,8 +188,7 @@ export async function provide(op, input, root = process.cwd()) {
153
188
  // The daemon keeps the state fresh from the stream: read it from disk unless asked for fresh.
154
189
  if (op === 'state' && !input.fresh) {
155
190
  const kept = readJson(join(stateDir(root), 'team.json'));
156
- const live = readJson(join(stateDir(root), 'hub-stream.json'), {});
157
- if (kept?.hub && live.connected && Date.now() - Date.parse(kept.at) < 5 * 60_000)
191
+ if (kept?.hub && linkedSessions(root).length && Date.now() - Date.parse(kept.at) < 5 * 60_000)
158
192
  return named(kept, root);
159
193
  }
160
194
  const { team: _team, ...body } = input;
@@ -189,20 +223,23 @@ export async function provide(op, input, root = process.cwd()) {
189
223
  }
190
224
 
191
225
  /**
192
- * The state names what coordinates the team, for code-kit's Team pane: the hub, whether this session's
193
- * stream is live, and who this machine is signed in as.
226
+ * The state names what coordinates the team, for code-kit's Team pane: the hub, whether the calling
227
+ * session's own link is live (any session's, when no session is calling), and who this machine is
228
+ * signed in as.
194
229
  */
195
230
  function named(state, root) {
196
- const live = readJson(join(stateDir(root), 'hub-stream.json'), {});
231
+ const session = sessionOf();
232
+ const link = session ? linkOf(root, session) : { up: linkedSessions(root).length > 0 };
197
233
  return {
198
234
  ...state,
199
235
  provider: {
200
236
  name: 'code-kit hub',
201
- live: Boolean(live.connected),
237
+ live: link.up,
202
238
  account: credentials()?.login ?? null,
203
- detail: live.connected
239
+ detail: link.up
204
240
  ? 'claims, leases, intents and presence as they happen; every push checked for drift'
205
- : (live.why ?? 'not connected to its live updates; read on each refresh'),
241
+ : (link.why ??
242
+ "this session has no live link, so the team's state is read on each refresh; a session links as it starts"),
206
243
  },
207
244
  };
208
245
  }
@@ -449,6 +486,132 @@ async function connect(root) {
449
486
  console.log(
450
487
  `This repository now coordinates through the hub (${state.claims.length} claims, ${state.intents.length} open intents). Commit .claude/code-kit.json and .mcp.json so the team does too.`,
451
488
  );
489
+ // The session that ran connect links now, rather than at its next start.
490
+ const session = sessionOf();
491
+ if (session) {
492
+ startDaemon(root, session, claudeOf(), url);
493
+ console.log(
494
+ 'This session is linked to the hub now; other sessions link as they start, or run connect in them.',
495
+ );
496
+ }
497
+ }
498
+
499
+ /** The Claude Code process this command runs under, for the link to end with it; null when there's none. */
500
+ function claudeOf() {
501
+ let pid = process.ppid;
502
+ for (let i = 0; i < 8 && pid > 1; i += 1) {
503
+ const line = psOf(pid);
504
+ if (!line) return null;
505
+ if (/(^|\/)claude(\s|$)/.test(line.command)) return pid;
506
+ pid = line.ppid;
507
+ }
508
+ return null;
509
+ }
510
+ function psOf(pid) {
511
+ try {
512
+ const out = execFileSync('ps', ['-o', 'ppid=,command=', '-p', String(pid)], {
513
+ encoding: 'utf8',
514
+ }).trim();
515
+ const m = out.match(/^(\d+)\s+(.*)$/);
516
+ return m ? { ppid: Number(m[1]), command: m[2] } : null;
517
+ } catch {
518
+ return null;
519
+ }
520
+ }
521
+
522
+ /**
523
+ * `team provider status`: whether this repository is connected to the hub, one check a line, each failing
524
+ * one saying what to do, then the answer. Connected means this machine is signed in, the config here and
525
+ * on the base branch name the hub, and the hub can see the repository with nothing left to set up; live
526
+ * means the calling session's own link is up. True when connected.
527
+ */
528
+ async function status(root) {
529
+ const lines = [];
530
+ const check = (ok, text) => (lines.push(` ${ok ? '✓' : '✗'} ${text}`), ok);
531
+ const repo = repoOf(originOf(root));
532
+ const cred = credentials();
533
+ const url = (() => {
534
+ try {
535
+ return hubUrl();
536
+ } catch {
537
+ return null;
538
+ }
539
+ })();
540
+ const names = (config) => Boolean(url) && config?.team?.provider?.hub?.replace(/\/$/, '') === url;
541
+ const signed = check(
542
+ Boolean(cred?.token),
543
+ cred?.token
544
+ ? `this machine is signed in as ${cred.login}`
545
+ : "this machine isn't signed in: run /code-kit:team-init (it signs in through GitHub)",
546
+ );
547
+ const here = check(
548
+ names(readJson(join(root, '.claude', 'code-kit.json'))),
549
+ names(readJson(join(root, '.claude', 'code-kit.json')))
550
+ ? 'the config here names the hub'
551
+ : 'the config here doesn\'t name the hub: set "team": { "provider": { "hub": "' +
552
+ (url ?? '<the hub>') +
553
+ '" } } in .claude/code-kit.json',
554
+ );
555
+ const base =
556
+ git(root, 'symbolic-ref', '--short', 'refs/remotes/origin/HEAD') ??
557
+ (git(root, 'rev-parse', '--verify', '-q', 'origin/main') ? 'origin/main' : null);
558
+ const committed = (() => {
559
+ if (!base) return false;
560
+ try {
561
+ return names(JSON.parse(git(root, 'show', `${base}:.claude/code-kit.json`) ?? ''));
562
+ } catch {
563
+ return false;
564
+ }
565
+ })();
566
+ check(
567
+ committed,
568
+ committed
569
+ ? `${base}'s config names the hub, so the team's sessions use it`
570
+ : `${base ?? 'the base branch'}'s config doesn't name the hub yet (as last fetched): commit the team section and merge it, so the hub knows the tech lead and every teammate's session uses it`,
571
+ );
572
+ const mcp = readJson(join(root, '.mcp.json'), {})?.mcpServers?.['code-kit-hub'];
573
+ check(
574
+ Boolean(url) && mcp?.url === `${url}/mcp`,
575
+ mcp
576
+ ? "the project's MCP servers include the hub's"
577
+ : "the project's MCP servers don't include the hub's: run /code-kit:team-init, and commit .mcp.json",
578
+ );
579
+ let seen = false;
580
+ if (signed && repo && url) {
581
+ const state = await provide('state', { fresh: true }, root);
582
+ const setup = Array.isArray(state.setup) ? state.setup : [];
583
+ seen = check(
584
+ !state.unreachable && state.ok !== false,
585
+ state.unreachable || state.ok === false
586
+ ? `the hub can't use ${repo}: ${state.why}`
587
+ : `the hub sees ${repo}`,
588
+ );
589
+ // What the hub says is left to set up doesn't stop the connection: it's shown, not failed.
590
+ for (const line of seen ? setup : []) lines.push(` ! ${line}`);
591
+ } else if (!repo)
592
+ check(false, "this repository has no GitHub origin, so the hub can't be used here");
593
+ const session = sessionOf();
594
+ const linked = linkedSessions(root);
595
+ const live = session ? linkOf(root, session).up : linked.length > 0;
596
+ check(
597
+ live,
598
+ session
599
+ ? live
600
+ ? 'this session is linked: claims, intents and presence arrive as they happen'
601
+ : "this session isn't linked yet: run /code-kit:team-init, or start a new session, once the checks above pass"
602
+ : `${linked.length} session${linked.length === 1 ? '' : 's'} on this checkout linked`,
603
+ );
604
+ const connected = signed && here && committed && seen;
605
+ console.log(`code-kit hub${url ? ` (${url})` : ''}${repo ? `, for ${repo}` : ''}:`);
606
+ for (const line of lines) console.log(line);
607
+ console.log(
608
+ connected
609
+ ? live || !session
610
+ ? 'Connected.'
611
+ : "Connected, but this session isn't linked yet."
612
+ : 'Not connected yet: fix each ✗ above, in order.',
613
+ );
614
+ return connected;
452
615
  }
453
616
 
454
617
  // --- the daemon (HUB-5): the stream, the team's state on disk, presence --------------------------
@@ -457,7 +620,7 @@ async function daemon({ root, session, watch }) {
457
620
  const dir = stateDir(root);
458
621
  const repo = repoOf(originOf(root));
459
622
  if (!repo || !credentials()?.token) return;
460
- const streamFile = join(dir, 'hub-stream.json');
623
+ const streamFile = streamFileOf(root, session);
461
624
  const eventsFile = join(dir, 'team-events.jsonl');
462
625
  writeJson(join(dir, `hub-daemon-${session}.json`), { pid: process.pid, started: Date.now() });
463
626
  let lastId = readJson(streamFile, {}).lastId ?? 0;
@@ -498,10 +661,10 @@ async function daemon({ root, session, watch }) {
498
661
  stopping = true;
499
662
  clearInterval(tick);
500
663
  await hub('POST', `/v1/repos/${repo}/leave`, { session }, { timeoutMs: 3000 }).catch(() => {});
501
- writeJson(streamFile, { lastId, connected: false });
502
- try {
503
- unlinkSync(join(dir, `hub-daemon-${session}.json`));
504
- } catch {}
664
+ for (const file of [streamFile, join(dir, `hub-daemon-${session}.json`)])
665
+ try {
666
+ unlinkSync(file);
667
+ } catch {}
505
668
  process.exit(0);
506
669
  };
507
670
  process.on('SIGTERM', stop);
@@ -670,18 +833,7 @@ async function main(argv) {
670
833
  console.log('Signed out of code-kit hub on this machine.');
671
834
  return 0;
672
835
  }
673
- if (cmd === 'status') {
674
- const cred = credentials();
675
- console.log(
676
- cred?.token ? `Signed in to ${cred.url} as ${cred.login}.` : `Not signed in: run ${SIGN_IN}.`,
677
- );
678
- const repo = repoOf(originOf(process.cwd()));
679
- if (repo && cred?.token) {
680
- const live = readJson(join(stateDir(process.cwd()), 'hub-stream.json'), {});
681
- console.log(`${repo}: ${live.connected ? 'live' : (live.why ?? 'not connected')}`);
682
- }
683
- return 0;
684
- }
836
+ if (cmd === 'status') return status(process.cwd()).then((ok) => (ok ? 0 : 1));
685
837
  if (cmd === 'connect') return connect(process.cwd()).then(() => 0);
686
838
  if (cmd === 'drift-model') {
687
839
  const choice = args[0];
@@ -46,10 +46,12 @@ function runCheck(check, files, root, actor, skipped) {
46
46
  skipped.push({ check: check.name, command, where: where.replace(/^ in /, '') || null });
47
47
  return null; // not installed yet (fresh clone): the check can't run
48
48
  }
49
+ // #62: a check that ran out of time didn't fail; it's marked so a merge can try it again.
49
50
  if (res.error || res.status === null)
50
51
  return {
51
52
  check: check.name,
52
- title: `${check.name}: \`${command}\` did not finish${where} (${res.error?.code ?? res.signal}).`,
53
+ timedOut: res.error?.code === 'ETIMEDOUT' || res.signal === 'SIGTERM',
54
+ title: `${check.name}: \`${command}\` did not finish${where} (${res.error?.code ?? res.signal}) within ${check.timeoutSeconds ?? 240} seconds; \`timeoutSeconds\` on the check gives it longer.`,
53
55
  };
54
56
  if (res.status !== 0)
55
57
  return {
@@ -127,6 +127,8 @@ function delegateRules(given, protectedPaths = []) {
127
127
  return {
128
128
  protected: d.protected ?? [],
129
129
  merge: d.merge === true,
130
+ // #63: pushing the protected branch is the person's unless they delegate it: a push can deploy.
131
+ push: d.push === true,
130
132
  tests: d.tests === true,
131
133
  dependencies: d.dependencies
132
134
  ? {
@@ -236,6 +238,13 @@ export function approvalSettings(config) {
236
238
  'Who merges a reviewed branch into the base: merging is not releasing',
237
239
  ['lead', 'person'],
238
240
  ),
241
+ row(
242
+ 'approvals.push',
243
+ who(Boolean(rules?.push)),
244
+ 'person',
245
+ 'Who pushes the base to the remote once merged: a push can deploy, so it stays yours unless you say',
246
+ ['lead', 'person'],
247
+ ),
239
248
  row(
240
249
  'approvals.tests',
241
250
  who(Boolean(rules?.tests)),
@@ -253,6 +262,7 @@ export function withApprovalSetting(raw, config, key, text) {
253
262
  const delegate = {
254
263
  protected: [...(rules?.protected ?? [])],
255
264
  merge: Boolean(rules?.merge),
265
+ push: Boolean(rules?.push),
256
266
  tests: Boolean(rules?.tests),
257
267
  ...(deps ? { dependencies: { ...deps } } : {}),
258
268
  };
@@ -279,7 +289,7 @@ export function withApprovalSetting(raw, config, key, text) {
279
289
  lead
280
290
  ? (delegate.dependencies = deps ?? delegateRules({ dependencies: {} }).dependencies)
281
291
  : delete delegate.dependencies;
282
- else if (rule === 'merge' || rule === 'tests') delegate[rule] = lead;
292
+ else if (rule === 'merge' || rule === 'push' || rule === 'tests') delegate[rule] = lead;
283
293
  else if (config.protected.some((p) => p.approval === rule) && rule !== 'kit')
284
294
  delegate.protected = lead
285
295
  ? [...new Set([...delegate.protected, rule])]
@@ -381,6 +391,7 @@ export function validate(c) {
381
391
  (isObj(d) &&
382
392
  (d.protected === undefined || isList(d.protected)) &&
383
393
  [undefined, true, false].includes(d.merge) &&
394
+ [undefined, true, false].includes(d.push) &&
384
395
  [undefined, true, false].includes(d.tests) &&
385
396
  (dep === undefined ||
386
397
  (isObj(dep) &&
@@ -390,7 +401,7 @@ export function validate(c) {
390
401
  ([undefined, true, false].includes(dep.allowInstallScripts) ||
391
402
  (isList(dep.allowInstallScripts) &&
392
403
  dep.allowInstallScripts.every((k) => ['binary', 'build', 'other'].includes(k))))))),
393
- '"approvals.delegate" must be false, or { protected?: [approval names], merge?: true|false, tests?: true|false, dependencies?: { licences?, minWeeklyDownloads?, maxMonthsSinceRelease?, allowInstallScripts?: true|false|["binary","build","other"] } }',
404
+ '"approvals.delegate" must be false, or { protected?: [approval names], merge?: true|false, push?: true|false, tests?: true|false, dependencies?: { licences?, minWeeklyDownloads?, maxMonthsSinceRelease?, allowInstallScripts?: true|false|["binary","build","other"] } }',
394
405
  );
395
406
  need(
396
407
  c.branches === undefined || isList(c.branches?.protected),
@@ -82,7 +82,8 @@ function laneStandards(root, config, lane) {
82
82
  return lines.join('');
83
83
  }
84
84
 
85
- const marked = (body) => `${BEGIN(INSTRUCTIONS_VERSION)}\n${body.trim()}\n${END}`;
85
+ // With a blank line inside each marker, as Prettier writes markdown, so a project's format check passes (#77).
86
+ const marked = (body) => `${BEGIN(INSTRUCTIONS_VERSION)}\n\n${body.trim()}\n\n${END}`;
86
87
 
87
88
  /** The lead's section of CLAUDE.md, between its markers. */
88
89
  export function leadSection(root, config) {
@@ -13,6 +13,7 @@ import { decidedBy, laneOf, openRequests } from './rules.mjs';
13
13
  import { instructionsState } from './instructions.mjs';
14
14
  import { SECURITY_AGENT_FILE } from './security.mjs';
15
15
  import { laneClaims } from './laneclaims.mjs';
16
+ import { keptBecause, worktrees } from './worktrees.mjs';
16
17
  import { pickPerLane } from './schedule.mjs';
17
18
 
18
19
  export const DRAFT_FILE = '.claude/code-kit.draft.json';
@@ -77,7 +78,8 @@ const hasMarkdown = (dir) => {
77
78
  };
78
79
 
79
80
  /**
80
- * { step, why, args, then, attention, brief }. `brief`: the brief's open questions (ASK-4). `step` is a skill (spec-design, init, check, dispatch,
81
+ * { step, why, args, then, attention, brief, worktrees }. `brief`: the brief's open questions (ASK-4);
82
+ * `worktrees`: the merged ones still holding changes, { branch, story, changes, files }, for the person (LEAN-8). `step` is a skill (spec-design, init, check, dispatch,
81
83
  * review), `merge` (the head of the merge queue, where the lead merges), `wait` (lanes are
82
84
  * building) or `done`. `base(config)` resolves the branch lanes merge into.
83
85
  */
@@ -85,6 +87,8 @@ export function nextStep(root, base) {
85
87
  const attention = [];
86
88
  // ASK-4: the brief's open questions, for the person's Decisions tab.
87
89
  let briefQuestions = [];
90
+ // LEAN-8: merged worktrees still holding changes, for the person to keep or discard.
91
+ let holdingTrees = [];
88
92
  const result = (step, why, extra = {}) => ({
89
93
  step,
90
94
  why,
@@ -92,6 +96,7 @@ export function nextStep(root, base) {
92
96
  then: [],
93
97
  attention,
94
98
  brief: briefQuestions,
99
+ worktrees: holdingTrees,
95
100
  ...extra,
96
101
  });
97
102
  const isRepo = git(root, 'rev-parse', '--is-inside-work-tree') === 'true';
@@ -159,6 +164,26 @@ export function nextStep(root, base) {
159
164
  );
160
165
  }
161
166
 
167
+ // LEAN-8: worktrees that are done with are only disk space; prune takes them after each merge and as a
168
+ // session starts, and those still holding changes are the person's to keep or discard.
169
+ const trees = worktrees(root, config);
170
+ const leftovers = trees.filter((w) => !keptBecause(w) && !w.missing).length;
171
+ if (leftovers)
172
+ attention.push(
173
+ `${leftovers} worktree${leftovers === 1 ? ' is' : 's are'} done with; \`code-kit worktrees prune\` removes them, keeping their reports.`,
174
+ );
175
+ const holding = trees.filter((w) => w.kind === 'agent' && w.merged && !w.clean && !w.agentAtWork);
176
+ holdingTrees = holding.map((w) => ({
177
+ branch: w.branch,
178
+ story: w.story,
179
+ changes: w.changes,
180
+ files: w.files,
181
+ }));
182
+ if (holding.length)
183
+ attention.push(
184
+ `${holding.map((w) => w.branch).join(', ')} ${holding.length === 1 ? 'is' : 'are'} merged, but ${holding.length === 1 ? 'its worktree still holds' : 'their worktrees still hold'} uncommitted changes: the person keeps or discards each, from the band (discarding keeps them as a patch).`,
185
+ );
186
+
162
187
  const { specs, plan } = config.docs ?? {};
163
188
  if (!specs || !plan)
164
189
  return result(
@@ -256,12 +281,6 @@ export function nextStep(root, base) {
256
281
  attention.push(
257
282
  `${unbuildable.join(', ')} can't be dispatched until the plan's gaps are fixed.`,
258
283
  );
259
- // LEAN-6: worktrees whose story is merged and which hold nothing uncommitted are only disk space.
260
- const leftovers = mergedWorktrees(root, config);
261
- if (leftovers)
262
- attention.push(
263
- `${leftovers} worktree${leftovers === 1 ? ' is' : 's are'} merged and clean; \`code-kit worktrees prune\` removes them, keeping their reports.`,
264
- );
265
284
  // ONB-8: code-kit's own instructions to the lead and the lanes, brought up to date before the next step:
266
285
  // a newer code-kit's rules reach the agents only through them.
267
286
  const instructions = instructionsState(root, config);
@@ -524,23 +543,3 @@ export function missingSkills(root) {
524
543
  }
525
544
  return out;
526
545
  }
527
-
528
- /** LEAN-6: how many of the project's worktrees have a merged branch and nothing uncommitted. */
529
- function mergedWorktrees(root, config) {
530
- const list = git(root, 'worktree', 'list', '--porcelain');
531
- if (!list) return 0;
532
- const from = config.branches?.protected?.[0];
533
- if (!from || !git(root, 'rev-parse', '--verify', '-q', from)) return 0;
534
- let n = 0;
535
- for (const block of list.split('\n\n').slice(1)) {
536
- const path = /^worktree (.+)$/m.exec(block)?.[1];
537
- const branch = /^branch refs\/heads\/(.+)$/m.exec(block)?.[1];
538
- if (!path || !branch) continue;
539
- const merged =
540
- spawnSync('git', ['merge-base', '--is-ancestor', branch, from], { cwd: root }).status === 0;
541
- if (!merged) continue;
542
- const status = spawnSync('git', ['-C', path, 'status', '--porcelain'], { encoding: 'utf8' });
543
- if (status.status === 0 && !status.stdout.trim()) n++;
544
- }
545
- return n;
546
- }
@@ -1,10 +1,15 @@
1
- // What the npm registry says about a package, for the lead's delegated dependency approvals: the
2
- // licence, how often it's downloaded, when it was last released, and whether installing it runs
3
- // scripts. The rule itself is a pure function, so it's tested without the network.
1
+ // What the npm registry, or PyPI for a Python package, says about a package, for the lead's delegated
2
+ // dependency approvals: the licence, how often it's downloaded, when it was last released, and whether
3
+ // installing it runs scripts. The rule itself is a pure function, so it's tested without the network.
4
4
  import { DEPENDENCY_APPROVAL } from './dependencies.mjs';
5
5
 
6
6
  const REGISTRY = () => process.env.CODE_KIT_NPM_REGISTRY ?? 'https://registry.npmjs.org';
7
7
  const DOWNLOADS = () => process.env.CODE_KIT_NPM_DOWNLOADS ?? 'https://api.npmjs.org';
8
+ const PYPI = () => process.env.CODE_KIT_PYPI ?? 'https://pypi.org';
9
+ const PYPI_STATS = () => process.env.CODE_KIT_PYPI_STATS ?? 'https://pypistats.org';
10
+
11
+ /** The package managers whose packages come from PyPI. */
12
+ export const PYPI_MANAGERS = new Set(['pip', 'uv', 'poetry', 'pipenv', 'pdm']);
8
13
 
9
14
  /** The package an approval name stands for: `dep-@scope+pkg` → `@scope/pkg`. */
10
15
  export const packageOf = (approvalName) =>
@@ -69,6 +74,68 @@ export async function npmFacts(pkg) {
69
74
  }
70
75
  }
71
76
 
77
+ // PyPI's licence words as the SPDX ids a rule names: its classifiers first, then its free-text field.
78
+ const PYPI_LICENCES = [
79
+ [/\bMIT\b/i, 'MIT'],
80
+ [/\bApache\b.*2|Apache Software License/i, 'Apache-2.0'],
81
+ [/BSD.*3|3-Clause|New BSD|Modified BSD/i, 'BSD-3-Clause'],
82
+ [/BSD.*2|2-Clause|Simplified BSD/i, 'BSD-2-Clause'],
83
+ [/\bISC\b/i, 'ISC'],
84
+ [/Python Software Foundation|\bPSF\b/i, 'PSF-2.0'],
85
+ [/Mozilla Public License 2|\bMPL[- ]?2/i, 'MPL-2.0'],
86
+ [/\bLGPL/i, 'LGPL'],
87
+ [/\bAGPL/i, 'AGPL-3.0'],
88
+ [/\bGPL.*3|GPLv3/i, 'GPL-3.0'],
89
+ [/\bGPL.*2|GPLv2/i, 'GPL-2.0'],
90
+ ];
91
+
92
+ /** A PyPI project's licence as an SPDX id: its licence expression, else its classifiers, else its text. */
93
+ export function pypiLicence(info) {
94
+ if (info?.license_expression) return info.license_expression;
95
+ const words = [
96
+ ...(info?.classifiers ?? []).filter((c) => c.startsWith('License ::')),
97
+ ...(info?.license && info.license.length <= 80 ? [info.license] : []),
98
+ ];
99
+ for (const w of words) for (const [re, id] of PYPI_LICENCES) if (re.test(w)) return id;
100
+ return info?.license && info.license.length <= 40 ? info.license : 'unknown';
101
+ }
102
+
103
+ /**
104
+ * #70: the same facts for a Python package, from PyPI: a release with no wheel builds from source as it
105
+ * installs, which is what an npm install script that compiles is.
106
+ */
107
+ export async function pypiFacts(pkg) {
108
+ const name = encodeURIComponent(pkg.replace(/\[.*\]$/, '').toLowerCase());
109
+ try {
110
+ const meta = await fetch(`${PYPI()}/pypi/${name}/json`, {
111
+ headers: { accept: 'application/json' },
112
+ });
113
+ if (meta.status === 404) return { error: `${pkg} isn't on PyPI` };
114
+ if (!meta.ok) return { error: `PyPI answered ${meta.status} for ${pkg}` };
115
+ const doc = await meta.json();
116
+ const files = doc.urls ?? [];
117
+ const stats = await fetch(`${PYPI_STATS()}/api/packages/${name}/recent`, {
118
+ headers: { accept: 'application/json' },
119
+ });
120
+ const weeklyDownloads = stats.ok ? ((await stats.json()).data?.last_week ?? 0) : 0;
121
+ const wheel = files.some((f) => f.packagetype === 'bdist_wheel');
122
+ return {
123
+ licence: pypiLicence(doc.info),
124
+ weeklyDownloads,
125
+ lastRelease:
126
+ files
127
+ .map((f) => f.upload_time_iso_8601)
128
+ .filter(Boolean)
129
+ .sort()
130
+ .at(-1) ?? null,
131
+ installScripts: files.length > 0 && !wheel,
132
+ installScriptKind: files.length > 0 && !wheel ? 'build' : null,
133
+ };
134
+ } catch (e) {
135
+ return { error: `couldn't reach PyPI (${e.message})` };
136
+ }
137
+ }
138
+
72
139
  /** Why `facts` falls outside `rule`, as sentences; empty when it's within it. */
73
140
  export function dependencyRuleProblems(facts, rule, now = Date.now()) {
74
141
  if (facts.error) return [facts.error];
@@ -7,7 +7,16 @@
7
7
  // them the new-issue link to open; a report someone already filed gets a comment instead of a second issue.
8
8
  import { createHash } from 'node:crypto';
9
9
  import { execFileSync } from 'node:child_process';
10
- import { appendFileSync, mkdirSync, readFileSync, realpathSync } from 'node:fs';
10
+ import {
11
+ appendFileSync,
12
+ closeSync,
13
+ mkdirSync,
14
+ openSync,
15
+ readFileSync,
16
+ realpathSync,
17
+ statSync,
18
+ unlinkSync,
19
+ } from 'node:fs';
11
20
  import { homedir, platform, release } from 'node:os';
12
21
  import { dirname, join } from 'node:path';
13
22
  import { fileURLToPath } from 'node:url';
@@ -21,6 +30,47 @@ const PLUGIN = JSON.parse(
21
30
  export const REPO = PLUGIN.repository.replace(/^https:\/\/github\.com\//, '').replace(/\.git$/, '');
22
31
 
23
32
  const fileOf = (root) => join(projectWorktrees(root)[0] ?? root, REPORTS_FILE);
33
+ /** REPORT-3: held while a report is being sent, so a second Send can't file it again. */
34
+ const sendingOf = (root, id) => join(dirname(fileOf(root)), `report-sending-${id}`);
35
+ /** A send that's held its claim this long died before it finished, and the report is a draft again. */
36
+ const SENDING_MS = 5 * 60 * 1000;
37
+
38
+ function sendingSince(root, id, now = Date.now()) {
39
+ try {
40
+ const at = statSync(sendingOf(root, id)).mtimeMs;
41
+ return now - at < SENDING_MS ? at : null;
42
+ } catch {
43
+ return null;
44
+ }
45
+ }
46
+
47
+ /** Claims the send of `id`, or says it's being sent already: one claim, whichever process asks first. */
48
+ function claimSend(root, id, now = Date.now()) {
49
+ const file = sendingOf(root, id);
50
+ mkdirSync(dirname(file), { recursive: true });
51
+ for (let tries = 0; tries < 2; tries++) {
52
+ try {
53
+ closeSync(openSync(file, 'wx'));
54
+ return true;
55
+ } catch {
56
+ if (sendingSince(root, id, now) !== null) return false;
57
+ try {
58
+ unlinkSync(file);
59
+ } catch {
60
+ // taken by another send meanwhile
61
+ }
62
+ }
63
+ }
64
+ return false;
65
+ }
66
+
67
+ const releaseSend = (root, id) => {
68
+ try {
69
+ unlinkSync(sendingOf(root, id));
70
+ } catch {
71
+ // gone already
72
+ }
73
+ };
24
74
 
25
75
  function records(root) {
26
76
  try {
@@ -86,8 +136,8 @@ function bodyOf(r) {
86
136
  }
87
137
 
88
138
  /**
89
- * Every report, oldest first: { id, at, by, title, body, fingerprint, state: 'draft' | 'sent' | 'discarded',
90
- * sent: { at, url, comment } | null }.
139
+ * Every report, oldest first: { id, at, by, title, body, fingerprint, state: 'draft' | 'sending' | 'sent' |
140
+ * 'discarded', sent: { at, url, comment } | null }. `sending`: a Send is filing it now.
91
141
  */
92
142
  export function readReports(root) {
93
143
  const byId = new Map();
@@ -102,7 +152,9 @@ export function readReports(root) {
102
152
  });
103
153
  if (r.kind === 'discarded') d.state = 'discarded';
104
154
  }
105
- return [...byId.values()].map(({ kind, ...r }) => r);
155
+ return [...byId.values()].map(({ kind, ...r }) =>
156
+ r.state === 'draft' && sendingSince(root, r.id) !== null ? { ...r, state: 'sending' } : r,
157
+ );
106
158
  }
107
159
 
108
160
  export const draftReports = (root) => readReports(root).filter((r) => r.state === 'draft');
@@ -175,9 +227,24 @@ const gh = (args) =>
175
227
  * it already, otherwise a new issue; without a working `gh`, nothing is sent and the link is returned.
176
228
  */
177
229
  export function sendReport(root, id, now = Date.now()) {
178
- const r = readReports(root).find((x) => x.id === id);
179
- if (!r) return { ok: false, why: `There's no report ${id}.` };
180
- if (r.state !== 'draft') return { ok: false, why: `${id} was ${r.state} already.` };
230
+ const before = readReports(root).find((x) => x.id === id);
231
+ if (!before) return { ok: false, why: `There's no report ${id}.` };
232
+ if (before.state === 'sending') return { ok: false, why: `${id} is being sent already.` };
233
+ if (before.state !== 'draft') return { ok: false, why: `${id} was ${before.state} already.` };
234
+ if (!claimSend(root, id, now)) return { ok: false, why: `${id} is being sent already.` };
235
+ try {
236
+ // Another send may have finished between the read and the claim.
237
+ const r = readReports(root).find((x) => x.id === id);
238
+ if (r.sent || r.state === 'discarded')
239
+ return { ok: false, why: `${id} was ${r.sent ? 'sent' : 'discarded'} already.` };
240
+ return fileIssue(root, r, now);
241
+ } finally {
242
+ releaseSend(root, id);
243
+ }
244
+ }
245
+
246
+ function fileIssue(root, r, now) {
247
+ const { id } = r;
181
248
  let url;
182
249
  let comment = false;
183
250
  try {
@@ -240,6 +307,8 @@ export function sendReport(root, id, now = Date.now()) {
240
307
  export function discardReport(root, id, now = Date.now()) {
241
308
  const r = readReports(root).find((x) => x.id === id);
242
309
  if (!r) return { ok: false, why: `There's no report ${id}.` };
310
+ if (r.state === 'sending')
311
+ return { ok: false, why: `${id} is being sent, so it can't be discarded.` };
243
312
  if (r.state !== 'draft') return { ok: false, why: `${id} was ${r.state} already.` };
244
313
  append(root, { kind: 'discarded', id, at: new Date(now).toISOString() });
245
314
  return { ok: true };